sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 1 | /* This is single source file, bootstrap version of Jim Tcl. See http://jim.tcl.tk/ */ |
| 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 |
| 8 | #define TCL_LIBRARY "." |
| 9 | #define jim_ext_bootstrap |
| 10 | #define jim_ext_aio |
| 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) |
| 21 | #define TCL_PLATFORM_OS "windows" |
| 22 | #define TCL_PLATFORM_PLATFORM "windows" |
| 23 | #define TCL_PLATFORM_PATH_SEPARATOR ";" |
| 24 | #define HAVE_MKDIR_ONE_ARG |
| 25 | #define HAVE_SYSTEM |
| 26 | #elif defined(__MINGW32__) |
| 27 | #define TCL_PLATFORM_OS "mingw" |
| 28 | #define TCL_PLATFORM_PLATFORM "windows" |
| 29 | #define TCL_PLATFORM_PATH_SEPARATOR ";" |
| 30 | #define HAVE_MKDIR_ONE_ARG |
| 31 | #define HAVE_SYSTEM |
| 32 | #define HAVE_SYS_TIME_H |
| 33 | #define HAVE_DIRENT_H |
| 34 | #define HAVE_UNISTD_H |
| 35 | #define HAVE_UMASK |
| 36 | #include <sys/stat.h> |
| 37 | #ifndef S_IRWXG |
| 38 | #define S_IRWXG 0 |
| 39 | #endif |
| 40 | #ifndef S_IRWXO |
| 41 | #define S_IRWXO 0 |
| 42 | #endif |
| 43 | #else |
| 44 | #define TCL_PLATFORM_OS "unknown" |
| 45 | #define TCL_PLATFORM_PLATFORM "unix" |
| 46 | #define TCL_PLATFORM_PATH_SEPARATOR ":" |
| 47 | #ifdef _MINIX |
| 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 |
| 63 | #define HAVE_DIRENT_H |
| 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 | |
| 75 | #ifdef __cplusplus |
| 76 | extern "C" { |
| 77 | #endif |
| 78 | |
| 79 | |
| 80 | #if defined(_WIN32) || defined(WIN32) |
| 81 | |
| 82 | #define HAVE_DLOPEN |
| 83 | void *dlopen(const char *path, int mode); |
| 84 | int dlclose(void *handle); |
| 85 | void *dlsym(void *handle, const char *symbol); |
| 86 | char *dlerror(void); |
| 87 | |
| 88 | |
| 89 | #if defined(__MINGW32__) |
| 90 | #define JIM_SPRINTF_DOUBLE_NEEDS_FIX |
| 91 | #endif |
| 92 | |
| 93 | #ifdef _MSC_VER |
| 94 | |
| 95 | |
| 96 | #if _MSC_VER >= 1000 |
| 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) |
| 110 | #endif |
| 111 | #define JIM_WIDE_MIN LLONG_MIN |
| 112 | #define JIM_WIDE_MAX LLONG_MAX |
| 113 | #define JIM_WIDE_MODIFIER "I64d" |
| 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; |
| 125 | }; |
| 126 | |
| 127 | typedef struct DIR { |
| 128 | long handle; |
| 129 | struct _finddata_t info; |
| 130 | struct dirent result; |
| 131 | char *name; |
| 132 | } DIR; |
| 133 | |
| 134 | DIR *opendir(const char *name); |
| 135 | int closedir(DIR *dir); |
| 136 | struct dirent *readdir(DIR *dir); |
| 137 | |
| 138 | #endif |
| 139 | |
| 140 | #endif |
| 141 | |
| 142 | #ifdef __cplusplus |
| 143 | } |
| 144 | #endif |
| 145 | |
| 146 | #endif |
| 147 | #ifndef UTF8_UTIL_H |
| 148 | #define UTF8_UTIL_H |
| 149 | |
| 150 | #ifdef __cplusplus |
| 151 | extern "C" { |
| 152 | #endif |
| 153 | |
| 154 | |
| 155 | |
| 156 | #define MAX_UTF8_LEN 4 |
| 157 | |
| 158 | int utf8_fromunicode(char *p, unsigned uc); |
| 159 | |
| 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 | |
| 178 | #ifdef __cplusplus |
| 179 | } |
| 180 | #endif |
| 181 | |
| 182 | #endif |
| 183 | |
| 184 | #ifndef __JIM__H |
| 185 | #define __JIM__H |
| 186 | |
| 187 | #ifdef __cplusplus |
| 188 | extern "C" { |
| 189 | #endif |
| 190 | |
| 191 | #include <time.h> |
| 192 | #include <limits.h> |
| 193 | #include <stdlib.h> |
| 194 | #include <stdarg.h> |
| 195 | |
| 196 | |
| 197 | #ifndef HAVE_NO_AUTOCONF |
| 198 | #endif |
| 199 | |
| 200 | |
| 201 | |
| 202 | #ifndef jim_wide |
| 203 | # ifdef HAVE_LONG_LONG |
| 204 | # define jim_wide long long |
| 205 | # ifndef LLONG_MAX |
| 206 | # define LLONG_MAX 9223372036854775807LL |
| 207 | # endif |
| 208 | # ifndef LLONG_MIN |
| 209 | # define LLONG_MIN (-LLONG_MAX - 1LL) |
| 210 | # endif |
| 211 | # define JIM_WIDE_MIN LLONG_MIN |
| 212 | # define JIM_WIDE_MAX LLONG_MAX |
| 213 | # else |
| 214 | # define jim_wide long |
| 215 | # define JIM_WIDE_MIN LONG_MIN |
| 216 | # define JIM_WIDE_MAX LONG_MAX |
| 217 | # endif |
| 218 | |
| 219 | |
| 220 | # ifdef HAVE_LONG_LONG |
| 221 | # define JIM_WIDE_MODIFIER "lld" |
| 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 |
| 250 | |
| 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 | |
| 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" |
| 287 | #define JIM_INTERACTIVE "tcl_interactive" |
| 288 | |
| 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; |
| 300 | union { |
| 301 | void *val; |
| 302 | int intval; |
| 303 | } u; |
| 304 | struct Jim_HashEntry *next; |
| 305 | } Jim_HashEntry; |
| 306 | |
| 307 | typedef struct Jim_HashTableType { |
| 308 | unsigned int (*hashFunction)(const void *key); |
| 309 | void *(*keyDup)(void *privdata, const void *key); |
| 310 | void *(*valDup)(void *privdata, const void *obj); |
| 311 | int (*keyCompare)(void *privdata, const void *key1, const void *key2); |
| 312 | void (*keyDestructor)(void *privdata, void *key); |
| 313 | void (*valDestructor)(void *privdata, void *obj); |
| 314 | } Jim_HashTableType; |
| 315 | |
| 316 | typedef struct Jim_HashTable { |
| 317 | Jim_HashEntry **table; |
| 318 | const Jim_HashTableType *type; |
| 319 | void *privdata; |
| 320 | unsigned int size; |
| 321 | unsigned int sizemask; |
| 322 | unsigned int used; |
| 323 | unsigned int collisions; |
| 324 | unsigned int uniq; |
| 325 | } Jim_HashTable; |
| 326 | |
| 327 | typedef struct Jim_HashTableIterator { |
| 328 | Jim_HashTable *ht; |
| 329 | Jim_HashEntry *entry, *nextEntry; |
| 330 | int index; |
| 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 | |
| 400 | int intValue; |
| 401 | |
| 402 | double doubleValue; |
| 403 | |
| 404 | void *ptr; |
| 405 | |
| 406 | struct { |
| 407 | void *ptr1; |
| 408 | void *ptr2; |
| 409 | } twoPtrValue; |
| 410 | |
| 411 | struct { |
| 412 | void *ptr; |
| 413 | int int1; |
| 414 | int int2; |
| 415 | } ptrIntValue; |
| 416 | |
| 417 | struct { |
| 418 | struct Jim_VarVal *vv; |
| 419 | unsigned long callFrameId; |
| 420 | int global; |
| 421 | } varValue; |
| 422 | |
| 423 | struct { |
| 424 | struct Jim_Obj *nsObj; |
| 425 | struct Jim_Cmd *cmdPtr; |
| 426 | unsigned long procEpoch; |
| 427 | } cmdValue; |
| 428 | |
| 429 | struct { |
| 430 | struct Jim_Obj **ele; |
| 431 | int len; |
| 432 | int maxLen; |
| 433 | } listValue; |
| 434 | |
| 435 | struct Jim_Dict *dictValue; |
| 436 | |
| 437 | struct { |
| 438 | int maxLength; |
| 439 | int charLength; |
| 440 | } strValue; |
| 441 | |
| 442 | struct { |
| 443 | unsigned long id; |
| 444 | struct Jim_Reference *refPtr; |
| 445 | } refValue; |
| 446 | |
| 447 | struct { |
| 448 | struct Jim_Obj *fileNameObj; |
| 449 | int lineNumber; |
| 450 | } sourceValue; |
| 451 | |
| 452 | struct { |
| 453 | struct Jim_Obj *varNameObjPtr; |
| 454 | struct Jim_Obj *indexObjPtr; |
| 455 | } dictSubstValue; |
| 456 | struct { |
| 457 | int line; |
| 458 | int argc; |
| 459 | } scriptLineValue; |
| 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 | |
| 493 | |
| 494 | struct Jim_Interp; |
| 495 | |
| 496 | typedef void (Jim_FreeInternalRepProc)(struct Jim_Interp *interp, |
| 497 | struct Jim_Obj *objPtr); |
| 498 | typedef void (Jim_DupInternalRepProc)(struct Jim_Interp *interp, |
| 499 | struct Jim_Obj *srcPtr, Jim_Obj *dupPtr); |
| 500 | typedef void (Jim_UpdateStringProc)(struct Jim_Obj *objPtr); |
| 501 | |
| 502 | typedef struct Jim_ObjType { |
| 503 | const char *name; |
| 504 | Jim_FreeInternalRepProc *freeIntRepProc; |
| 505 | Jim_DupInternalRepProc *dupIntRepProc; |
| 506 | Jim_UpdateStringProc *updateStringProc; |
| 507 | int flags; |
| 508 | } Jim_ObjType; |
| 509 | |
| 510 | |
| 511 | #define JIM_TYPE_NONE 0 |
| 512 | #define JIM_TYPE_REFERENCES 1 |
| 513 | |
| 514 | |
| 515 | |
| 516 | typedef struct Jim_CallFrame { |
| 517 | unsigned long id; |
| 518 | int level; |
| 519 | struct Jim_HashTable vars; |
| 520 | struct Jim_HashTable *staticVars; |
| 521 | struct Jim_CallFrame *parent; |
| 522 | Jim_Obj *const *argv; |
| 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 | |
| 533 | |
| 534 | typedef struct Jim_EvalFrame { |
| 535 | Jim_CallFrame *framePtr; |
| 536 | int level; |
| 537 | int procLevel; |
| 538 | struct Jim_Cmd *cmd; |
| 539 | struct Jim_EvalFrame *parent; |
| 540 | Jim_Obj *const *argv; |
| 541 | int argc; |
| 542 | Jim_Obj *scriptObj; |
| 543 | } Jim_EvalFrame; |
| 544 | |
| 545 | typedef struct Jim_VarVal { |
| 546 | Jim_Obj *objPtr; |
| 547 | struct Jim_CallFrame *linkFramePtr; |
| 548 | int refCount; |
| 549 | } Jim_VarVal; |
| 550 | |
| 551 | |
| 552 | typedef int Jim_CmdProc(struct Jim_Interp *interp, int argc, |
| 553 | Jim_Obj *const *argv); |
| 554 | typedef void Jim_DelCmdProc(struct Jim_Interp *interp, void *privData); |
| 555 | |
| 556 | typedef struct Jim_Dict { |
| 557 | struct JimDictHashEntry { |
| 558 | int offset; |
| 559 | unsigned hash; |
| 560 | } *ht; |
| 561 | unsigned int size; |
| 562 | unsigned int sizemask; |
| 563 | unsigned int uniq; |
| 564 | Jim_Obj **table; |
| 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; |
| 596 | Jim_HashTable *staticVars; |
| 597 | int argListLen; |
| 598 | int reqArity; |
| 599 | int optArity; |
| 600 | int argsPos; |
| 601 | int upcall; |
| 602 | struct Jim_ProcArg { |
| 603 | Jim_Obj *nameObjPtr; |
| 604 | Jim_Obj *defaultObjPtr; |
| 605 | } *arglist; |
| 606 | Jim_Obj *nsObj; |
| 607 | } proc; |
| 608 | } u; |
| 609 | } Jim_Cmd; |
| 610 | |
| 611 | |
| 612 | typedef struct Jim_PrngState { |
| 613 | unsigned char sbox[256]; |
| 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; |
| 624 | int returnCode; |
| 625 | int returnLevel; |
| 626 | int exitCode; |
| 627 | long id; |
| 628 | int signal_level; |
| 629 | jim_wide sigmask; |
| 630 | int (*signal_set_result)(struct Jim_Interp *interp, jim_wide sigmask); |
| 631 | Jim_CallFrame *framePtr; |
| 632 | Jim_CallFrame *topFramePtr; |
| 633 | struct Jim_HashTable commands; |
| 634 | unsigned long procEpoch; /* Incremented every time the result |
| 635 | of procedures names lookup caching |
| 636 | may no longer be valid. */ |
| 637 | unsigned long callFrameEpoch; /* Incremented every time a new |
| 638 | callframe is created. This id is used for the |
| 639 | 'ID' field contained in the Jim_CallFrame |
| 640 | structure. */ |
| 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; |
| 654 | struct Jim_HashTable references; |
| 655 | unsigned long lastCollectId; /* reference max Id of the last GC |
| 656 | execution. It's set to ~0 while the collection |
| 657 | is running as sentinel to avoid to recursive |
| 658 | calls via the [collect] command inside |
| 659 | finalizers. */ |
| 660 | jim_wide lastCollectTime; |
| 661 | Jim_Obj *stackTrace; |
| 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; |
| 673 | int oldCmdCacheSize; |
| 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 | |
| 709 | #define JIM_REFERENCE_TAGLEN 7 /* The tag is fixed-length, because the reference |
| 710 | string representation must be fixed length. */ |
| 711 | typedef struct Jim_Reference { |
| 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 |
| 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 |
| 1236 | |
| 1237 | #ifndef JIM_SUBCMD_H |
| 1238 | #define JIM_SUBCMD_H |
| 1239 | |
| 1240 | |
| 1241 | #ifdef __cplusplus |
| 1242 | extern "C" { |
| 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 | |
| 1257 | typedef struct { |
| 1258 | const char *cmd; |
| 1259 | const char *args; |
| 1260 | jim_subcmd_function *function; |
| 1261 | short minargs; |
| 1262 | short maxargs; |
| 1263 | unsigned short flags; |
| 1264 | } jim_subcmd_type; |
| 1265 | |
| 1266 | #define JIM_DEF_SUBCMD(name, args, minargs, maxargs) { name, args, NULL, minargs, maxargs } |
| 1267 | #define JIM_DEF_SUBCMD_HIDDEN(name, args, minargs, maxargs) { name, args, NULL, minargs, maxargs, JIM_MODFLAG_HIDDEN } |
| 1268 | |
| 1269 | const jim_subcmd_type * |
| 1270 | Jim_ParseSubCmd(Jim_Interp *interp, const jim_subcmd_type *command_table, int argc, Jim_Obj *const *argv); |
| 1271 | |
| 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 | |
| 1285 | #endif |
| 1286 | #ifndef JIMREGEXP_H |
| 1287 | #define JIMREGEXP_H |
| 1288 | |
| 1289 | |
| 1290 | #ifdef __cplusplus |
| 1291 | extern "C" { |
| 1292 | #endif |
| 1293 | |
| 1294 | #include <stdlib.h> |
| 1295 | |
| 1296 | typedef struct { |
| 1297 | int rm_so; |
| 1298 | int rm_eo; |
| 1299 | } regmatch_t; |
| 1300 | |
| 1301 | |
| 1302 | typedef struct regexp { |
| 1303 | |
| 1304 | int re_nsub; |
| 1305 | |
| 1306 | |
| 1307 | int cflags; |
| 1308 | int err; |
| 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; |
| 1320 | |
| 1321 | |
| 1322 | int eflags; |
| 1323 | const char *start; |
| 1324 | const char *reginput; |
| 1325 | const char *regbol; |
| 1326 | |
| 1327 | |
| 1328 | regmatch_t *pmatch; |
| 1329 | int nmatch; |
| 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, |
| 1348 | REG_ERR_NULL_ARGUMENT, |
| 1349 | REG_ERR_UNKNOWN, |
| 1350 | REG_ERR_TOO_BIG, |
| 1351 | REG_ERR_NOMEM, |
| 1352 | REG_ERR_TOO_MANY_PAREN, |
| 1353 | REG_ERR_UNMATCHED_PAREN, |
| 1354 | REG_ERR_UNMATCHED_BRACES, |
| 1355 | REG_ERR_BAD_COUNT, |
| 1356 | REG_ERR_JUNK_ON_END, |
| 1357 | REG_ERR_OPERAND_COULD_BE_EMPTY, |
| 1358 | REG_ERR_NESTED_COUNT, |
| 1359 | REG_ERR_INTERNAL, |
| 1360 | REG_ERR_COUNT_FOLLOWS_NOTHING, |
| 1361 | REG_ERR_INVALID_ESCAPE, |
| 1362 | REG_ERR_CORRUPTED, |
| 1363 | REG_ERR_NULL_CHAR, |
| 1364 | REG_ERR_UNMATCHED_BRACKET, |
| 1365 | REG_ERR_NUM |
| 1366 | }; |
| 1367 | |
| 1368 | int jim_regcomp(regex_t *preg, const char *regex, int cflags); |
| 1369 | int jim_regexec(regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags); |
| 1370 | size_t jim_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size); |
| 1371 | void jim_regfree(regex_t *preg); |
| 1372 | |
| 1373 | #ifdef __cplusplus |
| 1374 | } |
| 1375 | #endif |
| 1376 | |
| 1377 | #endif |
| 1378 | #ifndef JIM_SIGNAL_H |
| 1379 | #define JIM_SIGNAL_H |
| 1380 | |
| 1381 | #ifdef __cplusplus |
| 1382 | extern "C" { |
| 1383 | #endif |
| 1384 | |
| 1385 | const char *Jim_SignalId(int sig); |
| 1386 | |
| 1387 | #ifdef __cplusplus |
| 1388 | } |
| 1389 | #endif |
| 1390 | |
| 1391 | #endif |
| 1392 | #ifndef JIMIOCOMPAT_H |
| 1393 | #define JIMIOCOMPAT_H |
| 1394 | |
| 1395 | |
| 1396 | #include <stdio.h> |
| 1397 | #include <errno.h> |
| 1398 | #include <sys/stat.h> |
| 1399 | |
| 1400 | |
| 1401 | void Jim_SetResultErrno(Jim_Interp *interp, const char *msg); |
| 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> |
| 1413 | #include <fcntl.h> |
| 1414 | #include <io.h> |
| 1415 | #include <process.h> |
| 1416 | |
| 1417 | typedef HANDLE phandle_t; |
| 1418 | #define JIM_BAD_PHANDLE INVALID_HANDLE_VALUE |
| 1419 | |
| 1420 | |
| 1421 | #define WIFEXITED(STATUS) (((STATUS) & 0xff00) == 0) |
| 1422 | #define WEXITSTATUS(STATUS) ((STATUS) & 0x00ff) |
| 1423 | #define WIFSIGNALED(STATUS) (((STATUS) & 0xff00) != 0) |
| 1424 | #define WTERMSIG(STATUS) (((STATUS) >> 8) & 0xff) |
| 1425 | #define WNOHANG 1 |
| 1426 | |
| 1427 | int Jim_Errno(void); |
| 1428 | |
| 1429 | long waitpid(phandle_t phandle, int *status, int nohang); |
| 1430 | |
| 1431 | phandle_t JimWaitPid(long processid, int *status, int nohang); |
| 1432 | |
| 1433 | long JimProcessPid(phandle_t phandle); |
| 1434 | |
| 1435 | #define HAVE_PIPE |
| 1436 | #define pipe(P) _pipe((P), 0, O_NOINHERIT) |
| 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 |
| 1453 | #if defined(HAVE_FSTAT64) |
| 1454 | #define Jim_FileStat fstat64 |
| 1455 | #endif |
| 1456 | #if defined(HAVE_LSTAT64) |
| 1457 | #define Jim_LinkStat lstat64 |
| 1458 | #endif |
| 1459 | #else |
| 1460 | typedef struct stat jim_stat_t; |
| 1461 | #define Jim_Stat stat |
| 1462 | #if defined(HAVE_FSTAT) |
| 1463 | #define Jim_FileStat fstat |
| 1464 | #endif |
| 1465 | #if defined(HAVE_LSTAT) |
| 1466 | #define Jim_LinkStat lstat |
| 1467 | #endif |
| 1468 | #endif |
| 1469 | #if defined(HAVE_LSEEK64) |
| 1470 | #define Jim_Lseek lseek64 |
| 1471 | #else |
| 1472 | #define Jim_Lseek lseek |
| 1473 | #endif |
| 1474 | |
| 1475 | #if defined(HAVE_UNISTD_H) |
| 1476 | #include <unistd.h> |
| 1477 | #include <fcntl.h> |
| 1478 | #include <sys/wait.h> |
| 1479 | |
| 1480 | typedef int phandle_t; |
| 1481 | #define Jim_Errno() errno |
| 1482 | #define JIM_BAD_PHANDLE -1 |
| 1483 | #define JimProcessPid(PIDTYPE) (PIDTYPE) |
| 1484 | #define JimWaitPid waitpid |
| 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 |
| 1509 | int Jim_bootstrapInit(Jim_Interp *interp) |
| 1510 | { |
| 1511 | if (Jim_PackageProvide(interp, "bootstrap", "1.0", JIM_ERRMSG)) |
| 1512 | return JIM_ERR; |
| 1513 | |
| 1514 | return Jim_EvalSource(interp, "bootstrap.tcl", 1, |
| 1515 | "\n" |
| 1516 | "proc package {cmd args} {\n" |
| 1517 | " if {$cmd eq \"require\"} {\n" |
| 1518 | " foreach path $::auto_path {\n" |
| 1519 | " lassign $args pkg\n" |
| 1520 | " set pkgpath $path/$pkg.tcl\n" |
| 1521 | " if {$path eq \".\"} {\n" |
| 1522 | " set pkgpath $pkg.tcl\n" |
| 1523 | " }\n" |
| 1524 | " if {[file exists $pkgpath]} {\n" |
| 1525 | " tailcall uplevel #0 [list source $pkgpath]\n" |
| 1526 | " }\n" |
| 1527 | " }\n" |
| 1528 | " }\n" |
| 1529 | "}\n" |
| 1530 | "set tcl_platform(bootstrap) 1\n" |
| 1531 | ); |
| 1532 | } |
| 1533 | int Jim_initjimshInit(Jim_Interp *interp) |
| 1534 | { |
| 1535 | if (Jim_PackageProvide(interp, "initjimsh", "1.0", JIM_ERRMSG)) |
| 1536 | return JIM_ERR; |
| 1537 | |
| 1538 | return Jim_EvalSource(interp, "initjimsh.tcl", 1, |
| 1539 | "\n" |
| 1540 | "\n" |
| 1541 | "\n" |
| 1542 | "proc _jimsh_init {} {\n" |
| 1543 | " rename _jimsh_init {}\n" |
| 1544 | " global jim::exe jim::argv0 tcl_interactive auto_path tcl_platform\n" |
| 1545 | "\n" |
| 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" |
| 1564 | " }\n" |
| 1565 | " }\n" |
| 1566 | "\n" |
| 1567 | "\n" |
| 1568 | " lappend p {*}[split [env JIMLIB {}] $tcl_platform(pathSeparator)]\n" |
| 1569 | " if {[exists jim::exe]} {\n" |
| 1570 | " lappend p [file dirname $jim::exe]\n" |
| 1571 | " }\n" |
| 1572 | " lappend p {*}$auto_path\n" |
| 1573 | " set auto_path $p\n" |
| 1574 | "\n" |
| 1575 | " if {$tcl_interactive && [env HOME {}] ne \"\"} {\n" |
| 1576 | " foreach src {.jimrc jimrc.tcl} {\n" |
| 1577 | " if {[file exists [env HOME]/$src]} {\n" |
| 1578 | " uplevel #0 source [env HOME]/$src\n" |
| 1579 | " break\n" |
| 1580 | " }\n" |
| 1581 | " }\n" |
| 1582 | " }\n" |
| 1583 | " return \"\"\n" |
| 1584 | "}\n" |
| 1585 | "\n" |
| 1586 | "if {$tcl_platform(platform) eq \"windows\"} {\n" |
| 1587 | " set jim::argv0 [string map {\\\\ /} $jim::argv0]\n" |
| 1588 | "}\n" |
| 1589 | "\n" |
| 1590 | "\n" |
| 1591 | "set tcl::autocomplete_commands {array clock debug dict file history info namespace package signal socket string tcl::prefix zlib}\n" |
| 1592 | "\n" |
| 1593 | "\n" |
| 1594 | "\n" |
| 1595 | "proc tcl::autocomplete {prefix} {\n" |
| 1596 | " if {[set space [string first \" \" $prefix]] != -1} {\n" |
| 1597 | " set cmd [string range $prefix 0 $space-1]\n" |
| 1598 | " if {$cmd in $::tcl::autocomplete_commands || [info channel $cmd] ne \"\"} {\n" |
| 1599 | " set arg [string range $prefix $space+1 end]\n" |
| 1600 | "\n" |
| 1601 | " return [lmap p [$cmd -commands] {\n" |
| 1602 | " if {![string match \"${arg}*\" $p]} continue\n" |
| 1603 | " function \"$cmd $p\"\n" |
| 1604 | " }]\n" |
| 1605 | " }\n" |
| 1606 | " }\n" |
| 1607 | "\n" |
| 1608 | " if {[string match \"source *\" $prefix]} {\n" |
| 1609 | " set path [string range $prefix 7 end]\n" |
| 1610 | " return [lmap p [glob -nocomplain \"${path}*\"] {\n" |
| 1611 | " function \"source $p\"\n" |
| 1612 | " }]\n" |
| 1613 | " }\n" |
| 1614 | "\n" |
| 1615 | " return [lmap p [lsort [info commands $prefix*]] {\n" |
| 1616 | " if {[string match \"* *\" $p]} {\n" |
| 1617 | " continue\n" |
| 1618 | " }\n" |
| 1619 | " function $p\n" |
| 1620 | " }]\n" |
| 1621 | "}\n" |
| 1622 | "\n" |
| 1623 | "\n" |
| 1624 | "set tcl::stdhint_commands {array clock debug dict file history info namespace package signal string zlib}\n" |
| 1625 | "\n" |
| 1626 | "set tcl::stdhint_cols {\n" |
| 1627 | " none {0}\n" |
| 1628 | " black {30}\n" |
| 1629 | " red {31}\n" |
| 1630 | " green {32}\n" |
| 1631 | " yellow {33}\n" |
| 1632 | " blue {34}\n" |
| 1633 | " purple {35}\n" |
| 1634 | " cyan {36}\n" |
| 1635 | " normal {37}\n" |
| 1636 | " grey {30 1}\n" |
| 1637 | " gray {30 1}\n" |
| 1638 | " lred {31 1}\n" |
| 1639 | " lgreen {32 1}\n" |
| 1640 | " lyellow {33 1}\n" |
| 1641 | " lblue {34 1}\n" |
| 1642 | " lpurple {35 1}\n" |
| 1643 | " lcyan {36 1}\n" |
| 1644 | " white {37 1}\n" |
| 1645 | "}\n" |
| 1646 | "\n" |
| 1647 | "\n" |
| 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" |
| 1660 | " set subcmd [lindex $help $n]\n" |
| 1661 | " incr n\n" |
| 1662 | " set hint [join [lrange $help $n end]]\n" |
| 1663 | " set prefix \"\"\n" |
| 1664 | " if {![string match \"* \" $string]} {\n" |
| 1665 | " if {$n == 3 && $subcmd ne $arg} {\n" |
| 1666 | "\n" |
| 1667 | " set prefix \"[string range $subcmd [string length $arg] end] \"\n" |
| 1668 | " } else {\n" |
| 1669 | " set prefix \" \"\n" |
| 1670 | " }\n" |
| 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" |
| 1698 | ); |
| 1699 | } |
| 1700 | int Jim_globInit(Jim_Interp *interp) |
| 1701 | { |
| 1702 | if (Jim_PackageProvide(interp, "glob", "1.0", JIM_ERRMSG)) |
| 1703 | return JIM_ERR; |
| 1704 | |
| 1705 | return Jim_EvalSource(interp, "glob.tcl", 1, |
| 1706 | "\n" |
| 1707 | "\n" |
| 1708 | "\n" |
| 1709 | "\n" |
| 1710 | "\n" |
| 1711 | "\n" |
| 1712 | "\n" |
| 1713 | "package require readdir\n" |
| 1714 | "\n" |
| 1715 | "\n" |
| 1716 | "proc glob.globdir {dir pattern} {\n" |
| 1717 | " if {[file exists $dir/$pattern]} {\n" |
| 1718 | "\n" |
| 1719 | " return [list $pattern]\n" |
| 1720 | " }\n" |
| 1721 | "\n" |
| 1722 | " set result {}\n" |
| 1723 | " set files [readdir $dir]\n" |
| 1724 | " lappend files . ..\n" |
| 1725 | "\n" |
| 1726 | " foreach name $files {\n" |
| 1727 | " if {[string match $pattern $name]} {\n" |
| 1728 | "\n" |
| 1729 | " if {[string index $name 0] eq \".\" && [string index $pattern 0] ne \".\"} {\n" |
| 1730 | " continue\n" |
| 1731 | " }\n" |
| 1732 | " lappend result $name\n" |
| 1733 | " }\n" |
| 1734 | " }\n" |
| 1735 | "\n" |
| 1736 | " return $result\n" |
| 1737 | "}\n" |
| 1738 | "\n" |
| 1739 | "\n" |
| 1740 | "\n" |
| 1741 | "\n" |
| 1742 | "proc glob.explode {pattern} {\n" |
| 1743 | " set oldexp {}\n" |
| 1744 | " set newexp {\"\"}\n" |
| 1745 | "\n" |
| 1746 | " while 1 {\n" |
| 1747 | " set oldexp $newexp\n" |
| 1748 | " set newexp {}\n" |
| 1749 | " set ob [string first \\{ $pattern]\n" |
| 1750 | " set cb [string first \\} $pattern]\n" |
| 1751 | "\n" |
| 1752 | " if {$ob < $cb && $ob != -1} {\n" |
| 1753 | " set mid [string range $pattern 0 $ob-1]\n" |
| 1754 | " set subexp [lassign [glob.explode [string range $pattern $ob+1 end]] pattern]\n" |
| 1755 | " if {$pattern eq \"\"} {\n" |
| 1756 | " error \"unmatched open brace in glob pattern\"\n" |
| 1757 | " }\n" |
| 1758 | " set pattern [string range $pattern 1 end]\n" |
| 1759 | "\n" |
| 1760 | " foreach subs $subexp {\n" |
| 1761 | " foreach sub [split $subs ,] {\n" |
| 1762 | " foreach old $oldexp {\n" |
| 1763 | " lappend newexp $old$mid$sub\n" |
| 1764 | " }\n" |
| 1765 | " }\n" |
| 1766 | " }\n" |
| 1767 | " } elseif {$cb != -1} {\n" |
| 1768 | " set suf [string range $pattern 0 $cb-1]\n" |
| 1769 | " set rest [string range $pattern $cb end]\n" |
| 1770 | " break\n" |
| 1771 | " } else {\n" |
| 1772 | " set suf $pattern\n" |
| 1773 | " set rest \"\"\n" |
| 1774 | " break\n" |
| 1775 | " }\n" |
| 1776 | " }\n" |
| 1777 | "\n" |
| 1778 | " foreach old $oldexp {\n" |
| 1779 | " lappend newexp $old$suf\n" |
| 1780 | " }\n" |
| 1781 | " list $rest {*}$newexp\n" |
| 1782 | "}\n" |
| 1783 | "\n" |
| 1784 | "\n" |
| 1785 | "\n" |
| 1786 | "proc glob.glob {base pattern} {\n" |
| 1787 | " set dir [file dirname $pattern]\n" |
| 1788 | " if {$pattern eq $dir || $pattern eq \"\"} {\n" |
| 1789 | " return [list [file join $base $dir] $pattern]\n" |
| 1790 | " } elseif {$pattern eq [file tail $pattern]} {\n" |
| 1791 | " set dir \"\"\n" |
| 1792 | " }\n" |
| 1793 | "\n" |
| 1794 | "\n" |
| 1795 | " set dirlist [glob.glob $base $dir]\n" |
| 1796 | " set pattern [file tail $pattern]\n" |
| 1797 | "\n" |
| 1798 | "\n" |
| 1799 | " set result {}\n" |
| 1800 | " foreach {realdir dir} $dirlist {\n" |
| 1801 | " if {![file isdir $realdir]} {\n" |
| 1802 | " continue\n" |
| 1803 | " }\n" |
| 1804 | " if {[string index $dir end] ne \"/\" && $dir ne \"\"} {\n" |
| 1805 | " append dir /\n" |
| 1806 | " }\n" |
| 1807 | " foreach name [glob.globdir $realdir $pattern] {\n" |
| 1808 | " lappend result [file join $realdir $name] $dir$name\n" |
| 1809 | " }\n" |
| 1810 | " }\n" |
| 1811 | " return $result\n" |
| 1812 | "}\n" |
| 1813 | "\n" |
| 1814 | "\n" |
| 1815 | "\n" |
| 1816 | "\n" |
| 1817 | "\n" |
| 1818 | "\n" |
| 1819 | "\n" |
| 1820 | "\n" |
| 1821 | "\n" |
| 1822 | "\n" |
| 1823 | "\n" |
| 1824 | "\n" |
| 1825 | "proc glob {args} {\n" |
| 1826 | " set nocomplain 0\n" |
| 1827 | " set base \"\"\n" |
| 1828 | " set tails 0\n" |
| 1829 | "\n" |
| 1830 | " set n 0\n" |
| 1831 | " foreach arg $args {\n" |
| 1832 | " if {[info exists param]} {\n" |
| 1833 | " set $param $arg\n" |
| 1834 | " unset param\n" |
| 1835 | " incr n\n" |
| 1836 | " continue\n" |
| 1837 | " }\n" |
| 1838 | " switch -glob -- $arg {\n" |
| 1839 | " -d* {\n" |
| 1840 | " set switch $arg\n" |
| 1841 | " set param base\n" |
| 1842 | " }\n" |
| 1843 | " -n* {\n" |
| 1844 | " set nocomplain 1\n" |
| 1845 | " }\n" |
| 1846 | " -ta* {\n" |
| 1847 | " set tails 1\n" |
| 1848 | " }\n" |
| 1849 | " -- {\n" |
| 1850 | " incr n\n" |
| 1851 | " break\n" |
| 1852 | " }\n" |
| 1853 | " -* {\n" |
| 1854 | " return -code error \"bad option \\\"$arg\\\": must be -directory, -nocomplain, -tails, or --\"\n" |
| 1855 | " }\n" |
| 1856 | " * {\n" |
| 1857 | " break\n" |
| 1858 | " }\n" |
| 1859 | " }\n" |
| 1860 | " incr n\n" |
| 1861 | " }\n" |
| 1862 | " if {[info exists param]} {\n" |
| 1863 | " return -code error \"missing argument to \\\"$switch\\\"\"\n" |
| 1864 | " }\n" |
| 1865 | " if {[llength $args] <= $n} {\n" |
| 1866 | " return -code error \"wrong # args: should be \\\"glob ?options? pattern ?pattern ...?\\\"\"\n" |
| 1867 | " }\n" |
| 1868 | "\n" |
| 1869 | " set args [lrange $args $n end]\n" |
| 1870 | "\n" |
| 1871 | " set result {}\n" |
| 1872 | " foreach pattern $args {\n" |
| 1873 | " set escpattern [string map {\n" |
| 1874 | " \\\\\\\\ \\x01 \\\\\\{ \\x02 \\\\\\} \\x03 \\\\, \\x04\n" |
| 1875 | " } $pattern]\n" |
| 1876 | " set patexps [lassign [glob.explode $escpattern] rest]\n" |
| 1877 | " if {$rest ne \"\"} {\n" |
| 1878 | " return -code error \"unmatched close brace in glob pattern\"\n" |
| 1879 | " }\n" |
| 1880 | " foreach patexp $patexps {\n" |
| 1881 | " set patexp [string map {\n" |
| 1882 | " \\x01 \\\\\\\\ \\x02 \\{ \\x03 \\} \\x04 ,\n" |
| 1883 | " } $patexp]\n" |
| 1884 | " foreach {realname name} [glob.glob $base $patexp] {\n" |
| 1885 | " incr n\n" |
| 1886 | " if {$tails} {\n" |
| 1887 | " lappend result $name\n" |
| 1888 | " } else {\n" |
| 1889 | " lappend result [file join $base $name]\n" |
| 1890 | " }\n" |
| 1891 | " }\n" |
| 1892 | " }\n" |
| 1893 | " }\n" |
| 1894 | "\n" |
| 1895 | " if {!$nocomplain && [llength $result] == 0} {\n" |
| 1896 | " set s $(([llength $args] > 1) ? \"s\" : \"\")\n" |
| 1897 | " return -code error \"no files matched glob pattern$s \\\"[join $args]\\\"\"\n" |
| 1898 | " }\n" |
| 1899 | "\n" |
| 1900 | " return $result\n" |
| 1901 | "}\n" |
| 1902 | ); |
| 1903 | } |
| 1904 | int Jim_stdlibInit(Jim_Interp *interp) |
| 1905 | { |
| 1906 | if (Jim_PackageProvide(interp, "stdlib", "1.0", JIM_ERRMSG)) |
| 1907 | return JIM_ERR; |
| 1908 | |
| 1909 | return Jim_EvalSource(interp, "stdlib.tcl", 1, |
| 1910 | "\n" |
| 1911 | "\n" |
| 1912 | "if {![exists -command ref]} {\n" |
| 1913 | "\n" |
| 1914 | " proc ref {args} {{count 0}} {\n" |
| 1915 | " format %08x [incr count]\n" |
| 1916 | " }\n" |
| 1917 | "}\n" |
| 1918 | "\n" |
| 1919 | "\n" |
| 1920 | "proc lambda {arglist args} {\n" |
| 1921 | " tailcall proc [ref {} function lambda.finalizer] $arglist {*}$args\n" |
| 1922 | "}\n" |
| 1923 | "\n" |
| 1924 | "proc lambda.finalizer {name val} {\n" |
| 1925 | " rename $name {}\n" |
| 1926 | "}\n" |
| 1927 | "\n" |
| 1928 | "\n" |
| 1929 | "proc curry {args} {\n" |
| 1930 | " alias [ref {} function lambda.finalizer] {*}$args\n" |
| 1931 | "}\n" |
| 1932 | "\n" |
| 1933 | "\n" |
| 1934 | "\n" |
| 1935 | "\n" |
| 1936 | "\n" |
| 1937 | "\n" |
| 1938 | "\n" |
| 1939 | "\n" |
| 1940 | "\n" |
| 1941 | "proc function {value} {\n" |
| 1942 | " return $value\n" |
| 1943 | "}\n" |
| 1944 | "\n" |
| 1945 | "\n" |
| 1946 | "proc stackdump {stacktrace} {\n" |
| 1947 | " set lines {}\n" |
| 1948 | " lappend lines \"Traceback (most recent call last):\"\n" |
| 1949 | " foreach {cmd l f p} [lreverse $stacktrace] {\n" |
| 1950 | " set line {}\n" |
| 1951 | " if {$f ne \"\"} {\n" |
| 1952 | " append line \" File \\\"$f\\\", line $l\"\n" |
| 1953 | " }\n" |
| 1954 | " if {$p ne \"\"} {\n" |
| 1955 | " append line \", in $p\"\n" |
| 1956 | " }\n" |
| 1957 | " if {$line ne \"\"} {\n" |
| 1958 | " lappend lines $line\n" |
| 1959 | " if {$cmd ne \"\"} {\n" |
| 1960 | " set nl [string first \\n $cmd 1]\n" |
| 1961 | " if {$nl >= 0} {\n" |
| 1962 | " set cmd [string range $cmd 0 $nl-1]...\n" |
| 1963 | " }\n" |
| 1964 | " lappend lines \" $cmd\"\n" |
| 1965 | " }\n" |
| 1966 | " }\n" |
| 1967 | " }\n" |
| 1968 | " if {[llength $lines] > 1} {\n" |
| 1969 | " return [join $lines \\n]\n" |
| 1970 | " }\n" |
| 1971 | "}\n" |
| 1972 | "\n" |
| 1973 | "\n" |
| 1974 | "\n" |
| 1975 | "proc defer {script} {\n" |
| 1976 | " upvar jim::defer v\n" |
| 1977 | " lappend v $script\n" |
| 1978 | "}\n" |
| 1979 | "\n" |
| 1980 | "\n" |
| 1981 | "\n" |
| 1982 | "proc errorInfo {msg {stacktrace \"\"}} {\n" |
| 1983 | " if {$stacktrace eq \"\"} {\n" |
| 1984 | "\n" |
| 1985 | " set stacktrace [info stacktrace]\n" |
| 1986 | " }\n" |
| 1987 | " lassign $stacktrace p f l cmd\n" |
| 1988 | " if {$f ne \"\"} {\n" |
| 1989 | " set result \"$f:$l: Error: \"\n" |
| 1990 | " }\n" |
| 1991 | " append result \"$msg\\n\"\n" |
| 1992 | " append result [stackdump $stacktrace]\n" |
| 1993 | "\n" |
| 1994 | "\n" |
| 1995 | " string trim $result\n" |
| 1996 | "}\n" |
| 1997 | "\n" |
| 1998 | "\n" |
| 1999 | "\n" |
| 2000 | "proc {info nameofexecutable} {} {\n" |
| 2001 | " if {[exists ::jim::exe]} {\n" |
| 2002 | " return $::jim::exe\n" |
| 2003 | " }\n" |
| 2004 | "}\n" |
| 2005 | "\n" |
| 2006 | "\n" |
| 2007 | "proc {dict update} {&varName args script} {\n" |
| 2008 | " set keys {}\n" |
| 2009 | " foreach {n v} $args {\n" |
| 2010 | " upvar $v var_$v\n" |
| 2011 | " if {[dict exists $varName $n]} {\n" |
| 2012 | " set var_$v [dict get $varName $n]\n" |
| 2013 | " }\n" |
| 2014 | " }\n" |
| 2015 | " catch {uplevel 1 $script} msg opts\n" |
| 2016 | " if {[info exists varName]} {\n" |
| 2017 | " foreach {n v} $args {\n" |
| 2018 | " if {[info exists var_$v]} {\n" |
| 2019 | " dict set varName $n [set var_$v]\n" |
| 2020 | " } else {\n" |
| 2021 | " dict unset varName $n\n" |
| 2022 | " }\n" |
| 2023 | " }\n" |
| 2024 | " }\n" |
| 2025 | " return {*}$opts $msg\n" |
| 2026 | "}\n" |
| 2027 | "\n" |
| 2028 | "proc {dict replace} {dictionary {args {key value}}} {\n" |
| 2029 | " if {[llength ${key value}] % 2} {\n" |
| 2030 | " tailcall {dict replace}\n" |
| 2031 | " }\n" |
| 2032 | " tailcall dict merge $dictionary ${key value}\n" |
| 2033 | "}\n" |
| 2034 | "\n" |
| 2035 | "\n" |
| 2036 | "proc {dict lappend} {varName key {args value}} {\n" |
| 2037 | " upvar $varName dict\n" |
| 2038 | " if {[exists dict] && [dict exists $dict $key]} {\n" |
| 2039 | " set list [dict get $dict $key]\n" |
| 2040 | " }\n" |
| 2041 | " lappend list {*}$value\n" |
| 2042 | " dict set dict $key $list\n" |
| 2043 | "}\n" |
| 2044 | "\n" |
| 2045 | "\n" |
| 2046 | "proc {dict append} {varName key {args value}} {\n" |
| 2047 | " upvar $varName dict\n" |
| 2048 | " if {[exists dict] && [dict exists $dict $key]} {\n" |
| 2049 | " set str [dict get $dict $key]\n" |
| 2050 | " }\n" |
| 2051 | " append str {*}$value\n" |
| 2052 | " dict set dict $key $str\n" |
| 2053 | "}\n" |
| 2054 | "\n" |
| 2055 | "\n" |
| 2056 | "proc {dict incr} {varName key {increment 1}} {\n" |
| 2057 | " upvar $varName dict\n" |
| 2058 | " if {[exists dict] && [dict exists $dict $key]} {\n" |
| 2059 | " set value [dict get $dict $key]\n" |
| 2060 | " }\n" |
| 2061 | " incr value $increment\n" |
| 2062 | " dict set dict $key $value\n" |
| 2063 | "}\n" |
| 2064 | "\n" |
| 2065 | "\n" |
| 2066 | "proc {dict remove} {dictionary {args key}} {\n" |
| 2067 | " foreach k $key {\n" |
| 2068 | " dict unset dictionary $k\n" |
| 2069 | " }\n" |
| 2070 | " return $dictionary\n" |
| 2071 | "}\n" |
| 2072 | "\n" |
| 2073 | "\n" |
| 2074 | "proc {dict for} {vars dictionary script} {\n" |
| 2075 | " if {[llength $vars] != 2} {\n" |
| 2076 | " return -code error \"must have exactly two variable names\"\n" |
| 2077 | " }\n" |
| 2078 | " dict size $dictionary\n" |
| 2079 | " tailcall foreach $vars $dictionary $script\n" |
| 2080 | "}\n" |
| 2081 | ); |
| 2082 | } |
| 2083 | int Jim_tclcompatInit(Jim_Interp *interp) |
| 2084 | { |
| 2085 | if (Jim_PackageProvide(interp, "tclcompat", "1.0", JIM_ERRMSG)) |
| 2086 | return JIM_ERR; |
| 2087 | |
| 2088 | return Jim_EvalSource(interp, "tclcompat.tcl", 1, |
| 2089 | "\n" |
| 2090 | "\n" |
| 2091 | "\n" |
| 2092 | "\n" |
| 2093 | "\n" |
| 2094 | "\n" |
| 2095 | "\n" |
| 2096 | "\n" |
| 2097 | "set env [env]\n" |
| 2098 | "\n" |
| 2099 | "\n" |
| 2100 | "if {[exists -command stdout]} {\n" |
| 2101 | "\n" |
| 2102 | " foreach p {gets flush close eof seek tell} {\n" |
| 2103 | " proc $p {chan args} {p} {\n" |
| 2104 | " tailcall $chan $p {*}$args\n" |
| 2105 | " }\n" |
| 2106 | " }\n" |
| 2107 | " unset p\n" |
| 2108 | "\n" |
| 2109 | "\n" |
| 2110 | "\n" |
| 2111 | " proc puts {{-nonewline {}} {chan stdout} msg} {\n" |
| 2112 | " if {${-nonewline} ni {-nonewline {}}} {\n" |
| 2113 | " tailcall ${-nonewline} puts $msg\n" |
| 2114 | " }\n" |
| 2115 | " tailcall $chan puts {*}${-nonewline} $msg\n" |
| 2116 | " }\n" |
| 2117 | "\n" |
| 2118 | "\n" |
| 2119 | "\n" |
| 2120 | "\n" |
| 2121 | "\n" |
| 2122 | " proc read {{-nonewline {}} chan} {\n" |
| 2123 | " if {${-nonewline} ni {-nonewline {}}} {\n" |
| 2124 | " tailcall ${-nonewline} read {*}${chan}\n" |
| 2125 | " }\n" |
| 2126 | " tailcall $chan read {*}${-nonewline}\n" |
| 2127 | " }\n" |
| 2128 | "\n" |
| 2129 | " proc fconfigure {f args} {\n" |
| 2130 | " foreach {n v} $args {\n" |
| 2131 | " switch -glob -- $n {\n" |
| 2132 | " -bl* {\n" |
| 2133 | " $f ndelay $(!$v)\n" |
| 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" |
| 2145 | " }\n" |
| 2146 | " }\n" |
| 2147 | "}\n" |
| 2148 | "\n" |
| 2149 | "\n" |
| 2150 | "proc fileevent {args} {\n" |
| 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" |
| 2164 | " set max [string length $name]\n" |
| 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" |
| 2176 | " try {\n" |
| 2177 | " if {$force ni {{} -force}} {\n" |
| 2178 | " error \"bad option \\\"$force\\\": should be -force\"\n" |
| 2179 | " }\n" |
| 2180 | "\n" |
| 2181 | " set in [open $source rb]\n" |
| 2182 | "\n" |
| 2183 | " if {[file exists $target]} {\n" |
| 2184 | " if {$force eq \"\"} {\n" |
| 2185 | " error \"error copying \\\"$source\\\" to \\\"$target\\\": file already exists\"\n" |
| 2186 | " }\n" |
| 2187 | "\n" |
| 2188 | " if {$source eq $target} {\n" |
| 2189 | " return\n" |
| 2190 | " }\n" |
| 2191 | "\n" |
| 2192 | "\n" |
| 2193 | " file stat $source ss\n" |
| 2194 | " file stat $target ts\n" |
| 2195 | " if {$ss(dev) == $ts(dev) && $ss(ino) == $ts(ino) && $ss(ino)} {\n" |
| 2196 | " return\n" |
| 2197 | " }\n" |
| 2198 | " }\n" |
| 2199 | " set out [open $target wb]\n" |
| 2200 | " $in copyto $out\n" |
| 2201 | " $out close\n" |
| 2202 | " } on error {msg opts} {\n" |
| 2203 | " incr opts(-level)\n" |
| 2204 | " return {*}$opts $msg\n" |
| 2205 | " } finally {\n" |
| 2206 | " catch {$in close}\n" |
| 2207 | " }\n" |
| 2208 | "}\n" |
| 2209 | "\n" |
| 2210 | "\n" |
| 2211 | "\n" |
| 2212 | "proc popen {cmd {mode r}} {\n" |
| 2213 | " lassign [pipe] r w\n" |
| 2214 | " try {\n" |
| 2215 | " if {[string match \"w*\" $mode]} {\n" |
| 2216 | " lappend cmd <@$r &\n" |
| 2217 | " set pids [exec {*}$cmd]\n" |
| 2218 | " $r close\n" |
| 2219 | " set f $w\n" |
| 2220 | " } else {\n" |
| 2221 | " lappend cmd >@$w &\n" |
| 2222 | " set pids [exec {*}$cmd]\n" |
| 2223 | " $w close\n" |
| 2224 | " set f $r\n" |
| 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" |
| 2235 | " lassign [wait $p] status - rc\n" |
| 2236 | " if {$status eq \"CHILDSTATUS\"} {\n" |
| 2237 | " if {$rc == 0} {\n" |
| 2238 | " continue\n" |
| 2239 | " }\n" |
| 2240 | " set msg \"child process exited abnormally\"\n" |
| 2241 | " } else {\n" |
| 2242 | " set msg \"child killed: received signal\"\n" |
| 2243 | " }\n" |
| 2244 | " set retopts [list -code error -errorcode [list $status $p $rc] $msg]\n" |
| 2245 | " }\n" |
| 2246 | " return {*}$retopts\n" |
| 2247 | " }\n" |
| 2248 | " tailcall $f $cmd {*}$args\n" |
| 2249 | " }\n" |
| 2250 | " } on error {error opts} {\n" |
| 2251 | " $r close\n" |
| 2252 | " $w close\n" |
| 2253 | " error $error\n" |
| 2254 | " }\n" |
| 2255 | "}\n" |
| 2256 | "\n" |
| 2257 | "\n" |
| 2258 | "local proc pid {{channelId {}}} {\n" |
| 2259 | " if {$channelId eq \"\"} {\n" |
| 2260 | " tailcall upcall pid\n" |
| 2261 | " }\n" |
| 2262 | " if {[catch {$channelId tell}]} {\n" |
| 2263 | " return -code error \"can not find channel named \\\"$channelId\\\"\"\n" |
| 2264 | " }\n" |
| 2265 | " if {[catch {$channelId pid} pids]} {\n" |
| 2266 | " return \"\"\n" |
| 2267 | " }\n" |
| 2268 | " return $pids\n" |
| 2269 | "}\n" |
| 2270 | "\n" |
| 2271 | "\n" |
| 2272 | "\n" |
| 2273 | "proc throw {code {msg \"\"}} {\n" |
| 2274 | " return -code $code $msg\n" |
| 2275 | "}\n" |
| 2276 | "\n" |
| 2277 | "\n" |
| 2278 | "proc {file delete force} {path} {\n" |
| 2279 | " foreach e [readdir $path] {\n" |
| 2280 | " file delete -force $path/$e\n" |
| 2281 | " }\n" |
| 2282 | " file delete $path\n" |
| 2283 | "}\n" |
| 2284 | ); |
| 2285 | } |
| 2286 | |
| 2287 | |
| 2288 | #include <stdio.h> |
| 2289 | #include <string.h> |
| 2290 | #include <errno.h> |
| 2291 | #include <fcntl.h> |
| 2292 | #include <assert.h> |
| 2293 | #ifdef HAVE_UNISTD_H |
| 2294 | #include <unistd.h> |
| 2295 | #include <sys/stat.h> |
| 2296 | #endif |
| 2297 | #ifdef HAVE_UTIL_H |
| 2298 | #include <util.h> |
| 2299 | #endif |
| 2300 | #ifdef HAVE_PTY_H |
| 2301 | #include <pty.h> |
| 2302 | #endif |
| 2303 | |
| 2304 | |
| 2305 | #if defined(HAVE_SYS_SOCKET_H) && defined(HAVE_SELECT) && defined(HAVE_NETINET_IN_H) && defined(HAVE_NETDB_H) && defined(HAVE_ARPA_INET_H) |
| 2306 | #include <sys/socket.h> |
| 2307 | #include <netinet/in.h> |
| 2308 | #include <netinet/tcp.h> |
| 2309 | #include <arpa/inet.h> |
| 2310 | #include <netdb.h> |
| 2311 | #ifdef HAVE_SYS_UN_H |
| 2312 | #include <sys/un.h> |
| 2313 | #endif |
| 2314 | #define HAVE_SOCKETS |
| 2315 | #elif defined (__MINGW32__) |
| 2316 | |
| 2317 | #endif |
| 2318 | |
| 2319 | #if defined(JIM_SSL) |
| 2320 | #include <openssl/ssl.h> |
| 2321 | #include <openssl/err.h> |
| 2322 | #endif |
| 2323 | |
| 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, |
| 2345 | }; |
| 2346 | |
| 2347 | #if defined(JIM_IPV6) |
| 2348 | #define IPV6 1 |
| 2349 | #else |
| 2350 | #define IPV6 0 |
| 2351 | #ifndef PF_INET6 |
| 2352 | #define PF_INET6 0 |
| 2353 | #endif |
| 2354 | #endif |
| 2355 | #if defined(HAVE_SYS_UN_H) && defined(PF_UNIX) |
| 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 | { |
| 2366 | #ifdef HAVE_SELECT |
| 2367 | int retval; |
| 2368 | struct timeval tv; |
| 2369 | fd_set rfds; |
| 2370 | |
| 2371 | FD_ZERO(&rfds); |
| 2372 | |
| 2373 | FD_SET(fd, &rfds); |
| 2374 | tv.tv_sec = ms / 1000; |
| 2375 | tv.tv_usec = (ms % 1000) * 1000; |
| 2376 | |
| 2377 | retval = select(fd + 1, &rfds, NULL, NULL, ms == 0 ? NULL : &tv); |
| 2378 | |
| 2379 | if (retval > 0) { |
| 2380 | return JIM_OK; |
| 2381 | } |
| 2382 | return JIM_ERR; |
| 2383 | #else |
| 2384 | return JIM_OK; |
| 2385 | #endif |
| 2386 | } |
| 2387 | |
| 2388 | |
| 2389 | struct AioFile; |
| 2390 | |
| 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) { |
| 2431 | |
| 2432 | int ret; |
| 2433 | |
| 2434 | errno = 0; |
| 2435 | ret = read(af->fd, buf, len); |
| 2436 | if (ret <= 0 && errno != EAGAIN && errno != EINTR) { |
| 2437 | af->flags |= AIO_EOF; |
| 2438 | } |
| 2439 | return ret; |
| 2440 | } |
| 2441 | errno = ETIMEDOUT; |
| 2442 | return -1; |
| 2443 | } |
| 2444 | |
| 2445 | static int stdio_error(const AioFile *af) |
| 2446 | { |
| 2447 | if (af->flags & AIO_EOF) { |
| 2448 | return JIM_OK; |
| 2449 | } |
| 2450 | |
| 2451 | switch (errno) { |
| 2452 | case EAGAIN: |
| 2453 | case EINTR: |
| 2454 | case ETIMEDOUT: |
| 2455 | #ifdef ECONNRESET |
| 2456 | case ECONNRESET: |
| 2457 | #endif |
| 2458 | #ifdef ECONNABORTED |
| 2459 | case ECONNABORTED: |
| 2460 | #endif |
| 2461 | return JIM_OK; |
| 2462 | default: |
| 2463 | return JIM_ERR; |
| 2464 | } |
| 2465 | } |
| 2466 | |
| 2467 | static const char *stdio_strerror(struct AioFile *af) |
| 2468 | { |
| 2469 | return strerror(errno); |
| 2470 | } |
| 2471 | |
| 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 | { |
| 2482 | #ifdef O_NDELAY |
| 2483 | int old = !!(af->flags & AIO_NONBLOCK); |
| 2484 | if (old != nb) { |
| 2485 | int fmode = fcntl(af->fd, F_GETFL); |
| 2486 | if (nb) { |
| 2487 | fmode |= O_NDELAY; |
| 2488 | af->flags |= AIO_NONBLOCK; |
| 2489 | } |
| 2490 | else { |
| 2491 | fmode &= ~O_NDELAY; |
| 2492 | af->flags &= ~AIO_NONBLOCK; |
| 2493 | } |
| 2494 | (void)fcntl(af->fd, F_SETFL, fmode); |
| 2495 | } |
| 2496 | #endif |
| 2497 | } |
| 2498 | |
| 2499 | static int aio_start_nonblocking(AioFile *af) |
| 2500 | { |
| 2501 | int old = !!(af->flags & AIO_NONBLOCK); |
| 2502 | if (af->timeout) { |
| 2503 | aio_set_nonblocking(af, 1); |
| 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) |
| 2515 | { |
| 2516 | if (af && af->fops) |
| 2517 | return af->fops->strerror(af); |
| 2518 | |
| 2519 | return strerror(errno); |
| 2520 | } |
| 2521 | |
| 2522 | static void JimAioSetError(Jim_Interp *interp, Jim_Obj *name) |
| 2523 | { |
| 2524 | AioFile *af = Jim_CmdPrivData(interp); |
| 2525 | |
| 2526 | if (name) { |
| 2527 | Jim_SetResultFormatted(interp, "%#s: %s", name, JimAioErrorString(af)); |
| 2528 | } |
| 2529 | else { |
| 2530 | Jim_SetResultString(interp, JimAioErrorString(af), -1); |
| 2531 | } |
| 2532 | } |
| 2533 | |
| 2534 | static int aio_eof(AioFile *af) |
| 2535 | { |
| 2536 | return af->flags & AIO_EOF; |
| 2537 | } |
| 2538 | |
| 2539 | static int JimCheckStreamError(Jim_Interp *interp, AioFile *af) |
| 2540 | { |
| 2541 | int ret = 0; |
| 2542 | if (!aio_eof(af)) { |
| 2543 | ret = af->fops->error(af); |
| 2544 | if (ret) { |
| 2545 | JimAioSetError(interp, af->filename); |
| 2546 | } |
| 2547 | } |
| 2548 | return ret; |
| 2549 | } |
| 2550 | |
| 2551 | static void aio_consume(Jim_Obj *objPtr, int n) |
| 2552 | { |
| 2553 | assert(objPtr->bytes); |
| 2554 | assert(n <= objPtr->length); |
| 2555 | |
| 2556 | |
| 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); |
| 2583 | if (len) { |
| 2584 | int ret = af->fops->writer(af, pt, len); |
| 2585 | if (ret > 0) { |
| 2586 | |
| 2587 | aio_consume(af->writebuf, ret); |
| 2588 | } |
| 2589 | if (ret < 0) { |
| 2590 | return JimCheckStreamError(interp, af); |
| 2591 | } |
| 2592 | if (Jim_Length(af->writebuf)) { |
| 2593 | #ifdef jim_ext_eventloop |
| 2594 | void *handler = Jim_FindFileHandler(interp, af->fd, JIM_EVENT_WRITABLE); |
| 2595 | if (handler == NULL) { |
| 2596 | Jim_CreateFileHandler(interp, af->fd, JIM_EVENT_WRITABLE, aio_autoflush, af, NULL); |
| 2597 | return JIM_OK; |
| 2598 | } |
| 2599 | else if (handler == af) { |
| 2600 | |
| 2601 | return JIM_OK; |
| 2602 | } |
| 2603 | #endif |
| 2604 | |
| 2605 | Jim_SetResultString(interp, "send buffer is full", -1); |
| 2606 | return JIM_ERR; |
| 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 | |
| 2618 | if (neededLen >= 0) { |
| 2619 | neededLen -= Jim_Length(af->readbuf); |
| 2620 | if (neededLen <= 0) { |
| 2621 | return JIM_OK; |
| 2622 | } |
| 2623 | } |
| 2624 | |
| 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 | |
| 2658 | return JIM_OK; |
| 2659 | } |
| 2660 | |
| 2661 | static Jim_Obj *aio_read_consume(Jim_Interp *interp, AioFile *af, int neededLen) |
| 2662 | { |
| 2663 | Jim_Obj *objPtr = NULL; |
| 2664 | |
| 2665 | if (neededLen < 0 || af->readbuf == NULL || Jim_Length(af->readbuf) <= neededLen) { |
| 2666 | objPtr = af->readbuf; |
| 2667 | af->readbuf = NULL; |
| 2668 | } |
| 2669 | else if (af->readbuf) { |
| 2670 | |
| 2671 | int len; |
| 2672 | const char *pt = Jim_GetString(af->readbuf, &len); |
| 2673 | |
| 2674 | objPtr = Jim_NewStringObj(interp, pt, neededLen); |
| 2675 | aio_consume(af->readbuf, neededLen); |
| 2676 | } |
| 2677 | |
| 2678 | return objPtr; |
| 2679 | } |
| 2680 | |
| 2681 | static void JimAioDelProc(Jim_Interp *interp, void *privData) |
| 2682 | { |
| 2683 | AioFile *af = privData; |
| 2684 | |
| 2685 | JIM_NOTUSED(interp); |
| 2686 | |
| 2687 | |
| 2688 | aio_flush(interp, af); |
| 2689 | Jim_DecrRefCount(interp, af->writebuf); |
| 2690 | |
| 2691 | #if UNIX_SOCKETS |
| 2692 | if (af->addr_family == PF_UNIX && (af->flags & AIO_NODELETE) == 0) { |
| 2693 | |
| 2694 | Jim_Obj *filenameObj = aio_sockname(interp, af->fd); |
| 2695 | if (filenameObj) { |
| 2696 | if (Jim_Length(filenameObj)) { |
| 2697 | remove(Jim_String(filenameObj)); |
| 2698 | } |
| 2699 | Jim_FreeNewObj(interp, filenameObj); |
| 2700 | } |
| 2701 | } |
| 2702 | #endif |
| 2703 | |
| 2704 | Jim_DecrRefCount(interp, af->filename); |
| 2705 | |
| 2706 | #ifdef jim_ext_eventloop |
| 2707 | |
| 2708 | Jim_DeleteFileHandler(interp, af->fd, JIM_EVENT_READABLE | JIM_EVENT_WRITABLE | JIM_EVENT_EXCEPTION); |
| 2709 | #endif |
| 2710 | |
| 2711 | #if defined(JIM_SSL) |
| 2712 | if (af->ssl != NULL) { |
| 2713 | SSL_free(af->ssl); |
| 2714 | } |
| 2715 | #endif |
| 2716 | if (!(af->flags & AIO_KEEPOPEN)) { |
| 2717 | close(af->fd); |
| 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 | { |
| 2729 | AioFile *af = Jim_CmdPrivData(interp); |
| 2730 | int nonewline = 0; |
| 2731 | jim_wide neededLen = -1; |
| 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; |
| 2742 | } |
| 2743 | switch (option) { |
| 2744 | case OPT_PENDING: |
| 2745 | |
| 2746 | break; |
| 2747 | case OPT_NONEWLINE: |
| 2748 | nonewline++; |
| 2749 | break; |
| 2750 | } |
| 2751 | } |
| 2752 | else { |
| 2753 | if (Jim_GetWide(interp, argv[0], &neededLen) != JIM_OK) |
| 2754 | return JIM_ERR; |
| 2755 | if (neededLen < 0) { |
| 2756 | Jim_SetResultString(interp, "invalid parameter: negative len", -1); |
| 2757 | return JIM_ERR; |
| 2758 | } |
| 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) { |
| 2781 | int len; |
| 2782 | const char *s = Jim_GetString(objPtr, &len); |
| 2783 | |
| 2784 | if (len > 0 && s[len - 1] == '\n') { |
| 2785 | objPtr->length--; |
| 2786 | objPtr->bytes[objPtr->length] = '\0'; |
| 2787 | } |
| 2788 | } |
| 2789 | Jim_SetResult(interp, objPtr); |
| 2790 | } |
| 2791 | else { |
| 2792 | Jim_SetEmptyResult(interp); |
| 2793 | } |
| 2794 | return JIM_OK; |
| 2795 | } |
| 2796 | |
| 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 | } |
| 2808 | |
| 2809 | static int aio_cmd_getfd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 2810 | { |
| 2811 | AioFile *af = Jim_CmdPrivData(interp); |
| 2812 | |
| 2813 | |
| 2814 | aio_flush(interp, af); |
| 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]); |
| 2877 | return JIM_ERR; |
| 2878 | } |
| 2879 | |
| 2880 | |
| 2881 | objv[0] = argv[0]; |
| 2882 | objv[1] = Jim_NewStringObj(interp, "puts", -1); |
| 2883 | objv[2] = Jim_NewStringObj(interp, "-nonewline", -1); |
| 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]); |
| 2898 | if (Jim_EvalObjVector(interp, 4, objv) != JIM_OK) { |
| 2899 | ok = 0; |
| 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 | |
| 2915 | if (!ok) { |
| 2916 | return JIM_ERR; |
| 2917 | } |
| 2918 | |
| 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 | |
| 3026 | if (!nl && len == 0) { |
| 3027 | |
| 3028 | len = -1; |
| 3029 | } |
| 3030 | Jim_SetResultInt(interp, len); |
| 3031 | } |
| 3032 | else { |
| 3033 | Jim_SetResult(interp, objPtr); |
| 3034 | } |
| 3035 | return JIM_OK; |
| 3036 | } |
| 3037 | |
| 3038 | static int aio_cmd_puts(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3039 | { |
| 3040 | AioFile *af = Jim_CmdPrivData(interp); |
| 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 | |
| 3075 | |
| 3076 | wdata = Jim_GetString(af->writebuf, &wlen); |
| 3077 | switch (af->wbuft) { |
| 3078 | case WBUF_OPT_NONE: |
| 3079 | |
| 3080 | wnow = 1; |
| 3081 | break; |
| 3082 | |
| 3083 | case WBUF_OPT_LINE: |
| 3084 | |
| 3085 | if (nl || memchr(wdata, '\n', wlen) != NULL) { |
| 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 | |
| 3097 | if (wnow) { |
| 3098 | return aio_flush(interp, af); |
| 3099 | } |
| 3100 | return JIM_OK; |
| 3101 | } |
| 3102 | |
| 3103 | static int aio_cmd_isatty(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3104 | { |
| 3105 | #ifdef HAVE_ISATTY |
| 3106 | AioFile *af = Jim_CmdPrivData(interp); |
| 3107 | Jim_SetResultInt(interp, isatty(af->fd)); |
| 3108 | #else |
| 3109 | Jim_SetResultInt(interp, 0); |
| 3110 | #endif |
| 3111 | |
| 3112 | return JIM_OK; |
| 3113 | } |
| 3114 | |
| 3115 | |
| 3116 | static int aio_cmd_flush(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3117 | { |
| 3118 | AioFile *af = Jim_CmdPrivData(interp); |
| 3119 | return aio_flush(interp, af); |
| 3120 | } |
| 3121 | |
| 3122 | static int aio_cmd_eof(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3123 | { |
| 3124 | AioFile *af = Jim_CmdPrivData(interp); |
| 3125 | |
| 3126 | Jim_SetResultInt(interp, !!aio_eof(af)); |
| 3127 | return JIM_OK; |
| 3128 | } |
| 3129 | |
| 3130 | static int aio_cmd_close(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3131 | { |
| 3132 | AioFile *af = Jim_CmdPrivData(interp); |
| 3133 | if (argc == 3) { |
| 3134 | int option = -1; |
| 3135 | #if defined(HAVE_SOCKETS) |
| 3136 | static const char * const options[] = { "r", "w", "-nodelete", NULL }; |
| 3137 | enum { OPT_R, OPT_W, OPT_NODELETE }; |
| 3138 | |
| 3139 | if (Jim_GetEnum(interp, argv[2], options, &option, NULL, JIM_ERRMSG) != JIM_OK) { |
| 3140 | return JIM_ERR; |
| 3141 | } |
| 3142 | #endif |
| 3143 | switch (option) { |
| 3144 | #if defined(HAVE_SHUTDOWN) |
| 3145 | case OPT_R: |
| 3146 | case OPT_W: |
| 3147 | if (shutdown(af->fd, option == OPT_R ? SHUT_RD : SHUT_WR) == 0) { |
| 3148 | return JIM_OK; |
| 3149 | } |
| 3150 | JimAioSetError(interp, NULL); |
| 3151 | return JIM_ERR; |
| 3152 | #endif |
| 3153 | #if UNIX_SOCKETS |
| 3154 | case OPT_NODELETE: |
| 3155 | if (af->addr_family == PF_UNIX) { |
| 3156 | af->flags |= AIO_NODELETE; |
| 3157 | break; |
| 3158 | } |
| 3159 | |
| 3160 | #endif |
| 3161 | default: |
| 3162 | Jim_SetResultString(interp, "not supported", -1); |
| 3163 | return JIM_ERR; |
| 3164 | } |
| 3165 | } |
| 3166 | |
| 3167 | |
| 3168 | af->flags &= ~AIO_KEEPOPEN; |
| 3169 | |
| 3170 | return Jim_DeleteCommand(interp, argv[0]); |
| 3171 | } |
| 3172 | |
| 3173 | static int aio_cmd_seek(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3174 | { |
| 3175 | AioFile *af = Jim_CmdPrivData(interp); |
| 3176 | int orig = SEEK_SET; |
| 3177 | jim_wide offset; |
| 3178 | |
| 3179 | if (argc == 2) { |
| 3180 | if (Jim_CompareStringImmediate(interp, argv[1], "start")) |
| 3181 | orig = SEEK_SET; |
| 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 | } |
| 3193 | if (orig != SEEK_CUR || offset != 0) { |
| 3194 | |
| 3195 | aio_flush(interp, af); |
| 3196 | } |
| 3197 | if (Jim_Lseek(af->fd, offset, orig) == -1) { |
| 3198 | JimAioSetError(interp, af->filename); |
| 3199 | return JIM_ERR; |
| 3200 | } |
| 3201 | if (af->readbuf) { |
| 3202 | Jim_FreeNewObj(interp, af->readbuf); |
| 3203 | af->readbuf = NULL; |
| 3204 | } |
| 3205 | af->flags &= ~AIO_EOF; |
| 3206 | return JIM_OK; |
| 3207 | } |
| 3208 | |
| 3209 | static int aio_cmd_tell(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3210 | { |
| 3211 | AioFile *af = Jim_CmdPrivData(interp); |
| 3212 | |
| 3213 | Jim_SetResultInt(interp, Jim_Lseek(af->fd, 0, SEEK_CUR)); |
| 3214 | return JIM_OK; |
| 3215 | } |
| 3216 | |
| 3217 | static int aio_cmd_filename(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3218 | { |
| 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); |
| 3268 | |
| 3269 | if (argc) { |
| 3270 | long nb; |
| 3271 | |
| 3272 | if (Jim_GetLong(interp, argv[0], &nb) != JIM_OK) { |
| 3273 | return JIM_ERR; |
| 3274 | } |
| 3275 | aio_set_nonblocking(af, nb); |
| 3276 | } |
| 3277 | Jim_SetResultInt(interp, (af->flags & AIO_NONBLOCK) ? 1 : 0); |
| 3278 | return JIM_OK; |
| 3279 | } |
| 3280 | #endif |
| 3281 | |
| 3282 | |
| 3283 | #ifdef HAVE_FSYNC |
| 3284 | static int aio_cmd_sync(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3285 | { |
| 3286 | AioFile *af = Jim_CmdPrivData(interp); |
| 3287 | |
| 3288 | if (aio_flush(interp, af) != JIM_OK) { |
| 3289 | return JIM_ERR; |
| 3290 | } |
| 3291 | fsync(af->fd); |
| 3292 | return JIM_OK; |
| 3293 | } |
| 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) { |
| 3386 | if (Jim_GetLong(interp, argv[0], &af->timeout) != JIM_OK) { |
| 3387 | return JIM_ERR; |
| 3388 | } |
| 3389 | } |
| 3390 | Jim_SetResultInt(interp, af->timeout); |
| 3391 | return JIM_OK; |
| 3392 | #else |
| 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 | |
| 3403 | Jim_Obj *objPtr = Jim_FindFileHandler(interp, af->fd, mask); |
| 3404 | if (objPtr) { |
| 3405 | Jim_SetResult(interp, objPtr); |
| 3406 | } |
| 3407 | return JIM_OK; |
| 3408 | } |
| 3409 | |
| 3410 | |
| 3411 | Jim_DeleteFileHandler(interp, af->fd, mask); |
| 3412 | |
| 3413 | |
| 3414 | if (Jim_Length(argv[0])) { |
| 3415 | Jim_CreateScriptFileHandler(interp, af->fd, mask, argv[0]); |
| 3416 | } |
| 3417 | |
| 3418 | return JIM_OK; |
| 3419 | } |
| 3420 | |
| 3421 | static int aio_cmd_readable(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3422 | { |
| 3423 | AioFile *af = Jim_CmdPrivData(interp); |
| 3424 | |
| 3425 | return aio_eventinfo(interp, af, JIM_EVENT_READABLE, argc, argv); |
| 3426 | } |
| 3427 | |
| 3428 | static int aio_cmd_writable(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3429 | { |
| 3430 | AioFile *af = Jim_CmdPrivData(interp); |
| 3431 | |
| 3432 | return aio_eventinfo(interp, af, JIM_EVENT_WRITABLE, argc, argv); |
| 3433 | } |
| 3434 | |
| 3435 | static int aio_cmd_onexception(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3436 | { |
| 3437 | AioFile *af = Jim_CmdPrivData(interp); |
| 3438 | |
| 3439 | return aio_eventinfo(interp, af, JIM_EVENT_EXCEPTION, argc, argv); |
| 3440 | } |
| 3441 | #endif |
| 3442 | |
| 3443 | #if defined(jim_ext_file) && defined(Jim_FileStat) |
| 3444 | static int aio_cmd_stat(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3445 | { |
| 3446 | jim_stat_t sb; |
| 3447 | AioFile *af = Jim_CmdPrivData(interp); |
| 3448 | |
| 3449 | if (Jim_FileStat(af->fd, &sb) == -1) { |
| 3450 | JimAioSetError(interp, NULL); |
| 3451 | return JIM_ERR; |
| 3452 | } |
| 3453 | return Jim_FileStoreStatData(interp, argc == 0 ? NULL : argv[0], &sb); |
| 3454 | } |
| 3455 | #endif |
| 3456 | |
| 3457 | |
| 3458 | |
| 3459 | |
| 3460 | static const jim_subcmd_type aio_command_table[] = { |
| 3461 | { "read", |
| 3462 | "?-nonewline|len?", |
| 3463 | aio_cmd_read, |
| 3464 | 0, |
| 3465 | 2, |
| 3466 | |
| 3467 | }, |
| 3468 | { "copyto", |
| 3469 | "handle ?size?", |
| 3470 | aio_cmd_copy, |
| 3471 | 1, |
| 3472 | 2, |
| 3473 | |
| 3474 | }, |
| 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, |
| 3501 | 1, |
| 3502 | 2, |
| 3503 | |
| 3504 | }, |
| 3505 | { "isatty", |
| 3506 | NULL, |
| 3507 | aio_cmd_isatty, |
| 3508 | 0, |
| 3509 | 0, |
| 3510 | |
| 3511 | }, |
| 3512 | { "flush", |
| 3513 | NULL, |
| 3514 | aio_cmd_flush, |
| 3515 | 0, |
| 3516 | 0, |
| 3517 | |
| 3518 | }, |
| 3519 | { "eof", |
| 3520 | NULL, |
| 3521 | aio_cmd_eof, |
| 3522 | 0, |
| 3523 | 0, |
| 3524 | |
| 3525 | }, |
| 3526 | { "close", |
| 3527 | "?r(ead)|w(rite)?", |
| 3528 | aio_cmd_close, |
| 3529 | 0, |
| 3530 | 1, |
| 3531 | JIM_MODFLAG_FULLARGV, |
| 3532 | |
| 3533 | }, |
| 3534 | { "seek", |
| 3535 | "offset ?start|current|end", |
| 3536 | aio_cmd_seek, |
| 3537 | 1, |
| 3538 | 2, |
| 3539 | |
| 3540 | }, |
| 3541 | { "tell", |
| 3542 | NULL, |
| 3543 | aio_cmd_tell, |
| 3544 | 0, |
| 3545 | 0, |
| 3546 | |
| 3547 | }, |
| 3548 | { "filename", |
| 3549 | NULL, |
| 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, |
| 3569 | 1, |
| 3570 | |
| 3571 | }, |
| 3572 | #endif |
| 3573 | #ifdef HAVE_FSYNC |
| 3574 | { "sync", |
| 3575 | NULL, |
| 3576 | aio_cmd_sync, |
| 3577 | 0, |
| 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", |
| 3605 | "?var?", |
| 3606 | aio_cmd_stat, |
| 3607 | 0, |
| 3608 | 1, |
| 3609 | |
| 3610 | }, |
| 3611 | #endif |
| 3612 | #ifdef jim_ext_eventloop |
| 3613 | { "readable", |
| 3614 | "?readable-script?", |
| 3615 | aio_cmd_readable, |
| 3616 | 0, |
| 3617 | 1, |
| 3618 | |
| 3619 | }, |
| 3620 | { "writable", |
| 3621 | "?writable-script?", |
| 3622 | aio_cmd_writable, |
| 3623 | 0, |
| 3624 | 1, |
| 3625 | |
| 3626 | }, |
| 3627 | { "onexception", |
| 3628 | "?exception-script?", |
| 3629 | aio_cmd_onexception, |
| 3630 | 0, |
| 3631 | 1, |
| 3632 | |
| 3633 | }, |
| 3634 | { "timeout", |
| 3635 | "?ms?", |
| 3636 | aio_cmd_timeout, |
| 3637 | 0, |
| 3638 | 1, |
| 3639 | |
| 3640 | }, |
| 3641 | #endif |
| 3642 | { NULL } |
| 3643 | }; |
| 3644 | |
| 3645 | static int JimAioSubCmdProc(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3646 | { |
| 3647 | return Jim_CallSubCmd(interp, Jim_ParseSubCmd(interp, aio_command_table, argc, argv), argc, argv); |
| 3648 | } |
| 3649 | |
| 3650 | static int parse_posix_open_mode(Jim_Interp *interp, Jim_Obj *modeObj) |
| 3651 | { |
| 3652 | int i; |
| 3653 | int flags = 0; |
| 3654 | #ifndef O_NOCTTY |
| 3655 | |
| 3656 | #define O_NOCTTY 0 |
| 3657 | #endif |
| 3658 | static const char * const modetypes[] = { |
| 3659 | "RDONLY", "WRONLY", "RDWR", "APPEND", "BINARY", "CREAT", "EXCL", "NOCTTY", "TRUNC", NULL |
| 3660 | }; |
| 3661 | static const int modeflags[] = { |
| 3662 | O_RDONLY, O_WRONLY, O_RDWR, O_APPEND, 0, O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC, |
| 3663 | }; |
| 3664 | |
| 3665 | for (i = 0; i < Jim_ListLength(interp, modeObj); i++) { |
| 3666 | int opt; |
| 3667 | Jim_Obj *objPtr = Jim_ListGetIndex(interp, modeObj, i); |
| 3668 | if (Jim_GetEnum(interp, objPtr, modetypes, &opt, "access mode", JIM_ERRMSG) != JIM_OK) { |
| 3669 | return -1; |
| 3670 | } |
| 3671 | flags |= modeflags[opt]; |
| 3672 | } |
| 3673 | return flags; |
| 3674 | } |
| 3675 | |
| 3676 | static int parse_open_mode(Jim_Interp *interp, Jim_Obj *filenameObj, Jim_Obj *modeObj) |
| 3677 | { |
| 3678 | |
| 3679 | int flags; |
| 3680 | const char *mode = Jim_String(modeObj); |
| 3681 | if (*mode == 'R' || *mode == 'W') { |
| 3682 | return parse_posix_open_mode(interp, modeObj); |
| 3683 | } |
| 3684 | if (*mode == 'r') { |
| 3685 | flags = O_RDONLY; |
| 3686 | } |
| 3687 | else if (*mode == 'w') { |
| 3688 | flags = O_WRONLY | O_CREAT | O_TRUNC; |
| 3689 | } |
| 3690 | else if (*mode == 'a') { |
| 3691 | flags = O_WRONLY | O_CREAT | O_APPEND; |
| 3692 | } |
| 3693 | else { |
| 3694 | Jim_SetResultFormatted(interp, "%s: invalid open mode '%s'", Jim_String(filenameObj), mode); |
| 3695 | return -1; |
| 3696 | } |
| 3697 | mode++; |
| 3698 | |
| 3699 | if (*mode == 'b') { |
| 3700 | #ifdef O_BINARY |
| 3701 | flags |= O_BINARY; |
| 3702 | #endif |
| 3703 | mode++; |
| 3704 | } |
| 3705 | |
| 3706 | if (*mode == 't') { |
| 3707 | #ifdef O_TEXT |
| 3708 | flags |= O_TEXT; |
| 3709 | #endif |
| 3710 | mode++; |
| 3711 | } |
| 3712 | |
| 3713 | if (*mode == '+') { |
| 3714 | mode++; |
| 3715 | |
| 3716 | flags &= ~(O_RDONLY | O_WRONLY); |
| 3717 | flags |= O_RDWR; |
| 3718 | } |
| 3719 | |
| 3720 | if (*mode == 'x') { |
| 3721 | mode++; |
| 3722 | #ifdef O_EXCL |
| 3723 | flags |= O_EXCL; |
| 3724 | #endif |
| 3725 | } |
| 3726 | |
| 3727 | if (*mode == 'F') { |
| 3728 | mode++; |
| 3729 | #ifdef O_LARGEFILE |
| 3730 | flags |= O_LARGEFILE; |
| 3731 | #endif |
| 3732 | } |
| 3733 | |
| 3734 | if (*mode == 'e') { |
| 3735 | |
| 3736 | mode++; |
| 3737 | } |
| 3738 | return flags; |
| 3739 | } |
| 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 | { |
| 3769 | |
| 3770 | |
| 3771 | if (*filename == '|') { |
| 3772 | Jim_Obj *evalObj[3]; |
| 3773 | int i = 0; |
| 3774 | |
| 3775 | evalObj[i++] = Jim_NewStringObj(interp, "::popen", -1); |
| 3776 | evalObj[i++] = Jim_NewStringObj(interp, filename + 1, -1); |
| 3777 | if (argc == 3 + n) { |
| 3778 | evalObj[i++] = argv[2 + n]; |
| 3779 | } |
| 3780 | |
| 3781 | return Jim_EvalObjVector(interp, i, evalObj); |
| 3782 | } |
| 3783 | } |
| 3784 | #endif |
| 3785 | if (argc == 3 + n) { |
| 3786 | openflags = parse_open_mode(interp, argv[1], argv[2 + n]); |
| 3787 | if (openflags == -1) { |
| 3788 | return JIM_ERR; |
| 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; |
| 3821 | char buf[AIO_CMD_LEN]; |
| 3822 | Jim_Obj *cmdname; |
| 3823 | |
| 3824 | snprintf(buf, sizeof(buf), hdlfmt, Jim_GetId(interp)); |
| 3825 | cmdname = Jim_NewStringObj(interp, buf, -1); |
| 3826 | if (!filename) { |
| 3827 | filename = cmdname; |
| 3828 | } |
| 3829 | Jim_IncrRefCount(filename); |
| 3830 | |
| 3831 | |
| 3832 | af = Jim_Alloc(sizeof(*af)); |
| 3833 | memset(af, 0, sizeof(*af)); |
| 3834 | af->filename = filename; |
| 3835 | af->fd = fd; |
| 3836 | af->addr_family = family; |
| 3837 | af->fops = &stdio_fops; |
| 3838 | af->ssl = NULL; |
| 3839 | if (flags & AIO_WBUF_NONE) { |
| 3840 | af->wbuft = WBUF_OPT_NONE; |
| 3841 | } |
| 3842 | else { |
| 3843 | #ifdef HAVE_ISATTY |
| 3844 | af->wbuft = isatty(af->fd) ? WBUF_OPT_LINE : WBUF_OPT_FULL; |
| 3845 | #else |
| 3846 | af->wbuft = WBUF_OPT_FULL; |
| 3847 | #endif |
| 3848 | } |
| 3849 | |
| 3850 | #ifdef FD_CLOEXEC |
| 3851 | if ((flags & AIO_KEEPOPEN) == 0) { |
| 3852 | (void)fcntl(af->fd, F_SETFD, FD_CLOEXEC); |
| 3853 | } |
| 3854 | #endif |
| 3855 | aio_set_nonblocking(af, !!(flags & AIO_NONBLOCK)); |
| 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 | } |
| 3879 | |
| 3880 | #if defined(HAVE_PIPE) || (defined(HAVE_SOCKETPAIR) && UNIX_SOCKETS) || defined(HAVE_OPENPTY) |
| 3881 | static int JimMakeChannelPair(Jim_Interp *interp, int p[2], Jim_Obj *filename, |
| 3882 | const char *hdlfmt, int family, int flags) |
| 3883 | { |
| 3884 | if (JimMakeChannel(interp, p[0], filename, hdlfmt, family, flags)) { |
| 3885 | Jim_Obj *objPtr = Jim_NewListObj(interp, NULL, 0); |
| 3886 | Jim_ListAppendElement(interp, objPtr, Jim_GetResult(interp)); |
| 3887 | if (JimMakeChannel(interp, p[1], filename, hdlfmt, family, flags)) { |
| 3888 | Jim_ListAppendElement(interp, objPtr, Jim_GetResult(interp)); |
| 3889 | Jim_SetResult(interp, objPtr); |
| 3890 | return JIM_OK; |
| 3891 | } |
| 3892 | } |
| 3893 | |
| 3894 | |
| 3895 | close(p[0]); |
| 3896 | close(p[1]); |
| 3897 | JimAioSetError(interp, NULL); |
| 3898 | return JIM_ERR; |
| 3899 | } |
| 3900 | #endif |
| 3901 | |
| 3902 | #ifdef HAVE_PIPE |
| 3903 | static int JimCreatePipe(Jim_Interp *interp, Jim_Obj *filenameObj, int flags) |
| 3904 | { |
| 3905 | int p[2]; |
| 3906 | |
| 3907 | if (pipe(p) != 0) { |
| 3908 | JimAioSetError(interp, NULL); |
| 3909 | return JIM_ERR; |
| 3910 | } |
| 3911 | |
| 3912 | return JimMakeChannelPair(interp, p, filenameObj, "aio.pipe%ld", 0, flags); |
| 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 |
| 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 | |
| 3933 | |
| 3934 | return JimMakeChannelPair(interp, p, Jim_NewStringObj(interp, path, -1), "aio.pty%ld", 0, 0); |
| 3935 | return JimMakeChannelPair(interp, p, Jim_NewStringObj(interp, path, -1), "aio.pty%ld", 0, 0); |
| 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> |
| 3973 | #include <stdio.h> |
| 3974 | #include <string.h> |
| 3975 | |
| 3976 | |
| 3977 | #ifdef HAVE_DIRENT_H |
| 3978 | #include <dirent.h> |
| 3979 | #endif |
| 3980 | |
| 3981 | int Jim_ReaddirCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3982 | { |
| 3983 | const char *dirPath; |
| 3984 | DIR *dirPtr; |
| 3985 | struct dirent *entryPtr; |
| 3986 | int nocomplain = 0; |
| 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); |
| 3998 | if (dirPtr == NULL) { |
| 3999 | if (nocomplain) { |
| 4000 | return JIM_OK; |
| 4001 | } |
| 4002 | Jim_SetResultString(interp, strerror(errno), -1); |
| 4003 | return JIM_ERR; |
| 4004 | } |
| 4005 | else { |
| 4006 | Jim_Obj *listObj = Jim_NewListObj(interp, NULL, 0); |
| 4007 | |
| 4008 | while ((entryPtr = readdir(dirPtr)) != NULL) { |
| 4009 | if (entryPtr->d_name[0] == '.') { |
| 4010 | if (entryPtr->d_name[1] == '\0') { |
| 4011 | continue; |
| 4012 | } |
| 4013 | if ((entryPtr->d_name[1] == '.') && (entryPtr->d_name[2] == '\0')) |
| 4014 | continue; |
| 4015 | } |
| 4016 | Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, entryPtr->d_name, -1)); |
| 4017 | } |
| 4018 | closedir(dirPtr); |
| 4019 | |
| 4020 | Jim_SetResult(interp, listObj); |
| 4021 | |
| 4022 | return JIM_OK; |
| 4023 | } |
| 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> |
| 4035 | |
| 4036 | #if defined(JIM_REGEXP) |
| 4037 | #else |
| 4038 | #include <regex.h> |
| 4039 | #define jim_regcomp regcomp |
| 4040 | #define jim_regexec regexec |
| 4041 | #define jim_regerror regerror |
| 4042 | #define jim_regfree regfree |
| 4043 | #endif |
| 4044 | |
| 4045 | static void FreeRegexpInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) |
| 4046 | { |
| 4047 | jim_regfree(objPtr->internalRep.ptrIntValue.ptr); |
| 4048 | Jim_Free(objPtr->internalRep.ptrIntValue.ptr); |
| 4049 | } |
| 4050 | |
| 4051 | static const Jim_ObjType regexpObjType = { |
| 4052 | "regexp", |
| 4053 | FreeRegexpInternalRep, |
| 4054 | NULL, |
| 4055 | NULL, |
| 4056 | JIM_TYPE_NONE |
| 4057 | }; |
| 4058 | |
| 4059 | static regex_t *SetRegexpFromAny(Jim_Interp *interp, Jim_Obj *objPtr, unsigned flags) |
| 4060 | { |
| 4061 | regex_t *compre; |
| 4062 | const char *pattern; |
| 4063 | int ret; |
| 4064 | |
| 4065 | |
| 4066 | if (objPtr->typePtr == ®expObjType && |
| 4067 | objPtr->internalRep.ptrIntValue.ptr && objPtr->internalRep.ptrIntValue.int1 == flags) { |
| 4068 | |
| 4069 | return objPtr->internalRep.ptrIntValue.ptr; |
| 4070 | } |
| 4071 | |
| 4072 | |
| 4073 | |
| 4074 | |
| 4075 | pattern = Jim_String(objPtr); |
| 4076 | compre = Jim_Alloc(sizeof(regex_t)); |
| 4077 | |
| 4078 | if ((ret = jim_regcomp(compre, pattern, REG_EXTENDED | flags)) != 0) { |
| 4079 | char buf[100]; |
| 4080 | |
| 4081 | jim_regerror(ret, compre, buf, sizeof(buf)); |
| 4082 | Jim_SetResultFormatted(interp, "couldn't compile regular expression pattern: %s", buf); |
| 4083 | jim_regfree(compre); |
| 4084 | Jim_Free(compre); |
| 4085 | return NULL; |
| 4086 | } |
| 4087 | |
| 4088 | Jim_FreeIntRep(interp, objPtr); |
| 4089 | |
| 4090 | objPtr->typePtr = ®expObjType; |
| 4091 | objPtr->internalRep.ptrIntValue.int1 = flags; |
| 4092 | objPtr->internalRep.ptrIntValue.ptr = compre; |
| 4093 | |
| 4094 | return compre; |
| 4095 | } |
| 4096 | |
| 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; |
| 4108 | int result = JIM_OK; |
| 4109 | const char *pattern; |
| 4110 | const char *source_str; |
| 4111 | int num_matches = 0; |
| 4112 | int num_vars; |
| 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; |
| 4136 | } |
| 4137 | switch (option) { |
| 4138 | case OPT_INDICES: |
| 4139 | opt_indices = 1; |
| 4140 | break; |
| 4141 | |
| 4142 | case OPT_NOCASE: |
| 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: |
| 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; |
| 4197 | } |
| 4198 | |
| 4199 | pattern = Jim_String(argv[i]); |
| 4200 | source_str = Jim_GetString(argv[i + 1], &source_len); |
| 4201 | |
| 4202 | num_vars = argc - i - 2; |
| 4203 | |
| 4204 | if (opt_inline) { |
| 4205 | if (num_vars) { |
| 4206 | Jim_SetResultString(interp, "regexp match variables not allowed when using -inline", |
| 4207 | -1); |
| 4208 | result = JIM_ERR; |
| 4209 | goto done; |
| 4210 | } |
| 4211 | num_vars = regex->re_nsub + 1; |
| 4212 | } |
| 4213 | |
| 4214 | pmatch = Jim_Alloc((num_vars + 1) * sizeof(*pmatch)); |
| 4215 | |
| 4216 | if (offset) { |
| 4217 | if (offset < 0) { |
| 4218 | offset += source_len + 1; |
| 4219 | } |
| 4220 | if (offset > source_len) { |
| 4221 | source_str += source_len; |
| 4222 | } |
| 4223 | else if (offset > 0) { |
| 4224 | source_str += utf8_index(source_str, offset); |
| 4225 | } |
| 4226 | eflags |= REG_NOTBOL; |
| 4227 | } |
| 4228 | |
| 4229 | if (opt_inline) { |
| 4230 | resultListObj = Jim_NewListObj(interp, NULL, 0); |
| 4231 | } |
| 4232 | |
| 4233 | next_match: |
| 4234 | match = jim_regexec(regex, source_str, num_vars + 1, pmatch, eflags); |
| 4235 | if (match >= REG_BADPAT) { |
| 4236 | char buf[100]; |
| 4237 | |
| 4238 | jim_regerror(match, regex, buf, sizeof(buf)); |
| 4239 | Jim_SetResultFormatted(interp, "error while matching pattern: %s", buf); |
| 4240 | result = JIM_ERR; |
| 4241 | goto done; |
| 4242 | } |
| 4243 | |
| 4244 | if (match == REG_NOMATCH) { |
| 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 | } |
| 4259 | else { |
| 4260 | resultObj = Jim_NewStringObj(interp, "", 0); |
| 4261 | } |
| 4262 | |
| 4263 | if (pmatch[j].rm_so == -1) { |
| 4264 | if (opt_indices) { |
| 4265 | Jim_ListAppendElement(interp, resultObj, Jim_NewIntObj(interp, -1)); |
| 4266 | Jim_ListAppendElement(interp, resultObj, Jim_NewIntObj(interp, -1)); |
| 4267 | } |
| 4268 | } |
| 4269 | else { |
| 4270 | if (opt_indices) { |
| 4271 | |
| 4272 | int so = utf8_strlen(source_str, pmatch[j].rm_so); |
| 4273 | int eo = utf8_strlen(source_str, pmatch[j].rm_eo); |
| 4274 | Jim_ListAppendElement(interp, resultObj, Jim_NewIntObj(interp, offset + so)); |
| 4275 | Jim_ListAppendElement(interp, resultObj, Jim_NewIntObj(interp, offset + eo - 1)); |
| 4276 | } |
| 4277 | else { |
| 4278 | Jim_AppendString(interp, resultObj, source_str + pmatch[j].rm_so, pmatch[j].rm_eo - pmatch[j].rm_so); |
| 4279 | } |
| 4280 | } |
| 4281 | |
| 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 { |
| 4301 | source_str++; |
| 4302 | offset++; |
| 4303 | } |
| 4304 | if (*source_str) { |
| 4305 | eflags = REG_NOTBOL; |
| 4306 | goto next_match; |
| 4307 | } |
| 4308 | } |
| 4309 | |
| 4310 | done: |
| 4311 | if (result == JIM_OK) { |
| 4312 | if (opt_inline) { |
| 4313 | Jim_SetResult(interp, resultListObj); |
| 4314 | } |
| 4315 | else { |
| 4316 | Jim_SetResultInt(interp, num_matches); |
| 4317 | } |
| 4318 | } |
| 4319 | |
| 4320 | Jim_Free(pmatch); |
| 4321 | return result; |
| 4322 | } |
| 4323 | |
| 4324 | #define MAX_SUB_MATCHES 50 |
| 4325 | |
| 4326 | int Jim_RegsubCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 4327 | { |
| 4328 | int regcomp_flags = 0; |
| 4329 | int regexec_flags = 0; |
| 4330 | int opt_all = 0; |
| 4331 | int opt_command = 0; |
| 4332 | int offset = 0; |
| 4333 | regex_t *regex; |
| 4334 | const char *p; |
| 4335 | int result = JIM_OK; |
| 4336 | regmatch_t pmatch[MAX_SUB_MATCHES + 1]; |
| 4337 | int num_matches = 0; |
| 4338 | |
| 4339 | int i, j, n; |
| 4340 | Jim_Obj *varname; |
| 4341 | Jim_Obj *resultObj; |
| 4342 | Jim_Obj *cmd_prefix = NULL; |
| 4343 | Jim_Obj *regcomp_obj = NULL; |
| 4344 | const char *source_str; |
| 4345 | int source_len; |
| 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; |
| 4369 | } |
| 4370 | switch (option) { |
| 4371 | case OPT_NOCASE: |
| 4372 | regcomp_flags |= REG_ICASE; |
| 4373 | break; |
| 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 { |
| 4443 | replace_str = Jim_GetString(argv[i + 2], &replace_len); |
| 4444 | } |
| 4445 | varname = argv[i + 3]; |
| 4446 | |
| 4447 | |
| 4448 | resultObj = Jim_NewStringObj(interp, "", 0); |
| 4449 | |
| 4450 | if (offset) { |
| 4451 | if (offset < 0) { |
| 4452 | offset += source_len + 1; |
| 4453 | } |
| 4454 | if (offset > source_len) { |
| 4455 | offset = source_len; |
| 4456 | } |
| 4457 | else if (offset < 0) { |
| 4458 | offset = 0; |
| 4459 | } |
| 4460 | } |
| 4461 | |
| 4462 | offset = utf8_index(source_str, offset); |
| 4463 | |
| 4464 | |
| 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 | |
| 4477 | jim_regerror(match, regex, buf, sizeof(buf)); |
| 4478 | Jim_SetResultFormatted(interp, "error while matching pattern: %s", buf); |
| 4479 | return JIM_ERR; |
| 4480 | } |
| 4481 | if (match == REG_NOMATCH) { |
| 4482 | break; |
| 4483 | } |
| 4484 | |
| 4485 | num_matches++; |
| 4486 | |
| 4487 | Jim_AppendString(interp, resultObj, p, pmatch[0].rm_so); |
| 4488 | |
| 4489 | if (opt_command) { |
| 4490 | |
| 4491 | Jim_Obj *cmdListObj = Jim_DuplicateObj(interp, cmd_prefix); |
| 4492 | for (j = 0; j < MAX_SUB_MATCHES; j++) { |
| 4493 | if (pmatch[j].rm_so == -1) { |
| 4494 | break; |
| 4495 | } |
| 4496 | else { |
| 4497 | Jim_Obj *srcObj = Jim_NewStringObj(interp, p + pmatch[j].rm_so, pmatch[j].rm_eo - pmatch[j].rm_so); |
| 4498 | Jim_ListAppendElement(interp, cmdListObj, srcObj); |
| 4499 | } |
| 4500 | } |
| 4501 | Jim_IncrRefCount(cmdListObj); |
| 4502 | |
| 4503 | result = Jim_EvalObj(interp, cmdListObj); |
| 4504 | Jim_DecrRefCount(interp, cmdListObj); |
| 4505 | if (result != JIM_OK) { |
| 4506 | goto cmd_error; |
| 4507 | } |
| 4508 | Jim_AppendString(interp, resultObj, Jim_String(Jim_GetResult(interp)), -1); |
| 4509 | } |
| 4510 | else { |
| 4511 | |
| 4512 | for (j = 0; j < replace_len; j++) { |
| 4513 | int idx; |
| 4514 | int c = replace_str[j]; |
| 4515 | |
| 4516 | if (c == '&') { |
| 4517 | idx = 0; |
| 4518 | } |
| 4519 | else if (c == '\\' && j < replace_len) { |
| 4520 | c = replace_str[++j]; |
| 4521 | if ((c >= '0') && (c <= '9')) { |
| 4522 | idx = c - '0'; |
| 4523 | } |
| 4524 | else if ((c == '\\') || (c == '&')) { |
| 4525 | Jim_AppendString(interp, resultObj, replace_str + j, 1); |
| 4526 | continue; |
| 4527 | } |
| 4528 | else { |
| 4529 | Jim_AppendString(interp, resultObj, replace_str + j - 1, (j == replace_len) ? 1 : 2); |
| 4530 | continue; |
| 4531 | } |
| 4532 | } |
| 4533 | else { |
| 4534 | Jim_AppendString(interp, resultObj, replace_str + j, 1); |
| 4535 | continue; |
| 4536 | } |
| 4537 | if ((idx < MAX_SUB_MATCHES) && pmatch[idx].rm_so != -1 && pmatch[idx].rm_eo != -1) { |
| 4538 | Jim_AppendString(interp, resultObj, p + pmatch[idx].rm_so, |
| 4539 | pmatch[idx].rm_eo - pmatch[idx].rm_so); |
| 4540 | } |
| 4541 | } |
| 4542 | } |
| 4543 | |
| 4544 | p += pmatch[0].rm_eo; |
| 4545 | n -= pmatch[0].rm_eo; |
| 4546 | |
| 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) { |
| 4573 | |
| 4574 | if (argc - i == 4) { |
| 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 | } |
| 4585 | } |
| 4586 | else { |
| 4587 | Jim_FreeObj(interp, resultObj); |
| 4588 | } |
| 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> |
| 4609 | #include <string.h> |
| 4610 | #include <stdio.h> |
| 4611 | #include <errno.h> |
| 4612 | |
| 4613 | |
| 4614 | #ifdef HAVE_UTIMES |
| 4615 | #include <sys/time.h> |
| 4616 | #endif |
| 4617 | #ifdef HAVE_UNISTD_H |
| 4618 | #include <unistd.h> |
| 4619 | #elif defined(_MSC_VER) |
| 4620 | #include <direct.h> |
| 4621 | #define F_OK 0 |
| 4622 | #define W_OK 2 |
| 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 |
| 4633 | #define ISWINDOWS 0 |
| 4634 | #endif |
| 4635 | |
| 4636 | |
| 4637 | #if defined(HAVE_STRUCT_STAT_ST_MTIMESPEC) |
| 4638 | #define STAT_MTIME_US(STAT) ((STAT).st_mtimespec.tv_sec * 1000000ll + (STAT).st_mtimespec.tv_nsec / 1000) |
| 4639 | #elif defined(HAVE_STRUCT_STAT_ST_MTIM) |
| 4640 | #define STAT_MTIME_US(STAT) ((STAT).st_mtim.tv_sec * 1000000ll + (STAT).st_mtim.tv_nsec / 1000) |
| 4641 | #endif |
| 4642 | |
| 4643 | |
| 4644 | static void JimFixPath(char *path) |
| 4645 | { |
| 4646 | if (ISWINDOWS) { |
| 4647 | |
| 4648 | char *p = path; |
| 4649 | while ((p = strchr(p, '\\')) != NULL) { |
| 4650 | *p++ = '/'; |
| 4651 | } |
| 4652 | } |
| 4653 | } |
| 4654 | |
| 4655 | |
| 4656 | static const char *JimGetFileType(int mode) |
| 4657 | { |
| 4658 | if (S_ISREG(mode)) { |
| 4659 | return "file"; |
| 4660 | } |
| 4661 | else if (S_ISDIR(mode)) { |
| 4662 | return "directory"; |
| 4663 | } |
| 4664 | #ifdef S_ISCHR |
| 4665 | else if (S_ISCHR(mode)) { |
| 4666 | return "characterSpecial"; |
| 4667 | } |
| 4668 | #endif |
| 4669 | #ifdef S_ISBLK |
| 4670 | else if (S_ISBLK(mode)) { |
| 4671 | return "blockSpecial"; |
| 4672 | } |
| 4673 | #endif |
| 4674 | #ifdef S_ISFIFO |
| 4675 | else if (S_ISFIFO(mode)) { |
| 4676 | return "fifo"; |
| 4677 | } |
| 4678 | #endif |
| 4679 | #ifdef S_ISLNK |
| 4680 | else if (S_ISLNK(mode)) { |
| 4681 | return "link"; |
| 4682 | } |
| 4683 | #endif |
| 4684 | #ifdef S_ISSOCK |
| 4685 | else if (S_ISSOCK(mode)) { |
| 4686 | return "socket"; |
| 4687 | } |
| 4688 | #endif |
| 4689 | return "unknown"; |
| 4690 | } |
| 4691 | |
| 4692 | static void AppendStatElement(Jim_Interp *interp, Jim_Obj *listObj, const char *key, jim_wide value) |
| 4693 | { |
| 4694 | Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, key, -1)); |
| 4695 | Jim_ListAppendElement(interp, listObj, Jim_NewIntObj(interp, value)); |
| 4696 | } |
| 4697 | |
| 4698 | int Jim_FileStoreStatData(Jim_Interp *interp, Jim_Obj *varName, const jim_stat_t *sb) |
| 4699 | { |
| 4700 | |
| 4701 | Jim_Obj *listObj = Jim_NewListObj(interp, NULL, 0); |
| 4702 | |
| 4703 | AppendStatElement(interp, listObj, "dev", sb->st_dev); |
| 4704 | AppendStatElement(interp, listObj, "ino", sb->st_ino); |
| 4705 | AppendStatElement(interp, listObj, "mode", sb->st_mode); |
| 4706 | AppendStatElement(interp, listObj, "nlink", sb->st_nlink); |
| 4707 | AppendStatElement(interp, listObj, "uid", sb->st_uid); |
| 4708 | AppendStatElement(interp, listObj, "gid", sb->st_gid); |
| 4709 | AppendStatElement(interp, listObj, "size", sb->st_size); |
| 4710 | AppendStatElement(interp, listObj, "atime", sb->st_atime); |
| 4711 | AppendStatElement(interp, listObj, "mtime", sb->st_mtime); |
| 4712 | AppendStatElement(interp, listObj, "ctime", sb->st_ctime); |
| 4713 | #ifdef STAT_MTIME_US |
| 4714 | AppendStatElement(interp, listObj, "mtimeus", STAT_MTIME_US(*sb)); |
| 4715 | #endif |
| 4716 | Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, "type", -1)); |
| 4717 | Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, JimGetFileType((int)sb->st_mode), -1)); |
| 4718 | |
| 4719 | |
| 4720 | if (varName) { |
| 4721 | Jim_Obj *objPtr; |
| 4722 | objPtr = Jim_GetVariable(interp, varName, JIM_NONE); |
| 4723 | |
| 4724 | if (objPtr) { |
| 4725 | Jim_Obj *objv[2]; |
| 4726 | |
| 4727 | objv[0] = objPtr; |
| 4728 | objv[1] = listObj; |
| 4729 | |
| 4730 | objPtr = Jim_DictMerge(interp, 2, objv); |
| 4731 | if (objPtr == NULL) { |
| 4732 | |
| 4733 | Jim_SetResultFormatted(interp, "can't set \"%#s(dev)\": variable isn't array", varName); |
| 4734 | Jim_FreeNewObj(interp, listObj); |
| 4735 | return JIM_ERR; |
| 4736 | } |
| 4737 | |
| 4738 | Jim_InvalidateStringRep(objPtr); |
| 4739 | |
| 4740 | Jim_FreeNewObj(interp, listObj); |
| 4741 | listObj = objPtr; |
| 4742 | } |
| 4743 | Jim_SetVariable(interp, varName, listObj); |
| 4744 | } |
| 4745 | |
| 4746 | |
| 4747 | Jim_SetResult(interp, listObj); |
| 4748 | |
| 4749 | return JIM_OK; |
| 4750 | } |
| 4751 | |
| 4752 | static int JimPathLenNoTrailingSlashes(const char *path, int len) |
| 4753 | { |
| 4754 | int i; |
| 4755 | for (i = len; i > 1 && path[i - 1] == '/'; i--) { |
| 4756 | |
| 4757 | if (ISWINDOWS && path[i - 2] == ':') { |
| 4758 | |
| 4759 | break; |
| 4760 | } |
| 4761 | } |
| 4762 | return i; |
| 4763 | } |
| 4764 | |
| 4765 | static Jim_Obj *JimStripTrailingSlashes(Jim_Interp *interp, Jim_Obj *objPtr) |
| 4766 | { |
| 4767 | int len = Jim_Length(objPtr); |
| 4768 | const char *path = Jim_String(objPtr); |
| 4769 | int i = JimPathLenNoTrailingSlashes(path, len); |
| 4770 | if (i != len) { |
| 4771 | objPtr = Jim_NewStringObj(interp, path, i); |
| 4772 | } |
| 4773 | Jim_IncrRefCount(objPtr); |
| 4774 | return objPtr; |
| 4775 | } |
| 4776 | |
| 4777 | static int file_cmd_dirname(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 4778 | { |
| 4779 | Jim_Obj *objPtr = JimStripTrailingSlashes(interp, argv[0]); |
| 4780 | const char *path = Jim_String(objPtr); |
| 4781 | const char *p = strrchr(path, '/'); |
| 4782 | |
| 4783 | if (!p) { |
| 4784 | Jim_SetResultString(interp, ".", -1); |
| 4785 | } |
| 4786 | else if (p[1] == 0) { |
| 4787 | |
| 4788 | Jim_SetResult(interp, objPtr); |
| 4789 | } |
| 4790 | else if (p == path) { |
| 4791 | Jim_SetResultString(interp, "/", -1); |
| 4792 | } |
| 4793 | else if (ISWINDOWS && p[-1] == ':') { |
| 4794 | |
| 4795 | Jim_SetResultString(interp, path, p - path + 1); |
| 4796 | } |
| 4797 | else { |
| 4798 | |
| 4799 | int len = JimPathLenNoTrailingSlashes(path, p - path); |
| 4800 | Jim_SetResultString(interp, path, len); |
| 4801 | } |
| 4802 | Jim_DecrRefCount(interp, objPtr); |
| 4803 | return JIM_OK; |
| 4804 | } |
| 4805 | |
| 4806 | static int file_cmd_split(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 4807 | { |
| 4808 | Jim_Obj *listObj = Jim_NewListObj(interp, NULL, 0); |
| 4809 | const char *path = Jim_String(argv[0]); |
| 4810 | |
| 4811 | if (*path == '/') { |
| 4812 | Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, "/", 1)); |
| 4813 | } |
| 4814 | |
| 4815 | while (1) { |
| 4816 | |
| 4817 | while (*path == '/') { |
| 4818 | path++; |
| 4819 | } |
| 4820 | if (*path) { |
| 4821 | const char *pt = strchr(path, '/'); |
| 4822 | if (pt) { |
| 4823 | Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, path, pt - path)); |
| 4824 | path = pt; |
| 4825 | continue; |
| 4826 | } |
| 4827 | Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, path, -1)); |
| 4828 | } |
| 4829 | break; |
| 4830 | } |
| 4831 | Jim_SetResult(interp, listObj); |
| 4832 | return JIM_OK; |
| 4833 | } |
| 4834 | |
| 4835 | static int file_cmd_rootname(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 4836 | { |
| 4837 | const char *path = Jim_String(argv[0]); |
| 4838 | const char *lastSlash = strrchr(path, '/'); |
| 4839 | const char *p = strrchr(path, '.'); |
| 4840 | |
| 4841 | if (p == NULL || (lastSlash != NULL && lastSlash > p)) { |
| 4842 | Jim_SetResult(interp, argv[0]); |
| 4843 | } |
| 4844 | else { |
| 4845 | Jim_SetResultString(interp, path, p - path); |
| 4846 | } |
| 4847 | return JIM_OK; |
| 4848 | } |
| 4849 | |
| 4850 | static int file_cmd_extension(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 4851 | { |
| 4852 | Jim_Obj *objPtr = JimStripTrailingSlashes(interp, argv[0]); |
| 4853 | const char *path = Jim_String(objPtr); |
| 4854 | const char *lastSlash = strrchr(path, '/'); |
| 4855 | const char *p = strrchr(path, '.'); |
| 4856 | |
| 4857 | if (p == NULL || (lastSlash != NULL && lastSlash >= p)) { |
| 4858 | p = ""; |
| 4859 | } |
| 4860 | Jim_SetResultString(interp, p, -1); |
| 4861 | Jim_DecrRefCount(interp, objPtr); |
| 4862 | return JIM_OK; |
| 4863 | } |
| 4864 | |
| 4865 | static int file_cmd_tail(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 4866 | { |
| 4867 | Jim_Obj *objPtr = JimStripTrailingSlashes(interp, argv[0]); |
| 4868 | const char *path = Jim_String(objPtr); |
| 4869 | const char *lastSlash = strrchr(path, '/'); |
| 4870 | |
| 4871 | if (lastSlash) { |
| 4872 | Jim_SetResultString(interp, lastSlash + 1, -1); |
| 4873 | } |
| 4874 | else { |
| 4875 | Jim_SetResult(interp, objPtr); |
| 4876 | } |
| 4877 | Jim_DecrRefCount(interp, objPtr); |
| 4878 | return JIM_OK; |
| 4879 | } |
| 4880 | |
| 4881 | #ifndef HAVE_RESTRICT |
| 4882 | #define restrict |
| 4883 | #endif |
| 4884 | |
| 4885 | static char *JimRealPath(const char *restrict path, char *restrict resolved_path, size_t len) |
| 4886 | { |
| 4887 | #if defined(HAVE__FULLPATH) |
| 4888 | return _fullpath(resolved_path, path, len); |
| 4889 | #elif defined(HAVE_REALPATH) |
| 4890 | return realpath(path, resolved_path); |
| 4891 | #else |
| 4892 | return NULL; |
| 4893 | #endif |
| 4894 | } |
| 4895 | |
| 4896 | static int file_cmd_normalize(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 4897 | { |
| 4898 | const char *path = Jim_String(argv[0]); |
| 4899 | char *newname = Jim_Alloc(MAXPATHLEN); |
| 4900 | |
| 4901 | if (JimRealPath(path, newname, MAXPATHLEN)) { |
| 4902 | JimFixPath(newname); |
| 4903 | Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, newname, -1)); |
| 4904 | return JIM_OK; |
| 4905 | } |
| 4906 | Jim_Free(newname); |
| 4907 | Jim_SetResultFormatted(interp, "can't normalize \"%#s\": %s", argv[0], strerror(errno)); |
| 4908 | return JIM_ERR; |
| 4909 | } |
| 4910 | |
| 4911 | static int file_cmd_join(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 4912 | { |
| 4913 | int i; |
| 4914 | char *newname = Jim_Alloc(MAXPATHLEN + 1); |
| 4915 | char *last = newname; |
| 4916 | |
| 4917 | *newname = 0; |
| 4918 | |
| 4919 | |
| 4920 | for (i = 0; i < argc; i++) { |
| 4921 | int len; |
| 4922 | const char *part = Jim_GetString(argv[i], &len); |
| 4923 | |
| 4924 | if (*part == '/') { |
| 4925 | |
| 4926 | last = newname; |
| 4927 | } |
| 4928 | else if (ISWINDOWS && strchr(part, ':')) { |
| 4929 | |
| 4930 | last = newname; |
| 4931 | } |
| 4932 | else if (part[0] == '.') { |
| 4933 | if (part[1] == '/') { |
| 4934 | part += 2; |
| 4935 | len -= 2; |
| 4936 | } |
| 4937 | else if (part[1] == 0 && last != newname) { |
| 4938 | |
| 4939 | continue; |
| 4940 | } |
| 4941 | } |
| 4942 | |
| 4943 | |
| 4944 | if (last != newname && last[-1] != '/') { |
| 4945 | *last++ = '/'; |
| 4946 | } |
| 4947 | |
| 4948 | if (len) { |
| 4949 | if (last + len - newname >= MAXPATHLEN) { |
| 4950 | Jim_Free(newname); |
| 4951 | Jim_SetResultString(interp, "Path too long", -1); |
| 4952 | return JIM_ERR; |
| 4953 | } |
| 4954 | memcpy(last, part, len); |
| 4955 | last += len; |
| 4956 | } |
| 4957 | |
| 4958 | |
| 4959 | if (last > newname + 1 && last[-1] == '/') { |
| 4960 | |
| 4961 | if (!ISWINDOWS || !(last > newname + 2 && last[-2] == ':')) { |
| 4962 | *--last = 0; |
| 4963 | } |
| 4964 | } |
| 4965 | } |
| 4966 | |
| 4967 | *last = 0; |
| 4968 | |
| 4969 | |
| 4970 | |
| 4971 | Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, newname, last - newname)); |
| 4972 | |
| 4973 | return JIM_OK; |
| 4974 | } |
| 4975 | |
| 4976 | static int file_access(Jim_Interp *interp, Jim_Obj *filename, int mode) |
| 4977 | { |
| 4978 | Jim_SetResultBool(interp, access(Jim_String(filename), mode) != -1); |
| 4979 | |
| 4980 | return JIM_OK; |
| 4981 | } |
| 4982 | |
| 4983 | static int file_cmd_readable(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 4984 | { |
| 4985 | return file_access(interp, argv[0], R_OK); |
| 4986 | } |
| 4987 | |
| 4988 | static int file_cmd_writable(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 4989 | { |
| 4990 | return file_access(interp, argv[0], W_OK); |
| 4991 | } |
| 4992 | |
| 4993 | static int file_cmd_executable(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 4994 | { |
| 4995 | #ifdef X_OK |
| 4996 | return file_access(interp, argv[0], X_OK); |
| 4997 | #else |
| 4998 | |
| 4999 | Jim_SetResultBool(interp, 1); |
| 5000 | return JIM_OK; |
| 5001 | #endif |
| 5002 | } |
| 5003 | |
| 5004 | static int file_cmd_exists(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 5005 | { |
| 5006 | return file_access(interp, argv[0], F_OK); |
| 5007 | } |
| 5008 | |
| 5009 | static int file_cmd_delete(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 5010 | { |
| 5011 | int force = Jim_CompareStringImmediate(interp, argv[0], "-force"); |
| 5012 | |
| 5013 | if (force || Jim_CompareStringImmediate(interp, argv[0], "--")) { |
| 5014 | argc--; |
| 5015 | argv++; |
| 5016 | } |
| 5017 | |
| 5018 | while (argc--) { |
| 5019 | const char *path = Jim_String(argv[0]); |
| 5020 | |
| 5021 | if (unlink(path) == -1 && errno != ENOENT) { |
| 5022 | if (rmdir(path) == -1) { |
| 5023 | |
| 5024 | if (!force || Jim_EvalPrefix(interp, "file delete force", 1, argv) != JIM_OK) { |
| 5025 | Jim_SetResultFormatted(interp, "couldn't delete file \"%s\": %s", path, |
| 5026 | strerror(errno)); |
| 5027 | return JIM_ERR; |
| 5028 | } |
| 5029 | } |
| 5030 | } |
| 5031 | argv++; |
| 5032 | } |
| 5033 | return JIM_OK; |
| 5034 | } |
| 5035 | |
| 5036 | #ifdef HAVE_MKDIR_ONE_ARG |
| 5037 | #define MKDIR_DEFAULT(PATHNAME) mkdir(PATHNAME) |
| 5038 | #else |
| 5039 | #define MKDIR_DEFAULT(PATHNAME) mkdir(PATHNAME, 0755) |
| 5040 | #endif |
| 5041 | |
| 5042 | static int mkdir_all(char *path) |
| 5043 | { |
| 5044 | int ok = 1; |
| 5045 | |
| 5046 | |
| 5047 | goto first; |
| 5048 | |
| 5049 | while (ok--) { |
| 5050 | |
| 5051 | { |
| 5052 | char *slash = strrchr(path, '/'); |
| 5053 | |
| 5054 | if (slash && slash != path) { |
| 5055 | *slash = 0; |
| 5056 | if (mkdir_all(path) != 0) { |
| 5057 | return -1; |
| 5058 | } |
| 5059 | *slash = '/'; |
| 5060 | } |
| 5061 | } |
| 5062 | first: |
| 5063 | if (MKDIR_DEFAULT(path) == 0) { |
| 5064 | return 0; |
| 5065 | } |
| 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 | } |
| 5077 | |
| 5078 | errno = EEXIST; |
| 5079 | } |
| 5080 | |
| 5081 | break; |
| 5082 | } |
| 5083 | return -1; |
| 5084 | } |
| 5085 | |
| 5086 | static int file_cmd_mkdir(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 5087 | { |
| 5088 | while (argc--) { |
| 5089 | char *path = Jim_StrDup(Jim_String(argv[0])); |
| 5090 | int rc = mkdir_all(path); |
| 5091 | |
| 5092 | Jim_Free(path); |
| 5093 | if (rc != 0) { |
| 5094 | Jim_SetResultFormatted(interp, "can't create directory \"%#s\": %s", argv[0], |
| 5095 | strerror(errno)); |
| 5096 | return JIM_ERR; |
| 5097 | } |
| 5098 | argv++; |
| 5099 | } |
| 5100 | return JIM_OK; |
| 5101 | } |
| 5102 | |
| 5103 | static int file_cmd_tempfile(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 5104 | { |
| 5105 | int fd = Jim_MakeTempFile(interp, (argc >= 1) ? Jim_String(argv[0]) : NULL, 0); |
| 5106 | |
| 5107 | if (fd < 0) { |
| 5108 | return JIM_ERR; |
| 5109 | } |
| 5110 | close(fd); |
| 5111 | |
| 5112 | return JIM_OK; |
| 5113 | } |
| 5114 | |
| 5115 | static int file_cmd_rename(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 5116 | { |
| 5117 | const char *source; |
| 5118 | const char *dest; |
| 5119 | int force = 0; |
| 5120 | |
| 5121 | if (argc == 3) { |
| 5122 | if (!Jim_CompareStringImmediate(interp, argv[0], "-force")) { |
| 5123 | return -1; |
| 5124 | } |
| 5125 | force++; |
| 5126 | argv++; |
| 5127 | argc--; |
| 5128 | } |
| 5129 | |
| 5130 | source = Jim_String(argv[0]); |
| 5131 | dest = Jim_String(argv[1]); |
| 5132 | |
| 5133 | if (!force && access(dest, F_OK) == 0) { |
| 5134 | Jim_SetResultFormatted(interp, "error renaming \"%#s\" to \"%#s\": target exists", argv[0], |
| 5135 | argv[1]); |
| 5136 | return JIM_ERR; |
| 5137 | } |
| 5138 | #if ISWINDOWS |
| 5139 | if (access(dest, F_OK) == 0) { |
| 5140 | |
| 5141 | remove(dest); |
| 5142 | } |
| 5143 | #endif |
| 5144 | if (rename(source, dest) != 0) { |
| 5145 | Jim_SetResultFormatted(interp, "error renaming \"%#s\" to \"%#s\": %s", argv[0], argv[1], |
| 5146 | strerror(errno)); |
| 5147 | return JIM_ERR; |
| 5148 | } |
| 5149 | |
| 5150 | return JIM_OK; |
| 5151 | } |
| 5152 | |
| 5153 | #if defined(HAVE_LINK) && defined(HAVE_SYMLINK) |
| 5154 | static int file_cmd_link(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 5155 | { |
| 5156 | int ret; |
| 5157 | const char *source; |
| 5158 | const char *dest; |
| 5159 | static const char * const options[] = { "-hard", "-symbolic", NULL }; |
| 5160 | enum { OPT_HARD, OPT_SYMBOLIC, }; |
| 5161 | int option = OPT_HARD; |
| 5162 | |
| 5163 | if (argc == 3) { |
| 5164 | if (Jim_GetEnum(interp, argv[0], options, &option, NULL, JIM_ENUM_ABBREV | JIM_ERRMSG) != JIM_OK) { |
| 5165 | return JIM_ERR; |
| 5166 | } |
| 5167 | argv++; |
| 5168 | argc--; |
| 5169 | } |
| 5170 | |
| 5171 | dest = Jim_String(argv[0]); |
| 5172 | source = Jim_String(argv[1]); |
| 5173 | |
| 5174 | if (option == OPT_HARD) { |
| 5175 | ret = link(source, dest); |
| 5176 | } |
| 5177 | else { |
| 5178 | ret = symlink(source, dest); |
| 5179 | } |
| 5180 | |
| 5181 | if (ret != 0) { |
| 5182 | Jim_SetResultFormatted(interp, "error linking \"%#s\" to \"%#s\": %s", argv[0], argv[1], |
| 5183 | strerror(errno)); |
| 5184 | return JIM_ERR; |
| 5185 | } |
| 5186 | |
| 5187 | return JIM_OK; |
| 5188 | } |
| 5189 | #endif |
| 5190 | |
| 5191 | static int file_stat(Jim_Interp *interp, Jim_Obj *filename, jim_stat_t *sb) |
| 5192 | { |
| 5193 | const char *path = Jim_String(filename); |
| 5194 | |
| 5195 | if (Jim_Stat(path, sb) == -1) { |
| 5196 | Jim_SetResultFormatted(interp, "could not read \"%#s\": %s", filename, strerror(errno)); |
| 5197 | return JIM_ERR; |
| 5198 | } |
| 5199 | return JIM_OK; |
| 5200 | } |
| 5201 | |
| 5202 | #ifdef Jim_LinkStat |
| 5203 | static int file_lstat(Jim_Interp *interp, Jim_Obj *filename, jim_stat_t *sb) |
| 5204 | { |
| 5205 | const char *path = Jim_String(filename); |
| 5206 | |
| 5207 | if (Jim_LinkStat(path, sb) == -1) { |
| 5208 | Jim_SetResultFormatted(interp, "could not read \"%#s\": %s", filename, strerror(errno)); |
| 5209 | return JIM_ERR; |
| 5210 | } |
| 5211 | return JIM_OK; |
| 5212 | } |
| 5213 | #else |
| 5214 | #define file_lstat file_stat |
| 5215 | #endif |
| 5216 | |
| 5217 | static int file_cmd_atime(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 5218 | { |
| 5219 | jim_stat_t sb; |
| 5220 | |
| 5221 | if (file_stat(interp, argv[0], &sb) != JIM_OK) { |
| 5222 | return JIM_ERR; |
| 5223 | } |
| 5224 | Jim_SetResultInt(interp, sb.st_atime); |
| 5225 | return JIM_OK; |
| 5226 | } |
| 5227 | |
| 5228 | static int JimSetFileTimes(Jim_Interp *interp, const char *filename, jim_wide us) |
| 5229 | { |
| 5230 | #ifdef HAVE_UTIMES |
| 5231 | struct timeval times[2]; |
| 5232 | |
| 5233 | times[1].tv_sec = times[0].tv_sec = us / 1000000; |
| 5234 | times[1].tv_usec = times[0].tv_usec = us % 1000000; |
| 5235 | |
| 5236 | if (utimes(filename, times) != 0) { |
| 5237 | Jim_SetResultFormatted(interp, "can't set time on \"%s\": %s", filename, strerror(errno)); |
| 5238 | return JIM_ERR; |
| 5239 | } |
| 5240 | return JIM_OK; |
| 5241 | #else |
| 5242 | Jim_SetResultString(interp, "Not implemented", -1); |
| 5243 | return JIM_ERR; |
| 5244 | #endif |
| 5245 | } |
| 5246 | |
| 5247 | static int file_cmd_mtime(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 5248 | { |
| 5249 | jim_stat_t sb; |
| 5250 | |
| 5251 | if (argc == 2) { |
| 5252 | jim_wide secs; |
| 5253 | if (Jim_GetWide(interp, argv[1], &secs) != JIM_OK) { |
| 5254 | return JIM_ERR; |
| 5255 | } |
| 5256 | return JimSetFileTimes(interp, Jim_String(argv[0]), secs * 1000000); |
| 5257 | } |
| 5258 | if (file_stat(interp, argv[0], &sb) != JIM_OK) { |
| 5259 | return JIM_ERR; |
| 5260 | } |
| 5261 | Jim_SetResultInt(interp, sb.st_mtime); |
| 5262 | return JIM_OK; |
| 5263 | } |
| 5264 | |
| 5265 | #ifdef STAT_MTIME_US |
| 5266 | static int file_cmd_mtimeus(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 5267 | { |
| 5268 | jim_stat_t sb; |
| 5269 | |
| 5270 | if (argc == 2) { |
| 5271 | jim_wide us; |
| 5272 | if (Jim_GetWide(interp, argv[1], &us) != JIM_OK) { |
| 5273 | return JIM_ERR; |
| 5274 | } |
| 5275 | return JimSetFileTimes(interp, Jim_String(argv[0]), us); |
| 5276 | } |
| 5277 | if (file_stat(interp, argv[0], &sb) != JIM_OK) { |
| 5278 | return JIM_ERR; |
| 5279 | } |
| 5280 | Jim_SetResultInt(interp, STAT_MTIME_US(sb)); |
| 5281 | return JIM_OK; |
| 5282 | } |
| 5283 | #endif |
| 5284 | |
| 5285 | static int file_cmd_copy(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 5286 | { |
| 5287 | return Jim_EvalPrefix(interp, "file copy", argc, argv); |
| 5288 | } |
| 5289 | |
| 5290 | static int file_cmd_size(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 5291 | { |
| 5292 | jim_stat_t sb; |
| 5293 | |
| 5294 | if (file_stat(interp, argv[0], &sb) != JIM_OK) { |
| 5295 | return JIM_ERR; |
| 5296 | } |
| 5297 | Jim_SetResultInt(interp, sb.st_size); |
| 5298 | return JIM_OK; |
| 5299 | } |
| 5300 | |
| 5301 | static int file_cmd_isdirectory(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 5302 | { |
| 5303 | jim_stat_t sb; |
| 5304 | int ret = 0; |
| 5305 | |
| 5306 | if (file_stat(interp, argv[0], &sb) == JIM_OK) { |
| 5307 | ret = S_ISDIR(sb.st_mode); |
| 5308 | } |
| 5309 | Jim_SetResultInt(interp, ret); |
| 5310 | return JIM_OK; |
| 5311 | } |
| 5312 | |
| 5313 | static int file_cmd_isfile(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 5314 | { |
| 5315 | jim_stat_t sb; |
| 5316 | int ret = 0; |
| 5317 | |
| 5318 | if (file_stat(interp, argv[0], &sb) == JIM_OK) { |
| 5319 | ret = S_ISREG(sb.st_mode); |
| 5320 | } |
| 5321 | Jim_SetResultInt(interp, ret); |
| 5322 | return JIM_OK; |
| 5323 | } |
| 5324 | |
| 5325 | #ifdef HAVE_GETEUID |
| 5326 | static int file_cmd_owned(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 5327 | { |
| 5328 | jim_stat_t sb; |
| 5329 | int ret = 0; |
| 5330 | |
| 5331 | if (file_stat(interp, argv[0], &sb) == JIM_OK) { |
| 5332 | ret = (geteuid() == sb.st_uid); |
| 5333 | } |
| 5334 | Jim_SetResultInt(interp, ret); |
| 5335 | return JIM_OK; |
| 5336 | } |
| 5337 | #endif |
| 5338 | |
| 5339 | #if defined(HAVE_READLINK) |
| 5340 | static int file_cmd_readlink(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 5341 | { |
| 5342 | const char *path = Jim_String(argv[0]); |
| 5343 | char *linkValue = Jim_Alloc(MAXPATHLEN + 1); |
| 5344 | |
| 5345 | int linkLength = readlink(path, linkValue, MAXPATHLEN); |
| 5346 | |
| 5347 | if (linkLength == -1) { |
| 5348 | Jim_Free(linkValue); |
| 5349 | Jim_SetResultFormatted(interp, "could not read link \"%#s\": %s", argv[0], strerror(errno)); |
| 5350 | return JIM_ERR; |
| 5351 | } |
| 5352 | linkValue[linkLength] = 0; |
| 5353 | Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, linkValue, linkLength)); |
| 5354 | return JIM_OK; |
| 5355 | } |
| 5356 | #endif |
| 5357 | |
| 5358 | static int file_cmd_type(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 5359 | { |
| 5360 | jim_stat_t sb; |
| 5361 | |
| 5362 | if (file_lstat(interp, argv[0], &sb) != JIM_OK) { |
| 5363 | return JIM_ERR; |
| 5364 | } |
| 5365 | Jim_SetResultString(interp, JimGetFileType((int)sb.st_mode), -1); |
| 5366 | return JIM_OK; |
| 5367 | } |
| 5368 | |
| 5369 | #ifdef Jim_LinkStat |
| 5370 | static int file_cmd_lstat(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 5371 | { |
| 5372 | jim_stat_t sb; |
| 5373 | |
| 5374 | if (file_lstat(interp, argv[0], &sb) != JIM_OK) { |
| 5375 | return JIM_ERR; |
| 5376 | } |
| 5377 | return Jim_FileStoreStatData(interp, argc == 2 ? argv[1] : NULL, &sb); |
| 5378 | } |
| 5379 | #else |
| 5380 | #define file_cmd_lstat file_cmd_stat |
| 5381 | #endif |
| 5382 | |
| 5383 | static int file_cmd_stat(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 5384 | { |
| 5385 | jim_stat_t sb; |
| 5386 | |
| 5387 | if (file_stat(interp, argv[0], &sb) != JIM_OK) { |
| 5388 | return JIM_ERR; |
| 5389 | } |
| 5390 | return Jim_FileStoreStatData(interp, argc == 2 ? argv[1] : NULL, &sb); |
| 5391 | } |
| 5392 | |
| 5393 | static const jim_subcmd_type file_command_table[] = { |
| 5394 | { "atime", |
| 5395 | "name", |
| 5396 | file_cmd_atime, |
| 5397 | 1, |
| 5398 | 1, |
| 5399 | |
| 5400 | }, |
| 5401 | { "mtime", |
| 5402 | "name ?time?", |
| 5403 | file_cmd_mtime, |
| 5404 | 1, |
| 5405 | 2, |
| 5406 | |
| 5407 | }, |
| 5408 | #ifdef STAT_MTIME_US |
| 5409 | { "mtimeus", |
| 5410 | "name ?time?", |
| 5411 | file_cmd_mtimeus, |
| 5412 | 1, |
| 5413 | 2, |
| 5414 | |
| 5415 | }, |
| 5416 | #endif |
| 5417 | { "copy", |
| 5418 | "?-force? source dest", |
| 5419 | file_cmd_copy, |
| 5420 | 2, |
| 5421 | 3, |
| 5422 | |
| 5423 | }, |
| 5424 | { "dirname", |
| 5425 | "name", |
| 5426 | file_cmd_dirname, |
| 5427 | 1, |
| 5428 | 1, |
| 5429 | |
| 5430 | }, |
| 5431 | { "rootname", |
| 5432 | "name", |
| 5433 | file_cmd_rootname, |
| 5434 | 1, |
| 5435 | 1, |
| 5436 | |
| 5437 | }, |
| 5438 | { "extension", |
| 5439 | "name", |
| 5440 | file_cmd_extension, |
| 5441 | 1, |
| 5442 | 1, |
| 5443 | |
| 5444 | }, |
| 5445 | { "tail", |
| 5446 | "name", |
| 5447 | file_cmd_tail, |
| 5448 | 1, |
| 5449 | 1, |
| 5450 | |
| 5451 | }, |
| 5452 | { "split", |
| 5453 | "name", |
| 5454 | file_cmd_split, |
| 5455 | 1, |
| 5456 | 1, |
| 5457 | |
| 5458 | }, |
| 5459 | { "normalize", |
| 5460 | "name", |
| 5461 | file_cmd_normalize, |
| 5462 | 1, |
| 5463 | 1, |
| 5464 | |
| 5465 | }, |
| 5466 | { "join", |
| 5467 | "name ?name ...?", |
| 5468 | file_cmd_join, |
| 5469 | 1, |
| 5470 | -1, |
| 5471 | |
| 5472 | }, |
| 5473 | { "readable", |
| 5474 | "name", |
| 5475 | file_cmd_readable, |
| 5476 | 1, |
| 5477 | 1, |
| 5478 | |
| 5479 | }, |
| 5480 | { "writable", |
| 5481 | "name", |
| 5482 | file_cmd_writable, |
| 5483 | 1, |
| 5484 | 1, |
| 5485 | |
| 5486 | }, |
| 5487 | { "executable", |
| 5488 | "name", |
| 5489 | file_cmd_executable, |
| 5490 | 1, |
| 5491 | 1, |
| 5492 | |
| 5493 | }, |
| 5494 | { "exists", |
| 5495 | "name", |
| 5496 | file_cmd_exists, |
| 5497 | 1, |
| 5498 | 1, |
| 5499 | |
| 5500 | }, |
| 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", |
| 5536 | file_cmd_link, |
| 5537 | 2, |
| 5538 | 3, |
| 5539 | |
| 5540 | }, |
| 5541 | #endif |
| 5542 | #if defined(HAVE_READLINK) |
| 5543 | { "readlink", |
| 5544 | "name", |
| 5545 | file_cmd_readlink, |
| 5546 | 1, |
| 5547 | 1, |
| 5548 | |
| 5549 | }, |
| 5550 | #endif |
| 5551 | { "size", |
| 5552 | "name", |
| 5553 | file_cmd_size, |
| 5554 | 1, |
| 5555 | 1, |
| 5556 | |
| 5557 | }, |
| 5558 | { "stat", |
| 5559 | "name ?var?", |
| 5560 | file_cmd_stat, |
| 5561 | 1, |
| 5562 | 2, |
| 5563 | |
| 5564 | }, |
| 5565 | { "lstat", |
| 5566 | "name ?var?", |
| 5567 | file_cmd_lstat, |
| 5568 | 1, |
| 5569 | 2, |
| 5570 | |
| 5571 | }, |
| 5572 | { "type", |
| 5573 | "name", |
| 5574 | file_cmd_type, |
| 5575 | 1, |
| 5576 | 1, |
| 5577 | |
| 5578 | }, |
| 5579 | #ifdef HAVE_GETEUID |
| 5580 | { "owned", |
| 5581 | "name", |
| 5582 | file_cmd_owned, |
| 5583 | 1, |
| 5584 | 1, |
| 5585 | |
| 5586 | }, |
| 5587 | #endif |
| 5588 | { "isdirectory", |
| 5589 | "name", |
| 5590 | file_cmd_isdirectory, |
| 5591 | 1, |
| 5592 | 1, |
| 5593 | |
| 5594 | }, |
| 5595 | { "isfile", |
| 5596 | "name", |
| 5597 | file_cmd_isfile, |
| 5598 | 1, |
| 5599 | 1, |
| 5600 | |
| 5601 | }, |
| 5602 | { |
| 5603 | NULL |
| 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; |
| 5615 | } |
| 5616 | return JIM_OK; |
| 5617 | } |
| 5618 | |
| 5619 | static int Jim_PwdCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 5620 | { |
| 5621 | char *cwd = Jim_Alloc(MAXPATHLEN); |
| 5622 | |
| 5623 | if (getcwd(cwd, MAXPATHLEN) == NULL) { |
| 5624 | Jim_SetResultString(interp, "Failed to get pwd", -1); |
| 5625 | Jim_Free(cwd); |
| 5626 | return JIM_ERR; |
| 5627 | } |
| 5628 | JimFixPath(cwd); |
| 5629 | Jim_SetResultString(interp, cwd, -1); |
| 5630 | |
| 5631 | Jim_Free(cwd); |
| 5632 | return JIM_OK; |
| 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> |
| 5646 | |
| 5647 | |
| 5648 | #if (!(defined(HAVE_VFORK) || defined(HAVE_FORK)) || !defined(HAVE_WAITPID)) && !defined(__MINGW32__) |
| 5649 | static int Jim_ExecCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 5650 | { |
| 5651 | Jim_Obj *cmdlineObj = Jim_NewEmptyStringObj(interp); |
| 5652 | int i, j; |
| 5653 | int rc; |
| 5654 | |
| 5655 | |
| 5656 | for (i = 1; i < argc; i++) { |
| 5657 | int len; |
| 5658 | const char *arg = Jim_GetString(argv[i], &len); |
| 5659 | |
| 5660 | if (i > 1) { |
| 5661 | Jim_AppendString(interp, cmdlineObj, " ", 1); |
| 5662 | } |
| 5663 | if (strpbrk(arg, "\\\" ") == NULL) { |
| 5664 | |
| 5665 | Jim_AppendString(interp, cmdlineObj, arg, len); |
| 5666 | continue; |
| 5667 | } |
| 5668 | |
| 5669 | Jim_AppendString(interp, cmdlineObj, "\"", 1); |
| 5670 | for (j = 0; j < len; j++) { |
| 5671 | if (arg[j] == '\\' || arg[j] == '"') { |
| 5672 | Jim_AppendString(interp, cmdlineObj, "\\", 1); |
| 5673 | } |
| 5674 | Jim_AppendString(interp, cmdlineObj, &arg[j], 1); |
| 5675 | } |
| 5676 | Jim_AppendString(interp, cmdlineObj, "\"", 1); |
| 5677 | } |
| 5678 | rc = system(Jim_String(cmdlineObj)); |
| 5679 | |
| 5680 | Jim_FreeNewObj(interp, cmdlineObj); |
| 5681 | |
| 5682 | if (rc) { |
| 5683 | Jim_Obj *errorCode = Jim_NewListObj(interp, NULL, 0); |
| 5684 | Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, "CHILDSTATUS", -1)); |
| 5685 | Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, 0)); |
| 5686 | Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, rc)); |
| 5687 | Jim_SetGlobalVariableStr(interp, "errorCode", errorCode); |
| 5688 | return JIM_ERR; |
| 5689 | } |
| 5690 | |
| 5691 | return JIM_OK; |
| 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 | |
| 5703 | #include <errno.h> |
| 5704 | #include <signal.h> |
| 5705 | #include <sys/stat.h> |
| 5706 | |
| 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__) |
| 5719 | static phandle_t JimStartWinProcess(Jim_Interp *interp, char **argv, char **env, int inputId, int outputId, int errorId); |
| 5720 | #endif |
| 5721 | |
| 5722 | static void Jim_RemoveTrailingNewline(Jim_Obj *objPtr) |
| 5723 | { |
| 5724 | int len; |
| 5725 | const char *s = Jim_GetString(objPtr, &len); |
| 5726 | |
| 5727 | if (len > 0 && s[len - 1] == '\n') { |
| 5728 | objPtr->length--; |
| 5729 | objPtr->bytes[objPtr->length] = '\0'; |
| 5730 | } |
| 5731 | } |
| 5732 | |
| 5733 | static int JimAppendStreamToString(Jim_Interp *interp, int fd, Jim_Obj *strObj) |
| 5734 | { |
| 5735 | char buf[256]; |
| 5736 | int ret = 0; |
| 5737 | |
| 5738 | while (1) { |
| 5739 | int retval = read(fd, buf, sizeof(buf)); |
| 5740 | if (retval > 0) { |
| 5741 | ret = 1; |
| 5742 | Jim_AppendString(interp, strObj, buf, retval); |
| 5743 | } |
| 5744 | if (retval <= 0) { |
| 5745 | break; |
| 5746 | } |
| 5747 | } |
| 5748 | close(fd); |
| 5749 | return ret; |
| 5750 | } |
| 5751 | |
| 5752 | static char **JimBuildEnv(Jim_Interp *interp) |
| 5753 | { |
| 5754 | int i; |
| 5755 | int size; |
| 5756 | int num; |
| 5757 | int n; |
| 5758 | char **envptr; |
| 5759 | char *envdata; |
| 5760 | |
| 5761 | Jim_Obj *objPtr = Jim_GetGlobalVariableStr(interp, "env", JIM_NONE); |
| 5762 | |
| 5763 | if (!objPtr) { |
| 5764 | return JimOriginalEnviron(); |
| 5765 | } |
| 5766 | |
| 5767 | |
| 5768 | |
| 5769 | num = Jim_ListLength(interp, objPtr); |
| 5770 | if (num % 2) { |
| 5771 | |
| 5772 | num--; |
| 5773 | } |
| 5774 | size = Jim_Length(objPtr) + 2; |
| 5775 | |
| 5776 | envptr = Jim_Alloc(sizeof(*envptr) * (num / 2 + 1) + size); |
| 5777 | envdata = (char *)&envptr[num / 2 + 1]; |
| 5778 | |
| 5779 | n = 0; |
| 5780 | for (i = 0; i < num; i += 2) { |
| 5781 | const char *s1, *s2; |
| 5782 | Jim_Obj *elemObj; |
| 5783 | |
| 5784 | Jim_ListIndex(interp, objPtr, i, &elemObj, JIM_NONE); |
| 5785 | s1 = Jim_String(elemObj); |
| 5786 | Jim_ListIndex(interp, objPtr, i + 1, &elemObj, JIM_NONE); |
| 5787 | s2 = Jim_String(elemObj); |
| 5788 | |
| 5789 | envptr[n] = envdata; |
| 5790 | envdata += sprintf(envdata, "%s=%s", s1, s2); |
| 5791 | envdata++; |
| 5792 | n++; |
| 5793 | } |
| 5794 | envptr[n] = NULL; |
| 5795 | *envdata = 0; |
| 5796 | |
| 5797 | return envptr; |
| 5798 | } |
| 5799 | |
| 5800 | static void JimFreeEnv(char **env, char **original_environ) |
| 5801 | { |
| 5802 | if (env != original_environ) { |
| 5803 | Jim_Free(env); |
| 5804 | } |
| 5805 | } |
| 5806 | |
| 5807 | static Jim_Obj *JimMakeErrorCode(Jim_Interp *interp, long pid, int waitStatus, Jim_Obj *errStrObj) |
| 5808 | { |
| 5809 | Jim_Obj *errorCode = Jim_NewListObj(interp, NULL, 0); |
| 5810 | |
| 5811 | if (pid <= 0) { |
| 5812 | Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, "NONE", -1)); |
| 5813 | Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, pid)); |
| 5814 | Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, -1)); |
| 5815 | } |
| 5816 | else if (WIFEXITED(waitStatus)) { |
| 5817 | Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, "CHILDSTATUS", -1)); |
| 5818 | Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, pid)); |
| 5819 | Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, WEXITSTATUS(waitStatus))); |
| 5820 | } |
| 5821 | else { |
| 5822 | const char *type; |
| 5823 | const char *action; |
| 5824 | const char *signame; |
| 5825 | |
| 5826 | if (WIFSIGNALED(waitStatus)) { |
| 5827 | type = "CHILDKILLED"; |
| 5828 | action = "killed"; |
| 5829 | signame = Jim_SignalId(WTERMSIG(waitStatus)); |
| 5830 | } |
| 5831 | else { |
| 5832 | type = "CHILDSUSP"; |
| 5833 | action = "suspended"; |
| 5834 | signame = "none"; |
| 5835 | } |
| 5836 | |
| 5837 | Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, type, -1)); |
| 5838 | |
| 5839 | if (errStrObj) { |
| 5840 | Jim_AppendStrings(interp, errStrObj, "child ", action, " by signal ", Jim_SignalId(WTERMSIG(waitStatus)), "\n", NULL); |
| 5841 | } |
| 5842 | |
| 5843 | Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, pid)); |
| 5844 | Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, signame, -1)); |
| 5845 | } |
| 5846 | return errorCode; |
| 5847 | } |
| 5848 | |
| 5849 | static int JimCheckWaitStatus(Jim_Interp *interp, long pid, int waitStatus, Jim_Obj *errStrObj) |
| 5850 | { |
| 5851 | if (WIFEXITED(waitStatus) && WEXITSTATUS(waitStatus) == 0) { |
| 5852 | return JIM_OK; |
| 5853 | } |
| 5854 | Jim_SetGlobalVariableStr(interp, "errorCode", JimMakeErrorCode(interp, pid, waitStatus, errStrObj)); |
| 5855 | |
| 5856 | return JIM_ERR; |
| 5857 | } |
| 5858 | |
| 5859 | |
| 5860 | struct WaitInfo |
| 5861 | { |
| 5862 | phandle_t phandle; |
| 5863 | int status; |
| 5864 | int flags; |
| 5865 | }; |
| 5866 | |
| 5867 | |
| 5868 | struct WaitInfoTable { |
| 5869 | struct WaitInfo *info; |
| 5870 | int size; |
| 5871 | int used; |
| 5872 | int refcount; |
| 5873 | }; |
| 5874 | |
| 5875 | |
| 5876 | #define WI_DETACHED 2 |
| 5877 | |
| 5878 | #define WAIT_TABLE_GROW_BY 4 |
| 5879 | |
| 5880 | static void JimFreeWaitInfoTable(struct Jim_Interp *interp, void *privData) |
| 5881 | { |
| 5882 | struct WaitInfoTable *table = privData; |
| 5883 | |
| 5884 | if (--table->refcount == 0) { |
| 5885 | Jim_Free(table->info); |
| 5886 | Jim_Free(table); |
| 5887 | } |
| 5888 | } |
| 5889 | |
| 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) |
| 5901 | { |
| 5902 | int i; |
| 5903 | |
| 5904 | |
| 5905 | for (i = 0; i < table->used; i++) { |
| 5906 | if (phandle == table->info[i].phandle) { |
| 5907 | if (i != table->used - 1) { |
| 5908 | table->info[i] = table->info[table->used - 1]; |
| 5909 | } |
| 5910 | table->used--; |
| 5911 | return 0; |
| 5912 | } |
| 5913 | } |
| 5914 | return -1; |
| 5915 | } |
| 5916 | |
| 5917 | static int Jim_ExecCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 5918 | { |
| 5919 | int outputId; |
| 5920 | int errorId; |
| 5921 | phandle_t *pidPtr; |
| 5922 | int numPids, result; |
| 5923 | int child_siginfo = 1; |
| 5924 | Jim_Obj *childErrObj; |
| 5925 | Jim_Obj *errStrObj; |
| 5926 | struct WaitInfoTable *table = Jim_CmdPrivData(interp); |
| 5927 | |
| 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); |
| 5939 | for (i = 0; i < numPids; i++) { |
| 5940 | Jim_ListAppendElement(interp, listObj, Jim_NewIntObj(interp, JimProcessPid(pidPtr[i]))); |
| 5941 | } |
| 5942 | Jim_SetResult(interp, listObj); |
| 5943 | JimDetachPids(table, numPids, pidPtr); |
| 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 | |
| 5955 | result = JIM_OK; |
| 5956 | |
| 5957 | errStrObj = Jim_NewStringObj(interp, "", 0); |
| 5958 | |
| 5959 | |
| 5960 | if (outputId != -1) { |
| 5961 | if (JimAppendStreamToString(interp, outputId, errStrObj) < 0) { |
| 5962 | result = JIM_ERR; |
| 5963 | Jim_SetResultErrno(interp, "error reading from output pipe"); |
| 5964 | } |
| 5965 | } |
| 5966 | |
| 5967 | |
| 5968 | childErrObj = Jim_NewStringObj(interp, "", 0); |
| 5969 | Jim_IncrRefCount(childErrObj); |
| 5970 | |
| 5971 | if (JimCleanupChildren(interp, numPids, pidPtr, childErrObj) != JIM_OK) { |
| 5972 | result = JIM_ERR; |
| 5973 | } |
| 5974 | |
| 5975 | if (errorId != -1) { |
| 5976 | int ret; |
| 5977 | Jim_Lseek(errorId, 0, SEEK_SET); |
| 5978 | ret = JimAppendStreamToString(interp, errorId, errStrObj); |
| 5979 | if (ret < 0) { |
| 5980 | Jim_SetResultErrno(interp, "error reading from error pipe"); |
| 5981 | result = JIM_ERR; |
| 5982 | } |
| 5983 | else if (ret > 0) { |
| 5984 | |
| 5985 | child_siginfo = 0; |
| 5986 | } |
| 5987 | } |
| 5988 | |
| 5989 | if (child_siginfo) { |
| 5990 | |
| 5991 | Jim_AppendObj(interp, errStrObj, childErrObj); |
| 5992 | } |
| 5993 | Jim_DecrRefCount(interp, childErrObj); |
| 5994 | |
| 5995 | |
| 5996 | Jim_RemoveTrailingNewline(errStrObj); |
| 5997 | |
| 5998 | |
| 5999 | Jim_SetResult(interp, errStrObj); |
| 6000 | |
| 6001 | return result; |
| 6002 | } |
| 6003 | |
| 6004 | static long JimWaitForProcess(struct WaitInfoTable *table, phandle_t phandle, int *statusPtr) |
| 6005 | { |
| 6006 | if (JimWaitRemove(table, phandle) == 0) { |
| 6007 | |
| 6008 | return waitpid(phandle, statusPtr, 0); |
| 6009 | } |
| 6010 | |
| 6011 | |
| 6012 | return -1; |
| 6013 | } |
| 6014 | |
| 6015 | static void JimDetachPids(struct WaitInfoTable *table, int numPids, const phandle_t *pidPtr) |
| 6016 | { |
| 6017 | int j; |
| 6018 | |
| 6019 | for (j = 0; j < numPids; j++) { |
| 6020 | |
| 6021 | int i; |
| 6022 | for (i = 0; i < table->used; i++) { |
| 6023 | if (pidPtr[j] == table->info[i].phandle) { |
| 6024 | table->info[i].flags |= WI_DETACHED; |
| 6025 | break; |
| 6026 | } |
| 6027 | } |
| 6028 | } |
| 6029 | } |
| 6030 | |
| 6031 | static int JimGetChannelFd(Jim_Interp *interp, const char *name) |
| 6032 | { |
| 6033 | Jim_Obj *objv[2]; |
| 6034 | |
| 6035 | objv[0] = Jim_NewStringObj(interp, name, -1); |
| 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 | |
| 6047 | static void JimReapDetachedPids(struct WaitInfoTable *table) |
| 6048 | { |
| 6049 | struct WaitInfo *waitPtr; |
| 6050 | int count; |
| 6051 | int dest; |
| 6052 | |
| 6053 | if (!table) { |
| 6054 | return; |
| 6055 | } |
| 6056 | |
| 6057 | waitPtr = table->info; |
| 6058 | dest = 0; |
| 6059 | for (count = table->used; count > 0; waitPtr++, count--) { |
| 6060 | if (waitPtr->flags & WI_DETACHED) { |
| 6061 | int status; |
| 6062 | long pid = waitpid(waitPtr->phandle, &status, WNOHANG); |
| 6063 | if (pid > 0) { |
| 6064 | |
| 6065 | table->used--; |
| 6066 | continue; |
| 6067 | } |
| 6068 | } |
| 6069 | if (waitPtr != &table->info[dest]) { |
| 6070 | table->info[dest] = *waitPtr; |
| 6071 | } |
| 6072 | dest++; |
| 6073 | } |
| 6074 | } |
| 6075 | |
| 6076 | static int Jim_WaitCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 6077 | { |
| 6078 | struct WaitInfoTable *table = Jim_CmdPrivData(interp); |
| 6079 | int nohang = 0; |
| 6080 | long pid; |
| 6081 | phandle_t phandle; |
| 6082 | int status; |
| 6083 | Jim_Obj *errCodeObj; |
| 6084 | |
| 6085 | |
| 6086 | if (argc == 1) { |
| 6087 | JimReapDetachedPids(table); |
| 6088 | return JIM_OK; |
| 6089 | } |
| 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 | |
| 6101 | |
| 6102 | phandle = JimWaitPid(pid, &status, nohang ? WNOHANG : 0); |
| 6103 | if (phandle == JIM_BAD_PHANDLE) { |
| 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 | |
| 6118 | if (phandle != JIM_BAD_PHANDLE && (WIFEXITED(status) || WIFSIGNALED(status))) { |
| 6119 | |
| 6120 | JimWaitRemove(table, phandle); |
| 6121 | } |
| 6122 | Jim_SetResult(interp, errCodeObj); |
| 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. */ |
| 6315 | int cmdCount; /* Count of number of distinct commands |
| 6316 | * found in argc/argv. */ |
| 6317 | const char *input = NULL; /* Describes input for pipeline, depending |
| 6318 | * on "inputFile". NULL means take input |
| 6319 | * from stdin/pipe. */ |
| 6320 | int input_len = 0; |
| 6321 | |
| 6322 | #define FILE_NAME 0 |
| 6323 | #define FILE_APPEND 1 |
| 6324 | #define FILE_HANDLE 2 |
| 6325 | #define FILE_TEXT 3 |
| 6326 | |
| 6327 | int inputFile = FILE_NAME; /* 1 means input is name of input file. |
| 6328 | * 2 means input is filehandle name. |
| 6329 | * 0 means input holds actual |
| 6330 | * text to be input to command. */ |
| 6331 | |
| 6332 | int outputFile = FILE_NAME; /* 0 means output is the name of output file. |
| 6333 | * 1 means output is the name of output file, and append. |
| 6334 | * 2 means output is filehandle name. |
| 6335 | * All this is ignored if output is NULL |
| 6336 | */ |
| 6337 | int errorFile = FILE_NAME; /* 0 means error is the name of error file. |
| 6338 | * 1 means error is the name of error file, and append. |
| 6339 | * 2 means error is filehandle name. |
| 6340 | * All this is ignored if error is NULL |
| 6341 | */ |
| 6342 | const char *output = NULL; /* Holds name of output file to pipe to, |
| 6343 | * or NULL if output goes to stdout/pipe. */ |
| 6344 | const char *error = NULL; /* Holds name of stderr file to pipe to, |
| 6345 | * or NULL if stderr goes to stderr/pipe. */ |
| 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 | |
| 6434 | if (input != NULL) { |
| 6435 | if (inputFile == FILE_TEXT) { |
| 6436 | inputId = Jim_MakeTempFile(interp, NULL, 1); |
| 6437 | if (inputId == -1) { |
| 6438 | goto error; |
| 6439 | } |
| 6440 | if (write(inputId, input, input_len) != input_len) { |
| 6441 | Jim_SetResultErrno(interp, "couldn't write temp file"); |
| 6442 | close(inputId); |
| 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())); |
| 6473 | goto error; |
| 6474 | } |
| 6475 | } |
| 6476 | } |
| 6477 | else if (outPipePtr != NULL) { |
| 6478 | if (pipe(pipeIds) != 0) { |
| 6479 | Jim_SetResultErrno(interp, "couldn't create output pipe"); |
| 6480 | goto error; |
| 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 | |
| 6492 | if (lastOutputId != -1) { |
| 6493 | errorId = dup(lastOutputId); |
| 6494 | } |
| 6495 | else { |
| 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) { |
| 6510 | Jim_SetResultFormatted(interp, "couldn't write file \"%s\": %s", error, strerror(Jim_Errno())); |
| 6511 | goto error; |
| 6512 | } |
| 6513 | } |
| 6514 | } |
| 6515 | else if (errFilePtr != NULL) { |
| 6516 | errorId = Jim_MakeTempFile(interp, NULL, 1); |
| 6517 | if (errorId == -1) { |
| 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 |
| 6578 | phandle = vfork(); |
| 6579 | #else |
| 6580 | phandle = fork(); |
| 6581 | #endif |
| 6582 | if (phandle < 0) { |
| 6583 | Jim_SetResultErrno(interp, "couldn't fork child process"); |
| 6584 | goto error; |
| 6585 | } |
| 6586 | if (phandle == 0) { |
| 6587 | |
| 6588 | |
| 6589 | if (inputId != -1 && inputId != fileno(stdin)) { |
| 6590 | dup2(inputId, fileno(stdin)); |
| 6591 | close(inputId); |
| 6592 | } |
| 6593 | if (outputId != -1 && outputId != fileno(stdout)) { |
| 6594 | dup2(outputId, fileno(stdout)); |
| 6595 | if (outputId != errorId) { |
| 6596 | close(outputId); |
| 6597 | } |
| 6598 | } |
| 6599 | if (errorId != -1 && errorId != fileno(stderr)) { |
| 6600 | dup2(errorId, fileno(stderr)); |
| 6601 | close(errorId); |
| 6602 | } |
| 6603 | |
| 6604 | if (outPipePtr && *outPipePtr != -1) { |
| 6605 | close(*outPipePtr); |
| 6606 | } |
| 6607 | if (errFilePtr && *errFilePtr != -1) { |
| 6608 | close(*errFilePtr); |
| 6609 | } |
| 6610 | if (pipeIds[0] != -1) { |
| 6611 | close(pipeIds[0]); |
| 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 | { |
| 6626 | |
| 6627 | static char *const false_argv[2] = {"false", NULL}; |
| 6628 | execvp(false_argv[0],false_argv); |
| 6629 | } |
| 6630 | #endif |
| 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 | } |
| 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 | |
| 6653 | if (inputId != -1) { |
| 6654 | close(inputId); |
| 6655 | } |
| 6656 | if (outputId != -1) { |
| 6657 | close(outputId); |
| 6658 | } |
| 6659 | inputId = pipeIds[0]; |
| 6660 | pipeIds[0] = pipeIds[1] = -1; |
| 6661 | } |
| 6662 | *pidArrayPtr = pidPtr; |
| 6663 | |
| 6664 | |
| 6665 | cleanup: |
| 6666 | if (inputId != -1) { |
| 6667 | close(inputId); |
| 6668 | } |
| 6669 | if (lastOutputId != -1) { |
| 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)) { |
| 6687 | close(*errFilePtr); |
| 6688 | *errFilePtr = -1; |
| 6689 | } |
| 6690 | if (pipeIds[0] != -1) { |
| 6691 | close(pipeIds[0]); |
| 6692 | } |
| 6693 | if (pipeIds[1] != -1) { |
| 6694 | close(pipeIds[1]); |
| 6695 | } |
| 6696 | if (pidPtr != NULL) { |
| 6697 | for (i = 0; i < numPids; i++) { |
| 6698 | if (pidPtr[i] != JIM_BAD_PHANDLE) { |
| 6699 | JimDetachPids(table, 1, &pidPtr[i]); |
| 6700 | } |
| 6701 | } |
| 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); |
| 6741 | int result = JIM_OK; |
| 6742 | int i; |
| 6743 | |
| 6744 | |
| 6745 | for (i = 0; i < numPids; i++) { |
| 6746 | int waitStatus = 0; |
| 6747 | long pid = JimWaitForProcess(table, pidPtr[i], &waitStatus); |
| 6748 | if (pid > 0) { |
| 6749 | if (JimCheckWaitStatus(interp, pid, waitStatus, errStrObj) != JIM_OK) { |
| 6750 | result = JIM_ERR; |
| 6751 | } |
| 6752 | } |
| 6753 | } |
| 6754 | Jim_Free(pidPtr); |
| 6755 | |
| 6756 | return result; |
| 6757 | } |
| 6758 | |
| 6759 | int Jim_execInit(Jim_Interp *interp) |
| 6760 | { |
| 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__) |
| 6776 | |
| 6777 | |
| 6778 | static int |
| 6779 | JimWinFindExecutable(const char *originalName, char fullPath[MAX_PATH]) |
| 6780 | { |
| 6781 | int i; |
| 6782 | static char extensions[][5] = {".exe", "", ".bat"}; |
| 6783 | |
| 6784 | for (i = 0; i < (int) (sizeof(extensions) / sizeof(extensions[0])); i++) { |
| 6785 | snprintf(fullPath, MAX_PATH, "%s%s", originalName, extensions[i]); |
| 6786 | |
| 6787 | if (SearchPath(NULL, fullPath, NULL, MAX_PATH, fullPath, NULL) == 0) { |
| 6788 | continue; |
| 6789 | } |
| 6790 | if (GetFileAttributes(fullPath) & FILE_ATTRIBUTE_DIRECTORY) { |
| 6791 | continue; |
| 6792 | } |
| 6793 | return 0; |
| 6794 | } |
| 6795 | |
| 6796 | return -1; |
| 6797 | } |
| 6798 | |
| 6799 | static char **JimSaveEnv(char **env) |
| 6800 | { |
| 6801 | return env; |
| 6802 | } |
| 6803 | |
| 6804 | static void JimRestoreEnv(char **env) |
| 6805 | { |
| 6806 | JimFreeEnv(env, Jim_GetEnviron()); |
| 6807 | } |
| 6808 | |
| 6809 | static char **JimOriginalEnviron(void) |
| 6810 | { |
| 6811 | return NULL; |
| 6812 | } |
| 6813 | |
| 6814 | static Jim_Obj * |
| 6815 | JimWinBuildCommandLine(Jim_Interp *interp, char **argv) |
| 6816 | { |
| 6817 | char *start, *special; |
| 6818 | int quote, i; |
| 6819 | |
| 6820 | Jim_Obj *strObj = Jim_NewStringObj(interp, "", 0); |
| 6821 | |
| 6822 | for (i = 0; argv[i]; i++) { |
| 6823 | if (i > 0) { |
| 6824 | Jim_AppendString(interp, strObj, " ", 1); |
| 6825 | } |
| 6826 | |
| 6827 | if (argv[i][0] == '\0') { |
| 6828 | quote = 1; |
| 6829 | } |
| 6830 | else { |
| 6831 | quote = 0; |
| 6832 | for (start = argv[i]; *start != '\0'; start++) { |
| 6833 | if (isspace(UCHAR(*start))) { |
| 6834 | quote = 1; |
| 6835 | break; |
| 6836 | } |
| 6837 | } |
| 6838 | } |
| 6839 | if (quote) { |
| 6840 | Jim_AppendString(interp, strObj, "\"" , 1); |
| 6841 | } |
| 6842 | |
| 6843 | start = argv[i]; |
| 6844 | for (special = argv[i]; ; ) { |
| 6845 | if ((*special == '\\') && (special[1] == '\\' || |
| 6846 | special[1] == '"' || (quote && special[1] == '\0'))) { |
| 6847 | Jim_AppendString(interp, strObj, start, special - start); |
| 6848 | start = special; |
| 6849 | while (1) { |
| 6850 | special++; |
| 6851 | if (*special == '"' || (quote && *special == '\0')) { |
| 6852 | |
| 6853 | Jim_AppendString(interp, strObj, start, special - start); |
| 6854 | break; |
| 6855 | } |
| 6856 | if (*special != '\\') { |
| 6857 | break; |
| 6858 | } |
| 6859 | } |
| 6860 | Jim_AppendString(interp, strObj, start, special - start); |
| 6861 | start = special; |
| 6862 | } |
| 6863 | if (*special == '"') { |
| 6864 | if (special == start) { |
| 6865 | Jim_AppendString(interp, strObj, "\"", 1); |
| 6866 | } |
| 6867 | else { |
| 6868 | Jim_AppendString(interp, strObj, start, special - start); |
| 6869 | } |
| 6870 | Jim_AppendString(interp, strObj, "\\\"", 2); |
| 6871 | start = special + 1; |
| 6872 | } |
| 6873 | if (*special == '\0') { |
| 6874 | break; |
| 6875 | } |
| 6876 | special++; |
| 6877 | } |
| 6878 | Jim_AppendString(interp, strObj, start, special - start); |
| 6879 | if (quote) { |
| 6880 | Jim_AppendString(interp, strObj, "\"", 1); |
| 6881 | } |
| 6882 | } |
| 6883 | return strObj; |
| 6884 | } |
| 6885 | |
| 6886 | static phandle_t |
| 6887 | JimStartWinProcess(Jim_Interp *interp, char **argv, char **env, int inputId, int outputId, int errorId) |
| 6888 | { |
| 6889 | STARTUPINFO startInfo; |
| 6890 | PROCESS_INFORMATION procInfo; |
| 6891 | HANDLE hProcess; |
| 6892 | char execPath[MAX_PATH]; |
| 6893 | phandle_t phandle = INVALID_HANDLE_VALUE; |
| 6894 | Jim_Obj *cmdLineObj; |
| 6895 | char *winenv; |
| 6896 | |
| 6897 | if (JimWinFindExecutable(argv[0], execPath) < 0) { |
| 6898 | return phandle; |
| 6899 | } |
| 6900 | argv[0] = execPath; |
| 6901 | |
| 6902 | hProcess = GetCurrentProcess(); |
| 6903 | cmdLineObj = JimWinBuildCommandLine(interp, argv); |
| 6904 | |
| 6905 | |
| 6906 | ZeroMemory(&startInfo, sizeof(startInfo)); |
| 6907 | startInfo.cb = sizeof(startInfo); |
| 6908 | startInfo.dwFlags = STARTF_USESTDHANDLES; |
| 6909 | startInfo.hStdInput = INVALID_HANDLE_VALUE; |
| 6910 | startInfo.hStdOutput= INVALID_HANDLE_VALUE; |
| 6911 | startInfo.hStdError = INVALID_HANDLE_VALUE; |
| 6912 | |
| 6913 | if (inputId == -1) { |
| 6914 | inputId = _fileno(stdin); |
| 6915 | } |
| 6916 | DuplicateHandle(hProcess, (HANDLE)_get_osfhandle(inputId), hProcess, &startInfo.hStdInput, |
| 6917 | 0, TRUE, DUPLICATE_SAME_ACCESS); |
| 6918 | if (startInfo.hStdInput == INVALID_HANDLE_VALUE) { |
| 6919 | goto end; |
| 6920 | } |
| 6921 | |
| 6922 | if (outputId == -1) { |
| 6923 | outputId = _fileno(stdout); |
| 6924 | } |
| 6925 | DuplicateHandle(hProcess, (HANDLE)_get_osfhandle(outputId), hProcess, &startInfo.hStdOutput, |
| 6926 | 0, TRUE, DUPLICATE_SAME_ACCESS); |
| 6927 | if (startInfo.hStdOutput == INVALID_HANDLE_VALUE) { |
| 6928 | goto end; |
| 6929 | } |
| 6930 | |
| 6931 | |
| 6932 | if (errorId == -1) { |
| 6933 | errorId = _fileno(stderr); |
| 6934 | } |
| 6935 | DuplicateHandle(hProcess, (HANDLE)_get_osfhandle(errorId), hProcess, &startInfo.hStdError, |
| 6936 | 0, TRUE, DUPLICATE_SAME_ACCESS); |
| 6937 | if (startInfo.hStdError == INVALID_HANDLE_VALUE) { |
| 6938 | goto end; |
| 6939 | } |
| 6940 | |
| 6941 | if (env == NULL) { |
| 6942 | |
| 6943 | winenv = NULL; |
| 6944 | } |
| 6945 | else if (env[0] == NULL) { |
| 6946 | winenv = (char *)"\0"; |
| 6947 | } |
| 6948 | else { |
| 6949 | winenv = env[0]; |
| 6950 | } |
| 6951 | |
| 6952 | if (!CreateProcess(NULL, (char *)Jim_String(cmdLineObj), NULL, NULL, TRUE, |
| 6953 | 0, winenv, NULL, &startInfo, &procInfo)) { |
| 6954 | goto end; |
| 6955 | } |
| 6956 | |
| 6957 | |
| 6958 | WaitForInputIdle(procInfo.hProcess, 5000); |
| 6959 | CloseHandle(procInfo.hThread); |
| 6960 | |
| 6961 | phandle = procInfo.hProcess; |
| 6962 | |
| 6963 | end: |
| 6964 | Jim_FreeNewObj(interp, cmdLineObj); |
| 6965 | if (startInfo.hStdInput != INVALID_HANDLE_VALUE) { |
| 6966 | CloseHandle(startInfo.hStdInput); |
| 6967 | } |
| 6968 | if (startInfo.hStdOutput != INVALID_HANDLE_VALUE) { |
| 6969 | CloseHandle(startInfo.hStdOutput); |
| 6970 | } |
| 6971 | if (startInfo.hStdError != INVALID_HANDLE_VALUE) { |
| 6972 | CloseHandle(startInfo.hStdError); |
| 6973 | } |
| 6974 | return phandle; |
| 6975 | } |
| 6976 | |
| 6977 | #else |
| 6978 | |
| 6979 | static char **JimOriginalEnviron(void) |
| 6980 | { |
| 6981 | return Jim_GetEnviron(); |
| 6982 | } |
| 6983 | |
| 6984 | static char **JimSaveEnv(char **env) |
| 6985 | { |
| 6986 | char **saveenv = Jim_GetEnviron(); |
| 6987 | Jim_SetEnviron(env); |
| 6988 | return saveenv; |
| 6989 | } |
| 6990 | |
| 6991 | static void JimRestoreEnv(char **env) |
| 6992 | { |
| 6993 | JimFreeEnv(Jim_GetEnviron(), env); |
| 6994 | Jim_SetEnviron(env); |
| 6995 | } |
| 6996 | #endif |
| 6997 | #endif |
| 6998 | |
| 6999 | |
| 7000 | #include <stdlib.h> |
| 7001 | #include <string.h> |
| 7002 | #include <stdio.h> |
| 7003 | #include <time.h> |
| 7004 | |
| 7005 | |
| 7006 | #ifdef HAVE_SYS_TIME_H |
| 7007 | #include <sys/time.h> |
| 7008 | #endif |
| 7009 | |
| 7010 | struct clock_options { |
| 7011 | int gmt; |
| 7012 | const char *format; |
| 7013 | }; |
| 7014 | |
| 7015 | static int parse_clock_options(Jim_Interp *interp, int argc, Jim_Obj *const *argv, struct clock_options *opts) |
| 7016 | { |
| 7017 | static const char * const options[] = { "-gmt", "-format", NULL }; |
| 7018 | enum { OPT_GMT, OPT_FORMAT, }; |
| 7019 | int i; |
| 7020 | |
| 7021 | for (i = 0; i < argc; i += 2) { |
| 7022 | int option; |
| 7023 | if (Jim_GetEnum(interp, argv[i], options, &option, NULL, JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) { |
| 7024 | return JIM_ERR; |
| 7025 | } |
| 7026 | switch (option) { |
| 7027 | case OPT_GMT: |
| 7028 | if (Jim_GetBoolean(interp, argv[i + 1], &opts->gmt) != JIM_OK) { |
| 7029 | return JIM_ERR; |
| 7030 | } |
| 7031 | break; |
| 7032 | case OPT_FORMAT: |
| 7033 | opts->format = Jim_String(argv[i + 1]); |
| 7034 | break; |
| 7035 | } |
| 7036 | } |
| 7037 | return JIM_OK; |
| 7038 | } |
| 7039 | |
| 7040 | static int clock_cmd_format(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 7041 | { |
| 7042 | |
| 7043 | char buf[100]; |
| 7044 | time_t t; |
| 7045 | jim_wide seconds; |
| 7046 | struct clock_options options = { 0, "%a %b %d %H:%M:%S %Z %Y" }; |
| 7047 | struct tm *tm; |
| 7048 | |
| 7049 | if (Jim_GetWide(interp, argv[0], &seconds) != JIM_OK) { |
| 7050 | return JIM_ERR; |
| 7051 | } |
| 7052 | if (argc % 2 == 0) { |
| 7053 | return -1; |
| 7054 | } |
| 7055 | if (parse_clock_options(interp, argc - 1, argv + 1, &options) == JIM_ERR) { |
| 7056 | return JIM_ERR; |
| 7057 | } |
| 7058 | |
| 7059 | t = seconds; |
| 7060 | tm = options.gmt ? gmtime(&t) : localtime(&t); |
| 7061 | |
| 7062 | if (tm == NULL || strftime(buf, sizeof(buf), options.format, tm) == 0) { |
| 7063 | Jim_SetResultString(interp, "format string too long or invalid time", -1); |
| 7064 | return JIM_ERR; |
| 7065 | } |
| 7066 | |
| 7067 | Jim_SetResultString(interp, buf, -1); |
| 7068 | |
| 7069 | return JIM_OK; |
| 7070 | } |
| 7071 | |
| 7072 | #ifdef HAVE_STRPTIME |
| 7073 | static time_t jim_timegm(const struct tm *tm) |
| 7074 | { |
| 7075 | int m = tm->tm_mon + 1; |
| 7076 | int y = 1900 + tm->tm_year - (m <= 2); |
| 7077 | int era = (y >= 0 ? y : y - 399) / 400; |
| 7078 | unsigned yoe = (unsigned)(y - era * 400); |
| 7079 | unsigned doy = (153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + tm->tm_mday - 1; |
| 7080 | unsigned doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; |
| 7081 | long days = (era * 146097 + (int)doe - 719468); |
| 7082 | int secs = tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec; |
| 7083 | |
| 7084 | return days * 24 * 60 * 60 + secs; |
| 7085 | } |
| 7086 | |
| 7087 | static int clock_cmd_scan(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 7088 | { |
| 7089 | char *pt; |
| 7090 | struct tm tm; |
| 7091 | time_t now = time(NULL); |
| 7092 | |
| 7093 | struct clock_options options = { 0, NULL }; |
| 7094 | |
| 7095 | if (argc % 2 == 0) { |
| 7096 | return -1; |
| 7097 | } |
| 7098 | |
| 7099 | if (parse_clock_options(interp, argc - 1, argv + 1, &options) == JIM_ERR) { |
| 7100 | return JIM_ERR; |
| 7101 | } |
| 7102 | if (options.format == NULL) { |
| 7103 | return -1; |
| 7104 | } |
| 7105 | |
| 7106 | localtime_r(&now, &tm); |
| 7107 | |
| 7108 | pt = strptime(Jim_String(argv[0]), options.format, &tm); |
| 7109 | if (pt == 0 || *pt != 0) { |
| 7110 | Jim_SetResultString(interp, "Failed to parse time according to format", -1); |
| 7111 | return JIM_ERR; |
| 7112 | } |
| 7113 | |
| 7114 | |
| 7115 | tm.tm_isdst = options.gmt ? 0 : -1; |
| 7116 | Jim_SetResultInt(interp, options.gmt ? jim_timegm(&tm) : mktime(&tm)); |
| 7117 | |
| 7118 | return JIM_OK; |
| 7119 | } |
| 7120 | #endif |
| 7121 | |
| 7122 | static int clock_cmd_seconds(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 7123 | { |
| 7124 | Jim_SetResultInt(interp, Jim_GetTimeUsec(CLOCK_REALTIME) / 1000000); |
| 7125 | return JIM_OK; |
| 7126 | } |
| 7127 | |
| 7128 | static int clock_cmd_clicks(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 7129 | { |
| 7130 | Jim_SetResultInt(interp, Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW)); |
| 7131 | return JIM_OK; |
| 7132 | } |
| 7133 | |
| 7134 | static int clock_cmd_micros(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 7135 | { |
| 7136 | Jim_SetResultInt(interp, Jim_GetTimeUsec(CLOCK_REALTIME)); |
| 7137 | return JIM_OK; |
| 7138 | } |
| 7139 | |
| 7140 | static int clock_cmd_millis(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 7141 | { |
| 7142 | Jim_SetResultInt(interp, Jim_GetTimeUsec(CLOCK_REALTIME) / 1000); |
| 7143 | return JIM_OK; |
| 7144 | } |
| 7145 | |
| 7146 | static const jim_subcmd_type clock_command_table[] = { |
| 7147 | { "clicks", |
| 7148 | NULL, |
| 7149 | clock_cmd_clicks, |
| 7150 | 0, |
| 7151 | 0, |
| 7152 | |
| 7153 | }, |
| 7154 | { "format", |
| 7155 | "seconds ?-format string? ?-gmt boolean?", |
| 7156 | clock_cmd_format, |
| 7157 | 1, |
| 7158 | 5, |
| 7159 | |
| 7160 | }, |
| 7161 | { "microseconds", |
| 7162 | NULL, |
| 7163 | clock_cmd_micros, |
| 7164 | 0, |
| 7165 | 0, |
| 7166 | |
| 7167 | }, |
| 7168 | { "milliseconds", |
| 7169 | NULL, |
| 7170 | clock_cmd_millis, |
| 7171 | 0, |
| 7172 | 0, |
| 7173 | |
| 7174 | }, |
| 7175 | #ifdef HAVE_STRPTIME |
| 7176 | { "scan", |
| 7177 | "str -format format ?-gmt boolean?", |
| 7178 | clock_cmd_scan, |
| 7179 | 3, |
| 7180 | 5, |
| 7181 | |
| 7182 | }, |
| 7183 | #endif |
| 7184 | { "seconds", |
| 7185 | NULL, |
| 7186 | clock_cmd_seconds, |
| 7187 | 0, |
| 7188 | 0, |
| 7189 | |
| 7190 | }, |
| 7191 | { NULL } |
| 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> |
| 7203 | #include <string.h> |
| 7204 | #include <stdio.h> |
| 7205 | #include <errno.h> |
| 7206 | |
| 7207 | |
| 7208 | static int array_cmd_exists(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 7209 | { |
| 7210 | |
| 7211 | Jim_Obj *dictObj = Jim_GetVariable(interp, argv[0], JIM_UNSHARED); |
| 7212 | Jim_SetResultInt(interp, dictObj && Jim_DictSize(interp, dictObj) != -1); |
| 7213 | return JIM_OK; |
| 7214 | } |
| 7215 | |
| 7216 | static int array_cmd_get(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 7217 | { |
| 7218 | Jim_Obj *objPtr = Jim_GetVariable(interp, argv[0], JIM_NONE); |
| 7219 | Jim_Obj *patternObj; |
| 7220 | |
| 7221 | if (!objPtr) { |
| 7222 | return JIM_OK; |
| 7223 | } |
| 7224 | |
| 7225 | patternObj = (argc == 1) ? NULL : argv[1]; |
| 7226 | |
| 7227 | |
| 7228 | if (patternObj == NULL || Jim_CompareStringImmediate(interp, patternObj, "*")) { |
| 7229 | if (Jim_IsList(objPtr) && Jim_ListLength(interp, objPtr) % 2 == 0) { |
| 7230 | |
| 7231 | Jim_SetResult(interp, objPtr); |
| 7232 | return JIM_OK; |
| 7233 | } |
| 7234 | } |
| 7235 | |
| 7236 | return Jim_DictMatchTypes(interp, objPtr, patternObj, JIM_DICTMATCH_KEYS, JIM_DICTMATCH_KEYS | JIM_DICTMATCH_VALUES); |
| 7237 | } |
| 7238 | |
| 7239 | static int array_cmd_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 7240 | { |
| 7241 | Jim_Obj *objPtr = Jim_GetVariable(interp, argv[0], JIM_NONE); |
| 7242 | |
| 7243 | if (!objPtr) { |
| 7244 | return JIM_OK; |
| 7245 | } |
| 7246 | |
| 7247 | return Jim_DictMatchTypes(interp, objPtr, argc == 1 ? NULL : argv[1], JIM_DICTMATCH_KEYS, JIM_DICTMATCH_KEYS); |
| 7248 | } |
| 7249 | |
| 7250 | static int array_cmd_unset(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 7251 | { |
| 7252 | int i; |
| 7253 | int len; |
| 7254 | Jim_Obj *resultObj; |
| 7255 | Jim_Obj *objPtr; |
| 7256 | Jim_Obj **dictValuesObj; |
| 7257 | |
| 7258 | if (argc == 1 || Jim_CompareStringImmediate(interp, argv[1], "*")) { |
| 7259 | |
| 7260 | Jim_UnsetVariable(interp, argv[0], JIM_NONE); |
| 7261 | return JIM_OK; |
| 7262 | } |
| 7263 | |
| 7264 | objPtr = Jim_GetVariable(interp, argv[0], JIM_NONE); |
| 7265 | |
| 7266 | if (objPtr == NULL) { |
| 7267 | |
| 7268 | return JIM_OK; |
| 7269 | } |
| 7270 | |
| 7271 | dictValuesObj = Jim_DictPairs(interp, objPtr, &len); |
| 7272 | if (dictValuesObj == NULL) { |
| 7273 | |
| 7274 | Jim_SetResultString(interp, "", -1); |
| 7275 | return JIM_OK; |
| 7276 | } |
| 7277 | |
| 7278 | |
| 7279 | resultObj = Jim_NewDictObj(interp, NULL, 0); |
| 7280 | |
| 7281 | for (i = 0; i < len; i += 2) { |
| 7282 | if (!Jim_StringMatchObj(interp, argv[1], dictValuesObj[i], 0)) { |
| 7283 | Jim_DictAddElement(interp, resultObj, dictValuesObj[i], dictValuesObj[i + 1]); |
| 7284 | } |
| 7285 | } |
| 7286 | |
| 7287 | Jim_SetVariable(interp, argv[0], resultObj); |
| 7288 | return JIM_OK; |
| 7289 | } |
| 7290 | |
| 7291 | static int array_cmd_size(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 7292 | { |
| 7293 | Jim_Obj *objPtr; |
| 7294 | int len = 0; |
| 7295 | |
| 7296 | |
| 7297 | objPtr = Jim_GetVariable(interp, argv[0], JIM_NONE); |
| 7298 | if (objPtr) { |
| 7299 | len = Jim_DictSize(interp, objPtr); |
| 7300 | if (len < 0) { |
| 7301 | |
| 7302 | Jim_SetResultInt(interp, 0); |
| 7303 | return JIM_OK; |
| 7304 | } |
| 7305 | } |
| 7306 | |
| 7307 | Jim_SetResultInt(interp, len); |
| 7308 | |
| 7309 | return JIM_OK; |
| 7310 | } |
| 7311 | |
| 7312 | static int array_cmd_stat(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 7313 | { |
| 7314 | Jim_Obj *objPtr = Jim_GetVariable(interp, argv[0], JIM_NONE); |
| 7315 | if (objPtr) { |
| 7316 | return Jim_DictInfo(interp, objPtr); |
| 7317 | } |
| 7318 | Jim_SetResultFormatted(interp, "\"%#s\" isn't an array", argv[0], NULL); |
| 7319 | return JIM_ERR; |
| 7320 | } |
| 7321 | |
| 7322 | static int array_cmd_set(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 7323 | { |
| 7324 | int i; |
| 7325 | int len; |
| 7326 | Jim_Obj *listObj = argv[1]; |
| 7327 | Jim_Obj *dictObj; |
| 7328 | |
| 7329 | len = Jim_ListLength(interp, listObj); |
| 7330 | if (len % 2) { |
| 7331 | Jim_SetResultString(interp, "list must have an even number of elements", -1); |
| 7332 | return JIM_ERR; |
| 7333 | } |
| 7334 | |
| 7335 | dictObj = Jim_GetVariable(interp, argv[0], JIM_UNSHARED); |
| 7336 | if (!dictObj) { |
| 7337 | |
| 7338 | return Jim_SetVariable(interp, argv[0], listObj); |
| 7339 | } |
| 7340 | else if (Jim_DictSize(interp, dictObj) < 0) { |
| 7341 | return JIM_ERR; |
| 7342 | } |
| 7343 | |
| 7344 | if (Jim_IsShared(dictObj)) { |
| 7345 | dictObj = Jim_DuplicateObj(interp, dictObj); |
| 7346 | } |
| 7347 | |
| 7348 | for (i = 0; i < len; i += 2) { |
| 7349 | Jim_Obj *nameObj; |
| 7350 | Jim_Obj *valueObj; |
| 7351 | |
| 7352 | Jim_ListIndex(interp, listObj, i, &nameObj, JIM_NONE); |
| 7353 | Jim_ListIndex(interp, listObj, i + 1, &valueObj, JIM_NONE); |
| 7354 | |
| 7355 | Jim_DictAddElement(interp, dictObj, nameObj, valueObj); |
| 7356 | } |
| 7357 | return Jim_SetVariable(interp, argv[0], dictObj); |
| 7358 | } |
| 7359 | |
| 7360 | static const jim_subcmd_type array_command_table[] = { |
| 7361 | { "exists", |
| 7362 | "arrayName", |
| 7363 | array_cmd_exists, |
| 7364 | 1, |
| 7365 | 1, |
| 7366 | |
| 7367 | }, |
| 7368 | { "get", |
| 7369 | "arrayName ?pattern?", |
| 7370 | array_cmd_get, |
| 7371 | 1, |
| 7372 | 2, |
| 7373 | |
| 7374 | }, |
| 7375 | { "names", |
| 7376 | "arrayName ?pattern?", |
| 7377 | array_cmd_names, |
| 7378 | 1, |
| 7379 | 2, |
| 7380 | |
| 7381 | }, |
| 7382 | { "set", |
| 7383 | "arrayName list", |
| 7384 | array_cmd_set, |
| 7385 | 2, |
| 7386 | 2, |
| 7387 | |
| 7388 | }, |
| 7389 | { "size", |
| 7390 | "arrayName", |
| 7391 | array_cmd_size, |
| 7392 | 1, |
| 7393 | 1, |
| 7394 | |
| 7395 | }, |
| 7396 | { "stat", |
| 7397 | "arrayName", |
| 7398 | array_cmd_stat, |
| 7399 | 1, |
| 7400 | 1, |
| 7401 | |
| 7402 | }, |
| 7403 | { "unset", |
| 7404 | "arrayName ?pattern?", |
| 7405 | array_cmd_unset, |
| 7406 | 1, |
| 7407 | 2, |
| 7408 | |
| 7409 | }, |
| 7410 | { NULL |
| 7411 | } |
| 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 *); |
| 7423 | extern int Jim_aioInit(Jim_Interp *); |
| 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); |
| 7434 | Jim_aioInit(interp); |
| 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; |
| 7445 | } |
| 7446 | #ifndef JIM_TINY |
| 7447 | #define JIM_OPTIMIZATION |
| 7448 | #endif |
| 7449 | |
| 7450 | #include <stdio.h> |
| 7451 | #include <stdlib.h> |
| 7452 | |
| 7453 | #include <string.h> |
| 7454 | #include <stdarg.h> |
| 7455 | #include <ctype.h> |
| 7456 | #include <limits.h> |
| 7457 | #include <assert.h> |
| 7458 | #include <errno.h> |
| 7459 | #include <time.h> |
| 7460 | #include <setjmp.h> |
| 7461 | |
| 7462 | |
| 7463 | #ifdef HAVE_SYS_TIME_H |
| 7464 | #include <sys/time.h> |
| 7465 | #endif |
| 7466 | #ifdef HAVE_EXECINFO_H |
| 7467 | #include <execinfo.h> |
| 7468 | #endif |
| 7469 | #ifdef HAVE_CRT_EXTERNS_H |
| 7470 | #include <crt_externs.h> |
| 7471 | #endif |
| 7472 | |
| 7473 | |
| 7474 | #include <math.h> |
| 7475 | |
| 7476 | |
| 7477 | |
| 7478 | |
| 7479 | |
| 7480 | #ifndef TCL_LIBRARY |
| 7481 | #define TCL_LIBRARY "." |
| 7482 | #endif |
| 7483 | #ifndef TCL_PLATFORM_OS |
| 7484 | #define TCL_PLATFORM_OS "unknown" |
| 7485 | #endif |
| 7486 | #ifndef TCL_PLATFORM_PLATFORM |
| 7487 | #define TCL_PLATFORM_PLATFORM "unknown" |
| 7488 | #endif |
| 7489 | #ifndef TCL_PLATFORM_PATH_SEPARATOR |
| 7490 | #define TCL_PLATFORM_PATH_SEPARATOR ":" |
| 7491 | #endif |
| 7492 | |
| 7493 | |
| 7494 | |
| 7495 | |
| 7496 | |
| 7497 | |
| 7498 | |
| 7499 | #ifdef JIM_MAINTAINER |
| 7500 | #define JIM_DEBUG_COMMAND |
| 7501 | #define JIM_DEBUG_PANIC |
| 7502 | #endif |
| 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, ...); |
| 7515 | #define JimPanic(X) JimPanicDump X |
| 7516 | #else |
| 7517 | #define JimPanic(X) |
| 7518 | #endif |
| 7519 | |
| 7520 | #ifdef JIM_OPTIMIZATION |
| 7521 | static int JimIsWide(Jim_Obj *objPtr); |
| 7522 | #define JIM_IF_OPTIM(X) X |
| 7523 | #else |
| 7524 | #define JIM_IF_OPTIM(X) |
| 7525 | #endif |
| 7526 | |
| 7527 | |
| 7528 | static char JimEmptyStringRep[] = ""; |
| 7529 | |
| 7530 | static void JimFreeCallFrame(Jim_Interp *interp, Jim_CallFrame *cf, int action); |
| 7531 | static int ListSetIndex(Jim_Interp *interp, Jim_Obj *listPtr, int listindex, Jim_Obj *newObjPtr, |
| 7532 | int flags); |
| 7533 | static int Jim_ListIndices(Jim_Interp *interp, Jim_Obj *listPtr, Jim_Obj *const *indexv, int indexc, |
| 7534 | Jim_Obj **resultObj, int flags); |
| 7535 | static int JimDeleteLocalProcs(Jim_Interp *interp, Jim_Stack *localCommands); |
| 7536 | static Jim_Obj *JimExpandDictSugar(Jim_Interp *interp, Jim_Obj *objPtr); |
| 7537 | static void SetDictSubstFromAny(Jim_Interp *interp, Jim_Obj *objPtr); |
| 7538 | static void JimSetFailedEnumResult(Jim_Interp *interp, const char *arg, const char *badtype, |
| 7539 | const char *prefix, const char *const *tablePtr, const char *name); |
| 7540 | static int JimCallProcedure(Jim_Interp *interp, Jim_Cmd *cmd, int argc, Jim_Obj *const *argv); |
| 7541 | static int JimGetWideNoErr(Jim_Interp *interp, Jim_Obj *objPtr, jim_wide * widePtr); |
| 7542 | static int JimSign(jim_wide w); |
| 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) { |
| 7564 | *uc = utf8_upper(*uc); |
| 7565 | } |
| 7566 | return l; |
| 7567 | } |
| 7568 | |
| 7569 | static Jim_Obj *JimPushInterpObjImpl(Jim_Obj **iop, Jim_Obj *no) |
| 7570 | { |
| 7571 | Jim_Obj *io = *iop; |
| 7572 | Jim_IncrRefCount(no); |
| 7573 | *iop = no; |
| 7574 | return io; |
| 7575 | } |
| 7576 | |
| 7577 | #define JimPushInterpObj(IO, NO) JimPushInterpObjImpl(&(IO), NO) |
| 7578 | #define JimPopInterpObj(I, IO, SO) do { Jim_DecrRefCount(I, IO); IO = SO; } while (0) |
| 7579 | |
| 7580 | |
| 7581 | #define JIM_CHARSET_SCAN 2 |
| 7582 | #define JIM_CHARSET_GLOB 0 |
| 7583 | |
| 7584 | static const char *JimCharsetMatch(const char *pattern, int plen, int c, int flags) |
| 7585 | { |
| 7586 | int not = 0; |
| 7587 | int pchar; |
| 7588 | int match = 0; |
| 7589 | int nocase = 0; |
| 7590 | int n; |
| 7591 | |
| 7592 | if (flags & JIM_NOCASE) { |
| 7593 | nocase++; |
| 7594 | c = utf8_upper(c); |
| 7595 | } |
| 7596 | |
| 7597 | if (flags & JIM_CHARSET_SCAN) { |
| 7598 | if (*pattern == '^') { |
| 7599 | not++; |
| 7600 | pattern++; |
| 7601 | plen--; |
| 7602 | } |
| 7603 | |
| 7604 | |
| 7605 | if (*pattern == ']') { |
| 7606 | goto first; |
| 7607 | } |
| 7608 | } |
| 7609 | |
| 7610 | while (plen && *pattern != ']') { |
| 7611 | |
| 7612 | if (pattern[0] == '\\') { |
| 7613 | first: |
| 7614 | n = utf8_tounicode_case(pattern, &pchar, nocase); |
| 7615 | pattern += n; |
| 7616 | plen -= n; |
| 7617 | } |
| 7618 | else { |
| 7619 | |
| 7620 | int start; |
| 7621 | int end; |
| 7622 | |
| 7623 | n = utf8_tounicode_case(pattern, &start, nocase); |
| 7624 | pattern += n; |
| 7625 | plen -= n; |
| 7626 | if (pattern[0] == '-' && plen > 1) { |
| 7627 | |
| 7628 | n = 1 + utf8_tounicode_case(pattern + 1, &end, nocase); |
| 7629 | pattern += n; |
| 7630 | plen -= n; |
| 7631 | |
| 7632 | |
| 7633 | if ((c >= start && c <= end) || (c >= end && c <= start)) { |
| 7634 | match = 1; |
| 7635 | } |
| 7636 | continue; |
| 7637 | } |
| 7638 | pchar = start; |
| 7639 | } |
| 7640 | |
| 7641 | if (pchar == c) { |
| 7642 | match = 1; |
| 7643 | } |
| 7644 | } |
| 7645 | if (not) { |
| 7646 | match = !match; |
| 7647 | } |
| 7648 | |
| 7649 | return match ? pattern : NULL; |
| 7650 | } |
| 7651 | |
| 7652 | |
| 7653 | |
| 7654 | static int JimGlobMatch(const char *pattern, int plen, const char *string, int slen, int nocase) |
| 7655 | { |
| 7656 | int c; |
| 7657 | int pchar; |
| 7658 | int n; |
| 7659 | const char *p; |
| 7660 | while (plen) { |
| 7661 | switch (pattern[0]) { |
| 7662 | case '*': |
| 7663 | while (pattern[1] == '*' && plen) { |
| 7664 | pattern++; |
| 7665 | plen--; |
| 7666 | } |
| 7667 | pattern++; |
| 7668 | plen--; |
| 7669 | if (!plen) { |
| 7670 | return 1; |
| 7671 | } |
| 7672 | while (slen) { |
| 7673 | |
| 7674 | if (JimGlobMatch(pattern, plen, string, slen, nocase)) |
| 7675 | return 1; |
| 7676 | n = utf8_tounicode(string, &c); |
| 7677 | string += n; |
| 7678 | slen -= n; |
| 7679 | } |
| 7680 | return 0; |
| 7681 | |
| 7682 | case '?': |
| 7683 | n = utf8_tounicode(string, &c); |
| 7684 | string += n; |
| 7685 | slen -= n; |
| 7686 | break; |
| 7687 | |
| 7688 | case '[': { |
| 7689 | n = utf8_tounicode(string, &c); |
| 7690 | string += n; |
| 7691 | slen -= n; |
| 7692 | p = JimCharsetMatch(pattern + 1, plen - 1, c, nocase ? JIM_NOCASE : 0); |
| 7693 | if (!p) { |
| 7694 | return 0; |
| 7695 | } |
| 7696 | plen -= p - pattern; |
| 7697 | pattern = p; |
| 7698 | |
| 7699 | if (!plen) { |
| 7700 | |
| 7701 | continue; |
| 7702 | } |
| 7703 | break; |
| 7704 | } |
| 7705 | case '\\': |
| 7706 | if (pattern[1]) { |
| 7707 | pattern++; |
| 7708 | plen--; |
| 7709 | } |
| 7710 | |
| 7711 | default: |
| 7712 | n = utf8_tounicode_case(string, &c, nocase); |
| 7713 | string += n; |
| 7714 | slen -= n; |
| 7715 | utf8_tounicode_case(pattern, &pchar, nocase); |
| 7716 | if (pchar != c) { |
| 7717 | return 0; |
| 7718 | } |
| 7719 | break; |
| 7720 | } |
| 7721 | n = utf8_tounicode_case(pattern, &pchar, nocase); |
| 7722 | pattern += n; |
| 7723 | plen -= n; |
| 7724 | if (!slen) { |
| 7725 | while (*pattern == '*' && plen) { |
| 7726 | pattern++; |
| 7727 | plen--; |
| 7728 | } |
| 7729 | break; |
| 7730 | } |
| 7731 | } |
| 7732 | if (!plen && !slen) { |
| 7733 | return 1; |
| 7734 | } |
| 7735 | return 0; |
| 7736 | } |
| 7737 | |
| 7738 | static int JimStringCompareUtf8(const char *s1, int l1, const char *s2, int l2, int nocase) |
| 7739 | { |
| 7740 | int minlen = l1; |
| 7741 | if (l2 < l1) { |
| 7742 | minlen = l2; |
| 7743 | } |
| 7744 | while (minlen) { |
| 7745 | int c1, c2; |
| 7746 | s1 += utf8_tounicode_case(s1, &c1, nocase); |
| 7747 | s2 += utf8_tounicode_case(s2, &c2, nocase); |
| 7748 | if (c1 != c2) { |
| 7749 | return JimSign(c1 - c2); |
| 7750 | } |
| 7751 | minlen--; |
| 7752 | } |
| 7753 | |
| 7754 | if (l1 < l2) { |
| 7755 | return -1; |
| 7756 | } |
| 7757 | if (l1 > l2) { |
| 7758 | return 1; |
| 7759 | } |
| 7760 | return 0; |
| 7761 | } |
| 7762 | |
| 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); |
| 7774 | |
| 7775 | l1bytelen = utf8_index(s1, l1); |
| 7776 | |
| 7777 | for (i = idx; i <= l2 - l1; i++) { |
| 7778 | int c; |
| 7779 | if (memcmp(s2, s1, l1bytelen) == 0) { |
| 7780 | return i; |
| 7781 | } |
| 7782 | s2 += utf8_tounicode(s2, &c); |
| 7783 | } |
| 7784 | return -1; |
| 7785 | } |
| 7786 | |
| 7787 | static int JimStringLast(const char *s1, int l1, const char *s2, int l2) |
| 7788 | { |
| 7789 | const char *p; |
| 7790 | |
| 7791 | if (!l1 || !l2 || l1 > l2) |
| 7792 | return -1; |
| 7793 | |
| 7794 | |
| 7795 | for (p = s2 + l2 - 1; p != s2 - 1; p--) { |
| 7796 | if (*p == *s1 && memcmp(s1, p, l1) == 0) { |
| 7797 | return p - s2; |
| 7798 | } |
| 7799 | } |
| 7800 | return -1; |
| 7801 | } |
| 7802 | |
| 7803 | #ifdef JIM_UTF8 |
| 7804 | static int JimStringLastUtf8(const char *s1, int l1, const char *s2, int l2) |
| 7805 | { |
| 7806 | int n = JimStringLast(s1, utf8_index(s1, l1), s2, utf8_index(s2, l2)); |
| 7807 | if (n > 0) { |
| 7808 | n = utf8_strlen(s2, n); |
| 7809 | } |
| 7810 | return n; |
| 7811 | } |
| 7812 | #endif |
| 7813 | |
| 7814 | static int JimCheckConversion(const char *str, const char *endptr) |
| 7815 | { |
| 7816 | if (str[0] == '\0' || str == endptr) { |
| 7817 | return JIM_ERR; |
| 7818 | } |
| 7819 | |
| 7820 | if (endptr[0] != '\0') { |
| 7821 | while (*endptr) { |
| 7822 | if (!isspace(UCHAR(*endptr))) { |
| 7823 | return JIM_ERR; |
| 7824 | } |
| 7825 | endptr++; |
| 7826 | } |
| 7827 | } |
| 7828 | return JIM_OK; |
| 7829 | } |
| 7830 | |
| 7831 | static int JimNumberBase(const char *str, int *base, int *sign) |
| 7832 | { |
| 7833 | int i = 0; |
| 7834 | |
| 7835 | *base = 0; |
| 7836 | |
| 7837 | while (isspace(UCHAR(str[i]))) { |
| 7838 | i++; |
| 7839 | } |
| 7840 | |
| 7841 | if (str[i] == '-') { |
| 7842 | *sign = -1; |
| 7843 | i++; |
| 7844 | } |
| 7845 | else { |
| 7846 | if (str[i] == '+') { |
| 7847 | i++; |
| 7848 | } |
| 7849 | *sign = 1; |
| 7850 | } |
| 7851 | |
| 7852 | if (str[i] != '0') { |
| 7853 | |
| 7854 | return 0; |
| 7855 | } |
| 7856 | |
| 7857 | |
| 7858 | switch (str[i + 1]) { |
| 7859 | case 'x': case 'X': *base = 16; break; |
| 7860 | case 'o': case 'O': *base = 8; break; |
| 7861 | case 'b': case 'B': *base = 2; break; |
| 7862 | case 'd': case 'D': *base = 10; break; |
| 7863 | default: return 0; |
| 7864 | } |
| 7865 | i += 2; |
| 7866 | |
| 7867 | if (str[i] != '-' && str[i] != '+' && !isspace(UCHAR(str[i]))) { |
| 7868 | |
| 7869 | return i; |
| 7870 | } |
| 7871 | |
| 7872 | *base = 0; |
| 7873 | return 0; |
| 7874 | } |
| 7875 | |
| 7876 | static long jim_strtol(const char *str, char **endptr) |
| 7877 | { |
| 7878 | int sign; |
| 7879 | int base; |
| 7880 | int i = JimNumberBase(str, &base, &sign); |
| 7881 | |
| 7882 | if (base != 0) { |
| 7883 | long value = strtol(str + i, endptr, base); |
| 7884 | if (endptr == NULL || *endptr != str + i) { |
| 7885 | return value * sign; |
| 7886 | } |
| 7887 | } |
| 7888 | |
| 7889 | |
| 7890 | return strtol(str, endptr, 10); |
| 7891 | } |
| 7892 | |
| 7893 | |
| 7894 | static jim_wide jim_strtoull(const char *str, char **endptr) |
| 7895 | { |
| 7896 | #ifdef HAVE_LONG_LONG |
| 7897 | int sign; |
| 7898 | int base; |
| 7899 | int i = JimNumberBase(str, &base, &sign); |
| 7900 | |
| 7901 | if (base != 0) { |
| 7902 | jim_wide value = strtoull(str + i, endptr, base); |
| 7903 | if (endptr == NULL || *endptr != str + i) { |
| 7904 | return value * sign; |
| 7905 | } |
| 7906 | } |
| 7907 | |
| 7908 | |
| 7909 | return strtoull(str, endptr, 10); |
| 7910 | #else |
| 7911 | return (unsigned long)jim_strtol(str, endptr); |
| 7912 | #endif |
| 7913 | } |
| 7914 | |
| 7915 | int Jim_StringToWide(const char *str, jim_wide * widePtr, int base) |
| 7916 | { |
| 7917 | char *endptr; |
| 7918 | |
| 7919 | if (base) { |
| 7920 | *widePtr = strtoull(str, &endptr, base); |
| 7921 | } |
| 7922 | else { |
| 7923 | *widePtr = jim_strtoull(str, &endptr); |
| 7924 | } |
| 7925 | |
| 7926 | return JimCheckConversion(str, endptr); |
| 7927 | } |
| 7928 | |
| 7929 | int Jim_StringToDouble(const char *str, double *doublePtr) |
| 7930 | { |
| 7931 | char *endptr; |
| 7932 | |
| 7933 | |
| 7934 | errno = 0; |
| 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 | |
| 7946 | |
| 7947 | if (b == 1) { |
| 7948 | |
| 7949 | return 1; |
| 7950 | } |
| 7951 | if (e < 0) { |
| 7952 | if (b != -1) { |
| 7953 | return 0; |
| 7954 | } |
| 7955 | e = -e; |
| 7956 | } |
| 7957 | while (e) |
| 7958 | { |
| 7959 | if (e & 1) { |
| 7960 | res *= b; |
| 7961 | } |
| 7962 | e >>= 1; |
| 7963 | b *= b; |
| 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) { |
| 7975 | return; |
| 7976 | } |
| 7977 | |
| 7978 | va_start(ap, fmt); |
| 7979 | |
| 7980 | fprintf(stderr, "\nJIM INTERPRETER PANIC: "); |
| 7981 | vfprintf(stderr, fmt, ap); |
| 7982 | fprintf(stderr, "\n\n"); |
| 7983 | va_end(ap); |
| 7984 | |
| 7985 | #if defined(HAVE_BACKTRACE) |
| 7986 | { |
| 7987 | void *array[40]; |
| 7988 | int size, i; |
| 7989 | char **strings; |
| 7990 | |
| 7991 | size = backtrace(array, 40); |
| 7992 | strings = backtrace_symbols(array, size); |
| 7993 | for (i = 0; i < size; i++) |
| 7994 | fprintf(stderr, "[backtrace] %s\n", strings[i]); |
| 7995 | fprintf(stderr, "[backtrace] Include the above lines and the output\n"); |
| 7996 | fprintf(stderr, "[backtrace] of 'nm <executable>' in the bug report.\n"); |
| 7997 | } |
| 7998 | #endif |
| 7999 | |
| 8000 | exit(1); |
| 8001 | } |
| 8002 | #endif |
| 8003 | |
| 8004 | |
| 8005 | void *JimDefaultAllocator(void *ptr, size_t size) |
| 8006 | { |
| 8007 | if (size == 0) { |
| 8008 | free(ptr); |
| 8009 | return NULL; |
| 8010 | } |
| 8011 | else if (ptr) { |
| 8012 | return realloc(ptr, size); |
| 8013 | } |
| 8014 | else { |
| 8015 | return malloc(size); |
| 8016 | } |
| 8017 | } |
| 8018 | |
| 8019 | void *(*Jim_Allocator)(void *ptr, size_t size) = JimDefaultAllocator; |
| 8020 | |
| 8021 | char *Jim_StrDup(const char *s) |
| 8022 | { |
| 8023 | return Jim_StrDupLen(s, strlen(s)); |
| 8024 | } |
| 8025 | |
| 8026 | char *Jim_StrDupLen(const char *s, int l) |
| 8027 | { |
| 8028 | char *copy = Jim_Alloc(l + 1); |
| 8029 | |
| 8030 | memcpy(copy, s, l); |
| 8031 | copy[l] = 0; |
| 8032 | return copy; |
| 8033 | } |
| 8034 | |
| 8035 | |
| 8036 | jim_wide Jim_GetTimeUsec(unsigned type) |
| 8037 | { |
| 8038 | long long now; |
| 8039 | struct timeval tv; |
| 8040 | |
| 8041 | #if defined(HAVE_CLOCK_GETTIME) |
| 8042 | struct timespec ts; |
| 8043 | |
| 8044 | if (clock_gettime(type, &ts) == 0) { |
| 8045 | now = ts.tv_sec * 1000000LL + ts.tv_nsec / 1000; |
| 8046 | } |
| 8047 | else |
| 8048 | #endif |
| 8049 | { |
| 8050 | gettimeofday(&tv, NULL); |
| 8051 | |
| 8052 | now = tv.tv_sec * 1000000LL + tv.tv_usec; |
| 8053 | } |
| 8054 | |
| 8055 | return now; |
| 8056 | } |
| 8057 | |
| 8058 | |
| 8059 | |
| 8060 | |
| 8061 | |
| 8062 | static void JimExpandHashTableIfNeeded(Jim_HashTable *ht); |
| 8063 | static unsigned int JimHashTableNextPower(unsigned int size); |
| 8064 | static Jim_HashEntry *JimInsertHashEntry(Jim_HashTable *ht, const void *key, int replace); |
| 8065 | |
| 8066 | |
| 8067 | |
| 8068 | |
| 8069 | unsigned int Jim_IntHashFunction(unsigned int key) |
| 8070 | { |
| 8071 | key += ~(key << 15); |
| 8072 | key ^= (key >> 10); |
| 8073 | key += (key << 3); |
| 8074 | key ^= (key >> 6); |
| 8075 | key += ~(key << 11); |
| 8076 | key ^= (key >> 16); |
| 8077 | return key; |
| 8078 | } |
| 8079 | |
| 8080 | |
| 8081 | unsigned int Jim_GenHashFunction(const unsigned char *string, int length) |
| 8082 | { |
| 8083 | unsigned result = 0; |
| 8084 | string += length; |
| 8085 | while (length--) { |
| 8086 | result += (result << 3) + (unsigned char)(*--string); |
| 8087 | } |
| 8088 | return result; |
| 8089 | } |
| 8090 | |
| 8091 | |
| 8092 | |
| 8093 | static void JimResetHashTable(Jim_HashTable *ht) |
| 8094 | { |
| 8095 | ht->table = NULL; |
| 8096 | ht->size = 0; |
| 8097 | ht->sizemask = 0; |
| 8098 | ht->used = 0; |
| 8099 | ht->collisions = 0; |
| 8100 | #ifdef JIM_RANDOMISE_HASH |
| 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; |
| 8112 | iter->entry = NULL; |
| 8113 | iter->nextEntry = NULL; |
| 8114 | } |
| 8115 | |
| 8116 | |
| 8117 | int Jim_InitHashTable(Jim_HashTable *ht, const Jim_HashTableType *type, void *privDataPtr) |
| 8118 | { |
| 8119 | JimResetHashTable(ht); |
| 8120 | ht->type = type; |
| 8121 | ht->privdata = privDataPtr; |
| 8122 | return JIM_OK; |
| 8123 | } |
| 8124 | |
| 8125 | |
| 8126 | void Jim_ExpandHashTable(Jim_HashTable *ht, unsigned int size) |
| 8127 | { |
| 8128 | Jim_HashTable n; |
| 8129 | unsigned int realsize = JimHashTableNextPower(size), i; |
| 8130 | |
| 8131 | if (size <= ht->used) |
| 8132 | return; |
| 8133 | |
| 8134 | Jim_InitHashTable(&n, ht->type, ht->privdata); |
| 8135 | n.size = realsize; |
| 8136 | n.sizemask = realsize - 1; |
| 8137 | n.table = Jim_Alloc(realsize * sizeof(Jim_HashEntry *)); |
| 8138 | |
| 8139 | n.uniq = ht->uniq; |
| 8140 | |
| 8141 | |
| 8142 | memset(n.table, 0, realsize * sizeof(Jim_HashEntry *)); |
| 8143 | |
| 8144 | n.used = ht->used; |
| 8145 | for (i = 0; ht->used > 0; i++) { |
| 8146 | Jim_HashEntry *he, *nextHe; |
| 8147 | |
| 8148 | if (ht->table[i] == NULL) |
| 8149 | continue; |
| 8150 | |
| 8151 | |
| 8152 | he = ht->table[i]; |
| 8153 | while (he) { |
| 8154 | unsigned int h; |
| 8155 | |
| 8156 | nextHe = he->next; |
| 8157 | |
| 8158 | h = Jim_HashKey(ht, he->key) & n.sizemask; |
| 8159 | he->next = n.table[h]; |
| 8160 | n.table[h] = he; |
| 8161 | ht->used--; |
| 8162 | |
| 8163 | he = nextHe; |
| 8164 | } |
| 8165 | } |
| 8166 | assert(ht->used == 0); |
| 8167 | Jim_Free(ht->table); |
| 8168 | |
| 8169 | |
| 8170 | *ht = n; |
| 8171 | } |
| 8172 | |
| 8173 | int Jim_AddHashEntry(Jim_HashTable *ht, const void *key, void *val) |
| 8174 | { |
| 8175 | Jim_HashEntry *entry = JimInsertHashEntry(ht, key, 0);; |
| 8176 | if (entry == NULL) |
| 8177 | return JIM_ERR; |
| 8178 | |
| 8179 | |
| 8180 | Jim_SetHashKey(ht, entry, key); |
| 8181 | Jim_SetHashVal(ht, entry, val); |
| 8182 | return JIM_OK; |
| 8183 | } |
| 8184 | |
| 8185 | |
| 8186 | int Jim_ReplaceHashEntry(Jim_HashTable *ht, const void *key, void *val) |
| 8187 | { |
| 8188 | int existed; |
| 8189 | Jim_HashEntry *entry; |
| 8190 | |
| 8191 | entry = JimInsertHashEntry(ht, key, 1); |
| 8192 | if (entry->key) { |
| 8193 | if (ht->type->valDestructor && ht->type->valDup) { |
| 8194 | void *newval = ht->type->valDup(ht->privdata, val); |
| 8195 | ht->type->valDestructor(ht->privdata, entry->u.val); |
| 8196 | entry->u.val = newval; |
| 8197 | } |
| 8198 | else { |
| 8199 | Jim_FreeEntryVal(ht, entry); |
| 8200 | Jim_SetHashVal(ht, entry, val); |
| 8201 | } |
| 8202 | existed = 1; |
| 8203 | } |
| 8204 | else { |
| 8205 | |
| 8206 | Jim_SetHashKey(ht, entry, key); |
| 8207 | Jim_SetHashVal(ht, entry, val); |
| 8208 | existed = 0; |
| 8209 | } |
| 8210 | |
| 8211 | return existed; |
| 8212 | } |
| 8213 | |
| 8214 | int Jim_DeleteHashEntry(Jim_HashTable *ht, const void *key) |
| 8215 | { |
| 8216 | if (ht->used) { |
| 8217 | unsigned int h = Jim_HashKey(ht, key) & ht->sizemask; |
| 8218 | Jim_HashEntry *prevHe = NULL; |
| 8219 | Jim_HashEntry *he = ht->table[h]; |
| 8220 | |
| 8221 | while (he) { |
| 8222 | if (Jim_CompareHashKeys(ht, key, he->key)) { |
| 8223 | |
| 8224 | if (prevHe) |
| 8225 | prevHe->next = he->next; |
| 8226 | else |
| 8227 | ht->table[h] = he->next; |
| 8228 | ht->used--; |
| 8229 | Jim_FreeEntryKey(ht, he); |
| 8230 | Jim_FreeEntryVal(ht, he); |
| 8231 | Jim_Free(he); |
| 8232 | return JIM_OK; |
| 8233 | } |
| 8234 | prevHe = he; |
| 8235 | he = he->next; |
| 8236 | } |
| 8237 | } |
| 8238 | |
| 8239 | return JIM_ERR; |
| 8240 | } |
| 8241 | |
| 8242 | void Jim_ClearHashTable(Jim_HashTable *ht) |
| 8243 | { |
| 8244 | unsigned int i; |
| 8245 | |
| 8246 | |
| 8247 | for (i = 0; ht->used > 0; i++) { |
| 8248 | Jim_HashEntry *he, *nextHe; |
| 8249 | |
| 8250 | he = ht->table[i]; |
| 8251 | while (he) { |
| 8252 | nextHe = he->next; |
| 8253 | Jim_FreeEntryKey(ht, he); |
| 8254 | Jim_FreeEntryVal(ht, he); |
| 8255 | Jim_Free(he); |
| 8256 | ht->used--; |
| 8257 | he = nextHe; |
| 8258 | } |
| 8259 | ht->table[i] = NULL; |
| 8260 | } |
| 8261 | } |
| 8262 | |
| 8263 | int Jim_FreeHashTable(Jim_HashTable *ht) |
| 8264 | { |
| 8265 | Jim_ClearHashTable(ht); |
| 8266 | |
| 8267 | Jim_Free(ht->table); |
| 8268 | |
| 8269 | JimResetHashTable(ht); |
| 8270 | return JIM_OK; |
| 8271 | } |
| 8272 | |
| 8273 | Jim_HashEntry *Jim_FindHashEntry(Jim_HashTable *ht, const void *key) |
| 8274 | { |
| 8275 | Jim_HashEntry *he; |
| 8276 | unsigned int h; |
| 8277 | |
| 8278 | if (ht->used == 0) |
| 8279 | return NULL; |
| 8280 | h = Jim_HashKey(ht, key) & ht->sizemask; |
| 8281 | he = ht->table[h]; |
| 8282 | while (he) { |
| 8283 | if (Jim_CompareHashKeys(ht, key, he->key)) |
| 8284 | return he; |
| 8285 | he = he->next; |
| 8286 | } |
| 8287 | return NULL; |
| 8288 | } |
| 8289 | |
| 8290 | Jim_HashTableIterator *Jim_GetHashTableIterator(Jim_HashTable *ht) |
| 8291 | { |
| 8292 | Jim_HashTableIterator *iter = Jim_Alloc(sizeof(*iter)); |
| 8293 | JimInitHashTableIterator(ht, iter); |
| 8294 | return iter; |
| 8295 | } |
| 8296 | |
| 8297 | Jim_HashEntry *Jim_NextHashEntry(Jim_HashTableIterator *iter) |
| 8298 | { |
| 8299 | while (1) { |
| 8300 | if (iter->entry == NULL) { |
| 8301 | iter->index++; |
| 8302 | if (iter->index >= (signed)iter->ht->size) |
| 8303 | break; |
| 8304 | iter->entry = iter->ht->table[iter->index]; |
| 8305 | } |
| 8306 | else { |
| 8307 | iter->entry = iter->nextEntry; |
| 8308 | } |
| 8309 | if (iter->entry) { |
| 8310 | iter->nextEntry = iter->entry->next; |
| 8311 | return iter->entry; |
| 8312 | } |
| 8313 | } |
| 8314 | return NULL; |
| 8315 | } |
| 8316 | |
| 8317 | |
| 8318 | |
| 8319 | |
| 8320 | static void JimExpandHashTableIfNeeded(Jim_HashTable *ht) |
| 8321 | { |
| 8322 | if (ht->size == 0) |
| 8323 | Jim_ExpandHashTable(ht, JIM_HT_INITIAL_SIZE); |
| 8324 | if (ht->size == ht->used) |
| 8325 | Jim_ExpandHashTable(ht, ht->size * 2); |
| 8326 | } |
| 8327 | |
| 8328 | |
| 8329 | static unsigned int JimHashTableNextPower(unsigned int size) |
| 8330 | { |
| 8331 | unsigned int i = JIM_HT_INITIAL_SIZE; |
| 8332 | |
| 8333 | if (size >= 2147483648U) |
| 8334 | return 2147483648U; |
| 8335 | while (1) { |
| 8336 | if (i >= size) |
| 8337 | return i; |
| 8338 | i *= 2; |
| 8339 | } |
| 8340 | } |
| 8341 | |
| 8342 | static Jim_HashEntry *JimInsertHashEntry(Jim_HashTable *ht, const void *key, int replace) |
| 8343 | { |
| 8344 | unsigned int h; |
| 8345 | Jim_HashEntry *he; |
| 8346 | |
| 8347 | |
| 8348 | JimExpandHashTableIfNeeded(ht); |
| 8349 | |
| 8350 | |
| 8351 | h = Jim_HashKey(ht, key) & ht->sizemask; |
| 8352 | |
| 8353 | he = ht->table[h]; |
| 8354 | while (he) { |
| 8355 | if (Jim_CompareHashKeys(ht, key, he->key)) |
| 8356 | return replace ? he : NULL; |
| 8357 | he = he->next; |
| 8358 | } |
| 8359 | |
| 8360 | |
| 8361 | he = Jim_Alloc(sizeof(*he)); |
| 8362 | he->next = ht->table[h]; |
| 8363 | ht->table[h] = he; |
| 8364 | ht->used++; |
| 8365 | he->key = NULL; |
| 8366 | |
| 8367 | return he; |
| 8368 | } |
| 8369 | |
| 8370 | |
| 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 | } |
| 8394 | |
| 8395 | static const Jim_HashTableType JimPackageHashTableType = { |
| 8396 | JimStringCopyHTHashFunction, |
| 8397 | JimStringCopyHTDup, |
| 8398 | NULL, |
| 8399 | JimStringCopyHTKeyCompare, |
| 8400 | JimStringCopyHTKeyDestructor, |
| 8401 | NULL |
| 8402 | }; |
| 8403 | |
| 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 | |
| 8415 | if (assocPtr->delProc != NULL) |
| 8416 | assocPtr->delProc((Jim_Interp *)privdata, assocPtr->data); |
| 8417 | Jim_Free(data); |
| 8418 | } |
| 8419 | |
| 8420 | static const Jim_HashTableType JimAssocDataHashTableType = { |
| 8421 | JimStringCopyHTHashFunction, |
| 8422 | JimStringCopyHTDup, |
| 8423 | NULL, |
| 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; |
| 8452 | |
| 8453 | if (neededLen > stack->maxlen) { |
| 8454 | stack->maxlen = neededLen < 20 ? 20 : neededLen * 2; |
| 8455 | stack->vector = Jim_Realloc(stack->vector, sizeof(void *) * stack->maxlen); |
| 8456 | } |
| 8457 | stack->vector[stack->len] = element; |
| 8458 | stack->len++; |
| 8459 | } |
| 8460 | |
| 8461 | void *Jim_StackPop(Jim_Stack *stack) |
| 8462 | { |
| 8463 | if (stack->len == 0) |
| 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 |
| 8474 | #define JIM_TT_VAR 3 |
| 8475 | #define JIM_TT_DICTSUGAR 4 |
| 8476 | #define JIM_TT_CMD 5 |
| 8477 | |
| 8478 | #define JIM_TT_SEP 6 |
| 8479 | #define JIM_TT_EOL 7 |
| 8480 | #define JIM_TT_EOF 8 |
| 8481 | |
| 8482 | #define JIM_TT_LINE 9 |
| 8483 | #define JIM_TT_WORD 10 |
| 8484 | |
| 8485 | |
| 8486 | #define JIM_TT_SUBEXPR_START 11 |
| 8487 | #define JIM_TT_SUBEXPR_END 12 |
| 8488 | #define JIM_TT_SUBEXPR_COMMA 13 |
| 8489 | #define JIM_TT_EXPR_INT 14 |
| 8490 | #define JIM_TT_EXPR_DOUBLE 15 |
| 8491 | #define JIM_TT_EXPR_BOOLEAN 16 |
| 8492 | |
| 8493 | #define JIM_TT_EXPRSUGAR 17 |
| 8494 | |
| 8495 | |
| 8496 | #define JIM_TT_EXPR_OP 20 |
| 8497 | |
| 8498 | #define TOKEN_IS_SEP(type) (type >= JIM_TT_SEP && type <= JIM_TT_EOF) |
| 8499 | |
| 8500 | #define TOKEN_IS_EXPR_START(type) (type == JIM_TT_NONE || type == JIM_TT_SUBEXPR_START || type == JIM_TT_SUBEXPR_COMMA) |
| 8501 | |
| 8502 | #define TOKEN_IS_EXPR_OP(type) (type >= JIM_TT_EXPR_OP) |
| 8503 | |
| 8504 | struct JimParseMissing { |
| 8505 | int ch; |
| 8506 | int line; |
| 8507 | }; |
| 8508 | |
| 8509 | struct JimParserCtx |
| 8510 | { |
| 8511 | const char *p; |
| 8512 | int len; |
| 8513 | int linenr; |
| 8514 | const char *tstart; |
| 8515 | const char *tend; |
| 8516 | int tline; |
| 8517 | int tt; |
| 8518 | int eof; |
| 8519 | int inquote; |
| 8520 | int comment; |
| 8521 | struct JimParseMissing missing; |
| 8522 | const char *errmsg; |
| 8523 | }; |
| 8524 | |
| 8525 | static int JimParseScript(struct JimParserCtx *pc); |
| 8526 | static int JimParseSep(struct JimParserCtx *pc); |
| 8527 | static int JimParseEol(struct JimParserCtx *pc); |
| 8528 | static int JimParseCmd(struct JimParserCtx *pc); |
| 8529 | static int JimParseQuote(struct JimParserCtx *pc); |
| 8530 | static int JimParseVar(struct JimParserCtx *pc); |
| 8531 | static int JimParseBrace(struct JimParserCtx *pc); |
| 8532 | static int JimParseStr(struct JimParserCtx *pc); |
| 8533 | static int JimParseComment(struct JimParserCtx *pc); |
| 8534 | static void JimParseSubCmd(struct JimParserCtx *pc); |
| 8535 | static int JimParseSubQuote(struct JimParserCtx *pc); |
| 8536 | static Jim_Obj *JimParserGetTokenObj(Jim_Interp *interp, struct JimParserCtx *pc); |
| 8537 | |
| 8538 | static void JimParserInit(struct JimParserCtx *pc, const char *prg, int len, int linenr) |
| 8539 | { |
| 8540 | pc->p = prg; |
| 8541 | pc->len = len; |
| 8542 | pc->tstart = NULL; |
| 8543 | pc->tend = NULL; |
| 8544 | pc->tline = 0; |
| 8545 | pc->tt = JIM_TT_NONE; |
| 8546 | pc->eof = 0; |
| 8547 | pc->inquote = 0; |
| 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) { |
| 8559 | pc->tstart = pc->p; |
| 8560 | pc->tend = pc->p - 1; |
| 8561 | pc->tline = pc->linenr; |
| 8562 | pc->tt = JIM_TT_EOL; |
| 8563 | if (pc->inquote) { |
| 8564 | pc->missing.ch = '"'; |
| 8565 | } |
| 8566 | pc->eof = 1; |
| 8567 | return JIM_OK; |
| 8568 | } |
| 8569 | switch (*(pc->p)) { |
| 8570 | case '\\': |
| 8571 | if (*(pc->p + 1) == '\n' && !pc->inquote) { |
| 8572 | return JimParseSep(pc); |
| 8573 | } |
| 8574 | pc->comment = 0; |
| 8575 | return JimParseStr(pc); |
| 8576 | case ' ': |
| 8577 | case '\t': |
| 8578 | case '\r': |
| 8579 | case '\f': |
| 8580 | if (!pc->inquote) |
| 8581 | return JimParseSep(pc); |
| 8582 | pc->comment = 0; |
| 8583 | return JimParseStr(pc); |
| 8584 | case '\n': |
| 8585 | case ';': |
| 8586 | pc->comment = 1; |
| 8587 | if (!pc->inquote) |
| 8588 | return JimParseEol(pc); |
| 8589 | return JimParseStr(pc); |
| 8590 | case '[': |
| 8591 | pc->comment = 0; |
| 8592 | return JimParseCmd(pc); |
| 8593 | case '$': |
| 8594 | pc->comment = 0; |
| 8595 | if (JimParseVar(pc) == JIM_ERR) { |
| 8596 | |
| 8597 | pc->tstart = pc->tend = pc->p++; |
| 8598 | pc->len--; |
| 8599 | pc->tt = JIM_TT_ESC; |
| 8600 | } |
| 8601 | return JIM_OK; |
| 8602 | case '#': |
| 8603 | if (pc->comment) { |
| 8604 | JimParseComment(pc); |
| 8605 | continue; |
| 8606 | } |
| 8607 | return JimParseStr(pc); |
| 8608 | default: |
| 8609 | pc->comment = 0; |
| 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; |
| 8621 | while (isspace(UCHAR(*pc->p)) || (*pc->p == '\\' && *(pc->p + 1) == '\n')) { |
| 8622 | if (*pc->p == '\n') { |
| 8623 | break; |
| 8624 | } |
| 8625 | if (*pc->p == '\\') { |
| 8626 | pc->p++; |
| 8627 | pc->len--; |
| 8628 | pc->linenr++; |
| 8629 | } |
| 8630 | pc->p++; |
| 8631 | pc->len--; |
| 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; |
| 8643 | while (isspace(UCHAR(*pc->p)) || *pc->p == ';') { |
| 8644 | if (*pc->p == '\n') |
| 8645 | pc->linenr++; |
| 8646 | pc->p++; |
| 8647 | pc->len--; |
| 8648 | } |
| 8649 | pc->tend = pc->p - 1; |
| 8650 | pc->tt = JIM_TT_EOL; |
| 8651 | return JIM_OK; |
| 8652 | } |
| 8653 | |
| 8654 | |
| 8655 | static void JimParseSubBrace(struct JimParserCtx *pc) |
| 8656 | { |
| 8657 | int level = 1; |
| 8658 | |
| 8659 | |
| 8660 | pc->p++; |
| 8661 | pc->len--; |
| 8662 | while (pc->len) { |
| 8663 | switch (*pc->p) { |
| 8664 | case '\\': |
| 8665 | if (pc->len > 1) { |
| 8666 | if (*++pc->p == '\n') { |
| 8667 | pc->linenr++; |
| 8668 | } |
| 8669 | pc->len--; |
| 8670 | } |
| 8671 | break; |
| 8672 | |
| 8673 | case '{': |
| 8674 | level++; |
| 8675 | break; |
| 8676 | |
| 8677 | case '}': |
| 8678 | if (--level == 0) { |
| 8679 | pc->tend = pc->p - 1; |
| 8680 | pc->p++; |
| 8681 | pc->len--; |
| 8682 | return; |
| 8683 | } |
| 8684 | break; |
| 8685 | |
| 8686 | case '\n': |
| 8687 | pc->linenr++; |
| 8688 | break; |
| 8689 | } |
| 8690 | pc->p++; |
| 8691 | pc->len--; |
| 8692 | } |
| 8693 | pc->missing.ch = '{'; |
| 8694 | pc->missing.line = pc->tline; |
| 8695 | pc->tend = pc->p - 1; |
| 8696 | } |
| 8697 | |
| 8698 | static int JimParseSubQuote(struct JimParserCtx *pc) |
| 8699 | { |
| 8700 | int tt = JIM_TT_STR; |
| 8701 | int line = pc->tline; |
| 8702 | |
| 8703 | |
| 8704 | pc->p++; |
| 8705 | pc->len--; |
| 8706 | while (pc->len) { |
| 8707 | switch (*pc->p) { |
| 8708 | case '\\': |
| 8709 | if (pc->len > 1) { |
| 8710 | if (*++pc->p == '\n') { |
| 8711 | pc->linenr++; |
| 8712 | } |
| 8713 | pc->len--; |
| 8714 | tt = JIM_TT_ESC; |
| 8715 | } |
| 8716 | break; |
| 8717 | |
| 8718 | case '"': |
| 8719 | pc->tend = pc->p - 1; |
| 8720 | pc->p++; |
| 8721 | pc->len--; |
| 8722 | return tt; |
| 8723 | |
| 8724 | case '[': |
| 8725 | JimParseSubCmd(pc); |
| 8726 | tt = JIM_TT_ESC; |
| 8727 | continue; |
| 8728 | |
| 8729 | case '\n': |
| 8730 | pc->linenr++; |
| 8731 | break; |
| 8732 | |
| 8733 | case '$': |
| 8734 | tt = JIM_TT_ESC; |
| 8735 | break; |
| 8736 | } |
| 8737 | pc->p++; |
| 8738 | pc->len--; |
| 8739 | } |
| 8740 | pc->missing.ch = '"'; |
| 8741 | pc->missing.line = line; |
| 8742 | pc->tend = pc->p - 1; |
| 8743 | return tt; |
| 8744 | } |
| 8745 | |
| 8746 | static void JimParseSubCmd(struct JimParserCtx *pc) |
| 8747 | { |
| 8748 | int level = 1; |
| 8749 | int startofword = 1; |
| 8750 | int line = pc->tline; |
| 8751 | |
| 8752 | |
| 8753 | pc->p++; |
| 8754 | pc->len--; |
| 8755 | while (pc->len) { |
| 8756 | switch (*pc->p) { |
| 8757 | case '\\': |
| 8758 | if (pc->len > 1) { |
| 8759 | if (*++pc->p == '\n') { |
| 8760 | pc->linenr++; |
| 8761 | } |
| 8762 | pc->len--; |
| 8763 | } |
| 8764 | break; |
| 8765 | |
| 8766 | case '[': |
| 8767 | level++; |
| 8768 | break; |
| 8769 | |
| 8770 | case ']': |
| 8771 | if (--level == 0) { |
| 8772 | pc->tend = pc->p - 1; |
| 8773 | pc->p++; |
| 8774 | pc->len--; |
| 8775 | return; |
| 8776 | } |
| 8777 | break; |
| 8778 | |
| 8779 | case '"': |
| 8780 | if (startofword) { |
| 8781 | JimParseSubQuote(pc); |
| 8782 | if (pc->missing.ch == '"') { |
| 8783 | return; |
| 8784 | } |
| 8785 | continue; |
| 8786 | } |
| 8787 | break; |
| 8788 | |
| 8789 | case '{': |
| 8790 | JimParseSubBrace(pc); |
| 8791 | startofword = 0; |
| 8792 | continue; |
| 8793 | |
| 8794 | case '\n': |
| 8795 | pc->linenr++; |
| 8796 | break; |
| 8797 | } |
| 8798 | startofword = isspace(UCHAR(*pc->p)); |
| 8799 | pc->p++; |
| 8800 | pc->len--; |
| 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++; |
| 8841 | pc->len--; |
| 8842 | |
| 8843 | #ifdef EXPRSUGAR_BRACKET |
| 8844 | if (*pc->p == '[') { |
| 8845 | |
| 8846 | JimParseCmd(pc); |
| 8847 | pc->tt = JIM_TT_EXPRSUGAR; |
| 8848 | return JIM_OK; |
| 8849 | } |
| 8850 | #endif |
| 8851 | |
| 8852 | pc->tstart = pc->p; |
| 8853 | pc->tt = JIM_TT_VAR; |
| 8854 | pc->tline = pc->linenr; |
| 8855 | |
| 8856 | if (*pc->p == '{') { |
| 8857 | pc->tstart = ++pc->p; |
| 8858 | pc->len--; |
| 8859 | |
| 8860 | while (pc->len && *pc->p != '}') { |
| 8861 | if (*pc->p == '\n') { |
| 8862 | pc->linenr++; |
| 8863 | } |
| 8864 | pc->p++; |
| 8865 | pc->len--; |
| 8866 | } |
| 8867 | pc->tend = pc->p - 1; |
| 8868 | if (pc->len) { |
| 8869 | pc->p++; |
| 8870 | pc->len--; |
| 8871 | } |
| 8872 | } |
| 8873 | else { |
| 8874 | while (1) { |
| 8875 | |
| 8876 | if (pc->p[0] == ':' && pc->p[1] == ':') { |
| 8877 | while (*pc->p == ':') { |
| 8878 | pc->p++; |
| 8879 | pc->len--; |
| 8880 | } |
| 8881 | continue; |
| 8882 | } |
| 8883 | if (isalnum(UCHAR(*pc->p)) || *pc->p == '_' || UCHAR(*pc->p) >= 0x80) { |
| 8884 | pc->p++; |
| 8885 | pc->len--; |
| 8886 | continue; |
| 8887 | } |
| 8888 | break; |
| 8889 | } |
| 8890 | |
| 8891 | if (*pc->p == '(') { |
| 8892 | int count = 1; |
| 8893 | const char *paren = NULL; |
| 8894 | |
| 8895 | pc->tt = JIM_TT_DICTSUGAR; |
| 8896 | |
| 8897 | while (count && pc->len) { |
| 8898 | pc->p++; |
| 8899 | pc->len--; |
| 8900 | if (*pc->p == '\\' && pc->len >= 1) { |
| 8901 | pc->p++; |
| 8902 | pc->len--; |
| 8903 | } |
| 8904 | else if (*pc->p == '(') { |
| 8905 | count++; |
| 8906 | } |
| 8907 | else if (*pc->p == ')') { |
| 8908 | paren = pc->p; |
| 8909 | count--; |
| 8910 | } |
| 8911 | } |
| 8912 | if (count == 0) { |
| 8913 | pc->p++; |
| 8914 | pc->len--; |
| 8915 | } |
| 8916 | else if (paren) { |
| 8917 | |
| 8918 | paren++; |
| 8919 | pc->len += (pc->p - paren); |
| 8920 | pc->p = paren; |
| 8921 | } |
| 8922 | #ifndef EXPRSUGAR_BRACKET |
| 8923 | if (*pc->tstart == '(') { |
| 8924 | pc->tt = JIM_TT_EXPRSUGAR; |
| 8925 | } |
| 8926 | #endif |
| 8927 | } |
| 8928 | pc->tend = pc->p - 1; |
| 8929 | } |
| 8930 | if (pc->tstart == pc->p) { |
| 8931 | pc->p--; |
| 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) { |
| 8943 | |
| 8944 | if (*pc->p == '{') { |
| 8945 | return JimParseBrace(pc); |
| 8946 | } |
| 8947 | if (*pc->p == '"') { |
| 8948 | pc->inquote = 1; |
| 8949 | pc->p++; |
| 8950 | pc->len--; |
| 8951 | |
| 8952 | pc->missing.line = pc->tline; |
| 8953 | } |
| 8954 | } |
| 8955 | pc->tstart = pc->p; |
| 8956 | pc->tline = pc->linenr; |
| 8957 | while (1) { |
| 8958 | if (pc->len == 0) { |
| 8959 | if (pc->inquote) { |
| 8960 | pc->missing.ch = '"'; |
| 8961 | } |
| 8962 | pc->tend = pc->p - 1; |
| 8963 | pc->tt = JIM_TT_ESC; |
| 8964 | return JIM_OK; |
| 8965 | } |
| 8966 | switch (*pc->p) { |
| 8967 | case '\\': |
| 8968 | if (!pc->inquote && *(pc->p + 1) == '\n') { |
| 8969 | pc->tend = pc->p - 1; |
| 8970 | pc->tt = JIM_TT_ESC; |
| 8971 | return JIM_OK; |
| 8972 | } |
| 8973 | if (pc->len >= 2) { |
| 8974 | if (*(pc->p + 1) == '\n') { |
| 8975 | pc->linenr++; |
| 8976 | } |
| 8977 | pc->p++; |
| 8978 | pc->len--; |
| 8979 | } |
| 8980 | else if (pc->len == 1) { |
| 8981 | |
| 8982 | pc->missing.ch = '\\'; |
| 8983 | } |
| 8984 | break; |
| 8985 | case '(': |
| 8986 | |
| 8987 | if (pc->len > 1 && pc->p[1] != '$') { |
| 8988 | break; |
| 8989 | } |
| 8990 | |
| 8991 | case ')': |
| 8992 | |
| 8993 | if (*pc->p == '(' || pc->tt == JIM_TT_VAR) { |
| 8994 | if (pc->p == pc->tstart) { |
| 8995 | |
| 8996 | pc->p++; |
| 8997 | pc->len--; |
| 8998 | } |
| 8999 | pc->tend = pc->p - 1; |
| 9000 | pc->tt = JIM_TT_ESC; |
| 9001 | return JIM_OK; |
| 9002 | } |
| 9003 | break; |
| 9004 | |
| 9005 | case '$': |
| 9006 | case '[': |
| 9007 | pc->tend = pc->p - 1; |
| 9008 | pc->tt = JIM_TT_ESC; |
| 9009 | return JIM_OK; |
| 9010 | case ' ': |
| 9011 | case '\t': |
| 9012 | case '\n': |
| 9013 | case '\r': |
| 9014 | case '\f': |
| 9015 | case ';': |
| 9016 | if (!pc->inquote) { |
| 9017 | pc->tend = pc->p - 1; |
| 9018 | pc->tt = JIM_TT_ESC; |
| 9019 | return JIM_OK; |
| 9020 | } |
| 9021 | else if (*pc->p == '\n') { |
| 9022 | pc->linenr++; |
| 9023 | } |
| 9024 | break; |
| 9025 | case '"': |
| 9026 | if (pc->inquote) { |
| 9027 | pc->tend = pc->p - 1; |
| 9028 | pc->tt = JIM_TT_ESC; |
| 9029 | pc->p++; |
| 9030 | pc->len--; |
| 9031 | pc->inquote = 0; |
| 9032 | return JIM_OK; |
| 9033 | } |
| 9034 | break; |
| 9035 | } |
| 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 == '\\') { |
| 9047 | pc->p++; |
| 9048 | pc->len--; |
| 9049 | if (pc->len == 0) { |
| 9050 | pc->missing.ch = '\\'; |
| 9051 | return JIM_OK; |
| 9052 | } |
| 9053 | if (*pc->p == '\n') { |
| 9054 | pc->linenr++; |
| 9055 | } |
| 9056 | } |
| 9057 | else if (*pc->p == '\n') { |
| 9058 | pc->p++; |
| 9059 | pc->len--; |
| 9060 | pc->linenr++; |
| 9061 | break; |
| 9062 | } |
| 9063 | pc->p++; |
| 9064 | pc->len--; |
| 9065 | } |
| 9066 | return JIM_OK; |
| 9067 | } |
| 9068 | |
| 9069 | |
| 9070 | static int xdigitval(int c) |
| 9071 | { |
| 9072 | if (c >= '0' && c <= '9') |
| 9073 | return c - '0'; |
| 9074 | if (c >= 'a' && c <= 'f') |
| 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'; |
| 9086 | return -1; |
| 9087 | } |
| 9088 | |
| 9089 | static int JimEscape(char *dest, const char *s, int slen) |
| 9090 | { |
| 9091 | char *p = dest; |
| 9092 | int i, len; |
| 9093 | |
| 9094 | for (i = 0; i < slen; i++) { |
| 9095 | switch (s[i]) { |
| 9096 | case '\\': |
| 9097 | switch (s[i + 1]) { |
| 9098 | case 'a': |
| 9099 | *p++ = 0x7; |
| 9100 | i++; |
| 9101 | break; |
| 9102 | case 'b': |
| 9103 | *p++ = 0x8; |
| 9104 | i++; |
| 9105 | break; |
| 9106 | case 'f': |
| 9107 | *p++ = 0xc; |
| 9108 | i++; |
| 9109 | break; |
| 9110 | case 'n': |
| 9111 | *p++ = 0xa; |
| 9112 | i++; |
| 9113 | break; |
| 9114 | case 'r': |
| 9115 | *p++ = 0xd; |
| 9116 | i++; |
| 9117 | break; |
| 9118 | case 't': |
| 9119 | *p++ = 0x9; |
| 9120 | i++; |
| 9121 | break; |
| 9122 | case 'u': |
| 9123 | case 'U': |
| 9124 | case 'x': |
| 9125 | { |
| 9126 | unsigned val = 0; |
| 9127 | int k; |
| 9128 | int maxchars = 2; |
| 9129 | |
| 9130 | i++; |
| 9131 | |
| 9132 | if (s[i] == 'U') { |
| 9133 | maxchars = 8; |
| 9134 | } |
| 9135 | else if (s[i] == 'u') { |
| 9136 | if (s[i + 1] == '{') { |
| 9137 | maxchars = 6; |
| 9138 | i++; |
| 9139 | } |
| 9140 | else { |
| 9141 | maxchars = 4; |
| 9142 | } |
| 9143 | } |
| 9144 | |
| 9145 | for (k = 0; k < maxchars; k++) { |
| 9146 | int c = xdigitval(s[i + k + 1]); |
| 9147 | if (c == -1) { |
| 9148 | break; |
| 9149 | } |
| 9150 | val = (val << 4) | c; |
| 9151 | } |
| 9152 | |
| 9153 | if (s[i] == '{') { |
| 9154 | if (k == 0 || val > 0x1fffff || s[i + k + 1] != '}') { |
| 9155 | |
| 9156 | i--; |
| 9157 | k = 0; |
| 9158 | } |
| 9159 | else { |
| 9160 | |
| 9161 | k++; |
| 9162 | } |
| 9163 | } |
| 9164 | if (k) { |
| 9165 | |
| 9166 | if (s[i] == 'x') { |
| 9167 | *p++ = val; |
| 9168 | } |
| 9169 | else { |
| 9170 | p += utf8_fromunicode(p, val); |
| 9171 | } |
| 9172 | i += k; |
| 9173 | break; |
| 9174 | } |
| 9175 | |
| 9176 | *p++ = s[i]; |
| 9177 | } |
| 9178 | break; |
| 9179 | case 'v': |
| 9180 | *p++ = 0xb; |
| 9181 | i++; |
| 9182 | break; |
| 9183 | case '\0': |
| 9184 | *p++ = '\\'; |
| 9185 | i++; |
| 9186 | break; |
| 9187 | case '\n': |
| 9188 | |
| 9189 | *p++ = ' '; |
| 9190 | do { |
| 9191 | i++; |
| 9192 | } while (s[i + 1] == ' ' || s[i + 1] == '\t'); |
| 9193 | break; |
| 9194 | case '0': |
| 9195 | case '1': |
| 9196 | case '2': |
| 9197 | case '3': |
| 9198 | case '4': |
| 9199 | case '5': |
| 9200 | case '6': |
| 9201 | case '7': |
| 9202 | |
| 9203 | { |
| 9204 | int val = 0; |
| 9205 | int c = odigitval(s[i + 1]); |
| 9206 | |
| 9207 | val = c; |
| 9208 | c = odigitval(s[i + 2]); |
| 9209 | if (c == -1) { |
| 9210 | *p++ = val; |
| 9211 | i++; |
| 9212 | break; |
| 9213 | } |
| 9214 | val = (val * 8) + c; |
| 9215 | c = odigitval(s[i + 3]); |
| 9216 | if (c == -1) { |
| 9217 | *p++ = val; |
| 9218 | i += 2; |
| 9219 | break; |
| 9220 | } |
| 9221 | val = (val * 8) + c; |
| 9222 | *p++ = val; |
| 9223 | i += 3; |
| 9224 | } |
| 9225 | break; |
| 9226 | default: |
| 9227 | *p++ = s[i + 1]; |
| 9228 | i++; |
| 9229 | break; |
| 9230 | } |
| 9231 | break; |
| 9232 | default: |
| 9233 | *p++ = s[i]; |
| 9234 | break; |
| 9235 | } |
| 9236 | } |
| 9237 | len = p - dest; |
| 9238 | *p = '\0'; |
| 9239 | return len; |
| 9240 | } |
| 9241 | |
| 9242 | static Jim_Obj *JimParserGetTokenObj(Jim_Interp *interp, struct JimParserCtx *pc) |
| 9243 | { |
| 9244 | const char *start, *end; |
| 9245 | char *token; |
| 9246 | int len; |
| 9247 | |
| 9248 | start = pc->tstart; |
| 9249 | end = pc->tend; |
| 9250 | len = (end - start) + 1; |
| 9251 | if (len < 0) { |
| 9252 | len = 0; |
| 9253 | } |
| 9254 | token = Jim_Alloc(len + 1); |
| 9255 | if (pc->tt != JIM_TT_ESC) { |
| 9256 | |
| 9257 | memcpy(token, start, len); |
| 9258 | token[len] = '\0'; |
| 9259 | } |
| 9260 | else { |
| 9261 | |
| 9262 | len = JimEscape(token, start, len); |
| 9263 | } |
| 9264 | |
| 9265 | return Jim_NewStringObjNoAlloc(interp, token, len); |
| 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); |
| 9277 | } |
| 9278 | switch (*pc->p) { |
| 9279 | case '"': |
| 9280 | return JimParseListQuote(pc); |
| 9281 | |
| 9282 | case '{': |
| 9283 | return JimParseBrace(pc); |
| 9284 | |
| 9285 | default: |
| 9286 | if (pc->len) { |
| 9287 | return JimParseListStr(pc); |
| 9288 | } |
| 9289 | break; |
| 9290 | } |
| 9291 | |
| 9292 | pc->tstart = pc->tend = pc->p; |
| 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; |
| 9304 | while (isspace(UCHAR(*pc->p))) { |
| 9305 | if (*pc->p == '\n') { |
| 9306 | pc->linenr++; |
| 9307 | } |
| 9308 | pc->p++; |
| 9309 | pc->len--; |
| 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--; |
| 9321 | |
| 9322 | pc->tstart = pc->p; |
| 9323 | pc->tline = pc->linenr; |
| 9324 | pc->tt = JIM_TT_STR; |
| 9325 | |
| 9326 | while (pc->len) { |
| 9327 | switch (*pc->p) { |
| 9328 | case '\\': |
| 9329 | pc->tt = JIM_TT_ESC; |
| 9330 | if (--pc->len == 0) { |
| 9331 | |
| 9332 | pc->tend = pc->p; |
| 9333 | return JIM_OK; |
| 9334 | } |
| 9335 | pc->p++; |
| 9336 | break; |
| 9337 | case '\n': |
| 9338 | pc->linenr++; |
| 9339 | break; |
| 9340 | case '"': |
| 9341 | pc->tend = pc->p - 1; |
| 9342 | pc->p++; |
| 9343 | pc->len--; |
| 9344 | return JIM_OK; |
| 9345 | } |
| 9346 | pc->p++; |
| 9347 | pc->len--; |
| 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; |
| 9359 | pc->tt = JIM_TT_STR; |
| 9360 | |
| 9361 | while (pc->len) { |
| 9362 | if (isspace(UCHAR(*pc->p))) { |
| 9363 | pc->tend = pc->p - 1; |
| 9364 | return JIM_OK; |
| 9365 | } |
| 9366 | if (*pc->p == '\\') { |
| 9367 | if (--pc->len == 0) { |
| 9368 | |
| 9369 | pc->tend = pc->p; |
| 9370 | return JIM_OK; |
| 9371 | } |
| 9372 | pc->tt = JIM_TT_ESC; |
| 9373 | pc->p++; |
| 9374 | } |
| 9375 | pc->p++; |
| 9376 | pc->len--; |
| 9377 | } |
| 9378 | pc->tend = pc->p - 1; |
| 9379 | return JIM_OK; |
| 9380 | } |
| 9381 | |
| 9382 | |
| 9383 | |
| 9384 | Jim_Obj *Jim_NewObj(Jim_Interp *interp) |
| 9385 | { |
| 9386 | Jim_Obj *objPtr; |
| 9387 | |
| 9388 | |
| 9389 | if (interp->freeList != NULL) { |
| 9390 | |
| 9391 | objPtr = interp->freeList; |
| 9392 | interp->freeList = objPtr->nextObjPtr; |
| 9393 | } |
| 9394 | else { |
| 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) |
| 9406 | interp->liveList->prevObjPtr = objPtr; |
| 9407 | interp->liveList = objPtr; |
| 9408 | |
| 9409 | return objPtr; |
| 9410 | } |
| 9411 | |
| 9412 | void Jim_FreeObj(Jim_Interp *interp, Jim_Obj *objPtr) |
| 9413 | { |
| 9414 | |
| 9415 | JimPanic((objPtr->refCount != 0, "!!!Object %p freed with bad refcount %d, type=%s", objPtr, |
| 9416 | objPtr->refCount, objPtr->typePtr ? objPtr->typePtr->name : "<none>")); |
| 9417 | |
| 9418 | |
| 9419 | Jim_FreeIntRep(interp, objPtr); |
| 9420 | |
| 9421 | if (objPtr->bytes != NULL) { |
| 9422 | if (objPtr->bytes != JimEmptyStringRep) |
| 9423 | Jim_Free(objPtr->bytes); |
| 9424 | } |
| 9425 | |
| 9426 | if (objPtr->prevObjPtr) |
| 9427 | objPtr->prevObjPtr->nextObjPtr = objPtr->nextObjPtr; |
| 9428 | if (objPtr->nextObjPtr) |
| 9429 | objPtr->nextObjPtr->prevObjPtr = objPtr->prevObjPtr; |
| 9430 | if (interp->liveList == objPtr) |
| 9431 | interp->liveList = objPtr->nextObjPtr; |
| 9432 | #ifdef JIM_DISABLE_OBJECT_POOL |
| 9433 | Jim_Free(objPtr); |
| 9434 | #else |
| 9435 | |
| 9436 | objPtr->prevObjPtr = NULL; |
| 9437 | objPtr->nextObjPtr = interp->freeList; |
| 9438 | if (interp->freeList) |
| 9439 | interp->freeList->prevObjPtr = objPtr; |
| 9440 | interp->freeList = objPtr; |
| 9441 | objPtr->refCount = -1; |
| 9442 | #endif |
| 9443 | } |
| 9444 | |
| 9445 | |
| 9446 | void Jim_InvalidateStringRep(Jim_Obj *objPtr) |
| 9447 | { |
| 9448 | if (objPtr->bytes != NULL) { |
| 9449 | if (objPtr->bytes != JimEmptyStringRep) |
| 9450 | Jim_Free(objPtr->bytes); |
| 9451 | } |
| 9452 | objPtr->bytes = NULL; |
| 9453 | } |
| 9454 | |
| 9455 | |
| 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) { |
| 9468 | dupPtr->bytes = JimEmptyStringRep; |
| 9469 | dupPtr->length = 0; |
| 9470 | dupPtr->typePtr = NULL; |
| 9471 | return dupPtr; |
| 9472 | } |
| 9473 | else { |
| 9474 | dupPtr->bytes = Jim_Alloc(objPtr->length + 1); |
| 9475 | dupPtr->length = objPtr->length; |
| 9476 | |
| 9477 | memcpy(dupPtr->bytes, objPtr->bytes, objPtr->length + 1); |
| 9478 | } |
| 9479 | |
| 9480 | |
| 9481 | dupPtr->typePtr = objPtr->typePtr; |
| 9482 | if (objPtr->typePtr != NULL) { |
| 9483 | if (objPtr->typePtr->dupIntRepProc == NULL) { |
| 9484 | dupPtr->internalRep = objPtr->internalRep; |
| 9485 | } |
| 9486 | else { |
| 9487 | |
| 9488 | objPtr->typePtr->dupIntRepProc(interp, objPtr, dupPtr); |
| 9489 | } |
| 9490 | } |
| 9491 | return dupPtr; |
| 9492 | } |
| 9493 | |
| 9494 | const char *Jim_GetString(Jim_Obj *objPtr, int *lenPtr) |
| 9495 | { |
| 9496 | if (objPtr->bytes == NULL) { |
| 9497 | |
| 9498 | JimPanic((objPtr->typePtr->updateStringProc == NULL, "UpdateStringProc called against '%s' type.", objPtr->typePtr->name)); |
| 9499 | objPtr->typePtr->updateStringProc(objPtr); |
| 9500 | } |
| 9501 | if (lenPtr) |
| 9502 | *lenPtr = objPtr->length; |
| 9503 | return objPtr->bytes; |
| 9504 | } |
| 9505 | |
| 9506 | |
| 9507 | int Jim_Length(Jim_Obj *objPtr) |
| 9508 | { |
| 9509 | if (objPtr->bytes == NULL) { |
| 9510 | |
| 9511 | Jim_GetString(objPtr, NULL); |
| 9512 | } |
| 9513 | return objPtr->length; |
| 9514 | } |
| 9515 | |
| 9516 | |
| 9517 | const char *Jim_String(Jim_Obj *objPtr) |
| 9518 | { |
| 9519 | if (objPtr->bytes == NULL) { |
| 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); |
| 9531 | } |
| 9532 | |
| 9533 | static void FreeDictSubstInternalRep(Jim_Interp *interp, Jim_Obj *objPtr); |
| 9534 | static void DupDictSubstInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr); |
| 9535 | |
| 9536 | static const Jim_ObjType dictSubstObjType = { |
| 9537 | "dict-substitution", |
| 9538 | FreeDictSubstInternalRep, |
| 9539 | DupDictSubstInternalRep, |
| 9540 | NULL, |
| 9541 | JIM_TYPE_NONE, |
| 9542 | }; |
| 9543 | |
| 9544 | static void FreeInterpolatedInternalRep(Jim_Interp *interp, Jim_Obj *objPtr); |
| 9545 | static void DupInterpolatedInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr); |
| 9546 | |
| 9547 | static const Jim_ObjType interpolatedObjType = { |
| 9548 | "interpolated", |
| 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; |
| 9566 | |
| 9567 | Jim_IncrRefCount(dupPtr->internalRep.dictSubstValue.indexObjPtr); |
| 9568 | } |
| 9569 | |
| 9570 | static void DupStringInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr); |
| 9571 | static int SetStringFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr); |
| 9572 | |
| 9573 | static const Jim_ObjType stringObjType = { |
| 9574 | "string", |
| 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 | |
| 9595 | if (objPtr->bytes == NULL) { |
| 9596 | |
| 9597 | JimPanic((objPtr->typePtr->updateStringProc == NULL, "UpdateStringProc called against '%s' type.", objPtr->typePtr->name)); |
| 9598 | objPtr->typePtr->updateStringProc(objPtr); |
| 9599 | } |
| 9600 | |
| 9601 | Jim_FreeIntRep(interp, objPtr); |
| 9602 | |
| 9603 | objPtr->typePtr = &stringObjType; |
| 9604 | objPtr->internalRep.strValue.maxLength = objPtr->length; |
| 9605 | |
| 9606 | objPtr->internalRep.strValue.charLength = -1; |
| 9607 | } |
| 9608 | return JIM_OK; |
| 9609 | } |
| 9610 | |
| 9611 | int Jim_Utf8Length(Jim_Interp *interp, Jim_Obj *objPtr) |
| 9612 | { |
| 9613 | #ifdef JIM_UTF8 |
| 9614 | SetStringFromAny(interp, objPtr); |
| 9615 | |
| 9616 | if (objPtr->internalRep.strValue.charLength < 0) { |
| 9617 | objPtr->internalRep.strValue.charLength = utf8_strlen(objPtr->bytes, objPtr->length); |
| 9618 | } |
| 9619 | return objPtr->internalRep.strValue.charLength; |
| 9620 | #else |
| 9621 | return Jim_Length(objPtr); |
| 9622 | #endif |
| 9623 | } |
| 9624 | |
| 9625 | |
| 9626 | Jim_Obj *Jim_NewStringObj(Jim_Interp *interp, const char *s, int len) |
| 9627 | { |
| 9628 | Jim_Obj *objPtr = Jim_NewObj(interp); |
| 9629 | |
| 9630 | |
| 9631 | if (len == -1) |
| 9632 | len = strlen(s); |
| 9633 | |
| 9634 | if (len == 0) { |
| 9635 | objPtr->bytes = JimEmptyStringRep; |
| 9636 | } |
| 9637 | else { |
| 9638 | objPtr->bytes = Jim_StrDupLen(s, len); |
| 9639 | } |
| 9640 | objPtr->length = len; |
| 9641 | |
| 9642 | |
| 9643 | objPtr->typePtr = NULL; |
| 9644 | return objPtr; |
| 9645 | } |
| 9646 | |
| 9647 | |
| 9648 | Jim_Obj *Jim_NewStringObjUtf8(Jim_Interp *interp, const char *s, int charlen) |
| 9649 | { |
| 9650 | #ifdef JIM_UTF8 |
| 9651 | |
| 9652 | int bytelen = utf8_index(s, charlen); |
| 9653 | |
| 9654 | Jim_Obj *objPtr = Jim_NewStringObj(interp, s, bytelen); |
| 9655 | |
| 9656 | |
| 9657 | objPtr->typePtr = &stringObjType; |
| 9658 | objPtr->internalRep.strValue.maxLength = bytelen; |
| 9659 | objPtr->internalRep.strValue.charLength = charlen; |
| 9660 | |
| 9661 | return objPtr; |
| 9662 | #else |
| 9663 | return Jim_NewStringObj(interp, s, charlen); |
| 9664 | #endif |
| 9665 | } |
| 9666 | |
| 9667 | Jim_Obj *Jim_NewStringObjNoAlloc(Jim_Interp *interp, char *s, int len) |
| 9668 | { |
| 9669 | Jim_Obj *objPtr = Jim_NewObj(interp); |
| 9670 | |
| 9671 | objPtr->bytes = s; |
| 9672 | objPtr->length = (len == -1) ? strlen(s) : len; |
| 9673 | objPtr->typePtr = NULL; |
| 9674 | return objPtr; |
| 9675 | } |
| 9676 | |
| 9677 | static void StringAppendString(Jim_Obj *objPtr, const char *str, int len) |
| 9678 | { |
| 9679 | int needlen; |
| 9680 | |
| 9681 | if (len == -1) |
| 9682 | len = strlen(str); |
| 9683 | needlen = objPtr->length + len; |
| 9684 | if (objPtr->internalRep.strValue.maxLength < needlen || |
| 9685 | objPtr->internalRep.strValue.maxLength == 0) { |
| 9686 | needlen *= 2; |
| 9687 | |
| 9688 | if (needlen < 7) { |
| 9689 | needlen = 7; |
| 9690 | } |
| 9691 | if (objPtr->bytes == JimEmptyStringRep) { |
| 9692 | objPtr->bytes = Jim_Alloc(needlen + 1); |
| 9693 | } |
| 9694 | else { |
| 9695 | objPtr->bytes = Jim_Realloc(objPtr->bytes, needlen + 1); |
| 9696 | } |
| 9697 | objPtr->internalRep.strValue.maxLength = needlen; |
| 9698 | } |
| 9699 | memcpy(objPtr->bytes + objPtr->length, str, len); |
| 9700 | objPtr->bytes[objPtr->length + len] = '\0'; |
| 9701 | |
| 9702 | if (objPtr->internalRep.strValue.charLength >= 0) { |
| 9703 | |
| 9704 | objPtr->internalRep.strValue.charLength += utf8_strlen(objPtr->bytes + objPtr->length, len); |
| 9705 | } |
| 9706 | objPtr->length += len; |
| 9707 | } |
| 9708 | |
| 9709 | void Jim_AppendString(Jim_Interp *interp, Jim_Obj *objPtr, const char *str, int len) |
| 9710 | { |
| 9711 | JimPanic((Jim_IsShared(objPtr), "Jim_AppendString called with shared object")); |
| 9712 | SetStringFromAny(interp, objPtr); |
| 9713 | StringAppendString(objPtr, str, len); |
| 9714 | } |
| 9715 | |
| 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; |
| 9727 | |
| 9728 | SetStringFromAny(interp, objPtr); |
| 9729 | va_start(ap, objPtr); |
| 9730 | while (1) { |
| 9731 | const char *s = va_arg(ap, const char *); |
| 9732 | |
| 9733 | if (s == NULL) |
| 9734 | break; |
| 9735 | Jim_AppendString(interp, objPtr, s, -1); |
| 9736 | } |
| 9737 | va_end(ap); |
| 9738 | } |
| 9739 | |
| 9740 | int Jim_StringEqObj(Jim_Obj *aObjPtr, Jim_Obj *bObjPtr) |
| 9741 | { |
| 9742 | if (aObjPtr == bObjPtr) { |
| 9743 | return 1; |
| 9744 | } |
| 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 | { |
| 9756 | int plen, slen; |
| 9757 | const char *pattern = Jim_GetString(patternObjPtr, &plen); |
| 9758 | const char *string = Jim_GetString(objPtr, &slen); |
| 9759 | return JimGlobMatch(pattern, plen, string, slen, nocase); |
| 9760 | } |
| 9761 | |
| 9762 | int Jim_StringCompareObj(Jim_Interp *interp, Jim_Obj *firstObjPtr, Jim_Obj *secondObjPtr, int nocase) |
| 9763 | { |
| 9764 | const char *s1 = Jim_String(firstObjPtr); |
| 9765 | int l1 = Jim_Utf8Length(interp, firstObjPtr); |
| 9766 | const char *s2 = Jim_String(secondObjPtr); |
| 9767 | int l2 = Jim_Utf8Length(interp, secondObjPtr); |
| 9768 | return JimStringCompareUtf8(s1, l1, s2, l2, nocase); |
| 9769 | } |
| 9770 | |
| 9771 | static int JimRelToAbsIndex(int len, int idx) |
| 9772 | { |
| 9773 | if (idx < 0 && idx > -INT_MAX) |
| 9774 | return len + idx; |
| 9775 | return idx; |
| 9776 | } |
| 9777 | |
| 9778 | static void JimRelToAbsRange(int len, int *firstPtr, int *lastPtr, int *rangeLenPtr) |
| 9779 | { |
| 9780 | int rangeLen; |
| 9781 | |
| 9782 | if (*firstPtr > *lastPtr) { |
| 9783 | rangeLen = 0; |
| 9784 | } |
| 9785 | else { |
| 9786 | rangeLen = *lastPtr - *firstPtr + 1; |
| 9787 | if (rangeLen) { |
| 9788 | if (*firstPtr < 0) { |
| 9789 | rangeLen += *firstPtr; |
| 9790 | *firstPtr = 0; |
| 9791 | } |
| 9792 | if (*lastPtr >= len) { |
| 9793 | rangeLen -= (*lastPtr - (len - 1)); |
| 9794 | *lastPtr = len - 1; |
| 9795 | } |
| 9796 | } |
| 9797 | } |
| 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) { |
| 9809 | return JIM_ERR; |
| 9810 | } |
| 9811 | if (Jim_GetIndex(interp, lastObjPtr, last) != JIM_OK) { |
| 9812 | return JIM_ERR; |
| 9813 | } |
| 9814 | *first = JimRelToAbsIndex(len, *first); |
| 9815 | *last = JimRelToAbsIndex(len, *last); |
| 9816 | JimRelToAbsRange(len, first, last, range); |
| 9817 | return JIM_OK; |
| 9818 | } |
| 9819 | |
| 9820 | Jim_Obj *Jim_StringByteRangeObj(Jim_Interp *interp, |
| 9821 | Jim_Obj *strObjPtr, Jim_Obj *firstObjPtr, Jim_Obj *lastObjPtr) |
| 9822 | { |
| 9823 | int first, last; |
| 9824 | const char *str; |
| 9825 | int rangeLen; |
| 9826 | int bytelen; |
| 9827 | |
| 9828 | str = Jim_GetString(strObjPtr, &bytelen); |
| 9829 | |
| 9830 | if (JimStringGetRange(interp, firstObjPtr, lastObjPtr, bytelen, &first, &last, &rangeLen) != JIM_OK) { |
| 9831 | return NULL; |
| 9832 | } |
| 9833 | |
| 9834 | if (first == 0 && rangeLen == bytelen) { |
| 9835 | return strObjPtr; |
| 9836 | } |
| 9837 | return Jim_NewStringObj(interp, str + first, rangeLen); |
| 9838 | } |
| 9839 | |
| 9840 | Jim_Obj *Jim_StringRangeObj(Jim_Interp *interp, |
| 9841 | Jim_Obj *strObjPtr, Jim_Obj *firstObjPtr, Jim_Obj *lastObjPtr) |
| 9842 | { |
| 9843 | #ifdef JIM_UTF8 |
| 9844 | int first, last; |
| 9845 | const char *str; |
| 9846 | int len, rangeLen; |
| 9847 | int bytelen; |
| 9848 | |
| 9849 | str = Jim_GetString(strObjPtr, &bytelen); |
| 9850 | len = Jim_Utf8Length(interp, strObjPtr); |
| 9851 | |
| 9852 | if (JimStringGetRange(interp, firstObjPtr, lastObjPtr, len, &first, &last, &rangeLen) != JIM_OK) { |
| 9853 | return NULL; |
| 9854 | } |
| 9855 | |
| 9856 | if (first == 0 && rangeLen == len) { |
| 9857 | return strObjPtr; |
| 9858 | } |
| 9859 | if (len == bytelen) { |
| 9860 | |
| 9861 | return Jim_NewStringObj(interp, str + first, rangeLen); |
| 9862 | } |
| 9863 | return Jim_NewStringObjUtf8(interp, str + utf8_index(str, first), rangeLen); |
| 9864 | #else |
| 9865 | return Jim_StringByteRangeObj(interp, strObjPtr, firstObjPtr, lastObjPtr); |
| 9866 | #endif |
| 9867 | } |
| 9868 | |
| 9869 | Jim_Obj *JimStringReplaceObj(Jim_Interp *interp, |
| 9870 | Jim_Obj *strObjPtr, Jim_Obj *firstObjPtr, Jim_Obj *lastObjPtr, Jim_Obj *newStrObj) |
| 9871 | { |
| 9872 | int first, last; |
| 9873 | const char *str; |
| 9874 | int len, rangeLen; |
| 9875 | Jim_Obj *objPtr; |
| 9876 | |
| 9877 | len = Jim_Utf8Length(interp, strObjPtr); |
| 9878 | |
| 9879 | if (JimStringGetRange(interp, firstObjPtr, lastObjPtr, len, &first, &last, &rangeLen) != JIM_OK) { |
| 9880 | return NULL; |
| 9881 | } |
| 9882 | |
| 9883 | if (last < first) { |
| 9884 | return strObjPtr; |
| 9885 | } |
| 9886 | |
| 9887 | str = Jim_String(strObjPtr); |
| 9888 | |
| 9889 | |
| 9890 | objPtr = Jim_NewStringObjUtf8(interp, str, first); |
| 9891 | |
| 9892 | |
| 9893 | if (newStrObj) { |
| 9894 | Jim_AppendObj(interp, objPtr, newStrObj); |
| 9895 | } |
| 9896 | |
| 9897 | |
| 9898 | Jim_AppendString(interp, objPtr, str + utf8_index(str, last + 1), len - last - 1); |
| 9899 | |
| 9900 | return objPtr; |
| 9901 | } |
| 9902 | |
| 9903 | static void JimStrCopyUpperLower(char *dest, const char *str, int uc) |
| 9904 | { |
| 9905 | while (*str) { |
| 9906 | int c; |
| 9907 | str += utf8_tounicode(str, &c); |
| 9908 | dest += utf8_getchars(dest, uc ? utf8_upper(c) : utf8_lower(c)); |
| 9909 | } |
| 9910 | *dest = 0; |
| 9911 | } |
| 9912 | |
| 9913 | static Jim_Obj *JimStringToLower(Jim_Interp *interp, Jim_Obj *strObjPtr) |
| 9914 | { |
| 9915 | char *buf; |
| 9916 | int len; |
| 9917 | const char *str; |
| 9918 | |
| 9919 | str = Jim_GetString(strObjPtr, &len); |
| 9920 | |
| 9921 | #ifdef JIM_UTF8 |
| 9922 | len *= 2; |
| 9923 | #endif |
| 9924 | buf = Jim_Alloc(len + 1); |
| 9925 | JimStrCopyUpperLower(buf, str, 0); |
| 9926 | return Jim_NewStringObjNoAlloc(interp, buf, -1); |
| 9927 | } |
| 9928 | |
| 9929 | static Jim_Obj *JimStringToUpper(Jim_Interp *interp, Jim_Obj *strObjPtr) |
| 9930 | { |
| 9931 | char *buf; |
| 9932 | const char *str; |
| 9933 | int len; |
| 9934 | |
| 9935 | str = Jim_GetString(strObjPtr, &len); |
| 9936 | |
| 9937 | #ifdef JIM_UTF8 |
| 9938 | len *= 2; |
| 9939 | #endif |
| 9940 | buf = Jim_Alloc(len + 1); |
| 9941 | JimStrCopyUpperLower(buf, str, 1); |
| 9942 | return Jim_NewStringObjNoAlloc(interp, buf, -1); |
| 9943 | } |
| 9944 | |
| 9945 | static Jim_Obj *JimStringToTitle(Jim_Interp *interp, Jim_Obj *strObjPtr) |
| 9946 | { |
| 9947 | char *buf, *p; |
| 9948 | int len; |
| 9949 | int c; |
| 9950 | const char *str; |
| 9951 | |
| 9952 | str = Jim_GetString(strObjPtr, &len); |
| 9953 | |
| 9954 | #ifdef JIM_UTF8 |
| 9955 | len *= 2; |
| 9956 | #endif |
| 9957 | buf = p = Jim_Alloc(len + 1); |
| 9958 | |
| 9959 | str += utf8_tounicode(str, &c); |
| 9960 | p += utf8_getchars(p, utf8_title(c)); |
| 9961 | |
| 9962 | JimStrCopyUpperLower(p, str, 0); |
| 9963 | |
| 9964 | return Jim_NewStringObjNoAlloc(interp, buf, -1); |
| 9965 | } |
| 9966 | |
| 9967 | static const char *utf8_memchr(const char *str, int len, int c) |
| 9968 | { |
| 9969 | #ifdef JIM_UTF8 |
| 9970 | while (len) { |
| 9971 | int sc; |
| 9972 | int n = utf8_tounicode(str, &sc); |
| 9973 | if (sc == c) { |
| 9974 | return str; |
| 9975 | } |
| 9976 | str += n; |
| 9977 | len -= n; |
| 9978 | } |
| 9979 | return NULL; |
| 9980 | #else |
| 9981 | return memchr(str, c, len); |
| 9982 | #endif |
| 9983 | } |
| 9984 | |
| 9985 | static const char *JimFindTrimLeft(const char *str, int len, const char *trimchars, int trimlen) |
| 9986 | { |
| 9987 | while (len) { |
| 9988 | int c; |
| 9989 | int n = utf8_tounicode(str, &c); |
| 9990 | |
| 9991 | if (utf8_memchr(trimchars, trimlen, c) == NULL) { |
| 9992 | |
| 9993 | break; |
| 9994 | } |
| 9995 | str += n; |
| 9996 | len -= n; |
| 9997 | } |
| 9998 | return str; |
| 9999 | } |
| 10000 | |
| 10001 | static const char *JimFindTrimRight(const char *str, int len, const char *trimchars, int trimlen) |
| 10002 | { |
| 10003 | str += len; |
| 10004 | |
| 10005 | while (len) { |
| 10006 | int c; |
| 10007 | int n = utf8_prev_len(str, len); |
| 10008 | |
| 10009 | len -= n; |
| 10010 | str -= n; |
| 10011 | |
| 10012 | n = utf8_tounicode(str, &c); |
| 10013 | |
| 10014 | if (utf8_memchr(trimchars, trimlen, c) == NULL) { |
| 10015 | return str + n; |
| 10016 | } |
| 10017 | } |
| 10018 | |
| 10019 | return NULL; |
| 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); |
| 10031 | const char *trimchars = default_trim_chars; |
| 10032 | int trimcharslen = default_trim_chars_len; |
| 10033 | const char *newstr; |
| 10034 | |
| 10035 | if (trimcharsObjPtr) { |
| 10036 | trimchars = Jim_GetString(trimcharsObjPtr, &trimcharslen); |
| 10037 | } |
| 10038 | |
| 10039 | newstr = JimFindTrimLeft(str, len, trimchars, trimcharslen); |
| 10040 | if (newstr == str) { |
| 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; |
| 10052 | int trimcharslen = default_trim_chars_len; |
| 10053 | const char *nontrim; |
| 10054 | |
| 10055 | if (trimcharsObjPtr) { |
| 10056 | trimchars = Jim_GetString(trimcharsObjPtr, &trimcharslen); |
| 10057 | } |
| 10058 | |
| 10059 | SetStringFromAny(interp, strObjPtr); |
| 10060 | |
| 10061 | len = Jim_Length(strObjPtr); |
| 10062 | nontrim = JimFindTrimRight(strObjPtr->bytes, len, trimchars, trimcharslen); |
| 10063 | |
| 10064 | if (nontrim == NULL) { |
| 10065 | |
| 10066 | return Jim_NewEmptyStringObj(interp); |
| 10067 | } |
| 10068 | if (nontrim == strObjPtr->bytes + len) { |
| 10069 | |
| 10070 | return strObjPtr; |
| 10071 | } |
| 10072 | |
| 10073 | if (Jim_IsShared(strObjPtr)) { |
| 10074 | strObjPtr = Jim_NewStringObj(interp, strObjPtr->bytes, (nontrim - strObjPtr->bytes)); |
| 10075 | } |
| 10076 | else { |
| 10077 | |
| 10078 | strObjPtr->bytes[nontrim - strObjPtr->bytes] = 0; |
| 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); |
| 10090 | |
| 10091 | |
| 10092 | strObjPtr = JimStringTrimRight(interp, objPtr, trimcharsObjPtr); |
| 10093 | |
| 10094 | |
| 10095 | if (objPtr != strObjPtr && objPtr->refCount == 0) { |
| 10096 | |
| 10097 | Jim_FreeNewObj(interp, objPtr); |
| 10098 | } |
| 10099 | |
| 10100 | return strObjPtr; |
| 10101 | } |
| 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", |
| 10119 | "double", "lower", "upper", "space", "xdigit", |
| 10120 | "control", "print", "graph", "punct", "boolean", |
| 10121 | NULL |
| 10122 | }; |
| 10123 | enum { |
| 10124 | STR_IS_INTEGER, STR_IS_ALPHA, STR_IS_ALNUM, STR_IS_ASCII, STR_IS_DIGIT, |
| 10125 | STR_IS_DOUBLE, STR_IS_LOWER, STR_IS_UPPER, STR_IS_SPACE, STR_IS_XDIGIT, |
| 10126 | STR_IS_CONTROL, STR_IS_PRINT, STR_IS_GRAPH, STR_IS_PUNCT, STR_IS_BOOLEAN, |
| 10127 | }; |
| 10128 | int strclass; |
| 10129 | int len; |
| 10130 | int i; |
| 10131 | const char *str; |
| 10132 | int (*isclassfunc)(int c) = NULL; |
| 10133 | |
| 10134 | if (Jim_GetEnum(interp, strClass, strclassnames, &strclass, "class", JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) { |
| 10135 | return JIM_ERR; |
| 10136 | } |
| 10137 | |
| 10138 | str = Jim_GetString(strObjPtr, &len); |
| 10139 | if (len == 0) { |
| 10140 | Jim_SetResultBool(interp, !strict); |
| 10141 | return JIM_OK; |
| 10142 | } |
| 10143 | |
| 10144 | switch (strclass) { |
| 10145 | case STR_IS_INTEGER: |
| 10146 | { |
| 10147 | jim_wide w; |
| 10148 | Jim_SetResultBool(interp, JimGetWideNoErr(interp, strObjPtr, &w) == JIM_OK); |
| 10149 | return JIM_OK; |
| 10150 | } |
| 10151 | |
| 10152 | case STR_IS_DOUBLE: |
| 10153 | { |
| 10154 | double d; |
| 10155 | Jim_SetResultBool(interp, Jim_GetDouble(interp, strObjPtr, &d) == JIM_OK && errno != ERANGE); |
| 10156 | return JIM_OK; |
| 10157 | } |
| 10158 | |
| 10159 | case STR_IS_BOOLEAN: |
| 10160 | { |
| 10161 | int b; |
| 10162 | Jim_SetResultBool(interp, Jim_GetBoolean(interp, strObjPtr, &b) == JIM_OK); |
| 10163 | return JIM_OK; |
| 10164 | } |
| 10165 | |
| 10166 | case STR_IS_ALPHA: isclassfunc = isalpha; break; |
| 10167 | case STR_IS_ALNUM: isclassfunc = isalnum; break; |
| 10168 | case STR_IS_ASCII: isclassfunc = jim_isascii; break; |
| 10169 | case STR_IS_DIGIT: isclassfunc = isdigit; break; |
| 10170 | case STR_IS_LOWER: isclassfunc = islower; break; |
| 10171 | case STR_IS_UPPER: isclassfunc = isupper; break; |
| 10172 | case STR_IS_SPACE: isclassfunc = isspace; break; |
| 10173 | case STR_IS_XDIGIT: isclassfunc = isxdigit; break; |
| 10174 | case STR_IS_CONTROL: isclassfunc = iscntrl; break; |
| 10175 | case STR_IS_PRINT: isclassfunc = isprint; break; |
| 10176 | case STR_IS_GRAPH: isclassfunc = isgraph; break; |
| 10177 | case STR_IS_PUNCT: isclassfunc = ispunct; break; |
| 10178 | default: |
| 10179 | return JIM_ERR; |
| 10180 | } |
| 10181 | |
| 10182 | for (i = 0; i < len; i++) { |
| 10183 | if (!isclassfunc(UCHAR(str[i]))) { |
| 10184 | Jim_SetResultBool(interp, 0); |
| 10185 | return JIM_OK; |
| 10186 | } |
| 10187 | } |
| 10188 | Jim_SetResultBool(interp, 1); |
| 10189 | return JIM_OK; |
| 10190 | } |
| 10191 | |
| 10192 | |
| 10193 | |
| 10194 | static const Jim_ObjType comparedStringObjType = { |
| 10195 | "compared-string", |
| 10196 | NULL, |
| 10197 | NULL, |
| 10198 | NULL, |
| 10199 | JIM_TYPE_REFERENCES, |
| 10200 | }; |
| 10201 | |
| 10202 | int Jim_CompareStringImmediate(Jim_Interp *interp, Jim_Obj *objPtr, const char *str) |
| 10203 | { |
| 10204 | if (objPtr->typePtr == &comparedStringObjType && objPtr->internalRep.ptr == str) { |
| 10205 | return 1; |
| 10206 | } |
| 10207 | else { |
| 10208 | if (strcmp(str, Jim_String(objPtr)) != 0) |
| 10209 | return 0; |
| 10210 | |
| 10211 | if (objPtr->typePtr != &comparedStringObjType) { |
| 10212 | Jim_FreeIntRep(interp, objPtr); |
| 10213 | objPtr->typePtr = &comparedStringObjType; |
| 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 | |
| 10241 | static void FreeSourceInternalRep(Jim_Interp *interp, Jim_Obj *objPtr); |
| 10242 | static void DupSourceInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr); |
| 10243 | |
| 10244 | static const Jim_ObjType sourceObjType = { |
| 10245 | "source", |
| 10246 | FreeSourceInternalRep, |
| 10247 | DupSourceInternalRep, |
| 10248 | NULL, |
| 10249 | JIM_TYPE_REFERENCES, |
| 10250 | }; |
| 10251 | |
| 10252 | void FreeSourceInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) |
| 10253 | { |
| 10254 | Jim_DecrRefCount(interp, objPtr->internalRep.sourceValue.fileNameObj); |
| 10255 | } |
| 10256 | |
| 10257 | void DupSourceInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) |
| 10258 | { |
| 10259 | dupPtr->internalRep.sourceValue = srcPtr->internalRep.sourceValue; |
| 10260 | Jim_IncrRefCount(dupPtr->internalRep.sourceValue.fileNameObj); |
| 10261 | } |
| 10262 | |
| 10263 | static const Jim_ObjType scriptLineObjType = { |
| 10264 | "scriptline", |
| 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 | |
| 10276 | #ifdef DEBUG_SHOW_SCRIPT |
| 10277 | char buf[100]; |
| 10278 | snprintf(buf, sizeof(buf), "line=%d, argc=%d", line, argc); |
| 10279 | objPtr = Jim_NewStringObj(interp, buf, -1); |
| 10280 | #else |
| 10281 | objPtr = Jim_NewEmptyStringObj(interp); |
| 10282 | #endif |
| 10283 | objPtr->typePtr = &scriptLineObjType; |
| 10284 | objPtr->internalRep.scriptLineValue.argc = argc; |
| 10285 | objPtr->internalRep.scriptLineValue.line = line; |
| 10286 | |
| 10287 | return objPtr; |
| 10288 | } |
| 10289 | |
| 10290 | static void FreeScriptInternalRep(Jim_Interp *interp, Jim_Obj *objPtr); |
| 10291 | static void DupScriptInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr); |
| 10292 | |
| 10293 | static const Jim_ObjType scriptObjType = { |
| 10294 | "script", |
| 10295 | FreeScriptInternalRep, |
| 10296 | DupScriptInternalRep, |
| 10297 | NULL, |
| 10298 | JIM_TYPE_NONE, |
| 10299 | }; |
| 10300 | |
| 10301 | typedef struct ScriptToken |
| 10302 | { |
| 10303 | Jim_Obj *objPtr; |
| 10304 | int type; |
| 10305 | } ScriptToken; |
| 10306 | |
| 10307 | typedef struct ScriptObj |
| 10308 | { |
| 10309 | ScriptToken *token; |
| 10310 | Jim_Obj *fileNameObj; |
| 10311 | int len; |
| 10312 | int substFlags; |
| 10313 | int inUse; /* Used to share a ScriptObj. Currently |
| 10314 | only used by Jim_EvalObj() as protection against |
| 10315 | shimmering of the currently evaluated object. */ |
| 10316 | int firstline; |
| 10317 | int linenr; |
| 10318 | int missing; |
| 10319 | } ScriptObj; |
| 10320 | |
| 10321 | static void JimSetScriptFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr); |
| 10322 | static int JimParseCheckMissing(Jim_Interp *interp, int ch); |
| 10323 | static ScriptObj *JimGetScript(Jim_Interp *interp, Jim_Obj *objPtr); |
| 10324 | static void JimSetErrorStack(Jim_Interp *interp, ScriptObj *script); |
| 10325 | |
| 10326 | void FreeScriptInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) |
| 10327 | { |
| 10328 | int i; |
| 10329 | struct ScriptObj *script = (void *)objPtr->internalRep.ptr; |
| 10330 | |
| 10331 | if (--script->inUse != 0) |
| 10332 | return; |
| 10333 | for (i = 0; i < script->len; i++) { |
| 10334 | Jim_DecrRefCount(interp, script->token[i].objPtr); |
| 10335 | } |
| 10336 | Jim_Free(script->token); |
| 10337 | Jim_DecrRefCount(interp, script->fileNameObj); |
| 10338 | Jim_Free(script); |
| 10339 | } |
| 10340 | |
| 10341 | void DupScriptInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) |
| 10342 | { |
| 10343 | JIM_NOTUSED(interp); |
| 10344 | JIM_NOTUSED(srcPtr); |
| 10345 | |
| 10346 | dupPtr->typePtr = NULL; |
| 10347 | } |
| 10348 | |
| 10349 | typedef struct |
| 10350 | { |
| 10351 | const char *token; |
| 10352 | int len; |
| 10353 | int type; |
| 10354 | int line; |
| 10355 | } ParseToken; |
| 10356 | |
| 10357 | typedef struct |
| 10358 | { |
| 10359 | |
| 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); |
| 10379 | } |
| 10380 | } |
| 10381 | |
| 10382 | static void ScriptAddToken(ParseTokenList *tokenlist, const char *token, int len, int type, |
| 10383 | int line) |
| 10384 | { |
| 10385 | ParseToken *t; |
| 10386 | |
| 10387 | if (tokenlist->count == tokenlist->size) { |
| 10388 | |
| 10389 | tokenlist->size *= 2; |
| 10390 | if (tokenlist->list != tokenlist->static_list) { |
| 10391 | tokenlist->list = |
| 10392 | Jim_Realloc(tokenlist->list, tokenlist->size * sizeof(*tokenlist->list)); |
| 10393 | } |
| 10394 | else { |
| 10395 | |
| 10396 | tokenlist->list = Jim_Alloc(tokenlist->size * sizeof(*tokenlist->list)); |
| 10397 | memcpy(tokenlist->list, tokenlist->static_list, |
| 10398 | tokenlist->count * sizeof(*tokenlist->list)); |
| 10399 | } |
| 10400 | } |
| 10401 | t = &tokenlist->list[tokenlist->count++]; |
| 10402 | t->token = token; |
| 10403 | t->len = len; |
| 10404 | t->type = type; |
| 10405 | t->line = line; |
| 10406 | } |
| 10407 | |
| 10408 | static int JimCountWordTokens(struct ScriptObj *script, ParseToken *t) |
| 10409 | { |
| 10410 | int expand = 1; |
| 10411 | int count = 0; |
| 10412 | |
| 10413 | |
| 10414 | if (t->type == JIM_TT_STR && !TOKEN_IS_SEP(t[1].type)) { |
| 10415 | if ((t->len == 1 && *t->token == '*') || (t->len == 6 && strncmp(t->token, "expand", 6) == 0)) { |
| 10416 | |
| 10417 | expand = -1; |
| 10418 | t++; |
| 10419 | } |
| 10420 | else { |
| 10421 | if (script->missing == ' ') { |
| 10422 | |
| 10423 | script->missing = '}'; |
| 10424 | script->linenr = t[1].line; |
| 10425 | } |
| 10426 | } |
| 10427 | } |
| 10428 | |
| 10429 | |
| 10430 | while (!TOKEN_IS_SEP(t->type)) { |
| 10431 | t++; |
| 10432 | count++; |
| 10433 | } |
| 10434 | |
| 10435 | return count * expand; |
| 10436 | } |
| 10437 | |
| 10438 | static Jim_Obj *JimMakeScriptObj(Jim_Interp *interp, const ParseToken *t) |
| 10439 | { |
| 10440 | Jim_Obj *objPtr; |
| 10441 | |
| 10442 | if (t->type == JIM_TT_ESC && memchr(t->token, '\\', t->len) != NULL) { |
| 10443 | |
| 10444 | int len = t->len; |
| 10445 | char *str = Jim_Alloc(len + 1); |
| 10446 | len = JimEscape(str, t->token, len); |
| 10447 | objPtr = Jim_NewStringObjNoAlloc(interp, str, len); |
| 10448 | } |
| 10449 | else { |
| 10450 | objPtr = Jim_NewStringObj(interp, t->token, t->len); |
| 10451 | } |
| 10452 | return objPtr; |
| 10453 | } |
| 10454 | |
| 10455 | static void ScriptObjAddTokens(Jim_Interp *interp, struct ScriptObj *script, |
| 10456 | ParseTokenList *tokenlist) |
| 10457 | { |
| 10458 | int i; |
| 10459 | struct ScriptToken *token; |
| 10460 | |
| 10461 | int lineargs = 0; |
| 10462 | |
| 10463 | ScriptToken *linefirst; |
| 10464 | int count; |
| 10465 | int linenr; |
| 10466 | |
| 10467 | #ifdef DEBUG_SHOW_SCRIPT_TOKENS |
| 10468 | printf("==== Tokens ====\n"); |
| 10469 | for (i = 0; i < tokenlist->count; i++) { |
| 10470 | printf("[%2d]@%d %s '%.*s'\n", i, tokenlist->list[i].line, jim_tt_name(tokenlist->list[i].type), |
| 10471 | tokenlist->list[i].len, tokenlist->list[i].token); |
| 10472 | } |
| 10473 | #endif |
| 10474 | |
| 10475 | |
| 10476 | count = tokenlist->count; |
| 10477 | for (i = 0; i < tokenlist->count; i++) { |
| 10478 | if (tokenlist->list[i].type == JIM_TT_EOL) { |
| 10479 | count++; |
| 10480 | } |
| 10481 | } |
| 10482 | linenr = script->firstline = tokenlist->list[0].line; |
| 10483 | |
| 10484 | token = script->token = Jim_Alloc(sizeof(ScriptToken) * count); |
| 10485 | |
| 10486 | |
| 10487 | linefirst = token++; |
| 10488 | |
| 10489 | for (i = 0; i < tokenlist->count; ) { |
| 10490 | |
| 10491 | int wordtokens; |
| 10492 | |
| 10493 | |
| 10494 | while (tokenlist->list[i].type == JIM_TT_SEP) { |
| 10495 | i++; |
| 10496 | } |
| 10497 | |
| 10498 | wordtokens = JimCountWordTokens(script, tokenlist->list + i); |
| 10499 | |
| 10500 | if (wordtokens == 0) { |
| 10501 | |
| 10502 | if (lineargs) { |
| 10503 | linefirst->type = JIM_TT_LINE; |
| 10504 | linefirst->objPtr = JimNewScriptLineObj(interp, lineargs, linenr); |
| 10505 | Jim_IncrRefCount(linefirst->objPtr); |
| 10506 | |
| 10507 | |
| 10508 | lineargs = 0; |
| 10509 | linefirst = token++; |
| 10510 | } |
| 10511 | i++; |
| 10512 | continue; |
| 10513 | } |
| 10514 | else if (wordtokens != 1) { |
| 10515 | |
| 10516 | token->type = JIM_TT_WORD; |
| 10517 | token->objPtr = Jim_NewIntObj(interp, wordtokens); |
| 10518 | Jim_IncrRefCount(token->objPtr); |
| 10519 | token++; |
| 10520 | if (wordtokens < 0) { |
| 10521 | |
| 10522 | i++; |
| 10523 | wordtokens = -wordtokens - 1; |
| 10524 | lineargs--; |
| 10525 | } |
| 10526 | } |
| 10527 | |
| 10528 | if (lineargs == 0) { |
| 10529 | |
| 10530 | linenr = tokenlist->list[i].line; |
| 10531 | } |
| 10532 | lineargs++; |
| 10533 | |
| 10534 | |
| 10535 | while (wordtokens--) { |
| 10536 | const ParseToken *t = &tokenlist->list[i++]; |
| 10537 | |
| 10538 | token->type = t->type; |
| 10539 | token->objPtr = JimMakeScriptObj(interp, t); |
| 10540 | Jim_IncrRefCount(token->objPtr); |
| 10541 | |
| 10542 | Jim_SetSourceInfo(interp, token->objPtr, script->fileNameObj, t->line); |
| 10543 | token++; |
| 10544 | } |
| 10545 | } |
| 10546 | |
| 10547 | if (lineargs == 0) { |
| 10548 | token--; |
| 10549 | } |
| 10550 | |
| 10551 | script->len = token - script->token; |
| 10552 | |
| 10553 | JimPanic((script->len >= count, "allocated script array is too short")); |
| 10554 | |
| 10555 | #ifdef DEBUG_SHOW_SCRIPT |
| 10556 | printf("==== Script (%s) ====\n", Jim_String(script->fileNameObj)); |
| 10557 | for (i = 0; i < script->len; i++) { |
| 10558 | const ScriptToken *t = &script->token[i]; |
| 10559 | printf("[%2d] %s %s\n", i, jim_tt_name(t->type), Jim_String(t->objPtr)); |
| 10560 | } |
| 10561 | #endif |
| 10562 | |
| 10563 | } |
| 10564 | |
| 10565 | int Jim_ScriptIsComplete(Jim_Interp *interp, Jim_Obj *scriptObj, char *stateCharPtr) |
| 10566 | { |
| 10567 | ScriptObj *script = JimGetScript(interp, scriptObj); |
| 10568 | if (stateCharPtr) { |
| 10569 | *stateCharPtr = script->missing; |
| 10570 | } |
| 10571 | return script->missing == ' ' || script->missing == '}'; |
| 10572 | } |
| 10573 | |
| 10574 | static int JimParseCheckMissing(Jim_Interp *interp, int ch) |
| 10575 | { |
| 10576 | const char *msg; |
| 10577 | |
| 10578 | switch (ch) { |
| 10579 | case '\\': |
| 10580 | case ' ': |
| 10581 | return JIM_OK; |
| 10582 | |
| 10583 | case '[': |
| 10584 | msg = "unmatched \"[\""; |
| 10585 | break; |
| 10586 | case '{': |
| 10587 | msg = "missing close-brace"; |
| 10588 | break; |
| 10589 | case '}': |
| 10590 | msg = "extra characters after close-brace"; |
| 10591 | break; |
| 10592 | case '"': |
| 10593 | default: |
| 10594 | msg = "missing quote"; |
| 10595 | break; |
| 10596 | } |
| 10597 | |
| 10598 | Jim_SetResultString(interp, msg, -1); |
| 10599 | return JIM_ERR; |
| 10600 | } |
| 10601 | |
| 10602 | Jim_Obj *Jim_GetSourceInfo(Jim_Interp *interp, Jim_Obj *objPtr, int *lineptr) |
| 10603 | { |
| 10604 | int line; |
| 10605 | Jim_Obj *fileNameObj; |
| 10606 | |
| 10607 | if (objPtr->typePtr == &sourceObjType) { |
| 10608 | fileNameObj = objPtr->internalRep.sourceValue.fileNameObj; |
| 10609 | line = objPtr->internalRep.sourceValue.lineNumber; |
| 10610 | } |
| 10611 | else if (objPtr->typePtr == &scriptObjType) { |
| 10612 | ScriptObj *script = JimGetScript(interp, objPtr); |
| 10613 | fileNameObj = script->fileNameObj; |
| 10614 | line = script->firstline; |
| 10615 | } |
| 10616 | else { |
| 10617 | fileNameObj = interp->emptyObj; |
| 10618 | line = 1; |
| 10619 | } |
| 10620 | *lineptr = line; |
| 10621 | return fileNameObj; |
| 10622 | } |
| 10623 | |
| 10624 | void Jim_SetSourceInfo(Jim_Interp *interp, Jim_Obj *objPtr, |
| 10625 | Jim_Obj *fileNameObj, int lineNumber) |
| 10626 | { |
| 10627 | JimPanic((Jim_IsShared(objPtr), "Jim_SetSourceInfo called with shared object")); |
| 10628 | Jim_FreeIntRep(interp, objPtr); |
| 10629 | Jim_IncrRefCount(fileNameObj); |
| 10630 | objPtr->internalRep.sourceValue.fileNameObj = fileNameObj; |
| 10631 | objPtr->internalRep.sourceValue.lineNumber = lineNumber; |
| 10632 | objPtr->typePtr = &sourceObjType; |
| 10633 | } |
| 10634 | |
| 10635 | static void SubstObjAddTokens(Jim_Interp *interp, struct ScriptObj *script, |
| 10636 | ParseTokenList *tokenlist) |
| 10637 | { |
| 10638 | int i; |
| 10639 | struct ScriptToken *token; |
| 10640 | |
| 10641 | token = script->token = Jim_Alloc(sizeof(ScriptToken) * tokenlist->count); |
| 10642 | |
| 10643 | for (i = 0; i < tokenlist->count; i++) { |
| 10644 | const ParseToken *t = &tokenlist->list[i]; |
| 10645 | |
| 10646 | |
| 10647 | token->type = t->type; |
| 10648 | token->objPtr = JimMakeScriptObj(interp, t); |
| 10649 | Jim_IncrRefCount(token->objPtr); |
| 10650 | token++; |
| 10651 | } |
| 10652 | |
| 10653 | script->len = i; |
| 10654 | } |
| 10655 | |
| 10656 | static void JimSetScriptFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr) |
| 10657 | { |
| 10658 | int scriptTextLen; |
| 10659 | const char *scriptText = Jim_GetString(objPtr, &scriptTextLen); |
| 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 | |
| 10671 | ScriptTokenListInit(&tokenlist); |
| 10672 | |
| 10673 | JimParserInit(&parser, scriptText, scriptTextLen, line); |
| 10674 | while (!parser.eof) { |
| 10675 | JimParseScript(&parser); |
| 10676 | ScriptAddToken(&tokenlist, parser.tstart, parser.tend - parser.tstart + 1, parser.tt, |
| 10677 | parser.tline); |
| 10678 | } |
| 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); |
| 10694 | script->missing = parser.missing.ch; |
| 10695 | script->linenr = parser.missing.line; |
| 10696 | |
| 10697 | ScriptObjAddTokens(interp, script, &tokenlist); |
| 10698 | |
| 10699 | |
| 10700 | ScriptTokenListFree(&tokenlist); |
| 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) { |
| 10713 | |
| 10714 | objPtr = interp->nullScriptObj; |
| 10715 | } |
| 10716 | |
| 10717 | if (objPtr->typePtr != &scriptObjType || ((struct ScriptObj *)Jim_GetIntRepPtr(objPtr))->substFlags) { |
| 10718 | JimSetScriptFromAny(interp, objPtr); |
| 10719 | } |
| 10720 | |
| 10721 | return (ScriptObj *)Jim_GetIntRepPtr(objPtr); |
| 10722 | } |
| 10723 | |
| 10724 | void Jim_InterpIncrProcEpoch(Jim_Interp *interp) |
| 10725 | { |
| 10726 | interp->procEpoch++; |
| 10727 | |
| 10728 | |
| 10729 | while (interp->oldCmdCache) { |
| 10730 | Jim_Cmd *next = interp->oldCmdCache->prevCmd; |
| 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); |
| 10753 | Jim_Free(cmdPtr->u.proc.staticVars); |
| 10754 | } |
| 10755 | } |
| 10756 | else { |
| 10757 | |
| 10758 | if (cmdPtr->u.native.delProc) { |
| 10759 | cmdPtr->u.native.delProc(interp, cmdPtr->u.native.privData); |
| 10760 | } |
| 10761 | } |
| 10762 | if (cmdPtr->prevCmd) { |
| 10763 | |
| 10764 | JimDecrCmdRefCount(interp, cmdPtr->prevCmd); |
| 10765 | } |
| 10766 | |
| 10767 | cmdPtr->prevCmd = interp->oldCmdCache; |
| 10768 | interp->oldCmdCache = cmdPtr; |
| 10769 | if (!interp->quitting && ++interp->oldCmdCacheSize >= 1000) { |
| 10770 | Jim_InterpIncrProcEpoch(interp); |
| 10771 | } |
| 10772 | } |
| 10773 | } |
| 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) { |
| 10785 | if (vv->objPtr) { |
| 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; |
| 10803 | const char *string; |
| 10804 | |
| 10805 | #ifdef JIM_OPTIMIZATION |
| 10806 | if (JimIsWide(keyObj) && keyObj->bytes == NULL) { |
| 10807 | |
| 10808 | jim_wide objValue = JimWideValue(keyObj); |
| 10809 | if (objValue > INT_MIN && objValue < INT_MAX) { |
| 10810 | unsigned result = 0; |
| 10811 | unsigned value = (unsigned)objValue; |
| 10812 | |
| 10813 | if (objValue < 0) { |
| 10814 | value = (unsigned)-objValue; |
| 10815 | } |
| 10816 | |
| 10817 | |
| 10818 | do { |
| 10819 | result += (result << 3) + (value % 10 + '0'); |
| 10820 | value /= 10; |
| 10821 | } while (value); |
| 10822 | |
| 10823 | if (objValue < 0) { |
| 10824 | result += (result << 3) + '-'; |
| 10825 | } |
| 10826 | return result; |
| 10827 | } |
| 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 | } |
| 10839 | |
| 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 | } |
| 10851 | |
| 10852 | |
| 10853 | static void *JimVariablesHTValDup(void *privdata, const void *val) |
| 10854 | { |
| 10855 | JimIncrVarRef((Jim_VarVal *)val); |
| 10856 | return (void *)val; |
| 10857 | } |
| 10858 | |
| 10859 | static const Jim_HashTableType JimVariablesHashTableType = { |
| 10860 | JimObjectHTHashFunction, |
| 10861 | JimObjectHTKeyValDup, |
| 10862 | JimVariablesHTValDup, |
| 10863 | JimObjectHTKeyCompare, |
| 10864 | JimObjectHTKeyValDestructor, |
| 10865 | JimVariablesHTValDestructor |
| 10866 | }; |
| 10867 | |
| 10868 | |
| 10869 | static const char *Jim_GetStringNoQualifier(Jim_Obj *objPtr, int *length) |
| 10870 | { |
| 10871 | int len; |
| 10872 | const char *str = Jim_GetString(objPtr, &len); |
| 10873 | if (len >= 2 && str[0] == ':' && str[1] == ':') { |
| 10874 | while (len && *str == ':') { |
| 10875 | len--; |
| 10876 | str++; |
| 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 | } |
| 10905 | |
| 10906 | static const Jim_HashTableType JimCommandsHashTableType = { |
| 10907 | JimCommandsHT_HashFunction, |
| 10908 | JimObjectHTKeyValDup, |
| 10909 | NULL, |
| 10910 | JimCommandsHT_KeyCompare, |
| 10911 | JimObjectHTKeyValDestructor, |
| 10912 | JimCommandsHT_ValDestructor |
| 10913 | }; |
| 10914 | |
| 10915 | |
| 10916 | |
| 10917 | Jim_Obj *Jim_MakeGlobalNamespaceName(Jim_Interp *interp, Jim_Obj *nameObjPtr) |
| 10918 | { |
| 10919 | #ifdef jim_ext_namespace |
| 10920 | Jim_Obj *resultObj; |
| 10921 | |
| 10922 | const char *name = Jim_String(nameObjPtr); |
| 10923 | if (name[0] == ':' && name[1] == ':') { |
| 10924 | return nameObjPtr; |
| 10925 | } |
| 10926 | Jim_IncrRefCount(nameObjPtr); |
| 10927 | resultObj = Jim_NewStringObj(interp, "::", -1); |
| 10928 | Jim_AppendObj(interp, resultObj, nameObjPtr); |
| 10929 | Jim_DecrRefCount(interp, nameObjPtr); |
| 10930 | |
| 10931 | return resultObj; |
| 10932 | #else |
| 10933 | return nameObjPtr; |
| 10934 | #endif |
| 10935 | } |
| 10936 | |
| 10937 | static Jim_Obj *JimQualifyName(Jim_Interp *interp, Jim_Obj *objPtr) |
| 10938 | { |
| 10939 | #ifdef jim_ext_namespace |
| 10940 | if (Jim_Length(interp->framePtr->nsObj)) { |
| 10941 | int len; |
| 10942 | const char *name = Jim_GetString(objPtr, &len); |
| 10943 | if (len < 2 || name[0] != ':' || name[1] != ':') { |
| 10944 | |
| 10945 | objPtr = Jim_DuplicateObj(interp, interp->framePtr->nsObj); |
| 10946 | Jim_AppendStrings(interp, objPtr, "::", name, NULL); |
| 10947 | } |
| 10948 | } |
| 10949 | #endif |
| 10950 | Jim_IncrRefCount(objPtr); |
| 10951 | return objPtr; |
| 10952 | } |
| 10953 | |
| 10954 | static void JimCreateCommand(Jim_Interp *interp, Jim_Obj *nameObjPtr, Jim_Cmd *cmd) |
| 10955 | { |
| 10956 | JimPanic((nameObjPtr->refCount == 0, "JimCreateCommand called with zero ref count name")); |
| 10957 | |
| 10958 | if (interp->local) { |
| 10959 | Jim_HashEntry *he = Jim_FindHashEntry(&interp->commands, nameObjPtr); |
| 10960 | if (he) { |
| 10961 | |
| 10962 | cmd->prevCmd = Jim_GetHashEntryVal(he); |
| 10963 | Jim_SetHashVal(&interp->commands, he, cmd); |
| 10964 | |
| 10965 | Jim_InterpIncrProcEpoch(interp); |
| 10966 | return; |
| 10967 | } |
| 10968 | } |
| 10969 | |
| 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 | |
| 11020 | len = Jim_ListLength(interp, staticsListObjPtr); |
| 11021 | if (len == 0) { |
| 11022 | return JIM_OK; |
| 11023 | } |
| 11024 | |
| 11025 | cmdPtr->u.proc.staticVars = Jim_Alloc(sizeof(Jim_HashTable)); |
| 11026 | Jim_InitHashTable(cmdPtr->u.proc.staticVars, &JimVariablesHashTableType, interp); |
| 11027 | for (i = 0; i < len; i++) { |
| 11028 | Jim_Obj *initObjPtr = NULL; |
| 11029 | Jim_Obj *nameObjPtr; |
| 11030 | Jim_VarVal *vv = NULL; |
| 11031 | Jim_Obj *objPtr = Jim_ListGetIndex(interp, staticsListObjPtr, i); |
| 11032 | int subLen = Jim_ListLength(interp, objPtr); |
| 11033 | int byref = 0; |
| 11034 | |
| 11035 | |
| 11036 | if (subLen != 1 && subLen != 2) { |
| 11037 | Jim_SetResultFormatted(interp, "too many fields in static specifier \"%#s\"", |
| 11038 | objPtr); |
| 11039 | return JIM_ERR; |
| 11040 | } |
| 11041 | |
| 11042 | nameObjPtr = Jim_ListGetIndex(interp, objPtr, 0); |
| 11043 | |
| 11044 | |
| 11045 | if (subLen == 1) { |
| 11046 | int len; |
| 11047 | const char *pt = Jim_GetString(nameObjPtr, &len); |
| 11048 | if (*pt == '&') { |
| 11049 | |
| 11050 | nameObjPtr = Jim_NewStringObj(interp, pt + 1, len - 1); |
| 11051 | byref = 1; |
| 11052 | } |
| 11053 | } |
| 11054 | Jim_IncrRefCount(nameObjPtr); |
| 11055 | |
| 11056 | if (subLen == 1) { |
| 11057 | switch (SetVariableFromAny(interp, nameObjPtr)) { |
| 11058 | case JIM_DICT_SUGAR: |
| 11059 | |
| 11060 | if (byref) { |
| 11061 | Jim_SetResultFormatted(interp, "Can't link to array element \"%#s\"", nameObjPtr); |
| 11062 | } |
| 11063 | else { |
| 11064 | Jim_SetResultFormatted(interp, "Can't initialise array element \"%#s\"", nameObjPtr); |
| 11065 | } |
| 11066 | Jim_DecrRefCount(interp, nameObjPtr); |
| 11067 | return JIM_ERR; |
| 11068 | |
| 11069 | case JIM_OK: |
| 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 | |
| 11085 | Jim_SetResultFormatted(interp, |
| 11086 | "variable for initialization of static \"%#s\" not found in the local context", |
| 11087 | nameObjPtr); |
| 11088 | Jim_DecrRefCount(interp, nameObjPtr); |
| 11089 | return JIM_ERR; |
| 11090 | } |
| 11091 | } |
| 11092 | else { |
| 11093 | initObjPtr = Jim_ListGetIndex(interp, objPtr, 1); |
| 11094 | } |
| 11095 | |
| 11096 | if (vv == NULL) { |
| 11097 | vv = Jim_Alloc(sizeof(*vv)); |
| 11098 | vv->objPtr = initObjPtr; |
| 11099 | Jim_IncrRefCount(vv->objPtr); |
| 11100 | vv->linkFramePtr = NULL; |
| 11101 | vv->refCount = 0; |
| 11102 | } |
| 11103 | |
| 11104 | if (JimSetNewVariable(cmdPtr->u.proc.staticVars, nameObjPtr, vv) != JIM_OK) { |
| 11105 | Jim_SetResultFormatted(interp, |
| 11106 | "static variable name \"%#s\" duplicated in statics list", nameObjPtr); |
| 11107 | JimIncrVarRef(vv); |
| 11108 | JimDecrVarRef(interp, vv); |
| 11109 | Jim_DecrRefCount(interp, nameObjPtr); |
| 11110 | return JIM_ERR; |
| 11111 | } |
| 11112 | |
| 11113 | Jim_DecrRefCount(interp, nameObjPtr); |
| 11114 | } |
| 11115 | return JIM_OK; |
| 11116 | } |
| 11117 | |
| 11118 | |
| 11119 | #ifdef jim_ext_namespace |
| 11120 | static const char *Jim_memrchr(const char *p, int c, int len) |
| 11121 | { |
| 11122 | int i; |
| 11123 | for (i = len; i > 0; i--) { |
| 11124 | if (p[i] == c) { |
| 11125 | return p + i; |
| 11126 | } |
| 11127 | } |
| 11128 | return NULL; |
| 11129 | } |
| 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] == ':') { |
| 11141 | pt++; |
| 11142 | Jim_DecrRefCount(interp, cmdPtr->u.proc.nsObj); |
| 11143 | cmdPtr->u.proc.nsObj = Jim_NewStringObj(interp, cmdname, pt - cmdname - 2); |
| 11144 | Jim_IncrRefCount(cmdPtr->u.proc.nsObj); |
| 11145 | |
| 11146 | Jim_Obj *tempObj = Jim_NewStringObj(interp, pt, len - (pt - cmdname)); |
| 11147 | if (Jim_FindHashEntry(&interp->commands, tempObj)) { |
| 11148 | |
| 11149 | Jim_InterpIncrProcEpoch(interp); |
| 11150 | } |
| 11151 | Jim_FreeNewObj(interp, tempObj); |
| 11152 | } |
| 11153 | } |
| 11154 | #endif |
| 11155 | } |
| 11156 | |
| 11157 | static Jim_Cmd *JimCreateProcedureCmd(Jim_Interp *interp, Jim_Obj *argListObjPtr, |
| 11158 | Jim_Obj *staticsListObjPtr, Jim_Obj *bodyObjPtr, Jim_Obj *nsObj) |
| 11159 | { |
| 11160 | Jim_Cmd *cmdPtr; |
| 11161 | int argListLen; |
| 11162 | int i; |
| 11163 | |
| 11164 | argListLen = Jim_ListLength(interp, argListObjPtr); |
| 11165 | |
| 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); |
| 11177 | cmdPtr->u.proc.nsObj = nsObj ? nsObj : interp->emptyObj; |
| 11178 | Jim_IncrRefCount(argListObjPtr); |
| 11179 | Jim_IncrRefCount(bodyObjPtr); |
| 11180 | Jim_IncrRefCount(cmdPtr->u.proc.nsObj); |
| 11181 | |
| 11182 | |
| 11183 | if (staticsListObjPtr && JimCreateProcedureStatics(interp, cmdPtr, staticsListObjPtr) != JIM_OK) { |
| 11184 | goto err; |
| 11185 | } |
| 11186 | |
| 11187 | |
| 11188 | |
| 11189 | for (i = 0; i < argListLen; i++) { |
| 11190 | Jim_Obj *argPtr; |
| 11191 | Jim_Obj *nameObjPtr; |
| 11192 | Jim_Obj *defaultObjPtr; |
| 11193 | int len; |
| 11194 | |
| 11195 | |
| 11196 | argPtr = Jim_ListGetIndex(interp, argListObjPtr, i); |
| 11197 | len = Jim_ListLength(interp, argPtr); |
| 11198 | if (len == 0) { |
| 11199 | Jim_SetResultString(interp, "argument with no name", -1); |
| 11200 | err: |
| 11201 | JimDecrCmdRefCount(interp, cmdPtr); |
| 11202 | return NULL; |
| 11203 | } |
| 11204 | if (len > 2) { |
| 11205 | Jim_SetResultFormatted(interp, "too many fields in argument specifier \"%#s\"", argPtr); |
| 11206 | goto err; |
| 11207 | } |
| 11208 | |
| 11209 | if (len == 2) { |
| 11210 | |
| 11211 | nameObjPtr = Jim_ListGetIndex(interp, argPtr, 0); |
| 11212 | defaultObjPtr = Jim_ListGetIndex(interp, argPtr, 1); |
| 11213 | } |
| 11214 | else { |
| 11215 | |
| 11216 | nameObjPtr = argPtr; |
| 11217 | defaultObjPtr = NULL; |
| 11218 | } |
| 11219 | |
| 11220 | |
| 11221 | if (Jim_CompareStringImmediate(interp, nameObjPtr, "args")) { |
| 11222 | if (cmdPtr->u.proc.argsPos >= 0) { |
| 11223 | Jim_SetResultString(interp, "'args' specified more than once", -1); |
| 11224 | goto err; |
| 11225 | } |
| 11226 | cmdPtr->u.proc.argsPos = i; |
| 11227 | } |
| 11228 | else { |
| 11229 | if (len == 2) { |
| 11230 | cmdPtr->u.proc.optArity++; |
| 11231 | } |
| 11232 | else { |
| 11233 | cmdPtr->u.proc.reqArity++; |
| 11234 | } |
| 11235 | } |
| 11236 | |
| 11237 | cmdPtr->u.proc.arglist[i].nameObjPtr = nameObjPtr; |
| 11238 | cmdPtr->u.proc.arglist[i].defaultObjPtr = defaultObjPtr; |
| 11239 | } |
| 11240 | |
| 11241 | return cmdPtr; |
| 11242 | } |
| 11243 | |
| 11244 | int Jim_DeleteCommand(Jim_Interp *interp, Jim_Obj *nameObj) |
| 11245 | { |
| 11246 | int ret = JIM_OK; |
| 11247 | |
| 11248 | nameObj = JimQualifyName(interp, nameObj); |
| 11249 | |
| 11250 | if (Jim_DeleteHashEntry(&interp->commands, nameObj) == JIM_ERR) { |
| 11251 | Jim_SetResultFormatted(interp, "can't delete \"%#s\": command doesn't exist", nameObj); |
| 11252 | ret = JIM_ERR; |
| 11253 | } |
| 11254 | Jim_DecrRefCount(interp, nameObj); |
| 11255 | |
| 11256 | return ret; |
| 11257 | } |
| 11258 | |
| 11259 | int Jim_RenameCommand(Jim_Interp *interp, Jim_Obj *oldNameObj, Jim_Obj *newNameObj) |
| 11260 | { |
| 11261 | int ret = JIM_ERR; |
| 11262 | Jim_HashEntry *he; |
| 11263 | Jim_Cmd *cmdPtr; |
| 11264 | |
| 11265 | if (Jim_Length(newNameObj) == 0) { |
| 11266 | return Jim_DeleteCommand(interp, oldNameObj); |
| 11267 | } |
| 11268 | |
| 11269 | |
| 11270 | |
| 11271 | oldNameObj = JimQualifyName(interp, oldNameObj); |
| 11272 | newNameObj = JimQualifyName(interp, newNameObj); |
| 11273 | |
| 11274 | |
| 11275 | he = Jim_FindHashEntry(&interp->commands, oldNameObj); |
| 11276 | if (he == NULL) { |
| 11277 | Jim_SetResultFormatted(interp, "can't rename \"%#s\": command doesn't exist", oldNameObj); |
| 11278 | } |
| 11279 | else if (Jim_FindHashEntry(&interp->commands, newNameObj)) { |
| 11280 | Jim_SetResultFormatted(interp, "can't rename to \"%#s\": command already exists", newNameObj); |
| 11281 | } |
| 11282 | else { |
| 11283 | cmdPtr = Jim_GetHashEntryVal(he); |
| 11284 | if (cmdPtr->prevCmd) { |
| 11285 | Jim_SetResultFormatted(interp, "can't rename local command \"%#s\"", oldNameObj); |
| 11286 | } |
| 11287 | else { |
| 11288 | |
| 11289 | JimIncrCmdRefCount(cmdPtr); |
| 11290 | JimUpdateProcNamespace(interp, cmdPtr, newNameObj); |
| 11291 | Jim_AddHashEntry(&interp->commands, newNameObj, cmdPtr); |
| 11292 | |
| 11293 | |
| 11294 | Jim_DeleteHashEntry(&interp->commands, oldNameObj); |
| 11295 | |
| 11296 | |
| 11297 | Jim_InterpIncrProcEpoch(interp); |
| 11298 | |
| 11299 | ret = JIM_OK; |
| 11300 | } |
| 11301 | } |
| 11302 | |
| 11303 | Jim_DecrRefCount(interp, oldNameObj); |
| 11304 | Jim_DecrRefCount(interp, newNameObj); |
| 11305 | |
| 11306 | return ret; |
| 11307 | } |
| 11308 | |
| 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; |
| 11320 | Jim_IncrRefCount(dupPtr->internalRep.cmdValue.nsObj); |
| 11321 | } |
| 11322 | |
| 11323 | static const Jim_ObjType commandObjType = { |
| 11324 | "command", |
| 11325 | FreeCommandInternalRep, |
| 11326 | DupCommandInternalRep, |
| 11327 | NULL, |
| 11328 | JIM_TYPE_REFERENCES, |
| 11329 | }; |
| 11330 | |
| 11331 | Jim_Cmd *Jim_GetCommand(Jim_Interp *interp, Jim_Obj *objPtr, int flags) |
| 11332 | { |
| 11333 | Jim_Cmd *cmd; |
| 11334 | |
| 11335 | if (objPtr->typePtr == &commandObjType |
| 11336 | && objPtr->internalRep.cmdValue.procEpoch == interp->procEpoch |
| 11337 | #ifdef jim_ext_namespace |
| 11338 | && Jim_StringEqObj(objPtr->internalRep.cmdValue.nsObj, interp->framePtr->nsObj) |
| 11339 | #endif |
| 11340 | && objPtr->internalRep.cmdValue.cmdPtr->inUse) { |
| 11341 | |
| 11342 | cmd = objPtr->internalRep.cmdValue.cmdPtr; |
| 11343 | } |
| 11344 | else { |
| 11345 | Jim_Obj *qualifiedNameObj = JimQualifyName(interp, objPtr); |
| 11346 | Jim_HashEntry *he = Jim_FindHashEntry(&interp->commands, qualifiedNameObj); |
| 11347 | #ifdef jim_ext_namespace |
| 11348 | if (he == NULL && Jim_Length(interp->framePtr->nsObj)) { |
| 11349 | he = Jim_FindHashEntry(&interp->commands, objPtr); |
| 11350 | } |
| 11351 | #endif |
| 11352 | if (he == NULL) { |
| 11353 | if (flags & JIM_ERRMSG) { |
| 11354 | Jim_SetResultFormatted(interp, "invalid command name \"%#s\"", objPtr); |
| 11355 | } |
| 11356 | Jim_DecrRefCount(interp, qualifiedNameObj); |
| 11357 | return NULL; |
| 11358 | } |
| 11359 | cmd = Jim_GetHashEntryVal(he); |
| 11360 | |
| 11361 | cmd->cmdNameObj = Jim_GetHashEntryKey(he); |
| 11362 | |
| 11363 | |
| 11364 | Jim_FreeIntRep(interp, objPtr); |
| 11365 | objPtr->typePtr = &commandObjType; |
| 11366 | objPtr->internalRep.cmdValue.procEpoch = interp->procEpoch; |
| 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 | |
| 11378 | |
| 11379 | |
| 11380 | static const Jim_ObjType variableObjType = { |
| 11381 | "variable", |
| 11382 | NULL, |
| 11383 | NULL, |
| 11384 | NULL, |
| 11385 | JIM_TYPE_REFERENCES, |
| 11386 | }; |
| 11387 | |
| 11388 | static int SetVariableFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr) |
| 11389 | { |
| 11390 | const char *varName; |
| 11391 | Jim_CallFrame *framePtr; |
| 11392 | int global; |
| 11393 | int len; |
| 11394 | Jim_VarVal *vv; |
| 11395 | |
| 11396 | |
| 11397 | if (objPtr->typePtr == &variableObjType) { |
| 11398 | framePtr = objPtr->internalRep.varValue.global ? interp->topFramePtr : interp->framePtr; |
| 11399 | if (objPtr->internalRep.varValue.callFrameId == framePtr->id) { |
| 11400 | |
| 11401 | return JIM_OK; |
| 11402 | } |
| 11403 | |
| 11404 | } |
| 11405 | else if (objPtr->typePtr == &dictSubstObjType) { |
| 11406 | return JIM_DICT_SUGAR; |
| 11407 | } |
| 11408 | |
| 11409 | varName = Jim_GetString(objPtr, &len); |
| 11410 | |
| 11411 | |
| 11412 | if (len && varName[len - 1] == ')' && strchr(varName, '(') != NULL) { |
| 11413 | return JIM_DICT_SUGAR; |
| 11414 | } |
| 11415 | |
| 11416 | if (varName[0] == ':' && varName[1] == ':') { |
| 11417 | while (*varName == ':') { |
| 11418 | varName++; |
| 11419 | len--; |
| 11420 | } |
| 11421 | global = 1; |
| 11422 | framePtr = interp->topFramePtr; |
| 11423 | |
| 11424 | Jim_Obj *tempObj = Jim_NewStringObj(interp, varName, len); |
| 11425 | vv = JimFindVariable(&framePtr->vars, tempObj); |
| 11426 | Jim_FreeNewObj(interp, tempObj); |
| 11427 | } |
| 11428 | else { |
| 11429 | global = 0; |
| 11430 | framePtr = interp->framePtr; |
| 11431 | |
| 11432 | vv = JimFindVariable(&framePtr->vars, objPtr); |
| 11433 | if (vv == NULL && framePtr->staticVars) { |
| 11434 | |
| 11435 | vv = JimFindVariable(framePtr->staticVars, objPtr); |
| 11436 | } |
| 11437 | } |
| 11438 | |
| 11439 | if (vv == NULL) { |
| 11440 | return JIM_ERR; |
| 11441 | } |
| 11442 | |
| 11443 | |
| 11444 | Jim_FreeIntRep(interp, objPtr); |
| 11445 | objPtr->typePtr = &variableObjType; |
| 11446 | objPtr->internalRep.varValue.callFrameId = framePtr->id; |
| 11447 | objPtr->internalRep.varValue.vv = vv; |
| 11448 | objPtr->internalRep.varValue.global = global; |
| 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; |
| 11483 | int global; |
| 11484 | int len; |
| 11485 | |
| 11486 | |
| 11487 | Jim_VarVal *vv = Jim_Alloc(sizeof(*vv)); |
| 11488 | |
| 11489 | vv->objPtr = valObjPtr; |
| 11490 | Jim_IncrRefCount(valObjPtr); |
| 11491 | vv->linkFramePtr = NULL; |
| 11492 | vv->refCount = 0; |
| 11493 | |
| 11494 | name = Jim_GetString(nameObjPtr, &len); |
| 11495 | if (name[0] == ':' && name[1] == ':') { |
| 11496 | while (*name == ':') { |
| 11497 | name++; |
| 11498 | len--; |
| 11499 | } |
| 11500 | framePtr = interp->topFramePtr; |
| 11501 | global = 1; |
| 11502 | JimSetNewVariable(&framePtr->vars, Jim_NewStringObj(interp, name, len), vv); |
| 11503 | } |
| 11504 | else { |
| 11505 | framePtr = interp->framePtr; |
| 11506 | global = 0; |
| 11507 | JimSetNewVariable(&framePtr->vars, nameObjPtr, vv); |
| 11508 | } |
| 11509 | |
| 11510 | |
| 11511 | Jim_FreeIntRep(interp, nameObjPtr); |
| 11512 | nameObjPtr->typePtr = &variableObjType; |
| 11513 | nameObjPtr->internalRep.varValue.callFrameId = framePtr->id; |
| 11514 | nameObjPtr->internalRep.varValue.vv = vv; |
| 11515 | nameObjPtr->internalRep.varValue.global = global; |
| 11516 | |
| 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) { |
| 11539 | Jim_IncrRefCount(valObjPtr); |
| 11540 | Jim_DecrRefCount(interp, vv->objPtr); |
| 11541 | vv->objPtr = valObjPtr; |
| 11542 | } |
| 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; |
| 11563 | int result; |
| 11564 | |
| 11565 | nameObjPtr = Jim_NewStringObj(interp, name, -1); |
| 11566 | Jim_IncrRefCount(nameObjPtr); |
| 11567 | result = Jim_SetVariable(interp, nameObjPtr, objPtr); |
| 11568 | Jim_DecrRefCount(interp, nameObjPtr); |
| 11569 | return result; |
| 11570 | } |
| 11571 | |
| 11572 | int Jim_SetGlobalVariableStr(Jim_Interp *interp, const char *name, Jim_Obj *objPtr) |
| 11573 | { |
| 11574 | Jim_CallFrame *savedFramePtr; |
| 11575 | int result; |
| 11576 | |
| 11577 | savedFramePtr = interp->framePtr; |
| 11578 | interp->framePtr = interp->topFramePtr; |
| 11579 | result = Jim_SetVariableStr(interp, name, objPtr); |
| 11580 | interp->framePtr = savedFramePtr; |
| 11581 | return result; |
| 11582 | } |
| 11583 | |
| 11584 | int Jim_SetVariableStrWithStr(Jim_Interp *interp, const char *name, const char *val) |
| 11585 | { |
| 11586 | Jim_Obj *valObjPtr; |
| 11587 | int result; |
| 11588 | |
| 11589 | valObjPtr = Jim_NewStringObj(interp, val, -1); |
| 11590 | Jim_IncrRefCount(valObjPtr); |
| 11591 | result = Jim_SetVariableStr(interp, name, valObjPtr); |
| 11592 | Jim_DecrRefCount(interp, valObjPtr); |
| 11593 | return result; |
| 11594 | } |
| 11595 | |
| 11596 | int Jim_SetVariableLink(Jim_Interp *interp, Jim_Obj *nameObjPtr, |
| 11597 | Jim_Obj *targetNameObjPtr, Jim_CallFrame *targetCallFrame) |
| 11598 | { |
| 11599 | const char *varName; |
| 11600 | const char *targetName; |
| 11601 | Jim_CallFrame *framePtr; |
| 11602 | Jim_VarVal *vv; |
| 11603 | int len; |
| 11604 | int varnamelen; |
| 11605 | |
| 11606 | |
| 11607 | switch (SetVariableFromAny(interp, nameObjPtr)) { |
| 11608 | case JIM_DICT_SUGAR: |
| 11609 | |
| 11610 | Jim_SetResultFormatted(interp, "bad variable name \"%#s\": upvar won't create a scalar variable that looks like an array element", nameObjPtr); |
| 11611 | return JIM_ERR; |
| 11612 | |
| 11613 | case JIM_OK: |
| 11614 | vv = nameObjPtr->internalRep.varValue.vv; |
| 11615 | |
| 11616 | if (vv->linkFramePtr == NULL) { |
| 11617 | Jim_SetResultFormatted(interp, "variable \"%#s\" already exists", nameObjPtr); |
| 11618 | return JIM_ERR; |
| 11619 | } |
| 11620 | |
| 11621 | |
| 11622 | vv->linkFramePtr = NULL; |
| 11623 | break; |
| 11624 | } |
| 11625 | |
| 11626 | |
| 11627 | |
| 11628 | varName = Jim_GetString(nameObjPtr, &varnamelen); |
| 11629 | |
| 11630 | if (varName[0] == ':' && varName[1] == ':') { |
| 11631 | while (*varName == ':') { |
| 11632 | varName++; |
| 11633 | varnamelen--; |
| 11634 | } |
| 11635 | |
| 11636 | framePtr = interp->topFramePtr; |
| 11637 | } |
| 11638 | else { |
| 11639 | framePtr = interp->framePtr; |
| 11640 | } |
| 11641 | |
| 11642 | targetName = Jim_GetString(targetNameObjPtr, &len); |
| 11643 | if (targetName[0] == ':' && targetName[1] == ':') { |
| 11644 | while (*targetName == ':') { |
| 11645 | targetName++; |
| 11646 | len--; |
| 11647 | } |
| 11648 | targetNameObjPtr = Jim_NewStringObj(interp, targetName, len); |
| 11649 | targetCallFrame = interp->topFramePtr; |
| 11650 | } |
| 11651 | Jim_IncrRefCount(targetNameObjPtr); |
| 11652 | |
| 11653 | if (framePtr->level < targetCallFrame->level) { |
| 11654 | Jim_SetResultFormatted(interp, |
| 11655 | "bad variable name \"%#s\": upvar won't create namespace variable that refers to procedure variable", |
| 11656 | nameObjPtr); |
| 11657 | Jim_DecrRefCount(interp, targetNameObjPtr); |
| 11658 | return JIM_ERR; |
| 11659 | } |
| 11660 | |
| 11661 | |
| 11662 | if (framePtr == targetCallFrame) { |
| 11663 | Jim_Obj *objPtr = targetNameObjPtr; |
| 11664 | |
| 11665 | |
| 11666 | while (1) { |
| 11667 | if (Jim_Length(objPtr) == varnamelen && memcmp(Jim_String(objPtr), varName, varnamelen) == 0) { |
| 11668 | Jim_SetResultString(interp, "can't upvar from variable to itself", -1); |
| 11669 | Jim_DecrRefCount(interp, targetNameObjPtr); |
| 11670 | return JIM_ERR; |
| 11671 | } |
| 11672 | if (SetVariableFromAny(interp, objPtr) != JIM_OK) |
| 11673 | break; |
| 11674 | vv = objPtr->internalRep.varValue.vv; |
| 11675 | if (vv->linkFramePtr != targetCallFrame) |
| 11676 | break; |
| 11677 | objPtr = vv->objPtr; |
| 11678 | } |
| 11679 | } |
| 11680 | |
| 11681 | |
| 11682 | Jim_SetVariable(interp, nameObjPtr, targetNameObjPtr); |
| 11683 | |
| 11684 | nameObjPtr->internalRep.varValue.vv->linkFramePtr = targetCallFrame; |
| 11685 | Jim_DecrRefCount(interp, targetNameObjPtr); |
| 11686 | return JIM_OK; |
| 11687 | } |
| 11688 | |
| 11689 | Jim_Obj *Jim_GetVariable(Jim_Interp *interp, Jim_Obj *nameObjPtr, int flags) |
| 11690 | { |
| 11691 | if (interp->safeexpr) { |
| 11692 | return nameObjPtr; |
| 11693 | } |
| 11694 | switch (SetVariableFromAny(interp, nameObjPtr)) { |
| 11695 | case JIM_OK:{ |
| 11696 | Jim_VarVal *vv = nameObjPtr->internalRep.varValue.vv; |
| 11697 | |
| 11698 | if (vv->linkFramePtr == NULL) { |
| 11699 | return vv->objPtr; |
| 11700 | } |
| 11701 | else { |
| 11702 | Jim_Obj *objPtr; |
| 11703 | |
| 11704 | |
| 11705 | Jim_CallFrame *savedCallFrame = interp->framePtr; |
| 11706 | |
| 11707 | interp->framePtr = vv->linkFramePtr; |
| 11708 | objPtr = Jim_GetVariable(interp, vv->objPtr, flags); |
| 11709 | interp->framePtr = savedCallFrame; |
| 11710 | if (objPtr) { |
| 11711 | return objPtr; |
| 11712 | } |
| 11713 | |
| 11714 | } |
| 11715 | } |
| 11716 | break; |
| 11717 | |
| 11718 | case JIM_DICT_SUGAR: |
| 11719 | |
| 11720 | return JimDictSugarGet(interp, nameObjPtr, flags); |
| 11721 | } |
| 11722 | if (flags & JIM_ERRMSG) { |
| 11723 | Jim_SetResultFormatted(interp, "can't read \"%#s\": no such variable", nameObjPtr); |
| 11724 | } |
| 11725 | return NULL; |
| 11726 | } |
| 11727 | |
| 11728 | Jim_Obj *Jim_GetGlobalVariable(Jim_Interp *interp, Jim_Obj *nameObjPtr, int flags) |
| 11729 | { |
| 11730 | Jim_CallFrame *savedFramePtr; |
| 11731 | Jim_Obj *objPtr; |
| 11732 | |
| 11733 | savedFramePtr = interp->framePtr; |
| 11734 | interp->framePtr = interp->topFramePtr; |
| 11735 | objPtr = Jim_GetVariable(interp, nameObjPtr, flags); |
| 11736 | interp->framePtr = savedFramePtr; |
| 11737 | |
| 11738 | return objPtr; |
| 11739 | } |
| 11740 | |
| 11741 | Jim_Obj *Jim_GetVariableStr(Jim_Interp *interp, const char *name, int flags) |
| 11742 | { |
| 11743 | Jim_Obj *nameObjPtr, *varObjPtr; |
| 11744 | |
| 11745 | nameObjPtr = Jim_NewStringObj(interp, name, -1); |
| 11746 | Jim_IncrRefCount(nameObjPtr); |
| 11747 | varObjPtr = Jim_GetVariable(interp, nameObjPtr, flags); |
| 11748 | Jim_DecrRefCount(interp, nameObjPtr); |
| 11749 | return varObjPtr; |
| 11750 | } |
| 11751 | |
| 11752 | Jim_Obj *Jim_GetGlobalVariableStr(Jim_Interp *interp, const char *name, int flags) |
| 11753 | { |
| 11754 | Jim_CallFrame *savedFramePtr; |
| 11755 | Jim_Obj *objPtr; |
| 11756 | |
| 11757 | savedFramePtr = interp->framePtr; |
| 11758 | interp->framePtr = interp->topFramePtr; |
| 11759 | objPtr = Jim_GetVariableStr(interp, name, flags); |
| 11760 | interp->framePtr = savedFramePtr; |
| 11761 | |
| 11762 | return objPtr; |
| 11763 | } |
| 11764 | |
| 11765 | int Jim_UnsetVariable(Jim_Interp *interp, Jim_Obj *nameObjPtr, int flags) |
| 11766 | { |
| 11767 | Jim_VarVal *vv; |
| 11768 | int retval; |
| 11769 | Jim_CallFrame *framePtr; |
| 11770 | |
| 11771 | retval = SetVariableFromAny(interp, nameObjPtr); |
| 11772 | if (retval == JIM_DICT_SUGAR) { |
| 11773 | |
| 11774 | return JimDictSugarSet(interp, nameObjPtr, NULL); |
| 11775 | } |
| 11776 | else if (retval == JIM_OK) { |
| 11777 | vv = nameObjPtr->internalRep.varValue.vv; |
| 11778 | |
| 11779 | |
| 11780 | if (vv->linkFramePtr) { |
| 11781 | framePtr = interp->framePtr; |
| 11782 | interp->framePtr = vv->linkFramePtr; |
| 11783 | retval = Jim_UnsetVariable(interp, vv->objPtr, JIM_NONE); |
| 11784 | interp->framePtr = framePtr; |
| 11785 | } |
| 11786 | else { |
| 11787 | if (nameObjPtr->internalRep.varValue.global) { |
| 11788 | int len; |
| 11789 | const char *name = Jim_GetString(nameObjPtr, &len); |
| 11790 | while (*name == ':') { |
| 11791 | name++; |
| 11792 | len--; |
| 11793 | } |
| 11794 | framePtr = interp->topFramePtr; |
| 11795 | Jim_Obj *tempObj = Jim_NewStringObj(interp, name, len); |
| 11796 | retval = JimUnsetVariable(&framePtr->vars, tempObj); |
| 11797 | Jim_FreeNewObj(interp, tempObj); |
| 11798 | } |
| 11799 | else { |
| 11800 | framePtr = interp->framePtr; |
| 11801 | retval = JimUnsetVariable(&framePtr->vars, nameObjPtr); |
| 11802 | } |
| 11803 | |
| 11804 | if (retval == JIM_OK) { |
| 11805 | |
| 11806 | framePtr->id = interp->callFrameEpoch++; |
| 11807 | } |
| 11808 | } |
| 11809 | } |
| 11810 | if (retval != JIM_OK && (flags & JIM_ERRMSG)) { |
| 11811 | Jim_SetResultFormatted(interp, "can't unset \"%#s\": no such variable", nameObjPtr); |
| 11812 | } |
| 11813 | return retval; |
| 11814 | } |
| 11815 | |
| 11816 | |
| 11817 | |
| 11818 | static void JimDictSugarParseVarKey(Jim_Interp *interp, Jim_Obj *objPtr, |
| 11819 | Jim_Obj **varPtrPtr, Jim_Obj **keyPtrPtr) |
| 11820 | { |
| 11821 | const char *str, *p; |
| 11822 | int len, keyLen; |
| 11823 | Jim_Obj *varObjPtr, *keyObjPtr; |
| 11824 | |
| 11825 | str = Jim_GetString(objPtr, &len); |
| 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; |
| 11847 | } |
| 11848 | |
| 11849 | static int JimDictSugarSet(Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *valObjPtr) |
| 11850 | { |
| 11851 | int err; |
| 11852 | |
| 11853 | SetDictSubstFromAny(interp, objPtr); |
| 11854 | |
| 11855 | err = Jim_SetDictKeysVector(interp, objPtr->internalRep.dictSubstValue.varNameObjPtr, |
| 11856 | &objPtr->internalRep.dictSubstValue.indexObjPtr, 1, valObjPtr, JIM_MUSTEXIST); |
| 11857 | |
| 11858 | if (err == JIM_OK) { |
| 11859 | |
| 11860 | Jim_SetEmptyResult(interp); |
| 11861 | } |
| 11862 | else { |
| 11863 | if (!valObjPtr) { |
| 11864 | |
| 11865 | if (Jim_GetVariable(interp, objPtr->internalRep.dictSubstValue.varNameObjPtr, JIM_NONE)) { |
| 11866 | Jim_SetResultFormatted(interp, "can't unset \"%#s\": no such element in array", |
| 11867 | objPtr); |
| 11868 | return err; |
| 11869 | } |
| 11870 | } |
| 11871 | |
| 11872 | Jim_SetResultFormatted(interp, "can't %s \"%#s\": variable isn't array", |
| 11873 | (valObjPtr ? "set" : "unset"), objPtr); |
| 11874 | } |
| 11875 | return err; |
| 11876 | } |
| 11877 | |
| 11878 | static Jim_Obj *JimDictExpandArrayVariable(Jim_Interp *interp, Jim_Obj *varObjPtr, |
| 11879 | Jim_Obj *keyObjPtr, int flags) |
| 11880 | { |
| 11881 | Jim_Obj *dictObjPtr; |
| 11882 | Jim_Obj *resObjPtr = NULL; |
| 11883 | int ret; |
| 11884 | |
| 11885 | dictObjPtr = Jim_GetVariable(interp, varObjPtr, JIM_ERRMSG); |
| 11886 | if (!dictObjPtr) { |
| 11887 | return NULL; |
| 11888 | } |
| 11889 | |
| 11890 | ret = Jim_DictKey(interp, dictObjPtr, keyObjPtr, &resObjPtr, JIM_NONE); |
| 11891 | if (ret != JIM_OK) { |
| 11892 | Jim_SetResultFormatted(interp, |
| 11893 | "can't read \"%#s(%#s)\": %s array", varObjPtr, keyObjPtr, |
| 11894 | ret < 0 ? "variable isn't" : "no such element in"); |
| 11895 | } |
| 11896 | else if ((flags & JIM_UNSHARED) && Jim_IsShared(dictObjPtr)) { |
| 11897 | |
| 11898 | Jim_SetVariable(interp, varObjPtr, Jim_DuplicateObj(interp, dictObjPtr)); |
| 11899 | } |
| 11900 | |
| 11901 | return resObjPtr; |
| 11902 | } |
| 11903 | |
| 11904 | |
| 11905 | static Jim_Obj *JimDictSugarGet(Jim_Interp *interp, Jim_Obj *objPtr, int flags) |
| 11906 | { |
| 11907 | SetDictSubstFromAny(interp, objPtr); |
| 11908 | |
| 11909 | return JimDictExpandArrayVariable(interp, |
| 11910 | objPtr->internalRep.dictSubstValue.varNameObjPtr, |
| 11911 | objPtr->internalRep.dictSubstValue.indexObjPtr, flags); |
| 11912 | } |
| 11913 | |
| 11914 | |
| 11915 | |
| 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; |
| 11927 | |
| 11928 | Jim_IncrRefCount(dupPtr->internalRep.dictSubstValue.varNameObjPtr); |
| 11929 | Jim_IncrRefCount(dupPtr->internalRep.dictSubstValue.indexObjPtr); |
| 11930 | } |
| 11931 | |
| 11932 | |
| 11933 | static void SetDictSubstFromAny(Jim_Interp *interp, Jim_Obj *objPtr) |
| 11934 | { |
| 11935 | if (objPtr->typePtr != &dictSubstObjType) { |
| 11936 | Jim_Obj *varObjPtr, *keyObjPtr; |
| 11937 | |
| 11938 | if (objPtr->typePtr == &interpolatedObjType) { |
| 11939 | |
| 11940 | |
| 11941 | varObjPtr = objPtr->internalRep.dictSubstValue.varNameObjPtr; |
| 11942 | keyObjPtr = objPtr->internalRep.dictSubstValue.indexObjPtr; |
| 11943 | |
| 11944 | Jim_IncrRefCount(varObjPtr); |
| 11945 | Jim_IncrRefCount(keyObjPtr); |
| 11946 | } |
| 11947 | else { |
| 11948 | JimDictSugarParseVarKey(interp, objPtr, &varObjPtr, &keyObjPtr); |
| 11949 | } |
| 11950 | |
| 11951 | Jim_FreeIntRep(interp, objPtr); |
| 11952 | objPtr->typePtr = &dictSubstObjType; |
| 11953 | objPtr->internalRep.dictSubstValue.varNameObjPtr = varObjPtr; |
| 11954 | objPtr->internalRep.dictSubstValue.indexObjPtr = keyObjPtr; |
| 11955 | } |
| 11956 | } |
| 11957 | |
| 11958 | static Jim_Obj *JimExpandDictSugar(Jim_Interp *interp, Jim_Obj *objPtr) |
| 11959 | { |
| 11960 | Jim_Obj *resObjPtr = NULL; |
| 11961 | Jim_Obj *substKeyObjPtr = NULL; |
| 11962 | |
| 11963 | if (interp->safeexpr) { |
| 11964 | return objPtr; |
| 11965 | } |
| 11966 | |
| 11967 | SetDictSubstFromAny(interp, objPtr); |
| 11968 | |
| 11969 | if (Jim_SubstObj(interp, objPtr->internalRep.dictSubstValue.indexObjPtr, |
| 11970 | &substKeyObjPtr, JIM_NONE) |
| 11971 | != JIM_OK) { |
| 11972 | return NULL; |
| 11973 | } |
| 11974 | Jim_IncrRefCount(substKeyObjPtr); |
| 11975 | resObjPtr = |
| 11976 | JimDictExpandArrayVariable(interp, objPtr->internalRep.dictSubstValue.varNameObjPtr, |
| 11977 | substKeyObjPtr, 0); |
| 11978 | Jim_DecrRefCount(interp, substKeyObjPtr); |
| 11979 | |
| 11980 | return resObjPtr; |
| 11981 | } |
| 11982 | |
| 11983 | |
| 11984 | static Jim_CallFrame *JimCreateCallFrame(Jim_Interp *interp, Jim_CallFrame *parent, Jim_Obj *nsObj) |
| 11985 | { |
| 11986 | Jim_CallFrame *cf; |
| 11987 | |
| 11988 | if (interp->freeFramesList) { |
| 11989 | cf = interp->freeFramesList; |
| 11990 | interp->freeFramesList = cf->next; |
| 11991 | |
| 11992 | cf->argv = NULL; |
| 11993 | cf->argc = 0; |
| 11994 | cf->procArgsObjPtr = NULL; |
| 11995 | cf->procBodyObjPtr = NULL; |
| 11996 | cf->next = NULL; |
| 11997 | cf->staticVars = NULL; |
| 11998 | cf->localCommands = NULL; |
| 11999 | cf->tailcallObj = NULL; |
| 12000 | cf->tailcallCmd = NULL; |
| 12001 | } |
| 12002 | else { |
| 12003 | cf = Jim_Alloc(sizeof(*cf)); |
| 12004 | memset(cf, 0, sizeof(*cf)); |
| 12005 | |
| 12006 | Jim_InitHashTable(&cf->vars, &JimVariablesHashTableType, interp); |
| 12007 | } |
| 12008 | |
| 12009 | cf->id = interp->callFrameEpoch++; |
| 12010 | cf->parent = parent; |
| 12011 | cf->level = parent ? parent->level + 1 : 0; |
| 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) { |
| 12023 | Jim_Obj *cmdNameObj; |
| 12024 | |
| 12025 | while ((cmdNameObj = Jim_StackPop(localCommands)) != NULL) { |
| 12026 | Jim_HashTable *ht = &interp->commands; |
| 12027 | Jim_HashEntry *he = Jim_FindHashEntry(ht, cmdNameObj); |
| 12028 | if (he) { |
| 12029 | Jim_Cmd *cmd = Jim_GetHashEntryVal(he); |
| 12030 | if (cmd->prevCmd) { |
| 12031 | Jim_Cmd *prevCmd = cmd->prevCmd; |
| 12032 | cmd->prevCmd = NULL; |
| 12033 | |
| 12034 | |
| 12035 | JimDecrCmdRefCount(interp, cmd); |
| 12036 | |
| 12037 | |
| 12038 | Jim_SetHashVal(ht, he, prevCmd); |
| 12039 | } |
| 12040 | else { |
| 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 | |
| 12052 | static int JimInvokeDefer(Jim_Interp *interp, int retcode) |
| 12053 | { |
| 12054 | Jim_Obj *objPtr; |
| 12055 | |
| 12056 | |
| 12057 | if (JimFindVariable(&interp->framePtr->vars, interp->defer) == NULL) { |
| 12058 | return retcode; |
| 12059 | } |
| 12060 | objPtr = Jim_GetVariable(interp, interp->defer, JIM_NONE); |
| 12061 | |
| 12062 | if (objPtr) { |
| 12063 | int ret = JIM_OK; |
| 12064 | int i; |
| 12065 | int listLen = Jim_ListLength(interp, objPtr); |
| 12066 | Jim_Obj *resultObjPtr; |
| 12067 | |
| 12068 | Jim_IncrRefCount(objPtr); |
| 12069 | |
| 12070 | resultObjPtr = Jim_GetResult(interp); |
| 12071 | Jim_IncrRefCount(resultObjPtr); |
| 12072 | Jim_SetEmptyResult(interp); |
| 12073 | |
| 12074 | |
| 12075 | for (i = listLen; i > 0; i--) { |
| 12076 | |
| 12077 | Jim_Obj *scriptObjPtr = Jim_ListGetIndex(interp, objPtr, i - 1); |
| 12078 | ret = Jim_EvalObj(interp, scriptObjPtr); |
| 12079 | if (ret != JIM_OK) { |
| 12080 | break; |
| 12081 | } |
| 12082 | } |
| 12083 | |
| 12084 | if (ret == JIM_OK || retcode == JIM_ERR) { |
| 12085 | |
| 12086 | Jim_SetResult(interp, resultObjPtr); |
| 12087 | } |
| 12088 | else { |
| 12089 | retcode = ret; |
| 12090 | } |
| 12091 | |
| 12092 | Jim_DecrRefCount(interp, resultObjPtr); |
| 12093 | Jim_DecrRefCount(interp, objPtr); |
| 12094 | } |
| 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) |
| 12106 | Jim_DecrRefCount(interp, cf->procArgsObjPtr); |
| 12107 | if (cf->procBodyObjPtr) |
| 12108 | Jim_DecrRefCount(interp, cf->procBodyObjPtr); |
| 12109 | Jim_DecrRefCount(interp, cf->nsObj); |
| 12110 | if (action == JIM_FCF_FULL || cf->vars.size != JIM_HT_INITIAL_SIZE) |
| 12111 | Jim_FreeHashTable(&cf->vars); |
| 12112 | else { |
| 12113 | Jim_ClearHashTable(&cf->vars); |
| 12114 | } |
| 12115 | cf->next = interp->freeFramesList; |
| 12116 | interp->freeFramesList = cf; |
| 12117 | } |
| 12118 | |
| 12119 | |
| 12120 | |
| 12121 | int Jim_IsBigEndian(void) |
| 12122 | { |
| 12123 | union { |
| 12124 | unsigned short s; |
| 12125 | unsigned char c[2]; |
| 12126 | } uval = {0x0102}; |
| 12127 | |
| 12128 | return uval.c[0] == 1; |
| 12129 | } |
| 12130 | |
| 12131 | |
| 12132 | Jim_Interp *Jim_CreateInterp(void) |
| 12133 | { |
| 12134 | Jim_Interp *i = Jim_Alloc(sizeof(*i)); |
| 12135 | |
| 12136 | memset(i, 0, sizeof(*i)); |
| 12137 | |
| 12138 | i->maxCallFrameDepth = JIM_MAX_CALLFRAME_DEPTH; |
| 12139 | i->maxEvalDepth = JIM_MAX_EVAL_DEPTH; |
| 12140 | i->lastCollectTime = Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW); |
| 12141 | |
| 12142 | Jim_InitHashTable(&i->commands, &JimCommandsHashTableType, i); |
| 12143 | #ifdef JIM_REFERENCES |
| 12144 | Jim_InitHashTable(&i->references, &JimReferencesHashTableType, i); |
| 12145 | #endif |
| 12146 | Jim_InitHashTable(&i->assocData, &JimAssocDataHashTableType, i); |
| 12147 | Jim_InitHashTable(&i->packages, &JimPackageHashTableType, NULL); |
| 12148 | i->emptyObj = Jim_NewEmptyStringObj(i); |
| 12149 | i->trueObj = Jim_NewIntObj(i, 1); |
| 12150 | i->falseObj = Jim_NewIntObj(i, 0); |
| 12151 | i->framePtr = i->topFramePtr = JimCreateCallFrame(i, NULL, i->emptyObj); |
| 12152 | i->result = i->emptyObj; |
| 12153 | i->stackTrace = Jim_NewListObj(i, NULL, 0); |
| 12154 | i->unknown = Jim_NewStringObj(i, "unknown", -1); |
| 12155 | i->defer = Jim_NewStringObj(i, "jim::defer", -1); |
| 12156 | i->errorProc = i->emptyObj; |
| 12157 | i->nullScriptObj = Jim_NewEmptyStringObj(i); |
| 12158 | i->evalFrame = &i->topEvalFrame; |
| 12159 | i->currentFilenameObj = Jim_NewEmptyStringObj(i); |
| 12160 | Jim_IncrRefCount(i->emptyObj); |
| 12161 | Jim_IncrRefCount(i->result); |
| 12162 | Jim_IncrRefCount(i->stackTrace); |
| 12163 | Jim_IncrRefCount(i->unknown); |
| 12164 | Jim_IncrRefCount(i->defer); |
| 12165 | Jim_IncrRefCount(i->nullScriptObj); |
| 12166 | Jim_IncrRefCount(i->errorProc); |
| 12167 | Jim_IncrRefCount(i->trueObj); |
| 12168 | Jim_IncrRefCount(i->falseObj); |
| 12169 | Jim_IncrRefCount(i->currentFilenameObj); |
| 12170 | |
| 12171 | |
| 12172 | Jim_SetVariableStrWithStr(i, JIM_LIBPATH, TCL_LIBRARY); |
| 12173 | Jim_SetVariableStrWithStr(i, JIM_INTERACTIVE, "0"); |
| 12174 | |
| 12175 | Jim_SetVariableStrWithStr(i, "tcl_platform(engine)", "Jim"); |
| 12176 | Jim_SetVariableStrWithStr(i, "tcl_platform(os)", TCL_PLATFORM_OS); |
| 12177 | Jim_SetVariableStrWithStr(i, "tcl_platform(platform)", TCL_PLATFORM_PLATFORM); |
| 12178 | Jim_SetVariableStrWithStr(i, "tcl_platform(pathSeparator)", TCL_PLATFORM_PATH_SEPARATOR); |
| 12179 | Jim_SetVariableStrWithStr(i, "tcl_platform(byteOrder)", Jim_IsBigEndian() ? "bigEndian" : "littleEndian"); |
| 12180 | Jim_SetVariableStrWithStr(i, "tcl_platform(threaded)", "0"); |
| 12181 | Jim_SetVariableStrWithStr(i, "tcl_platform(bootstrap)", "0"); |
| 12182 | Jim_SetVariableStr(i, "tcl_platform(pointerSize)", Jim_NewIntObj(i, sizeof(void *))); |
| 12183 | Jim_SetVariableStr(i, "tcl_platform(wordSize)", Jim_NewIntObj(i, sizeof(jim_wide))); |
| 12184 | Jim_SetVariableStr(i, "tcl_platform(stackFormat)", Jim_NewIntObj(i, 4)); |
| 12185 | |
| 12186 | return i; |
| 12187 | } |
| 12188 | |
| 12189 | void Jim_FreeInterp(Jim_Interp *i) |
| 12190 | { |
| 12191 | Jim_CallFrame *cf, *cfx; |
| 12192 | |
| 12193 | Jim_Obj *objPtr, *nextObjPtr; |
| 12194 | |
| 12195 | i->quitting = 1; |
| 12196 | |
| 12197 | |
| 12198 | for (cf = i->framePtr; cf; cf = cfx) { |
| 12199 | |
| 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); |
| 12213 | Jim_DecrRefCount(i, i->errorProc); |
| 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); |
| 12224 | #endif |
| 12225 | Jim_FreeHashTable(&i->packages); |
| 12226 | Jim_Free(i->prngState); |
| 12227 | Jim_FreeHashTable(&i->assocData); |
| 12228 | if (i->traceCmdObj) { |
| 12229 | Jim_DecrRefCount(i, i->traceCmdObj); |
| 12230 | } |
| 12231 | |
| 12232 | #ifdef JIM_MAINTAINER |
| 12233 | if (i->liveList != NULL) { |
| 12234 | objPtr = i->liveList; |
| 12235 | |
| 12236 | printf("\n-------------------------------------\n"); |
| 12237 | printf("Objects still in the free list:\n"); |
| 12238 | while (objPtr) { |
| 12239 | const char *type = objPtr->typePtr ? objPtr->typePtr->name : "string"; |
| 12240 | Jim_String(objPtr); |
| 12241 | |
| 12242 | if (objPtr->bytes && strlen(objPtr->bytes) > 20) { |
| 12243 | printf("%p (%d) %-10s: '%.20s...'\n", |
| 12244 | (void *)objPtr, objPtr->refCount, type, objPtr->bytes); |
| 12245 | } |
| 12246 | else { |
| 12247 | printf("%p (%d) %-10s: '%s'\n", |
| 12248 | (void *)objPtr, objPtr->refCount, type, objPtr->bytes ? objPtr->bytes : "(null)"); |
| 12249 | } |
| 12250 | if (objPtr->typePtr == &sourceObjType) { |
| 12251 | printf("FILE %s LINE %d\n", |
| 12252 | Jim_String(objPtr->internalRep.sourceValue.fileNameObj), |
| 12253 | objPtr->internalRep.sourceValue.lineNumber); |
| 12254 | } |
| 12255 | objPtr = objPtr->nextObjPtr; |
| 12256 | } |
| 12257 | printf("-------------------------------------\n\n"); |
| 12258 | JimPanic((1, "Live list non empty freeing the interpreter! Leak?")); |
| 12259 | } |
| 12260 | #endif |
| 12261 | |
| 12262 | |
| 12263 | objPtr = i->freeList; |
| 12264 | while (objPtr) { |
| 12265 | nextObjPtr = objPtr->nextObjPtr; |
| 12266 | Jim_Free(objPtr); |
| 12267 | objPtr = nextObjPtr; |
| 12268 | } |
| 12269 | |
| 12270 | |
| 12271 | for (cf = i->freeFramesList; cf; cf = cfx) { |
| 12272 | cfx = cf->next; |
| 12273 | if (cf->vars.table) |
| 12274 | Jim_FreeHashTable(&cf->vars); |
| 12275 | Jim_Free(cf); |
| 12276 | } |
| 12277 | |
| 12278 | |
| 12279 | Jim_Free(i); |
| 12280 | } |
| 12281 | |
| 12282 | Jim_CallFrame *Jim_GetCallFrameByLevel(Jim_Interp *interp, Jim_Obj *levelObjPtr) |
| 12283 | { |
| 12284 | long level; |
| 12285 | const char *str; |
| 12286 | Jim_CallFrame *framePtr; |
| 12287 | |
| 12288 | if (levelObjPtr) { |
| 12289 | str = Jim_String(levelObjPtr); |
| 12290 | if (str[0] == '#') { |
| 12291 | char *endptr; |
| 12292 | |
| 12293 | level = jim_strtol(str + 1, &endptr); |
| 12294 | if (str[1] == '\0' || endptr[0] != '\0') { |
| 12295 | level = -1; |
| 12296 | } |
| 12297 | } |
| 12298 | else { |
| 12299 | if (Jim_GetLong(interp, levelObjPtr, &level) != JIM_OK || level < 0) { |
| 12300 | level = -1; |
| 12301 | } |
| 12302 | else { |
| 12303 | |
| 12304 | level = interp->framePtr->level - level; |
| 12305 | } |
| 12306 | } |
| 12307 | } |
| 12308 | else { |
| 12309 | str = "1"; |
| 12310 | level = interp->framePtr->level - 1; |
| 12311 | } |
| 12312 | |
| 12313 | if (level == 0) { |
| 12314 | return interp->topFramePtr; |
| 12315 | } |
| 12316 | if (level > 0) { |
| 12317 | |
| 12318 | for (framePtr = interp->framePtr; framePtr; framePtr = framePtr->parent) { |
| 12319 | if (framePtr->level == level) { |
| 12320 | return framePtr; |
| 12321 | } |
| 12322 | } |
| 12323 | } |
| 12324 | |
| 12325 | Jim_SetResultFormatted(interp, "bad level \"%s\"", str); |
| 12326 | return NULL; |
| 12327 | } |
| 12328 | |
| 12329 | static Jim_CallFrame *JimGetCallFrameByInteger(Jim_Interp *interp, long level) |
| 12330 | { |
| 12331 | Jim_CallFrame *framePtr; |
| 12332 | |
| 12333 | if (level == 0) { |
| 12334 | return interp->framePtr; |
| 12335 | } |
| 12336 | |
| 12337 | if (level < 0) { |
| 12338 | |
| 12339 | level = interp->framePtr->level + level; |
| 12340 | } |
| 12341 | |
| 12342 | if (level > 0) { |
| 12343 | |
| 12344 | for (framePtr = interp->framePtr; framePtr; framePtr = framePtr->parent) { |
| 12345 | if (framePtr->level == level) { |
| 12346 | return framePtr; |
| 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 | |
| 12358 | if (proclevel == 0) { |
| 12359 | return interp->evalFrame; |
| 12360 | } |
| 12361 | |
| 12362 | if (proclevel < 0) { |
| 12363 | |
| 12364 | proclevel = interp->procLevel + proclevel; |
| 12365 | } |
| 12366 | |
| 12367 | if (proclevel >= 0) { |
| 12368 | |
| 12369 | for (evalFrame = interp->evalFrame; evalFrame; evalFrame = evalFrame->parent) { |
| 12370 | if (evalFrame->procLevel == proclevel) { |
| 12371 | return evalFrame; |
| 12372 | } |
| 12373 | } |
| 12374 | } |
| 12375 | return NULL; |
| 12376 | } |
| 12377 | |
| 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; |
| 12389 | } |
| 12390 | } |
| 12391 | return NULL; |
| 12392 | } |
| 12393 | |
| 12394 | static void JimAddStackFrame(Jim_Interp *interp, Jim_EvalFrame *frame, Jim_Obj *listObj) |
| 12395 | { |
| 12396 | Jim_Obj *procNameObj = JimProcForEvalFrame(interp, frame); |
| 12397 | Jim_Obj *fileNameObj = interp->emptyObj; |
| 12398 | int linenr = 1; |
| 12399 | |
| 12400 | if (frame->scriptObj) { |
| 12401 | ScriptObj *script = JimGetScript(interp, frame->scriptObj); |
| 12402 | fileNameObj = script->fileNameObj; |
| 12403 | linenr = script->linenr; |
| 12404 | } |
| 12405 | |
| 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); |
| 12431 | Jim_ListAppendElement(interp, stackTrace, script->fileNameObj); |
| 12432 | Jim_ListAppendElement(interp, stackTrace, Jim_NewIntObj(interp, script->linenr)); |
| 12433 | Jim_ListAppendElement(interp, stackTrace, interp->emptyObj); |
| 12434 | } |
| 12435 | else { |
| 12436 | for (i = 0; i <= interp->procLevel; i++) { |
| 12437 | Jim_EvalFrame *frame = JimGetEvalFrameByProcLevel(interp, -i); |
| 12438 | if (frame) { |
| 12439 | JimAddStackFrame(interp, frame, stackTrace); |
| 12440 | } |
| 12441 | } |
| 12442 | } |
| 12443 | JimSetStackTrace(interp, stackTrace); |
| 12444 | } |
| 12445 | } |
| 12446 | |
| 12447 | int Jim_SetAssocData(Jim_Interp *interp, const char *key, Jim_InterpDeleteProc * delProc, |
| 12448 | void *data) |
| 12449 | { |
| 12450 | AssocDataValue *assocEntryPtr = (AssocDataValue *) Jim_Alloc(sizeof(AssocDataValue)); |
| 12451 | |
| 12452 | assocEntryPtr->delProc = delProc; |
| 12453 | assocEntryPtr->data = data; |
| 12454 | return Jim_AddHashEntry(&interp->assocData, key, assocEntryPtr); |
| 12455 | } |
| 12456 | |
| 12457 | void *Jim_GetAssocData(Jim_Interp *interp, const char *key) |
| 12458 | { |
| 12459 | Jim_HashEntry *entryPtr = Jim_FindHashEntry(&interp->assocData, key); |
| 12460 | |
| 12461 | if (entryPtr != NULL) { |
| 12462 | AssocDataValue *assocEntryPtr = Jim_GetHashEntryVal(entryPtr); |
| 12463 | return assocEntryPtr->data; |
| 12464 | } |
| 12465 | return NULL; |
| 12466 | } |
| 12467 | |
| 12468 | int Jim_DeleteAssocData(Jim_Interp *interp, const char *key) |
| 12469 | { |
| 12470 | return Jim_DeleteHashEntry(&interp->assocData, key); |
| 12471 | } |
| 12472 | |
| 12473 | int Jim_GetExitCode(Jim_Interp *interp) |
| 12474 | { |
| 12475 | return interp->exitCode; |
| 12476 | } |
| 12477 | |
| 12478 | static void UpdateStringOfInt(struct Jim_Obj *objPtr); |
| 12479 | static int SetIntFromAny(Jim_Interp *interp, Jim_Obj *objPtr, int flags); |
| 12480 | |
| 12481 | static const Jim_ObjType intObjType = { |
| 12482 | "int", |
| 12483 | NULL, |
| 12484 | NULL, |
| 12485 | UpdateStringOfInt, |
| 12486 | JIM_TYPE_NONE, |
| 12487 | }; |
| 12488 | |
| 12489 | static const Jim_ObjType coercedDoubleObjType = { |
| 12490 | "coerced-double", |
| 12491 | NULL, |
| 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); |
| 12503 | int pos = 0; |
| 12504 | |
| 12505 | if (wideValue == 0) { |
| 12506 | buf[pos++] = '0'; |
| 12507 | } |
| 12508 | else { |
| 12509 | char tmp[JIM_INTEGER_SPACE]; |
| 12510 | int num = 0; |
| 12511 | int i; |
| 12512 | |
| 12513 | if (wideValue < 0) { |
| 12514 | buf[pos++] = '-'; |
| 12515 | i = wideValue % 10; |
| 12516 | tmp[num++] = (i > 0) ? (10 - i) : -i; |
| 12517 | wideValue /= -10; |
| 12518 | } |
| 12519 | |
| 12520 | while (wideValue) { |
| 12521 | tmp[num++] = wideValue % 10; |
| 12522 | wideValue /= 10; |
| 12523 | } |
| 12524 | |
| 12525 | for (i = 0; i < num; i++) { |
| 12526 | buf[pos++] = '0' + tmp[num - i - 1]; |
| 12527 | } |
| 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; |
| 12539 | |
| 12540 | if (objPtr->typePtr == &coercedDoubleObjType) { |
| 12541 | |
| 12542 | objPtr->typePtr = &intObjType; |
| 12543 | return JIM_OK; |
| 12544 | } |
| 12545 | |
| 12546 | |
| 12547 | str = Jim_String(objPtr); |
| 12548 | |
| 12549 | if (Jim_StringToWide(str, &wideValue, 0) != JIM_OK) { |
| 12550 | if (flags & JIM_ERRMSG) { |
| 12551 | Jim_SetResultFormatted(interp, "expected integer but got \"%#s\"", objPtr); |
| 12552 | } |
| 12553 | return JIM_ERR; |
| 12554 | } |
| 12555 | if ((wideValue == JIM_WIDE_MIN || wideValue == JIM_WIDE_MAX) && errno == ERANGE) { |
| 12556 | Jim_SetResultString(interp, "Integer value too big to be represented", -1); |
| 12557 | return JIM_ERR; |
| 12558 | } |
| 12559 | |
| 12560 | Jim_FreeIntRep(interp, objPtr); |
| 12561 | objPtr->typePtr = &intObjType; |
| 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 |
| 12573 | |
| 12574 | int Jim_GetWide(Jim_Interp *interp, Jim_Obj *objPtr, jim_wide * widePtr) |
| 12575 | { |
| 12576 | if (objPtr->typePtr != &intObjType && SetIntFromAny(interp, objPtr, JIM_ERRMSG) == JIM_ERR) |
| 12577 | return JIM_ERR; |
| 12578 | *widePtr = JimWideValue(objPtr); |
| 12579 | return JIM_OK; |
| 12580 | } |
| 12581 | |
| 12582 | int Jim_GetWideExpr(Jim_Interp *interp, Jim_Obj *objPtr, jim_wide * widePtr) |
| 12583 | { |
| 12584 | int ret = JIM_OK; |
| 12585 | |
| 12586 | if (objPtr->typePtr == &sourceObjType || objPtr->typePtr == NULL) { |
| 12587 | SetIntFromAny(interp, objPtr, 0); |
| 12588 | } |
| 12589 | if (objPtr->typePtr == &intObjType) { |
| 12590 | *widePtr = JimWideValue(objPtr); |
| 12591 | } |
| 12592 | else { |
| 12593 | JimPanic((interp->safeexpr, "interp->safeexpr is set")); |
| 12594 | interp->safeexpr++; |
| 12595 | ret = Jim_EvalExpression(interp, objPtr); |
| 12596 | interp->safeexpr--; |
| 12597 | |
| 12598 | if (ret == JIM_OK) { |
| 12599 | ret = Jim_GetWide(interp, Jim_GetResult(interp), widePtr); |
| 12600 | } |
| 12601 | if (ret != JIM_OK) { |
| 12602 | Jim_SetResultFormatted(interp, "expected integer expression but got \"%#s\"", objPtr); |
| 12603 | } |
| 12604 | } |
| 12605 | return ret; |
| 12606 | } |
| 12607 | |
| 12608 | |
| 12609 | static int JimGetWideNoErr(Jim_Interp *interp, Jim_Obj *objPtr, jim_wide * widePtr) |
| 12610 | { |
| 12611 | if (objPtr->typePtr != &intObjType && SetIntFromAny(interp, objPtr, JIM_NONE) == JIM_ERR) |
| 12612 | return JIM_ERR; |
| 12613 | *widePtr = JimWideValue(objPtr); |
| 12614 | return JIM_OK; |
| 12615 | } |
| 12616 | |
| 12617 | int Jim_GetLong(Jim_Interp *interp, Jim_Obj *objPtr, long *longPtr) |
| 12618 | { |
| 12619 | jim_wide wideValue; |
| 12620 | int retval; |
| 12621 | |
| 12622 | retval = Jim_GetWide(interp, objPtr, &wideValue); |
| 12623 | if (retval == JIM_OK) { |
| 12624 | *longPtr = (long)wideValue; |
| 12625 | return JIM_OK; |
| 12626 | } |
| 12627 | return JIM_ERR; |
| 12628 | } |
| 12629 | |
| 12630 | Jim_Obj *Jim_NewIntObj(Jim_Interp *interp, jim_wide wideValue) |
| 12631 | { |
| 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 |
| 12643 | |
| 12644 | static void UpdateStringOfDouble(struct Jim_Obj *objPtr); |
| 12645 | static int SetDoubleFromAny(Jim_Interp *interp, Jim_Obj *objPtr); |
| 12646 | |
| 12647 | static const Jim_ObjType doubleObjType = { |
| 12648 | "double", |
| 12649 | NULL, |
| 12650 | NULL, |
| 12651 | UpdateStringOfDouble, |
| 12652 | JIM_TYPE_NONE, |
| 12653 | }; |
| 12654 | |
| 12655 | #if !HAVE_DECL_ISNAN |
| 12656 | #undef isnan |
| 12657 | #define isnan(X) ((X) != (X)) |
| 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 | |
| 12669 | if (isnan(value)) { |
| 12670 | JimSetStringBytes(objPtr, "NaN"); |
| 12671 | return; |
| 12672 | } |
| 12673 | if (isinf(value)) { |
| 12674 | if (value < 0) { |
| 12675 | JimSetStringBytes(objPtr, "-Inf"); |
| 12676 | } |
| 12677 | else { |
| 12678 | JimSetStringBytes(objPtr, "Inf"); |
| 12679 | } |
| 12680 | return; |
| 12681 | } |
| 12682 | { |
| 12683 | char buf[JIM_DOUBLE_SPACE + 1]; |
| 12684 | int i; |
| 12685 | int len = sprintf(buf, "%.12g", value); |
| 12686 | |
| 12687 | |
| 12688 | for (i = 0; i < len; i++) { |
| 12689 | if (buf[i] == '.' || buf[i] == 'e') { |
| 12690 | #if defined(JIM_SPRINTF_DOUBLE_NEEDS_FIX) |
| 12691 | char *e = strchr(buf, 'e'); |
| 12692 | if (e && (e[1] == '-' || e[1] == '+') && e[2] == '0') { |
| 12693 | |
| 12694 | e += 2; |
| 12695 | memmove(e, e + 1, len - (e - buf)); |
| 12696 | } |
| 12697 | #endif |
| 12698 | break; |
| 12699 | } |
| 12700 | } |
| 12701 | if (buf[i] == '\0') { |
| 12702 | buf[i++] = '.'; |
| 12703 | buf[i++] = '0'; |
| 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; |
| 12715 | const char *str; |
| 12716 | |
| 12717 | #ifdef HAVE_LONG_LONG |
| 12718 | |
| 12719 | #define MIN_INT_IN_DOUBLE -(1LL << 53) |
| 12720 | #define MAX_INT_IN_DOUBLE -(MIN_INT_IN_DOUBLE + 1) |
| 12721 | |
| 12722 | if (objPtr->typePtr == &intObjType |
| 12723 | && JimWideValue(objPtr) >= MIN_INT_IN_DOUBLE |
| 12724 | && JimWideValue(objPtr) <= MAX_INT_IN_DOUBLE) { |
| 12725 | |
| 12726 | |
| 12727 | objPtr->typePtr = &coercedDoubleObjType; |
| 12728 | return JIM_OK; |
| 12729 | } |
| 12730 | #endif |
| 12731 | str = Jim_String(objPtr); |
| 12732 | |
| 12733 | if (Jim_StringToWide(str, &wideValue, 10) == JIM_OK) { |
| 12734 | |
| 12735 | Jim_FreeIntRep(interp, objPtr); |
| 12736 | objPtr->typePtr = &coercedDoubleObjType; |
| 12737 | objPtr->internalRep.wideValue = wideValue; |
| 12738 | return JIM_OK; |
| 12739 | } |
| 12740 | else { |
| 12741 | |
| 12742 | if (Jim_StringToDouble(str, &doubleValue) != JIM_OK) { |
| 12743 | Jim_SetResultFormatted(interp, "expected floating-point number but got \"%#s\"", objPtr); |
| 12744 | return JIM_ERR; |
| 12745 | } |
| 12746 | |
| 12747 | Jim_FreeIntRep(interp, objPtr); |
| 12748 | } |
| 12749 | objPtr->typePtr = &doubleObjType; |
| 12750 | objPtr->internalRep.doubleValue = doubleValue; |
| 12751 | return JIM_OK; |
| 12752 | } |
| 12753 | |
| 12754 | int Jim_GetDouble(Jim_Interp *interp, Jim_Obj *objPtr, double *doublePtr) |
| 12755 | { |
| 12756 | if (objPtr->typePtr == &coercedDoubleObjType) { |
| 12757 | *doublePtr = JimWideValue(objPtr); |
| 12758 | return JIM_OK; |
| 12759 | } |
| 12760 | if (objPtr->typePtr != &doubleObjType && SetDoubleFromAny(interp, objPtr) == JIM_ERR) |
| 12761 | return JIM_ERR; |
| 12762 | |
| 12763 | if (objPtr->typePtr == &coercedDoubleObjType) { |
| 12764 | *doublePtr = JimWideValue(objPtr); |
| 12765 | } |
| 12766 | else { |
| 12767 | *doublePtr = objPtr->internalRep.doubleValue; |
| 12768 | } |
| 12769 | return JIM_OK; |
| 12770 | } |
| 12771 | |
| 12772 | Jim_Obj *Jim_NewDoubleObj(Jim_Interp *interp, double doubleValue) |
| 12773 | { |
| 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); |
| 12785 | |
| 12786 | int Jim_GetBoolean(Jim_Interp *interp, Jim_Obj *objPtr, int * booleanPtr) |
| 12787 | { |
| 12788 | if (objPtr->typePtr != &intObjType && SetBooleanFromAny(interp, objPtr, JIM_ERRMSG) == JIM_ERR) |
| 12789 | return JIM_ERR; |
| 12790 | *booleanPtr = (int) JimWideValue(objPtr); |
| 12791 | return JIM_OK; |
| 12792 | } |
| 12793 | |
| 12794 | static const char * const jim_true_false_strings[8] = { |
| 12795 | "1", "true", "yes", "on", |
| 12796 | "0", "false", "no", "off" |
| 12797 | }; |
| 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)); |
| 12809 | if (index < 0) { |
| 12810 | if (flags & JIM_ERRMSG) { |
| 12811 | Jim_SetResultFormatted(interp, "expected boolean but got \"%#s\"", objPtr); |
| 12812 | } |
| 12813 | return JIM_ERR; |
| 12814 | } |
| 12815 | |
| 12816 | |
| 12817 | Jim_FreeIntRep(interp, objPtr); |
| 12818 | objPtr->typePtr = &intObjType; |
| 12819 | |
| 12820 | objPtr->internalRep.wideValue = index < 4 ? 1 : 0; |
| 12821 | return JIM_OK; |
| 12822 | } |
| 12823 | |
| 12824 | static void ListInsertElements(Jim_Obj *listPtr, int idx, int elemc, Jim_Obj *const *elemVec); |
| 12825 | static void ListAppendElement(Jim_Obj *listPtr, Jim_Obj *objPtr); |
| 12826 | static void FreeListInternalRep(Jim_Interp *interp, Jim_Obj *objPtr); |
| 12827 | static void DupListInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr); |
| 12828 | static void UpdateStringOfList(struct Jim_Obj *objPtr); |
| 12829 | static int SetListFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr); |
| 12830 | |
| 12831 | static const Jim_ObjType listObjType = { |
| 12832 | "list", |
| 12833 | FreeListInternalRep, |
| 12834 | DupListInternalRep, |
| 12835 | UpdateStringOfList, |
| 12836 | JIM_TYPE_NONE, |
| 12837 | }; |
| 12838 | |
| 12839 | void FreeListInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) |
| 12840 | { |
| 12841 | int i; |
| 12842 | |
| 12843 | for (i = 0; i < objPtr->internalRep.listValue.len; i++) { |
| 12844 | Jim_DecrRefCount(interp, objPtr->internalRep.listValue.ele[i]); |
| 12845 | } |
| 12846 | Jim_Free(objPtr->internalRep.listValue.ele); |
| 12847 | } |
| 12848 | |
| 12849 | void DupListInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) |
| 12850 | { |
| 12851 | int i; |
| 12852 | |
| 12853 | JIM_NOTUSED(interp); |
| 12854 | |
| 12855 | dupPtr->internalRep.listValue.len = srcPtr->internalRep.listValue.len; |
| 12856 | dupPtr->internalRep.listValue.maxLen = srcPtr->internalRep.listValue.maxLen; |
| 12857 | dupPtr->internalRep.listValue.ele = |
| 12858 | Jim_Alloc(sizeof(Jim_Obj *) * srcPtr->internalRep.listValue.maxLen); |
| 12859 | memcpy(dupPtr->internalRep.listValue.ele, srcPtr->internalRep.listValue.ele, |
| 12860 | sizeof(Jim_Obj *) * srcPtr->internalRep.listValue.len); |
| 12861 | for (i = 0; i < dupPtr->internalRep.listValue.len; i++) { |
| 12862 | Jim_IncrRefCount(dupPtr->internalRep.listValue.ele[i]); |
| 12863 | } |
| 12864 | dupPtr->typePtr = &listObjType; |
| 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 | |
| 12876 | if (len == 0) |
| 12877 | return JIM_ELESTR_BRACE; |
| 12878 | if (s[0] == '"' || s[0] == '{') { |
| 12879 | trySimple = 0; |
| 12880 | goto testbrace; |
| 12881 | } |
| 12882 | for (i = 0; i < len; i++) { |
| 12883 | switch (s[i]) { |
| 12884 | case ' ': |
| 12885 | case '$': |
| 12886 | case '"': |
| 12887 | case '[': |
| 12888 | case ']': |
| 12889 | case ';': |
| 12890 | case '\\': |
| 12891 | case '\r': |
| 12892 | case '\n': |
| 12893 | case '\t': |
| 12894 | case '\f': |
| 12895 | case '\v': |
| 12896 | trySimple = 0; |
| 12897 | |
| 12898 | case '{': |
| 12899 | case '}': |
| 12900 | goto testbrace; |
| 12901 | } |
| 12902 | } |
| 12903 | return JIM_ELESTR_SIMPLE; |
| 12904 | |
| 12905 | testbrace: |
| 12906 | |
| 12907 | if (s[len - 1] == '\\') |
| 12908 | return JIM_ELESTR_QUOTE; |
| 12909 | level = 0; |
| 12910 | blevel = 0; |
| 12911 | for (i = 0; i < len; i++) { |
| 12912 | switch (s[i]) { |
| 12913 | case '{': |
| 12914 | level++; |
| 12915 | break; |
| 12916 | case '}': |
| 12917 | level--; |
| 12918 | if (level < 0) |
| 12919 | return JIM_ELESTR_QUOTE; |
| 12920 | break; |
| 12921 | case '[': |
| 12922 | blevel++; |
| 12923 | break; |
| 12924 | case ']': |
| 12925 | blevel--; |
| 12926 | break; |
| 12927 | case '\\': |
| 12928 | if (s[i + 1] == '\n') |
| 12929 | return JIM_ELESTR_QUOTE; |
| 12930 | else if (s[i + 1] != '\0') |
| 12931 | i++; |
| 12932 | break; |
| 12933 | } |
| 12934 | } |
| 12935 | if (blevel < 0) { |
| 12936 | return JIM_ELESTR_QUOTE; |
| 12937 | } |
| 12938 | |
| 12939 | if (level == 0) { |
| 12940 | if (!trySimple) |
| 12941 | return JIM_ELESTR_BRACE; |
| 12942 | for (i = 0; i < len; i++) { |
| 12943 | switch (s[i]) { |
| 12944 | case ' ': |
| 12945 | case '$': |
| 12946 | case '"': |
| 12947 | case '[': |
| 12948 | case ']': |
| 12949 | case ';': |
| 12950 | case '\\': |
| 12951 | case '\r': |
| 12952 | case '\n': |
| 12953 | case '\t': |
| 12954 | case '\f': |
| 12955 | case '\v': |
| 12956 | return JIM_ELESTR_BRACE; |
| 12957 | break; |
| 12958 | } |
| 12959 | } |
| 12960 | return JIM_ELESTR_SIMPLE; |
| 12961 | } |
| 12962 | return JIM_ELESTR_QUOTE; |
| 12963 | } |
| 12964 | |
| 12965 | static int BackslashQuoteString(const char *s, int len, char *q) |
| 12966 | { |
| 12967 | char *p = q; |
| 12968 | |
| 12969 | while (len--) { |
| 12970 | switch (*s) { |
| 12971 | case ' ': |
| 12972 | case '$': |
| 12973 | case '"': |
| 12974 | case '[': |
| 12975 | case ']': |
| 12976 | case '{': |
| 12977 | case '}': |
| 12978 | case ';': |
| 12979 | case '\\': |
| 12980 | *p++ = '\\'; |
| 12981 | *p++ = *s++; |
| 12982 | break; |
| 12983 | case '\n': |
| 12984 | *p++ = '\\'; |
| 12985 | *p++ = 'n'; |
| 12986 | s++; |
| 12987 | break; |
| 12988 | case '\r': |
| 12989 | *p++ = '\\'; |
| 12990 | *p++ = 'r'; |
| 12991 | s++; |
| 12992 | break; |
| 12993 | case '\t': |
| 12994 | *p++ = '\\'; |
| 12995 | *p++ = 't'; |
| 12996 | s++; |
| 12997 | break; |
| 12998 | case '\f': |
| 12999 | *p++ = '\\'; |
| 13000 | *p++ = 'f'; |
| 13001 | s++; |
| 13002 | break; |
| 13003 | case '\v': |
| 13004 | *p++ = '\\'; |
| 13005 | *p++ = 'v'; |
| 13006 | s++; |
| 13007 | break; |
| 13008 | default: |
| 13009 | *p++ = *s++; |
| 13010 | break; |
| 13011 | } |
| 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; |
| 13023 | const char *strRep; |
| 13024 | char *p; |
| 13025 | unsigned char *quotingType, staticQuoting[STATIC_QUOTING_LEN]; |
| 13026 | |
| 13027 | |
| 13028 | if (objc > STATIC_QUOTING_LEN) { |
| 13029 | quotingType = Jim_Alloc(objc); |
| 13030 | } |
| 13031 | else { |
| 13032 | quotingType = staticQuoting; |
| 13033 | } |
| 13034 | bufLen = 0; |
| 13035 | for (i = 0; i < objc; i++) { |
| 13036 | int len; |
| 13037 | |
| 13038 | strRep = Jim_GetString(objv[i], &len); |
| 13039 | quotingType[i] = ListElementQuotingType(strRep, len); |
| 13040 | switch (quotingType[i]) { |
| 13041 | case JIM_ELESTR_SIMPLE: |
| 13042 | if (i != 0 || strRep[0] != '#') { |
| 13043 | bufLen += len; |
| 13044 | break; |
| 13045 | } |
| 13046 | |
| 13047 | quotingType[i] = JIM_ELESTR_BRACE; |
| 13048 | |
| 13049 | case JIM_ELESTR_BRACE: |
| 13050 | bufLen += len + 2; |
| 13051 | break; |
| 13052 | case JIM_ELESTR_QUOTE: |
| 13053 | bufLen += len * 2; |
| 13054 | break; |
| 13055 | } |
| 13056 | bufLen++; |
| 13057 | } |
| 13058 | bufLen++; |
| 13059 | |
| 13060 | |
| 13061 | p = objPtr->bytes = Jim_Alloc(bufLen + 1); |
| 13062 | realLength = 0; |
| 13063 | for (i = 0; i < objc; i++) { |
| 13064 | int len, qlen; |
| 13065 | |
| 13066 | strRep = Jim_GetString(objv[i], &len); |
| 13067 | |
| 13068 | switch (quotingType[i]) { |
| 13069 | case JIM_ELESTR_SIMPLE: |
| 13070 | memcpy(p, strRep, len); |
| 13071 | p += len; |
| 13072 | realLength += len; |
| 13073 | break; |
| 13074 | case JIM_ELESTR_BRACE: |
| 13075 | *p++ = '{'; |
| 13076 | memcpy(p, strRep, len); |
| 13077 | p += len; |
| 13078 | *p++ = '}'; |
| 13079 | realLength += len + 2; |
| 13080 | break; |
| 13081 | case JIM_ELESTR_QUOTE: |
| 13082 | if (i == 0 && strRep[0] == '#') { |
| 13083 | *p++ = '\\'; |
| 13084 | realLength++; |
| 13085 | } |
| 13086 | qlen = BackslashQuoteString(strRep, len, p); |
| 13087 | p += qlen; |
| 13088 | realLength += qlen; |
| 13089 | break; |
| 13090 | } |
| 13091 | |
| 13092 | if (i + 1 != objc) { |
| 13093 | *p++ = ' '; |
| 13094 | realLength++; |
| 13095 | } |
| 13096 | } |
| 13097 | *p = '\0'; |
| 13098 | objPtr->length = realLength; |
| 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; |
| 13116 | int strLen; |
| 13117 | Jim_Obj *fileNameObj; |
| 13118 | int linenr; |
| 13119 | |
| 13120 | if (objPtr->typePtr == &listObjType) { |
| 13121 | return JIM_OK; |
| 13122 | } |
| 13123 | |
| 13124 | |
| 13125 | if (Jim_IsDict(objPtr) && objPtr->bytes == NULL) { |
| 13126 | Jim_Dict *dict = objPtr->internalRep.dictValue; |
| 13127 | |
| 13128 | |
| 13129 | objPtr->typePtr = &listObjType; |
| 13130 | objPtr->internalRep.listValue.len = dict->len; |
| 13131 | objPtr->internalRep.listValue.maxLen = dict->maxLen; |
| 13132 | objPtr->internalRep.listValue.ele = dict->table; |
| 13133 | |
| 13134 | |
| 13135 | Jim_Free(dict->ht); |
| 13136 | |
| 13137 | |
| 13138 | Jim_Free(dict); |
| 13139 | return JIM_OK; |
| 13140 | } |
| 13141 | |
| 13142 | |
| 13143 | fileNameObj = Jim_GetSourceInfo(interp, objPtr, &linenr); |
| 13144 | Jim_IncrRefCount(fileNameObj); |
| 13145 | |
| 13146 | |
| 13147 | str = Jim_GetString(objPtr, &strLen); |
| 13148 | |
| 13149 | Jim_FreeIntRep(interp, objPtr); |
| 13150 | objPtr->typePtr = &listObjType; |
| 13151 | objPtr->internalRep.listValue.len = 0; |
| 13152 | objPtr->internalRep.listValue.maxLen = 0; |
| 13153 | objPtr->internalRep.listValue.ele = NULL; |
| 13154 | |
| 13155 | |
| 13156 | if (strLen) { |
| 13157 | JimParserInit(&parser, str, strLen, linenr); |
| 13158 | while (!parser.eof) { |
| 13159 | Jim_Obj *elementPtr; |
| 13160 | |
| 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; |
| 13172 | } |
| 13173 | |
| 13174 | Jim_Obj *Jim_NewListObj(Jim_Interp *interp, Jim_Obj *const *elements, int len) |
| 13175 | { |
| 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) { |
| 13187 | ListInsertElements(objPtr, 0, len, elements); |
| 13188 | } |
| 13189 | |
| 13190 | return objPtr; |
| 13191 | } |
| 13192 | |
| 13193 | static void JimListGetElements(Jim_Interp *interp, Jim_Obj *listObj, int *listLen, |
| 13194 | Jim_Obj ***listVec) |
| 13195 | { |
| 13196 | *listLen = Jim_ListLength(interp, listObj); |
| 13197 | *listVec = listObj->internalRep.listValue.ele; |
| 13198 | } |
| 13199 | |
| 13200 | |
| 13201 | static int JimSign(jim_wide w) |
| 13202 | { |
| 13203 | if (w == 0) { |
| 13204 | return 0; |
| 13205 | } |
| 13206 | else if (w < 0) { |
| 13207 | return -1; |
| 13208 | } |
| 13209 | return 1; |
| 13210 | } |
| 13211 | |
| 13212 | |
| 13213 | struct lsort_info { |
| 13214 | jmp_buf jmpbuf; |
| 13215 | Jim_Obj *command; |
| 13216 | Jim_Interp *interp; |
| 13217 | enum { |
| 13218 | JIM_LSORT_ASCII, |
| 13219 | JIM_LSORT_NOCASE, |
| 13220 | JIM_LSORT_INTEGER, |
| 13221 | JIM_LSORT_REAL, |
| 13222 | JIM_LSORT_COMMAND, |
| 13223 | JIM_LSORT_DICT |
| 13224 | } type; |
| 13225 | int order; |
| 13226 | Jim_Obj **indexv; |
| 13227 | int indexc; |
| 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 | |
| 13239 | if (Jim_ListIndices(sort_info->interp, *lhsObj, sort_info->indexv, sort_info->indexc, &lObj, JIM_ERRMSG) != JIM_OK || |
| 13240 | Jim_ListIndices(sort_info->interp, *rhsObj, sort_info->indexv, sort_info->indexc, &rObj, JIM_ERRMSG) != JIM_OK) { |
| 13241 | longjmp(sort_info->jmpbuf, JIM_ERR); |
| 13242 | } |
| 13243 | return sort_info->subfn(&lObj, &rObj); |
| 13244 | } |
| 13245 | |
| 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); |
| 13263 | const char *right = Jim_String(*rhsObj); |
| 13264 | |
| 13265 | while (1) { |
| 13266 | if (isdigit(UCHAR(*left)) && isdigit(UCHAR(*right))) { |
| 13267 | |
| 13268 | jim_wide lint, rint; |
| 13269 | char *lend, *rend; |
| 13270 | lint = jim_strtoull(left, &lend); |
| 13271 | rint = jim_strtoull(right, &rend); |
| 13272 | if (lint != rint) { |
| 13273 | return JimSign(lint - rint) * sort_info->order; |
| 13274 | } |
| 13275 | if (lend -left != rend - right) { |
| 13276 | return JimSign((lend - left) - (rend - right)) * sort_info->order; |
| 13277 | } |
| 13278 | left = lend; |
| 13279 | right = rend; |
| 13280 | } |
| 13281 | else { |
| 13282 | int cl, cr; |
| 13283 | left += utf8_tounicode_case(left, &cl, 1); |
| 13284 | right += utf8_tounicode_case(right, &cr, 1); |
| 13285 | if (cl != cr) { |
| 13286 | return JimSign(cl - cr) * sort_info->order; |
| 13287 | } |
| 13288 | if (cl == 0) { |
| 13289 | |
| 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 | |
| 13301 | if (Jim_GetWide(sort_info->interp, *lhsObj, &lhs) != JIM_OK || |
| 13302 | Jim_GetWide(sort_info->interp, *rhsObj, &rhs) != JIM_OK) { |
| 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 | |
| 13314 | if (Jim_GetDouble(sort_info->interp, *lhsObj, &lhs) != JIM_OK || |
| 13315 | Jim_GetDouble(sort_info->interp, *rhsObj, &rhs) != JIM_OK) { |
| 13316 | longjmp(sort_info->jmpbuf, JIM_ERR); |
| 13317 | } |
| 13318 | if (lhs == rhs) { |
| 13319 | return 0; |
| 13320 | } |
| 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; |
| 13332 | |
| 13333 | jim_wide ret = 0; |
| 13334 | |
| 13335 | |
| 13336 | compare_script = Jim_DuplicateObj(sort_info->interp, sort_info->command); |
| 13337 | Jim_ListAppendElement(sort_info->interp, compare_script, *lhsObj); |
| 13338 | Jim_ListAppendElement(sort_info->interp, compare_script, *rhsObj); |
| 13339 | |
| 13340 | rc = Jim_EvalObj(sort_info->interp, compare_script); |
| 13341 | |
| 13342 | if (rc != JIM_OK || Jim_GetWide(sort_info->interp, Jim_GetResult(sort_info->interp), &ret) != JIM_OK) { |
| 13343 | longjmp(sort_info->jmpbuf, rc); |
| 13344 | } |
| 13345 | |
| 13346 | return JimSign(ret) * sort_info->order; |
| 13347 | } |
| 13348 | |
| 13349 | static void ListRemoveDuplicates(Jim_Obj *listObjPtr, int (*comp)(Jim_Obj **lhs, Jim_Obj **rhs)) |
| 13350 | { |
| 13351 | int src; |
| 13352 | int dst = 0; |
| 13353 | Jim_Obj **ele = listObjPtr->internalRep.listValue.ele; |
| 13354 | |
| 13355 | for (src = 1; src < listObjPtr->internalRep.listValue.len; src++) { |
| 13356 | if (comp(&ele[dst], &ele[src]) == 0) { |
| 13357 | |
| 13358 | Jim_DecrRefCount(sort_info->interp, ele[dst]); |
| 13359 | } |
| 13360 | else { |
| 13361 | |
| 13362 | dst++; |
| 13363 | } |
| 13364 | ele[dst] = ele[src]; |
| 13365 | } |
| 13366 | |
| 13367 | |
| 13368 | dst++; |
| 13369 | if (dst < listObjPtr->internalRep.listValue.len) { |
| 13370 | ele[dst] = ele[src]; |
| 13371 | } |
| 13372 | |
| 13373 | |
| 13374 | listObjPtr->internalRep.listValue.len = dst; |
| 13375 | } |
| 13376 | |
| 13377 | |
| 13378 | static int ListSortElements(Jim_Interp *interp, Jim_Obj *listObjPtr, struct lsort_info *info) |
| 13379 | { |
| 13380 | struct lsort_info *prev_info; |
| 13381 | |
| 13382 | typedef int (qsort_comparator) (const void *, const void *); |
| 13383 | int (*fn) (Jim_Obj **, Jim_Obj **); |
| 13384 | Jim_Obj **vector; |
| 13385 | int len; |
| 13386 | int rc; |
| 13387 | |
| 13388 | JimPanic((Jim_IsShared(listObjPtr), "ListSortElements called with shared object")); |
| 13389 | SetListFromAny(interp, listObjPtr); |
| 13390 | |
| 13391 | |
| 13392 | prev_info = sort_info; |
| 13393 | sort_info = info; |
| 13394 | |
| 13395 | vector = listObjPtr->internalRep.listValue.ele; |
| 13396 | len = listObjPtr->internalRep.listValue.len; |
| 13397 | switch (info->type) { |
| 13398 | case JIM_LSORT_ASCII: |
| 13399 | fn = ListSortString; |
| 13400 | break; |
| 13401 | case JIM_LSORT_NOCASE: |
| 13402 | fn = ListSortStringNoCase; |
| 13403 | break; |
| 13404 | case JIM_LSORT_INTEGER: |
| 13405 | fn = ListSortInteger; |
| 13406 | break; |
| 13407 | case JIM_LSORT_REAL: |
| 13408 | fn = ListSortReal; |
| 13409 | break; |
| 13410 | case JIM_LSORT_COMMAND: |
| 13411 | fn = ListSortCommand; |
| 13412 | break; |
| 13413 | case JIM_LSORT_DICT: |
| 13414 | fn = ListSortDict; |
| 13415 | break; |
| 13416 | default: |
| 13417 | fn = NULL; |
| 13418 | JimPanic((1, "ListSort called with invalid sort type")); |
| 13419 | return -1; |
| 13420 | } |
| 13421 | |
| 13422 | if (info->indexc) { |
| 13423 | |
| 13424 | info->subfn = fn; |
| 13425 | fn = ListSortIndexHelper; |
| 13426 | } |
| 13427 | |
| 13428 | if ((rc = setjmp(info->jmpbuf)) == 0) { |
| 13429 | qsort(vector, len, sizeof(Jim_Obj *), (qsort_comparator *) fn); |
| 13430 | |
| 13431 | if (info->unique && len > 1) { |
| 13432 | ListRemoveDuplicates(listObjPtr, fn); |
| 13433 | } |
| 13434 | |
| 13435 | Jim_InvalidateStringRep(listObjPtr); |
| 13436 | } |
| 13437 | sort_info = prev_info; |
| 13438 | |
| 13439 | return rc; |
| 13440 | } |
| 13441 | |
| 13442 | |
| 13443 | static void ListEnsureLength(Jim_Obj *listPtr, int idx) |
| 13444 | { |
| 13445 | assert(idx >= 0); |
| 13446 | if (idx >= listPtr->internalRep.listValue.maxLen) { |
| 13447 | if (idx < 4) { |
| 13448 | |
| 13449 | idx = 4; |
| 13450 | } |
| 13451 | listPtr->internalRep.listValue.ele = Jim_Realloc(listPtr->internalRep.listValue.ele, |
| 13452 | sizeof(Jim_Obj *) * idx); |
| 13453 | |
| 13454 | listPtr->internalRep.listValue.maxLen = idx; |
| 13455 | } |
| 13456 | } |
| 13457 | |
| 13458 | static void ListInsertElements(Jim_Obj *listPtr, int idx, int elemc, Jim_Obj *const *elemVec) |
| 13459 | { |
| 13460 | int currentLen = listPtr->internalRep.listValue.len; |
| 13461 | int requiredLen = currentLen + elemc; |
| 13462 | int i; |
| 13463 | Jim_Obj **point; |
| 13464 | |
| 13465 | if (elemc == 0) { |
| 13466 | |
| 13467 | return; |
| 13468 | } |
| 13469 | |
| 13470 | if (requiredLen > listPtr->internalRep.listValue.maxLen) { |
| 13471 | if (currentLen) { |
| 13472 | |
| 13473 | requiredLen *= 2; |
| 13474 | } |
| 13475 | ListEnsureLength(listPtr, requiredLen); |
| 13476 | } |
| 13477 | if (idx < 0) { |
| 13478 | idx = currentLen; |
| 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 | |
| 13490 | static void ListAppendElement(Jim_Obj *listPtr, Jim_Obj *objPtr) |
| 13491 | { |
| 13492 | ListInsertElements(listPtr, -1, 1, &objPtr); |
| 13493 | } |
| 13494 | |
| 13495 | static void ListAppendList(Jim_Obj *listPtr, Jim_Obj *appendListPtr) |
| 13496 | { |
| 13497 | ListInsertElements(listPtr, -1, |
| 13498 | appendListPtr->internalRep.listValue.len, appendListPtr->internalRep.listValue.ele); |
| 13499 | } |
| 13500 | |
| 13501 | void Jim_ListAppendElement(Jim_Interp *interp, Jim_Obj *listPtr, Jim_Obj *objPtr) |
| 13502 | { |
| 13503 | JimPanic((Jim_IsShared(listPtr), "Jim_ListAppendElement called with shared object")); |
| 13504 | SetListFromAny(interp, listPtr); |
| 13505 | Jim_InvalidateStringRep(listPtr); |
| 13506 | ListAppendElement(listPtr, objPtr); |
| 13507 | } |
| 13508 | |
| 13509 | void Jim_ListAppendList(Jim_Interp *interp, Jim_Obj *listPtr, Jim_Obj *appendListPtr) |
| 13510 | { |
| 13511 | JimPanic((Jim_IsShared(listPtr), "Jim_ListAppendList called with shared object")); |
| 13512 | SetListFromAny(interp, listPtr); |
| 13513 | SetListFromAny(interp, appendListPtr); |
| 13514 | Jim_InvalidateStringRep(listPtr); |
| 13515 | ListAppendList(listPtr, appendListPtr); |
| 13516 | } |
| 13517 | |
| 13518 | int Jim_ListLength(Jim_Interp *interp, Jim_Obj *objPtr) |
| 13519 | { |
| 13520 | SetListFromAny(interp, objPtr); |
| 13521 | return objPtr->internalRep.listValue.len; |
| 13522 | } |
| 13523 | |
| 13524 | void Jim_ListInsertElements(Jim_Interp *interp, Jim_Obj *listPtr, int idx, |
| 13525 | int objc, Jim_Obj *const *objVec) |
| 13526 | { |
| 13527 | JimPanic((Jim_IsShared(listPtr), "Jim_ListInsertElement called with shared object")); |
| 13528 | SetListFromAny(interp, listPtr); |
| 13529 | if (idx >= 0 && idx > listPtr->internalRep.listValue.len) |
| 13530 | idx = listPtr->internalRep.listValue.len; |
| 13531 | else if (idx < 0) |
| 13532 | idx = 0; |
| 13533 | Jim_InvalidateStringRep(listPtr); |
| 13534 | ListInsertElements(listPtr, idx, objc, objVec); |
| 13535 | } |
| 13536 | |
| 13537 | Jim_Obj *Jim_ListGetIndex(Jim_Interp *interp, Jim_Obj *listPtr, int idx) |
| 13538 | { |
| 13539 | SetListFromAny(interp, listPtr); |
| 13540 | if ((idx >= 0 && idx >= listPtr->internalRep.listValue.len) || |
| 13541 | (idx < 0 && (-idx - 1) >= listPtr->internalRep.listValue.len)) { |
| 13542 | return NULL; |
| 13543 | } |
| 13544 | if (idx < 0) |
| 13545 | idx = listPtr->internalRep.listValue.len + idx; |
| 13546 | return listPtr->internalRep.listValue.ele[idx]; |
| 13547 | } |
| 13548 | |
| 13549 | int Jim_ListIndex(Jim_Interp *interp, Jim_Obj *listPtr, int idx, Jim_Obj **objPtrPtr, int flags) |
| 13550 | { |
| 13551 | *objPtrPtr = Jim_ListGetIndex(interp, listPtr, idx); |
| 13552 | if (*objPtrPtr == NULL) { |
| 13553 | if (flags & JIM_ERRMSG) { |
| 13554 | Jim_SetResultString(interp, "list index out of range", -1); |
| 13555 | } |
| 13556 | return JIM_ERR; |
| 13557 | } |
| 13558 | return JIM_OK; |
| 13559 | } |
| 13560 | |
| 13561 | static int Jim_ListIndices(Jim_Interp *interp, Jim_Obj *listPtr, |
| 13562 | Jim_Obj *const *indexv, int indexc, Jim_Obj **resultObj, int flags) |
| 13563 | { |
| 13564 | int i; |
| 13565 | int static_idxes[5]; |
| 13566 | int *idxes = static_idxes; |
| 13567 | int ret = JIM_OK; |
| 13568 | |
| 13569 | if (indexc > sizeof(static_idxes) / sizeof(*static_idxes)) { |
| 13570 | idxes = Jim_Alloc(indexc * sizeof(*idxes)); |
| 13571 | } |
| 13572 | |
| 13573 | for (i = 0; i < indexc; i++) { |
| 13574 | ret = Jim_GetIndex(interp, indexv[i], &idxes[i]); |
| 13575 | if (ret != JIM_OK) { |
| 13576 | goto err; |
| 13577 | } |
| 13578 | } |
| 13579 | |
| 13580 | for (i = 0; i < indexc; i++) { |
| 13581 | Jim_Obj *objPtr = Jim_ListGetIndex(interp, listPtr, idxes[i]); |
| 13582 | if (!objPtr) { |
| 13583 | if (flags & JIM_ERRMSG) { |
| 13584 | if (idxes[i] < 0 || idxes[i] > Jim_ListLength(interp, listPtr)) { |
| 13585 | Jim_SetResultFormatted(interp, "index \"%#s\" out of range", indexv[i]); |
| 13586 | } |
| 13587 | else { |
| 13588 | Jim_SetResultFormatted(interp, "element %#s missing from sublist \"%#s\"", indexv[i], listPtr); |
| 13589 | } |
| 13590 | } |
| 13591 | return -1; |
| 13592 | } |
| 13593 | listPtr = objPtr; |
| 13594 | } |
| 13595 | *resultObj = listPtr; |
| 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); |
| 13607 | if ((idx >= 0 && idx >= listPtr->internalRep.listValue.len) || |
| 13608 | (idx < 0 && (-idx - 1) >= listPtr->internalRep.listValue.len)) { |
| 13609 | if (flags & JIM_ERRMSG) { |
| 13610 | Jim_SetResultString(interp, "list index out of range", -1); |
| 13611 | } |
| 13612 | return JIM_ERR; |
| 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, |
| 13624 | Jim_Obj *const *indexv, int indexc, Jim_Obj *newObjPtr) |
| 13625 | { |
| 13626 | Jim_Obj *varObjPtr, *objPtr, *listObjPtr; |
| 13627 | int shared, i, idx; |
| 13628 | |
| 13629 | varObjPtr = objPtr = Jim_GetVariable(interp, varNamePtr, JIM_ERRMSG | JIM_UNSHARED); |
| 13630 | if (objPtr == NULL) |
| 13631 | return JIM_ERR; |
| 13632 | if ((shared = Jim_IsShared(objPtr))) |
| 13633 | varObjPtr = objPtr = Jim_DuplicateObj(interp, objPtr); |
| 13634 | for (i = 0; i < indexc - 1; i++) { |
| 13635 | listObjPtr = objPtr; |
| 13636 | if (Jim_GetIndex(interp, indexv[i], &idx) != JIM_OK) |
| 13637 | goto err; |
| 13638 | |
| 13639 | objPtr = Jim_ListGetIndex(interp, listObjPtr, idx); |
| 13640 | if (objPtr == NULL) { |
| 13641 | Jim_SetResultFormatted(interp, "index \"%#s\" out of range", indexv[i]); |
| 13642 | goto err; |
| 13643 | } |
| 13644 | if (Jim_IsShared(objPtr)) { |
| 13645 | objPtr = Jim_DuplicateObj(interp, objPtr); |
| 13646 | ListSetIndex(interp, listObjPtr, idx, objPtr, JIM_NONE); |
| 13647 | } |
| 13648 | Jim_InvalidateStringRep(listObjPtr); |
| 13649 | } |
| 13650 | if (Jim_GetIndex(interp, indexv[indexc - 1], &idx) != JIM_OK) |
| 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); |
| 13664 | } |
| 13665 | return JIM_ERR; |
| 13666 | } |
| 13667 | |
| 13668 | Jim_Obj *Jim_ListJoin(Jim_Interp *interp, Jim_Obj *listObjPtr, const char *joinStr, int joinStrLen) |
| 13669 | { |
| 13670 | int i; |
| 13671 | int listLen = Jim_ListLength(interp, listObjPtr); |
| 13672 | Jim_Obj *resObjPtr = Jim_NewEmptyStringObj(interp); |
| 13673 | |
| 13674 | for (i = 0; i < listLen; ) { |
| 13675 | Jim_AppendObj(interp, resObjPtr, Jim_ListGetIndex(interp, listObjPtr, i)); |
| 13676 | if (++i != listLen) { |
| 13677 | Jim_AppendString(interp, resObjPtr, joinStr, joinStrLen); |
| 13678 | } |
| 13679 | } |
| 13680 | return resObjPtr; |
| 13681 | } |
| 13682 | |
| 13683 | Jim_Obj *Jim_ConcatObj(Jim_Interp *interp, int objc, Jim_Obj *const *objv) |
| 13684 | { |
| 13685 | int i; |
| 13686 | |
| 13687 | for (i = 0; i < objc; i++) { |
| 13688 | if (!Jim_IsList(objv[i])) |
| 13689 | break; |
| 13690 | } |
| 13691 | if (i == objc) { |
| 13692 | Jim_Obj *objPtr = Jim_NewListObj(interp, NULL, 0); |
| 13693 | |
| 13694 | for (i = 0; i < objc; i++) |
| 13695 | ListAppendList(objPtr, objv[i]); |
| 13696 | return objPtr; |
| 13697 | } |
| 13698 | else { |
| 13699 | |
| 13700 | int len = 0, objLen; |
| 13701 | char *bytes, *p; |
| 13702 | |
| 13703 | |
| 13704 | for (i = 0; i < objc; i++) { |
| 13705 | len += Jim_Length(objv[i]); |
| 13706 | } |
| 13707 | if (objc) |
| 13708 | len += objc - 1; |
| 13709 | |
| 13710 | p = bytes = Jim_Alloc(len + 1); |
| 13711 | for (i = 0; i < objc; i++) { |
| 13712 | const char *s = Jim_GetString(objv[i], &objLen); |
| 13713 | |
| 13714 | |
| 13715 | while (objLen && isspace(UCHAR(*s))) { |
| 13716 | s++; |
| 13717 | objLen--; |
| 13718 | len--; |
| 13719 | } |
| 13720 | |
| 13721 | while (objLen && isspace(UCHAR(s[objLen - 1]))) { |
| 13722 | |
| 13723 | if (objLen > 1 && s[objLen - 2] == '\\') { |
| 13724 | break; |
| 13725 | } |
| 13726 | objLen--; |
| 13727 | len--; |
| 13728 | } |
| 13729 | memcpy(p, s, objLen); |
| 13730 | p += objLen; |
| 13731 | if (i + 1 != objc) { |
| 13732 | if (objLen) |
| 13733 | *p++ = ' '; |
| 13734 | else { |
| 13735 | len--; |
| 13736 | } |
| 13737 | } |
| 13738 | } |
| 13739 | *p = '\0'; |
| 13740 | return Jim_NewStringObjNoAlloc(interp, bytes, len); |
| 13741 | } |
| 13742 | } |
| 13743 | |
| 13744 | Jim_Obj *Jim_ListRange(Jim_Interp *interp, Jim_Obj *listObjPtr, Jim_Obj *firstObjPtr, |
| 13745 | Jim_Obj *lastObjPtr) |
| 13746 | { |
| 13747 | int first, last; |
| 13748 | int len, rangeLen; |
| 13749 | |
| 13750 | if (Jim_GetIndex(interp, firstObjPtr, &first) != JIM_OK || |
| 13751 | Jim_GetIndex(interp, lastObjPtr, &last) != JIM_OK) |
| 13752 | return NULL; |
| 13753 | len = Jim_ListLength(interp, listObjPtr); |
| 13754 | first = JimRelToAbsIndex(len, first); |
| 13755 | last = JimRelToAbsIndex(len, last); |
| 13756 | JimRelToAbsRange(len, &first, &last, &rangeLen); |
| 13757 | if (first == 0 && last == len) { |
| 13758 | return listObjPtr; |
| 13759 | } |
| 13760 | return Jim_NewListObj(interp, listObjPtr->internalRep.listValue.ele + first, rangeLen); |
| 13761 | } |
| 13762 | |
| 13763 | static void FreeDictInternalRep(Jim_Interp *interp, Jim_Obj *objPtr); |
| 13764 | static void DupDictInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr); |
| 13765 | static void UpdateStringOfDict(struct Jim_Obj *objPtr); |
| 13766 | static int SetDictFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr); |
| 13767 | |
| 13768 | |
| 13769 | static const Jim_ObjType dictObjType = { |
| 13770 | "dict", |
| 13771 | FreeDictInternalRep, |
| 13772 | DupDictInternalRep, |
| 13773 | UpdateStringOfDict, |
| 13774 | JIM_TYPE_NONE, |
| 13775 | }; |
| 13776 | |
| 13777 | static void JimFreeDict(Jim_Interp *interp, Jim_Dict *dict) |
| 13778 | { |
| 13779 | int i; |
| 13780 | for (i = 0; i < dict->len; i++) { |
| 13781 | Jim_DecrRefCount(interp, dict->table[i]); |
| 13782 | } |
| 13783 | Jim_Free(dict->table); |
| 13784 | Jim_Free(dict->ht); |
| 13785 | Jim_Free(dict); |
| 13786 | } |
| 13787 | |
| 13788 | enum { |
| 13789 | DICT_HASH_FIND = -1, |
| 13790 | DICT_HASH_REMOVE = -2, |
| 13791 | DICT_HASH_ADD = -3, |
| 13792 | }; |
| 13793 | |
| 13794 | static int JimDictHashFind(Jim_Dict *dict, Jim_Obj *keyObjPtr, int op_tvoffset) |
| 13795 | { |
| 13796 | unsigned h = (JimObjectHTHashFunction(keyObjPtr) + dict->uniq); |
| 13797 | unsigned idx = h & dict->sizemask; |
| 13798 | int tvoffset = 0; |
| 13799 | unsigned peturb = h; |
| 13800 | unsigned first_removed = ~0; |
| 13801 | |
| 13802 | if (dict->len) { |
| 13803 | while ((tvoffset = dict->ht[idx].offset)) { |
| 13804 | if (tvoffset == -1) { |
| 13805 | if (first_removed == ~0) { |
| 13806 | first_removed = idx; |
| 13807 | } |
| 13808 | } |
| 13809 | else if (dict->ht[idx].hash == h) { |
| 13810 | if (Jim_StringEqObj(keyObjPtr, dict->table[tvoffset - 1])) { |
| 13811 | break; |
| 13812 | } |
| 13813 | } |
| 13814 | |
| 13815 | peturb >>= 5; |
| 13816 | idx = (5 * idx + 1 + peturb) & dict->sizemask; |
| 13817 | } |
| 13818 | } |
| 13819 | |
| 13820 | switch (op_tvoffset) { |
| 13821 | case DICT_HASH_FIND: |
| 13822 | |
| 13823 | break; |
| 13824 | case DICT_HASH_REMOVE: |
| 13825 | if (tvoffset) { |
| 13826 | |
| 13827 | dict->ht[idx].offset = -1; |
| 13828 | dict->dummy++; |
| 13829 | } |
| 13830 | |
| 13831 | break; |
| 13832 | case DICT_HASH_ADD: |
| 13833 | if (tvoffset == 0) { |
| 13834 | |
| 13835 | if (first_removed != ~0) { |
| 13836 | idx = first_removed; |
| 13837 | dict->dummy--; |
| 13838 | } |
| 13839 | dict->ht[idx].offset = dict->len + 1; |
| 13840 | dict->ht[idx].hash = h; |
| 13841 | } |
| 13842 | |
| 13843 | break; |
| 13844 | default: |
| 13845 | assert(tvoffset); |
| 13846 | |
| 13847 | dict->ht[idx].offset = op_tvoffset; |
| 13848 | break; |
| 13849 | } |
| 13850 | |
| 13851 | return tvoffset; |
| 13852 | } |
| 13853 | |
| 13854 | static void JimDictExpandHashTable(Jim_Dict *dict, unsigned int size) |
| 13855 | { |
| 13856 | int i; |
| 13857 | struct JimDictHashEntry *prevht = dict->ht; |
| 13858 | int prevsize = dict->size; |
| 13859 | |
| 13860 | dict->size = JimHashTableNextPower(size); |
| 13861 | dict->sizemask = dict->size - 1; |
| 13862 | |
| 13863 | |
| 13864 | dict->ht = Jim_Alloc(dict->size * sizeof(*dict->ht)); |
| 13865 | memset(dict->ht, 0, dict->size * sizeof(*dict->ht)); |
| 13866 | |
| 13867 | |
| 13868 | for (i = 0; i < prevsize; i++) { |
| 13869 | if (prevht[i].offset > 0) { |
| 13870 | |
| 13871 | unsigned h = prevht[i].hash; |
| 13872 | unsigned idx = h & dict->sizemask; |
| 13873 | unsigned peturb = h; |
| 13874 | |
| 13875 | while (dict->ht[idx].offset) { |
| 13876 | peturb >>= 5; |
| 13877 | idx = (5 * idx + 1 + peturb) & dict->sizemask; |
| 13878 | } |
| 13879 | dict->ht[idx].offset = prevht[i].offset; |
| 13880 | dict->ht[idx].hash = h; |
| 13881 | } |
| 13882 | } |
| 13883 | Jim_Free(prevht); |
| 13884 | } |
| 13885 | |
| 13886 | static int JimDictAdd(Jim_Dict *dict, Jim_Obj *keyObjPtr) |
| 13887 | { |
| 13888 | if (dict->size <= dict->len + dict->dummy) { |
| 13889 | JimDictExpandHashTable(dict, dict->size ? dict->size * 2 : 8); |
| 13890 | } |
| 13891 | return JimDictHashFind(dict, keyObjPtr, DICT_HASH_ADD); |
| 13892 | } |
| 13893 | |
| 13894 | static Jim_Dict *JimDictNew(Jim_Interp *interp, int table_size, int ht_size) |
| 13895 | { |
| 13896 | Jim_Dict *dict = Jim_Alloc(sizeof(*dict)); |
| 13897 | memset(dict, 0, sizeof(*dict)); |
| 13898 | |
| 13899 | if (ht_size) { |
| 13900 | JimDictExpandHashTable(dict, ht_size); |
| 13901 | } |
| 13902 | if (table_size) { |
| 13903 | dict->table = Jim_Alloc(table_size * sizeof(*dict->table)); |
| 13904 | dict->maxLen = table_size; |
| 13905 | } |
| 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; |
| 13923 | |
| 13924 | |
| 13925 | Jim_Dict *newDict = JimDictNew(interp, oldDict->maxLen, oldDict->size); |
| 13926 | |
| 13927 | |
| 13928 | for (i = 0; i < oldDict->len; i++) { |
| 13929 | newDict->table[i] = oldDict->table[i]; |
| 13930 | Jim_IncrRefCount(newDict->table[i]); |
| 13931 | } |
| 13932 | newDict->len = oldDict->len; |
| 13933 | |
| 13934 | |
| 13935 | newDict->uniq = oldDict->uniq; |
| 13936 | |
| 13937 | |
| 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 | |
| 13955 | if (objPtr->typePtr == &dictObjType) { |
| 13956 | return JIM_OK; |
| 13957 | } |
| 13958 | |
| 13959 | if (Jim_IsList(objPtr) && Jim_IsShared(objPtr)) { |
| 13960 | Jim_String(objPtr); |
| 13961 | } |
| 13962 | |
| 13963 | listlen = Jim_ListLength(interp, objPtr); |
| 13964 | if (listlen % 2) { |
| 13965 | Jim_SetResultString(interp, "missing value to go with key", -1); |
| 13966 | return JIM_ERR; |
| 13967 | } |
| 13968 | else { |
| 13969 | |
| 13970 | Jim_Dict *dict = JimDictNew(interp, 0, listlen); |
| 13971 | int i; |
| 13972 | |
| 13973 | |
| 13974 | dict->table = objPtr->internalRep.listValue.ele; |
| 13975 | dict->maxLen = objPtr->internalRep.listValue.maxLen; |
| 13976 | |
| 13977 | |
| 13978 | for (i = 0; i < listlen; i += 2) { |
| 13979 | int tvoffset = JimDictAdd(dict, dict->table[i]); |
| 13980 | if (tvoffset) { |
| 13981 | |
| 13982 | |
| 13983 | Jim_DecrRefCount(interp, dict->table[tvoffset]); |
| 13984 | |
| 13985 | dict->table[tvoffset] = dict->table[i + 1]; |
| 13986 | |
| 13987 | Jim_DecrRefCount(interp, dict->table[i]); |
| 13988 | } |
| 13989 | else { |
| 13990 | if (dict->len != i) { |
| 13991 | dict->table[dict->len++] = dict->table[i]; |
| 13992 | dict->table[dict->len++] = dict->table[i + 1]; |
| 13993 | } |
| 13994 | else { |
| 13995 | dict->len += 2; |
| 13996 | } |
| 13997 | } |
| 13998 | } |
| 13999 | |
| 14000 | objPtr->typePtr = &dictObjType; |
| 14001 | objPtr->internalRep.dictValue = dict; |
| 14002 | |
| 14003 | return JIM_OK; |
| 14004 | } |
| 14005 | } |
| 14006 | |
| 14007 | |
| 14008 | |
| 14009 | static int DictAddElement(Jim_Interp *interp, Jim_Obj *objPtr, |
| 14010 | Jim_Obj *keyObjPtr, Jim_Obj *valueObjPtr) |
| 14011 | { |
| 14012 | Jim_Dict *dict = objPtr->internalRep.dictValue; |
| 14013 | if (valueObjPtr == NULL) { |
| 14014 | |
| 14015 | int tvoffset = JimDictHashFind(dict, keyObjPtr, DICT_HASH_REMOVE); |
| 14016 | if (tvoffset) { |
| 14017 | |
| 14018 | Jim_DecrRefCount(interp, dict->table[tvoffset - 1]); |
| 14019 | Jim_DecrRefCount(interp, dict->table[tvoffset]); |
| 14020 | dict->len -= 2; |
| 14021 | if (tvoffset != dict->len + 1) { |
| 14022 | |
| 14023 | dict->table[tvoffset - 1] = dict->table[dict->len]; |
| 14024 | dict->table[tvoffset] = dict->table[dict->len + 1]; |
| 14025 | |
| 14026 | |
| 14027 | JimDictHashFind(dict, dict->table[tvoffset - 1], tvoffset); |
| 14028 | } |
| 14029 | return JIM_OK; |
| 14030 | } |
| 14031 | return JIM_ERR; |
| 14032 | } |
| 14033 | else { |
| 14034 | |
| 14035 | int tvoffset = JimDictAdd(dict, keyObjPtr); |
| 14036 | if (tvoffset) { |
| 14037 | |
| 14038 | Jim_IncrRefCount(valueObjPtr); |
| 14039 | Jim_DecrRefCount(interp, dict->table[tvoffset]); |
| 14040 | dict->table[tvoffset] = valueObjPtr; |
| 14041 | } |
| 14042 | else { |
| 14043 | if (dict->maxLen == dict->len) { |
| 14044 | |
| 14045 | if (dict->maxLen < 4) { |
| 14046 | dict->maxLen = 4; |
| 14047 | } |
| 14048 | else { |
| 14049 | dict->maxLen *= 2; |
| 14050 | } |
| 14051 | dict->table = Jim_Realloc(dict->table, dict->maxLen * sizeof(*dict->table)); |
| 14052 | } |
| 14053 | Jim_IncrRefCount(keyObjPtr); |
| 14054 | Jim_IncrRefCount(valueObjPtr); |
| 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, |
| 14066 | Jim_Obj *keyObjPtr, Jim_Obj *valueObjPtr) |
| 14067 | { |
| 14068 | JimPanic((Jim_IsShared(objPtr), "Jim_DictAddElement called with shared object")); |
| 14069 | if (SetDictFromAny(interp, objPtr) != JIM_OK) { |
| 14070 | return JIM_ERR; |
| 14071 | } |
| 14072 | Jim_InvalidateStringRep(objPtr); |
| 14073 | return DictAddElement(interp, objPtr, keyObjPtr, valueObjPtr); |
| 14074 | } |
| 14075 | |
| 14076 | Jim_Obj *Jim_NewDictObj(Jim_Interp *interp, Jim_Obj *const *elements, int len) |
| 14077 | { |
| 14078 | Jim_Obj *objPtr; |
| 14079 | int i; |
| 14080 | |
| 14081 | JimPanic((len % 2, "Jim_NewDictObj() 'len' argument must be even")); |
| 14082 | |
| 14083 | objPtr = Jim_NewObj(interp); |
| 14084 | objPtr->typePtr = &dictObjType; |
| 14085 | objPtr->bytes = NULL; |
| 14086 | |
| 14087 | objPtr->internalRep.dictValue = JimDictNew(interp, len, len); |
| 14088 | for (i = 0; i < len; i += 2) |
| 14089 | DictAddElement(interp, objPtr, elements[i], elements[i + 1]); |
| 14090 | return objPtr; |
| 14091 | } |
| 14092 | |
| 14093 | int Jim_DictKey(Jim_Interp *interp, Jim_Obj *dictPtr, Jim_Obj *keyPtr, |
| 14094 | Jim_Obj **objPtrPtr, int flags) |
| 14095 | { |
| 14096 | int tvoffset; |
| 14097 | Jim_Dict *dict; |
| 14098 | |
| 14099 | if (SetDictFromAny(interp, dictPtr) != JIM_OK) { |
| 14100 | return -1; |
| 14101 | } |
| 14102 | dict = dictPtr->internalRep.dictValue; |
| 14103 | tvoffset = JimDictHashFind(dict, keyPtr, DICT_HASH_FIND); |
| 14104 | if (tvoffset == 0) { |
| 14105 | if (flags & JIM_ERRMSG) { |
| 14106 | Jim_SetResultFormatted(interp, "key \"%#s\" not known in dictionary", keyPtr); |
| 14107 | } |
| 14108 | return JIM_ERR; |
| 14109 | } |
| 14110 | *objPtrPtr = dict->table[tvoffset]; |
| 14111 | return JIM_OK; |
| 14112 | } |
| 14113 | |
| 14114 | Jim_Obj **Jim_DictPairs(Jim_Interp *interp, Jim_Obj *dictPtr, int *len) |
| 14115 | { |
| 14116 | |
| 14117 | if (Jim_IsList(dictPtr)) { |
| 14118 | Jim_Obj **table; |
| 14119 | JimListGetElements(interp, dictPtr, len, &table); |
| 14120 | if (*len % 2 == 0) { |
| 14121 | return table; |
| 14122 | } |
| 14123 | |
| 14124 | } |
| 14125 | if (SetDictFromAny(interp, dictPtr) != JIM_OK) { |
| 14126 | |
| 14127 | *len = 1; |
| 14128 | return NULL; |
| 14129 | } |
| 14130 | *len = dictPtr->internalRep.dictValue->len; |
| 14131 | return dictPtr->internalRep.dictValue->table; |
| 14132 | } |
| 14133 | |
| 14134 | |
| 14135 | int Jim_DictKeysVector(Jim_Interp *interp, Jim_Obj *dictPtr, |
| 14136 | Jim_Obj *const *keyv, int keyc, Jim_Obj **objPtrPtr, int flags) |
| 14137 | { |
| 14138 | int i; |
| 14139 | |
| 14140 | if (keyc == 0) { |
| 14141 | *objPtrPtr = dictPtr; |
| 14142 | return JIM_OK; |
| 14143 | } |
| 14144 | |
| 14145 | for (i = 0; i < keyc; i++) { |
| 14146 | Jim_Obj *objPtr; |
| 14147 | |
| 14148 | int rc = Jim_DictKey(interp, dictPtr, keyv[i], &objPtr, flags); |
| 14149 | if (rc != JIM_OK) { |
| 14150 | return rc; |
| 14151 | } |
| 14152 | dictPtr = objPtr; |
| 14153 | } |
| 14154 | *objPtrPtr = dictPtr; |
| 14155 | return JIM_OK; |
| 14156 | } |
| 14157 | |
| 14158 | int Jim_SetDictKeysVector(Jim_Interp *interp, Jim_Obj *varNamePtr, |
| 14159 | Jim_Obj *const *keyv, int keyc, Jim_Obj *newObjPtr, int flags) |
| 14160 | { |
| 14161 | Jim_Obj *varObjPtr, *objPtr, *dictObjPtr; |
| 14162 | int shared, i; |
| 14163 | |
| 14164 | varObjPtr = objPtr = Jim_GetVariable(interp, varNamePtr, flags); |
| 14165 | if (objPtr == NULL) { |
| 14166 | if (newObjPtr == NULL && (flags & JIM_MUSTEXIST)) { |
| 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); |
| 14177 | for (i = 0; i < keyc; i++) { |
| 14178 | dictObjPtr = objPtr; |
| 14179 | |
| 14180 | |
| 14181 | if (SetDictFromAny(interp, dictObjPtr) != JIM_OK) { |
| 14182 | goto err; |
| 14183 | } |
| 14184 | |
| 14185 | if (i == keyc - 1) { |
| 14186 | |
| 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); |
| 14200 | if (Jim_DictKey(interp, dictObjPtr, keyv[i], &objPtr, |
| 14201 | newObjPtr ? JIM_NONE : JIM_ERRMSG) == JIM_OK) { |
| 14202 | if (Jim_IsShared(objPtr)) { |
| 14203 | objPtr = Jim_DuplicateObj(interp, objPtr); |
| 14204 | DictAddElement(interp, dictObjPtr, keyv[i], objPtr); |
| 14205 | } |
| 14206 | } |
| 14207 | else { |
| 14208 | if (newObjPtr == NULL) { |
| 14209 | goto err; |
| 14210 | } |
| 14211 | objPtr = Jim_NewDictObj(interp, NULL, 0); |
| 14212 | DictAddElement(interp, dictObjPtr, keyv[i], objPtr); |
| 14213 | } |
| 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 | } |
| 14225 | return JIM_OK; |
| 14226 | err: |
| 14227 | if (shared) { |
| 14228 | Jim_FreeNewObj(interp, varObjPtr); |
| 14229 | } |
| 14230 | return JIM_ERR; |
| 14231 | } |
| 14232 | |
| 14233 | static void UpdateStringOfIndex(struct Jim_Obj *objPtr); |
| 14234 | static int SetIndexFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr); |
| 14235 | |
| 14236 | static const Jim_ObjType indexObjType = { |
| 14237 | "index", |
| 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"); |
| 14249 | } |
| 14250 | else { |
| 14251 | char buf[JIM_INTEGER_SPACE + 1]; |
| 14252 | if (objPtr->internalRep.intValue >= 0 || objPtr->internalRep.intValue == -INT_MAX) { |
| 14253 | sprintf(buf, "%d", objPtr->internalRep.intValue); |
| 14254 | } |
| 14255 | else { |
| 14256 | |
| 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; |
| 14268 | const char *str; |
| 14269 | Jim_Obj *exprObj = objPtr; |
| 14270 | |
| 14271 | JimPanic((objPtr->refCount == 0, "SetIndexFromAny() called with zero refcount object")); |
| 14272 | |
| 14273 | |
| 14274 | str = Jim_String(objPtr); |
| 14275 | |
| 14276 | |
| 14277 | if (strncmp(str, "end", 3) == 0) { |
| 14278 | end = 1; |
| 14279 | str += 3; |
| 14280 | idx = 0; |
| 14281 | switch (*str) { |
| 14282 | case '\0': |
| 14283 | exprObj = NULL; |
| 14284 | break; |
| 14285 | |
| 14286 | case '-': |
| 14287 | case '+': |
| 14288 | exprObj = Jim_NewStringObj(interp, str, -1); |
| 14289 | break; |
| 14290 | |
| 14291 | default: |
| 14292 | goto badindex; |
| 14293 | } |
| 14294 | } |
| 14295 | if (exprObj) { |
| 14296 | int ret; |
| 14297 | Jim_IncrRefCount(exprObj); |
| 14298 | ret = Jim_GetWideExpr(interp, exprObj, &idx); |
| 14299 | Jim_DecrRefCount(interp, exprObj); |
| 14300 | if (ret != JIM_OK) { |
| 14301 | goto badindex; |
| 14302 | } |
| 14303 | } |
| 14304 | |
| 14305 | if (end) { |
| 14306 | if (idx > 0) { |
| 14307 | idx = INT_MAX; |
| 14308 | } |
| 14309 | else { |
| 14310 | |
| 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; |
| 14324 | objPtr->internalRep.intValue = idx; |
| 14325 | return JIM_OK; |
| 14326 | |
| 14327 | badindex: |
| 14328 | Jim_SetResultFormatted(interp, |
| 14329 | "bad index \"%#s\": must be intexpr or end?[+-]intexpr?", objPtr); |
| 14330 | return JIM_ERR; |
| 14331 | } |
| 14332 | |
| 14333 | int Jim_GetIndex(Jim_Interp *interp, Jim_Obj *objPtr, int *indexPtr) |
| 14334 | { |
| 14335 | |
| 14336 | if (objPtr->typePtr == &intObjType) { |
| 14337 | jim_wide val = JimWideValue(objPtr); |
| 14338 | |
| 14339 | if (val < 0) |
| 14340 | *indexPtr = -INT_MAX; |
| 14341 | else if (val > INT_MAX) |
| 14342 | *indexPtr = INT_MAX; |
| 14343 | else |
| 14344 | *indexPtr = (int)val; |
| 14345 | return JIM_OK; |
| 14346 | } |
| 14347 | if (objPtr->typePtr != &indexObjType && SetIndexFromAny(interp, objPtr) == JIM_ERR) |
| 14348 | return JIM_ERR; |
| 14349 | *indexPtr = objPtr->internalRep.intValue; |
| 14350 | return JIM_OK; |
| 14351 | } |
| 14352 | |
| 14353 | |
| 14354 | |
| 14355 | static const char * const jimReturnCodes[] = { |
| 14356 | "ok", |
| 14357 | "error", |
| 14358 | "return", |
| 14359 | "break", |
| 14360 | "continue", |
| 14361 | "signal", |
| 14362 | "exit", |
| 14363 | "eval", |
| 14364 | NULL |
| 14365 | }; |
| 14366 | |
| 14367 | #define jimReturnCodesSize (sizeof(jimReturnCodes)/sizeof(*jimReturnCodes) - 1) |
| 14368 | |
| 14369 | static const Jim_ObjType returnCodeObjType = { |
| 14370 | "return-code", |
| 14371 | NULL, |
| 14372 | NULL, |
| 14373 | NULL, |
| 14374 | JIM_TYPE_NONE, |
| 14375 | }; |
| 14376 | |
| 14377 | const char *Jim_ReturnCode(int code) |
| 14378 | { |
| 14379 | if (code < 0 || code >= (int)jimReturnCodesSize) { |
| 14380 | return "?"; |
| 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; |
| 14392 | |
| 14393 | |
| 14394 | if (JimGetWideNoErr(interp, objPtr, &wideValue) != JIM_ERR) |
| 14395 | returnCode = (int)wideValue; |
| 14396 | else if (Jim_GetEnum(interp, objPtr, jimReturnCodes, &returnCode, NULL, JIM_NONE) != JIM_OK) { |
| 14397 | Jim_SetResultFormatted(interp, "expected return code but got \"%#s\"", objPtr); |
| 14398 | return JIM_ERR; |
| 14399 | } |
| 14400 | |
| 14401 | Jim_FreeIntRep(interp, objPtr); |
| 14402 | objPtr->typePtr = &returnCodeObjType; |
| 14403 | objPtr->internalRep.intValue = returnCode; |
| 14404 | return JIM_OK; |
| 14405 | } |
| 14406 | |
| 14407 | int Jim_GetReturnCode(Jim_Interp *interp, Jim_Obj *objPtr, int *intPtr) |
| 14408 | { |
| 14409 | if (objPtr->typePtr != &returnCodeObjType && SetReturnCodeFromAny(interp, objPtr) == JIM_ERR) |
| 14410 | return JIM_ERR; |
| 14411 | *intPtr = objPtr->internalRep.intValue; |
| 14412 | return JIM_OK; |
| 14413 | } |
| 14414 | |
| 14415 | static int JimParseExprOperator(struct JimParserCtx *pc); |
| 14416 | static int JimParseExprNumber(struct JimParserCtx *pc); |
| 14417 | static int JimParseExprIrrational(struct JimParserCtx *pc); |
| 14418 | static int JimParseExprBoolean(struct JimParserCtx *pc); |
| 14419 | |
| 14420 | |
| 14421 | enum |
| 14422 | { |
| 14423 | |
| 14424 | |
| 14425 | |
| 14426 | JIM_EXPROP_MUL = JIM_TT_EXPR_OP, |
| 14427 | JIM_EXPROP_DIV, |
| 14428 | JIM_EXPROP_MOD, |
| 14429 | JIM_EXPROP_SUB, |
| 14430 | JIM_EXPROP_ADD, |
| 14431 | JIM_EXPROP_LSHIFT, |
| 14432 | JIM_EXPROP_RSHIFT, |
| 14433 | JIM_EXPROP_ROTL, |
| 14434 | JIM_EXPROP_ROTR, |
| 14435 | JIM_EXPROP_LT, |
| 14436 | JIM_EXPROP_GT, |
| 14437 | JIM_EXPROP_LTE, |
| 14438 | JIM_EXPROP_GTE, |
| 14439 | JIM_EXPROP_NUMEQ, |
| 14440 | JIM_EXPROP_NUMNE, |
| 14441 | JIM_EXPROP_BITAND, |
| 14442 | JIM_EXPROP_BITXOR, |
| 14443 | JIM_EXPROP_BITOR, |
| 14444 | JIM_EXPROP_LOGICAND, |
| 14445 | JIM_EXPROP_LOGICOR, |
| 14446 | JIM_EXPROP_TERNARY, |
| 14447 | JIM_EXPROP_COLON, |
| 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, |
| 14461 | JIM_EXPROP_STRGE, |
| 14462 | |
| 14463 | |
| 14464 | JIM_EXPROP_NOT, |
| 14465 | JIM_EXPROP_BITNOT, |
| 14466 | JIM_EXPROP_UNARYMINUS, |
| 14467 | JIM_EXPROP_UNARYPLUS, |
| 14468 | |
| 14469 | |
| 14470 | JIM_EXPROP_FUNC_INT, |
| 14471 | JIM_EXPROP_FUNC_WIDE, |
| 14472 | JIM_EXPROP_FUNC_ABS, |
| 14473 | JIM_EXPROP_FUNC_DOUBLE, |
| 14474 | JIM_EXPROP_FUNC_ROUND, |
| 14475 | JIM_EXPROP_FUNC_RAND, |
| 14476 | JIM_EXPROP_FUNC_SRAND, |
| 14477 | |
| 14478 | |
| 14479 | JIM_EXPROP_FUNC_SIN, |
| 14480 | JIM_EXPROP_FUNC_COS, |
| 14481 | JIM_EXPROP_FUNC_TAN, |
| 14482 | JIM_EXPROP_FUNC_ASIN, |
| 14483 | JIM_EXPROP_FUNC_ACOS, |
| 14484 | JIM_EXPROP_FUNC_ATAN, |
| 14485 | JIM_EXPROP_FUNC_ATAN2, |
| 14486 | JIM_EXPROP_FUNC_SINH, |
| 14487 | JIM_EXPROP_FUNC_COSH, |
| 14488 | JIM_EXPROP_FUNC_TANH, |
| 14489 | JIM_EXPROP_FUNC_CEIL, |
| 14490 | JIM_EXPROP_FUNC_FLOOR, |
| 14491 | JIM_EXPROP_FUNC_EXP, |
| 14492 | JIM_EXPROP_FUNC_LOG, |
| 14493 | JIM_EXPROP_FUNC_LOG10, |
| 14494 | JIM_EXPROP_FUNC_SQRT, |
| 14495 | JIM_EXPROP_FUNC_POW, |
| 14496 | JIM_EXPROP_FUNC_HYPOT, |
| 14497 | JIM_EXPROP_FUNC_FMOD, |
| 14498 | }; |
| 14499 | |
| 14500 | struct JimExprNode { |
| 14501 | int type; |
| 14502 | struct Jim_Obj *objPtr; |
| 14503 | |
| 14504 | struct JimExprNode *left; |
| 14505 | struct JimExprNode *right; |
| 14506 | struct JimExprNode *ternary; |
| 14507 | }; |
| 14508 | |
| 14509 | |
| 14510 | typedef struct Jim_ExprOperator |
| 14511 | { |
| 14512 | const char *name; |
| 14513 | int (*funcop) (Jim_Interp *interp, struct JimExprNode *opnode); |
| 14514 | unsigned char precedence; |
| 14515 | unsigned char arity; |
| 14516 | unsigned char attr; |
| 14517 | unsigned char namelen; |
| 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; |
| 14529 | double dA, dC = 0; |
| 14530 | jim_wide wA, wC = 0; |
| 14531 | Jim_Obj *A; |
| 14532 | |
| 14533 | if ((rc = JimExprGetTerm(interp, node->left, &A)) != JIM_OK) { |
| 14534 | return rc; |
| 14535 | } |
| 14536 | |
| 14537 | if ((A->typePtr != &doubleObjType || A->bytes) && JimGetWideNoErr(interp, A, &wA) == JIM_OK) { |
| 14538 | switch (node->type) { |
| 14539 | case JIM_EXPROP_FUNC_INT: |
| 14540 | case JIM_EXPROP_FUNC_WIDE: |
| 14541 | case JIM_EXPROP_FUNC_ROUND: |
| 14542 | case JIM_EXPROP_UNARYPLUS: |
| 14543 | wC = wA; |
| 14544 | break; |
| 14545 | case JIM_EXPROP_FUNC_DOUBLE: |
| 14546 | dC = wA; |
| 14547 | intresult = 0; |
| 14548 | break; |
| 14549 | case JIM_EXPROP_FUNC_ABS: |
| 14550 | wC = wA >= 0 ? wA : -wA; |
| 14551 | break; |
| 14552 | case JIM_EXPROP_UNARYMINUS: |
| 14553 | wC = -wA; |
| 14554 | break; |
| 14555 | case JIM_EXPROP_NOT: |
| 14556 | wC = !wA; |
| 14557 | break; |
| 14558 | default: |
| 14559 | abort(); |
| 14560 | } |
| 14561 | } |
| 14562 | else if ((rc = Jim_GetDouble(interp, A, &dA)) == JIM_OK) { |
| 14563 | switch (node->type) { |
| 14564 | case JIM_EXPROP_FUNC_INT: |
| 14565 | case JIM_EXPROP_FUNC_WIDE: |
| 14566 | wC = dA; |
| 14567 | break; |
| 14568 | case JIM_EXPROP_FUNC_ROUND: |
| 14569 | wC = dA < 0 ? (dA - 0.5) : (dA + 0.5); |
| 14570 | break; |
| 14571 | case JIM_EXPROP_FUNC_DOUBLE: |
| 14572 | case JIM_EXPROP_UNARYPLUS: |
| 14573 | dC = dA; |
| 14574 | intresult = 0; |
| 14575 | break; |
| 14576 | case JIM_EXPROP_FUNC_ABS: |
| 14577 | #ifdef JIM_MATH_FUNCTIONS |
| 14578 | dC = fabs(dA); |
| 14579 | #else |
| 14580 | dC = dA >= 0 ? dA : -dA; |
| 14581 | #endif |
| 14582 | intresult = 0; |
| 14583 | break; |
| 14584 | case JIM_EXPROP_UNARYMINUS: |
| 14585 | dC = -dA; |
| 14586 | intresult = 0; |
| 14587 | break; |
| 14588 | case JIM_EXPROP_NOT: |
| 14589 | wC = !dA; |
| 14590 | break; |
| 14591 | default: |
| 14592 | abort(); |
| 14593 | } |
| 14594 | } |
| 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 | |
| 14612 | if (rc == JIM_OK) { |
| 14613 | if (intresult) { |
| 14614 | Jim_SetResultInt(interp, wC); |
| 14615 | } |
| 14616 | else { |
| 14617 | Jim_SetResult(interp, Jim_NewDoubleObj(interp, dC)); |
| 14618 | } |
| 14619 | } |
| 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; |
| 14640 | int rc; |
| 14641 | |
| 14642 | if ((rc = JimExprGetTerm(interp, node->left, &A)) != JIM_OK) { |
| 14643 | return rc; |
| 14644 | } |
| 14645 | |
| 14646 | rc = Jim_GetWide(interp, A, &wA); |
| 14647 | if (rc == JIM_OK) { |
| 14648 | switch (node->type) { |
| 14649 | case JIM_EXPROP_BITNOT: |
| 14650 | Jim_SetResultInt(interp, ~wA); |
| 14651 | break; |
| 14652 | case JIM_EXPROP_FUNC_SRAND: |
| 14653 | JimPrngSeed(interp, (unsigned char *)&wA, sizeof(wA)); |
| 14654 | Jim_SetResult(interp, Jim_NewDoubleObj(interp, JimRandDouble(interp))); |
| 14655 | break; |
| 14656 | default: |
| 14657 | abort(); |
| 14658 | } |
| 14659 | } |
| 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 | |
| 14671 | Jim_SetResult(interp, Jim_NewDoubleObj(interp, JimRandDouble(interp))); |
| 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; |
| 14683 | |
| 14684 | if ((rc = JimExprGetTerm(interp, node->left, &A)) != JIM_OK) { |
| 14685 | return rc; |
| 14686 | } |
| 14687 | |
| 14688 | rc = Jim_GetDouble(interp, A, &dA); |
| 14689 | if (rc == JIM_OK) { |
| 14690 | switch (node->type) { |
| 14691 | case JIM_EXPROP_FUNC_SIN: |
| 14692 | dC = sin(dA); |
| 14693 | break; |
| 14694 | case JIM_EXPROP_FUNC_COS: |
| 14695 | dC = cos(dA); |
| 14696 | break; |
| 14697 | case JIM_EXPROP_FUNC_TAN: |
| 14698 | dC = tan(dA); |
| 14699 | break; |
| 14700 | case JIM_EXPROP_FUNC_ASIN: |
| 14701 | dC = asin(dA); |
| 14702 | break; |
| 14703 | case JIM_EXPROP_FUNC_ACOS: |
| 14704 | dC = acos(dA); |
| 14705 | break; |
| 14706 | case JIM_EXPROP_FUNC_ATAN: |
| 14707 | dC = atan(dA); |
| 14708 | break; |
| 14709 | case JIM_EXPROP_FUNC_SINH: |
| 14710 | dC = sinh(dA); |
| 14711 | break; |
| 14712 | case JIM_EXPROP_FUNC_COSH: |
| 14713 | dC = cosh(dA); |
| 14714 | break; |
| 14715 | case JIM_EXPROP_FUNC_TANH: |
| 14716 | dC = tanh(dA); |
| 14717 | break; |
| 14718 | case JIM_EXPROP_FUNC_CEIL: |
| 14719 | dC = ceil(dA); |
| 14720 | break; |
| 14721 | case JIM_EXPROP_FUNC_FLOOR: |
| 14722 | dC = floor(dA); |
| 14723 | break; |
| 14724 | case JIM_EXPROP_FUNC_EXP: |
| 14725 | dC = exp(dA); |
| 14726 | break; |
| 14727 | case JIM_EXPROP_FUNC_LOG: |
| 14728 | dC = log(dA); |
| 14729 | break; |
| 14730 | case JIM_EXPROP_FUNC_LOG10: |
| 14731 | dC = log10(dA); |
| 14732 | break; |
| 14733 | case JIM_EXPROP_FUNC_SQRT: |
| 14734 | dC = sqrt(dA); |
| 14735 | break; |
| 14736 | default: |
| 14737 | abort(); |
| 14738 | } |
| 14739 | Jim_SetResult(interp, Jim_NewDoubleObj(interp, dC)); |
| 14740 | } |
| 14741 | |
| 14742 | Jim_DecrRefCount(interp, A); |
| 14743 | |
| 14744 | return rc; |
| 14745 | } |
| 14746 | #endif |
| 14747 | |
| 14748 | |
| 14749 | static int JimExprOpIntBin(Jim_Interp *interp, struct JimExprNode *node) |
| 14750 | { |
| 14751 | jim_wide wA, wB; |
| 14752 | int rc; |
| 14753 | Jim_Obj *A, *B; |
| 14754 | |
| 14755 | if ((rc = JimExprGetTerm(interp, node->left, &A)) != JIM_OK) { |
| 14756 | return rc; |
| 14757 | } |
| 14758 | if ((rc = JimExprGetTerm(interp, node->right, &B)) != JIM_OK) { |
| 14759 | Jim_DecrRefCount(interp, A); |
| 14760 | return rc; |
| 14761 | } |
| 14762 | |
| 14763 | rc = JIM_ERR; |
| 14764 | |
| 14765 | if (Jim_GetWide(interp, A, &wA) == JIM_OK && Jim_GetWide(interp, B, &wB) == JIM_OK) { |
| 14766 | jim_wide wC; |
| 14767 | |
| 14768 | rc = JIM_OK; |
| 14769 | |
| 14770 | switch (node->type) { |
| 14771 | case JIM_EXPROP_LSHIFT: |
| 14772 | wC = wA << wB; |
| 14773 | break; |
| 14774 | case JIM_EXPROP_RSHIFT: |
| 14775 | wC = wA >> wB; |
| 14776 | break; |
| 14777 | case JIM_EXPROP_BITAND: |
| 14778 | wC = wA & wB; |
| 14779 | break; |
| 14780 | case JIM_EXPROP_BITXOR: |
| 14781 | wC = wA ^ wB; |
| 14782 | break; |
| 14783 | case JIM_EXPROP_BITOR: |
| 14784 | wC = wA | wB; |
| 14785 | break; |
| 14786 | case JIM_EXPROP_MOD: |
| 14787 | if (wB == 0) { |
| 14788 | wC = 0; |
| 14789 | Jim_SetResultString(interp, "Division by zero", -1); |
| 14790 | rc = JIM_ERR; |
| 14791 | } |
| 14792 | else { |
| 14793 | int negative = 0; |
| 14794 | |
| 14795 | if (wB < 0) { |
| 14796 | wB = -wB; |
| 14797 | wA = -wA; |
| 14798 | negative = 1; |
| 14799 | } |
| 14800 | wC = wA % wB; |
| 14801 | if (wC < 0) { |
| 14802 | wC += wB; |
| 14803 | } |
| 14804 | if (negative) { |
| 14805 | wC = -wC; |
| 14806 | } |
| 14807 | } |
| 14808 | break; |
| 14809 | case JIM_EXPROP_ROTL: |
| 14810 | case JIM_EXPROP_ROTR:{ |
| 14811 | |
| 14812 | unsigned long uA = (unsigned long)wA; |
| 14813 | unsigned long uB = (unsigned long)wB; |
| 14814 | const unsigned int S = sizeof(unsigned long) * 8; |
| 14815 | |
| 14816 | |
| 14817 | uB %= S; |
| 14818 | |
| 14819 | if (node->type == JIM_EXPROP_ROTR) { |
| 14820 | uB = S - uB; |
| 14821 | } |
| 14822 | wC = (unsigned long)(uA << uB) | (uA >> (S - uB)); |
| 14823 | break; |
| 14824 | } |
| 14825 | default: |
| 14826 | abort(); |
| 14827 | } |
| 14828 | Jim_SetResultInt(interp, wC); |
| 14829 | } |
| 14830 | |
| 14831 | Jim_DecrRefCount(interp, A); |
| 14832 | Jim_DecrRefCount(interp, B); |
| 14833 | |
| 14834 | return rc; |
| 14835 | } |
| 14836 | |
| 14837 | |
| 14838 | |
| 14839 | static int JimExprOpBin(Jim_Interp *interp, struct JimExprNode *node) |
| 14840 | { |
| 14841 | int rc = JIM_OK; |
| 14842 | double dA, dB, dC = 0; |
| 14843 | jim_wide wA, wB, wC = 0; |
| 14844 | Jim_Obj *A, *B; |
| 14845 | |
| 14846 | if ((rc = JimExprGetTerm(interp, node->left, &A)) != JIM_OK) { |
| 14847 | return rc; |
| 14848 | } |
| 14849 | if ((rc = JimExprGetTerm(interp, node->right, &B)) != JIM_OK) { |
| 14850 | Jim_DecrRefCount(interp, A); |
| 14851 | return rc; |
| 14852 | } |
| 14853 | |
| 14854 | if ((A->typePtr != &doubleObjType || A->bytes) && |
| 14855 | (B->typePtr != &doubleObjType || B->bytes) && |
| 14856 | JimGetWideNoErr(interp, A, &wA) == JIM_OK && JimGetWideNoErr(interp, B, &wB) == JIM_OK) { |
| 14857 | |
| 14858 | |
| 14859 | |
| 14860 | switch (node->type) { |
| 14861 | case JIM_EXPROP_POW: |
| 14862 | case JIM_EXPROP_FUNC_POW: |
| 14863 | if (wA == 0 && wB < 0) { |
| 14864 | Jim_SetResultString(interp, "exponentiation of zero by negative power", -1); |
| 14865 | rc = JIM_ERR; |
| 14866 | goto done; |
| 14867 | } |
| 14868 | wC = JimPowWide(wA, wB); |
| 14869 | goto intresult; |
| 14870 | case JIM_EXPROP_ADD: |
| 14871 | wC = wA + wB; |
| 14872 | goto intresult; |
| 14873 | case JIM_EXPROP_SUB: |
| 14874 | wC = wA - wB; |
| 14875 | goto intresult; |
| 14876 | case JIM_EXPROP_MUL: |
| 14877 | wC = wA * wB; |
| 14878 | goto intresult; |
| 14879 | case JIM_EXPROP_DIV: |
| 14880 | if (wB == 0) { |
| 14881 | Jim_SetResultString(interp, "Division by zero", -1); |
| 14882 | rc = JIM_ERR; |
| 14883 | goto done; |
| 14884 | } |
| 14885 | else { |
| 14886 | if (wB < 0) { |
| 14887 | wB = -wB; |
| 14888 | wA = -wA; |
| 14889 | } |
| 14890 | wC = wA / wB; |
| 14891 | if (wA % wB < 0) { |
| 14892 | wC--; |
| 14893 | } |
| 14894 | goto intresult; |
| 14895 | } |
| 14896 | case JIM_EXPROP_LT: |
| 14897 | wC = wA < wB; |
| 14898 | goto intresult; |
| 14899 | case JIM_EXPROP_GT: |
| 14900 | wC = wA > wB; |
| 14901 | goto intresult; |
| 14902 | case JIM_EXPROP_LTE: |
| 14903 | wC = wA <= wB; |
| 14904 | goto intresult; |
| 14905 | case JIM_EXPROP_GTE: |
| 14906 | wC = wA >= wB; |
| 14907 | goto intresult; |
| 14908 | case JIM_EXPROP_NUMEQ: |
| 14909 | wC = wA == wB; |
| 14910 | goto intresult; |
| 14911 | case JIM_EXPROP_NUMNE: |
| 14912 | wC = wA != wB; |
| 14913 | goto intresult; |
| 14914 | } |
| 14915 | } |
| 14916 | if (Jim_GetDouble(interp, A, &dA) == JIM_OK && Jim_GetDouble(interp, B, &dB) == JIM_OK) { |
| 14917 | switch (node->type) { |
| 14918 | #ifndef JIM_MATH_FUNCTIONS |
| 14919 | case JIM_EXPROP_POW: |
| 14920 | case JIM_EXPROP_FUNC_POW: |
| 14921 | case JIM_EXPROP_FUNC_ATAN2: |
| 14922 | case JIM_EXPROP_FUNC_HYPOT: |
| 14923 | case JIM_EXPROP_FUNC_FMOD: |
| 14924 | Jim_SetResultString(interp, "unsupported", -1); |
| 14925 | rc = JIM_ERR; |
| 14926 | goto done; |
| 14927 | #else |
| 14928 | case JIM_EXPROP_POW: |
| 14929 | case JIM_EXPROP_FUNC_POW: |
| 14930 | dC = pow(dA, dB); |
| 14931 | goto doubleresult; |
| 14932 | case JIM_EXPROP_FUNC_ATAN2: |
| 14933 | dC = atan2(dA, dB); |
| 14934 | goto doubleresult; |
| 14935 | case JIM_EXPROP_FUNC_HYPOT: |
| 14936 | dC = hypot(dA, dB); |
| 14937 | goto doubleresult; |
| 14938 | case JIM_EXPROP_FUNC_FMOD: |
| 14939 | dC = fmod(dA, dB); |
| 14940 | goto doubleresult; |
| 14941 | #endif |
| 14942 | case JIM_EXPROP_ADD: |
| 14943 | dC = dA + dB; |
| 14944 | goto doubleresult; |
| 14945 | case JIM_EXPROP_SUB: |
| 14946 | dC = dA - dB; |
| 14947 | goto doubleresult; |
| 14948 | case JIM_EXPROP_MUL: |
| 14949 | dC = dA * dB; |
| 14950 | goto doubleresult; |
| 14951 | case JIM_EXPROP_DIV: |
| 14952 | if (dB == 0) { |
| 14953 | #ifdef INFINITY |
| 14954 | dC = dA < 0 ? -INFINITY : INFINITY; |
| 14955 | #else |
| 14956 | dC = (dA < 0 ? -1.0 : 1.0) * strtod("Inf", NULL); |
| 14957 | #endif |
| 14958 | } |
| 14959 | else { |
| 14960 | dC = dA / dB; |
| 14961 | } |
| 14962 | goto doubleresult; |
| 14963 | case JIM_EXPROP_LT: |
| 14964 | wC = dA < dB; |
| 14965 | goto intresult; |
| 14966 | case JIM_EXPROP_GT: |
| 14967 | wC = dA > dB; |
| 14968 | goto intresult; |
| 14969 | case JIM_EXPROP_LTE: |
| 14970 | wC = dA <= dB; |
| 14971 | goto intresult; |
| 14972 | case JIM_EXPROP_GTE: |
| 14973 | wC = dA >= dB; |
| 14974 | goto intresult; |
| 14975 | case JIM_EXPROP_NUMEQ: |
| 14976 | wC = dA == dB; |
| 14977 | goto intresult; |
| 14978 | case JIM_EXPROP_NUMNE: |
| 14979 | wC = dA != dB; |
| 14980 | goto intresult; |
| 14981 | } |
| 14982 | } |
| 14983 | else { |
| 14984 | |
| 14985 | |
| 14986 | |
| 14987 | int i = Jim_StringCompareObj(interp, A, B, 0); |
| 14988 | |
| 14989 | switch (node->type) { |
| 14990 | case JIM_EXPROP_LT: |
| 14991 | wC = i < 0; |
| 14992 | goto intresult; |
| 14993 | case JIM_EXPROP_GT: |
| 14994 | wC = i > 0; |
| 14995 | goto intresult; |
| 14996 | case JIM_EXPROP_LTE: |
| 14997 | wC = i <= 0; |
| 14998 | goto intresult; |
| 14999 | case JIM_EXPROP_GTE: |
| 15000 | wC = i >= 0; |
| 15001 | goto intresult; |
| 15002 | case JIM_EXPROP_NUMEQ: |
| 15003 | wC = i == 0; |
| 15004 | goto intresult; |
| 15005 | case JIM_EXPROP_NUMNE: |
| 15006 | wC = i != 0; |
| 15007 | goto intresult; |
| 15008 | } |
| 15009 | } |
| 15010 | |
| 15011 | rc = JIM_ERR; |
| 15012 | done: |
| 15013 | Jim_DecrRefCount(interp, A); |
| 15014 | Jim_DecrRefCount(interp, B); |
| 15015 | return rc; |
| 15016 | intresult: |
| 15017 | Jim_SetResultInt(interp, wC); |
| 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; |
| 15029 | |
| 15030 | listlen = Jim_ListLength(interp, listObjPtr); |
| 15031 | for (i = 0; i < listlen; i++) { |
| 15032 | if (Jim_StringEqObj(Jim_ListGetIndex(interp, listObjPtr, i), valObj)) { |
| 15033 | return 1; |
| 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; |
| 15063 | jim_wide wC; |
| 15064 | int comp, rc; |
| 15065 | |
| 15066 | if ((rc = JimExprGetTerm(interp, node->left, &A)) != JIM_OK) { |
| 15067 | return rc; |
| 15068 | } |
| 15069 | if ((rc = JimExprGetTerm(interp, node->right, &B)) != JIM_OK) { |
| 15070 | Jim_DecrRefCount(interp, A); |
| 15071 | return rc; |
| 15072 | } |
| 15073 | |
| 15074 | switch (node->type) { |
| 15075 | case JIM_EXPROP_STREQ: |
| 15076 | case JIM_EXPROP_STRNE: |
| 15077 | wC = Jim_StringEqObj(A, B); |
| 15078 | if (node->type == JIM_EXPROP_STRNE) { |
| 15079 | wC = !wC; |
| 15080 | } |
| 15081 | break; |
| 15082 | case JIM_EXPROP_STRLT: |
| 15083 | case JIM_EXPROP_STRGT: |
| 15084 | case JIM_EXPROP_STRLE: |
| 15085 | case JIM_EXPROP_STRGE: |
| 15086 | comp = Jim_StringCompareObj(interp, A, B, 0); |
| 15087 | if (node->type == JIM_EXPROP_STRLT) { |
| 15088 | wC = comp == -1; |
| 15089 | } else if (node->type == JIM_EXPROP_STRGT) { |
| 15090 | wC = comp == 1; |
| 15091 | } else if (node->type == JIM_EXPROP_STRLE) { |
| 15092 | wC = comp == -1 || comp == 0; |
| 15093 | } else { |
| 15094 | wC = comp == 0 || comp == 1; |
| 15095 | } |
| 15096 | break; |
| 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; |
| 15130 | int b; |
| 15131 | int ret = -1; |
| 15132 | |
| 15133 | |
| 15134 | Jim_IncrRefCount(obj); |
| 15135 | |
| 15136 | if (Jim_GetLong(interp, obj, &l) == JIM_OK) { |
| 15137 | ret = (l != 0); |
| 15138 | } |
| 15139 | else if (Jim_GetDouble(interp, obj, &d) == JIM_OK) { |
| 15140 | ret = (d != 0); |
| 15141 | } |
| 15142 | else if (Jim_GetBoolean(interp, obj, &b) == JIM_OK) { |
| 15143 | ret = (b != 0); |
| 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); |
| 15155 | |
| 15156 | if (result == 1) { |
| 15157 | |
| 15158 | result = JimExprGetTermBoolean(interp, node->right); |
| 15159 | } |
| 15160 | if (result == -1) { |
| 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); |
| 15172 | |
| 15173 | if (result == 0) { |
| 15174 | |
| 15175 | result = JimExprGetTermBoolean(interp, node->right); |
| 15176 | } |
| 15177 | if (result == -1) { |
| 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); |
| 15189 | |
| 15190 | if (result == 1) { |
| 15191 | |
| 15192 | return JimExprEvalTermNode(interp, node->right); |
| 15193 | } |
| 15194 | else if (result == 0) { |
| 15195 | |
| 15196 | return JimExprEvalTermNode(interp, node->ternary); |
| 15197 | } |
| 15198 | |
| 15199 | return JIM_ERR; |
| 15200 | } |
| 15201 | |
| 15202 | enum |
| 15203 | { |
| 15204 | OP_FUNC = 0x0001, |
| 15205 | OP_RIGHT_ASSOC = 0x0002, |
| 15206 | }; |
| 15207 | |
| 15208 | #define OPRINIT_ATTR(N, P, ARITY, F, ATTR) {N, F, P, ARITY, ATTR, sizeof(N) - 1} |
| 15209 | #define OPRINIT(N, P, ARITY, F) OPRINIT_ATTR(N, P, ARITY, F, 0) |
| 15210 | |
| 15211 | static const struct Jim_ExprOperator Jim_ExprOperators[] = { |
| 15212 | OPRINIT("*", 110, 2, JimExprOpBin), |
| 15213 | OPRINIT("/", 110, 2, JimExprOpBin), |
| 15214 | OPRINIT("%", 110, 2, JimExprOpIntBin), |
| 15215 | |
| 15216 | OPRINIT("-", 100, 2, JimExprOpBin), |
| 15217 | OPRINIT("+", 100, 2, JimExprOpBin), |
| 15218 | |
| 15219 | OPRINIT("<<", 90, 2, JimExprOpIntBin), |
| 15220 | OPRINIT(">>", 90, 2, JimExprOpIntBin), |
| 15221 | |
| 15222 | OPRINIT("<<<", 90, 2, JimExprOpIntBin), |
| 15223 | OPRINIT(">>>", 90, 2, JimExprOpIntBin), |
| 15224 | |
| 15225 | OPRINIT("<", 80, 2, JimExprOpBin), |
| 15226 | OPRINIT(">", 80, 2, JimExprOpBin), |
| 15227 | OPRINIT("<=", 80, 2, JimExprOpBin), |
| 15228 | OPRINIT(">=", 80, 2, JimExprOpBin), |
| 15229 | |
| 15230 | OPRINIT("==", 70, 2, JimExprOpBin), |
| 15231 | OPRINIT("!=", 70, 2, JimExprOpBin), |
| 15232 | |
| 15233 | OPRINIT("&", 50, 2, JimExprOpIntBin), |
| 15234 | OPRINIT("^", 49, 2, JimExprOpIntBin), |
| 15235 | OPRINIT("|", 48, 2, JimExprOpIntBin), |
| 15236 | |
| 15237 | OPRINIT("&&", 10, 2, JimExprOpAnd), |
| 15238 | OPRINIT("||", 9, 2, JimExprOpOr), |
| 15239 | OPRINIT_ATTR("?", 5, 3, JimExprOpTernary, OP_RIGHT_ASSOC), |
| 15240 | OPRINIT_ATTR(":", 5, 3, NULL, OP_RIGHT_ASSOC), |
| 15241 | |
| 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), |
| 15254 | OPRINIT("gt", 75, 2, JimExprOpStrBin), |
| 15255 | OPRINIT("le", 75, 2, JimExprOpStrBin), |
| 15256 | OPRINIT("ge", 75, 2, JimExprOpStrBin), |
| 15257 | |
| 15258 | OPRINIT_ATTR("!", 150, 1, JimExprOpNumUnary, OP_RIGHT_ASSOC), |
| 15259 | OPRINIT_ATTR("~", 150, 1, JimExprOpIntUnary, OP_RIGHT_ASSOC), |
| 15260 | OPRINIT_ATTR(" -", 150, 1, JimExprOpNumUnary, OP_RIGHT_ASSOC), |
| 15261 | OPRINIT_ATTR(" +", 150, 1, JimExprOpNumUnary, OP_RIGHT_ASSOC), |
| 15262 | |
| 15263 | |
| 15264 | |
| 15265 | OPRINIT_ATTR("int", 200, 1, JimExprOpNumUnary, OP_FUNC), |
| 15266 | OPRINIT_ATTR("wide", 200, 1, JimExprOpNumUnary, OP_FUNC), |
| 15267 | OPRINIT_ATTR("abs", 200, 1, JimExprOpNumUnary, OP_FUNC), |
| 15268 | OPRINIT_ATTR("double", 200, 1, JimExprOpNumUnary, OP_FUNC), |
| 15269 | OPRINIT_ATTR("round", 200, 1, JimExprOpNumUnary, OP_FUNC), |
| 15270 | OPRINIT_ATTR("rand", 200, 0, JimExprOpNone, OP_FUNC), |
| 15271 | OPRINIT_ATTR("srand", 200, 1, JimExprOpIntUnary, OP_FUNC), |
| 15272 | |
| 15273 | #ifdef JIM_MATH_FUNCTIONS |
| 15274 | OPRINIT_ATTR("sin", 200, 1, JimExprOpDoubleUnary, OP_FUNC), |
| 15275 | OPRINIT_ATTR("cos", 200, 1, JimExprOpDoubleUnary, OP_FUNC), |
| 15276 | OPRINIT_ATTR("tan", 200, 1, JimExprOpDoubleUnary, OP_FUNC), |
| 15277 | OPRINIT_ATTR("asin", 200, 1, JimExprOpDoubleUnary, OP_FUNC), |
| 15278 | OPRINIT_ATTR("acos", 200, 1, JimExprOpDoubleUnary, OP_FUNC), |
| 15279 | OPRINIT_ATTR("atan", 200, 1, JimExprOpDoubleUnary, OP_FUNC), |
| 15280 | OPRINIT_ATTR("atan2", 200, 2, JimExprOpBin, OP_FUNC), |
| 15281 | OPRINIT_ATTR("sinh", 200, 1, JimExprOpDoubleUnary, OP_FUNC), |
| 15282 | OPRINIT_ATTR("cosh", 200, 1, JimExprOpDoubleUnary, OP_FUNC), |
| 15283 | OPRINIT_ATTR("tanh", 200, 1, JimExprOpDoubleUnary, OP_FUNC), |
| 15284 | OPRINIT_ATTR("ceil", 200, 1, JimExprOpDoubleUnary, OP_FUNC), |
| 15285 | OPRINIT_ATTR("floor", 200, 1, JimExprOpDoubleUnary, OP_FUNC), |
| 15286 | OPRINIT_ATTR("exp", 200, 1, JimExprOpDoubleUnary, OP_FUNC), |
| 15287 | OPRINIT_ATTR("log", 200, 1, JimExprOpDoubleUnary, OP_FUNC), |
| 15288 | OPRINIT_ATTR("log10", 200, 1, JimExprOpDoubleUnary, OP_FUNC), |
| 15289 | OPRINIT_ATTR("sqrt", 200, 1, JimExprOpDoubleUnary, OP_FUNC), |
| 15290 | OPRINIT_ATTR("pow", 200, 2, JimExprOpBin, OP_FUNC), |
| 15291 | OPRINIT_ATTR("hypot", 200, 2, JimExprOpBin, OP_FUNC), |
| 15292 | OPRINIT_ATTR("fmod", 200, 2, JimExprOpBin, OP_FUNC), |
| 15293 | #endif |
| 15294 | }; |
| 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 | |
| 15306 | while (1) { |
| 15307 | |
| 15308 | while (isspace(UCHAR(*pc->p)) || (*(pc->p) == '\\' && *(pc->p + 1) == '\n')) { |
| 15309 | if (*pc->p == '\n') { |
| 15310 | pc->linenr++; |
| 15311 | } |
| 15312 | pc->p++; |
| 15313 | pc->len--; |
| 15314 | } |
| 15315 | |
| 15316 | if (*pc->p == '#') { |
| 15317 | JimParseComment(pc); |
| 15318 | |
| 15319 | continue; |
| 15320 | } |
| 15321 | break; |
| 15322 | } |
| 15323 | |
| 15324 | |
| 15325 | pc->tline = pc->linenr; |
| 15326 | pc->tstart = pc->p; |
| 15327 | |
| 15328 | if (pc->len == 0) { |
| 15329 | pc->tend = pc->p; |
| 15330 | pc->tt = JIM_TT_EOL; |
| 15331 | pc->eof = 1; |
| 15332 | return JIM_OK; |
| 15333 | } |
| 15334 | switch (*(pc->p)) { |
| 15335 | case '(': |
| 15336 | pc->tt = JIM_TT_SUBEXPR_START; |
| 15337 | goto singlechar; |
| 15338 | case ')': |
| 15339 | pc->tt = JIM_TT_SUBEXPR_END; |
| 15340 | goto singlechar; |
| 15341 | case ',': |
| 15342 | pc->tt = JIM_TT_SUBEXPR_COMMA; |
| 15343 | singlechar: |
| 15344 | pc->tend = pc->p; |
| 15345 | pc->p++; |
| 15346 | pc->len--; |
| 15347 | break; |
| 15348 | case '[': |
| 15349 | return JimParseCmd(pc); |
| 15350 | case '$': |
| 15351 | if (JimParseVar(pc) == JIM_ERR) |
| 15352 | return JimParseExprOperator(pc); |
| 15353 | else { |
| 15354 | |
| 15355 | if (pc->tt == JIM_TT_EXPRSUGAR) { |
| 15356 | pc->errmsg = "nesting expr in expr is not allowed"; |
| 15357 | return JIM_ERR; |
| 15358 | } |
| 15359 | return JIM_OK; |
| 15360 | } |
| 15361 | break; |
| 15362 | case '0': |
| 15363 | case '1': |
| 15364 | case '2': |
| 15365 | case '3': |
| 15366 | case '4': |
| 15367 | case '5': |
| 15368 | case '6': |
| 15369 | case '7': |
| 15370 | case '8': |
| 15371 | case '9': |
| 15372 | case '.': |
| 15373 | return JimParseExprNumber(pc); |
| 15374 | case '"': |
| 15375 | return JimParseQuote(pc); |
| 15376 | case '{': |
| 15377 | return JimParseBrace(pc); |
| 15378 | |
| 15379 | case 'N': |
| 15380 | case 'I': |
| 15381 | case 'n': |
| 15382 | case 'i': |
| 15383 | if (JimParseExprIrrational(pc) == JIM_ERR) |
| 15384 | if (JimParseExprBoolean(pc) == JIM_ERR) |
| 15385 | return JimParseExprOperator(pc); |
| 15386 | break; |
| 15387 | case 't': |
| 15388 | case 'f': |
| 15389 | case 'o': |
| 15390 | case 'y': |
| 15391 | if (JimParseExprBoolean(pc) == JIM_ERR) |
| 15392 | return JimParseExprOperator(pc); |
| 15393 | break; |
| 15394 | default: |
| 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 | |
| 15406 | |
| 15407 | pc->tt = JIM_TT_EXPR_INT; |
| 15408 | |
| 15409 | jim_strtoull(pc->p, (char **)&pc->p); |
| 15410 | |
| 15411 | if (strchr("eENnIi.", *pc->p) || pc->p == pc->tstart) { |
| 15412 | if (strtod(pc->tstart, &end)) { } |
| 15413 | if (end == pc->tstart) |
| 15414 | return JIM_ERR; |
| 15415 | if (end > pc->p) { |
| 15416 | |
| 15417 | pc->tt = JIM_TT_EXPR_DOUBLE; |
| 15418 | pc->p = end; |
| 15419 | } |
| 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; |
| 15431 | |
| 15432 | for (i = 0; irrationals[i]; i++) { |
| 15433 | const char *irr = irrationals[i]; |
| 15434 | |
| 15435 | if (strncmp(irr, pc->p, 3) == 0) { |
| 15436 | pc->p += 3; |
| 15437 | pc->len -= 3; |
| 15438 | pc->tend = pc->p - 1; |
| 15439 | pc->tt = JIM_TT_EXPR_DOUBLE; |
| 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++) { |
| 15451 | if (strncmp(pc->p, jim_true_false_strings[i], jim_true_false_lens[i]) == 0) { |
| 15452 | pc->p += jim_true_false_lens[i]; |
| 15453 | pc->len -= jim_true_false_lens[i]; |
| 15454 | pc->tend = pc->p - 1; |
| 15455 | pc->tt = JIM_TT_EXPR_BOOLEAN; |
| 15456 | return JIM_OK; |
| 15457 | } |
| 15458 | } |
| 15459 | return JIM_ERR; |
| 15460 | } |
| 15461 | |
| 15462 | static const struct Jim_ExprOperator *JimExprOperatorInfoByOpcode(int opcode) |
| 15463 | { |
| 15464 | static Jim_ExprOperator dummy_op; |
| 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; |
| 15476 | int bestLen = 0; |
| 15477 | |
| 15478 | |
| 15479 | for (i = 0; i < (signed)JIM_EXPR_OPERATORS_NUM; i++) { |
| 15480 | const struct Jim_ExprOperator *op = &Jim_ExprOperators[i]; |
| 15481 | |
| 15482 | if (op->name[0] != pc->p[0]) { |
| 15483 | continue; |
| 15484 | } |
| 15485 | |
| 15486 | if (op->namelen > bestLen && strncmp(op->name, pc->p, op->namelen) == 0) { |
| 15487 | bestOp = op; |
| 15488 | bestLen = op->namelen; |
| 15489 | } |
| 15490 | } |
| 15491 | if (bestOp == NULL) { |
| 15492 | return JIM_ERR; |
| 15493 | } |
| 15494 | |
| 15495 | |
| 15496 | if (bestOp->attr & OP_FUNC) { |
| 15497 | const char *p = pc->p + bestLen; |
| 15498 | int len = pc->len - bestLen; |
| 15499 | |
| 15500 | while (len && isspace(UCHAR(*p))) { |
| 15501 | len--; |
| 15502 | p++; |
| 15503 | } |
| 15504 | if (*p != '(') { |
| 15505 | pc->errmsg = "function requires parentheses"; |
| 15506 | return JIM_ERR; |
| 15507 | } |
| 15508 | } |
| 15509 | pc->tend = pc->p + bestLen - 1; |
| 15510 | pc->p += bestLen; |
| 15511 | pc->len -= bestLen; |
| 15512 | |
| 15513 | pc->tt = (bestOp - Jim_ExprOperators) + JIM_TT_EXPR_OP; |
| 15514 | return JIM_OK; |
| 15515 | } |
| 15516 | |
| 15517 | |
| 15518 | static void FreeExprInternalRep(Jim_Interp *interp, Jim_Obj *objPtr); |
| 15519 | static void DupExprInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr); |
| 15520 | static int SetExprFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr); |
| 15521 | |
| 15522 | static const Jim_ObjType exprObjType = { |
| 15523 | "expression", |
| 15524 | FreeExprInternalRep, |
| 15525 | DupExprInternalRep, |
| 15526 | NULL, |
| 15527 | JIM_TYPE_NONE, |
| 15528 | }; |
| 15529 | |
| 15530 | |
| 15531 | struct ExprTree |
| 15532 | { |
| 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++) { |
| 15544 | if (nodes[i].objPtr) { |
| 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 | |
| 15563 | if (expr) { |
| 15564 | if (--expr->inUse != 0) { |
| 15565 | return; |
| 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); |
| 15577 | |
| 15578 | |
| 15579 | dupPtr->typePtr = NULL; |
| 15580 | } |
| 15581 | |
| 15582 | struct ExprBuilder { |
| 15583 | int parencount; |
| 15584 | int level; |
| 15585 | ParseToken *token; |
| 15586 | ParseToken *first_token; |
| 15587 | Jim_Stack stack; |
| 15588 | Jim_Obj *exprObjPtr; |
| 15589 | Jim_Obj *fileNameObj; |
| 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(" "); |
| 15601 | } |
| 15602 | if (TOKEN_IS_EXPR_OP(node->type)) { |
| 15603 | printf("%s\n", jim_tt_name(node->type)); |
| 15604 | if (node->left) { |
| 15605 | JimShowExprNode(node->left, level + 1); |
| 15606 | } |
| 15607 | if (node->right) { |
| 15608 | JimShowExprNode(node->right, level + 1); |
| 15609 | } |
| 15610 | if (node->ternary) { |
| 15611 | JimShowExprNode(node->ternary, level + 1); |
| 15612 | } |
| 15613 | } |
| 15614 | else { |
| 15615 | printf("[%s] %s\n", jim_tt_name(node->type), Jim_String(node->objPtr)); |
| 15616 | } |
| 15617 | } |
| 15618 | #endif |
| 15619 | |
| 15620 | #define EXPR_UNTIL_CLOSE 0x0001 |
| 15621 | #define EXPR_FUNC_ARGS 0x0002 |
| 15622 | #define EXPR_TERNARY 0x0004 |
| 15623 | |
| 15624 | static int ExprTreeBuildTree(Jim_Interp *interp, struct ExprBuilder *builder, int precedence, int flags, int exp_numterms) { |
| 15625 | int rc; |
| 15626 | struct JimExprNode *node; |
| 15627 | |
| 15628 | int exp_stacklen = builder->stack.len + exp_numterms; |
| 15629 | |
| 15630 | if (builder->level++ > 200) { |
| 15631 | Jim_SetResultString(interp, "Expression too complex", -1); |
| 15632 | return JIM_ERR; |
| 15633 | } |
| 15634 | |
| 15635 | while (builder->token->type != JIM_TT_EOL) { |
| 15636 | ParseToken *t = builder->token++; |
| 15637 | int prevtt; |
| 15638 | |
| 15639 | if (t == builder->first_token) { |
| 15640 | prevtt = JIM_TT_NONE; |
| 15641 | } |
| 15642 | else { |
| 15643 | prevtt = t[-1].type; |
| 15644 | } |
| 15645 | |
| 15646 | if (t->type == JIM_TT_SUBEXPR_START) { |
| 15647 | if (builder->stack.len == exp_stacklen) { |
| 15648 | Jim_SetResultFormatted(interp, "unexpected open parenthesis in expression: \"%#s\"", builder->exprObjPtr); |
| 15649 | return JIM_ERR; |
| 15650 | } |
| 15651 | builder->parencount++; |
| 15652 | rc = ExprTreeBuildTree(interp, builder, 0, EXPR_UNTIL_CLOSE, 1); |
| 15653 | if (rc != JIM_OK) { |
| 15654 | return rc; |
| 15655 | } |
| 15656 | |
| 15657 | } |
| 15658 | else if (t->type == JIM_TT_SUBEXPR_END) { |
| 15659 | if (!(flags & EXPR_UNTIL_CLOSE)) { |
| 15660 | if (builder->stack.len == exp_stacklen && builder->level > 1) { |
| 15661 | builder->token--; |
| 15662 | builder->level--; |
| 15663 | return JIM_OK; |
| 15664 | } |
| 15665 | Jim_SetResultFormatted(interp, "unexpected closing parenthesis in expression: \"%#s\"", builder->exprObjPtr); |
| 15666 | return JIM_ERR; |
| 15667 | } |
| 15668 | builder->parencount--; |
| 15669 | if (builder->stack.len == exp_stacklen) { |
| 15670 | |
| 15671 | break; |
| 15672 | } |
| 15673 | } |
| 15674 | else if (t->type == JIM_TT_SUBEXPR_COMMA) { |
| 15675 | if (!(flags & EXPR_FUNC_ARGS)) { |
| 15676 | if (builder->stack.len == exp_stacklen) { |
| 15677 | |
| 15678 | builder->token--; |
| 15679 | builder->level--; |
| 15680 | return JIM_OK; |
| 15681 | } |
| 15682 | Jim_SetResultFormatted(interp, "unexpected comma in expression: \"%#s\"", builder->exprObjPtr); |
| 15683 | return JIM_ERR; |
| 15684 | } |
| 15685 | else { |
| 15686 | |
| 15687 | if (builder->stack.len > exp_stacklen) { |
| 15688 | Jim_SetResultFormatted(interp, "too many arguments to math function"); |
| 15689 | return JIM_ERR; |
| 15690 | } |
| 15691 | } |
| 15692 | |
| 15693 | } |
| 15694 | else if (t->type == JIM_EXPROP_COLON) { |
| 15695 | if (!(flags & EXPR_TERNARY)) { |
| 15696 | if (builder->level != 1) { |
| 15697 | |
| 15698 | builder->token--; |
| 15699 | builder->level--; |
| 15700 | return JIM_OK; |
| 15701 | } |
| 15702 | Jim_SetResultFormatted(interp, ": without ? in expression: \"%#s\"", builder->exprObjPtr); |
| 15703 | return JIM_ERR; |
| 15704 | } |
| 15705 | if (builder->stack.len == exp_stacklen) { |
| 15706 | |
| 15707 | builder->token--; |
| 15708 | builder->level--; |
| 15709 | return JIM_OK; |
| 15710 | } |
| 15711 | |
| 15712 | } |
| 15713 | else if (TOKEN_IS_EXPR_OP(t->type)) { |
| 15714 | const struct Jim_ExprOperator *op; |
| 15715 | |
| 15716 | |
| 15717 | if (TOKEN_IS_EXPR_OP(prevtt) || TOKEN_IS_EXPR_START(prevtt)) { |
| 15718 | if (t->type == JIM_EXPROP_SUB) { |
| 15719 | t->type = JIM_EXPROP_UNARYMINUS; |
| 15720 | } |
| 15721 | else if (t->type == JIM_EXPROP_ADD) { |
| 15722 | t->type = JIM_EXPROP_UNARYPLUS; |
| 15723 | } |
| 15724 | } |
| 15725 | |
| 15726 | op = JimExprOperatorInfoByOpcode(t->type); |
| 15727 | |
| 15728 | if (op->precedence < precedence || (!(op->attr & OP_RIGHT_ASSOC) && op->precedence == precedence)) { |
| 15729 | |
| 15730 | builder->token--; |
| 15731 | break; |
| 15732 | } |
| 15733 | |
| 15734 | if (op->attr & OP_FUNC) { |
| 15735 | if (builder->token->type != JIM_TT_SUBEXPR_START) { |
| 15736 | Jim_SetResultString(interp, "missing arguments for math function", -1); |
| 15737 | return JIM_ERR; |
| 15738 | } |
| 15739 | builder->token++; |
| 15740 | if (op->arity == 0) { |
| 15741 | if (builder->token->type != JIM_TT_SUBEXPR_END) { |
| 15742 | Jim_SetResultString(interp, "too many arguments for math function", -1); |
| 15743 | return JIM_ERR; |
| 15744 | } |
| 15745 | builder->token++; |
| 15746 | goto noargs; |
| 15747 | } |
| 15748 | builder->parencount++; |
| 15749 | |
| 15750 | |
| 15751 | rc = ExprTreeBuildTree(interp, builder, 0, EXPR_FUNC_ARGS | EXPR_UNTIL_CLOSE, op->arity); |
| 15752 | } |
| 15753 | else if (t->type == JIM_EXPROP_TERNARY) { |
| 15754 | |
| 15755 | rc = ExprTreeBuildTree(interp, builder, op->precedence, EXPR_TERNARY, 2); |
| 15756 | } |
| 15757 | else { |
| 15758 | rc = ExprTreeBuildTree(interp, builder, op->precedence, 0, 1); |
| 15759 | } |
| 15760 | |
| 15761 | if (rc != JIM_OK) { |
| 15762 | return rc; |
| 15763 | } |
| 15764 | |
| 15765 | noargs: |
| 15766 | node = builder->next++; |
| 15767 | node->type = t->type; |
| 15768 | |
| 15769 | if (op->arity >= 3) { |
| 15770 | node->ternary = Jim_StackPop(&builder->stack); |
| 15771 | if (node->ternary == NULL) { |
| 15772 | goto missingoperand; |
| 15773 | } |
| 15774 | } |
| 15775 | if (op->arity >= 2) { |
| 15776 | node->right = Jim_StackPop(&builder->stack); |
| 15777 | if (node->right == NULL) { |
| 15778 | goto missingoperand; |
| 15779 | } |
| 15780 | } |
| 15781 | if (op->arity >= 1) { |
| 15782 | node->left = Jim_StackPop(&builder->stack); |
| 15783 | if (node->left == NULL) { |
| 15784 | missingoperand: |
| 15785 | Jim_SetResultFormatted(interp, "missing operand to %s in expression: \"%#s\"", op->name, builder->exprObjPtr); |
| 15786 | builder->next--; |
| 15787 | return JIM_ERR; |
| 15788 | |
| 15789 | } |
| 15790 | } |
| 15791 | |
| 15792 | |
| 15793 | Jim_StackPush(&builder->stack, node); |
| 15794 | } |
| 15795 | else { |
| 15796 | Jim_Obj *objPtr = NULL; |
| 15797 | |
| 15798 | |
| 15799 | |
| 15800 | |
| 15801 | if (!TOKEN_IS_EXPR_START(prevtt) && !TOKEN_IS_EXPR_OP(prevtt)) { |
| 15802 | Jim_SetResultFormatted(interp, "missing operator in expression: \"%#s\"", builder->exprObjPtr); |
| 15803 | return JIM_ERR; |
| 15804 | } |
| 15805 | |
| 15806 | |
| 15807 | if (t->type == JIM_TT_EXPR_INT || t->type == JIM_TT_EXPR_DOUBLE) { |
| 15808 | char *endptr; |
| 15809 | if (t->type == JIM_TT_EXPR_INT) { |
| 15810 | objPtr = Jim_NewIntObj(interp, jim_strtoull(t->token, &endptr)); |
| 15811 | } |
| 15812 | else { |
| 15813 | objPtr = Jim_NewDoubleObj(interp, strtod(t->token, &endptr)); |
| 15814 | } |
| 15815 | if (endptr != t->token + t->len) { |
| 15816 | |
| 15817 | Jim_FreeNewObj(interp, objPtr); |
| 15818 | objPtr = NULL; |
| 15819 | } |
| 15820 | } |
| 15821 | |
| 15822 | if (!objPtr) { |
| 15823 | |
| 15824 | objPtr = Jim_NewStringObj(interp, t->token, t->len); |
| 15825 | if (t->type == JIM_TT_CMD) { |
| 15826 | |
| 15827 | Jim_SetSourceInfo(interp, objPtr, builder->fileNameObj, t->line); |
| 15828 | } |
| 15829 | } |
| 15830 | |
| 15831 | |
| 15832 | node = builder->next++; |
| 15833 | node->objPtr = objPtr; |
| 15834 | Jim_IncrRefCount(node->objPtr); |
| 15835 | node->type = t->type; |
| 15836 | Jim_StackPush(&builder->stack, node); |
| 15837 | } |
| 15838 | } |
| 15839 | |
| 15840 | if (builder->stack.len == exp_stacklen) { |
| 15841 | builder->level--; |
| 15842 | return JIM_OK; |
| 15843 | } |
| 15844 | |
| 15845 | if ((flags & EXPR_FUNC_ARGS)) { |
| 15846 | Jim_SetResultFormatted(interp, "too %s arguments for math function", (builder->stack.len < exp_stacklen) ? "few" : "many"); |
| 15847 | } |
| 15848 | else { |
| 15849 | if (builder->stack.len < exp_stacklen) { |
| 15850 | if (builder->level == 0) { |
| 15851 | Jim_SetResultFormatted(interp, "empty expression"); |
| 15852 | } |
| 15853 | else { |
| 15854 | Jim_SetResultFormatted(interp, "syntax error in expression \"%#s\": premature end of expression", builder->exprObjPtr); |
| 15855 | } |
| 15856 | } |
| 15857 | else { |
| 15858 | Jim_SetResultFormatted(interp, "extra terms after expression"); |
| 15859 | } |
| 15860 | } |
| 15861 | |
| 15862 | return JIM_ERR; |
| 15863 | } |
| 15864 | |
| 15865 | static struct ExprTree *ExprTreeCreateTree(Jim_Interp *interp, const ParseTokenList *tokenlist, Jim_Obj *exprObjPtr, Jim_Obj *fileNameObj) |
| 15866 | { |
| 15867 | struct ExprTree *expr; |
| 15868 | struct ExprBuilder builder; |
| 15869 | int rc; |
| 15870 | struct JimExprNode *top = NULL; |
| 15871 | |
| 15872 | builder.parencount = 0; |
| 15873 | builder.level = 0; |
| 15874 | builder.token = builder.first_token = tokenlist->list; |
| 15875 | builder.exprObjPtr = exprObjPtr; |
| 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); |
| 15887 | |
| 15888 | if (builder.parencount) { |
| 15889 | Jim_SetResultString(interp, "missing close parenthesis", -1); |
| 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 | } |
| 15901 | |
| 15902 | expr = Jim_Alloc(sizeof(*expr)); |
| 15903 | expr->inUse = 1; |
| 15904 | expr->expr = top; |
| 15905 | expr->nodes = builder.nodes; |
| 15906 | expr->len = builder.next - builder.nodes; |
| 15907 | |
| 15908 | assert(expr->len <= tokenlist->count - 1); |
| 15909 | |
| 15910 | return expr; |
| 15911 | } |
| 15912 | |
| 15913 | static int SetExprFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr) |
| 15914 | { |
| 15915 | int exprTextLen; |
| 15916 | const char *exprText; |
| 15917 | struct JimParserCtx parser; |
| 15918 | struct ExprTree *expr; |
| 15919 | ParseTokenList tokenlist; |
| 15920 | int line; |
| 15921 | Jim_Obj *fileNameObj; |
| 15922 | int rc = JIM_ERR; |
| 15923 | |
| 15924 | |
| 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) { |
| 15938 | if (JimParseExpression(&parser) != JIM_OK) { |
| 15939 | ScriptTokenListFree(&tokenlist); |
| 15940 | Jim_SetResultFormatted(interp, "syntax error in expression: \"%#s\"", objPtr); |
| 15941 | if (parser.errmsg) { |
| 15942 | Jim_AppendStrings(interp, Jim_GetResult(interp), ": ", parser.errmsg, NULL); |
| 15943 | } |
| 15944 | expr = NULL; |
| 15945 | goto err; |
| 15946 | } |
| 15947 | |
| 15948 | ScriptAddToken(&tokenlist, parser.tstart, parser.tend - parser.tstart + 1, parser.tt, |
| 15949 | parser.tline); |
| 15950 | } |
| 15951 | |
| 15952 | #ifdef DEBUG_SHOW_EXPR_TOKENS |
| 15953 | { |
| 15954 | int i; |
| 15955 | printf("==== Expr Tokens (%s) ====\n", Jim_String(fileNameObj)); |
| 15956 | for (i = 0; i < tokenlist.count; i++) { |
| 15957 | printf("[%2d]@%d %s '%.*s'\n", i, tokenlist.list[i].line, jim_tt_name(tokenlist.list[i].type), |
| 15958 | tokenlist.list[i].len, tokenlist.list[i].token); |
| 15959 | } |
| 15960 | } |
| 15961 | #endif |
| 15962 | |
| 15963 | if (tokenlist.count <= 1) { |
| 15964 | Jim_SetResultString(interp, "empty expression", -1); |
| 15965 | rc = JIM_ERR; |
| 15966 | } |
| 15967 | else { |
| 15968 | rc = JimParseCheckMissing(interp, parser.missing.ch); |
| 15969 | } |
| 15970 | if (rc != JIM_OK) { |
| 15971 | ScriptTokenListFree(&tokenlist); |
| 15972 | Jim_DecrRefCount(interp, fileNameObj); |
| 15973 | return rc; |
| 15974 | } |
| 15975 | |
| 15976 | |
| 15977 | expr = ExprTreeCreateTree(interp, &tokenlist, objPtr, fileNameObj); |
| 15978 | |
| 15979 | |
| 15980 | ScriptTokenListFree(&tokenlist); |
| 15981 | |
| 15982 | if (!expr) { |
| 15983 | goto err; |
| 15984 | } |
| 15985 | |
| 15986 | #ifdef DEBUG_SHOW_EXPR |
| 15987 | printf("==== Expr ====\n"); |
| 15988 | JimShowExprNode(expr->expr, 0); |
| 15989 | #endif |
| 15990 | |
| 15991 | rc = JIM_OK; |
| 15992 | |
| 15993 | err: |
| 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 | { |
| 16007 | if (objPtr->typePtr != &exprObjType) { |
| 16008 | if (SetExprFromAny(interp, objPtr) != JIM_OK) { |
| 16009 | return NULL; |
| 16010 | } |
| 16011 | } |
| 16012 | return (struct ExprTree *) Jim_GetIntRepPtr(objPtr); |
| 16013 | } |
| 16014 | |
| 16015 | #ifdef JIM_OPTIMIZATION |
| 16016 | static Jim_Obj *JimExprIntValOrVar(Jim_Interp *interp, struct JimExprNode *node) |
| 16017 | { |
| 16018 | if (node->type == JIM_TT_EXPR_INT) |
| 16019 | return node->objPtr; |
| 16020 | else if (node->type == JIM_TT_VAR) |
| 16021 | return Jim_GetVariable(interp, node->objPtr, JIM_NONE); |
| 16022 | else if (node->type == JIM_TT_DICTSUGAR) |
| 16023 | return JimExpandDictSugar(interp, node->objPtr); |
| 16024 | else |
| 16025 | return NULL; |
| 16026 | } |
| 16027 | #endif |
| 16028 | |
| 16029 | |
| 16030 | static int JimExprEvalTermNode(Jim_Interp *interp, struct JimExprNode *node) |
| 16031 | { |
| 16032 | if (TOKEN_IS_EXPR_OP(node->type)) { |
| 16033 | const struct Jim_ExprOperator *op = JimExprOperatorInfoByOpcode(node->type); |
| 16034 | return op->funcop(interp, node); |
| 16035 | } |
| 16036 | else { |
| 16037 | Jim_Obj *objPtr; |
| 16038 | |
| 16039 | |
| 16040 | switch (node->type) { |
| 16041 | case JIM_TT_EXPR_INT: |
| 16042 | case JIM_TT_EXPR_DOUBLE: |
| 16043 | case JIM_TT_EXPR_BOOLEAN: |
| 16044 | case JIM_TT_STR: |
| 16045 | Jim_SetResult(interp, node->objPtr); |
| 16046 | return JIM_OK; |
| 16047 | |
| 16048 | case JIM_TT_VAR: |
| 16049 | objPtr = Jim_GetVariable(interp, node->objPtr, JIM_ERRMSG); |
| 16050 | if (objPtr) { |
| 16051 | Jim_SetResult(interp, objPtr); |
| 16052 | return JIM_OK; |
| 16053 | } |
| 16054 | return JIM_ERR; |
| 16055 | |
| 16056 | case JIM_TT_DICTSUGAR: |
| 16057 | objPtr = JimExpandDictSugar(interp, node->objPtr); |
| 16058 | if (objPtr) { |
| 16059 | Jim_SetResult(interp, objPtr); |
| 16060 | return JIM_OK; |
| 16061 | } |
| 16062 | return JIM_ERR; |
| 16063 | |
| 16064 | case JIM_TT_ESC: |
| 16065 | if (interp->safeexpr) { |
| 16066 | return JIM_ERR; |
| 16067 | } |
| 16068 | if (Jim_SubstObj(interp, node->objPtr, &objPtr, JIM_NONE) == JIM_OK) { |
| 16069 | Jim_SetResult(interp, objPtr); |
| 16070 | return JIM_OK; |
| 16071 | } |
| 16072 | return JIM_ERR; |
| 16073 | |
| 16074 | case JIM_TT_CMD: |
| 16075 | if (interp->safeexpr) { |
| 16076 | return JIM_ERR; |
| 16077 | } |
| 16078 | return Jim_EvalObj(interp, node->objPtr); |
| 16079 | |
| 16080 | default: |
| 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) { |
| 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)); |
| 16103 | } |
| 16104 | return -1; |
| 16105 | } |
| 16106 | |
| 16107 | int Jim_EvalExpression(Jim_Interp *interp, Jim_Obj *exprObjPtr) |
| 16108 | { |
| 16109 | struct ExprTree *expr; |
| 16110 | int retcode = JIM_OK; |
| 16111 | |
| 16112 | Jim_IncrRefCount(exprObjPtr); |
| 16113 | expr = JimGetExpression(interp, exprObjPtr); |
| 16114 | if (!expr) { |
| 16115 | retcode = JIM_ERR; |
| 16116 | goto done; |
| 16117 | } |
| 16118 | |
| 16119 | #ifdef JIM_OPTIMIZATION |
| 16120 | if (!interp->safeexpr) { |
| 16121 | Jim_Obj *objPtr; |
| 16122 | |
| 16123 | |
| 16124 | switch (expr->len) { |
| 16125 | case 1: |
| 16126 | objPtr = JimExprIntValOrVar(interp, expr->expr); |
| 16127 | if (objPtr) { |
| 16128 | Jim_SetResult(interp, objPtr); |
| 16129 | goto done; |
| 16130 | } |
| 16131 | break; |
| 16132 | |
| 16133 | case 2: |
| 16134 | if (expr->expr->type == JIM_EXPROP_NOT) { |
| 16135 | objPtr = JimExprIntValOrVar(interp, expr->expr->left); |
| 16136 | |
| 16137 | if (objPtr && JimIsWide(objPtr)) { |
| 16138 | Jim_SetResult(interp, JimWideValue(objPtr) ? interp->falseObj : interp->trueObj); |
| 16139 | goto done; |
| 16140 | } |
| 16141 | } |
| 16142 | break; |
| 16143 | |
| 16144 | case 3: |
| 16145 | objPtr = JimExprIntValOrVar(interp, expr->expr->left); |
| 16146 | if (objPtr && JimIsWide(objPtr)) { |
| 16147 | Jim_Obj *objPtr2 = JimExprIntValOrVar(interp, expr->expr->right); |
| 16148 | if (objPtr2 && JimIsWide(objPtr2)) { |
| 16149 | jim_wide wideValueA = JimWideValue(objPtr); |
| 16150 | jim_wide wideValueB = JimWideValue(objPtr2); |
| 16151 | int cmpRes; |
| 16152 | switch (expr->expr->type) { |
| 16153 | case JIM_EXPROP_LT: |
| 16154 | cmpRes = wideValueA < wideValueB; |
| 16155 | break; |
| 16156 | case JIM_EXPROP_LTE: |
| 16157 | cmpRes = wideValueA <= wideValueB; |
| 16158 | break; |
| 16159 | case JIM_EXPROP_GT: |
| 16160 | cmpRes = wideValueA > wideValueB; |
| 16161 | break; |
| 16162 | case JIM_EXPROP_GTE: |
| 16163 | cmpRes = wideValueA >= wideValueB; |
| 16164 | break; |
| 16165 | case JIM_EXPROP_NUMEQ: |
| 16166 | cmpRes = wideValueA == wideValueB; |
| 16167 | break; |
| 16168 | case JIM_EXPROP_NUMNE: |
| 16169 | cmpRes = wideValueA != wideValueB; |
| 16170 | break; |
| 16171 | default: |
| 16172 | goto noopt; |
| 16173 | } |
| 16174 | Jim_SetResult(interp, cmpRes ? interp->trueObj : interp->falseObj); |
| 16175 | goto done; |
| 16176 | } |
| 16177 | } |
| 16178 | break; |
| 16179 | } |
| 16180 | } |
| 16181 | noopt: |
| 16182 | #endif |
| 16183 | |
| 16184 | expr->inUse++; |
| 16185 | |
| 16186 | |
| 16187 | retcode = JimExprEvalTermNode(interp, expr->expr); |
| 16188 | |
| 16189 | |
| 16190 | Jim_FreeIntRep(interp, exprObjPtr); |
| 16191 | exprObjPtr->typePtr = &exprObjType; |
| 16192 | Jim_SetIntRepPtr(exprObjPtr, expr); |
| 16193 | |
| 16194 | done: |
| 16195 | Jim_DecrRefCount(interp, exprObjPtr); |
| 16196 | |
| 16197 | return retcode; |
| 16198 | } |
| 16199 | |
| 16200 | int Jim_GetBoolFromExpr(Jim_Interp *interp, Jim_Obj *exprObjPtr, int *boolPtr) |
| 16201 | { |
| 16202 | int retcode = Jim_EvalExpression(interp, exprObjPtr); |
| 16203 | |
| 16204 | if (retcode == JIM_OK) { |
| 16205 | switch (ExprBool(interp, Jim_GetResult(interp))) { |
| 16206 | case 0: |
| 16207 | *boolPtr = 0; |
| 16208 | break; |
| 16209 | |
| 16210 | case 1: |
| 16211 | *boolPtr = 1; |
| 16212 | break; |
| 16213 | |
| 16214 | case -1: |
| 16215 | retcode = JIM_ERR; |
| 16216 | break; |
| 16217 | } |
| 16218 | } |
| 16219 | return retcode; |
| 16220 | } |
| 16221 | |
| 16222 | |
| 16223 | |
| 16224 | |
| 16225 | typedef struct ScanFmtPartDescr |
| 16226 | { |
| 16227 | const char *arg; |
| 16228 | const char *prefix; |
| 16229 | size_t width; |
| 16230 | int pos; |
| 16231 | char type; |
| 16232 | char modifier; |
| 16233 | } ScanFmtPartDescr; |
| 16234 | |
| 16235 | |
| 16236 | typedef struct ScanFmtStringObj |
| 16237 | { |
| 16238 | jim_wide size; |
| 16239 | char *stringRep; |
| 16240 | size_t count; |
| 16241 | size_t convCount; |
| 16242 | size_t maxPos; |
| 16243 | const char *error; |
| 16244 | char *scratch; |
| 16245 | ScanFmtPartDescr descr[1]; |
| 16246 | } ScanFmtStringObj; |
| 16247 | |
| 16248 | |
| 16249 | static void FreeScanFmtInternalRep(Jim_Interp *interp, Jim_Obj *objPtr); |
| 16250 | static void DupScanFmtInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr); |
| 16251 | static void UpdateStringOfScanFmt(Jim_Obj *objPtr); |
| 16252 | |
| 16253 | static const Jim_ObjType scanFmtStringObjType = { |
| 16254 | "scanformatstring", |
| 16255 | FreeScanFmtInternalRep, |
| 16256 | DupScanFmtInternalRep, |
| 16257 | UpdateStringOfScanFmt, |
| 16258 | JIM_TYPE_NONE, |
| 16259 | }; |
| 16260 | |
| 16261 | void FreeScanFmtInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) |
| 16262 | { |
| 16263 | JIM_NOTUSED(interp); |
| 16264 | Jim_Free((char *)objPtr->internalRep.ptr); |
| 16265 | objPtr->internalRep.ptr = 0; |
| 16266 | } |
| 16267 | |
| 16268 | void DupScanFmtInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) |
| 16269 | { |
| 16270 | size_t size = (size_t) ((ScanFmtStringObj *) srcPtr->internalRep.ptr)->size; |
| 16271 | ScanFmtStringObj *newVec = (ScanFmtStringObj *) Jim_Alloc(size); |
| 16272 | |
| 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 | } |
| 16284 | |
| 16285 | |
| 16286 | static int SetScanFmtFromAny(Jim_Interp *interp, Jim_Obj *objPtr) |
| 16287 | { |
| 16288 | ScanFmtStringObj *fmtObj; |
| 16289 | char *buffer; |
| 16290 | int maxCount, i, approxSize, lastPos = -1; |
| 16291 | const char *fmt = Jim_String(objPtr); |
| 16292 | int maxFmtLen = Jim_Length(objPtr); |
| 16293 | const char *fmtEnd = fmt + maxFmtLen; |
| 16294 | int curr; |
| 16295 | |
| 16296 | Jim_FreeIntRep(interp, objPtr); |
| 16297 | |
| 16298 | for (i = 0, maxCount = 0; i < maxFmtLen; ++i) |
| 16299 | if (fmt[i] == '%') |
| 16300 | ++maxCount; |
| 16301 | |
| 16302 | approxSize = sizeof(ScanFmtStringObj) |
| 16303 | +(maxCount + 1) * sizeof(ScanFmtPartDescr) |
| 16304 | +maxFmtLen * sizeof(char) + 3 + 1 |
| 16305 | + maxFmtLen * sizeof(char) + 1 |
| 16306 | + maxFmtLen * sizeof(char) |
| 16307 | +(maxCount + 1) * sizeof(char) |
| 16308 | +1; |
| 16309 | fmtObj = (ScanFmtStringObj *) Jim_Alloc(approxSize); |
| 16310 | memset(fmtObj, 0, approxSize); |
| 16311 | fmtObj->size = approxSize; |
| 16312 | fmtObj->maxPos = 0; |
| 16313 | fmtObj->scratch = (char *)&fmtObj->descr[maxCount + 1]; |
| 16314 | fmtObj->stringRep = fmtObj->scratch + maxFmtLen + 3 + 1; |
| 16315 | memcpy(fmtObj->stringRep, fmt, maxFmtLen); |
| 16316 | buffer = fmtObj->stringRep + maxFmtLen + 1; |
| 16317 | objPtr->internalRep.ptr = fmtObj; |
| 16318 | objPtr->typePtr = &scanFmtStringObjType; |
| 16319 | for (i = 0, curr = 0; fmt < fmtEnd; ++fmt) { |
| 16320 | int width = 0, skip; |
| 16321 | ScanFmtPartDescr *descr = &fmtObj->descr[curr]; |
| 16322 | |
| 16323 | fmtObj->count++; |
| 16324 | descr->width = 0; |
| 16325 | |
| 16326 | if (*fmt != '%' || fmt[1] == '%') { |
| 16327 | descr->type = 0; |
| 16328 | descr->prefix = &buffer[i]; |
| 16329 | for (; fmt < fmtEnd; ++fmt) { |
| 16330 | if (*fmt == '%') { |
| 16331 | if (fmt[1] != '%') |
| 16332 | break; |
| 16333 | ++fmt; |
| 16334 | } |
| 16335 | buffer[i++] = *fmt; |
| 16336 | } |
| 16337 | buffer[i++] = 0; |
| 16338 | } |
| 16339 | |
| 16340 | ++fmt; |
| 16341 | |
| 16342 | if (fmt >= fmtEnd) |
| 16343 | goto done; |
| 16344 | descr->pos = 0; |
| 16345 | if (*fmt == '*') { |
| 16346 | descr->pos = -1; |
| 16347 | ++fmt; |
| 16348 | } |
| 16349 | else |
| 16350 | fmtObj->convCount++; |
| 16351 | |
| 16352 | if (sscanf(fmt, "%d%n", &width, &skip) == 1) { |
| 16353 | fmt += skip; |
| 16354 | |
| 16355 | if (descr->pos != -1 && *fmt == '$') { |
| 16356 | int prev; |
| 16357 | |
| 16358 | ++fmt; |
| 16359 | descr->pos = width; |
| 16360 | width = 0; |
| 16361 | |
| 16362 | if ((lastPos == 0 && descr->pos > 0) |
| 16363 | || (lastPos > 0 && descr->pos == 0)) { |
| 16364 | fmtObj->error = "cannot mix \"%\" and \"%n$\" conversion specifiers"; |
| 16365 | return JIM_ERR; |
| 16366 | } |
| 16367 | |
| 16368 | for (prev = 0; prev < curr; ++prev) { |
| 16369 | if (fmtObj->descr[prev].pos == -1) |
| 16370 | continue; |
| 16371 | if (fmtObj->descr[prev].pos == descr->pos) { |
| 16372 | fmtObj->error = |
| 16373 | "variable is assigned by multiple \"%n$\" conversion specifiers"; |
| 16374 | return JIM_ERR; |
| 16375 | } |
| 16376 | } |
| 16377 | if (descr->pos < 0) { |
| 16378 | fmtObj->error = |
| 16379 | "\"%n$\" conversion specifier is negative"; |
| 16380 | return JIM_ERR; |
| 16381 | } |
| 16382 | |
| 16383 | if (sscanf(fmt, "%d%n", &width, &skip) == 1) { |
| 16384 | descr->width = width; |
| 16385 | fmt += skip; |
| 16386 | } |
| 16387 | if (descr->pos > 0 && (size_t) descr->pos > fmtObj->maxPos) |
| 16388 | fmtObj->maxPos = descr->pos; |
| 16389 | } |
| 16390 | else { |
| 16391 | |
| 16392 | descr->width = width; |
| 16393 | } |
| 16394 | } |
| 16395 | |
| 16396 | if (lastPos == -1) |
| 16397 | lastPos = descr->pos; |
| 16398 | |
| 16399 | if (*fmt == '[') { |
| 16400 | int swapped = 1, beg = i, end, j; |
| 16401 | |
| 16402 | descr->type = '['; |
| 16403 | descr->arg = &buffer[i]; |
| 16404 | ++fmt; |
| 16405 | if (*fmt == '^') |
| 16406 | buffer[i++] = *fmt++; |
| 16407 | if (*fmt == ']') |
| 16408 | buffer[i++] = *fmt++; |
| 16409 | while (*fmt && *fmt != ']') |
| 16410 | buffer[i++] = *fmt++; |
| 16411 | if (*fmt != ']') { |
| 16412 | fmtObj->error = "unmatched [ in format string"; |
| 16413 | return JIM_ERR; |
| 16414 | } |
| 16415 | end = i; |
| 16416 | buffer[i++] = 0; |
| 16417 | |
| 16418 | while (swapped) { |
| 16419 | swapped = 0; |
| 16420 | for (j = beg + 1; j < end - 1; ++j) { |
| 16421 | if (buffer[j] == '-' && buffer[j - 1] > buffer[j + 1]) { |
| 16422 | char tmp = buffer[j - 1]; |
| 16423 | |
| 16424 | buffer[j - 1] = buffer[j + 1]; |
| 16425 | buffer[j + 1] = tmp; |
| 16426 | swapped = 1; |
| 16427 | } |
| 16428 | } |
| 16429 | } |
| 16430 | } |
| 16431 | else { |
| 16432 | |
| 16433 | if (fmt < fmtEnd && strchr("hlL", *fmt)) |
| 16434 | descr->modifier = tolower((int)*fmt++); |
| 16435 | |
| 16436 | if (fmt >= fmtEnd) { |
| 16437 | fmtObj->error = "missing scan conversion character"; |
| 16438 | return JIM_ERR; |
| 16439 | } |
| 16440 | |
| 16441 | descr->type = *fmt; |
| 16442 | if (strchr("efgcsndoxui", *fmt) == 0) { |
| 16443 | fmtObj->error = "bad scan conversion character"; |
| 16444 | return JIM_ERR; |
| 16445 | } |
| 16446 | else if (*fmt == 'c' && descr->width != 0) { |
| 16447 | fmtObj->error = "field width may not be specified in %c " "conversion"; |
| 16448 | return JIM_ERR; |
| 16449 | } |
| 16450 | else if (*fmt == 'u' && descr->modifier == 'l') { |
| 16451 | fmtObj->error = "unsigned wide not supported"; |
| 16452 | return JIM_ERR; |
| 16453 | } |
| 16454 | } |
| 16455 | curr++; |
| 16456 | } |
| 16457 | done: |
| 16458 | return JIM_OK; |
| 16459 | } |
| 16460 | |
| 16461 | |
| 16462 | |
| 16463 | #define FormatGetCnvCount(_fo_) \ |
| 16464 | ((ScanFmtStringObj*)((_fo_)->internalRep.ptr))->convCount |
| 16465 | #define FormatGetMaxPos(_fo_) \ |
| 16466 | ((ScanFmtStringObj*)((_fo_)->internalRep.ptr))->maxPos |
| 16467 | #define FormatGetError(_fo_) \ |
| 16468 | ((ScanFmtStringObj*)((_fo_)->internalRep.ptr))->error |
| 16469 | |
| 16470 | static Jim_Obj *JimScanAString(Jim_Interp *interp, const char *sdescr, const char *str) |
| 16471 | { |
| 16472 | char *buffer = Jim_StrDup(str); |
| 16473 | char *p = buffer; |
| 16474 | |
| 16475 | while (*str) { |
| 16476 | int c; |
| 16477 | int n; |
| 16478 | |
| 16479 | if (!sdescr && isspace(UCHAR(*str))) |
| 16480 | break; |
| 16481 | |
| 16482 | n = utf8_tounicode(str, &c); |
| 16483 | if (sdescr && !JimCharsetMatch(sdescr, strlen(sdescr), c, JIM_CHARSET_SCAN)) |
| 16484 | break; |
| 16485 | while (n--) |
| 16486 | *p++ = *str++; |
| 16487 | } |
| 16488 | *p = 0; |
| 16489 | return Jim_NewStringObjNoAlloc(interp, buffer, p - buffer); |
| 16490 | } |
| 16491 | |
| 16492 | |
| 16493 | static int ScanOneEntry(Jim_Interp *interp, const char *str, int pos, int str_bytelen, |
| 16494 | ScanFmtStringObj * fmtObj, long idx, Jim_Obj **valObjPtr) |
| 16495 | { |
| 16496 | const char *tok; |
| 16497 | const ScanFmtPartDescr *descr = &fmtObj->descr[idx]; |
| 16498 | size_t scanned = 0; |
| 16499 | size_t anchor = pos; |
| 16500 | int i; |
| 16501 | Jim_Obj *tmpObj = NULL; |
| 16502 | |
| 16503 | |
| 16504 | *valObjPtr = 0; |
| 16505 | if (descr->prefix) { |
| 16506 | for (i = 0; pos < str_bytelen && descr->prefix[i]; ++i) { |
| 16507 | |
| 16508 | if (isspace(UCHAR(descr->prefix[i]))) |
| 16509 | while (pos < str_bytelen && isspace(UCHAR(str[pos]))) |
| 16510 | ++pos; |
| 16511 | else if (descr->prefix[i] != str[pos]) |
| 16512 | break; |
| 16513 | else |
| 16514 | ++pos; |
| 16515 | } |
| 16516 | if (pos >= str_bytelen) { |
| 16517 | return -1; |
| 16518 | } |
| 16519 | else if (descr->prefix[i] != 0) |
| 16520 | return 0; |
| 16521 | } |
| 16522 | |
| 16523 | if (descr->type != 'c' && descr->type != '[' && descr->type != 'n') |
| 16524 | while (isspace(UCHAR(str[pos]))) |
| 16525 | ++pos; |
| 16526 | |
| 16527 | |
| 16528 | scanned = pos - anchor; |
| 16529 | |
| 16530 | |
| 16531 | if (descr->type == 'n') { |
| 16532 | |
| 16533 | *valObjPtr = Jim_NewIntObj(interp, anchor + scanned); |
| 16534 | } |
| 16535 | else if (pos >= str_bytelen) { |
| 16536 | |
| 16537 | return -1; |
| 16538 | } |
| 16539 | else if (descr->type == 'c') { |
| 16540 | int c; |
| 16541 | scanned += utf8_tounicode(&str[pos], &c); |
| 16542 | *valObjPtr = Jim_NewIntObj(interp, c); |
| 16543 | return scanned; |
| 16544 | } |
| 16545 | else { |
| 16546 | |
| 16547 | if (descr->width > 0) { |
| 16548 | size_t sLen = utf8_strlen(&str[pos], str_bytelen - pos); |
| 16549 | size_t tLen = descr->width > sLen ? sLen : descr->width; |
| 16550 | |
| 16551 | tmpObj = Jim_NewStringObjUtf8(interp, str + pos, tLen); |
| 16552 | tok = tmpObj->bytes; |
| 16553 | } |
| 16554 | else { |
| 16555 | |
| 16556 | tok = &str[pos]; |
| 16557 | } |
| 16558 | switch (descr->type) { |
| 16559 | case 'd': |
| 16560 | case 'o': |
| 16561 | case 'x': |
| 16562 | case 'u': |
| 16563 | case 'i':{ |
| 16564 | char *endp; |
| 16565 | jim_wide w; |
| 16566 | |
| 16567 | int base = descr->type == 'o' ? 8 |
| 16568 | : descr->type == 'x' ? 16 : descr->type == 'i' ? 0 : 10; |
| 16569 | |
| 16570 | |
| 16571 | if (base == 0) { |
| 16572 | w = jim_strtoull(tok, &endp); |
| 16573 | } |
| 16574 | else { |
| 16575 | w = strtoull(tok, &endp, base); |
| 16576 | } |
| 16577 | |
| 16578 | if (endp != tok) { |
| 16579 | |
| 16580 | *valObjPtr = Jim_NewIntObj(interp, w); |
| 16581 | |
| 16582 | |
| 16583 | scanned += endp - tok; |
| 16584 | } |
| 16585 | else { |
| 16586 | scanned = *tok ? 0 : -1; |
| 16587 | } |
| 16588 | break; |
| 16589 | } |
| 16590 | case 's': |
| 16591 | case '[':{ |
| 16592 | *valObjPtr = JimScanAString(interp, descr->arg, tok); |
| 16593 | scanned += Jim_Length(*valObjPtr); |
| 16594 | break; |
| 16595 | } |
| 16596 | case 'e': |
| 16597 | case 'f': |
| 16598 | case 'g':{ |
| 16599 | char *endp; |
| 16600 | double value = strtod(tok, &endp); |
| 16601 | |
| 16602 | if (endp != tok) { |
| 16603 | |
| 16604 | *valObjPtr = Jim_NewDoubleObj(interp, value); |
| 16605 | |
| 16606 | scanned += endp - tok; |
| 16607 | } |
| 16608 | else { |
| 16609 | scanned = *tok ? 0 : -1; |
| 16610 | } |
| 16611 | break; |
| 16612 | } |
| 16613 | } |
| 16614 | if (tmpObj) { |
| 16615 | Jim_FreeNewObj(interp, tmpObj); |
| 16616 | } |
| 16617 | } |
| 16618 | return scanned; |
| 16619 | } |
| 16620 | |
| 16621 | |
| 16622 | Jim_Obj *Jim_ScanString(Jim_Interp *interp, Jim_Obj *strObjPtr, Jim_Obj *fmtObjPtr, int flags) |
| 16623 | { |
| 16624 | size_t i, pos; |
| 16625 | int scanned = 1; |
| 16626 | const char *str = Jim_String(strObjPtr); |
| 16627 | int str_bytelen = Jim_Length(strObjPtr); |
| 16628 | Jim_Obj *resultList = 0; |
| 16629 | Jim_Obj **resultVec = 0; |
| 16630 | int resultc; |
| 16631 | Jim_Obj *emptyStr = 0; |
| 16632 | ScanFmtStringObj *fmtObj; |
| 16633 | |
| 16634 | |
| 16635 | JimPanic((fmtObjPtr->typePtr != &scanFmtStringObjType, "Jim_ScanString() for non-scan format")); |
| 16636 | |
| 16637 | fmtObj = (ScanFmtStringObj *) fmtObjPtr->internalRep.ptr; |
| 16638 | |
| 16639 | if (fmtObj->error != 0) { |
| 16640 | if (flags & JIM_ERRMSG) |
| 16641 | Jim_SetResultString(interp, fmtObj->error, -1); |
| 16642 | return 0; |
| 16643 | } |
| 16644 | |
| 16645 | emptyStr = Jim_NewEmptyStringObj(interp); |
| 16646 | Jim_IncrRefCount(emptyStr); |
| 16647 | |
| 16648 | resultList = Jim_NewListObj(interp, NULL, 0); |
| 16649 | if (fmtObj->maxPos > 0) { |
| 16650 | for (i = 0; i < fmtObj->maxPos; ++i) |
| 16651 | Jim_ListAppendElement(interp, resultList, emptyStr); |
| 16652 | JimListGetElements(interp, resultList, &resultc, &resultVec); |
| 16653 | } |
| 16654 | |
| 16655 | for (i = 0, pos = 0; i < fmtObj->count; ++i) { |
| 16656 | ScanFmtPartDescr *descr = &(fmtObj->descr[i]); |
| 16657 | Jim_Obj *value = 0; |
| 16658 | |
| 16659 | |
| 16660 | if (descr->type == 0) |
| 16661 | continue; |
| 16662 | |
| 16663 | if (scanned > 0) |
| 16664 | scanned = ScanOneEntry(interp, str, pos, str_bytelen, fmtObj, i, &value); |
| 16665 | |
| 16666 | if (scanned == -1 && i == 0) |
| 16667 | goto eof; |
| 16668 | |
| 16669 | pos += scanned; |
| 16670 | |
| 16671 | |
| 16672 | if (value == 0) |
| 16673 | value = Jim_NewEmptyStringObj(interp); |
| 16674 | |
| 16675 | if (descr->pos == -1) { |
| 16676 | Jim_FreeNewObj(interp, value); |
| 16677 | } |
| 16678 | else if (descr->pos == 0) |
| 16679 | |
| 16680 | Jim_ListAppendElement(interp, resultList, value); |
| 16681 | else if (resultVec[descr->pos - 1] == emptyStr) { |
| 16682 | |
| 16683 | Jim_DecrRefCount(interp, resultVec[descr->pos - 1]); |
| 16684 | Jim_IncrRefCount(value); |
| 16685 | resultVec[descr->pos - 1] = value; |
| 16686 | } |
| 16687 | else { |
| 16688 | |
| 16689 | Jim_FreeNewObj(interp, value); |
| 16690 | goto err; |
| 16691 | } |
| 16692 | } |
| 16693 | Jim_DecrRefCount(interp, emptyStr); |
| 16694 | return resultList; |
| 16695 | eof: |
| 16696 | Jim_DecrRefCount(interp, emptyStr); |
| 16697 | Jim_FreeNewObj(interp, resultList); |
| 16698 | return (Jim_Obj *)EOF; |
| 16699 | err: |
| 16700 | Jim_DecrRefCount(interp, emptyStr); |
| 16701 | Jim_FreeNewObj(interp, resultList); |
| 16702 | return 0; |
| 16703 | } |
| 16704 | |
| 16705 | |
| 16706 | static void JimPrngInit(Jim_Interp *interp) |
| 16707 | { |
| 16708 | #define PRNG_SEED_SIZE 256 |
| 16709 | int i; |
| 16710 | unsigned int *seed; |
| 16711 | time_t t = time(NULL); |
| 16712 | |
| 16713 | interp->prngState = Jim_Alloc(sizeof(Jim_PrngState)); |
| 16714 | |
| 16715 | seed = Jim_Alloc(PRNG_SEED_SIZE * sizeof(*seed)); |
| 16716 | for (i = 0; i < PRNG_SEED_SIZE; i++) { |
| 16717 | seed[i] = (rand() ^ t ^ clock()); |
| 16718 | } |
| 16719 | JimPrngSeed(interp, (unsigned char *)seed, PRNG_SEED_SIZE * sizeof(*seed)); |
| 16720 | Jim_Free(seed); |
| 16721 | } |
| 16722 | |
| 16723 | |
| 16724 | static void JimRandomBytes(Jim_Interp *interp, void *dest, unsigned int len) |
| 16725 | { |
| 16726 | Jim_PrngState *prng; |
| 16727 | unsigned char *destByte = (unsigned char *)dest; |
| 16728 | unsigned int si, sj, x; |
| 16729 | |
| 16730 | |
| 16731 | if (interp->prngState == NULL) |
| 16732 | JimPrngInit(interp); |
| 16733 | prng = interp->prngState; |
| 16734 | |
| 16735 | for (x = 0; x < len; x++) { |
| 16736 | prng->i = (prng->i + 1) & 0xff; |
| 16737 | si = prng->sbox[prng->i]; |
| 16738 | prng->j = (prng->j + si) & 0xff; |
| 16739 | sj = prng->sbox[prng->j]; |
| 16740 | prng->sbox[prng->i] = sj; |
| 16741 | prng->sbox[prng->j] = si; |
| 16742 | *destByte++ = prng->sbox[(si + sj) & 0xff]; |
| 16743 | } |
| 16744 | } |
| 16745 | |
| 16746 | |
| 16747 | static void JimPrngSeed(Jim_Interp *interp, unsigned char *seed, int seedLen) |
| 16748 | { |
| 16749 | int i; |
| 16750 | Jim_PrngState *prng; |
| 16751 | |
| 16752 | |
| 16753 | if (interp->prngState == NULL) |
| 16754 | JimPrngInit(interp); |
| 16755 | prng = interp->prngState; |
| 16756 | |
| 16757 | |
| 16758 | for (i = 0; i < 256; i++) |
| 16759 | prng->sbox[i] = i; |
| 16760 | |
| 16761 | for (i = 0; i < seedLen; i++) { |
| 16762 | unsigned char t; |
| 16763 | |
| 16764 | t = prng->sbox[i & 0xFF]; |
| 16765 | prng->sbox[i & 0xFF] = prng->sbox[seed[i]]; |
| 16766 | prng->sbox[seed[i]] = t; |
| 16767 | } |
| 16768 | prng->i = prng->j = 0; |
| 16769 | |
| 16770 | for (i = 0; i < 256; i += seedLen) { |
| 16771 | JimRandomBytes(interp, seed, seedLen); |
| 16772 | } |
| 16773 | } |
| 16774 | |
| 16775 | |
| 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); |
| 16786 | if (!intObjPtr) { |
| 16787 | |
| 16788 | wideValue = 0; |
| 16789 | } |
| 16790 | else if (Jim_GetWide(interp, intObjPtr, &wideValue) != JIM_OK) { |
| 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 | |
| 16801 | Jim_InvalidateStringRep(intObjPtr); |
| 16802 | JimWideValue(intObjPtr) = wideValue + increment; |
| 16803 | |
| 16804 | if (argv[1]->typePtr != &variableObjType) { |
| 16805 | |
| 16806 | Jim_SetVariable(interp, argv[1], intObjPtr); |
| 16807 | } |
| 16808 | } |
| 16809 | Jim_SetResult(interp, intObjPtr); |
| 16810 | return JIM_OK; |
| 16811 | } |
| 16812 | |
| 16813 | |
| 16814 | #define JIM_EVAL_SARGV_LEN 8 |
| 16815 | #define JIM_EVAL_SINTV_LEN 8 |
| 16816 | |
| 16817 | static int JimTraceCallback(Jim_Interp *interp, const char *type, int argc, Jim_Obj *const *argv) |
| 16818 | { |
| 16819 | JimPanic((interp->traceCmdObj == NULL, "xtrace invoked with no object")); |
| 16820 | |
| 16821 | int ret; |
| 16822 | Jim_Obj *nargv[7]; |
| 16823 | Jim_Obj *traceCmdObj = interp->traceCmdObj; |
| 16824 | Jim_Obj *resultObj = Jim_GetResult(interp); |
| 16825 | ScriptObj *script = NULL; |
| 16826 | |
| 16827 | |
| 16828 | |
| 16829 | if (interp->evalFrame->scriptObj) { |
| 16830 | script = JimGetScript(interp, interp->evalFrame->scriptObj); |
| 16831 | } |
| 16832 | |
| 16833 | nargv[0] = traceCmdObj; |
| 16834 | nargv[1] = Jim_NewStringObj(interp, type, -1); |
| 16835 | nargv[2] = script ? script->fileNameObj : interp->emptyObj; |
| 16836 | nargv[3] = Jim_NewIntObj(interp, script ? script->linenr : 1); |
| 16837 | nargv[4] = resultObj; |
| 16838 | nargv[5] = argv[0]; |
| 16839 | nargv[6] = Jim_NewListObj(interp, argv + 1, argc - 1); |
| 16840 | |
| 16841 | |
| 16842 | interp->traceCmdObj = NULL; |
| 16843 | |
| 16844 | Jim_IncrRefCount(resultObj); |
| 16845 | ret = Jim_EvalObjVector(interp, 7, nargv); |
| 16846 | Jim_DecrRefCount(interp, resultObj); |
| 16847 | |
| 16848 | if (ret == JIM_OK || ret == JIM_RETURN) { |
| 16849 | |
| 16850 | interp->traceCmdObj = traceCmdObj; |
| 16851 | Jim_SetEmptyResult(interp); |
| 16852 | ret = JIM_OK; |
| 16853 | } |
| 16854 | else { |
| 16855 | |
| 16856 | Jim_DecrRefCount(interp, traceCmdObj); |
| 16857 | } |
| 16858 | return ret; |
| 16859 | } |
| 16860 | |
| 16861 | |
| 16862 | static int JimUnknown(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 16863 | { |
| 16864 | int retcode; |
| 16865 | |
| 16866 | if (interp->unknown_called > 50) { |
| 16867 | return JIM_ERR; |
| 16868 | } |
| 16869 | |
| 16870 | |
| 16871 | |
| 16872 | if (Jim_GetCommand(interp, interp->unknown, JIM_NONE) == NULL) |
| 16873 | return JIM_ERR; |
| 16874 | |
| 16875 | interp->unknown_called++; |
| 16876 | |
| 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; |
| 16888 | frame->level = frame->parent->level + 1; |
| 16889 | frame->procLevel = interp->procLevel; |
| 16890 | frame->framePtr = interp->framePtr; |
| 16891 | if (scriptObj) { |
| 16892 | frame->scriptObj = scriptObj; |
| 16893 | } |
| 16894 | else { |
| 16895 | frame->scriptObj = frame->parent->scriptObj; |
| 16896 | } |
| 16897 | interp->evalFrame = frame; |
| 16898 | #if 0 |
| 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 | } |
| 16910 | |
| 16911 | |
| 16912 | static int JimInvokeCommand(Jim_Interp *interp, int objc, Jim_Obj *const *objv) |
| 16913 | { |
| 16914 | int retcode; |
| 16915 | Jim_Cmd *cmdPtr; |
| 16916 | void *prevPrivData; |
| 16917 | Jim_Obj *tailcallObj = NULL; |
| 16918 | |
| 16919 | #if 0 |
| 16920 | printf("invoke"); |
| 16921 | int j; |
| 16922 | for (j = 0; j < objc; j++) { |
| 16923 | printf(" '%s'", Jim_String(objv[j])); |
| 16924 | } |
| 16925 | printf("\n"); |
| 16926 | #endif |
| 16927 | |
| 16928 | cmdPtr = Jim_GetCommand(interp, objv[0], JIM_ERRMSG); |
| 16929 | if (cmdPtr == NULL) { |
| 16930 | return JimUnknown(interp, objc, objv); |
| 16931 | } |
| 16932 | JimIncrCmdRefCount(cmdPtr); |
| 16933 | |
| 16934 | if (interp->evalDepth == interp->maxEvalDepth) { |
| 16935 | Jim_SetResultString(interp, "Infinite eval recursion", -1); |
| 16936 | retcode = JIM_ERR; |
| 16937 | goto out; |
| 16938 | } |
| 16939 | interp->evalDepth++; |
| 16940 | prevPrivData = interp->cmdPrivData; |
| 16941 | |
| 16942 | tailcall: |
| 16943 | |
| 16944 | interp->evalFrame->argc = objc; |
| 16945 | interp->evalFrame->argv = objv; |
| 16946 | interp->evalFrame->cmd = cmdPtr; |
| 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 | } |
| 16968 | |
| 16969 | if (tailcallObj) { |
| 16970 | |
| 16971 | Jim_DecrRefCount(interp, tailcallObj); |
| 16972 | tailcallObj = NULL; |
| 16973 | } |
| 16974 | |
| 16975 | |
| 16976 | interp->evalFrame->argc = 0; |
| 16977 | interp->evalFrame->argv = NULL; |
| 16978 | |
| 16979 | |
| 16980 | if (retcode == JIM_EVAL && interp->framePtr->tailcallObj) { |
| 16981 | JimDecrCmdRefCount(interp, cmdPtr); |
| 16982 | |
| 16983 | |
| 16984 | cmdPtr = interp->framePtr->tailcallCmd; |
| 16985 | interp->framePtr->tailcallCmd = NULL; |
| 16986 | tailcallObj = interp->framePtr->tailcallObj; |
| 16987 | interp->framePtr->tailcallObj = NULL; |
| 16988 | objc = tailcallObj->internalRep.listValue.len; |
| 16989 | objv = tailcallObj->internalRep.listValue.ele; |
| 16990 | goto tailcall; |
| 16991 | } |
| 16992 | |
| 16993 | interp->cmdPrivData = prevPrivData; |
| 16994 | interp->evalDepth--; |
| 16995 | |
| 16996 | out: |
| 16997 | JimDecrCmdRefCount(interp, cmdPtr); |
| 16998 | |
| 16999 | if (retcode == JIM_ERR) { |
| 17000 | JimSetErrorStack(interp, NULL); |
| 17001 | } |
| 17002 | |
| 17003 | if (interp->framePtr->tailcallObj) { |
| 17004 | JimDecrCmdRefCount(interp, interp->framePtr->tailcallCmd); |
| 17005 | Jim_DecrRefCount(interp, interp->framePtr->tailcallObj); |
| 17006 | interp->framePtr->tailcallCmd = NULL; |
| 17007 | interp->framePtr->tailcallObj = NULL; |
| 17008 | } |
| 17009 | |
| 17010 | return retcode; |
| 17011 | } |
| 17012 | |
| 17013 | int Jim_EvalObjVector(Jim_Interp *interp, int objc, Jim_Obj *const *objv) |
| 17014 | { |
| 17015 | int i, retcode; |
| 17016 | Jim_EvalFrame frame; |
| 17017 | |
| 17018 | |
| 17019 | for (i = 0; i < objc; i++) |
| 17020 | Jim_IncrRefCount(objv[i]); |
| 17021 | |
| 17022 | |
| 17023 | JimPushEvalFrame(interp, &frame, NULL); |
| 17024 | |
| 17025 | retcode = JimInvokeCommand(interp, objc, objv); |
| 17026 | |
| 17027 | JimPopEvalFrame(interp); |
| 17028 | |
| 17029 | |
| 17030 | for (i = 0; i < objc; i++) |
| 17031 | Jim_DecrRefCount(interp, objv[i]); |
| 17032 | |
| 17033 | return retcode; |
| 17034 | } |
| 17035 | |
| 17036 | int Jim_EvalObjPrefix(Jim_Interp *interp, Jim_Obj *prefix, int objc, Jim_Obj *const *objv) |
| 17037 | { |
| 17038 | int ret; |
| 17039 | Jim_Obj **nargv = Jim_Alloc((objc + 1) * sizeof(*nargv)); |
| 17040 | |
| 17041 | nargv[0] = prefix; |
| 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; |
| 17053 | |
| 17054 | switch (token->type) { |
| 17055 | case JIM_TT_STR: |
| 17056 | case JIM_TT_ESC: |
| 17057 | objPtr = token->objPtr; |
| 17058 | break; |
| 17059 | case JIM_TT_VAR: |
| 17060 | objPtr = Jim_GetVariable(interp, token->objPtr, JIM_ERRMSG); |
| 17061 | break; |
| 17062 | case JIM_TT_DICTSUGAR: |
| 17063 | objPtr = JimExpandDictSugar(interp, token->objPtr); |
| 17064 | break; |
| 17065 | case JIM_TT_EXPRSUGAR: |
| 17066 | ret = Jim_EvalExpression(interp, token->objPtr); |
| 17067 | if (ret == JIM_OK) { |
| 17068 | objPtr = Jim_GetResult(interp); |
| 17069 | } |
| 17070 | else { |
| 17071 | objPtr = NULL; |
| 17072 | } |
| 17073 | break; |
| 17074 | case JIM_TT_CMD: |
| 17075 | ret = Jim_EvalObj(interp, token->objPtr); |
| 17076 | if (ret == JIM_OK || ret == JIM_RETURN) { |
| 17077 | objPtr = interp->result; |
| 17078 | } else { |
| 17079 | |
| 17080 | objPtr = NULL; |
| 17081 | } |
| 17082 | break; |
| 17083 | default: |
| 17084 | JimPanic((1, |
| 17085 | "default token type (%d) reached " "in Jim_SubstObj().", token->type)); |
| 17086 | objPtr = NULL; |
| 17087 | break; |
| 17088 | } |
| 17089 | if (objPtr) { |
| 17090 | *objPtrPtr = objPtr; |
| 17091 | return JIM_OK; |
| 17092 | } |
| 17093 | return ret; |
| 17094 | } |
| 17095 | |
| 17096 | static Jim_Obj *JimInterpolateTokens(Jim_Interp *interp, const ScriptToken * token, int tokens, int flags) |
| 17097 | { |
| 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); |
| 17110 | |
| 17111 | for (i = 0; i < tokens; i++) { |
| 17112 | switch (JimSubstOneToken(interp, &token[i], &intv[i])) { |
| 17113 | case JIM_OK: |
| 17114 | case JIM_RETURN: |
| 17115 | break; |
| 17116 | case JIM_BREAK: |
| 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 | |
| 17151 | |
| 17152 | if (tokens == 1 && intv[0] && intv == sintv) { |
| 17153 | |
| 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; |
| 17165 | objPtr->internalRep.dictSubstValue.varNameObjPtr = token[0].objPtr; |
| 17166 | objPtr->internalRep.dictSubstValue.indexObjPtr = intv[2]; |
| 17167 | Jim_IncrRefCount(intv[2]); |
| 17168 | } |
| 17169 | else if (tokens && intv[0] && intv[0]->typePtr == &sourceObjType) { |
| 17170 | |
| 17171 | int line; |
| 17172 | Jim_Obj *fileNameObj = Jim_GetSourceInfo(interp, intv[0], &line); |
| 17173 | Jim_SetSourceInfo(interp, objPtr, fileNameObj, line); |
| 17174 | } |
| 17175 | |
| 17176 | |
| 17177 | s = objPtr->bytes = Jim_Alloc(totlen + 1); |
| 17178 | objPtr->length = totlen; |
| 17179 | for (i = 0; i < tokens; i++) { |
| 17180 | if (intv[i]) { |
| 17181 | memcpy(s, intv[i]->bytes, intv[i]->length); |
| 17182 | s += intv[i]->length; |
| 17183 | Jim_DecrRefCount(interp, intv[i]); |
| 17184 | } |
| 17185 | } |
| 17186 | objPtr->bytes[totlen] = '\0'; |
| 17187 | |
| 17188 | if (intv != sintv) { |
| 17189 | Jim_Free(intv); |
| 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; |
| 17301 | |
| 17302 | JimPanic((Jim_IsList(listPtr) == 0, "JimEvalObjList() invoked on non-list.")); |
| 17303 | |
| 17304 | JimPushEvalFrame(interp, &frame, NULL); |
| 17305 | |
| 17306 | if (listPtr->internalRep.listValue.len) { |
| 17307 | Jim_IncrRefCount(listPtr); |
| 17308 | retcode = JimInvokeCommand(interp, |
| 17309 | listPtr->internalRep.listValue.len, |
| 17310 | listPtr->internalRep.listValue.ele); |
| 17311 | Jim_DecrRefCount(interp, listPtr); |
| 17312 | } |
| 17313 | |
| 17314 | JimPopEvalFrame(interp); |
| 17315 | |
| 17316 | return retcode; |
| 17317 | } |
| 17318 | |
| 17319 | int Jim_EvalObjList(Jim_Interp *interp, Jim_Obj *listPtr) |
| 17320 | { |
| 17321 | SetListFromAny(interp, listPtr); |
| 17322 | return JimEvalObjList(interp, listPtr); |
| 17323 | } |
| 17324 | |
| 17325 | int Jim_EvalObj(Jim_Interp *interp, Jim_Obj *scriptObjPtr) |
| 17326 | { |
| 17327 | int i; |
| 17328 | ScriptObj *script; |
| 17329 | ScriptToken *token; |
| 17330 | int retcode = JIM_OK; |
| 17331 | Jim_Obj *sargv[JIM_EVAL_SARGV_LEN], **argv = NULL; |
| 17332 | Jim_EvalFrame frame; |
| 17333 | |
| 17334 | if (Jim_IsList(scriptObjPtr) && scriptObjPtr->bytes == NULL) { |
| 17335 | return JimEvalObjList(interp, scriptObjPtr); |
| 17336 | } |
| 17337 | |
| 17338 | Jim_IncrRefCount(scriptObjPtr); |
| 17339 | script = JimGetScript(interp, scriptObjPtr); |
| 17340 | if (JimParseCheckMissing(interp, script->missing) == JIM_ERR) { |
| 17341 | JimSetErrorStack(interp, script); |
| 17342 | Jim_DecrRefCount(interp, scriptObjPtr); |
| 17343 | return JIM_ERR; |
| 17344 | } |
| 17345 | |
| 17346 | Jim_SetEmptyResult(interp); |
| 17347 | |
| 17348 | token = script->token; |
| 17349 | |
| 17350 | #ifdef JIM_OPTIMIZATION |
| 17351 | if (script->len == 0) { |
| 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 | |
| 17363 | if (objPtr && !Jim_IsShared(objPtr) && objPtr->typePtr == &intObjType) { |
| 17364 | JimWideValue(objPtr)++; |
| 17365 | Jim_InvalidateStringRep(objPtr); |
| 17366 | Jim_DecrRefCount(interp, scriptObjPtr); |
| 17367 | Jim_SetResult(interp, objPtr); |
| 17368 | return JIM_OK; |
| 17369 | } |
| 17370 | } |
| 17371 | #endif |
| 17372 | |
| 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; |
| 17384 | |
| 17385 | |
| 17386 | argc = token[i].objPtr->internalRep.scriptLineValue.argc; |
| 17387 | script->linenr = token[i].objPtr->internalRep.scriptLineValue.line; |
| 17388 | |
| 17389 | |
| 17390 | if (argc > JIM_EVAL_SARGV_LEN) |
| 17391 | argv = Jim_Alloc(sizeof(Jim_Obj *) * argc); |
| 17392 | |
| 17393 | |
| 17394 | i++; |
| 17395 | |
| 17396 | for (j = 0; j < argc; j++) { |
| 17397 | long wordtokens = 1; |
| 17398 | int expand = 0; |
| 17399 | Jim_Obj *wordObjPtr = NULL; |
| 17400 | |
| 17401 | if (token[i].type == JIM_TT_WORD) { |
| 17402 | wordtokens = JimWideValue(token[i++].objPtr); |
| 17403 | if (wordtokens < 0) { |
| 17404 | expand = 1; |
| 17405 | wordtokens = -wordtokens; |
| 17406 | } |
| 17407 | } |
| 17408 | |
| 17409 | if (wordtokens == 1) { |
| 17410 | |
| 17411 | switch (token[i].type) { |
| 17412 | case JIM_TT_ESC: |
| 17413 | case JIM_TT_STR: |
| 17414 | wordObjPtr = token[i].objPtr; |
| 17415 | break; |
| 17416 | case JIM_TT_VAR: |
| 17417 | wordObjPtr = Jim_GetVariable(interp, token[i].objPtr, JIM_ERRMSG); |
| 17418 | break; |
| 17419 | case JIM_TT_EXPRSUGAR: |
| 17420 | retcode = Jim_EvalExpression(interp, token[i].objPtr); |
| 17421 | if (retcode == JIM_OK) { |
| 17422 | wordObjPtr = Jim_GetResult(interp); |
| 17423 | } |
| 17424 | else { |
| 17425 | wordObjPtr = NULL; |
| 17426 | } |
| 17427 | break; |
| 17428 | case JIM_TT_DICTSUGAR: |
| 17429 | wordObjPtr = JimExpandDictSugar(interp, token[i].objPtr); |
| 17430 | break; |
| 17431 | case JIM_TT_CMD: |
| 17432 | retcode = Jim_EvalObj(interp, token[i].objPtr); |
| 17433 | if (retcode == JIM_OK) { |
| 17434 | wordObjPtr = Jim_GetResult(interp); |
| 17435 | } |
| 17436 | break; |
| 17437 | default: |
| 17438 | JimPanic((1, "default token type reached " "in Jim_EvalObj().")); |
| 17439 | } |
| 17440 | } |
| 17441 | else { |
| 17442 | wordObjPtr = JimInterpolateTokens(interp, token + i, wordtokens, JIM_NONE); |
| 17443 | } |
| 17444 | |
| 17445 | if (!wordObjPtr) { |
| 17446 | if (retcode == JIM_OK) { |
| 17447 | retcode = JIM_ERR; |
| 17448 | } |
| 17449 | break; |
| 17450 | } |
| 17451 | |
| 17452 | Jim_IncrRefCount(wordObjPtr); |
| 17453 | i += wordtokens; |
| 17454 | |
| 17455 | if (!expand) { |
| 17456 | argv[j] = wordObjPtr; |
| 17457 | } |
| 17458 | else { |
| 17459 | |
| 17460 | int len = Jim_ListLength(interp, wordObjPtr); |
| 17461 | int newargc = argc + len - 1; |
| 17462 | int k; |
| 17463 | |
| 17464 | if (len > 1) { |
| 17465 | if (argv == sargv) { |
| 17466 | if (newargc > JIM_EVAL_SARGV_LEN) { |
| 17467 | argv = Jim_Alloc(sizeof(*argv) * newargc); |
| 17468 | memcpy(argv, sargv, sizeof(*argv) * j); |
| 17469 | } |
| 17470 | } |
| 17471 | else { |
| 17472 | |
| 17473 | argv = Jim_Realloc(argv, sizeof(*argv) * newargc); |
| 17474 | } |
| 17475 | } |
| 17476 | |
| 17477 | |
| 17478 | for (k = 0; k < len; k++) { |
| 17479 | argv[j++] = wordObjPtr->internalRep.listValue.ele[k]; |
| 17480 | Jim_IncrRefCount(wordObjPtr->internalRep.listValue.ele[k]); |
| 17481 | } |
| 17482 | |
| 17483 | Jim_DecrRefCount(interp, wordObjPtr); |
| 17484 | |
| 17485 | |
| 17486 | j--; |
| 17487 | argc += len - 1; |
| 17488 | } |
| 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 | } |
| 17500 | |
| 17501 | |
| 17502 | while (j-- > 0) { |
| 17503 | Jim_DecrRefCount(interp, argv[j]); |
| 17504 | } |
| 17505 | |
| 17506 | if (argv != sargv) { |
| 17507 | Jim_Free(argv); |
| 17508 | argv = sargv; |
| 17509 | } |
| 17510 | } |
| 17511 | |
| 17512 | |
| 17513 | if (retcode == JIM_ERR) { |
| 17514 | JimSetErrorStack(interp, NULL); |
| 17515 | } |
| 17516 | |
| 17517 | JimPopEvalFrame(interp); |
| 17518 | |
| 17519 | Jim_FreeIntRep(interp, scriptObjPtr); |
| 17520 | scriptObjPtr->typePtr = &scriptObjType; |
| 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 | |
| 17532 | const char *varname = Jim_String(argNameObj); |
| 17533 | if (*varname == '&') { |
| 17534 | |
| 17535 | Jim_Obj *objPtr; |
| 17536 | Jim_CallFrame *savedCallFrame = interp->framePtr; |
| 17537 | |
| 17538 | interp->framePtr = interp->framePtr->parent; |
| 17539 | objPtr = Jim_GetVariable(interp, argValObj, JIM_ERRMSG); |
| 17540 | interp->framePtr = savedCallFrame; |
| 17541 | if (!objPtr) { |
| 17542 | return JIM_ERR; |
| 17543 | } |
| 17544 | |
| 17545 | |
| 17546 | objPtr = Jim_NewStringObj(interp, varname + 1, -1); |
| 17547 | Jim_IncrRefCount(objPtr); |
| 17548 | retcode = Jim_SetVariableLink(interp, objPtr, argValObj, interp->framePtr->parent); |
| 17549 | Jim_DecrRefCount(interp, objPtr); |
| 17550 | } |
| 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 | { |
| 17614 | Jim_CallFrame *callFramePtr; |
| 17615 | int retcode; |
| 17616 | |
| 17617 | |
| 17618 | callFramePtr = JimCreateCallFrame(interp, interp->framePtr, nsObj); |
| 17619 | callFramePtr->argv = interp->evalFrame->argv; |
| 17620 | callFramePtr->argc = interp->evalFrame->argc; |
| 17621 | callFramePtr->procArgsObjPtr = NULL; |
| 17622 | callFramePtr->procBodyObjPtr = scriptObj; |
| 17623 | callFramePtr->staticVars = NULL; |
| 17624 | Jim_IncrRefCount(scriptObj); |
| 17625 | interp->framePtr = callFramePtr; |
| 17626 | |
| 17627 | |
| 17628 | if (interp->framePtr->level == interp->maxCallFrameDepth) { |
| 17629 | Jim_SetResultString(interp, "Too many nested calls. Infinite recursion?", -1); |
| 17630 | retcode = JIM_ERR; |
| 17631 | } |
| 17632 | else { |
| 17633 | |
| 17634 | retcode = Jim_EvalObj(interp, scriptObj); |
| 17635 | } |
| 17636 | |
| 17637 | |
| 17638 | interp->framePtr = interp->framePtr->parent; |
| 17639 | JimFreeCallFrame(interp, callFramePtr, JIM_FCF_REUSE); |
| 17640 | |
| 17641 | return retcode; |
| 17642 | } |
| 17643 | #endif |
| 17644 | |
| 17645 | static int JimCallProcedure(Jim_Interp *interp, Jim_Cmd *cmd, int argc, Jim_Obj *const *argv) |
| 17646 | { |
| 17647 | Jim_CallFrame *callFramePtr; |
| 17648 | int i, d, retcode, optargs; |
| 17649 | |
| 17650 | |
| 17651 | if (argc - 1 < cmd->u.proc.reqArity || |
| 17652 | (cmd->u.proc.argsPos < 0 && argc - 1 > cmd->u.proc.reqArity + cmd->u.proc.optArity)) { |
| 17653 | JimSetProcWrongArgs(interp, argv[0], cmd); |
| 17654 | return JIM_ERR; |
| 17655 | } |
| 17656 | |
| 17657 | if (Jim_Length(cmd->u.proc.bodyObjPtr) == 0) { |
| 17658 | |
| 17659 | return JIM_OK; |
| 17660 | } |
| 17661 | |
| 17662 | |
| 17663 | if (interp->framePtr->level == interp->maxCallFrameDepth) { |
| 17664 | Jim_SetResultString(interp, "Too many nested calls. Infinite recursion?", -1); |
| 17665 | return JIM_ERR; |
| 17666 | } |
| 17667 | |
| 17668 | |
| 17669 | callFramePtr = JimCreateCallFrame(interp, interp->framePtr, cmd->u.proc.nsObj); |
| 17670 | callFramePtr->argv = argv; |
| 17671 | callFramePtr->argc = argc; |
| 17672 | callFramePtr->procArgsObjPtr = cmd->u.proc.argListObjPtr; |
| 17673 | callFramePtr->procBodyObjPtr = cmd->u.proc.bodyObjPtr; |
| 17674 | callFramePtr->staticVars = cmd->u.proc.staticVars; |
| 17675 | |
| 17676 | interp->procLevel++; |
| 17677 | |
| 17678 | Jim_IncrRefCount(cmd->u.proc.argListObjPtr); |
| 17679 | Jim_IncrRefCount(cmd->u.proc.bodyObjPtr); |
| 17680 | interp->framePtr = callFramePtr; |
| 17681 | |
| 17682 | |
| 17683 | optargs = (argc - 1 - cmd->u.proc.reqArity); |
| 17684 | |
| 17685 | |
| 17686 | i = 1; |
| 17687 | for (d = 0; d < cmd->u.proc.argListLen; d++) { |
| 17688 | Jim_Obj *nameObjPtr = cmd->u.proc.arglist[d].nameObjPtr; |
| 17689 | if (d == cmd->u.proc.argsPos) { |
| 17690 | |
| 17691 | Jim_Obj *listObjPtr; |
| 17692 | int argsLen = 0; |
| 17693 | if (cmd->u.proc.reqArity + cmd->u.proc.optArity < argc - 1) { |
| 17694 | argsLen = argc - 1 - (cmd->u.proc.reqArity + cmd->u.proc.optArity); |
| 17695 | } |
| 17696 | listObjPtr = Jim_NewListObj(interp, &argv[i], argsLen); |
| 17697 | |
| 17698 | |
| 17699 | if (cmd->u.proc.arglist[d].defaultObjPtr) { |
| 17700 | nameObjPtr =cmd->u.proc.arglist[d].defaultObjPtr; |
| 17701 | } |
| 17702 | retcode = Jim_SetVariable(interp, nameObjPtr, listObjPtr); |
| 17703 | if (retcode != JIM_OK) { |
| 17704 | goto badargset; |
| 17705 | } |
| 17706 | |
| 17707 | i += argsLen; |
| 17708 | continue; |
| 17709 | } |
| 17710 | |
| 17711 | |
| 17712 | if (cmd->u.proc.arglist[d].defaultObjPtr == NULL || optargs-- > 0) { |
| 17713 | retcode = JimSetProcArg(interp, nameObjPtr, argv[i++]); |
| 17714 | } |
| 17715 | else { |
| 17716 | |
| 17717 | retcode = Jim_SetVariable(interp, nameObjPtr, cmd->u.proc.arglist[d].defaultObjPtr); |
| 17718 | } |
| 17719 | if (retcode != JIM_OK) { |
| 17720 | goto badargset; |
| 17721 | } |
| 17722 | } |
| 17723 | |
| 17724 | if (interp->traceCmdObj == NULL || |
| 17725 | (retcode = JimTraceCallback(interp, "proc", argc, argv)) == JIM_OK) { |
| 17726 | |
| 17727 | retcode = Jim_EvalObj(interp, cmd->u.proc.bodyObjPtr); |
| 17728 | } |
| 17729 | |
| 17730 | badargset: |
| 17731 | |
| 17732 | |
| 17733 | retcode = JimInvokeDefer(interp, retcode); |
| 17734 | interp->framePtr = interp->framePtr->parent; |
| 17735 | JimFreeCallFrame(interp, callFramePtr, JIM_FCF_REUSE); |
| 17736 | |
| 17737 | |
| 17738 | if (retcode == JIM_RETURN) { |
| 17739 | if (--interp->returnLevel <= 0) { |
| 17740 | retcode = interp->returnCode; |
| 17741 | interp->returnCode = JIM_OK; |
| 17742 | interp->returnLevel = 0; |
| 17743 | } |
| 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; |
| 17784 | |
| 17785 | scriptObjPtr = Jim_NewStringObj(interp, script, -1); |
| 17786 | Jim_IncrRefCount(scriptObjPtr); |
| 17787 | if (filename) { |
| 17788 | Jim_SetSourceInfo(interp, scriptObjPtr, Jim_NewStringObj(interp, filename, -1), lineno); |
| 17789 | } |
| 17790 | retval = Jim_EvalObj(interp, scriptObjPtr); |
| 17791 | Jim_DecrRefCount(interp, scriptObjPtr); |
| 17792 | return retval; |
| 17793 | } |
| 17794 | |
| 17795 | int Jim_Eval(Jim_Interp *interp, const char *script) |
| 17796 | { |
| 17797 | return Jim_EvalObj(interp, Jim_NewStringObj(interp, script, -1)); |
| 17798 | } |
| 17799 | |
| 17800 | |
| 17801 | int Jim_EvalGlobal(Jim_Interp *interp, const char *script) |
| 17802 | { |
| 17803 | int retval; |
| 17804 | Jim_CallFrame *savedFramePtr = interp->framePtr; |
| 17805 | |
| 17806 | interp->framePtr = interp->topFramePtr; |
| 17807 | retval = Jim_Eval(interp, script); |
| 17808 | interp->framePtr = savedFramePtr; |
| 17809 | |
| 17810 | return retval; |
| 17811 | } |
| 17812 | |
| 17813 | int Jim_EvalFileGlobal(Jim_Interp *interp, const char *filename) |
| 17814 | { |
| 17815 | int retval; |
| 17816 | Jim_CallFrame *savedFramePtr = interp->framePtr; |
| 17817 | |
| 17818 | interp->framePtr = interp->topFramePtr; |
| 17819 | retval = Jim_EvalFile(interp, filename); |
| 17820 | interp->framePtr = savedFramePtr; |
| 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); |
| 17864 | |
| 17865 | |
| 17866 | if (retcode == JIM_RETURN) { |
| 17867 | if (--interp->returnLevel <= 0) { |
| 17868 | retcode = interp->returnCode; |
| 17869 | interp->returnCode = JIM_OK; |
| 17870 | interp->returnLevel = 0; |
| 17871 | } |
| 17872 | } |
| 17873 | |
| 17874 | return retcode; |
| 17875 | } |
| 17876 | |
| 17877 | static void JimParseSubst(struct JimParserCtx *pc, int flags) |
| 17878 | { |
| 17879 | pc->tstart = pc->p; |
| 17880 | pc->tline = pc->linenr; |
| 17881 | |
| 17882 | if (pc->len == 0) { |
| 17883 | pc->tend = pc->p; |
| 17884 | pc->tt = JIM_TT_EOL; |
| 17885 | pc->eof = 1; |
| 17886 | return; |
| 17887 | } |
| 17888 | if (*pc->p == '[' && !(flags & JIM_SUBST_NOCMD)) { |
| 17889 | JimParseCmd(pc); |
| 17890 | return; |
| 17891 | } |
| 17892 | if (*pc->p == '$' && !(flags & JIM_SUBST_NOVAR)) { |
| 17893 | if (JimParseVar(pc) == JIM_OK) { |
| 17894 | return; |
| 17895 | } |
| 17896 | |
| 17897 | pc->tstart = pc->p; |
| 17898 | |
| 17899 | pc->p++; |
| 17900 | pc->len--; |
| 17901 | } |
| 17902 | while (pc->len) { |
| 17903 | if (*pc->p == '$' && !(flags & JIM_SUBST_NOVAR)) { |
| 17904 | break; |
| 17905 | } |
| 17906 | if (*pc->p == '[' && !(flags & JIM_SUBST_NOCMD)) { |
| 17907 | break; |
| 17908 | } |
| 17909 | if (*pc->p == '\\' && pc->len > 1) { |
| 17910 | pc->p++; |
| 17911 | pc->len--; |
| 17912 | } |
| 17913 | pc->p++; |
| 17914 | pc->len--; |
| 17915 | } |
| 17916 | pc->tend = pc->p - 1; |
| 17917 | pc->tt = (flags & JIM_SUBST_NOESC) ? JIM_TT_STR : JIM_TT_ESC; |
| 17918 | } |
| 17919 | |
| 17920 | |
| 17921 | static int SetSubstFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr, int flags) |
| 17922 | { |
| 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); |
| 17934 | while (1) { |
| 17935 | JimParseSubst(&parser, flags); |
| 17936 | if (parser.eof) { |
| 17937 | |
| 17938 | break; |
| 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); |
| 17952 | SubstObjAddTokens(interp, script, &tokenlist); |
| 17953 | |
| 17954 | |
| 17955 | ScriptTokenListFree(&tokenlist); |
| 17956 | |
| 17957 | #ifdef DEBUG_SHOW_SUBST |
| 17958 | { |
| 17959 | int i; |
| 17960 | |
| 17961 | printf("==== Subst ====\n"); |
| 17962 | for (i = 0; i < script->len; i++) { |
| 17963 | printf("[%2d] %s '%s'\n", i, jim_tt_name(script->token[i].type), |
| 17964 | Jim_String(script->token[i].objPtr)); |
| 17965 | } |
| 17966 | } |
| 17967 | #endif |
| 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 | { |
| 17979 | if (objPtr->typePtr != &scriptObjType || ((ScriptObj *)Jim_GetIntRepPtr(objPtr))->substFlags != flags) |
| 17980 | SetSubstFromAny(interp, objPtr, flags); |
| 17981 | return (ScriptObj *) Jim_GetIntRepPtr(objPtr); |
| 17982 | } |
| 17983 | |
| 17984 | int Jim_SubstObj(Jim_Interp *interp, Jim_Obj *substObjPtr, Jim_Obj **resObjPtrPtr, int flags) |
| 17985 | { |
| 17986 | ScriptObj *script; |
| 17987 | |
| 17988 | JimPanic((substObjPtr->refCount == 0, "Jim_SubstObj() called with zero refcount object")); |
| 17989 | |
| 17990 | script = Jim_GetSubst(interp, substObjPtr, flags); |
| 17991 | |
| 17992 | Jim_IncrRefCount(substObjPtr); |
| 17993 | script->inUse++; |
| 17994 | |
| 17995 | *resObjPtrPtr = JimInterpolateTokens(interp, script->token, script->len, flags); |
| 17996 | |
| 17997 | script->inUse--; |
| 17998 | Jim_DecrRefCount(interp, substObjPtr); |
| 17999 | if (*resObjPtrPtr == NULL) { |
| 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 | |
| 18059 | #define JimTrivialMatch(pattern) (strpbrk((pattern), "*[?\\") == NULL) |
| 18060 | |
| 18061 | static Jim_Obj *JimHashtablePatternMatch(Jim_Interp *interp, Jim_HashTable *ht, Jim_Obj *patternObjPtr, |
| 18062 | JimHashtableIteratorCallbackType *callback, int type) |
| 18063 | { |
| 18064 | Jim_HashEntry *he; |
| 18065 | Jim_Obj *listObjPtr = Jim_NewListObj(interp, NULL, 0); |
| 18066 | |
| 18067 | |
| 18068 | if (patternObjPtr && JimTrivialMatch(Jim_String(patternObjPtr))) { |
| 18069 | he = Jim_FindHashEntry(ht, patternObjPtr); |
| 18070 | if (he) { |
| 18071 | callback(interp, listObjPtr, Jim_GetHashEntryKey(he), Jim_GetHashEntryVal(he), |
| 18072 | patternObjPtr, type); |
| 18073 | } |
| 18074 | } |
| 18075 | else { |
| 18076 | Jim_HashTableIterator htiter; |
| 18077 | JimInitHashTableIterator(ht, &htiter); |
| 18078 | while ((he = Jim_NextHashEntry(&htiter)) != NULL) { |
| 18079 | callback(interp, listObjPtr, Jim_GetHashEntryKey(he), Jim_GetHashEntryVal(he), |
| 18080 | patternObjPtr, type); |
| 18081 | } |
| 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) |
| 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 | } |
| 18159 | } |
| 18160 | } |
| 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 | |
| 18181 | if (Jim_GetLong(interp, levelObjPtr, &level) == JIM_OK) { |
| 18182 | Jim_CallFrame *targetCallFrame = JimGetCallFrameByInteger(interp, level); |
| 18183 | if (targetCallFrame && targetCallFrame != interp->topFramePtr) { |
| 18184 | #ifdef JIM_NO_INTROSPECTION |
| 18185 | |
| 18186 | *objPtrPtr = Jim_NewListObj(interp, targetCallFrame->argv, 1); |
| 18187 | #else |
| 18188 | *objPtrPtr = Jim_NewListObj(interp, targetCallFrame->argv, targetCallFrame->argc); |
| 18189 | #endif |
| 18190 | return JIM_OK; |
| 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 | |
| 18202 | if (Jim_GetLong(interp, levelObjPtr, &level) == JIM_OK) { |
| 18203 | Jim_EvalFrame *frame = JimGetEvalFrameByProcLevel(interp, level); |
| 18204 | if (frame) { |
| 18205 | Jim_Obj *listObj = Jim_NewListObj(interp, NULL, 0); |
| 18206 | |
| 18207 | Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, "type", -1)); |
| 18208 | Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, "source", -1)); |
| 18209 | if (frame->scriptObj) { |
| 18210 | ScriptObj *script = JimGetScript(interp, frame->scriptObj); |
| 18211 | Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, "line", -1)); |
| 18212 | Jim_ListAppendElement(interp, listObj, Jim_NewIntObj(interp, script->linenr)); |
| 18213 | Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, "file", -1)); |
| 18214 | Jim_ListAppendElement(interp, listObj, script->fileNameObj); |
| 18215 | } |
| 18216 | #ifndef JIM_NO_INTROSPECTION |
| 18217 | { |
| 18218 | Jim_Obj *cmdObj = Jim_NewListObj(interp, frame->argv, frame->argc); |
| 18219 | |
| 18220 | Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, "cmd", -1)); |
| 18221 | Jim_ListAppendElement(interp, listObj, cmdObj); |
| 18222 | } |
| 18223 | #endif |
| 18224 | { |
| 18225 | Jim_Obj *procNameObj = JimProcForEvalFrame(interp, frame); |
| 18226 | if (procNameObj) { |
| 18227 | Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, "proc", -1)); |
| 18228 | Jim_ListAppendElement(interp, listObj, procNameObj); |
| 18229 | } |
| 18230 | } |
| 18231 | Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, "level", -1)); |
| 18232 | Jim_ListAppendElement(interp, listObj, Jim_NewIntObj(interp, interp->framePtr->level - frame->framePtr->level)); |
| 18233 | |
| 18234 | *objPtrPtr = listObj; |
| 18235 | return JIM_OK; |
| 18236 | } |
| 18237 | } |
| 18238 | Jim_SetResultFormatted(interp, "bad level \"%#s\"", levelObjPtr); |
| 18239 | return JIM_ERR; |
| 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 | } |
| 18250 | else { |
| 18251 | fputs(Jim_String(argv[2]), stdout); |
| 18252 | } |
| 18253 | } |
| 18254 | else { |
| 18255 | puts(Jim_String(argv[1])); |
| 18256 | } |
| 18257 | return JIM_OK; |
| 18258 | } |
| 18259 | |
| 18260 | |
| 18261 | static int JimAddMulHelper(Jim_Interp *interp, int argc, Jim_Obj *const *argv, int op) |
| 18262 | { |
| 18263 | jim_wide wideValue, res; |
| 18264 | double doubleValue, doubleRes; |
| 18265 | int i; |
| 18266 | |
| 18267 | res = (op == JIM_EXPROP_ADD) ? 0 : 1; |
| 18268 | |
| 18269 | for (i = 1; i < argc; i++) { |
| 18270 | if (Jim_GetWide(interp, argv[i], &wideValue) != JIM_OK) |
| 18271 | goto trydouble; |
| 18272 | if (op == JIM_EXPROP_ADD) |
| 18273 | res += wideValue; |
| 18274 | else |
| 18275 | res *= wideValue; |
| 18276 | } |
| 18277 | Jim_SetResultInt(interp, res); |
| 18278 | return JIM_OK; |
| 18279 | trydouble: |
| 18280 | doubleRes = (double)res; |
| 18281 | for (; i < argc; i++) { |
| 18282 | if (Jim_GetDouble(interp, argv[i], &doubleValue) != JIM_OK) |
| 18283 | return JIM_ERR; |
| 18284 | if (op == JIM_EXPROP_ADD) |
| 18285 | doubleRes += doubleValue; |
| 18286 | else |
| 18287 | doubleRes *= doubleValue; |
| 18288 | } |
| 18289 | Jim_SetResult(interp, Jim_NewDoubleObj(interp, doubleRes)); |
| 18290 | return JIM_OK; |
| 18291 | } |
| 18292 | |
| 18293 | |
| 18294 | static int JimSubDivHelper(Jim_Interp *interp, int argc, Jim_Obj *const *argv, int op) |
| 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 { |
| 18306 | if (op == JIM_EXPROP_SUB) |
| 18307 | doubleRes = -doubleValue; |
| 18308 | else |
| 18309 | doubleRes = 1.0 / doubleValue; |
| 18310 | Jim_SetResult(interp, Jim_NewDoubleObj(interp, doubleRes)); |
| 18311 | return JIM_OK; |
| 18312 | } |
| 18313 | } |
| 18314 | if (op == JIM_EXPROP_SUB) { |
| 18315 | res = -wideValue; |
| 18316 | Jim_SetResultInt(interp, res); |
| 18317 | } |
| 18318 | else { |
| 18319 | doubleRes = 1.0 / wideValue; |
| 18320 | Jim_SetResult(interp, Jim_NewDoubleObj(interp, doubleRes)); |
| 18321 | } |
| 18322 | return JIM_OK; |
| 18323 | } |
| 18324 | else { |
| 18325 | if (Jim_GetWide(interp, argv[1], &res) != JIM_OK) { |
| 18326 | if (Jim_GetDouble(interp, argv[1], &doubleRes) |
| 18327 | != JIM_OK) { |
| 18328 | return JIM_ERR; |
| 18329 | } |
| 18330 | else { |
| 18331 | goto trydouble; |
| 18332 | } |
| 18333 | } |
| 18334 | } |
| 18335 | for (i = 2; i < argc; i++) { |
| 18336 | if (Jim_GetWide(interp, argv[i], &wideValue) != JIM_OK) { |
| 18337 | doubleRes = (double)res; |
| 18338 | goto trydouble; |
| 18339 | } |
| 18340 | if (op == JIM_EXPROP_SUB) |
| 18341 | res -= wideValue; |
| 18342 | else { |
| 18343 | if (wideValue == 0) { |
| 18344 | Jim_SetResultString(interp, "Division by zero", -1); |
| 18345 | return JIM_ERR; |
| 18346 | } |
| 18347 | res /= wideValue; |
| 18348 | } |
| 18349 | } |
| 18350 | Jim_SetResultInt(interp, res); |
| 18351 | return JIM_OK; |
| 18352 | trydouble: |
| 18353 | for (; i < argc; i++) { |
| 18354 | if (Jim_GetDouble(interp, argv[i], &doubleValue) != JIM_OK) |
| 18355 | return JIM_ERR; |
| 18356 | if (op == JIM_EXPROP_SUB) |
| 18357 | doubleRes -= doubleValue; |
| 18358 | else |
| 18359 | doubleRes /= doubleValue; |
| 18360 | } |
| 18361 | Jim_SetResult(interp, Jim_NewDoubleObj(interp, doubleRes)); |
| 18362 | return JIM_OK; |
| 18363 | } |
| 18364 | |
| 18365 | |
| 18366 | |
| 18367 | static int Jim_AddCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18368 | { |
| 18369 | return JimAddMulHelper(interp, argc, argv, JIM_EXPROP_ADD); |
| 18370 | } |
| 18371 | |
| 18372 | |
| 18373 | static int Jim_MulCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18374 | { |
| 18375 | return JimAddMulHelper(interp, argc, argv, JIM_EXPROP_MUL); |
| 18376 | } |
| 18377 | |
| 18378 | |
| 18379 | static int Jim_SubCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18380 | { |
| 18381 | return JimSubDivHelper(interp, argc, argv, JIM_EXPROP_SUB); |
| 18382 | } |
| 18383 | |
| 18384 | |
| 18385 | static int Jim_DivCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18386 | { |
| 18387 | return JimSubDivHelper(interp, argc, argv, JIM_EXPROP_DIV); |
| 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) |
| 18398 | return JIM_ERR; |
| 18399 | Jim_SetResult(interp, objPtr); |
| 18400 | return JIM_OK; |
| 18401 | } |
| 18402 | |
| 18403 | if (Jim_SetVariable(interp, argv[1], argv[2]) != JIM_OK) |
| 18404 | return JIM_ERR; |
| 18405 | Jim_SetResult(interp, argv[2]); |
| 18406 | return JIM_OK; |
| 18407 | } |
| 18408 | |
| 18409 | static int Jim_UnsetCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18410 | { |
| 18411 | int i = 1; |
| 18412 | int complain = 1; |
| 18413 | |
| 18414 | while (i < argc) { |
| 18415 | if (Jim_CompareStringImmediate(interp, argv[i], "--")) { |
| 18416 | i++; |
| 18417 | break; |
| 18418 | } |
| 18419 | if (Jim_CompareStringImmediate(interp, argv[i], "-nocomplain")) { |
| 18420 | complain = 0; |
| 18421 | i++; |
| 18422 | continue; |
| 18423 | } |
| 18424 | break; |
| 18425 | } |
| 18426 | |
| 18427 | while (i < argc) { |
| 18428 | if (Jim_UnsetVariable(interp, argv[i], complain ? JIM_ERRMSG : JIM_NONE) != JIM_OK |
| 18429 | && complain) { |
| 18430 | return JIM_ERR; |
| 18431 | } |
| 18432 | i++; |
| 18433 | } |
| 18434 | |
| 18435 | Jim_SetEmptyResult(interp); |
| 18436 | return JIM_OK; |
| 18437 | } |
| 18438 | |
| 18439 | static int JimCheckLoopRetcode(Jim_Interp *interp, int retval) |
| 18440 | { |
| 18441 | if (retval == JIM_BREAK || retval == JIM_CONTINUE) { |
| 18442 | if (--interp->break_level > 0) { |
| 18443 | return 1; |
| 18444 | } |
| 18445 | } |
| 18446 | return 0; |
| 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) |
| 18457 | return retval; |
| 18458 | if (!boolean) |
| 18459 | break; |
| 18460 | |
| 18461 | if ((retval = Jim_EvalObj(interp, argv[2])) != JIM_OK) { |
| 18462 | if (JimCheckLoopRetcode(interp, retval)) { |
| 18463 | return retval; |
| 18464 | } |
| 18465 | switch (retval) { |
| 18466 | case JIM_BREAK: |
| 18467 | goto out; |
| 18468 | case JIM_CONTINUE: |
| 18469 | continue; |
| 18470 | default: |
| 18471 | return retval; |
| 18472 | } |
| 18473 | } |
| 18474 | } |
| 18475 | out: |
| 18476 | Jim_SetEmptyResult(interp); |
| 18477 | return JIM_OK; |
| 18478 | } |
| 18479 | |
| 18480 | |
| 18481 | static int Jim_ForCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18482 | { |
| 18483 | int retval; |
| 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 | |
| 18494 | retval = Jim_GetBoolFromExpr(interp, argv[2], &boolean); |
| 18495 | |
| 18496 | |
| 18497 | #ifdef JIM_OPTIMIZATION |
| 18498 | if (retval == JIM_OK && boolean) { |
| 18499 | ScriptObj *incrScript; |
| 18500 | struct ExprTree *expr; |
| 18501 | jim_wide stop, currentVal; |
| 18502 | Jim_Obj *objPtr; |
| 18503 | int cmpOffset; |
| 18504 | |
| 18505 | |
| 18506 | expr = JimGetExpression(interp, argv[2]); |
| 18507 | incrScript = JimGetScript(interp, argv[3]); |
| 18508 | |
| 18509 | |
| 18510 | if (incrScript == NULL || incrScript->len != 3 || !expr || expr->len != 3) { |
| 18511 | goto evalstart; |
| 18512 | } |
| 18513 | |
| 18514 | if (incrScript->token[1].type != JIM_TT_ESC) { |
| 18515 | goto evalstart; |
| 18516 | } |
| 18517 | |
| 18518 | if (expr->expr->type == JIM_EXPROP_LT) { |
| 18519 | cmpOffset = 0; |
| 18520 | } |
| 18521 | else if (expr->expr->type == JIM_EXPROP_LTE) { |
| 18522 | cmpOffset = 1; |
| 18523 | } |
| 18524 | else { |
| 18525 | goto evalstart; |
| 18526 | } |
| 18527 | |
| 18528 | if (expr->expr->left->type != JIM_TT_VAR) { |
| 18529 | goto evalstart; |
| 18530 | } |
| 18531 | |
| 18532 | if (expr->expr->right->type != JIM_TT_VAR && expr->expr->right->type != JIM_TT_EXPR_INT) { |
| 18533 | goto evalstart; |
| 18534 | } |
| 18535 | |
| 18536 | |
| 18537 | if (!Jim_CompareStringImmediate(interp, incrScript->token[1].objPtr, "incr")) { |
| 18538 | goto evalstart; |
| 18539 | } |
| 18540 | |
| 18541 | |
| 18542 | if (!Jim_StringEqObj(incrScript->token[2].objPtr, expr->expr->left->objPtr)) { |
| 18543 | goto evalstart; |
| 18544 | } |
| 18545 | |
| 18546 | |
| 18547 | if (expr->expr->right->type == JIM_TT_EXPR_INT) { |
| 18548 | if (Jim_GetWideExpr(interp, expr->expr->right->objPtr, &stop) == JIM_ERR) { |
| 18549 | goto evalstart; |
| 18550 | } |
| 18551 | } |
| 18552 | else { |
| 18553 | stopVarNamePtr = expr->expr->right->objPtr; |
| 18554 | Jim_IncrRefCount(stopVarNamePtr); |
| 18555 | |
| 18556 | stop = 0; |
| 18557 | } |
| 18558 | |
| 18559 | |
| 18560 | varNamePtr = expr->expr->left->objPtr; |
| 18561 | Jim_IncrRefCount(varNamePtr); |
| 18562 | |
| 18563 | objPtr = Jim_GetVariable(interp, varNamePtr, JIM_NONE); |
| 18564 | if (objPtr == NULL || Jim_GetWide(interp, objPtr, ¤tVal) != JIM_OK) { |
| 18565 | goto testcond; |
| 18566 | } |
| 18567 | |
| 18568 | |
| 18569 | while (retval == JIM_OK) { |
| 18570 | |
| 18571 | |
| 18572 | |
| 18573 | |
| 18574 | if (stopVarNamePtr) { |
| 18575 | objPtr = Jim_GetVariable(interp, stopVarNamePtr, JIM_NONE); |
| 18576 | if (objPtr == NULL || Jim_GetWide(interp, objPtr, &stop) != JIM_OK) { |
| 18577 | goto testcond; |
| 18578 | } |
| 18579 | } |
| 18580 | |
| 18581 | if (currentVal >= stop + cmpOffset) { |
| 18582 | break; |
| 18583 | } |
| 18584 | |
| 18585 | |
| 18586 | retval = Jim_EvalObj(interp, argv[4]); |
| 18587 | if (JimCheckLoopRetcode(interp, retval)) { |
| 18588 | immediate++; |
| 18589 | goto out; |
| 18590 | } |
| 18591 | if (retval == JIM_OK || retval == JIM_CONTINUE) { |
| 18592 | retval = JIM_OK; |
| 18593 | |
| 18594 | objPtr = Jim_GetVariable(interp, varNamePtr, JIM_ERRMSG); |
| 18595 | |
| 18596 | |
| 18597 | if (objPtr == NULL) { |
| 18598 | retval = JIM_ERR; |
| 18599 | goto out; |
| 18600 | } |
| 18601 | if (!Jim_IsShared(objPtr) && objPtr->typePtr == &intObjType) { |
| 18602 | currentVal = ++JimWideValue(objPtr); |
| 18603 | Jim_InvalidateStringRep(objPtr); |
| 18604 | } |
| 18605 | else { |
| 18606 | if (Jim_GetWide(interp, objPtr, ¤tVal) != JIM_OK || |
| 18607 | Jim_SetVariable(interp, varNamePtr, Jim_NewIntObj(interp, |
| 18608 | ++currentVal)) != JIM_OK) { |
| 18609 | goto evalnext; |
| 18610 | } |
| 18611 | } |
| 18612 | } |
| 18613 | } |
| 18614 | goto out; |
| 18615 | } |
| 18616 | evalstart: |
| 18617 | #endif |
| 18618 | |
| 18619 | while (boolean && (retval == JIM_OK || retval == JIM_CONTINUE)) { |
| 18620 | |
| 18621 | retval = Jim_EvalObj(interp, argv[4]); |
| 18622 | if (JimCheckLoopRetcode(interp, retval)) { |
| 18623 | immediate++; |
| 18624 | break; |
| 18625 | } |
| 18626 | if (retval == JIM_OK || retval == JIM_CONTINUE) { |
| 18627 | |
| 18628 | JIM_IF_OPTIM(evalnext:) |
| 18629 | retval = Jim_EvalObj(interp, argv[3]); |
| 18630 | if (retval == JIM_OK || retval == JIM_CONTINUE) { |
| 18631 | |
| 18632 | JIM_IF_OPTIM(testcond:) |
| 18633 | retval = Jim_GetBoolFromExpr(interp, argv[2], &boolean); |
| 18634 | } |
| 18635 | } |
| 18636 | } |
| 18637 | JIM_IF_OPTIM(out:) |
| 18638 | if (stopVarNamePtr) { |
| 18639 | Jim_DecrRefCount(interp, stopVarNamePtr); |
| 18640 | } |
| 18641 | if (varNamePtr) { |
| 18642 | Jim_DecrRefCount(interp, varNamePtr); |
| 18643 | } |
| 18644 | |
| 18645 | if (!immediate) { |
| 18646 | if (retval == JIM_CONTINUE || retval == JIM_BREAK || retval == JIM_OK) { |
| 18647 | Jim_SetEmptyResult(interp); |
| 18648 | return JIM_OK; |
| 18649 | } |
| 18650 | } |
| 18651 | |
| 18652 | return retval; |
| 18653 | } |
| 18654 | |
| 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) { |
| 18669 | Jim_GetWideExpr(interp, argv[4], &incr); |
| 18670 | } |
| 18671 | if (retval != JIM_OK) { |
| 18672 | return retval; |
| 18673 | } |
| 18674 | if (argc == 4) { |
| 18675 | limit = i; |
| 18676 | i = 0; |
| 18677 | } |
| 18678 | bodyObjPtr = argv[argc - 1]; |
| 18679 | |
| 18680 | retval = Jim_SetVariable(interp, argv[1], Jim_NewIntObj(interp, i)); |
| 18681 | |
| 18682 | while (((i < limit && incr > 0) || (i > limit && incr < 0)) && retval == JIM_OK) { |
| 18683 | retval = Jim_EvalObj(interp, bodyObjPtr); |
| 18684 | if (JimCheckLoopRetcode(interp, retval)) { |
| 18685 | return retval; |
| 18686 | } |
| 18687 | if (retval == JIM_OK || retval == JIM_CONTINUE) { |
| 18688 | Jim_Obj *objPtr = Jim_GetVariable(interp, argv[1], JIM_ERRMSG); |
| 18689 | |
| 18690 | retval = JIM_OK; |
| 18691 | |
| 18692 | |
| 18693 | i += incr; |
| 18694 | |
| 18695 | if (objPtr && !Jim_IsShared(objPtr) && objPtr->typePtr == &intObjType) { |
| 18696 | if (argv[1]->typePtr != &variableObjType) { |
| 18697 | if (Jim_SetVariable(interp, argv[1], objPtr) != JIM_OK) { |
| 18698 | return JIM_ERR; |
| 18699 | } |
| 18700 | } |
| 18701 | JimWideValue(objPtr) = i; |
| 18702 | Jim_InvalidateStringRep(objPtr); |
| 18703 | |
| 18704 | if (argv[1]->typePtr != &variableObjType) { |
| 18705 | if (Jim_SetVariable(interp, argv[1], objPtr) != JIM_OK) { |
| 18706 | retval = JIM_ERR; |
| 18707 | break; |
| 18708 | } |
| 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) { |
| 18719 | Jim_SetEmptyResult(interp); |
| 18720 | return JIM_OK; |
| 18721 | } |
| 18722 | return retval; |
| 18723 | } |
| 18724 | |
| 18725 | typedef struct { |
| 18726 | Jim_Obj *objPtr; |
| 18727 | int idx; |
| 18728 | } Jim_ListIter; |
| 18729 | |
| 18730 | static void JimListIterInit(Jim_ListIter *iter, Jim_Obj *objPtr) |
| 18731 | { |
| 18732 | iter->objPtr = objPtr; |
| 18733 | iter->idx = 0; |
| 18734 | } |
| 18735 | |
| 18736 | static Jim_Obj *JimListIterNext(Jim_Interp *interp, Jim_ListIter *iter) |
| 18737 | { |
| 18738 | if (iter->idx >= Jim_ListLength(interp, iter->objPtr)) { |
| 18739 | return NULL; |
| 18740 | } |
| 18741 | return iter->objPtr->internalRep.listValue.ele[iter->idx++]; |
| 18742 | } |
| 18743 | |
| 18744 | static int JimListIterDone(Jim_Interp *interp, Jim_ListIter *iter) |
| 18745 | { |
| 18746 | return iter->idx >= Jim_ListLength(interp, iter->objPtr); |
| 18747 | } |
| 18748 | |
| 18749 | |
| 18750 | static int JimForeachMapHelper(Jim_Interp *interp, int argc, Jim_Obj *const *argv, int doMap) |
| 18751 | { |
| 18752 | int result = JIM_OK; |
| 18753 | int i, numargs; |
| 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) { |
| 18766 | iters = twoiters; |
| 18767 | } |
| 18768 | else { |
| 18769 | iters = Jim_Alloc(numargs * sizeof(*iters)); |
| 18770 | } |
| 18771 | for (i = 0; i < numargs; i++) { |
| 18772 | JimListIterInit(&iters[i], argv[i + 1]); |
| 18773 | if (i % 2 == 0 && JimListIterDone(interp, &iters[i])) { |
| 18774 | result = JIM_ERR; |
| 18775 | } |
| 18776 | } |
| 18777 | if (result != JIM_OK) { |
| 18778 | Jim_SetResultString(interp, "foreach varlist is empty", -1); |
| 18779 | goto empty_varlist; |
| 18780 | } |
| 18781 | |
| 18782 | if (doMap) { |
| 18783 | resultObj = Jim_NewListObj(interp, NULL, 0); |
| 18784 | } |
| 18785 | else { |
| 18786 | resultObj = interp->emptyObj; |
| 18787 | } |
| 18788 | Jim_IncrRefCount(resultObj); |
| 18789 | |
| 18790 | while (1) { |
| 18791 | |
| 18792 | for (i = 0; i < numargs; i += 2) { |
| 18793 | if (!JimListIterDone(interp, &iters[i + 1])) { |
| 18794 | break; |
| 18795 | } |
| 18796 | } |
| 18797 | if (i == numargs) { |
| 18798 | |
| 18799 | break; |
| 18800 | } |
| 18801 | |
| 18802 | |
| 18803 | for (i = 0; i < numargs; i += 2) { |
| 18804 | Jim_Obj *varName; |
| 18805 | |
| 18806 | |
| 18807 | JimListIterInit(&iters[i], argv[i + 1]); |
| 18808 | while ((varName = JimListIterNext(interp, &iters[i])) != NULL) { |
| 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 | } |
| 18823 | result = Jim_EvalObj(interp, script); |
| 18824 | if (JimCheckLoopRetcode(interp, result)) { |
| 18825 | goto err; |
| 18826 | } |
| 18827 | switch (result) { |
| 18828 | case JIM_OK: |
| 18829 | if (doMap) { |
| 18830 | Jim_ListAppendElement(interp, resultObj, interp->result); |
| 18831 | } |
| 18832 | break; |
| 18833 | case JIM_CONTINUE: |
| 18834 | break; |
| 18835 | case JIM_BREAK: |
| 18836 | goto out; |
| 18837 | default: |
| 18838 | goto err; |
| 18839 | } |
| 18840 | } |
| 18841 | out: |
| 18842 | result = JIM_OK; |
| 18843 | Jim_SetResult(interp, resultObj); |
| 18844 | err: |
| 18845 | Jim_DecrRefCount(interp, resultObj); |
| 18846 | empty_varlist: |
| 18847 | if (numargs > 2) { |
| 18848 | Jim_Free(iters); |
| 18849 | } |
| 18850 | return result; |
| 18851 | } |
| 18852 | |
| 18853 | |
| 18854 | static int Jim_ForeachCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18855 | { |
| 18856 | return JimForeachMapHelper(interp, argc, argv, 0); |
| 18857 | } |
| 18858 | |
| 18859 | |
| 18860 | static int Jim_LmapCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18861 | { |
| 18862 | return JimForeachMapHelper(interp, argc, argv, 1); |
| 18863 | } |
| 18864 | |
| 18865 | |
| 18866 | static int Jim_LassignCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18867 | { |
| 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); |
| 18878 | if (result != JIM_OK) { |
| 18879 | return result; |
| 18880 | } |
| 18881 | } |
| 18882 | |
| 18883 | resultObj = Jim_NewListObj(interp, NULL, 0); |
| 18884 | while (!JimListIterDone(interp, &iter)) { |
| 18885 | Jim_ListAppendElement(interp, resultObj, JimListIterNext(interp, &iter)); |
| 18886 | } |
| 18887 | |
| 18888 | Jim_SetResult(interp, resultObj); |
| 18889 | |
| 18890 | return JIM_OK; |
| 18891 | } |
| 18892 | |
| 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; |
| 18949 | int rc; |
| 18950 | |
| 18951 | parms[argc++] = commandObj; |
| 18952 | if (flags & JIM_NOCASE) { |
| 18953 | parms[argc++] = Jim_NewStringObj(interp, "-nocase", -1); |
| 18954 | } |
| 18955 | if (flags & JIM_OPT_END) { |
| 18956 | parms[argc++] = Jim_NewStringObj(interp, "--", -1); |
| 18957 | } |
| 18958 | parms[argc++] = patternObj; |
| 18959 | parms[argc++] = stringObj; |
| 18960 | |
| 18961 | rc = Jim_EvalObjVector(interp, argc, parms); |
| 18962 | |
| 18963 | if (rc != JIM_OK || Jim_GetLong(interp, Jim_GetResult(interp), &eq) != JIM_OK) { |
| 18964 | eq = -rc; |
| 18965 | } |
| 18966 | |
| 18967 | return eq; |
| 18968 | } |
| 18969 | |
| 18970 | |
| 18971 | static int Jim_SwitchCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18972 | { |
| 18973 | enum { SWITCH_EXACT, SWITCH_GLOB, SWITCH_RE, SWITCH_CMD }; |
| 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; |
| 18984 | else if (strncmp(option, "--", 2) == 0) { |
| 18985 | ++opt; |
| 18986 | break; |
| 18987 | } |
| 18988 | else if (strncmp(option, "-exact", 2) == 0) |
| 18989 | matchOpt = SWITCH_EXACT; |
| 18990 | else if (strncmp(option, "-glob", 2) == 0) |
| 18991 | matchOpt = SWITCH_GLOB; |
| 18992 | else if (strncmp(option, "-regexp", 2) == 0) { |
| 18993 | matchOpt = SWITCH_RE; |
| 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)) { |
| 19025 | switch (matchOpt) { |
| 19026 | case SWITCH_EXACT: |
| 19027 | if (Jim_StringEqObj(strObj, patObj)) |
| 19028 | scriptObj = caseList[i + 1]; |
| 19029 | break; |
| 19030 | case SWITCH_GLOB: |
| 19031 | if (Jim_StringMatchObj(interp, patObj, strObj, 0)) |
| 19032 | scriptObj = caseList[i + 1]; |
| 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 | |
| 19044 | if (rc < 0) { |
| 19045 | return -rc; |
| 19046 | } |
| 19047 | if (rc) |
| 19048 | scriptObj = caseList[i + 1]; |
| 19049 | break; |
| 19050 | } |
| 19051 | } |
| 19052 | } |
| 19053 | else { |
| 19054 | scriptObj = caseList[i + 1]; |
| 19055 | } |
| 19056 | } |
| 19057 | for (; i < patCount && Jim_CompareStringImmediate(interp, scriptObj, "-"); i += 2) |
| 19058 | scriptObj = caseList[i + 1]; |
| 19059 | if (scriptObj && Jim_CompareStringImmediate(interp, scriptObj, "-")) { |
| 19060 | Jim_SetResultFormatted(interp, "no body specified for pattern \"%#s\"", caseList[i - 2]); |
| 19061 | return JIM_ERR; |
| 19062 | } |
| 19063 | Jim_SetEmptyResult(interp); |
| 19064 | if (scriptObj) { |
| 19065 | return Jim_EvalObj(interp, scriptObj); |
| 19066 | } |
| 19067 | return JIM_OK; |
| 19068 | } |
| 19069 | |
| 19070 | |
| 19071 | static int Jim_ListCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19072 | { |
| 19073 | Jim_Obj *listObjPtr; |
| 19074 | |
| 19075 | listObjPtr = Jim_NewListObj(interp, argv + 1, argc - 1); |
| 19076 | Jim_SetResult(interp, listObjPtr); |
| 19077 | return JIM_OK; |
| 19078 | } |
| 19079 | |
| 19080 | |
| 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 | } |
| 19091 | else if (ret == JIM_OK) { |
| 19092 | Jim_SetResult(interp, objPtr); |
| 19093 | } |
| 19094 | return ret; |
| 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 | |
| 19105 | static int Jim_LsearchCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19106 | { |
| 19107 | static const char * const options[] = { |
| 19108 | "-bool", "-not", "-nocase", "-exact", "-glob", "-regexp", "-all", "-inline", "-command", |
| 19109 | "-stride", "-index", NULL |
| 19110 | }; |
| 19111 | enum |
| 19112 | { OPT_BOOL, OPT_NOT, OPT_NOCASE, OPT_EXACT, OPT_GLOB, OPT_REGEXP, OPT_ALL, OPT_INLINE, |
| 19113 | OPT_COMMAND, OPT_STRIDE, OPT_INDEX }; |
| 19114 | int i; |
| 19115 | int opt_bool = 0; |
| 19116 | int opt_not = 0; |
| 19117 | int opt_all = 0; |
| 19118 | int opt_inline = 0; |
| 19119 | int opt_match = OPT_EXACT; |
| 19120 | int listlen; |
| 19121 | int rc = JIM_OK; |
| 19122 | Jim_Obj *listObjPtr = NULL; |
| 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; |
| 19133 | } |
| 19134 | switch (option) { |
| 19135 | case OPT_BOOL: |
| 19136 | opt_bool = 1; |
| 19137 | opt_inline = 0; |
| 19138 | break; |
| 19139 | case OPT_NOT: |
| 19140 | opt_not = 1; |
| 19141 | break; |
| 19142 | case OPT_NOCASE: |
| 19143 | match_flags |= JIM_NOCASE; |
| 19144 | break; |
| 19145 | case OPT_INLINE: |
| 19146 | opt_inline = 1; |
| 19147 | opt_bool = 0; |
| 19148 | break; |
| 19149 | case OPT_ALL: |
| 19150 | opt_all = 1; |
| 19151 | break; |
| 19152 | case OPT_REGEXP: |
| 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) { |
| 19180 | Jim_SetResultString(interp, "stride length must be at least 1", -1); |
| 19181 | return JIM_ERR; |
| 19182 | } |
| 19183 | break; |
| 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) { |
| 19195 | Jim_SetResultString(interp, "list size must be a multiple of the stride length", -1); |
| 19196 | return JIM_ERR; |
| 19197 | } |
| 19198 | |
| 19199 | if (opt_all) { |
| 19200 | listObjPtr = Jim_NewListObj(interp, NULL, 0); |
| 19201 | } |
| 19202 | if (opt_match == OPT_REGEXP) { |
| 19203 | commandObj = Jim_NewStringObj(interp, "regexp", -1); |
| 19204 | } |
| 19205 | if (commandObj) { |
| 19206 | Jim_IncrRefCount(commandObj); |
| 19207 | } |
| 19208 | |
| 19209 | for (i = 0; i < listlen; i += stride) { |
| 19210 | int eq = 0; |
| 19211 | Jim_Obj *searchListObj; |
| 19212 | Jim_Obj *objPtr; |
| 19213 | int offset; |
| 19214 | |
| 19215 | if (indexObj) { |
| 19216 | int indexlen = Jim_ListLength(interp, indexObj); |
| 19217 | if (stride == 1) { |
| 19218 | searchListObj = Jim_ListGetIndex(interp, argv[0], i); |
| 19219 | } |
| 19220 | else { |
| 19221 | searchListObj = Jim_NewListObj(interp, argv[0]->internalRep.listValue.ele + i, stride); |
| 19222 | } |
| 19223 | Jim_IncrRefCount(searchListObj); |
| 19224 | rc = Jim_ListIndices(interp, searchListObj, indexObj->internalRep.listValue.ele, indexlen, &objPtr, JIM_ERRMSG); |
| 19225 | if (rc != JIM_OK) { |
| 19226 | Jim_DecrRefCount(interp, searchListObj); |
| 19227 | rc = JIM_ERR; |
| 19228 | goto done; |
| 19229 | } |
| 19230 | |
| 19231 | offset = 0; |
| 19232 | } |
| 19233 | else { |
| 19234 | |
| 19235 | searchListObj = argv[0]; |
| 19236 | offset = i; |
| 19237 | objPtr = Jim_ListGetIndex(interp, searchListObj, i); |
| 19238 | Jim_IncrRefCount(searchListObj); |
| 19239 | } |
| 19240 | |
| 19241 | switch (opt_match) { |
| 19242 | case OPT_EXACT: |
| 19243 | eq = Jim_StringCompareObj(interp, argv[1], objPtr, match_flags) == 0; |
| 19244 | break; |
| 19245 | |
| 19246 | case OPT_GLOB: |
| 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 | } |
| 19258 | break; |
| 19259 | } |
| 19260 | |
| 19261 | |
| 19262 | if ((!opt_bool && eq == !opt_not) || (opt_bool && (eq || opt_all))) { |
| 19263 | Jim_Obj *resultObj; |
| 19264 | |
| 19265 | if (opt_bool) { |
| 19266 | resultObj = Jim_NewIntObj(interp, eq ^ opt_not); |
| 19267 | } |
| 19268 | else if (!opt_inline) { |
| 19269 | resultObj = Jim_NewIntObj(interp, i); |
| 19270 | } |
| 19271 | else if (stride == 1) { |
| 19272 | resultObj = objPtr; |
| 19273 | } |
| 19274 | else if (opt_all) { |
| 19275 | |
| 19276 | ListInsertElements(listObjPtr, -1, stride, |
| 19277 | searchListObj->internalRep.listValue.ele + offset); |
| 19278 | |
| 19279 | resultObj = NULL; |
| 19280 | } |
| 19281 | else { |
| 19282 | resultObj = Jim_NewListObj(interp, searchListObj->internalRep.listValue.ele + offset, stride); |
| 19283 | } |
| 19284 | |
| 19285 | if (opt_all) { |
| 19286 | |
| 19287 | if (stride == 1) { |
| 19288 | Jim_ListAppendElement(interp, listObjPtr, resultObj); |
| 19289 | } |
| 19290 | } |
| 19291 | else { |
| 19292 | Jim_SetResult(interp, resultObj); |
| 19293 | Jim_DecrRefCount(interp, searchListObj); |
| 19294 | goto done; |
| 19295 | } |
| 19296 | } |
| 19297 | Jim_DecrRefCount(interp, searchListObj); |
| 19298 | } |
| 19299 | |
| 19300 | if (opt_all) { |
| 19301 | Jim_SetResult(interp, listObjPtr); |
| 19302 | listObjPtr = NULL; |
| 19303 | } |
| 19304 | else { |
| 19305 | |
| 19306 | if (opt_bool) { |
| 19307 | Jim_SetResultBool(interp, opt_not); |
| 19308 | } |
| 19309 | else if (!opt_inline) { |
| 19310 | Jim_SetResultInt(interp, -1); |
| 19311 | } |
| 19312 | } |
| 19313 | |
| 19314 | done: |
| 19315 | if (listObjPtr) { |
| 19316 | Jim_FreeNewObj(interp, listObjPtr); |
| 19317 | } |
| 19318 | if (commandObj) { |
| 19319 | Jim_DecrRefCount(interp, commandObj); |
| 19320 | } |
| 19321 | return rc; |
| 19322 | } |
| 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 | } |
| 19346 | |
| 19347 | |
| 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; |
| 19358 | len = Jim_ListLength(interp, listPtr); |
| 19359 | if (idx >= len) |
| 19360 | idx = len; |
| 19361 | else if (idx < 0) |
| 19362 | idx = len + idx + 1; |
| 19363 | Jim_ListInsertElements(interp, listPtr, idx, argc - 3, &argv[3]); |
| 19364 | Jim_SetResult(interp, listPtr); |
| 19365 | return JIM_OK; |
| 19366 | err: |
| 19367 | if (listPtr != argv[1]) { |
| 19368 | Jim_FreeNewObj(interp, listPtr); |
| 19369 | } |
| 19370 | return JIM_ERR; |
| 19371 | } |
| 19372 | |
| 19373 | |
| 19374 | static int Jim_LreplaceCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 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 | |
| 19385 | listObj = argv[1]; |
| 19386 | len = Jim_ListLength(interp, listObj); |
| 19387 | |
| 19388 | first = JimRelToAbsIndex(len, first); |
| 19389 | last = JimRelToAbsIndex(len, last); |
| 19390 | JimRelToAbsRange(len, &first, &last, &rangeLen); |
| 19391 | |
| 19392 | |
| 19393 | if (first > len) { |
| 19394 | first = len; |
| 19395 | } |
| 19396 | |
| 19397 | |
| 19398 | newListObj = Jim_NewListObj(interp, listObj->internalRep.listValue.ele, first); |
| 19399 | |
| 19400 | |
| 19401 | ListInsertElements(newListObj, -1, argc - 4, argv + 4); |
| 19402 | |
| 19403 | |
| 19404 | ListInsertElements(newListObj, -1, len - first - rangeLen, listObj->internalRep.listValue.ele + first + rangeLen); |
| 19405 | |
| 19406 | Jim_SetResult(interp, newListObj); |
| 19407 | return JIM_OK; |
| 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; |
| 19419 | } |
| 19420 | return Jim_ListSetIndex(interp, argv[1], argv + 2, argc - 3, argv[argc - 1]); |
| 19421 | } |
| 19422 | |
| 19423 | |
| 19424 | static int Jim_LsortCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const argv[]) |
| 19425 | { |
| 19426 | static const char * const options[] = { |
| 19427 | "-ascii", "-nocase", "-increasing", "-decreasing", "-command", "-integer", "-real", "-index", "-unique", |
| 19428 | "-stride", "-dictionary", NULL |
| 19429 | }; |
| 19430 | enum { |
| 19431 | OPT_ASCII, OPT_NOCASE, OPT_INCREASING, OPT_DECREASING, OPT_COMMAND, OPT_INTEGER, OPT_REAL, OPT_INDEX, OPT_UNIQUE, |
| 19432 | OPT_STRIDE, OPT_DICT |
| 19433 | }; |
| 19434 | Jim_Obj *resObj; |
| 19435 | int i; |
| 19436 | int retCode; |
| 19437 | int shared; |
| 19438 | long stride = 1; |
| 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; |
| 19449 | info.interp = interp; |
| 19450 | |
| 19451 | for (i = 1; i < (argc - 1); i++) { |
| 19452 | int option; |
| 19453 | |
| 19454 | if (Jim_GetEnum(interp, argv[i], options, &option, NULL, JIM_ENUM_ABBREV | JIM_ERRMSG) |
| 19455 | != JIM_OK) |
| 19456 | return JIM_ERR; |
| 19457 | switch (option) { |
| 19458 | case OPT_ASCII: |
| 19459 | info.type = JIM_LSORT_ASCII; |
| 19460 | break; |
| 19461 | case OPT_DICT: |
| 19462 | info.type = JIM_LSORT_DICT; |
| 19463 | break; |
| 19464 | case OPT_NOCASE: |
| 19465 | info.type = JIM_LSORT_NOCASE; |
| 19466 | break; |
| 19467 | case OPT_INTEGER: |
| 19468 | info.type = JIM_LSORT_INTEGER; |
| 19469 | break; |
| 19470 | case OPT_REAL: |
| 19471 | info.type = JIM_LSORT_REAL; |
| 19472 | break; |
| 19473 | case OPT_INCREASING: |
| 19474 | info.order = 1; |
| 19475 | break; |
| 19476 | case OPT_DECREASING: |
| 19477 | info.order = -1; |
| 19478 | break; |
| 19479 | case OPT_UNIQUE: |
| 19480 | info.unique = 1; |
| 19481 | break; |
| 19482 | case OPT_COMMAND: |
| 19483 | if (i >= (argc - 2)) { |
| 19484 | Jim_SetResultString(interp, "\"-command\" option must be followed by comparison command", -1); |
| 19485 | return JIM_ERR; |
| 19486 | } |
| 19487 | info.type = JIM_LSORT_COMMAND; |
| 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) { |
| 19499 | Jim_SetResultString(interp, "stride length must be at least 2", -1); |
| 19500 | return JIM_ERR; |
| 19501 | } |
| 19502 | break; |
| 19503 | case OPT_INDEX: |
| 19504 | if (i >= (argc - 2)) { |
| 19505 | badindex: |
| 19506 | Jim_SetResultString(interp, "\"-index\" option must be followed by list index", -1); |
| 19507 | return JIM_ERR; |
| 19508 | } |
| 19509 | JimListGetElements(interp, argv[i + 1], &info.indexc, &info.indexv); |
| 19510 | if (info.indexc == 0) { |
| 19511 | goto badindex; |
| 19512 | } |
| 19513 | i++; |
| 19514 | break; |
| 19515 | } |
| 19516 | } |
| 19517 | resObj = argv[argc - 1]; |
| 19518 | JimListGetElements(interp, resObj, &listlen, &elements); |
| 19519 | if (listlen <= 1) { |
| 19520 | |
| 19521 | Jim_SetResult(interp, resObj); |
| 19522 | return JIM_OK; |
| 19523 | } |
| 19524 | |
| 19525 | if (stride > 1) { |
| 19526 | Jim_Obj *tmpListObj; |
| 19527 | int i; |
| 19528 | |
| 19529 | if (listlen % stride) { |
| 19530 | Jim_SetResultString(interp, "list size must be a multiple of the stride length", -1); |
| 19531 | return JIM_ERR; |
| 19532 | } |
| 19533 | |
| 19534 | tmpListObj = Jim_NewListObj(interp, NULL, 0); |
| 19535 | Jim_IncrRefCount(tmpListObj); |
| 19536 | for (i = 0; i < listlen; i += stride) { |
| 19537 | Jim_ListAppendElement(interp, tmpListObj, Jim_NewListObj(interp, elements + i, stride)); |
| 19538 | } |
| 19539 | retCode = ListSortElements(interp, tmpListObj, &info); |
| 19540 | if (retCode == JIM_OK) { |
| 19541 | resObj = Jim_NewListObj(interp, NULL, 0); |
| 19542 | |
| 19543 | for (i = 0; i < listlen; i += stride) { |
| 19544 | Jim_ListAppendList(interp, resObj, Jim_ListGetIndex(interp, tmpListObj, i / stride)); |
| 19545 | } |
| 19546 | Jim_SetResult(interp, resObj); |
| 19547 | } |
| 19548 | Jim_DecrRefCount(interp, tmpListObj); |
| 19549 | } |
| 19550 | else { |
| 19551 | if ((shared = Jim_IsShared(resObj))) { |
| 19552 | resObj = Jim_DuplicateObj(interp, resObj); |
| 19553 | } |
| 19554 | retCode = ListSortElements(interp, resObj, &info); |
| 19555 | if (retCode == JIM_OK) { |
| 19556 | Jim_SetResult(interp, resObj); |
| 19557 | } |
| 19558 | else if (shared) { |
| 19559 | Jim_FreeNewObj(interp, resObj); |
| 19560 | } |
| 19561 | } |
| 19562 | return retCode; |
| 19563 | } |
| 19564 | |
| 19565 | |
| 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; |
| 19594 | } |
| 19595 | |
| 19596 | |
| 19597 | |
| 19598 | |
| 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)); |
| 19609 | } |
| 19610 | |
| 19611 | return rc; |
| 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; |
| 19679 | } |
| 19680 | interp->break_level = level; |
| 19681 | } |
| 19682 | return retcode; |
| 19683 | } |
| 19684 | |
| 19685 | |
| 19686 | static int Jim_BreakCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19687 | { |
| 19688 | return JimBreakContinueHelper(interp, argc, argv, JIM_BREAK); |
| 19689 | } |
| 19690 | |
| 19691 | |
| 19692 | static int Jim_ContinueCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19693 | { |
| 19694 | return JimBreakContinueHelper(interp, argc, argv, JIM_CONTINUE); |
| 19695 | } |
| 19696 | |
| 19697 | |
| 19698 | static int Jim_StacktraceCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19699 | { |
| 19700 | Jim_Obj *listObj; |
| 19701 | int i; |
| 19702 | jim_wide skip = 0; |
| 19703 | jim_wide last = 0; |
| 19704 | |
| 19705 | if (argc > 1) { |
| 19706 | if (Jim_GetWideExpr(interp, argv[1], &skip) != JIM_OK) { |
| 19707 | return JIM_ERR; |
| 19708 | } |
| 19709 | } |
| 19710 | if (argc > 2) { |
| 19711 | if (Jim_GetWideExpr(interp, argv[2], &last) != JIM_OK) { |
| 19712 | return JIM_ERR; |
| 19713 | } |
| 19714 | } |
| 19715 | |
| 19716 | listObj = Jim_NewListObj(interp, NULL, 0); |
| 19717 | for (i = skip; i <= interp->procLevel; i++) { |
| 19718 | Jim_EvalFrame *frame = JimGetEvalFrameByProcLevel(interp, -i); |
| 19719 | if (frame->procLevel < last) { |
| 19720 | break; |
| 19721 | } |
| 19722 | JimAddStackFrame(interp, frame, listObj); |
| 19723 | } |
| 19724 | Jim_SetResult(interp, listObj); |
| 19725 | return JIM_OK; |
| 19726 | } |
| 19727 | |
| 19728 | |
| 19729 | static int Jim_ReturnCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19730 | { |
| 19731 | int i; |
| 19732 | Jim_Obj *stackTraceObj = NULL; |
| 19733 | Jim_Obj *errorCodeObj = NULL; |
| 19734 | int returnCode = JIM_OK; |
| 19735 | long level = 1; |
| 19736 | |
| 19737 | for (i = 1; i < argc - 1; i += 2) { |
| 19738 | if (Jim_CompareStringImmediate(interp, argv[i], "-code")) { |
| 19739 | if (Jim_GetReturnCode(interp, argv[i + 1], &returnCode) == JIM_ERR) { |
| 19740 | return JIM_ERR; |
| 19741 | } |
| 19742 | } |
| 19743 | else if (Jim_CompareStringImmediate(interp, argv[i], "-errorinfo")) { |
| 19744 | stackTraceObj = argv[i + 1]; |
| 19745 | } |
| 19746 | else if (Jim_CompareStringImmediate(interp, argv[i], "-errorcode")) { |
| 19747 | errorCodeObj = argv[i + 1]; |
| 19748 | } |
| 19749 | else if (Jim_CompareStringImmediate(interp, argv[i], "-level")) { |
| 19750 | if (Jim_GetLong(interp, argv[i + 1], &level) != JIM_OK || level < 0) { |
| 19751 | Jim_SetResultFormatted(interp, "bad level \"%#s\"", argv[i + 1]); |
| 19752 | return JIM_ERR; |
| 19753 | } |
| 19754 | } |
| 19755 | else { |
| 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); |
| 19767 | } |
| 19768 | |
| 19769 | if (errorCodeObj && returnCode == JIM_ERR) { |
| 19770 | Jim_SetGlobalVariableStr(interp, "errorCode", errorCodeObj); |
| 19771 | } |
| 19772 | interp->returnCode = returnCode; |
| 19773 | interp->returnLevel = level; |
| 19774 | |
| 19775 | if (i == argc - 1) { |
| 19776 | Jim_SetResult(interp, argv[i]); |
| 19777 | } |
| 19778 | return level == 0 ? returnCode : JIM_RETURN; |
| 19779 | } |
| 19780 | |
| 19781 | |
| 19782 | static int Jim_TailcallCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19783 | { |
| 19784 | if (interp->framePtr->level == 0) { |
| 19785 | Jim_SetResultString(interp, "tailcall can only be called from a proc or lambda", -1); |
| 19786 | return JIM_ERR; |
| 19787 | } |
| 19788 | else if (argc >= 2) { |
| 19789 | |
| 19790 | Jim_CallFrame *cf = interp->framePtr->parent; |
| 19791 | |
| 19792 | Jim_Cmd *cmdPtr = Jim_GetCommand(interp, argv[1], JIM_ERRMSG); |
| 19793 | if (cmdPtr == NULL) { |
| 19794 | return JIM_ERR; |
| 19795 | } |
| 19796 | |
| 19797 | JimPanic((cf->tailcallCmd != NULL, "Already have a tailcallCmd")); |
| 19798 | |
| 19799 | |
| 19800 | JimIncrCmdRefCount(cmdPtr); |
| 19801 | cf->tailcallCmd = cmdPtr; |
| 19802 | |
| 19803 | |
| 19804 | JimPanic((cf->tailcallObj != NULL, "Already have a tailcallobj")); |
| 19805 | |
| 19806 | cf->tailcallObj = Jim_NewListObj(interp, argv + 1, argc - 1); |
| 19807 | Jim_IncrRefCount(cf->tailcallObj); |
| 19808 | |
| 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); |
| 19820 | |
| 19821 | |
| 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); |
| 19856 | } |
| 19857 | |
| 19858 | if (cmd) { |
| 19859 | |
| 19860 | Jim_Obj *nameObjPtr = JimQualifyName(interp, argv[1]); |
| 19861 | JimCreateCommand(interp, nameObjPtr, cmd); |
| 19862 | |
| 19863 | |
| 19864 | JimUpdateProcNamespace(interp, cmd, nameObjPtr); |
| 19865 | Jim_DecrRefCount(interp, nameObjPtr); |
| 19866 | |
| 19867 | |
| 19868 | Jim_SetResult(interp, argv[1]); |
| 19869 | return JIM_OK; |
| 19870 | } |
| 19871 | return JIM_ERR; |
| 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 | |
| 19882 | if (Jim_Length(argv[1])) { |
| 19883 | |
| 19884 | interp->traceCmdObj = argv[1]; |
| 19885 | Jim_IncrRefCount(interp->traceCmdObj); |
| 19886 | } |
| 19887 | return JIM_OK; |
| 19888 | } |
| 19889 | |
| 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 | |
| 19900 | |
| 19901 | |
| 19902 | if (retcode == 0) { |
| 19903 | Jim_Obj *cmdNameObj = Jim_GetResult(interp); |
| 19904 | |
| 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 | |
| 19916 | return retcode; |
| 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)); |
| 20046 | return JIM_OK; |
| 20047 | } |
| 20048 | |
| 20049 | |
| 20050 | static int Jim_UpvarCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 20051 | { |
| 20052 | int i; |
| 20053 | Jim_CallFrame *targetCallFrame; |
| 20054 | |
| 20055 | |
| 20056 | if (argc > 3 && (argc % 2 == 0)) { |
| 20057 | targetCallFrame = Jim_GetCallFrameByLevel(interp, argv[1]); |
| 20058 | argc--; |
| 20059 | argv++; |
| 20060 | } |
| 20061 | else { |
| 20062 | targetCallFrame = Jim_GetCallFrameByLevel(interp, NULL); |
| 20063 | } |
| 20064 | if (targetCallFrame == NULL) { |
| 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) |
| 20076 | return JIM_ERR; |
| 20077 | } |
| 20078 | return JIM_OK; |
| 20079 | } |
| 20080 | |
| 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 | |
| 20091 | const char *name = Jim_String(argv[i]); |
| 20092 | if (name[0] != ':' || name[1] != ':') { |
| 20093 | if (Jim_SetVariableLink(interp, argv[i], argv[i], interp->topFramePtr) != JIM_OK) |
| 20094 | return JIM_ERR; |
| 20095 | } |
| 20096 | } |
| 20097 | return JIM_OK; |
| 20098 | } |
| 20099 | |
| 20100 | static Jim_Obj *JimStringMap(Jim_Interp *interp, Jim_Obj *mapListObjPtr, |
| 20101 | Jim_Obj *objPtr, int nocase) |
| 20102 | { |
| 20103 | int numMaps; |
| 20104 | const char *str, *noMatchStart = NULL; |
| 20105 | int strLen, i; |
| 20106 | Jim_Obj *resultObjPtr; |
| 20107 | |
| 20108 | numMaps = Jim_ListLength(interp, mapListObjPtr); |
| 20109 | if (numMaps % 2) { |
| 20110 | Jim_SetResultString(interp, "list must contain an even number of elements", -1); |
| 20111 | return NULL; |
| 20112 | } |
| 20113 | |
| 20114 | str = Jim_String(objPtr); |
| 20115 | strLen = Jim_Utf8Length(interp, objPtr); |
| 20116 | |
| 20117 | |
| 20118 | resultObjPtr = Jim_NewStringObj(interp, "", 0); |
| 20119 | while (strLen) { |
| 20120 | for (i = 0; i < numMaps; i += 2) { |
| 20121 | Jim_Obj *eachObjPtr; |
| 20122 | const char *k; |
| 20123 | int kl; |
| 20124 | |
| 20125 | eachObjPtr = Jim_ListGetIndex(interp, mapListObjPtr, i); |
| 20126 | k = Jim_String(eachObjPtr); |
| 20127 | kl = Jim_Utf8Length(interp, eachObjPtr); |
| 20128 | |
| 20129 | if (strLen >= kl && kl) { |
| 20130 | int rc; |
| 20131 | rc = JimStringCompareUtf8(str, kl, k, kl, nocase); |
| 20132 | if (rc == 0) { |
| 20133 | if (noMatchStart) { |
| 20134 | Jim_AppendString(interp, resultObjPtr, noMatchStart, str - noMatchStart); |
| 20135 | noMatchStart = NULL; |
| 20136 | } |
| 20137 | Jim_AppendObj(interp, resultObjPtr, Jim_ListGetIndex(interp, mapListObjPtr, i + 1)); |
| 20138 | str += utf8_index(str, kl); |
| 20139 | strLen -= kl; |
| 20140 | break; |
| 20141 | } |
| 20142 | } |
| 20143 | } |
| 20144 | if (i == numMaps) { |
| 20145 | int c; |
| 20146 | if (noMatchStart == NULL) |
| 20147 | noMatchStart = str; |
| 20148 | str += utf8_tounicode(str, &c); |
| 20149 | strLen--; |
| 20150 | } |
| 20151 | } |
| 20152 | if (noMatchStart) { |
| 20153 | Jim_AppendString(interp, resultObjPtr, noMatchStart, str - noMatchStart); |
| 20154 | } |
| 20155 | return resultObjPtr; |
| 20156 | } |
| 20157 | |
| 20158 | |
| 20159 | static int Jim_StringCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 20160 | { |
| 20161 | int len; |
| 20162 | int opt_case = 1; |
| 20163 | int option; |
| 20164 | static const char * const nocase_options[] = { |
| 20165 | "-nocase", NULL |
| 20166 | }; |
| 20167 | static const char * const nocase_length_options[] = { |
| 20168 | "-nocase", "-length", NULL |
| 20169 | }; |
| 20170 | |
| 20171 | enum { |
| 20172 | OPT_BYTELENGTH, |
| 20173 | OPT_BYTERANGE, |
| 20174 | OPT_CAT, |
| 20175 | OPT_COMPARE, |
| 20176 | OPT_EQUAL, |
| 20177 | OPT_FIRST, |
| 20178 | OPT_INDEX, |
| 20179 | OPT_IS, |
| 20180 | OPT_LAST, |
| 20181 | OPT_LENGTH, |
| 20182 | OPT_MAP, |
| 20183 | OPT_MATCH, |
| 20184 | OPT_RANGE, |
| 20185 | OPT_REPEAT, |
| 20186 | OPT_REPLACE, |
| 20187 | OPT_REVERSE, |
| 20188 | OPT_TOLOWER, |
| 20189 | OPT_TOTITLE, |
| 20190 | OPT_TOUPPER, |
| 20191 | OPT_TRIM, |
| 20192 | OPT_TRIMLEFT, |
| 20193 | OPT_TRIMRIGHT, |
| 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), |
| 20205 | JIM_DEF_SUBCMD("last", "subString string ?index?", 2, 3), |
| 20206 | JIM_DEF_SUBCMD("length","string", 1, 1), |
| 20207 | JIM_DEF_SUBCMD("map", "?-nocase? mapList string", 2, 3), |
| 20208 | JIM_DEF_SUBCMD("match", "?-nocase? pattern string", 2, 3), |
| 20209 | JIM_DEF_SUBCMD("range", "string first last", 3, 3), |
| 20210 | JIM_DEF_SUBCMD("repeat", "string count", 2, 2), |
| 20211 | JIM_DEF_SUBCMD("replace", "string first last ?string?", 3, 4), |
| 20212 | JIM_DEF_SUBCMD("reverse", "string", 1, 1), |
| 20213 | JIM_DEF_SUBCMD("tolower", "string", 1, 1), |
| 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 | } |
| 20225 | if (ct->function) { |
| 20226 | |
| 20227 | return ct->function(interp, argc, argv); |
| 20228 | } |
| 20229 | |
| 20230 | option = ct - cmds; |
| 20231 | |
| 20232 | switch (option) { |
| 20233 | case OPT_LENGTH: |
| 20234 | Jim_SetResultInt(interp, Jim_Utf8Length(interp, argv[2])); |
| 20235 | return JIM_OK; |
| 20236 | |
| 20237 | case OPT_BYTELENGTH: |
| 20238 | Jim_SetResultInt(interp, Jim_Length(argv[2])); |
| 20239 | return JIM_OK; |
| 20240 | |
| 20241 | case OPT_CAT:{ |
| 20242 | Jim_Obj *objPtr; |
| 20243 | if (argc == 3) { |
| 20244 | |
| 20245 | objPtr = argv[2]; |
| 20246 | } |
| 20247 | else { |
| 20248 | int i; |
| 20249 | |
| 20250 | objPtr = Jim_NewStringObj(interp, "", 0); |
| 20251 | |
| 20252 | for (i = 2; i < argc; i++) { |
| 20253 | Jim_AppendObj(interp, objPtr, argv[i]); |
| 20254 | } |
| 20255 | } |
| 20256 | Jim_SetResult(interp, objPtr); |
| 20257 | return JIM_OK; |
| 20258 | } |
| 20259 | |
| 20260 | case OPT_COMPARE: |
| 20261 | case OPT_EQUAL: |
| 20262 | { |
| 20263 | |
| 20264 | long opt_length = -1; |
| 20265 | int n = argc - 4; |
| 20266 | int i = 2; |
| 20267 | while (n > 0) { |
| 20268 | int subopt; |
| 20269 | if (Jim_GetEnum(interp, argv[i++], nocase_length_options, &subopt, NULL, |
| 20270 | JIM_ENUM_ABBREV) != JIM_OK) { |
| 20271 | badcompareargs: |
| 20272 | Jim_SubCmdArgError(interp, ct, argv[0]); |
| 20273 | return JIM_ERR; |
| 20274 | } |
| 20275 | if (subopt == 0) { |
| 20276 | |
| 20277 | opt_case = 0; |
| 20278 | n--; |
| 20279 | } |
| 20280 | else { |
| 20281 | |
| 20282 | if (n < 2) { |
| 20283 | goto badcompareargs; |
| 20284 | } |
| 20285 | if (Jim_GetLong(interp, argv[i++], &opt_length) != JIM_OK) { |
| 20286 | return JIM_ERR; |
| 20287 | } |
| 20288 | n -= 2; |
| 20289 | } |
| 20290 | } |
| 20291 | if (n) { |
| 20292 | goto badcompareargs; |
| 20293 | } |
| 20294 | argv += argc - 2; |
| 20295 | if (opt_length < 0 && option != OPT_COMPARE && opt_case) { |
| 20296 | |
| 20297 | Jim_SetResultBool(interp, Jim_StringEqObj(argv[0], argv[1])); |
| 20298 | } |
| 20299 | else { |
| 20300 | const char *s1 = Jim_String(argv[0]); |
| 20301 | int l1 = Jim_Utf8Length(interp, argv[0]); |
| 20302 | const char *s2 = Jim_String(argv[1]); |
| 20303 | int l2 = Jim_Utf8Length(interp, argv[1]); |
| 20304 | if (opt_length >= 0) { |
| 20305 | if (l1 > opt_length) { |
| 20306 | l1 = opt_length; |
| 20307 | } |
| 20308 | if (l2 > opt_length) { |
| 20309 | l2 = opt_length; |
| 20310 | } |
| 20311 | } |
| 20312 | n = JimStringCompareUtf8(s1, l1, s2, l2, !opt_case); |
| 20313 | Jim_SetResultInt(interp, option == OPT_COMPARE ? n : n == 0); |
| 20314 | } |
| 20315 | return JIM_OK; |
| 20316 | } |
| 20317 | |
| 20318 | case OPT_MATCH: |
| 20319 | if (argc != 4 && |
| 20320 | (argc != 5 || |
| 20321 | Jim_GetEnum(interp, argv[2], nocase_options, &opt_case, NULL, |
| 20322 | JIM_ENUM_ABBREV) != JIM_OK)) { |
| 20323 | Jim_WrongNumArgs(interp, 2, argv, "?-nocase? pattern string"); |
| 20324 | return JIM_ERR; |
| 20325 | } |
| 20326 | if (opt_case == 0) { |
| 20327 | argv++; |
| 20328 | } |
| 20329 | Jim_SetResultBool(interp, Jim_StringMatchObj(interp, argv[2], argv[3], !opt_case)); |
| 20330 | return JIM_OK; |
| 20331 | |
| 20332 | case OPT_MAP:{ |
| 20333 | Jim_Obj *objPtr; |
| 20334 | |
| 20335 | if (argc != 4 && |
| 20336 | (argc != 5 || |
| 20337 | Jim_GetEnum(interp, argv[2], nocase_options, &opt_case, NULL, |
| 20338 | JIM_ENUM_ABBREV) != JIM_OK)) { |
| 20339 | Jim_WrongNumArgs(interp, 2, argv, "?-nocase? mapList string"); |
| 20340 | return JIM_ERR; |
| 20341 | } |
| 20342 | |
| 20343 | if (opt_case == 0) { |
| 20344 | argv++; |
| 20345 | } |
| 20346 | objPtr = JimStringMap(interp, argv[2], argv[3], !opt_case); |
| 20347 | if (objPtr == NULL) { |
| 20348 | return JIM_ERR; |
| 20349 | } |
| 20350 | Jim_SetResult(interp, objPtr); |
| 20351 | return JIM_OK; |
| 20352 | } |
| 20353 | |
| 20354 | case OPT_RANGE:{ |
| 20355 | Jim_Obj *objPtr = Jim_StringRangeObj(interp, argv[2], argv[3], argv[4]); |
| 20356 | if (objPtr == NULL) { |
| 20357 | return JIM_ERR; |
| 20358 | } |
| 20359 | Jim_SetResult(interp, objPtr); |
| 20360 | return JIM_OK; |
| 20361 | } |
| 20362 | |
| 20363 | case OPT_BYTERANGE:{ |
| 20364 | Jim_Obj *objPtr = Jim_StringByteRangeObj(interp, argv[2], argv[3], argv[4]); |
| 20365 | if (objPtr == NULL) { |
| 20366 | return JIM_ERR; |
| 20367 | } |
| 20368 | Jim_SetResult(interp, objPtr); |
| 20369 | return JIM_OK; |
| 20370 | } |
| 20371 | |
| 20372 | case OPT_REPLACE:{ |
| 20373 | Jim_Obj *objPtr = JimStringReplaceObj(interp, argv[2], argv[3], argv[4], argc == 6 ? argv[5] : NULL); |
| 20374 | if (objPtr == NULL) { |
| 20375 | return JIM_ERR; |
| 20376 | } |
| 20377 | Jim_SetResult(interp, objPtr); |
| 20378 | return JIM_OK; |
| 20379 | } |
| 20380 | |
| 20381 | |
| 20382 | case OPT_REPEAT:{ |
| 20383 | Jim_Obj *objPtr; |
| 20384 | jim_wide count; |
| 20385 | |
| 20386 | if (Jim_GetWideExpr(interp, argv[3], &count) != JIM_OK) { |
| 20387 | return JIM_ERR; |
| 20388 | } |
| 20389 | objPtr = Jim_NewStringObj(interp, "", 0); |
| 20390 | if (count > 0) { |
| 20391 | while (count--) { |
| 20392 | Jim_AppendObj(interp, objPtr, argv[2]); |
| 20393 | } |
| 20394 | } |
| 20395 | Jim_SetResult(interp, objPtr); |
| 20396 | return JIM_OK; |
| 20397 | } |
| 20398 | |
| 20399 | case OPT_REVERSE:{ |
| 20400 | char *buf, *p; |
| 20401 | const char *str; |
| 20402 | int i; |
| 20403 | |
| 20404 | str = Jim_GetString(argv[2], &len); |
| 20405 | buf = Jim_Alloc(len + 1); |
| 20406 | assert(buf); |
| 20407 | p = buf + len; |
| 20408 | *p = 0; |
| 20409 | for (i = 0; i < len; ) { |
| 20410 | int c; |
| 20411 | int l = utf8_tounicode(str, &c); |
| 20412 | memcpy(p - l, str, l); |
| 20413 | p -= l; |
| 20414 | i += l; |
| 20415 | str += l; |
| 20416 | } |
| 20417 | Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, buf, len)); |
| 20418 | return JIM_OK; |
| 20419 | } |
| 20420 | |
| 20421 | case OPT_INDEX:{ |
| 20422 | int idx; |
| 20423 | const char *str; |
| 20424 | |
| 20425 | if (Jim_GetIndex(interp, argv[3], &idx) != JIM_OK) { |
| 20426 | return JIM_ERR; |
| 20427 | } |
| 20428 | str = Jim_String(argv[2]); |
| 20429 | len = Jim_Utf8Length(interp, argv[2]); |
| 20430 | idx = JimRelToAbsIndex(len, idx); |
| 20431 | if (idx < 0 || idx >= len || str == NULL) { |
| 20432 | Jim_SetResultString(interp, "", 0); |
| 20433 | } |
| 20434 | else if (len == Jim_Length(argv[2])) { |
| 20435 | |
| 20436 | Jim_SetResultString(interp, str + idx, 1); |
| 20437 | } |
| 20438 | else { |
| 20439 | int c; |
| 20440 | int i = utf8_index(str, idx); |
| 20441 | Jim_SetResultString(interp, str + i, utf8_tounicode(str + i, &c)); |
| 20442 | } |
| 20443 | return JIM_OK; |
| 20444 | } |
| 20445 | |
| 20446 | case OPT_FIRST: |
| 20447 | case OPT_LAST:{ |
| 20448 | int idx = 0, l1, l2; |
| 20449 | const char *s1, *s2; |
| 20450 | |
| 20451 | s1 = Jim_String(argv[2]); |
| 20452 | s2 = Jim_String(argv[3]); |
| 20453 | l1 = Jim_Utf8Length(interp, argv[2]); |
| 20454 | l2 = Jim_Utf8Length(interp, argv[3]); |
| 20455 | if (argc == 5) { |
| 20456 | if (Jim_GetIndex(interp, argv[4], &idx) != JIM_OK) { |
| 20457 | return JIM_ERR; |
| 20458 | } |
| 20459 | idx = JimRelToAbsIndex(l2, idx); |
| 20460 | if (idx < 0) { |
| 20461 | idx = 0; |
| 20462 | } |
| 20463 | } |
| 20464 | else if (option == OPT_LAST) { |
| 20465 | idx = l2; |
| 20466 | } |
| 20467 | if (option == OPT_FIRST) { |
| 20468 | Jim_SetResultInt(interp, JimStringFirst(s1, l1, s2, l2, idx)); |
| 20469 | } |
| 20470 | else { |
| 20471 | #ifdef JIM_UTF8 |
| 20472 | Jim_SetResultInt(interp, JimStringLastUtf8(s1, l1, s2, idx)); |
| 20473 | #else |
| 20474 | Jim_SetResultInt(interp, JimStringLast(s1, l1, s2, idx)); |
| 20475 | #endif |
| 20476 | } |
| 20477 | return JIM_OK; |
| 20478 | } |
| 20479 | |
| 20480 | case OPT_TRIM: |
| 20481 | Jim_SetResult(interp, JimStringTrim(interp, argv[2], argc == 4 ? argv[3] : NULL)); |
| 20482 | return JIM_OK; |
| 20483 | case OPT_TRIMLEFT: |
| 20484 | Jim_SetResult(interp, JimStringTrimLeft(interp, argv[2], argc == 4 ? argv[3] : NULL)); |
| 20485 | return JIM_OK; |
| 20486 | case OPT_TRIMRIGHT:{ |
| 20487 | Jim_SetResult(interp, JimStringTrimRight(interp, argv[2], argc == 4 ? argv[3] : NULL)); |
| 20488 | return JIM_OK; |
| 20489 | } |
| 20490 | |
| 20491 | case OPT_TOLOWER: |
| 20492 | Jim_SetResult(interp, JimStringToLower(interp, argv[2])); |
| 20493 | return JIM_OK; |
| 20494 | case OPT_TOUPPER: |
| 20495 | Jim_SetResult(interp, JimStringToUpper(interp, argv[2])); |
| 20496 | return JIM_OK; |
| 20497 | case OPT_TOTITLE: |
| 20498 | Jim_SetResult(interp, JimStringToTitle(interp, argv[2])); |
| 20499 | return JIM_OK; |
| 20500 | |
| 20501 | case OPT_IS: |
| 20502 | if (argc == 5 && !Jim_CompareStringImmediate(interp, argv[3], "-strict")) { |
| 20503 | Jim_SubCmdArgError(interp, ct, argv[0]); |
| 20504 | return JIM_ERR; |
| 20505 | } |
| 20506 | return JimStringIs(interp, argv[argc - 1], argv[2], argc == 5); |
| 20507 | } |
| 20508 | return JIM_OK; |
| 20509 | } |
| 20510 | |
| 20511 | |
| 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) |
| 20522 | return JIM_OK; |
| 20523 | i = count; |
| 20524 | start = Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW); |
| 20525 | while (i-- > 0) { |
| 20526 | int retval; |
| 20527 | |
| 20528 | retval = Jim_EvalObj(interp, argv[1]); |
| 20529 | if (retval != JIM_OK) { |
| 20530 | return retval; |
| 20531 | } |
| 20532 | } |
| 20533 | elapsed = Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW) - start; |
| 20534 | if (elapsed < count * 10) { |
| 20535 | Jim_SetResult(interp, Jim_NewDoubleObj(interp, elapsed * 1.0 / count)); |
| 20536 | } |
| 20537 | else { |
| 20538 | Jim_SetResultInt(interp, count == 0 ? 0 : elapsed / count); |
| 20539 | } |
| 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 | } |
| 20559 | if (us < 1) { |
| 20560 | |
| 20561 | us = 1000 * 1000; |
| 20562 | } |
| 20563 | |
| 20564 | |
| 20565 | start = Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW); |
| 20566 | count = 0; |
| 20567 | do { |
| 20568 | int retval = Jim_EvalObj(interp, argv[1]); |
| 20569 | delta = Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW) - start; |
| 20570 | if (retval != JIM_OK) { |
| 20571 | return retval; |
| 20572 | } |
| 20573 | count++; |
| 20574 | } while (delta < us); |
| 20575 | |
| 20576 | |
| 20577 | start = Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW); |
| 20578 | n = 0; |
| 20579 | do { |
| 20580 | int retval = Jim_EvalObj(interp, interp->nullScriptObj); |
| 20581 | overhead = Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW) - start; |
| 20582 | if (retval != JIM_OK) { |
| 20583 | return retval; |
| 20584 | } |
| 20585 | n++; |
| 20586 | } while (n < count); |
| 20587 | |
| 20588 | delta -= overhead; |
| 20589 | |
| 20590 | us_per_iter = (double)delta / count; |
| 20591 | objPtr = Jim_NewListObj(interp, NULL, 0); |
| 20592 | |
| 20593 | Jim_ListAppendElement(interp, objPtr, Jim_NewStringObj(interp, "us_per_iter", -1)); |
| 20594 | Jim_ListAppendElement(interp, objPtr, Jim_NewDoubleObj(interp, us_per_iter)); |
| 20595 | Jim_ListAppendElement(interp, objPtr, Jim_NewStringObj(interp, "iters_per_sec", -1)); |
| 20596 | Jim_ListAppendElement(interp, objPtr, Jim_NewDoubleObj(interp, 1e6 / us_per_iter)); |
| 20597 | Jim_ListAppendElement(interp, objPtr, Jim_NewStringObj(interp, "count", -1)); |
| 20598 | Jim_ListAppendElement(interp, objPtr, Jim_NewIntObj(interp, count)); |
| 20599 | Jim_ListAppendElement(interp, objPtr, Jim_NewStringObj(interp, "elapsed_us", -1)); |
| 20600 | Jim_ListAppendElement(interp, objPtr, Jim_NewIntObj(interp, delta)); |
| 20601 | Jim_SetResult(interp, objPtr); |
| 20602 | return JIM_OK; |
| 20603 | } |
| 20604 | |
| 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; |
| 20624 | for (i = 0; i < len; i++) { |
| 20625 | int returncode; |
| 20626 | if (Jim_GetReturnCode(interp, Jim_ListGetIndex(interp, retcodeListObj, i), &returncode) != JIM_OK) { |
| 20627 | return JIM_ERR; |
| 20628 | } |
| 20629 | if (rc == returncode) { |
| 20630 | return JIM_OK; |
| 20631 | } |
| 20632 | } |
| 20633 | return -1; |
| 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; |
| 20644 | Jim_Obj *msgVarObj = NULL; |
| 20645 | Jim_Obj *optsVarObj = NULL; |
| 20646 | Jim_Obj *handlerScriptObj = NULL; |
| 20647 | Jim_Obj *errorCodeObj; |
| 20648 | int idx; |
| 20649 | |
| 20650 | |
| 20651 | jim_wide ignore_mask = (1 << JIM_EXIT) | (1 << JIM_EVAL) | (1 << JIM_SIGNAL); |
| 20652 | static const int max_ignore_code = sizeof(ignore_mask) * 8; |
| 20653 | |
| 20654 | JimPanic((istry != 0 && istry != 1, "wrong args to JimCatchTryHelper")); |
| 20655 | |
| 20656 | Jim_SetGlobalVariableStr(interp, "errorCode", Jim_NewStringObj(interp, "NONE", -1)); |
| 20657 | |
| 20658 | for (i = 1; i < argc - 1; i++) { |
| 20659 | const char *arg = Jim_String(argv[i]); |
| 20660 | jim_wide option; |
| 20661 | int ignore; |
| 20662 | |
| 20663 | |
| 20664 | if (strcmp(arg, "--") == 0) { |
| 20665 | i++; |
| 20666 | break; |
| 20667 | } |
| 20668 | if (*arg != '-') { |
| 20669 | break; |
| 20670 | } |
| 20671 | |
| 20672 | if (strncmp(arg, "-no", 3) == 0) { |
| 20673 | arg += 3; |
| 20674 | ignore = 1; |
| 20675 | } |
| 20676 | else { |
| 20677 | arg++; |
| 20678 | ignore = 0; |
| 20679 | } |
| 20680 | |
| 20681 | if (Jim_StringToWide(arg, &option, 10) != JIM_OK) { |
| 20682 | option = -1; |
| 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 | } |
| 20694 | else { |
| 20695 | ignore_mask &= (~((jim_wide)1 << option)); |
| 20696 | } |
| 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 | } |
| 20708 | |
| 20709 | interp->signal_level += sig; |
| 20710 | if (Jim_CheckSignal(interp)) { |
| 20711 | |
| 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 | |
| 20723 | idx++; |
| 20724 | if (istry) { |
| 20725 | while (idx < argc) { |
| 20726 | int option; |
| 20727 | int ret; |
| 20728 | static const char * const try_options[] = { "on", "trap", "finally", NULL }; |
| 20729 | enum { TRY_ON, TRY_TRAP, TRY_FINALLY, }; |
| 20730 | |
| 20731 | if (Jim_GetEnum(interp, argv[idx], try_options, &option, "handler", JIM_ERRMSG) != JIM_OK) { |
| 20732 | return JIM_ERR; |
| 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 | |
| 20769 | ret = -1; |
| 20770 | } |
| 20771 | |
| 20772 | if (ret == JIM_OK && handlerScriptObj == NULL) { |
| 20773 | msgVarObj = Jim_ListGetIndex(interp, argv[idx + 2], 0); |
| 20774 | optsVarObj = Jim_ListGetIndex(interp, argv[idx + 2], 1); |
| 20775 | handlerScriptObj = argv[idx + 3]; |
| 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 | } |
| 20787 | } |
| 20788 | } |
| 20789 | else { |
| 20790 | if (argc - idx >= 1) { |
| 20791 | msgVarObj = argv[idx]; |
| 20792 | idx++; |
| 20793 | if (argc - idx >= 1) { |
| 20794 | optsVarObj = argv[idx]; |
| 20795 | idx++; |
| 20796 | } |
| 20797 | } |
| 20798 | } |
| 20799 | |
| 20800 | |
| 20801 | if (exitCode >= 0 && exitCode < max_ignore_code && (((unsigned jim_wide)1 << exitCode) & ignore_mask)) { |
| 20802 | |
| 20803 | if (finallyScriptObj) { |
| 20804 | Jim_EvalObj(interp, finallyScriptObj); |
| 20805 | } |
| 20806 | return exitCode; |
| 20807 | } |
| 20808 | |
| 20809 | if (sig && exitCode == JIM_SIGNAL) { |
| 20810 | |
| 20811 | if (interp->signal_set_result) { |
| 20812 | interp->signal_set_result(interp, interp->sigmask); |
| 20813 | } |
| 20814 | else if (!istry) { |
| 20815 | Jim_SetResultInt(interp, interp->sigmask); |
| 20816 | } |
| 20817 | interp->sigmask = 0; |
| 20818 | } |
| 20819 | |
| 20820 | ok = 1; |
| 20821 | if (msgVarObj && Jim_Length(msgVarObj)) { |
| 20822 | if (Jim_SetVariable(interp, msgVarObj, Jim_GetResult(interp)) != JIM_OK) { |
| 20823 | ok = 0; |
| 20824 | } |
| 20825 | } |
| 20826 | if (ok && optsVarObj && Jim_Length(optsVarObj)) { |
| 20827 | Jim_Obj *optListObj = Jim_NewListObj(interp, NULL, 0); |
| 20828 | |
| 20829 | Jim_ListAppendElement(interp, optListObj, Jim_NewStringObj(interp, "-code", -1)); |
| 20830 | Jim_ListAppendElement(interp, optListObj, |
| 20831 | Jim_NewIntObj(interp, exitCode == JIM_RETURN ? interp->returnCode : exitCode)); |
| 20832 | Jim_ListAppendElement(interp, optListObj, Jim_NewStringObj(interp, "-level", -1)); |
| 20833 | Jim_ListAppendElement(interp, optListObj, Jim_NewIntObj(interp, interp->returnLevel)); |
| 20834 | if (exitCode == JIM_ERR) { |
| 20835 | Jim_ListAppendElement(interp, optListObj, Jim_NewStringObj(interp, "-errorinfo", |
| 20836 | -1)); |
| 20837 | Jim_ListAppendElement(interp, optListObj, interp->stackTrace); |
| 20838 | |
| 20839 | if (errorCodeObj) { |
| 20840 | Jim_ListAppendElement(interp, optListObj, Jim_NewStringObj(interp, "-errorcode", -1)); |
| 20841 | Jim_ListAppendElement(interp, optListObj, errorCodeObj); |
| 20842 | } |
| 20843 | } |
| 20844 | if (Jim_SetVariable(interp, optsVarObj, optListObj) != JIM_OK) { |
| 20845 | ok = 0; |
| 20846 | } |
| 20847 | } |
| 20848 | if (ok && handlerScriptObj) { |
| 20849 | |
| 20850 | exitCode = Jim_EvalObj(interp, handlerScriptObj); |
| 20851 | } |
| 20852 | |
| 20853 | if (finallyScriptObj) { |
| 20854 | |
| 20855 | Jim_Obj *prevResultObj = Jim_GetResult(interp); |
| 20856 | Jim_IncrRefCount(prevResultObj); |
| 20857 | int ret = Jim_EvalObj(interp, finallyScriptObj); |
| 20858 | if (ret == JIM_OK) { |
| 20859 | Jim_SetResult(interp, prevResultObj); |
| 20860 | } |
| 20861 | else { |
| 20862 | exitCode = ret; |
| 20863 | } |
| 20864 | Jim_DecrRefCount(interp, prevResultObj); |
| 20865 | } |
| 20866 | if (!istry) { |
| 20867 | Jim_SetResultInt(interp, exitCode); |
| 20868 | exitCode = JIM_OK; |
| 20869 | } |
| 20870 | return exitCode; |
| 20871 | } |
| 20872 | |
| 20873 | |
| 20874 | static int Jim_CatchCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 20875 | { |
| 20876 | return JimCatchTryHelper(interp, 0, argc, argv); |
| 20877 | } |
| 20878 | |
| 20879 | |
| 20880 | static int Jim_TryCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 20881 | { |
| 20882 | return JimCatchTryHelper(interp, 1, argc, argv); |
| 20883 | } |
| 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 |
| 20894 | |
| 20895 | int Jim_DictMatchTypes(Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *patternObj, int match_type, int return_types) |
| 20896 | { |
| 20897 | Jim_Obj *listObjPtr; |
| 20898 | Jim_Dict *dict; |
| 20899 | int i; |
| 20900 | |
| 20901 | if (SetDictFromAny(interp, objPtr) != JIM_OK) { |
| 20902 | return JIM_ERR; |
| 20903 | } |
| 20904 | dict = objPtr->internalRep.dictValue; |
| 20905 | |
| 20906 | listObjPtr = Jim_NewListObj(interp, NULL, 0); |
| 20907 | |
| 20908 | for (i = 0; i < dict->len; i += 2 ) { |
| 20909 | Jim_Obj *keyObj = dict->table[i]; |
| 20910 | Jim_Obj *valObj = dict->table[i + 1]; |
| 20911 | if (patternObj) { |
| 20912 | Jim_Obj *matchObj = (match_type == JIM_DICTMATCH_KEYS) ? keyObj : valObj; |
| 20913 | if (!Jim_StringMatchObj(interp, patternObj, matchObj, 0)) { |
| 20914 | |
| 20915 | continue; |
| 20916 | } |
| 20917 | } |
| 20918 | if (return_types & JIM_DICTMATCH_KEYS) { |
| 20919 | Jim_ListAppendElement(interp, listObjPtr, keyObj); |
| 20920 | } |
| 20921 | if (return_types & JIM_DICTMATCH_VALUES) { |
| 20922 | Jim_ListAppendElement(interp, listObjPtr, valObj); |
| 20923 | } |
| 20924 | } |
| 20925 | |
| 20926 | Jim_SetResult(interp, listObjPtr); |
| 20927 | return JIM_OK; |
| 20928 | } |
| 20929 | |
| 20930 | int Jim_DictSize(Jim_Interp *interp, Jim_Obj *objPtr) |
| 20931 | { |
| 20932 | if (SetDictFromAny(interp, objPtr) != JIM_OK) { |
| 20933 | return -1; |
| 20934 | } |
| 20935 | return objPtr->internalRep.dictValue->len / 2; |
| 20936 | } |
| 20937 | |
| 20938 | Jim_Obj *Jim_DictMerge(Jim_Interp *interp, int objc, Jim_Obj *const *objv) |
| 20939 | { |
| 20940 | Jim_Obj *objPtr = Jim_NewDictObj(interp, NULL, 0); |
| 20941 | int i; |
| 20942 | |
| 20943 | JimPanic((objc == 0, "Jim_DictMerge called with objc=0")); |
| 20944 | |
| 20945 | |
| 20946 | |
| 20947 | for (i = 0; i < objc; i++) { |
| 20948 | Jim_Obj **table; |
| 20949 | int tablelen; |
| 20950 | int j; |
| 20951 | |
| 20952 | table = Jim_DictPairs(interp, objv[i], &tablelen); |
| 20953 | if (tablelen && !table) { |
| 20954 | Jim_FreeNewObj(interp, objPtr); |
| 20955 | return NULL; |
| 20956 | } |
| 20957 | for (j = 0; j < tablelen; j += 2) { |
| 20958 | DictAddElement(interp, objPtr, table[j], table[j + 1]); |
| 20959 | } |
| 20960 | } |
| 20961 | return objPtr; |
| 20962 | } |
| 20963 | |
| 20964 | int Jim_DictInfo(Jim_Interp *interp, Jim_Obj *objPtr) |
| 20965 | { |
| 20966 | char buffer[100]; |
| 20967 | Jim_Obj *output; |
| 20968 | Jim_Dict *dict; |
| 20969 | |
| 20970 | if (SetDictFromAny(interp, objPtr) != JIM_OK) { |
| 20971 | return JIM_ERR; |
| 20972 | } |
| 20973 | |
| 20974 | dict = objPtr->internalRep.dictValue; |
| 20975 | |
| 20976 | |
| 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 | |
| 20988 | Jim_AppendString(interp, prefixObj, " ", 1); |
| 20989 | Jim_AppendString(interp, prefixObj, subcmd, -1); |
| 20990 | |
| 20991 | return Jim_EvalObjPrefix(interp, prefixObj, argc, argv); |
| 20992 | } |
| 20993 | |
| 20994 | static int JimDictWith(Jim_Interp *interp, Jim_Obj *dictVarName, Jim_Obj *const *keyv, int keyc, Jim_Obj *scriptObj) |
| 20995 | { |
| 20996 | int i; |
| 20997 | Jim_Obj *objPtr; |
| 20998 | Jim_Obj *dictObj; |
| 20999 | Jim_Obj **dictValues; |
| 21000 | int len; |
| 21001 | int ret = JIM_OK; |
| 21002 | |
| 21003 | |
| 21004 | dictObj = Jim_GetVariable(interp, dictVarName, JIM_ERRMSG); |
| 21005 | if (dictObj == NULL || Jim_DictKeysVector(interp, dictObj, keyv, keyc, &objPtr, JIM_ERRMSG) != JIM_OK) { |
| 21006 | return JIM_ERR; |
| 21007 | } |
| 21008 | |
| 21009 | dictValues = Jim_DictPairs(interp, objPtr, &len); |
| 21010 | if (len && dictValues == NULL) { |
| 21011 | return JIM_ERR; |
| 21012 | } |
| 21013 | for (i = 0; i < len; i += 2) { |
| 21014 | if (Jim_SetVariable(interp, dictValues[i], dictValues[i + 1]) == JIM_ERR) { |
| 21015 | return JIM_ERR; |
| 21016 | } |
| 21017 | } |
| 21018 | |
| 21019 | |
| 21020 | if (Jim_Length(scriptObj)) { |
| 21021 | ret = Jim_EvalObj(interp, scriptObj); |
| 21022 | |
| 21023 | |
| 21024 | if (ret == JIM_OK && Jim_GetVariable(interp, dictVarName, 0) != NULL) { |
| 21025 | |
| 21026 | Jim_Obj **newkeyv = Jim_Alloc(sizeof(*newkeyv) * (keyc + 1)); |
| 21027 | for (i = 0; i < keyc; i++) { |
| 21028 | newkeyv[i] = keyv[i]; |
| 21029 | } |
| 21030 | |
| 21031 | for (i = 0; i < len; i += 2) { |
| 21032 | |
| 21033 | if (Jim_StringCompareObj(interp, dictVarName, dictValues[i], 0) != 0) { |
| 21034 | |
| 21035 | objPtr = Jim_GetVariable(interp, dictValues[i], 0); |
| 21036 | newkeyv[keyc] = dictValues[i]; |
| 21037 | Jim_SetDictKeysVector(interp, dictVarName, newkeyv, keyc + 1, objPtr, JIM_NORESULT); |
| 21038 | } |
| 21039 | } |
| 21040 | Jim_Free(newkeyv); |
| 21041 | } |
| 21042 | } |
| 21043 | |
| 21044 | return ret; |
| 21045 | } |
| 21046 | |
| 21047 | |
| 21048 | static int Jim_DictCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 21049 | { |
| 21050 | Jim_Obj *objPtr; |
| 21051 | int types = JIM_DICTMATCH_KEYS; |
| 21052 | |
| 21053 | enum { |
| 21054 | OPT_CREATE, |
| 21055 | OPT_GET, |
| 21056 | OPT_GETDEF, |
| 21057 | OPT_GETWITHDEFAULT, |
| 21058 | OPT_SET, |
| 21059 | OPT_UNSET, |
| 21060 | OPT_EXISTS, |
| 21061 | OPT_KEYS, |
| 21062 | OPT_SIZE, |
| 21063 | OPT_INFO, |
| 21064 | OPT_MERGE, |
| 21065 | OPT_WITH, |
| 21066 | OPT_APPEND, |
| 21067 | OPT_LAPPEND, |
| 21068 | OPT_INCR, |
| 21069 | OPT_REMOVE, |
| 21070 | OPT_VALUES, |
| 21071 | OPT_FOR, |
| 21072 | OPT_REPLACE, |
| 21073 | OPT_UPDATE, |
| 21074 | OPT_COUNT |
| 21075 | }; |
| 21076 | static const jim_subcmd_type cmds[OPT_COUNT + 1] = { |
| 21077 | JIM_DEF_SUBCMD("create", "?key value ...?", 0, -2), |
| 21078 | JIM_DEF_SUBCMD("get", "dictionary ?key ...?", 1, -1), |
| 21079 | JIM_DEF_SUBCMD_HIDDEN("getdef", "dictionary ?key ...? key default", 3, -1), |
| 21080 | JIM_DEF_SUBCMD("getwithdefault", "dictionary ?key ...? key default", 3, -1), |
| 21081 | JIM_DEF_SUBCMD("set", "varName key ?key ...? value", 3, -1), |
| 21082 | JIM_DEF_SUBCMD("unset", "varName key ?key ...?", 2, -1), |
| 21083 | JIM_DEF_SUBCMD("exists", "dictionary key ?key ...?", 2, -1), |
| 21084 | JIM_DEF_SUBCMD("keys", "dictionary ?pattern?", 1, 2), |
| 21085 | JIM_DEF_SUBCMD("size", "dictionary", 1, 1), |
| 21086 | JIM_DEF_SUBCMD("info", "dictionary", 1, 1), |
| 21087 | JIM_DEF_SUBCMD("merge", "?...?", 0, -1), |
| 21088 | JIM_DEF_SUBCMD("with", "dictVar ?key ...? script", 2, -1), |
| 21089 | JIM_DEF_SUBCMD("append", "varName key ?value ...?", 2, -1), |
| 21090 | JIM_DEF_SUBCMD("lappend", "varName key ?value ...?", 2, -1), |
| 21091 | JIM_DEF_SUBCMD("incr", "varName key ?increment?", 2, 3), |
| 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 | } |
| 21103 | if (ct->function) { |
| 21104 | |
| 21105 | return ct->function(interp, argc, argv); |
| 21106 | } |
| 21107 | |
| 21108 | |
| 21109 | switch (ct - cmds) { |
| 21110 | case OPT_GET: |
| 21111 | if (Jim_DictKeysVector(interp, argv[2], argv + 3, argc - 3, &objPtr, |
| 21112 | JIM_ERRMSG) != JIM_OK) { |
| 21113 | return JIM_ERR; |
| 21114 | } |
| 21115 | Jim_SetResult(interp, objPtr); |
| 21116 | return JIM_OK; |
| 21117 | |
| 21118 | case OPT_GETDEF: |
| 21119 | case OPT_GETWITHDEFAULT:{ |
| 21120 | int rc = Jim_DictKeysVector(interp, argv[2], argv + 3, argc - 4, &objPtr, JIM_ERRMSG); |
| 21121 | if (rc == -1) { |
| 21122 | |
| 21123 | return JIM_ERR; |
| 21124 | } |
| 21125 | if (rc == JIM_ERR) { |
| 21126 | Jim_SetResult(interp, argv[argc - 1]); |
| 21127 | } |
| 21128 | else { |
| 21129 | Jim_SetResult(interp, objPtr); |
| 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; |
| 21141 | } |
| 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: |
| 21153 | types = JIM_DICTMATCH_VALUES; |
| 21154 | |
| 21155 | case OPT_KEYS: |
| 21156 | return Jim_DictMatchTypes(interp, argv[2], argc == 4 ? argv[3] : NULL, types, types); |
| 21157 | |
| 21158 | case OPT_SIZE: |
| 21159 | if (Jim_DictSize(interp, argv[2]) < 0) { |
| 21160 | return JIM_ERR; |
| 21161 | } |
| 21162 | Jim_SetResultInt(interp, Jim_DictSize(interp, argv[2])); |
| 21163 | return JIM_OK; |
| 21164 | |
| 21165 | case OPT_MERGE: |
| 21166 | if (argc == 2) { |
| 21167 | return JIM_OK; |
| 21168 | } |
| 21169 | objPtr = Jim_DictMerge(interp, argc - 2, argv + 2); |
| 21170 | if (objPtr == NULL) { |
| 21171 | return JIM_ERR; |
| 21172 | } |
| 21173 | Jim_SetResult(interp, objPtr); |
| 21174 | return JIM_OK; |
| 21175 | |
| 21176 | case OPT_CREATE: |
| 21177 | objPtr = Jim_NewDictObj(interp, argv + 2, argc - 2); |
| 21178 | Jim_SetResult(interp, objPtr); |
| 21179 | return JIM_OK; |
| 21180 | |
| 21181 | case OPT_INFO: |
| 21182 | return Jim_DictInfo(interp, argv[2]); |
| 21183 | |
| 21184 | case OPT_WITH: |
| 21185 | return JimDictWith(interp, argv[2], argv + 3, argc - 4, argv[argc - 1]); |
| 21186 | |
| 21187 | case OPT_UPDATE: |
| 21188 | if (argc < 6 || argc % 2) { |
| 21189 | |
| 21190 | argc = 2; |
| 21191 | } |
| 21192 | |
| 21193 | default: |
| 21194 | return Jim_EvalEnsemble(interp, "dict", Jim_String(argv[1]), argc - 2, argv + 2); |
| 21195 | } |
| 21196 | } |
| 21197 | |
| 21198 | |
| 21199 | static int Jim_SubstCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 21200 | { |
| 21201 | static const char * const options[] = { |
| 21202 | "-nobackslashes", "-nocommands", "-novariables", NULL |
| 21203 | }; |
| 21204 | enum |
| 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) { |
| 21215 | return JIM_ERR; |
| 21216 | } |
| 21217 | switch (option) { |
| 21218 | case OPT_NOBACKSLASHES: |
| 21219 | flags |= JIM_SUBST_NOESC; |
| 21220 | break; |
| 21221 | case OPT_NOCOMMANDS: |
| 21222 | flags |= JIM_SUBST_NOCMD; |
| 21223 | break; |
| 21224 | case OPT_NOVARIABLES: |
| 21225 | flags |= JIM_SUBST_NOVAR; |
| 21226 | break; |
| 21227 | } |
| 21228 | } |
| 21229 | if (Jim_SubstObj(interp, argv[argc - 1], &objPtr, flags) != JIM_OK) { |
| 21230 | return JIM_ERR; |
| 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] == ':'; |
| 21255 | } |
| 21256 | #endif |
| 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, |
| 21282 | INFO_PROCS, |
| 21283 | INFO_REFERENCES, |
| 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 | |
| 21329 | if (argc > 2 && Jim_CompareStringImmediate(interp, argv[1], "-nons")) { |
| 21330 | |
| 21331 | argc--; |
| 21332 | argv++; |
| 21333 | nons = 1; |
| 21334 | } |
| 21335 | #endif |
| 21336 | ct = Jim_ParseSubCmd(interp, cmds, argc, argv); |
| 21337 | if (!ct) { |
| 21338 | return JIM_ERR; |
| 21339 | } |
| 21340 | if (ct->function) { |
| 21341 | |
| 21342 | return ct->function(interp, argc, argv); |
| 21343 | } |
| 21344 | |
| 21345 | int option = ct - cmds; |
| 21346 | |
| 21347 | switch (option) { |
| 21348 | case INFO_EXISTS: |
| 21349 | Jim_SetResultBool(interp, Jim_GetVariable(interp, argv[2], 0) != NULL); |
| 21350 | return JIM_OK; |
| 21351 | |
| 21352 | case INFO_ALIAS:{ |
| 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) { |
| 21419 | if (Jim_Length(interp->framePtr->nsObj) || (argc == 3 && JimIsGlobalNamespace(argv[2]))) { |
| 21420 | return Jim_EvalPrefix(interp, "namespace info", argc - 1, argv + 1); |
| 21421 | } |
| 21422 | } |
| 21423 | #endif |
| 21424 | Jim_SetResult(interp, JimVariablesList(interp, argc == 3 ? argv[2] : NULL, mode)); |
| 21425 | return JIM_OK; |
| 21426 | |
| 21427 | case INFO_SCRIPT: |
| 21428 | if (argc == 3) { |
| 21429 | Jim_IncrRefCount(argv[2]); |
| 21430 | Jim_DecrRefCount(interp, interp->currentFilenameObj); |
| 21431 | interp->currentFilenameObj = argv[2]; |
| 21432 | } |
| 21433 | Jim_SetResult(interp, interp->currentFilenameObj); |
| 21434 | return JIM_OK; |
| 21435 | |
| 21436 | case INFO_SOURCE:{ |
| 21437 | Jim_Obj *resObjPtr; |
| 21438 | Jim_Obj *fileNameObj; |
| 21439 | |
| 21440 | if (argc == 4) { |
| 21441 | Jim_SubCmdArgError(interp, ct, argv[0]); |
| 21442 | return JIM_ERR; |
| 21443 | } |
| 21444 | if (argc == 5) { |
| 21445 | jim_wide line; |
| 21446 | if (Jim_GetWide(interp, argv[4], &line) != JIM_OK) { |
| 21447 | return JIM_ERR; |
| 21448 | } |
| 21449 | resObjPtr = Jim_NewStringObj(interp, Jim_String(argv[2]), Jim_Length(argv[2])); |
| 21450 | Jim_SetSourceInfo(interp, resObjPtr, argv[3], line); |
| 21451 | } |
| 21452 | else { |
| 21453 | int line; |
| 21454 | fileNameObj = Jim_GetSourceInfo(interp, argv[2], &line); |
| 21455 | resObjPtr = Jim_NewListObj(interp, NULL, 0); |
| 21456 | Jim_ListAppendElement(interp, resObjPtr, fileNameObj); |
| 21457 | Jim_ListAppendElement(interp, resObjPtr, Jim_NewIntObj(interp, line)); |
| 21458 | } |
| 21459 | Jim_SetResult(interp, resObjPtr); |
| 21460 | return JIM_OK; |
| 21461 | } |
| 21462 | |
| 21463 | case INFO_STACKTRACE: |
| 21464 | Jim_SetResult(interp, interp->stackTrace); |
| 21465 | return JIM_OK; |
| 21466 | |
| 21467 | case INFO_LEVEL: |
| 21468 | if (argc == 2) { |
| 21469 | Jim_SetResultInt(interp, interp->framePtr->level); |
| 21470 | } |
| 21471 | else { |
| 21472 | if (JimInfoLevel(interp, argv[2], &objPtr) != JIM_OK) { |
| 21473 | return JIM_ERR; |
| 21474 | } |
| 21475 | Jim_SetResult(interp, objPtr); |
| 21476 | } |
| 21477 | return JIM_OK; |
| 21478 | |
| 21479 | case INFO_FRAME: |
| 21480 | if (argc == 2) { |
| 21481 | Jim_SetResultInt(interp, interp->procLevel + 1); |
| 21482 | } |
| 21483 | else { |
| 21484 | if (JimInfoFrame(interp, argv[2], &objPtr) != JIM_OK) { |
| 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 |
| 21526 | default: |
| 21527 | Jim_SetResultString(interp, "unsupported", -1); |
| 21528 | return JIM_ERR; |
| 21529 | #else |
| 21530 | case INFO_BODY: |
| 21531 | Jim_SetResult(interp, cmdPtr->u.proc.bodyObjPtr); |
| 21532 | break; |
| 21533 | case INFO_ARGS: |
| 21534 | Jim_SetResult(interp, cmdPtr->u.proc.argListObjPtr); |
| 21535 | break; |
| 21536 | #endif |
| 21537 | case INFO_STATICS: |
| 21538 | if (cmdPtr->u.proc.staticVars) { |
| 21539 | Jim_SetResult(interp, JimHashtablePatternMatch(interp, cmdPtr->u.proc.staticVars, |
| 21540 | NULL, JimVariablesMatch, JIM_VARLIST_LOCALS | JIM_VARLIST_VALUES)); |
| 21541 | } |
| 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; |
| 21563 | |
| 21564 | Jim_SetResultBool(interp, Jim_ScriptIsComplete(interp, argv[2], &missing)); |
| 21565 | if (missing != ' ' && argc == 4) { |
| 21566 | Jim_SetVariable(interp, argv[3], Jim_NewStringObj(interp, &missing, 1)); |
| 21567 | } |
| 21568 | return JIM_OK; |
| 21569 | } |
| 21570 | |
| 21571 | case INFO_HOSTNAME: |
| 21572 | |
| 21573 | return Jim_Eval(interp, "os.gethostname"); |
| 21574 | |
| 21575 | case INFO_NAMEOFEXECUTABLE: |
| 21576 | |
| 21577 | return Jim_Eval(interp, "{info nameofexecutable}"); |
| 21578 | |
| 21579 | case INFO_RETURNCODES: |
| 21580 | if (argc == 2) { |
| 21581 | int i; |
| 21582 | Jim_Obj *listObjPtr = Jim_NewListObj(interp, NULL, 0); |
| 21583 | |
| 21584 | for (i = 0; jimReturnCodes[i]; i++) { |
| 21585 | Jim_ListAppendElement(interp, listObjPtr, Jim_NewIntObj(interp, i)); |
| 21586 | Jim_ListAppendElement(interp, listObjPtr, Jim_NewStringObj(interp, |
| 21587 | jimReturnCodes[i], -1)); |
| 21588 | } |
| 21589 | |
| 21590 | Jim_SetResult(interp, listObjPtr); |
| 21591 | } |
| 21592 | else if (argc == 3) { |
| 21593 | long code; |
| 21594 | const char *name; |
| 21595 | |
| 21596 | if (Jim_GetLong(interp, argv[2], &code) != JIM_OK) { |
| 21597 | return JIM_ERR; |
| 21598 | } |
| 21599 | name = Jim_ReturnCode(code); |
| 21600 | if (*name == '?') { |
| 21601 | Jim_SetResultInt(interp, code); |
| 21602 | } |
| 21603 | else { |
| 21604 | Jim_SetResultString(interp, name, -1); |
| 21605 | } |
| 21606 | } |
| 21607 | return JIM_OK; |
| 21608 | case INFO_REFERENCES: |
| 21609 | #ifdef JIM_REFERENCES |
| 21610 | return JimInfoReferences(interp, argc, argv); |
| 21611 | #else |
| 21612 | Jim_SetResultString(interp, "not supported", -1); |
| 21613 | return JIM_ERR; |
| 21614 | #endif |
| 21615 | default: |
| 21616 | abort(); |
| 21617 | } |
| 21618 | } |
| 21619 | |
| 21620 | |
| 21621 | static int Jim_ExistsCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 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; |
| 21637 | objPtr = argv[1]; |
| 21638 | } |
| 21639 | else if (argc == 3) { |
| 21640 | if (Jim_GetEnum(interp, argv[1], options, &option, NULL, JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) { |
| 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 | } |
| 21652 | else { |
| 21653 | |
| 21654 | Jim_Cmd *cmd = Jim_GetCommand(interp, objPtr, JIM_NONE); |
| 21655 | |
| 21656 | if (cmd) { |
| 21657 | switch (option) { |
| 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); |
| 21677 | return JIM_OK; |
| 21678 | } |
| 21679 | |
| 21680 | |
| 21681 | static int Jim_SplitCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 21682 | { |
| 21683 | const char *str, *splitChars, *noMatchStart; |
| 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]); |
| 21694 | |
| 21695 | |
| 21696 | if (argc == 2) { |
| 21697 | splitChars = " \n\t\r"; |
| 21698 | splitLen = 4; |
| 21699 | } |
| 21700 | else { |
| 21701 | splitChars = Jim_String(argv[2]); |
| 21702 | splitLen = Jim_Utf8Length(interp, argv[2]); |
| 21703 | } |
| 21704 | |
| 21705 | noMatchStart = str; |
| 21706 | resObjPtr = Jim_NewListObj(interp, NULL, 0); |
| 21707 | |
| 21708 | |
| 21709 | if (splitLen) { |
| 21710 | Jim_Obj *objPtr; |
| 21711 | while (strLen--) { |
| 21712 | const char *sc = splitChars; |
| 21713 | int scLen = splitLen; |
| 21714 | int sl = utf8_tounicode(str, &c); |
| 21715 | while (scLen--) { |
| 21716 | int pc; |
| 21717 | sc += utf8_tounicode(sc, &pc); |
| 21718 | if (c == pc) { |
| 21719 | objPtr = Jim_NewStringObj(interp, noMatchStart, (str - noMatchStart)); |
| 21720 | Jim_ListAppendElement(interp, resObjPtr, objPtr); |
| 21721 | noMatchStart = str + sl; |
| 21722 | break; |
| 21723 | } |
| 21724 | } |
| 21725 | str += sl; |
| 21726 | } |
| 21727 | objPtr = Jim_NewStringObj(interp, noMatchStart, (str - noMatchStart)); |
| 21728 | Jim_ListAppendElement(interp, resObjPtr, objPtr); |
| 21729 | } |
| 21730 | else { |
| 21731 | Jim_Obj **commonObj = NULL; |
| 21732 | #define NUM_COMMON (128 - 9) |
| 21733 | while (strLen--) { |
| 21734 | int n = utf8_tounicode(str, &c); |
| 21735 | #ifdef JIM_OPTIMIZATION |
| 21736 | if (c >= 9 && c < 128) { |
| 21737 | |
| 21738 | c -= 9; |
| 21739 | if (!commonObj) { |
| 21740 | commonObj = Jim_Alloc(sizeof(*commonObj) * NUM_COMMON); |
| 21741 | memset(commonObj, 0, sizeof(*commonObj) * NUM_COMMON); |
| 21742 | } |
| 21743 | if (!commonObj[c]) { |
| 21744 | commonObj[c] = Jim_NewStringObj(interp, str, 1); |
| 21745 | } |
| 21746 | Jim_ListAppendElement(interp, resObjPtr, commonObj[c]); |
| 21747 | str++; |
| 21748 | continue; |
| 21749 | } |
| 21750 | #endif |
| 21751 | Jim_ListAppendElement(interp, resObjPtr, Jim_NewStringObjUtf8(interp, str, 1)); |
| 21752 | str += n; |
| 21753 | } |
| 21754 | Jim_Free(commonObj); |
| 21755 | } |
| 21756 | |
| 21757 | Jim_SetResult(interp, resObjPtr); |
| 21758 | return JIM_OK; |
| 21759 | } |
| 21760 | |
| 21761 | |
| 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 | } |
| 21772 | else { |
| 21773 | joinStr = Jim_GetString(argv[2], &joinStrLen); |
| 21774 | } |
| 21775 | Jim_SetResult(interp, Jim_ListJoin(interp, argv[1], joinStr, joinStrLen)); |
| 21776 | return JIM_OK; |
| 21777 | } |
| 21778 | |
| 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; |
| 21789 | } |
| 21790 | |
| 21791 | |
| 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; |
| 21802 | } |
| 21803 | if (argc > 3) { |
| 21804 | int maxPos = FormatGetMaxPos(argv[2]); |
| 21805 | int count = FormatGetCnvCount(argv[2]); |
| 21806 | |
| 21807 | if (maxPos > argc - 3) { |
| 21808 | Jim_SetResultString(interp, "\"%n$\" argument index out of range", -1); |
| 21809 | return JIM_ERR; |
| 21810 | } |
| 21811 | else if (count > argc - 3) { |
| 21812 | Jim_SetResultString(interp, "different numbers of variable names and " |
| 21813 | "field specifiers", -1); |
| 21814 | return JIM_ERR; |
| 21815 | } |
| 21816 | else if (count < argc - 3) { |
| 21817 | Jim_SetResultString(interp, "variable is not assigned by any " |
| 21818 | "conversion specifiers", -1); |
| 21819 | return JIM_ERR; |
| 21820 | } |
| 21821 | } |
| 21822 | listPtr = Jim_ScanString(interp, argv[1], argv[2], JIM_ERRMSG); |
| 21823 | if (listPtr == 0) |
| 21824 | return JIM_ERR; |
| 21825 | if (argc > 3) { |
| 21826 | int rc = JIM_OK; |
| 21827 | int count = 0; |
| 21828 | |
| 21829 | if (listPtr != 0 && listPtr != (Jim_Obj *)EOF) { |
| 21830 | int len = Jim_ListLength(interp, listPtr); |
| 21831 | |
| 21832 | if (len != 0) { |
| 21833 | JimListGetElements(interp, listPtr, &outc, &outVec); |
| 21834 | for (i = 0; i < outc; ++i) { |
| 21835 | if (Jim_Length(outVec[i]) > 0) { |
| 21836 | ++count; |
| 21837 | if (Jim_SetVariable(interp, argv[3 + i], outVec[i]) != JIM_OK) { |
| 21838 | rc = JIM_ERR; |
| 21839 | } |
| 21840 | } |
| 21841 | } |
| 21842 | } |
| 21843 | Jim_FreeNewObj(interp, listPtr); |
| 21844 | } |
| 21845 | else { |
| 21846 | count = -1; |
| 21847 | } |
| 21848 | if (rc == JIM_OK) { |
| 21849 | Jim_SetResultInt(interp, count); |
| 21850 | } |
| 21851 | return rc; |
| 21852 | } |
| 21853 | else { |
| 21854 | if (listPtr == (Jim_Obj *)EOF) { |
| 21855 | Jim_SetResult(interp, Jim_NewListObj(interp, 0, 0)); |
| 21856 | return JIM_OK; |
| 21857 | } |
| 21858 | Jim_SetResult(interp, listPtr); |
| 21859 | } |
| 21860 | return JIM_OK; |
| 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 | } |
| 21871 | return JIM_ERR; |
| 21872 | } |
| 21873 | |
| 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 | } |
| 21884 | |
| 21885 | |
| 21886 | static int Jim_LrepeatCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 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 | } |
| 21898 | |
| 21899 | argc -= 2; |
| 21900 | argv += 2; |
| 21901 | |
| 21902 | objPtr = Jim_NewListObj(interp, NULL, 0); |
| 21903 | ListEnsureLength(objPtr, argc * count); |
| 21904 | while (count--) { |
| 21905 | ListInsertElements(objPtr, -1, argc, argv); |
| 21906 | } |
| 21907 | |
| 21908 | Jim_SetResult(interp, objPtr); |
| 21909 | return JIM_OK; |
| 21910 | } |
| 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 | |
| 21937 | environ = env; |
| 21938 | #endif |
| 21939 | } |
| 21940 | |
| 21941 | |
| 21942 | static int Jim_EnvCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 21943 | { |
| 21944 | const char *key; |
| 21945 | const char *val; |
| 21946 | |
| 21947 | if (argc == 1) { |
| 21948 | char **e = Jim_GetEnviron(); |
| 21949 | |
| 21950 | int i; |
| 21951 | Jim_Obj *listObjPtr = Jim_NewListObj(interp, NULL, 0); |
| 21952 | |
| 21953 | for (i = 0; e[i]; i++) { |
| 21954 | const char *equals = strchr(e[i], '='); |
| 21955 | |
| 21956 | if (equals) { |
| 21957 | Jim_ListAppendElement(interp, listObjPtr, Jim_NewStringObj(interp, e[i], |
| 21958 | equals - e[i])); |
| 21959 | Jim_ListAppendElement(interp, listObjPtr, Jim_NewStringObj(interp, equals + 1, -1)); |
| 21960 | } |
| 21961 | } |
| 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]); |
| 21972 | return JIM_ERR; |
| 21973 | } |
| 21974 | val = Jim_String(argv[2]); |
| 21975 | } |
| 21976 | Jim_SetResult(interp, Jim_NewStringObj(interp, val, -1)); |
| 21977 | return JIM_OK; |
| 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 | |
| 22009 | if (step == 0) |
| 22010 | return -1; |
| 22011 | if (start == end) |
| 22012 | return 0; |
| 22013 | else if (step > 0 && start > end) |
| 22014 | return -1; |
| 22015 | else if (step < 0 && end > start) |
| 22016 | return -1; |
| 22017 | len = end - start; |
| 22018 | if (len < 0) |
| 22019 | len = -len; |
| 22020 | if (step < 0) |
| 22021 | step = -step; |
| 22022 | len = 1 + ((len - 1) / step); |
| 22023 | if (len > INT_MAX) |
| 22024 | len = INT_MAX; |
| 22025 | return (int)((len < 0) ? -1 : len); |
| 22026 | } |
| 22027 | |
| 22028 | |
| 22029 | static int Jim_RangeCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 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 { |
| 22040 | if (Jim_GetWideExpr(interp, argv[1], &start) != JIM_OK || |
| 22041 | Jim_GetWideExpr(interp, argv[2], &end) != JIM_OK) |
| 22042 | return JIM_ERR; |
| 22043 | if (argc == 4 && Jim_GetWideExpr(interp, argv[3], &step) != JIM_OK) |
| 22044 | return JIM_ERR; |
| 22045 | } |
| 22046 | if ((len = JimRangeLen(start, end, step)) == -1) { |
| 22047 | Jim_SetResultString(interp, "Invalid (infinite?) range specified", -1); |
| 22048 | return JIM_ERR; |
| 22049 | } |
| 22050 | objPtr = Jim_NewListObj(interp, NULL, 0); |
| 22051 | ListEnsureLength(objPtr, len); |
| 22052 | for (i = 0; i < len; i++) |
| 22053 | ListAppendElement(objPtr, Jim_NewIntObj(interp, start + i * step)); |
| 22054 | Jim_SetResult(interp, objPtr); |
| 22055 | return JIM_OK; |
| 22056 | } |
| 22057 | |
| 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; |
| 22068 | } else if (argc == 3) { |
| 22069 | if (Jim_GetWideExpr(interp, argv[1], &min) != JIM_OK || |
| 22070 | Jim_GetWideExpr(interp, argv[2], &max) != JIM_OK) |
| 22071 | return JIM_ERR; |
| 22072 | } |
| 22073 | len = max-min; |
| 22074 | if (len < 0) { |
| 22075 | Jim_SetResultString(interp, "Invalid arguments (max < min)", -1); |
| 22076 | return JIM_ERR; |
| 22077 | } |
| 22078 | maxMul = JIM_WIDE_MAX - (len ? (JIM_WIDE_MAX%len) : 0); |
| 22079 | while (1) { |
| 22080 | jim_wide r; |
| 22081 | |
| 22082 | JimRandomBytes(interp, &r, sizeof(jim_wide)); |
| 22083 | if (r < 0 || r >= maxMul) continue; |
| 22084 | r = (len == 0) ? 0 : r%len; |
| 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 | { |
| 22196 | Jim_Obj *argv[2]; |
| 22197 | |
| 22198 | argv[0] = Jim_NewStringObj(interp, "errorInfo", -1); |
| 22199 | argv[1] = interp->result; |
| 22200 | |
| 22201 | Jim_EvalObjVector(interp, 2, argv); |
| 22202 | } |
| 22203 | |
| 22204 | static char **JimSortStringTable(const char *const *tablePtr) |
| 22205 | { |
| 22206 | int count; |
| 22207 | char **tablePtrSorted; |
| 22208 | |
| 22209 | |
| 22210 | for (count = 0; tablePtr[count]; count++) { |
| 22211 | } |
| 22212 | |
| 22213 | |
| 22214 | tablePtrSorted = Jim_Alloc(sizeof(char *) * (count + 1)); |
| 22215 | memcpy(tablePtrSorted, tablePtr, sizeof(char *) * count); |
| 22216 | qsort(tablePtrSorted, count, sizeof(char *), qsortCompareStringPointers); |
| 22217 | tablePtrSorted[count] = NULL; |
| 22218 | |
| 22219 | return tablePtrSorted; |
| 22220 | } |
| 22221 | |
| 22222 | static void JimSetFailedEnumResult(Jim_Interp *interp, const char *arg, const char *badtype, |
| 22223 | const char *prefix, const char *const *tablePtr, const char *name) |
| 22224 | { |
| 22225 | char **tablePtrSorted; |
| 22226 | int i; |
| 22227 | |
| 22228 | if (name == NULL) { |
| 22229 | name = "option"; |
| 22230 | } |
| 22231 | |
| 22232 | Jim_SetResultFormatted(interp, "%s%s \"%s\": must be ", badtype, name, arg); |
| 22233 | tablePtrSorted = JimSortStringTable(tablePtr); |
| 22234 | for (i = 0; tablePtrSorted[i]; i++) { |
| 22235 | if (tablePtrSorted[i + 1] == NULL && i > 0) { |
| 22236 | Jim_AppendString(interp, Jim_GetResult(interp), "or ", -1); |
| 22237 | } |
| 22238 | Jim_AppendStrings(interp, Jim_GetResult(interp), prefix, tablePtrSorted[i], NULL); |
| 22239 | if (tablePtrSorted[i + 1]) { |
| 22240 | Jim_AppendString(interp, Jim_GetResult(interp), ", ", -1); |
| 22241 | } |
| 22242 | } |
| 22243 | Jim_Free(tablePtrSorted); |
| 22244 | } |
| 22245 | |
| 22246 | |
| 22247 | int Jim_CheckShowCommands(Jim_Interp *interp, Jim_Obj *objPtr, const char *const *tablePtr) |
| 22248 | { |
| 22249 | if (Jim_CompareStringImmediate(interp, objPtr, "-commands")) { |
| 22250 | int i; |
| 22251 | char **tablePtrSorted = JimSortStringTable(tablePtr); |
| 22252 | Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0)); |
| 22253 | for (i = 0; tablePtrSorted[i]; i++) { |
| 22254 | Jim_ListAppendElement(interp, Jim_GetResult(interp), Jim_NewStringObj(interp, tablePtrSorted[i], -1)); |
| 22255 | } |
| 22256 | Jim_Free(tablePtrSorted); |
| 22257 | return JIM_OK; |
| 22258 | } |
| 22259 | return JIM_ERR; |
| 22260 | } |
| 22261 | |
| 22262 | static const Jim_ObjType getEnumObjType = { |
| 22263 | "get-enum", |
| 22264 | NULL, |
| 22265 | NULL, |
| 22266 | NULL, |
| 22267 | JIM_TYPE_REFERENCES |
| 22268 | }; |
| 22269 | |
| 22270 | int Jim_GetEnum(Jim_Interp *interp, Jim_Obj *objPtr, |
| 22271 | const char *const *tablePtr, int *indexPtr, const char *name, int flags) |
| 22272 | { |
| 22273 | const char *bad = "bad "; |
| 22274 | const char *const *entryPtr = NULL; |
| 22275 | int i; |
| 22276 | int match = -1; |
| 22277 | int arglen; |
| 22278 | const char *arg; |
| 22279 | |
| 22280 | if (objPtr->typePtr == &getEnumObjType) { |
| 22281 | if (objPtr->internalRep.ptrIntValue.ptr == tablePtr && objPtr->internalRep.ptrIntValue.int1 == flags) { |
| 22282 | *indexPtr = objPtr->internalRep.ptrIntValue.int2; |
| 22283 | return JIM_OK; |
| 22284 | } |
| 22285 | } |
| 22286 | |
| 22287 | arg = Jim_GetString(objPtr, &arglen); |
| 22288 | |
| 22289 | *indexPtr = -1; |
| 22290 | |
| 22291 | for (entryPtr = tablePtr, i = 0; *entryPtr != NULL; entryPtr++, i++) { |
| 22292 | if (Jim_CompareStringImmediate(interp, objPtr, *entryPtr)) { |
| 22293 | |
| 22294 | match = i; |
| 22295 | goto found; |
| 22296 | } |
| 22297 | if (flags & JIM_ENUM_ABBREV) { |
| 22298 | if (strncmp(arg, *entryPtr, arglen) == 0) { |
| 22299 | if (*arg == '-' && arglen == 1) { |
| 22300 | break; |
| 22301 | } |
| 22302 | if (match >= 0) { |
| 22303 | bad = "ambiguous "; |
| 22304 | goto ambiguous; |
| 22305 | } |
| 22306 | match = i; |
| 22307 | } |
| 22308 | } |
| 22309 | } |
| 22310 | |
| 22311 | |
| 22312 | if (match >= 0) { |
| 22313 | found: |
| 22314 | |
| 22315 | Jim_FreeIntRep(interp, objPtr); |
| 22316 | objPtr->typePtr = &getEnumObjType; |
| 22317 | objPtr->internalRep.ptrIntValue.ptr = (void *)tablePtr; |
| 22318 | objPtr->internalRep.ptrIntValue.int1 = flags; |
| 22319 | objPtr->internalRep.ptrIntValue.int2 = match; |
| 22320 | |
| 22321 | *indexPtr = match; |
| 22322 | return JIM_OK; |
| 22323 | } |
| 22324 | |
| 22325 | ambiguous: |
| 22326 | if (flags & JIM_ERRMSG) { |
| 22327 | JimSetFailedEnumResult(interp, arg, bad, "", tablePtr, name); |
| 22328 | } |
| 22329 | return JIM_ERR; |
| 22330 | } |
| 22331 | |
| 22332 | int Jim_FindByName(const char *name, const char * const array[], size_t len) |
| 22333 | { |
| 22334 | int i; |
| 22335 | |
| 22336 | for (i = 0; i < (int)len; i++) { |
| 22337 | if (array[i] && strcmp(array[i], name) == 0) { |
| 22338 | return i; |
| 22339 | } |
| 22340 | } |
| 22341 | return -1; |
| 22342 | } |
| 22343 | |
| 22344 | int Jim_IsDict(Jim_Obj *objPtr) |
| 22345 | { |
| 22346 | return objPtr->typePtr == &dictObjType; |
| 22347 | } |
| 22348 | |
| 22349 | int Jim_IsList(Jim_Obj *objPtr) |
| 22350 | { |
| 22351 | return objPtr->typePtr == &listObjType; |
| 22352 | } |
| 22353 | |
| 22354 | void Jim_SetResultFormatted(Jim_Interp *interp, const char *format, ...) |
| 22355 | { |
| 22356 | |
| 22357 | int len = strlen(format); |
| 22358 | int extra = 0; |
| 22359 | int n = 0; |
| 22360 | const char *params[5]; |
| 22361 | int nobjparam = 0; |
| 22362 | Jim_Obj *objparam[5]; |
| 22363 | char *buf; |
| 22364 | va_list args; |
| 22365 | int i; |
| 22366 | |
| 22367 | va_start(args, format); |
| 22368 | |
| 22369 | for (i = 0; i < len && n < 5; i++) { |
| 22370 | int l; |
| 22371 | |
| 22372 | if (strncmp(format + i, "%s", 2) == 0) { |
| 22373 | params[n] = va_arg(args, char *); |
| 22374 | |
| 22375 | l = strlen(params[n]); |
| 22376 | } |
| 22377 | else if (strncmp(format + i, "%#s", 3) == 0) { |
| 22378 | Jim_Obj *objPtr = va_arg(args, Jim_Obj *); |
| 22379 | |
| 22380 | params[n] = Jim_GetString(objPtr, &l); |
| 22381 | objparam[nobjparam++] = objPtr; |
| 22382 | Jim_IncrRefCount(objPtr); |
| 22383 | } |
| 22384 | else { |
| 22385 | if (format[i] == '%') { |
| 22386 | i++; |
| 22387 | } |
| 22388 | continue; |
| 22389 | } |
| 22390 | n++; |
| 22391 | extra += l; |
| 22392 | } |
| 22393 | |
| 22394 | len += extra; |
| 22395 | buf = Jim_Alloc(len + 1); |
| 22396 | len = snprintf(buf, len + 1, format, params[0], params[1], params[2], params[3], params[4]); |
| 22397 | |
| 22398 | va_end(args); |
| 22399 | |
| 22400 | Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, buf, len)); |
| 22401 | |
| 22402 | for (i = 0; i < nobjparam; i++) { |
| 22403 | Jim_DecrRefCount(interp, objparam[i]); |
| 22404 | } |
| 22405 | } |
| 22406 | |
| 22407 | int Jim_CheckAbiVersion(Jim_Interp *interp, int abi_version) |
| 22408 | { |
| 22409 | if (abi_version != JIM_ABI_VERSION) { |
| 22410 | Jim_SetResultString(interp, "ABI version mismatch", -1); |
| 22411 | return JIM_ERR; |
| 22412 | } |
| 22413 | return JIM_OK; |
| 22414 | } |
| 22415 | |
| 22416 | |
| 22417 | #ifndef jim_ext_package |
| 22418 | int Jim_PackageProvide(Jim_Interp *interp, const char *name, const char *ver, int flags) |
| 22419 | { |
| 22420 | return JIM_OK; |
| 22421 | } |
| 22422 | #endif |
| 22423 | #ifndef jim_ext_aio |
| 22424 | int Jim_AioFilehandle(Jim_Interp *interp, Jim_Obj *fhObj) |
| 22425 | { |
| 22426 | return -1; |
| 22427 | } |
| 22428 | #endif |
| 22429 | |
| 22430 | |
| 22431 | #include <stdio.h> |
| 22432 | #include <string.h> |
| 22433 | |
| 22434 | |
| 22435 | static int subcmd_null(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 22436 | { |
| 22437 | |
| 22438 | return JIM_OK; |
| 22439 | } |
| 22440 | |
| 22441 | static const jim_subcmd_type dummy_subcmd = { |
| 22442 | "dummy", NULL, subcmd_null, 0, 0, JIM_MODFLAG_HIDDEN |
| 22443 | }; |
| 22444 | |
| 22445 | static Jim_Obj *subcmd_cmd_list(Jim_Interp *interp, const jim_subcmd_type * ct, const char *sep) |
| 22446 | { |
| 22447 | |
| 22448 | Jim_Obj *listObj = Jim_NewListObj(interp, NULL, 0); |
| 22449 | Jim_Obj *sortCmd[2]; |
| 22450 | |
| 22451 | for (; ct->cmd; ct++) { |
| 22452 | if (!(ct->flags & JIM_MODFLAG_HIDDEN)) { |
| 22453 | Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, ct->cmd, -1)); |
| 22454 | } |
| 22455 | } |
| 22456 | |
| 22457 | |
| 22458 | sortCmd[0] = Jim_NewStringObj(interp, "lsort", -1); |
| 22459 | sortCmd[1] = listObj; |
| 22460 | |
| 22461 | if (Jim_EvalObjVector(interp, 2, sortCmd) == JIM_OK) { |
| 22462 | return Jim_ListJoin(interp, Jim_GetResult(interp), sep, strlen(sep)); |
| 22463 | } |
| 22464 | |
| 22465 | return Jim_GetResult(interp); |
| 22466 | } |
| 22467 | |
| 22468 | static void bad_subcmd(Jim_Interp *interp, const jim_subcmd_type * command_table, const char *type, |
| 22469 | Jim_Obj *cmd, Jim_Obj *subcmd) |
| 22470 | { |
| 22471 | Jim_SetResultFormatted(interp, "%#s, %s command \"%#s\": should be %#s", cmd, type, |
| 22472 | subcmd, subcmd_cmd_list(interp, command_table, ", ")); |
| 22473 | } |
| 22474 | |
| 22475 | static void show_cmd_usage(Jim_Interp *interp, const jim_subcmd_type * command_table, int argc, |
| 22476 | Jim_Obj *const *argv) |
| 22477 | { |
| 22478 | Jim_SetResultFormatted(interp, "Usage: \"%#s command ... \", where command is one of: %#s", |
| 22479 | argv[0], subcmd_cmd_list(interp, command_table, ", ")); |
| 22480 | } |
| 22481 | |
| 22482 | static void add_cmd_usage(Jim_Interp *interp, const jim_subcmd_type * ct, Jim_Obj *cmd) |
| 22483 | { |
| 22484 | if (cmd) { |
| 22485 | Jim_AppendStrings(interp, Jim_GetResult(interp), Jim_String(cmd), " ", NULL); |
| 22486 | } |
| 22487 | Jim_AppendStrings(interp, Jim_GetResult(interp), ct->cmd, NULL); |
| 22488 | if (ct->args && *ct->args) { |
| 22489 | Jim_AppendStrings(interp, Jim_GetResult(interp), " ", ct->args, NULL); |
| 22490 | } |
| 22491 | } |
| 22492 | |
| 22493 | void Jim_SubCmdArgError(Jim_Interp *interp, const jim_subcmd_type * ct, Jim_Obj *subcmd) |
| 22494 | { |
| 22495 | Jim_SetResultString(interp, "wrong # args: should be \"", -1); |
| 22496 | add_cmd_usage(interp, ct, subcmd); |
| 22497 | Jim_AppendStrings(interp, Jim_GetResult(interp), "\"", NULL); |
| 22498 | } |
| 22499 | |
| 22500 | static const Jim_ObjType subcmdLookupObjType = { |
| 22501 | "subcmd-lookup", |
| 22502 | NULL, |
| 22503 | NULL, |
| 22504 | NULL, |
| 22505 | JIM_TYPE_REFERENCES |
| 22506 | }; |
| 22507 | |
| 22508 | const jim_subcmd_type *Jim_ParseSubCmd(Jim_Interp *interp, const jim_subcmd_type * command_table, |
| 22509 | int argc, Jim_Obj *const *argv) |
| 22510 | { |
| 22511 | const jim_subcmd_type *ct; |
| 22512 | const jim_subcmd_type *partial = 0; |
| 22513 | int cmdlen; |
| 22514 | Jim_Obj *cmd; |
| 22515 | const char *cmdstr; |
| 22516 | int help = 0; |
| 22517 | int argsok = 1; |
| 22518 | |
| 22519 | if (argc < 2) { |
| 22520 | Jim_SetResultFormatted(interp, "wrong # args: should be \"%#s command ...\"\n" |
| 22521 | "Use \"%#s -help ?command?\" for help", argv[0], argv[0]); |
| 22522 | return 0; |
| 22523 | } |
| 22524 | |
| 22525 | cmd = argv[1]; |
| 22526 | |
| 22527 | |
| 22528 | if (cmd->typePtr == &subcmdLookupObjType) { |
| 22529 | if (cmd->internalRep.ptrIntValue.ptr == command_table) { |
| 22530 | ct = command_table + cmd->internalRep.ptrIntValue.int1; |
| 22531 | goto found; |
| 22532 | } |
| 22533 | } |
| 22534 | |
| 22535 | |
| 22536 | if (Jim_CompareStringImmediate(interp, cmd, "-help")) { |
| 22537 | if (argc == 2) { |
| 22538 | |
| 22539 | show_cmd_usage(interp, command_table, argc, argv); |
| 22540 | return &dummy_subcmd; |
| 22541 | } |
| 22542 | help = 1; |
| 22543 | |
| 22544 | |
| 22545 | cmd = argv[2]; |
| 22546 | } |
| 22547 | |
| 22548 | |
| 22549 | if (Jim_CompareStringImmediate(interp, cmd, "-commands")) { |
| 22550 | Jim_SetResult(interp, subcmd_cmd_list(interp, command_table, " ")); |
| 22551 | return &dummy_subcmd; |
| 22552 | } |
| 22553 | |
| 22554 | cmdstr = Jim_GetString(cmd, &cmdlen); |
| 22555 | |
| 22556 | for (ct = command_table; ct->cmd; ct++) { |
| 22557 | if (Jim_CompareStringImmediate(interp, cmd, ct->cmd)) { |
| 22558 | |
| 22559 | break; |
| 22560 | } |
| 22561 | if (strncmp(cmdstr, ct->cmd, cmdlen) == 0) { |
| 22562 | if (partial) { |
| 22563 | |
| 22564 | if (help) { |
| 22565 | |
| 22566 | show_cmd_usage(interp, command_table, argc, argv); |
| 22567 | return &dummy_subcmd; |
| 22568 | } |
| 22569 | bad_subcmd(interp, command_table, "ambiguous", argv[0], argv[1 + help]); |
| 22570 | return 0; |
| 22571 | } |
| 22572 | partial = ct; |
| 22573 | } |
| 22574 | continue; |
| 22575 | } |
| 22576 | |
| 22577 | |
| 22578 | if (partial && !ct->cmd) { |
| 22579 | ct = partial; |
| 22580 | } |
| 22581 | |
| 22582 | if (!ct->cmd) { |
| 22583 | |
| 22584 | if (help) { |
| 22585 | |
| 22586 | show_cmd_usage(interp, command_table, argc, argv); |
| 22587 | return &dummy_subcmd; |
| 22588 | } |
| 22589 | bad_subcmd(interp, command_table, "unknown", argv[0], argv[1 + help]); |
| 22590 | return 0; |
| 22591 | } |
| 22592 | |
| 22593 | if (help) { |
| 22594 | Jim_SetResultString(interp, "Usage: ", -1); |
| 22595 | |
| 22596 | add_cmd_usage(interp, ct, argv[0]); |
| 22597 | return &dummy_subcmd; |
| 22598 | } |
| 22599 | |
| 22600 | |
| 22601 | Jim_FreeIntRep(interp, cmd); |
| 22602 | cmd->typePtr = &subcmdLookupObjType; |
| 22603 | cmd->internalRep.ptrIntValue.ptr = (void *)command_table; |
| 22604 | cmd->internalRep.ptrIntValue.int1 = ct - command_table; |
| 22605 | |
| 22606 | found: |
| 22607 | |
| 22608 | |
| 22609 | if (argc - 2 < ct->minargs) { |
| 22610 | argsok = 0; |
| 22611 | } |
| 22612 | else if (ct->maxargs >= 0 && argc - 2 > ct->maxargs) { |
| 22613 | argsok = 0; |
| 22614 | } |
| 22615 | else if (ct->maxargs < -1 && (argc - 2) % -ct->maxargs != 0) { |
| 22616 | |
| 22617 | argsok = 0; |
| 22618 | } |
| 22619 | if (!argsok) { |
| 22620 | Jim_SetResultString(interp, "wrong # args: should be \"", -1); |
| 22621 | |
| 22622 | add_cmd_usage(interp, ct, argv[0]); |
| 22623 | Jim_AppendStrings(interp, Jim_GetResult(interp), "\"", NULL); |
| 22624 | |
| 22625 | return 0; |
| 22626 | } |
| 22627 | |
| 22628 | |
| 22629 | return ct; |
| 22630 | } |
| 22631 | |
| 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 | } |
| 22658 | |
| 22659 | int Jim_SubCmdProc(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 22660 | { |
| 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> |
| 22683 | #include <assert.h> |
| 22684 | |
| 22685 | |
| 22686 | int utf8_fromunicode(char *p, unsigned uc) |
| 22687 | { |
| 22688 | if (uc <= 0x7f) { |
| 22689 | *p = uc; |
| 22690 | return 1; |
| 22691 | } |
| 22692 | else if (uc <= 0x7ff) { |
| 22693 | *p++ = 0xc0 | ((uc & 0x7c0) >> 6); |
| 22694 | *p = 0x80 | (uc & 0x3f); |
| 22695 | return 2; |
| 22696 | } |
| 22697 | else if (uc <= 0xffff) { |
| 22698 | *p++ = 0xe0 | ((uc & 0xf000) >> 12); |
| 22699 | *p++ = 0x80 | ((uc & 0xfc0) >> 6); |
| 22700 | *p = 0x80 | (uc & 0x3f); |
| 22701 | return 3; |
| 22702 | } |
| 22703 | |
| 22704 | else { |
| 22705 | *p++ = 0xf0 | ((uc & 0x1c0000) >> 18); |
| 22706 | *p++ = 0x80 | ((uc & 0x3f000) >> 12); |
| 22707 | *p++ = 0x80 | ((uc & 0xfc0) >> 6); |
| 22708 | *p = 0x80 | (uc & 0x3f); |
| 22709 | return 4; |
| 22710 | } |
| 22711 | } |
| 22712 | |
| 22713 | #include <ctype.h> |
| 22714 | #include <string.h> |
| 22715 | #include <stdio.h> |
| 22716 | |
| 22717 | |
| 22718 | #define JIM_INTEGER_SPACE 24 |
| 22719 | #define MAX_FLOAT_WIDTH 320 |
| 22720 | |
| 22721 | Jim_Obj *Jim_FormatString(Jim_Interp *interp, Jim_Obj *fmtObjPtr, int objc, Jim_Obj *const *objv) |
| 22722 | { |
| 22723 | const char *span, *format, *formatEnd, *msg; |
| 22724 | int numBytes = 0, objIndex = 0, gotXpg = 0, gotSequential = 0; |
| 22725 | static const char * const mixedXPG = |
| 22726 | "cannot mix \"%\" and \"%n$\" conversion specifiers"; |
| 22727 | static const char * const badIndex[2] = { |
| 22728 | "not enough arguments for all format specifiers", |
| 22729 | "\"%n$\" argument index out of range" |
| 22730 | }; |
| 22731 | int formatLen; |
| 22732 | Jim_Obj *resultPtr; |
| 22733 | |
| 22734 | char *num_buffer = NULL; |
| 22735 | int num_buffer_size = 0; |
| 22736 | |
| 22737 | span = format = Jim_GetString(fmtObjPtr, &formatLen); |
| 22738 | formatEnd = format + formatLen; |
| 22739 | resultPtr = Jim_NewEmptyStringObj(interp); |
| 22740 | |
| 22741 | while (format != formatEnd) { |
| 22742 | char *end; |
| 22743 | int gotMinus, sawFlag; |
| 22744 | int gotPrecision, useShort; |
| 22745 | long width, precision; |
| 22746 | int newXpg; |
| 22747 | int ch; |
| 22748 | int step; |
| 22749 | int doubleType; |
| 22750 | char pad = ' '; |
| 22751 | char spec[2*JIM_INTEGER_SPACE + 12]; |
| 22752 | char *p; |
| 22753 | |
| 22754 | int formatted_chars; |
| 22755 | int formatted_bytes; |
| 22756 | const char *formatted_buf; |
| 22757 | |
| 22758 | step = utf8_tounicode(format, &ch); |
| 22759 | format += step; |
| 22760 | if (ch != '%') { |
| 22761 | numBytes += step; |
| 22762 | continue; |
| 22763 | } |
| 22764 | if (numBytes) { |
| 22765 | Jim_AppendString(interp, resultPtr, span, numBytes); |
| 22766 | numBytes = 0; |
| 22767 | } |
| 22768 | |
| 22769 | |
| 22770 | step = utf8_tounicode(format, &ch); |
| 22771 | if (ch == '%') { |
| 22772 | span = format; |
| 22773 | numBytes = step; |
| 22774 | format += step; |
| 22775 | continue; |
| 22776 | } |
| 22777 | |
| 22778 | |
| 22779 | newXpg = 0; |
| 22780 | if (isdigit(ch)) { |
| 22781 | int position = strtoul(format, &end, 10); |
| 22782 | if (*end == '$') { |
| 22783 | newXpg = 1; |
| 22784 | objIndex = position - 1; |
| 22785 | format = end + 1; |
| 22786 | step = utf8_tounicode(format, &ch); |
| 22787 | } |
| 22788 | } |
| 22789 | if (newXpg) { |
| 22790 | if (gotSequential) { |
| 22791 | msg = mixedXPG; |
| 22792 | goto errorMsg; |
| 22793 | } |
| 22794 | gotXpg = 1; |
| 22795 | } else { |
| 22796 | if (gotXpg) { |
| 22797 | msg = mixedXPG; |
| 22798 | goto errorMsg; |
| 22799 | } |
| 22800 | gotSequential = 1; |
| 22801 | } |
| 22802 | if ((objIndex < 0) || (objIndex >= objc)) { |
| 22803 | msg = badIndex[gotXpg]; |
| 22804 | goto errorMsg; |
| 22805 | } |
| 22806 | |
| 22807 | p = spec; |
| 22808 | *p++ = '%'; |
| 22809 | |
| 22810 | gotMinus = 0; |
| 22811 | sawFlag = 1; |
| 22812 | do { |
| 22813 | switch (ch) { |
| 22814 | case '-': |
| 22815 | gotMinus = 1; |
| 22816 | break; |
| 22817 | case '0': |
| 22818 | pad = ch; |
| 22819 | break; |
| 22820 | case ' ': |
| 22821 | case '+': |
| 22822 | case '#': |
| 22823 | break; |
| 22824 | default: |
| 22825 | sawFlag = 0; |
| 22826 | continue; |
| 22827 | } |
| 22828 | *p++ = ch; |
| 22829 | format += step; |
| 22830 | step = utf8_tounicode(format, &ch); |
| 22831 | |
| 22832 | } while (sawFlag && (p - spec <= 5)); |
| 22833 | |
| 22834 | |
| 22835 | width = 0; |
| 22836 | if (isdigit(ch)) { |
| 22837 | width = strtoul(format, &end, 10); |
| 22838 | format = end; |
| 22839 | step = utf8_tounicode(format, &ch); |
| 22840 | } else if (ch == '*') { |
| 22841 | if (objIndex >= objc - 1) { |
| 22842 | msg = badIndex[gotXpg]; |
| 22843 | goto errorMsg; |
| 22844 | } |
| 22845 | if (Jim_GetLong(interp, objv[objIndex], &width) != JIM_OK) { |
| 22846 | goto error; |
| 22847 | } |
| 22848 | if (width < 0) { |
| 22849 | width = -width; |
| 22850 | if (!gotMinus) { |
| 22851 | *p++ = '-'; |
| 22852 | gotMinus = 1; |
| 22853 | } |
| 22854 | } |
| 22855 | objIndex++; |
| 22856 | format += step; |
| 22857 | step = utf8_tounicode(format, &ch); |
| 22858 | } |
| 22859 | |
| 22860 | |
| 22861 | gotPrecision = precision = 0; |
| 22862 | if (ch == '.') { |
| 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]; |
| 22874 | goto errorMsg; |
| 22875 | } |
| 22876 | if (Jim_GetLong(interp, objv[objIndex], &precision) != JIM_OK) { |
| 22877 | goto error; |
| 22878 | } |
| 22879 | |
| 22880 | |
| 22881 | if (precision < 0) { |
| 22882 | precision = 0; |
| 22883 | } |
| 22884 | objIndex++; |
| 22885 | format += step; |
| 22886 | step = utf8_tounicode(format, &ch); |
| 22887 | } |
| 22888 | |
| 22889 | |
| 22890 | useShort = 0; |
| 22891 | if (ch == 'h') { |
| 22892 | useShort = 1; |
| 22893 | format += step; |
| 22894 | step = utf8_tounicode(format, &ch); |
| 22895 | } else if (ch == 'l') { |
| 22896 | |
| 22897 | format += step; |
| 22898 | step = utf8_tounicode(format, &ch); |
| 22899 | if (ch == 'l') { |
| 22900 | format += step; |
| 22901 | step = utf8_tounicode(format, &ch); |
| 22902 | } |
| 22903 | } |
| 22904 | |
| 22905 | format += step; |
| 22906 | span = format; |
| 22907 | |
| 22908 | |
| 22909 | if (ch == 'i') { |
| 22910 | ch = 'd'; |
| 22911 | } |
| 22912 | |
| 22913 | doubleType = 0; |
| 22914 | |
| 22915 | switch (ch) { |
| 22916 | case '\0': |
| 22917 | msg = "format string ended in middle of field specifier"; |
| 22918 | goto errorMsg; |
| 22919 | case 's': { |
| 22920 | formatted_buf = Jim_GetString(objv[objIndex], &formatted_bytes); |
| 22921 | formatted_chars = Jim_Utf8Length(interp, objv[objIndex]); |
| 22922 | if (gotPrecision && (precision < formatted_chars)) { |
| 22923 | |
| 22924 | formatted_chars = precision; |
| 22925 | formatted_bytes = utf8_index(formatted_buf, precision); |
| 22926 | } |
| 22927 | break; |
| 22928 | } |
| 22929 | case 'c': { |
| 22930 | jim_wide code; |
| 22931 | |
| 22932 | if (Jim_GetWide(interp, objv[objIndex], &code) != JIM_OK) { |
| 22933 | goto error; |
| 22934 | } |
| 22935 | |
| 22936 | formatted_bytes = utf8_getchars(spec, code); |
| 22937 | formatted_buf = spec; |
| 22938 | formatted_chars = 1; |
| 22939 | break; |
| 22940 | } |
| 22941 | case 'b': { |
| 22942 | unsigned jim_wide w; |
| 22943 | int length; |
| 22944 | int i; |
| 22945 | int j; |
| 22946 | |
| 22947 | if (Jim_GetWide(interp, objv[objIndex], (jim_wide *)&w) != JIM_OK) { |
| 22948 | goto error; |
| 22949 | } |
| 22950 | length = sizeof(w) * 8; |
| 22951 | |
| 22952 | |
| 22953 | |
| 22954 | if (num_buffer_size < length + 1) { |
| 22955 | num_buffer_size = length + 1; |
| 22956 | num_buffer = Jim_Realloc(num_buffer, num_buffer_size); |
| 22957 | } |
| 22958 | |
| 22959 | j = 0; |
| 22960 | for (i = length; i > 0; ) { |
| 22961 | i--; |
| 22962 | if (w & ((unsigned jim_wide)1 << i)) { |
| 22963 | num_buffer[j++] = '1'; |
| 22964 | } |
| 22965 | else if (j || i == 0) { |
| 22966 | num_buffer[j++] = '0'; |
| 22967 | } |
| 22968 | } |
| 22969 | num_buffer[j] = 0; |
| 22970 | formatted_chars = formatted_bytes = j; |
| 22971 | formatted_buf = num_buffer; |
| 22972 | break; |
| 22973 | } |
| 22974 | |
| 22975 | case 'e': |
| 22976 | case 'E': |
| 22977 | case 'f': |
| 22978 | case 'g': |
| 22979 | case 'G': |
| 22980 | doubleType = 1; |
| 22981 | |
| 22982 | case 'd': |
| 22983 | case 'u': |
| 22984 | case 'o': |
| 22985 | case 'x': |
| 22986 | case 'X': { |
| 22987 | jim_wide w; |
| 22988 | double d; |
| 22989 | int length; |
| 22990 | |
| 22991 | |
| 22992 | if (width) { |
| 22993 | p += sprintf(p, "%ld", width); |
| 22994 | } |
| 22995 | if (gotPrecision) { |
| 22996 | p += sprintf(p, ".%ld", precision); |
| 22997 | } |
| 22998 | |
| 22999 | |
| 23000 | if (doubleType) { |
| 23001 | if (Jim_GetDouble(interp, objv[objIndex], &d) != JIM_OK) { |
| 23002 | goto error; |
| 23003 | } |
| 23004 | length = MAX_FLOAT_WIDTH; |
| 23005 | } |
| 23006 | else { |
| 23007 | if (Jim_GetWide(interp, objv[objIndex], &w) != JIM_OK) { |
| 23008 | goto error; |
| 23009 | } |
| 23010 | length = JIM_INTEGER_SPACE; |
| 23011 | if (useShort) { |
| 23012 | if (ch == 'd') { |
| 23013 | w = (short)w; |
| 23014 | } |
| 23015 | else { |
| 23016 | w = (unsigned short)w; |
| 23017 | } |
| 23018 | } |
| 23019 | *p++ = 'l'; |
| 23020 | #ifdef HAVE_LONG_LONG |
| 23021 | if (sizeof(long long) == sizeof(jim_wide)) { |
| 23022 | *p++ = 'l'; |
| 23023 | } |
| 23024 | #endif |
| 23025 | } |
| 23026 | |
| 23027 | *p++ = (char) ch; |
| 23028 | *p = '\0'; |
| 23029 | |
| 23030 | |
| 23031 | if (width > 10000 || length > 10000 || precision > 10000) { |
| 23032 | Jim_SetResultString(interp, "format too long", -1); |
| 23033 | goto error; |
| 23034 | } |
| 23035 | |
| 23036 | |
| 23037 | |
| 23038 | if (width > length) { |
| 23039 | length = width; |
| 23040 | } |
| 23041 | if (gotPrecision) { |
| 23042 | length += precision; |
| 23043 | } |
| 23044 | |
| 23045 | |
| 23046 | if (num_buffer_size < length + 1) { |
| 23047 | num_buffer_size = length + 1; |
| 23048 | num_buffer = Jim_Realloc(num_buffer, num_buffer_size); |
| 23049 | } |
| 23050 | |
| 23051 | if (doubleType) { |
| 23052 | snprintf(num_buffer, length + 1, spec, d); |
| 23053 | } |
| 23054 | else { |
| 23055 | formatted_bytes = snprintf(num_buffer, length + 1, spec, w); |
| 23056 | } |
| 23057 | formatted_chars = formatted_bytes = strlen(num_buffer); |
| 23058 | formatted_buf = num_buffer; |
| 23059 | break; |
| 23060 | } |
| 23061 | |
| 23062 | default: { |
| 23063 | |
| 23064 | spec[0] = ch; |
| 23065 | spec[1] = '\0'; |
| 23066 | Jim_SetResultFormatted(interp, "bad field specifier \"%s\"", spec); |
| 23067 | goto error; |
| 23068 | } |
| 23069 | } |
| 23070 | |
| 23071 | if (!gotMinus) { |
| 23072 | while (formatted_chars < width) { |
| 23073 | Jim_AppendString(interp, resultPtr, &pad, 1); |
| 23074 | formatted_chars++; |
| 23075 | } |
| 23076 | } |
| 23077 | |
| 23078 | Jim_AppendString(interp, resultPtr, formatted_buf, formatted_bytes); |
| 23079 | |
| 23080 | while (formatted_chars < width) { |
| 23081 | Jim_AppendString(interp, resultPtr, &pad, 1); |
| 23082 | formatted_chars++; |
| 23083 | } |
| 23084 | |
| 23085 | objIndex += gotSequential; |
| 23086 | } |
| 23087 | if (numBytes) { |
| 23088 | Jim_AppendString(interp, resultPtr, span, numBytes); |
| 23089 | } |
| 23090 | |
| 23091 | Jim_Free(num_buffer); |
| 23092 | return resultPtr; |
| 23093 | |
| 23094 | errorMsg: |
| 23095 | Jim_SetResultString(interp, msg, -1); |
| 23096 | error: |
| 23097 | Jim_FreeNewObj(interp, resultPtr); |
| 23098 | Jim_Free(num_buffer); |
| 23099 | return NULL; |
| 23100 | } |
| 23101 | |
| 23102 | |
| 23103 | #if defined(JIM_REGEXP) |
| 23104 | #include <stdio.h> |
| 23105 | #include <ctype.h> |
| 23106 | #include <stdlib.h> |
| 23107 | #include <string.h> |
| 23108 | |
| 23109 | |
| 23110 | |
| 23111 | #define REG_MAX_PAREN 100 |
| 23112 | |
| 23113 | |
| 23114 | |
| 23115 | #define END 0 |
| 23116 | #define BOL 1 |
| 23117 | #define EOL 2 |
| 23118 | #define ANY 3 |
| 23119 | #define ANYOF 4 |
| 23120 | #define ANYBUT 5 |
| 23121 | #define BRANCH 6 |
| 23122 | #define BACK 7 |
| 23123 | #define EXACTLY 8 |
| 23124 | #define NOTHING 9 |
| 23125 | #define REP 10 |
| 23126 | #define REPMIN 11 |
| 23127 | #define REPX 12 |
| 23128 | #define REPXMIN 13 |
| 23129 | #define BOLX 14 |
| 23130 | #define EOLX 15 |
| 23131 | #define WORDA 16 |
| 23132 | #define WORDZ 17 |
| 23133 | |
| 23134 | #define OPENNC 1000 |
| 23135 | #define OPEN 1001 |
| 23136 | |
| 23137 | |
| 23138 | |
| 23139 | |
| 23140 | #define CLOSENC 2000 |
| 23141 | #define CLOSE 2001 |
| 23142 | #define CLOSE_END (CLOSE+REG_MAX_PAREN) |
| 23143 | |
| 23144 | #define REG_MAGIC 0xFADED00D |
| 23145 | |
| 23146 | |
| 23147 | #define OP(preg, p) (preg->program[p]) |
| 23148 | #define NEXT(preg, p) (preg->program[p + 1]) |
| 23149 | #define OPERAND(p) ((p) + 2) |
| 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 |
| 23161 | |
| 23162 | #define MAX_REP_COUNT 1000000 |
| 23163 | |
| 23164 | static int reg(regex_t *preg, int paren, int *flagp ); |
| 23165 | static int regpiece(regex_t *preg, int *flagp ); |
| 23166 | static int regbranch(regex_t *preg, int *flagp ); |
| 23167 | static int regatom(regex_t *preg, int *flagp ); |
| 23168 | static int regnode(regex_t *preg, int op ); |
| 23169 | static int regnext(regex_t *preg, int p ); |
| 23170 | static void regc(regex_t *preg, int b ); |
| 23171 | static int reginsert(regex_t *preg, int op, int size, int opnd ); |
| 23172 | static void regtail(regex_t *preg, int p, int val); |
| 23173 | static void regoptail(regex_t *preg, int p, int val ); |
| 23174 | static int regopsize(regex_t *preg, int p ); |
| 23175 | |
| 23176 | static int reg_range_find(const int *string, int c); |
| 23177 | static const char *str_find(const char *string, int c, int nocase); |
| 23178 | static int prefix_cmp(const int *prog, int proglen, const char *string, int nocase); |
| 23179 | |
| 23180 | |
| 23181 | #ifdef DEBUG |
| 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++) { |
| 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; |
| 23253 | int flags; |
| 23254 | |
| 23255 | #ifdef DEBUG |
| 23256 | fprintf(stderr, "Compiling: '%s'\n", exp); |
| 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 | |
| 23272 | |
| 23273 | preg->proglen = (strlen(exp) + 1) * 5; |
| 23274 | preg->program = malloc(preg->proglen * sizeof(int)); |
| 23275 | if (preg->program == NULL) |
| 23276 | FAIL(preg, REG_ERR_NOMEM); |
| 23277 | |
| 23278 | regc(preg, REG_MAGIC); |
| 23279 | if (reg(preg, 0, &flags) == 0) { |
| 23280 | return preg->err; |
| 23281 | } |
| 23282 | |
| 23283 | |
| 23284 | if (preg->re_nsub >= REG_MAX_PAREN) |
| 23285 | FAIL(preg,REG_ERR_TOO_BIG); |
| 23286 | |
| 23287 | |
| 23288 | preg->regstart = 0; |
| 23289 | preg->reganch = 0; |
| 23290 | preg->regmust = 0; |
| 23291 | preg->regmlen = 0; |
| 23292 | scan = 1; |
| 23293 | if (OP(preg, regnext(preg, scan)) == END) { |
| 23294 | scan = OPERAND(scan); |
| 23295 | |
| 23296 | |
| 23297 | if (OP(preg, scan) == EXACTLY) { |
| 23298 | preg->regstart = preg->program[OPERAND(scan)]; |
| 23299 | } |
| 23300 | else if (OP(preg, scan) == BOL) |
| 23301 | preg->reganch++; |
| 23302 | |
| 23303 | if (flags&SPSTART) { |
| 23304 | longest = 0; |
| 23305 | len = 0; |
| 23306 | for (; scan != 0; scan = regnext(preg, scan)) { |
| 23307 | if (OP(preg, scan) == EXACTLY) { |
| 23308 | int plen = str_int_len(preg->program + OPERAND(scan)); |
| 23309 | if (plen >= len) { |
| 23310 | longest = OPERAND(scan); |
| 23311 | len = plen; |
| 23312 | } |
| 23313 | } |
| 23314 | } |
| 23315 | preg->regmust = longest; |
| 23316 | preg->regmlen = len; |
| 23317 | } |
| 23318 | } |
| 23319 | |
| 23320 | #ifdef DEBUG |
| 23321 | regdump(preg); |
| 23322 | #endif |
| 23323 | |
| 23324 | return 0; |
| 23325 | } |
| 23326 | |
| 23327 | static int reg(regex_t *preg, int paren, int *flagp ) |
| 23328 | { |
| 23329 | int ret; |
| 23330 | int br; |
| 23331 | int ender; |
| 23332 | int parno = 0; |
| 23333 | int flags; |
| 23334 | |
| 23335 | *flagp = HASWIDTH; |
| 23336 | |
| 23337 | |
| 23338 | if (paren) { |
| 23339 | if (preg->regparse[0] == '?' && preg->regparse[1] == ':') { |
| 23340 | |
| 23341 | preg->regparse += 2; |
| 23342 | parno = -1; |
| 23343 | } |
| 23344 | else { |
| 23345 | parno = ++preg->re_nsub; |
| 23346 | } |
| 23347 | ret = regnode(preg, OPEN+parno); |
| 23348 | } else |
| 23349 | ret = 0; |
| 23350 | |
| 23351 | |
| 23352 | br = regbranch(preg, &flags); |
| 23353 | if (br == 0) |
| 23354 | return 0; |
| 23355 | if (ret != 0) |
| 23356 | regtail(preg, ret, br); |
| 23357 | else |
| 23358 | ret = br; |
| 23359 | if (!(flags&HASWIDTH)) |
| 23360 | *flagp &= ~HASWIDTH; |
| 23361 | *flagp |= flags&SPSTART; |
| 23362 | while (*preg->regparse == '|') { |
| 23363 | preg->regparse++; |
| 23364 | br = regbranch(preg, &flags); |
| 23365 | if (br == 0) |
| 23366 | return 0; |
| 23367 | regtail(preg, ret, br); |
| 23368 | if (!(flags&HASWIDTH)) |
| 23369 | *flagp &= ~HASWIDTH; |
| 23370 | *flagp |= flags&SPSTART; |
| 23371 | } |
| 23372 | |
| 23373 | |
| 23374 | ender = regnode(preg, (paren) ? CLOSE+parno : END); |
| 23375 | regtail(preg, ret, ender); |
| 23376 | |
| 23377 | |
| 23378 | for (br = ret; br != 0; br = regnext(preg, br)) |
| 23379 | regoptail(preg, br, ender); |
| 23380 | |
| 23381 | |
| 23382 | if (paren && *preg->regparse++ != ')') { |
| 23383 | preg->err = REG_ERR_UNMATCHED_PAREN; |
| 23384 | return 0; |
| 23385 | } else if (!paren && *preg->regparse != '\0') { |
| 23386 | if (*preg->regparse == ')') { |
| 23387 | preg->err = REG_ERR_UNMATCHED_PAREN; |
| 23388 | return 0; |
| 23389 | } else { |
| 23390 | preg->err = REG_ERR_JUNK_ON_END; |
| 23391 | return 0; |
| 23392 | } |
| 23393 | } |
| 23394 | |
| 23395 | return(ret); |
| 23396 | } |
| 23397 | |
| 23398 | static int regbranch(regex_t *preg, int *flagp ) |
| 23399 | { |
| 23400 | int ret; |
| 23401 | int chain; |
| 23402 | int latest; |
| 23403 | int flags; |
| 23404 | |
| 23405 | *flagp = WORST; |
| 23406 | |
| 23407 | ret = regnode(preg, BRANCH); |
| 23408 | chain = 0; |
| 23409 | while (*preg->regparse != '\0' && *preg->regparse != ')' && |
| 23410 | *preg->regparse != '|') { |
| 23411 | latest = regpiece(preg, &flags); |
| 23412 | if (latest == 0) |
| 23413 | return 0; |
| 23414 | *flagp |= flags&HASWIDTH; |
| 23415 | if (chain == 0) { |
| 23416 | *flagp |= flags&SPSTART; |
| 23417 | } |
| 23418 | else { |
| 23419 | regtail(preg, chain, latest); |
| 23420 | } |
| 23421 | chain = latest; |
| 23422 | } |
| 23423 | if (chain == 0) |
| 23424 | (void) regnode(preg, NOTHING); |
| 23425 | |
| 23426 | return(ret); |
| 23427 | } |
| 23428 | |
| 23429 | static int regpiece(regex_t *preg, int *flagp) |
| 23430 | { |
| 23431 | int ret; |
| 23432 | char op; |
| 23433 | int next; |
| 23434 | int flags; |
| 23435 | int min; |
| 23436 | int max; |
| 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 | |
| 23453 | |
| 23454 | if (op == '{') { |
| 23455 | char *end; |
| 23456 | |
| 23457 | min = strtoul(preg->regparse + 1, &end, 10); |
| 23458 | if (end == preg->regparse + 1) { |
| 23459 | preg->err = REG_ERR_BAD_COUNT; |
| 23460 | return 0; |
| 23461 | } |
| 23462 | if (*end == '}') { |
| 23463 | max = min; |
| 23464 | } |
| 23465 | else if (*end == '\0') { |
| 23466 | preg->err = REG_ERR_UNMATCHED_BRACES; |
| 23467 | return 0; |
| 23468 | } |
| 23469 | else { |
| 23470 | preg->regparse = end; |
| 23471 | max = strtoul(preg->regparse + 1, &end, 10); |
| 23472 | if (*end != '}') { |
| 23473 | preg->err = REG_ERR_UNMATCHED_BRACES; |
| 23474 | return 0; |
| 23475 | } |
| 23476 | } |
| 23477 | if (end == preg->regparse + 1) { |
| 23478 | max = MAX_REP_COUNT; |
| 23479 | } |
| 23480 | else if (max < min || max >= 100) { |
| 23481 | preg->err = REG_ERR_BAD_COUNT; |
| 23482 | return 0; |
| 23483 | } |
| 23484 | if (min >= 100) { |
| 23485 | preg->err = REG_ERR_BAD_COUNT; |
| 23486 | return 0; |
| 23487 | } |
| 23488 | |
| 23489 | preg->regparse = strchr(preg->regparse, '}'); |
| 23490 | } |
| 23491 | else { |
| 23492 | min = (op == '+'); |
| 23493 | max = (op == '?' ? 1 : MAX_REP_COUNT); |
| 23494 | } |
| 23495 | |
| 23496 | if (preg->regparse[1] == '?') { |
| 23497 | preg->regparse++; |
| 23498 | next = reginsert(preg, flags & SIMPLE ? REPMIN : REPXMIN, 5, ret); |
| 23499 | } |
| 23500 | else { |
| 23501 | next = reginsert(preg, flags & SIMPLE ? REP: REPX, 5, ret); |
| 23502 | } |
| 23503 | preg->program[ret + 2] = max; |
| 23504 | preg->program[ret + 3] = min; |
| 23505 | preg->program[ret + 4] = 0; |
| 23506 | |
| 23507 | *flagp = (min) ? (WORST|HASWIDTH) : (WORST|SPSTART); |
| 23508 | |
| 23509 | if (!(flags & SIMPLE)) { |
| 23510 | int back = regnode(preg, BACK); |
| 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; |
| 23522 | } |
| 23523 | |
| 23524 | static void reg_addrange(regex_t *preg, int lower, int upper) |
| 23525 | { |
| 23526 | if (lower > upper) { |
| 23527 | reg_addrange(preg, upper, lower); |
| 23528 | } |
| 23529 | |
| 23530 | regc(preg, upper - lower + 1); |
| 23531 | regc(preg, lower); |
| 23532 | } |
| 23533 | |
| 23534 | static void reg_addrange_str(regex_t *preg, const char *str) |
| 23535 | { |
| 23536 | while (*str) { |
| 23537 | reg_addrange(preg, *str, *str); |
| 23538 | str++; |
| 23539 | } |
| 23540 | } |
| 23541 | |
| 23542 | static int reg_utf8_tounicode_case(const char *s, int *uc, int upper) |
| 23543 | { |
| 23544 | int l = utf8_tounicode(s, uc); |
| 23545 | if (upper) { |
| 23546 | *uc = utf8_upper(*uc); |
| 23547 | } |
| 23548 | return l; |
| 23549 | } |
| 23550 | |
| 23551 | static int hexdigitval(int c) |
| 23552 | { |
| 23553 | if (c >= '0' && c <= '9') |
| 23554 | return c - '0'; |
| 23555 | if (c >= 'a' && c <= 'f') |
| 23556 | return c - 'a' + 10; |
| 23557 | if (c >= 'A' && c <= 'F') |
| 23558 | return c - 'A' + 10; |
| 23559 | return -1; |
| 23560 | } |
| 23561 | |
| 23562 | static int parse_hex(const char *s, int n, int *uc) |
| 23563 | { |
| 23564 | int val = 0; |
| 23565 | int k; |
| 23566 | |
| 23567 | for (k = 0; k < n; k++) { |
| 23568 | int c = hexdigitval(*s++); |
| 23569 | if (c == -1) { |
| 23570 | break; |
| 23571 | } |
| 23572 | val = (val << 4) | c; |
| 23573 | } |
| 23574 | if (k) { |
| 23575 | *uc = val; |
| 23576 | } |
| 23577 | return k; |
| 23578 | } |
| 23579 | |
| 23580 | static int reg_decode_escape(const char *s, int *ch) |
| 23581 | { |
| 23582 | int n; |
| 23583 | const char *s0 = s; |
| 23584 | |
| 23585 | *ch = *s++; |
| 23586 | |
| 23587 | switch (*ch) { |
| 23588 | case 'b': *ch = '\b'; break; |
| 23589 | case 'e': *ch = 27; break; |
| 23590 | case 'f': *ch = '\f'; break; |
| 23591 | case 'n': *ch = '\n'; break; |
| 23592 | case 'r': *ch = '\r'; break; |
| 23593 | case 't': *ch = '\t'; break; |
| 23594 | case 'v': *ch = '\v'; break; |
| 23595 | case 'u': |
| 23596 | if (*s == '{') { |
| 23597 | |
| 23598 | n = parse_hex(s + 1, 6, ch); |
| 23599 | if (n > 0 && s[n + 1] == '}' && *ch >= 0 && *ch <= 0x1fffff) { |
| 23600 | s += n + 2; |
| 23601 | } |
| 23602 | else { |
| 23603 | |
| 23604 | *ch = 'u'; |
| 23605 | } |
| 23606 | } |
| 23607 | else if ((n = parse_hex(s, 4, ch)) > 0) { |
| 23608 | s += n; |
| 23609 | } |
| 23610 | break; |
| 23611 | case 'U': |
| 23612 | if ((n = parse_hex(s, 8, ch)) > 0) { |
| 23613 | s += n; |
| 23614 | } |
| 23615 | break; |
| 23616 | case 'x': |
| 23617 | if ((n = parse_hex(s, 2, ch)) > 0) { |
| 23618 | s += n; |
| 23619 | } |
| 23620 | break; |
| 23621 | case '\0': |
| 23622 | s--; |
| 23623 | *ch = '\\'; |
| 23624 | break; |
| 23625 | } |
| 23626 | return s - s0; |
| 23627 | } |
| 23628 | |
| 23629 | static int regatom(regex_t *preg, int *flagp) |
| 23630 | { |
| 23631 | int ret; |
| 23632 | int flags; |
| 23633 | int nocase = (preg->cflags & REG_ICASE); |
| 23634 | |
| 23635 | int ch; |
| 23636 | int n = reg_utf8_tounicode_case(preg->regparse, &ch, nocase); |
| 23637 | |
| 23638 | *flagp = WORST; |
| 23639 | |
| 23640 | preg->regparse += n; |
| 23641 | switch (ch) { |
| 23642 | |
| 23643 | case '^': |
| 23644 | ret = regnode(preg, BOL); |
| 23645 | break; |
| 23646 | case '$': |
| 23647 | ret = regnode(preg, EOL); |
| 23648 | break; |
| 23649 | case '.': |
| 23650 | ret = regnode(preg, ANY); |
| 23651 | *flagp |= HASWIDTH|SIMPLE; |
| 23652 | break; |
| 23653 | case '[': { |
| 23654 | const char *pattern = preg->regparse; |
| 23655 | |
| 23656 | if (*pattern == '^') { |
| 23657 | ret = regnode(preg, ANYBUT); |
| 23658 | pattern++; |
| 23659 | } else |
| 23660 | ret = regnode(preg, ANYOF); |
| 23661 | |
| 23662 | |
| 23663 | if (*pattern == ']' || *pattern == '-') { |
| 23664 | reg_addrange(preg, *pattern, *pattern); |
| 23665 | pattern++; |
| 23666 | } |
| 23667 | |
| 23668 | while (*pattern != ']') { |
| 23669 | |
| 23670 | int start; |
| 23671 | int end; |
| 23672 | |
| 23673 | enum { |
| 23674 | CC_ALPHA, CC_ALNUM, CC_SPACE, CC_BLANK, CC_UPPER, CC_LOWER, |
| 23675 | CC_DIGIT, CC_XDIGIT, CC_CNTRL, CC_GRAPH, CC_PRINT, CC_PUNCT, |
| 23676 | CC_NUM |
| 23677 | }; |
| 23678 | int cc; |
| 23679 | |
| 23680 | if (!*pattern) { |
| 23681 | preg->err = REG_ERR_UNMATCHED_BRACKET; |
| 23682 | return 0; |
| 23683 | } |
| 23684 | |
| 23685 | pattern += reg_utf8_tounicode_case(pattern, &start, nocase); |
| 23686 | if (start == '\\') { |
| 23687 | |
| 23688 | switch (*pattern) { |
| 23689 | case 's': |
| 23690 | pattern++; |
| 23691 | cc = CC_SPACE; |
| 23692 | goto cc_switch; |
| 23693 | case 'd': |
| 23694 | pattern++; |
| 23695 | cc = CC_DIGIT; |
| 23696 | goto cc_switch; |
| 23697 | case 'w': |
| 23698 | pattern++; |
| 23699 | reg_addrange(preg, '_', '_'); |
| 23700 | cc = CC_ALNUM; |
| 23701 | goto cc_switch; |
| 23702 | } |
| 23703 | pattern += reg_decode_escape(pattern, &start); |
| 23704 | if (start == 0) { |
| 23705 | preg->err = REG_ERR_NULL_CHAR; |
| 23706 | return 0; |
| 23707 | } |
| 23708 | if (start == '\\' && *pattern == 0) { |
| 23709 | preg->err = REG_ERR_INVALID_ESCAPE; |
| 23710 | return 0; |
| 23711 | } |
| 23712 | } |
| 23713 | if (pattern[0] == '-' && pattern[1] && pattern[1] != ']') { |
| 23714 | |
| 23715 | pattern += utf8_tounicode(pattern, &end); |
| 23716 | pattern += reg_utf8_tounicode_case(pattern, &end, nocase); |
| 23717 | if (end == '\\') { |
| 23718 | pattern += reg_decode_escape(pattern, &end); |
| 23719 | if (end == 0) { |
| 23720 | preg->err = REG_ERR_NULL_CHAR; |
| 23721 | return 0; |
| 23722 | } |
| 23723 | if (end == '\\' && *pattern == 0) { |
| 23724 | preg->err = REG_ERR_INVALID_ESCAPE; |
| 23725 | return 0; |
| 23726 | } |
| 23727 | } |
| 23728 | |
| 23729 | reg_addrange(preg, start, end); |
| 23730 | continue; |
| 23731 | } |
| 23732 | if (start == '[' && pattern[0] == ':') { |
| 23733 | static const char *character_class[] = { |
| 23734 | ":alpha:", ":alnum:", ":space:", ":blank:", ":upper:", ":lower:", |
| 23735 | ":digit:", ":xdigit:", ":cntrl:", ":graph:", ":print:", ":punct:", |
| 23736 | }; |
| 23737 | |
| 23738 | for (cc = 0; cc < CC_NUM; cc++) { |
| 23739 | n = strlen(character_class[cc]); |
| 23740 | if (strncmp(pattern, character_class[cc], n) == 0) { |
| 23741 | if (pattern[n] != ']') { |
| 23742 | preg->err = REG_ERR_UNMATCHED_BRACKET; |
| 23743 | return 0; |
| 23744 | } |
| 23745 | |
| 23746 | pattern += n + 1; |
| 23747 | break; |
| 23748 | } |
| 23749 | } |
| 23750 | if (cc != CC_NUM) { |
| 23751 | cc_switch: |
| 23752 | switch (cc) { |
| 23753 | case CC_ALNUM: |
| 23754 | reg_addrange(preg, '0', '9'); |
| 23755 | |
| 23756 | case CC_ALPHA: |
| 23757 | if ((preg->cflags & REG_ICASE) == 0) { |
| 23758 | reg_addrange(preg, 'a', 'z'); |
| 23759 | } |
| 23760 | reg_addrange(preg, 'A', 'Z'); |
| 23761 | break; |
| 23762 | case CC_SPACE: |
| 23763 | reg_addrange_str(preg, " \t\r\n\f\v"); |
| 23764 | break; |
| 23765 | case CC_BLANK: |
| 23766 | reg_addrange_str(preg, " \t"); |
| 23767 | break; |
| 23768 | case CC_UPPER: |
| 23769 | reg_addrange(preg, 'A', 'Z'); |
| 23770 | break; |
| 23771 | case CC_LOWER: |
| 23772 | reg_addrange(preg, 'a', 'z'); |
| 23773 | break; |
| 23774 | case CC_XDIGIT: |
| 23775 | reg_addrange(preg, 'a', 'f'); |
| 23776 | reg_addrange(preg, 'A', 'F'); |
| 23777 | |
| 23778 | case CC_DIGIT: |
| 23779 | reg_addrange(preg, '0', '9'); |
| 23780 | break; |
| 23781 | case CC_CNTRL: |
| 23782 | reg_addrange(preg, 0, 31); |
| 23783 | reg_addrange(preg, 127, 127); |
| 23784 | break; |
| 23785 | case CC_PRINT: |
| 23786 | reg_addrange(preg, ' ', '~'); |
| 23787 | break; |
| 23788 | case CC_GRAPH: |
| 23789 | reg_addrange(preg, '!', '~'); |
| 23790 | break; |
| 23791 | case CC_PUNCT: |
| 23792 | reg_addrange(preg, '!', '/'); |
| 23793 | reg_addrange(preg, ':', '@'); |
| 23794 | reg_addrange(preg, '[', '`'); |
| 23795 | reg_addrange(preg, '{', '~'); |
| 23796 | break; |
| 23797 | } |
| 23798 | continue; |
| 23799 | } |
| 23800 | } |
| 23801 | |
| 23802 | reg_addrange(preg, start, start); |
| 23803 | } |
| 23804 | regc(preg, '\0'); |
| 23805 | |
| 23806 | if (*pattern) { |
| 23807 | pattern++; |
| 23808 | } |
| 23809 | preg->regparse = pattern; |
| 23810 | |
| 23811 | *flagp |= HASWIDTH|SIMPLE; |
| 23812 | } |
| 23813 | break; |
| 23814 | case '(': |
| 23815 | ret = reg(preg, 1, &flags); |
| 23816 | if (ret == 0) |
| 23817 | return 0; |
| 23818 | *flagp |= flags&(HASWIDTH|SPSTART); |
| 23819 | break; |
| 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; |
| 23830 | return 0; |
| 23831 | case 'A': |
| 23832 | ret = regnode(preg, BOLX); |
| 23833 | break; |
| 23834 | case 'Z': |
| 23835 | ret = regnode(preg, EOLX); |
| 23836 | break; |
| 23837 | case '<': |
| 23838 | case 'm': |
| 23839 | ret = regnode(preg, WORDA); |
| 23840 | break; |
| 23841 | case '>': |
| 23842 | case 'M': |
| 23843 | ret = regnode(preg, WORDZ); |
| 23844 | break; |
| 23845 | case 'd': |
| 23846 | case 'D': |
| 23847 | ret = regnode(preg, ch == 'd' ? ANYOF : ANYBUT); |
| 23848 | reg_addrange(preg, '0', '9'); |
| 23849 | regc(preg, '\0'); |
| 23850 | *flagp |= HASWIDTH|SIMPLE; |
| 23851 | break; |
| 23852 | case 'w': |
| 23853 | case 'W': |
| 23854 | ret = regnode(preg, ch == 'w' ? ANYOF : ANYBUT); |
| 23855 | if ((preg->cflags & REG_ICASE) == 0) { |
| 23856 | reg_addrange(preg, 'a', 'z'); |
| 23857 | } |
| 23858 | reg_addrange(preg, 'A', 'Z'); |
| 23859 | reg_addrange(preg, '0', '9'); |
| 23860 | reg_addrange(preg, '_', '_'); |
| 23861 | regc(preg, '\0'); |
| 23862 | *flagp |= HASWIDTH|SIMPLE; |
| 23863 | break; |
| 23864 | case 's': |
| 23865 | case 'S': |
| 23866 | ret = regnode(preg, ch == 's' ? ANYOF : ANYBUT); |
| 23867 | reg_addrange_str(preg," \t\r\n\f\v"); |
| 23868 | regc(preg, '\0'); |
| 23869 | *flagp |= HASWIDTH|SIMPLE; |
| 23870 | break; |
| 23871 | |
| 23872 | default: |
| 23873 | |
| 23874 | |
| 23875 | preg->regparse--; |
| 23876 | goto de_fault; |
| 23877 | } |
| 23878 | break; |
| 23879 | de_fault: |
| 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; |
| 23901 | } |
| 23902 | n += reg_decode_escape(preg->regparse + n, &ch); |
| 23903 | if (ch == 0) { |
| 23904 | preg->err = REG_ERR_NULL_CHAR; |
| 23905 | return 0; |
| 23906 | } |
| 23907 | } |
| 23908 | |
| 23909 | |
| 23910 | if (str_is_mult(&preg->regparse[n])) { |
| 23911 | |
| 23912 | if (added) { |
| 23913 | |
| 23914 | break; |
| 23915 | } |
| 23916 | |
| 23917 | regc(preg, ch); |
| 23918 | added++; |
| 23919 | preg->regparse += n; |
| 23920 | break; |
| 23921 | } |
| 23922 | |
| 23923 | |
| 23924 | regc(preg, ch); |
| 23925 | added++; |
| 23926 | preg->regparse += n; |
| 23927 | } |
| 23928 | regc(preg, '\0'); |
| 23929 | |
| 23930 | *flagp |= HASWIDTH; |
| 23931 | if (added == 1) |
| 23932 | *flagp |= SIMPLE; |
| 23933 | break; |
| 23934 | } |
| 23935 | break; |
| 23936 | } |
| 23937 | |
| 23938 | return(ret); |
| 23939 | } |
| 23940 | |
| 23941 | static void reg_grow(regex_t *preg, int n) |
| 23942 | { |
| 23943 | if (preg->p + n >= preg->proglen) { |
| 23944 | preg->proglen = (preg->p + n) * 2; |
| 23945 | preg->program = realloc(preg->program, preg->proglen * sizeof(int)); |
| 23946 | } |
| 23947 | } |
| 23948 | |
| 23949 | |
| 23950 | static int regnode(regex_t *preg, int op) |
| 23951 | { |
| 23952 | reg_grow(preg, 2); |
| 23953 | |
| 23954 | |
| 23955 | preg->program[preg->p++] = op; |
| 23956 | preg->program[preg->p++] = 0; |
| 23957 | |
| 23958 | |
| 23959 | return preg->p - 2; |
| 23960 | } |
| 23961 | |
| 23962 | static void regc(regex_t *preg, int b ) |
| 23963 | { |
| 23964 | reg_grow(preg, 1); |
| 23965 | preg->program[preg->p++] = b; |
| 23966 | } |
| 23967 | |
| 23968 | static int reginsert(regex_t *preg, int op, int size, int opnd ) |
| 23969 | { |
| 23970 | reg_grow(preg, size); |
| 23971 | |
| 23972 | |
| 23973 | memmove(preg->program + opnd + size, preg->program + opnd, sizeof(int) * (preg->p - opnd)); |
| 23974 | |
| 23975 | memset(preg->program + opnd, 0, sizeof(int) * size); |
| 23976 | |
| 23977 | preg->program[opnd] = op; |
| 23978 | |
| 23979 | preg->p += size; |
| 23980 | |
| 23981 | return opnd + size; |
| 23982 | } |
| 23983 | |
| 23984 | static void regtail(regex_t *preg, int p, int val) |
| 23985 | { |
| 23986 | int scan; |
| 23987 | int temp; |
| 23988 | int offset; |
| 23989 | |
| 23990 | |
| 23991 | scan = p; |
| 23992 | for (;;) { |
| 23993 | temp = regnext(preg, scan); |
| 23994 | if (temp == 0) |
| 23995 | break; |
| 23996 | scan = temp; |
| 23997 | } |
| 23998 | |
| 23999 | if (OP(preg, scan) == BACK) |
| 24000 | offset = scan - val; |
| 24001 | else |
| 24002 | offset = val - scan; |
| 24003 | |
| 24004 | preg->program[scan + 1] = offset; |
| 24005 | } |
| 24006 | |
| 24007 | |
| 24008 | static void regoptail(regex_t *preg, int p, int val ) |
| 24009 | { |
| 24010 | |
| 24011 | if (p != 0 && OP(preg, p) == BRANCH) { |
| 24012 | regtail(preg, OPERAND(p), val); |
| 24013 | } |
| 24014 | } |
| 24015 | |
| 24016 | |
| 24017 | static int regtry(regex_t *preg, const char *string ); |
| 24018 | static int regmatch(regex_t *preg, int prog); |
| 24019 | static int regrepeat(regex_t *preg, int p, int max); |
| 24020 | |
| 24021 | int jim_regexec(regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags) |
| 24022 | { |
| 24023 | const char *s; |
| 24024 | int scan; |
| 24025 | |
| 24026 | |
| 24027 | if (preg == NULL || preg->program == NULL || string == NULL) { |
| 24028 | return REG_ERR_NULL_ARGUMENT; |
| 24029 | } |
| 24030 | |
| 24031 | |
| 24032 | if (*preg->program != REG_MAGIC) { |
| 24033 | return REG_ERR_CORRUPTED; |
| 24034 | } |
| 24035 | |
| 24036 | #ifdef DEBUG |
| 24037 | fprintf(stderr, "regexec: %s\n", string); |
| 24038 | regdump(preg); |
| 24039 | #endif |
| 24040 | |
| 24041 | preg->eflags = eflags; |
| 24042 | preg->pmatch = pmatch; |
| 24043 | preg->nmatch = nmatch; |
| 24044 | preg->start = string; |
| 24045 | |
| 24046 | |
| 24047 | for (scan = OPERAND(1); scan != 0; scan += regopsize(preg, scan)) { |
| 24048 | int op = OP(preg, scan); |
| 24049 | if (op == END) |
| 24050 | break; |
| 24051 | if (op == REPX || op == REPXMIN) |
| 24052 | preg->program[scan + 4] = 0; |
| 24053 | } |
| 24054 | |
| 24055 | |
| 24056 | if (preg->regmust != 0) { |
| 24057 | s = string; |
| 24058 | while ((s = str_find(s, preg->program[preg->regmust], preg->cflags & REG_ICASE)) != NULL) { |
| 24059 | if (prefix_cmp(preg->program + preg->regmust, preg->regmlen, s, preg->cflags & REG_ICASE) >= 0) { |
| 24060 | break; |
| 24061 | } |
| 24062 | s++; |
| 24063 | } |
| 24064 | if (s == NULL) |
| 24065 | return REG_NOMATCH; |
| 24066 | } |
| 24067 | |
| 24068 | |
| 24069 | preg->regbol = string; |
| 24070 | |
| 24071 | |
| 24072 | if (preg->reganch) { |
| 24073 | if (eflags & REG_NOTBOL) { |
| 24074 | |
| 24075 | goto nextline; |
| 24076 | } |
| 24077 | while (1) { |
| 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; |
| 24089 | } |
| 24090 | } |
| 24091 | } |
| 24092 | return REG_NOMATCH; |
| 24093 | } |
| 24094 | } |
| 24095 | |
| 24096 | |
| 24097 | s = string; |
| 24098 | if (preg->regstart != '\0') { |
| 24099 | |
| 24100 | while ((s = str_find(s, preg->regstart, preg->cflags & REG_ICASE)) != NULL) { |
| 24101 | if (regtry(preg, s)) |
| 24102 | return REG_NOERROR; |
| 24103 | s++; |
| 24104 | } |
| 24105 | } |
| 24106 | else |
| 24107 | |
| 24108 | while (1) { |
| 24109 | if (regtry(preg, s)) |
| 24110 | return REG_NOERROR; |
| 24111 | if (*s == '\0') { |
| 24112 | break; |
| 24113 | } |
| 24114 | else { |
| 24115 | int c; |
| 24116 | s += utf8_tounicode(s, &c); |
| 24117 | } |
| 24118 | } |
| 24119 | |
| 24120 | |
| 24121 | return REG_NOMATCH; |
| 24122 | } |
| 24123 | |
| 24124 | |
| 24125 | static int regtry( regex_t *preg, const char *string ) |
| 24126 | { |
| 24127 | int i; |
| 24128 | |
| 24129 | preg->reginput = string; |
| 24130 | |
| 24131 | for (i = 0; i < preg->nmatch; i++) { |
| 24132 | preg->pmatch[i].rm_so = -1; |
| 24133 | preg->pmatch[i].rm_eo = -1; |
| 24134 | } |
| 24135 | if (regmatch(preg, 1)) { |
| 24136 | preg->pmatch[0].rm_so = string - preg->start; |
| 24137 | preg->pmatch[0].rm_eo = preg->reginput - preg->start; |
| 24138 | return(1); |
| 24139 | } else |
| 24140 | return(0); |
| 24141 | } |
| 24142 | |
| 24143 | static int prefix_cmp(const int *prog, int proglen, const char *string, int nocase) |
| 24144 | { |
| 24145 | const char *s = string; |
| 24146 | while (proglen && *s) { |
| 24147 | int ch; |
| 24148 | int n = reg_utf8_tounicode_case(s, &ch, nocase); |
| 24149 | if (ch != *prog) { |
| 24150 | return -1; |
| 24151 | } |
| 24152 | prog++; |
| 24153 | s += n; |
| 24154 | proglen--; |
| 24155 | } |
| 24156 | if (proglen == 0) { |
| 24157 | return s - string; |
| 24158 | } |
| 24159 | return -1; |
| 24160 | } |
| 24161 | |
| 24162 | static int reg_range_find(const int *range, int c) |
| 24163 | { |
| 24164 | while (*range) { |
| 24165 | |
| 24166 | if (c >= range[1] && c <= (range[0] + range[1] - 1)) { |
| 24167 | return 1; |
| 24168 | } |
| 24169 | range += 2; |
| 24170 | } |
| 24171 | return 0; |
| 24172 | } |
| 24173 | |
| 24174 | static const char *str_find(const char *string, int c, int nocase) |
| 24175 | { |
| 24176 | if (nocase) { |
| 24177 | |
| 24178 | c = utf8_upper(c); |
| 24179 | } |
| 24180 | while (*string) { |
| 24181 | int ch; |
| 24182 | int n = reg_utf8_tounicode_case(string, &ch, nocase); |
| 24183 | if (c == ch) { |
| 24184 | return string; |
| 24185 | } |
| 24186 | string += n; |
| 24187 | } |
| 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 | } |
| 24199 | } |
| 24200 | |
| 24201 | static int regmatchsimplerepeat(regex_t *preg, int scan, int matchmin) |
| 24202 | { |
| 24203 | int nextch = '\0'; |
| 24204 | const char *save; |
| 24205 | int no; |
| 24206 | int c; |
| 24207 | |
| 24208 | int max = preg->program[scan + 2]; |
| 24209 | int min = preg->program[scan + 3]; |
| 24210 | int next = regnext(preg, scan); |
| 24211 | |
| 24212 | if (OP(preg, next) == EXACTLY) { |
| 24213 | nextch = preg->program[OPERAND(next)]; |
| 24214 | } |
| 24215 | save = preg->reginput; |
| 24216 | no = regrepeat(preg, scan + 5, max); |
| 24217 | if (no < min) { |
| 24218 | return 0; |
| 24219 | } |
| 24220 | if (matchmin) { |
| 24221 | |
| 24222 | max = no; |
| 24223 | no = min; |
| 24224 | } |
| 24225 | |
| 24226 | while (1) { |
| 24227 | if (matchmin) { |
| 24228 | if (no > max) { |
| 24229 | break; |
| 24230 | } |
| 24231 | } |
| 24232 | else { |
| 24233 | if (no < min) { |
| 24234 | break; |
| 24235 | } |
| 24236 | } |
| 24237 | preg->reginput = save + utf8_index(save, no); |
| 24238 | reg_utf8_tounicode_case(preg->reginput, &c, (preg->cflags & REG_ICASE)); |
| 24239 | |
| 24240 | if (reg_iseol(preg, nextch) || c == nextch) { |
| 24241 | if (regmatch(preg, next)) { |
| 24242 | return(1); |
| 24243 | } |
| 24244 | } |
| 24245 | if (matchmin) { |
| 24246 | |
| 24247 | no++; |
| 24248 | } |
| 24249 | else { |
| 24250 | |
| 24251 | no--; |
| 24252 | } |
| 24253 | } |
| 24254 | return(0); |
| 24255 | } |
| 24256 | |
| 24257 | static int regmatchrepeat(regex_t *preg, int scan, int matchmin) |
| 24258 | { |
| 24259 | int *scanpt = preg->program + scan; |
| 24260 | |
| 24261 | int max = scanpt[2]; |
| 24262 | int min = scanpt[3]; |
| 24263 | |
| 24264 | |
| 24265 | if (scanpt[4] < min) { |
| 24266 | |
| 24267 | scanpt[4]++; |
| 24268 | if (regmatch(preg, scan + 5)) { |
| 24269 | return 1; |
| 24270 | } |
| 24271 | scanpt[4]--; |
| 24272 | return 0; |
| 24273 | } |
| 24274 | if (scanpt[4] > max) { |
| 24275 | return 0; |
| 24276 | } |
| 24277 | |
| 24278 | if (matchmin) { |
| 24279 | |
| 24280 | if (regmatch(preg, regnext(preg, scan))) { |
| 24281 | return 1; |
| 24282 | } |
| 24283 | |
| 24284 | scanpt[4]++; |
| 24285 | if (regmatch(preg, scan + 5)) { |
| 24286 | return 1; |
| 24287 | } |
| 24288 | scanpt[4]--; |
| 24289 | return 0; |
| 24290 | } |
| 24291 | |
| 24292 | if (scanpt[4] < max) { |
| 24293 | scanpt[4]++; |
| 24294 | if (regmatch(preg, scan + 5)) { |
| 24295 | return 1; |
| 24296 | } |
| 24297 | scanpt[4]--; |
| 24298 | } |
| 24299 | |
| 24300 | return regmatch(preg, regnext(preg, scan)); |
| 24301 | } |
| 24302 | |
| 24303 | |
| 24304 | static int regmatch(regex_t *preg, int prog) |
| 24305 | { |
| 24306 | int scan; |
| 24307 | int next; |
| 24308 | const char *save; |
| 24309 | |
| 24310 | scan = prog; |
| 24311 | |
| 24312 | #ifdef DEBUG |
| 24313 | if (scan != 0 && regnarrate) |
| 24314 | fprintf(stderr, "%s(\n", regprop(scan)); |
| 24315 | #endif |
| 24316 | while (scan != 0) { |
| 24317 | int n; |
| 24318 | int c; |
| 24319 | #ifdef DEBUG |
| 24320 | if (regnarrate) { |
| 24321 | fprintf(stderr, "%3d: %s...\n", scan, regprop(OP(preg, scan))); |
| 24322 | } |
| 24323 | #endif |
| 24324 | next = regnext(preg, scan); |
| 24325 | n = reg_utf8_tounicode_case(preg->reginput, &c, (preg->cflags & REG_ICASE)); |
| 24326 | |
| 24327 | switch (OP(preg, scan)) { |
| 24328 | case BOLX: |
| 24329 | if ((preg->eflags & REG_NOTBOL)) { |
| 24330 | return(0); |
| 24331 | } |
| 24332 | |
| 24333 | case BOL: |
| 24334 | if (preg->reginput != preg->regbol) { |
| 24335 | return(0); |
| 24336 | } |
| 24337 | break; |
| 24338 | case EOLX: |
| 24339 | if (c != 0) { |
| 24340 | |
| 24341 | return 0; |
| 24342 | } |
| 24343 | break; |
| 24344 | case EOL: |
| 24345 | if (!reg_iseol(preg, c)) { |
| 24346 | return(0); |
| 24347 | } |
| 24348 | break; |
| 24349 | case WORDA: |
| 24350 | |
| 24351 | if ((!isalnum(UCHAR(c))) && c != '_') |
| 24352 | return(0); |
| 24353 | |
| 24354 | if (preg->reginput > preg->regbol && |
| 24355 | (isalnum(UCHAR(preg->reginput[-1])) || preg->reginput[-1] == '_')) |
| 24356 | return(0); |
| 24357 | break; |
| 24358 | case WORDZ: |
| 24359 | |
| 24360 | if (preg->reginput > preg->regbol) { |
| 24361 | |
| 24362 | if (reg_iseol(preg, c) || !(isalnum(UCHAR(c)) || c == '_')) { |
| 24363 | c = preg->reginput[-1]; |
| 24364 | |
| 24365 | if (isalnum(UCHAR(c)) || c == '_') { |
| 24366 | break; |
| 24367 | } |
| 24368 | } |
| 24369 | } |
| 24370 | |
| 24371 | return(0); |
| 24372 | |
| 24373 | case ANY: |
| 24374 | if (reg_iseol(preg, c)) |
| 24375 | return 0; |
| 24376 | preg->reginput += n; |
| 24377 | break; |
| 24378 | case EXACTLY: { |
| 24379 | int opnd; |
| 24380 | int len; |
| 24381 | int slen; |
| 24382 | |
| 24383 | opnd = OPERAND(scan); |
| 24384 | len = str_int_len(preg->program + opnd); |
| 24385 | |
| 24386 | slen = prefix_cmp(preg->program + opnd, len, preg->reginput, preg->cflags & REG_ICASE); |
| 24387 | if (slen < 0) { |
| 24388 | return(0); |
| 24389 | } |
| 24390 | preg->reginput += slen; |
| 24391 | } |
| 24392 | break; |
| 24393 | case ANYOF: |
| 24394 | if (reg_iseol(preg, c) || reg_range_find(preg->program + OPERAND(scan), c) == 0) { |
| 24395 | return(0); |
| 24396 | } |
| 24397 | preg->reginput += n; |
| 24398 | break; |
| 24399 | case ANYBUT: |
| 24400 | if (reg_iseol(preg, c) || reg_range_find(preg->program + OPERAND(scan), c) != 0) { |
| 24401 | return(0); |
| 24402 | } |
| 24403 | preg->reginput += n; |
| 24404 | break; |
| 24405 | case NOTHING: |
| 24406 | break; |
| 24407 | case BACK: |
| 24408 | break; |
| 24409 | case BRANCH: |
| 24410 | if (OP(preg, next) != BRANCH) |
| 24411 | next = OPERAND(scan); |
| 24412 | else { |
| 24413 | do { |
| 24414 | save = preg->reginput; |
| 24415 | if (regmatch(preg, OPERAND(scan))) { |
| 24416 | return(1); |
| 24417 | } |
| 24418 | preg->reginput = save; |
| 24419 | scan = regnext(preg, scan); |
| 24420 | } while (scan != 0 && OP(preg, scan) == BRANCH); |
| 24421 | return(0); |
| 24422 | |
| 24423 | } |
| 24424 | break; |
| 24425 | case REP: |
| 24426 | case REPMIN: |
| 24427 | return regmatchsimplerepeat(preg, scan, OP(preg, scan) == REPMIN); |
| 24428 | |
| 24429 | case REPX: |
| 24430 | case REPXMIN: |
| 24431 | return regmatchrepeat(preg, scan, OP(preg, scan) == REPXMIN); |
| 24432 | |
| 24433 | case END: |
| 24434 | return 1; |
| 24435 | |
| 24436 | case OPENNC: |
| 24437 | case CLOSENC: |
| 24438 | return regmatch(preg, next); |
| 24439 | |
| 24440 | default: |
| 24441 | if (OP(preg, scan) >= OPEN+1 && OP(preg, scan) < CLOSE_END) { |
| 24442 | save = preg->reginput; |
| 24443 | if (regmatch(preg, next)) { |
| 24444 | if (OP(preg, scan) < CLOSE) { |
| 24445 | int no = OP(preg, scan) - OPEN; |
| 24446 | if (no < preg->nmatch && preg->pmatch[no].rm_so == -1) { |
| 24447 | preg->pmatch[no].rm_so = save - preg->start; |
| 24448 | } |
| 24449 | } |
| 24450 | else { |
| 24451 | int no = OP(preg, scan) - CLOSE; |
| 24452 | if (no < preg->nmatch && preg->pmatch[no].rm_eo == -1) { |
| 24453 | preg->pmatch[no].rm_eo = save - preg->start; |
| 24454 | } |
| 24455 | } |
| 24456 | return(1); |
| 24457 | } |
| 24458 | |
| 24459 | preg->reginput = save; |
| 24460 | return(0); |
| 24461 | } |
| 24462 | return REG_ERR_INTERNAL; |
| 24463 | } |
| 24464 | |
| 24465 | scan = next; |
| 24466 | } |
| 24467 | |
| 24468 | return REG_ERR_INTERNAL; |
| 24469 | } |
| 24470 | |
| 24471 | static int regrepeat(regex_t *preg, int p, int max) |
| 24472 | { |
| 24473 | int count = 0; |
| 24474 | const char *scan; |
| 24475 | int opnd; |
| 24476 | int ch; |
| 24477 | int n; |
| 24478 | |
| 24479 | scan = preg->reginput; |
| 24480 | opnd = OPERAND(p); |
| 24481 | switch (OP(preg, p)) { |
| 24482 | case ANY: |
| 24483 | while (!reg_iseol(preg, *scan) && count < max) { |
| 24484 | count++; |
| 24485 | scan += utf8_charlen(*scan); |
| 24486 | } |
| 24487 | break; |
| 24488 | case EXACTLY: |
| 24489 | while (count < max) { |
| 24490 | n = reg_utf8_tounicode_case(scan, &ch, preg->cflags & REG_ICASE); |
| 24491 | if (preg->program[opnd] != ch) { |
| 24492 | break; |
| 24493 | } |
| 24494 | count++; |
| 24495 | scan += n; |
| 24496 | } |
| 24497 | break; |
| 24498 | case ANYOF: |
| 24499 | while (count < max) { |
| 24500 | n = reg_utf8_tounicode_case(scan, &ch, preg->cflags & REG_ICASE); |
| 24501 | if (reg_iseol(preg, ch) || reg_range_find(preg->program + opnd, ch) == 0) { |
| 24502 | break; |
| 24503 | } |
| 24504 | count++; |
| 24505 | scan += n; |
| 24506 | } |
| 24507 | break; |
| 24508 | case ANYBUT: |
| 24509 | while (count < max) { |
| 24510 | n = reg_utf8_tounicode_case(scan, &ch, preg->cflags & REG_ICASE); |
| 24511 | if (reg_iseol(preg, ch) || reg_range_find(preg->program + opnd, ch) != 0) { |
| 24512 | break; |
| 24513 | } |
| 24514 | count++; |
| 24515 | scan += n; |
| 24516 | } |
| 24517 | break; |
| 24518 | default: |
| 24519 | preg->err = REG_ERR_INTERNAL; |
| 24520 | count = 0; |
| 24521 | break; |
| 24522 | } |
| 24523 | preg->reginput = scan; |
| 24524 | |
| 24525 | return(count); |
| 24526 | } |
| 24527 | |
| 24528 | static int regnext(regex_t *preg, int p ) |
| 24529 | { |
| 24530 | int offset; |
| 24531 | |
| 24532 | offset = NEXT(preg, p); |
| 24533 | |
| 24534 | if (offset == 0) |
| 24535 | return 0; |
| 24536 | |
| 24537 | if (OP(preg, p) == BACK) |
| 24538 | return(p-offset); |
| 24539 | else |
| 24540 | return(p+offset); |
| 24541 | } |
| 24542 | |
| 24543 | static int regopsize(regex_t *preg, int p ) |
| 24544 | { |
| 24545 | |
| 24546 | switch (OP(preg, p)) { |
| 24547 | case REP: |
| 24548 | case REPMIN: |
| 24549 | case REPX: |
| 24550 | case REPXMIN: |
| 24551 | return 5; |
| 24552 | |
| 24553 | case ANYOF: |
| 24554 | case ANYBUT: |
| 24555 | case EXACTLY: { |
| 24556 | int s = p + 2; |
| 24557 | while (preg->program[s++]) { |
| 24558 | } |
| 24559 | return s - p; |
| 24560 | } |
| 24561 | } |
| 24562 | return 2; |
| 24563 | } |
| 24564 | |
| 24565 | |
| 24566 | size_t jim_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size) |
| 24567 | { |
| 24568 | static const char *error_strings[] = { |
| 24569 | "success", |
| 24570 | "no match", |
| 24571 | "bad pattern", |
| 24572 | "null argument", |
| 24573 | "unknown error", |
| 24574 | "too big", |
| 24575 | "out of memory", |
| 24576 | "too many ()", |
| 24577 | "parentheses () not balanced", |
| 24578 | "braces {} not balanced", |
| 24579 | "invalid repetition count(s)", |
| 24580 | "extra characters", |
| 24581 | "*+ of empty atom", |
| 24582 | "nested count", |
| 24583 | "internal error", |
| 24584 | "count follows nothing", |
| 24585 | "invalid escape \\ sequence", |
| 24586 | "corrupted program", |
| 24587 | "contains null char", |
| 24588 | "brackets [] not balanced", |
| 24589 | }; |
| 24590 | const char *err; |
| 24591 | |
| 24592 | if (errcode < 0 || errcode >= REG_ERR_NUM) { |
| 24593 | err = "Bad error code"; |
| 24594 | } |
| 24595 | else { |
| 24596 | err = error_strings[errcode]; |
| 24597 | } |
| 24598 | |
| 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> |
| 24610 | |
| 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()) { |
| 24622 | case ERROR_FILE_NOT_FOUND: return ENOENT; |
| 24623 | case ERROR_PATH_NOT_FOUND: return ENOENT; |
| 24624 | case ERROR_TOO_MANY_OPEN_FILES: return EMFILE; |
| 24625 | case ERROR_ACCESS_DENIED: return EACCES; |
| 24626 | case ERROR_INVALID_HANDLE: return EBADF; |
| 24627 | case ERROR_BAD_ENVIRONMENT: return E2BIG; |
| 24628 | case ERROR_BAD_FORMAT: return ENOEXEC; |
| 24629 | case ERROR_INVALID_ACCESS: return EACCES; |
| 24630 | case ERROR_INVALID_DRIVE: return ENOENT; |
| 24631 | case ERROR_CURRENT_DIRECTORY: return EACCES; |
| 24632 | case ERROR_NOT_SAME_DEVICE: return EXDEV; |
| 24633 | case ERROR_NO_MORE_FILES: return ENOENT; |
| 24634 | case ERROR_WRITE_PROTECT: return EROFS; |
| 24635 | case ERROR_BAD_UNIT: return ENXIO; |
| 24636 | case ERROR_NOT_READY: return EBUSY; |
| 24637 | case ERROR_BAD_COMMAND: return EIO; |
| 24638 | case ERROR_CRC: return EIO; |
| 24639 | case ERROR_BAD_LENGTH: return EIO; |
| 24640 | case ERROR_SEEK: return EIO; |
| 24641 | case ERROR_WRITE_FAULT: return EIO; |
| 24642 | case ERROR_READ_FAULT: return EIO; |
| 24643 | case ERROR_GEN_FAILURE: return EIO; |
| 24644 | case ERROR_SHARING_VIOLATION: return EACCES; |
| 24645 | case ERROR_LOCK_VIOLATION: return EACCES; |
| 24646 | case ERROR_SHARING_BUFFER_EXCEEDED: return ENFILE; |
| 24647 | case ERROR_HANDLE_DISK_FULL: return ENOSPC; |
| 24648 | case ERROR_NOT_SUPPORTED: return ENODEV; |
| 24649 | case ERROR_REM_NOT_LIST: return EBUSY; |
| 24650 | case ERROR_DUP_NAME: return EEXIST; |
| 24651 | case ERROR_BAD_NETPATH: return ENOENT; |
| 24652 | case ERROR_NETWORK_BUSY: return EBUSY; |
| 24653 | case ERROR_DEV_NOT_EXIST: return ENODEV; |
| 24654 | case ERROR_TOO_MANY_CMDS: return EAGAIN; |
| 24655 | case ERROR_ADAP_HDW_ERR: return EIO; |
| 24656 | case ERROR_BAD_NET_RESP: return EIO; |
| 24657 | case ERROR_UNEXP_NET_ERR: return EIO; |
| 24658 | case ERROR_NETNAME_DELETED: return ENOENT; |
| 24659 | case ERROR_NETWORK_ACCESS_DENIED: return EACCES; |
| 24660 | case ERROR_BAD_DEV_TYPE: return ENODEV; |
| 24661 | case ERROR_BAD_NET_NAME: return ENOENT; |
| 24662 | case ERROR_TOO_MANY_NAMES: return ENFILE; |
| 24663 | case ERROR_TOO_MANY_SESS: return EIO; |
| 24664 | case ERROR_SHARING_PAUSED: return EAGAIN; |
| 24665 | case ERROR_REDIR_PAUSED: return EAGAIN; |
| 24666 | case ERROR_FILE_EXISTS: return EEXIST; |
| 24667 | case ERROR_CANNOT_MAKE: return ENOSPC; |
| 24668 | case ERROR_OUT_OF_STRUCTURES: return ENFILE; |
| 24669 | case ERROR_ALREADY_ASSIGNED: return EEXIST; |
| 24670 | case ERROR_INVALID_PASSWORD: return EPERM; |
| 24671 | case ERROR_NET_WRITE_FAULT: return EIO; |
| 24672 | case ERROR_NO_PROC_SLOTS: return EAGAIN; |
| 24673 | case ERROR_DISK_CHANGE: return EXDEV; |
| 24674 | case ERROR_BROKEN_PIPE: return EPIPE; |
| 24675 | case ERROR_OPEN_FAILED: return ENOENT; |
| 24676 | case ERROR_DISK_FULL: return ENOSPC; |
| 24677 | case ERROR_NO_MORE_SEARCH_HANDLES: return EMFILE; |
| 24678 | case ERROR_INVALID_TARGET_HANDLE: return EBADF; |
| 24679 | case ERROR_INVALID_NAME: return ENOENT; |
| 24680 | case ERROR_PROC_NOT_FOUND: return ESRCH; |
| 24681 | case ERROR_WAIT_NO_CHILDREN: return ECHILD; |
| 24682 | case ERROR_CHILD_NOT_COMPLETE: return ECHILD; |
| 24683 | case ERROR_DIRECT_ACCESS_HANDLE: return EBADF; |
| 24684 | case ERROR_SEEK_ON_DEVICE: return ESPIPE; |
| 24685 | case ERROR_BUSY_DRIVE: return EAGAIN; |
| 24686 | case ERROR_DIR_NOT_EMPTY: return EEXIST; |
| 24687 | case ERROR_NOT_LOCKED: return EACCES; |
| 24688 | case ERROR_BAD_PATHNAME: return ENOENT; |
| 24689 | case ERROR_LOCK_FAILED: return EACCES; |
| 24690 | case ERROR_ALREADY_EXISTS: return EEXIST; |
| 24691 | case ERROR_FILENAME_EXCED_RANGE: return ENAMETOOLONG; |
| 24692 | case ERROR_BAD_PIPE: return EPIPE; |
| 24693 | case ERROR_PIPE_BUSY: return EAGAIN; |
| 24694 | case ERROR_PIPE_NOT_CONNECTED: return EPIPE; |
| 24695 | case ERROR_DIRECTORY: return ENOTDIR; |
| 24696 | } |
| 24697 | return EINVAL; |
| 24698 | } |
| 24699 | |
| 24700 | long JimProcessPid(phandle_t pid) |
| 24701 | { |
| 24702 | if (pid == INVALID_HANDLE_VALUE) { |
| 24703 | return -1; |
| 24704 | } |
| 24705 | return GetProcessId(pid); |
| 24706 | } |
| 24707 | |
| 24708 | phandle_t JimWaitPid(long pid, int *status, int nohang) |
| 24709 | { |
| 24710 | if (pid > 0) { |
| 24711 | HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION | SYNCHRONIZE, FALSE, pid); |
| 24712 | if (h) { |
| 24713 | long pid = waitpid(h, status, nohang); |
| 24714 | CloseHandle(h); |
| 24715 | if (pid > 0) { |
| 24716 | return h; |
| 24717 | } |
| 24718 | } |
| 24719 | } |
| 24720 | return JIM_BAD_PHANDLE; |
| 24721 | } |
| 24722 | |
| 24723 | long waitpid(phandle_t phandle, int *status, int nohang) |
| 24724 | { |
| 24725 | long pid; |
| 24726 | DWORD ret = WaitForSingleObject(phandle, nohang ? 0 : INFINITE); |
| 24727 | if (ret == WAIT_TIMEOUT || ret == WAIT_FAILED) { |
| 24728 | |
| 24729 | return -1; |
| 24730 | } |
| 24731 | GetExitCodeProcess(phandle, &ret); |
| 24732 | *status = ret; |
| 24733 | |
| 24734 | pid = GetProcessId(phandle); |
| 24735 | CloseHandle(phandle); |
| 24736 | return pid; |
| 24737 | } |
| 24738 | |
| 24739 | int Jim_MakeTempFile(Jim_Interp *interp, const char *filename_template, int unlink_file) |
| 24740 | { |
| 24741 | char name[MAX_PATH]; |
| 24742 | HANDLE handle; |
| 24743 | |
| 24744 | if (!GetTempPath(MAX_PATH, name) || !GetTempFileName(name, filename_template ? filename_template : "JIM", 0, name)) { |
| 24745 | return -1; |
| 24746 | } |
| 24747 | |
| 24748 | handle = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, |
| 24749 | CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY | (unlink_file ? FILE_FLAG_DELETE_ON_CLOSE : 0), |
| 24750 | NULL); |
| 24751 | |
| 24752 | if (handle == INVALID_HANDLE_VALUE) { |
| 24753 | goto error; |
| 24754 | } |
| 24755 | |
| 24756 | Jim_SetResultString(interp, name, -1); |
| 24757 | return _open_osfhandle((intptr_t)handle, _O_RDWR | _O_TEXT); |
| 24758 | |
| 24759 | error: |
| 24760 | Jim_SetResultErrno(interp, name); |
| 24761 | DeleteFile(name); |
| 24762 | return -1; |
| 24763 | } |
| 24764 | |
| 24765 | int Jim_OpenForWrite(const char *filename, int append) |
| 24766 | { |
| 24767 | if (strcmp(filename, "/dev/null") == 0) { |
| 24768 | filename = "nul:"; |
| 24769 | } |
| 24770 | int fd = _open(filename, _O_WRONLY | _O_CREAT | _O_TEXT | (append ? _O_APPEND : _O_TRUNC), _S_IREAD | _S_IWRITE); |
| 24771 | if (fd >= 0 && append) { |
| 24772 | |
| 24773 | _lseek(fd, 0L, SEEK_END); |
| 24774 | } |
| 24775 | return fd; |
| 24776 | } |
| 24777 | |
| 24778 | int Jim_OpenForRead(const char *filename) |
| 24779 | { |
| 24780 | if (strcmp(filename, "/dev/null") == 0) { |
| 24781 | filename = "nul:"; |
| 24782 | } |
| 24783 | return _open(filename, _O_RDONLY | _O_TEXT, 0); |
| 24784 | } |
| 24785 | |
| 24786 | #elif defined(HAVE_UNISTD_H) |
| 24787 | |
| 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) { |
| 24801 | tmpdir = "/tmp/"; |
| 24802 | } |
| 24803 | filenameObj = Jim_NewStringObj(interp, tmpdir, -1); |
| 24804 | if (tmpdir[0] && tmpdir[strlen(tmpdir) - 1] != '/') { |
| 24805 | Jim_AppendString(interp, filenameObj, "/", 1); |
| 24806 | } |
| 24807 | Jim_AppendString(interp, filenameObj, "tcl.tmp.XXXXXX", -1); |
| 24808 | } |
| 24809 | else { |
| 24810 | filenameObj = Jim_NewStringObj(interp, filename_template, -1); |
| 24811 | } |
| 24812 | |
| 24813 | |
| 24814 | #ifdef HAVE_UMASK |
| 24815 | mask = umask(S_IXUSR | S_IRWXG | S_IRWXO); |
| 24816 | #endif |
| 24817 | #ifdef HAVE_MKSTEMP |
| 24818 | fd = mkstemp(filenameObj->bytes); |
| 24819 | #else |
| 24820 | if (mktemp(filenameObj->bytes) == NULL) { |
| 24821 | fd = -1; |
| 24822 | } |
| 24823 | else { |
| 24824 | fd = open(filenameObj->bytes, O_RDWR | O_CREAT | O_TRUNC); |
| 24825 | } |
| 24826 | #endif |
| 24827 | #ifdef HAVE_UMASK |
| 24828 | umask(mask); |
| 24829 | #endif |
| 24830 | if (fd < 0) { |
| 24831 | Jim_SetResultErrno(interp, Jim_String(filenameObj)); |
| 24832 | Jim_FreeNewObj(interp, filenameObj); |
| 24833 | return -1; |
| 24834 | } |
| 24835 | if (unlink_file) { |
| 24836 | remove(Jim_String(filenameObj)); |
| 24837 | } |
| 24838 | |
| 24839 | Jim_SetResult(interp, filenameObj); |
| 24840 | return fd; |
| 24841 | } |
| 24842 | |
| 24843 | int Jim_OpenForWrite(const char *filename, int append) |
| 24844 | { |
| 24845 | return open(filename, O_WRONLY | O_CREAT | (append ? O_APPEND : O_TRUNC), 0666); |
| 24846 | } |
| 24847 | |
| 24848 | int Jim_OpenForRead(const char *filename) |
| 24849 | { |
| 24850 | return open(filename, O_RDONLY, 0); |
| 24851 | } |
| 24852 | |
| 24853 | #endif |
| 24854 | |
| 24855 | #if defined(_WIN32) || defined(WIN32) |
| 24856 | #ifndef STRICT |
| 24857 | #define STRICT |
| 24858 | #endif |
| 24859 | #define WIN32_LEAN_AND_MEAN |
| 24860 | #include <windows.h> |
| 24861 | |
| 24862 | #if defined(HAVE_DLOPEN_COMPAT) |
| 24863 | void *dlopen(const char *path, int mode) |
| 24864 | { |
| 24865 | JIM_NOTUSED(mode); |
| 24866 | |
| 24867 | return (void *)LoadLibraryA(path); |
| 24868 | } |
| 24869 | |
| 24870 | int dlclose(void *handle) |
| 24871 | { |
| 24872 | FreeLibrary((HANDLE)handle); |
| 24873 | return 0; |
| 24874 | } |
| 24875 | |
| 24876 | void *dlsym(void *handle, const char *symbol) |
| 24877 | { |
| 24878 | return GetProcAddress((HMODULE)handle, symbol); |
| 24879 | } |
| 24880 | |
| 24881 | char *dlerror(void) |
| 24882 | { |
| 24883 | static char msg[121]; |
| 24884 | FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), |
| 24885 | LANG_NEUTRAL, msg, sizeof(msg) - 1, NULL); |
| 24886 | return msg; |
| 24887 | } |
| 24888 | #endif |
| 24889 | |
| 24890 | #ifdef _MSC_VER |
| 24891 | |
| 24892 | #include <sys/timeb.h> |
| 24893 | |
| 24894 | |
| 24895 | int gettimeofday(struct timeval *tv, void *unused) |
| 24896 | { |
| 24897 | struct _timeb tb; |
| 24898 | |
| 24899 | _ftime(&tb); |
| 24900 | tv->tv_sec = tb.time; |
| 24901 | tv->tv_usec = tb.millitm * 1000; |
| 24902 | |
| 24903 | return 0; |
| 24904 | } |
| 24905 | |
| 24906 | |
| 24907 | DIR *opendir(const char *name) |
| 24908 | { |
| 24909 | DIR *dir = 0; |
| 24910 | |
| 24911 | if (name && name[0]) { |
| 24912 | size_t base_length = strlen(name); |
| 24913 | const char *all = |
| 24914 | strchr("/\\", name[base_length - 1]) ? "*" : "/*"; |
| 24915 | |
| 24916 | if ((dir = (DIR *) Jim_Alloc(sizeof *dir)) != 0 && |
| 24917 | (dir->name = (char *)Jim_Alloc(base_length + strlen(all) + 1)) != 0) { |
| 24918 | strcat(strcpy(dir->name, name), all); |
| 24919 | |
| 24920 | if ((dir->handle = (long)_findfirst(dir->name, &dir->info)) != -1) |
| 24921 | dir->result.d_name = 0; |
| 24922 | else { |
| 24923 | Jim_Free(dir->name); |
| 24924 | Jim_Free(dir); |
| 24925 | dir = 0; |
| 24926 | } |
| 24927 | } |
| 24928 | else { |
| 24929 | Jim_Free(dir); |
| 24930 | dir = 0; |
| 24931 | errno = ENOMEM; |
| 24932 | } |
| 24933 | } |
| 24934 | else { |
| 24935 | errno = EINVAL; |
| 24936 | } |
| 24937 | return dir; |
| 24938 | } |
| 24939 | |
| 24940 | int closedir(DIR * dir) |
| 24941 | { |
| 24942 | int result = -1; |
| 24943 | |
| 24944 | if (dir) { |
| 24945 | if (dir->handle != -1) |
| 24946 | result = _findclose(dir->handle); |
| 24947 | Jim_Free(dir->name); |
| 24948 | Jim_Free(dir); |
| 24949 | } |
| 24950 | if (result == -1) |
| 24951 | errno = EBADF; |
| 24952 | return result; |
| 24953 | } |
| 24954 | |
| 24955 | struct dirent *readdir(DIR * dir) |
| 24956 | { |
| 24957 | struct dirent *result = 0; |
| 24958 | |
| 24959 | if (dir && dir->handle != -1) { |
| 24960 | if (!dir->result.d_name || _findnext(dir->handle, &dir->info) != -1) { |
| 24961 | result = &dir->result; |
| 24962 | result->d_name = dir->info.name; |
| 24963 | } |
| 24964 | } |
| 24965 | else { |
| 24966 | errno = EBADF; |
| 24967 | } |
| 24968 | return result; |
| 24969 | } |
| 24970 | #endif |
| 24971 | #endif |
| 24972 | #include <stdio.h> |
| 24973 | #include <signal.h> |
| 24974 | |
| 24975 | |
| 24976 | |
| 24977 | |
| 24978 | |
| 24979 | |
| 24980 | #ifndef SIGPIPE |
| 24981 | #define SIGPIPE 13 |
| 24982 | #endif |
| 24983 | #ifndef SIGINT |
| 24984 | #define SIGINT 2 |
| 24985 | #endif |
| 24986 | |
| 24987 | const char *Jim_SignalId(int sig) |
| 24988 | { |
| 24989 | static char buf[10]; |
| 24990 | switch (sig) { |
| 24991 | case SIGINT: return "SIGINT"; |
| 24992 | case SIGPIPE: return "SIGPIPE"; |
| 24993 | |
| 24994 | } |
| 24995 | snprintf(buf, sizeof(buf), "%d", sig); |
| 24996 | return buf; |
| 24997 | } |
| 24998 | #ifndef JIM_BOOTSTRAP_LIB_ONLY |
| 24999 | #include <errno.h> |
| 25000 | #include <string.h> |
| 25001 | #include <stdio.h> |
| 25002 | |
| 25003 | |
| 25004 | #ifdef USE_LINENOISE |
| 25005 | #ifdef HAVE_UNISTD_H |
| 25006 | #include <unistd.h> |
| 25007 | #endif |
| 25008 | #ifdef HAVE_SYS_STAT_H |
| 25009 | #include <sys/stat.h> |
| 25010 | #endif |
| 25011 | #include "linenoise.h" |
| 25012 | #else |
| 25013 | #define MAX_LINE_LEN 512 |
| 25014 | #endif |
| 25015 | |
| 25016 | #ifdef USE_LINENOISE |
| 25017 | struct JimCompletionInfo { |
| 25018 | Jim_Interp *interp; |
| 25019 | Jim_Obj *completion_command; |
| 25020 | Jim_Obj *hints_command; |
| 25021 | |
| 25022 | }; |
| 25023 | |
| 25024 | static struct JimCompletionInfo *JimGetCompletionInfo(Jim_Interp *interp); |
| 25025 | static void JimCompletionCallback(const char *prefix, linenoiseCompletions *comp, void *userdata); |
| 25026 | static const char completion_callback_assoc_key[] = "interactive-completion"; |
| 25027 | static char *JimHintsCallback(const char *prefix, int *color, int *bold, void *userdata); |
| 25028 | static void JimFreeHintsCallback(void *hint, void *userdata); |
| 25029 | #endif |
| 25030 | |
| 25031 | char *Jim_HistoryGetline(Jim_Interp *interp, const char *prompt) |
| 25032 | { |
| 25033 | #ifdef USE_LINENOISE |
| 25034 | struct JimCompletionInfo *compinfo = JimGetCompletionInfo(interp); |
| 25035 | char *result; |
| 25036 | Jim_Obj *objPtr; |
| 25037 | long mlmode = 0; |
| 25038 | if (compinfo->completion_command) { |
| 25039 | linenoiseSetCompletionCallback(JimCompletionCallback, compinfo); |
| 25040 | } |
| 25041 | if (compinfo->hints_command) { |
| 25042 | linenoiseSetHintsCallback(JimHintsCallback, compinfo); |
| 25043 | linenoiseSetFreeHintsCallback(JimFreeHintsCallback); |
| 25044 | } |
| 25045 | objPtr = Jim_GetVariableStr(interp, "history::multiline", JIM_NONE); |
| 25046 | if (objPtr && Jim_GetLong(interp, objPtr, &mlmode) == JIM_NONE) { |
| 25047 | linenoiseSetMultiLine(mlmode); |
| 25048 | } |
| 25049 | |
| 25050 | result = linenoise(prompt); |
| 25051 | |
| 25052 | linenoiseSetCompletionCallback(NULL, NULL); |
| 25053 | linenoiseSetHintsCallback(NULL, NULL); |
| 25054 | linenoiseSetFreeHintsCallback(NULL); |
| 25055 | return result; |
| 25056 | #else |
| 25057 | int len; |
| 25058 | char *line = Jim_Alloc(MAX_LINE_LEN); |
| 25059 | |
| 25060 | fputs(prompt, stdout); |
| 25061 | fflush(stdout); |
| 25062 | |
| 25063 | if (fgets(line, MAX_LINE_LEN, stdin) == NULL) { |
| 25064 | Jim_Free(line); |
| 25065 | return NULL; |
| 25066 | } |
| 25067 | len = strlen(line); |
| 25068 | if (len && line[len - 1] == '\n') { |
| 25069 | line[len - 1] = '\0'; |
| 25070 | } |
| 25071 | return line; |
| 25072 | #endif |
| 25073 | } |
| 25074 | |
| 25075 | void Jim_HistoryLoad(const char *filename) |
| 25076 | { |
| 25077 | #ifdef USE_LINENOISE |
| 25078 | linenoiseHistoryLoad(filename); |
| 25079 | #endif |
| 25080 | } |
| 25081 | |
| 25082 | void Jim_HistoryAdd(const char *line) |
| 25083 | { |
| 25084 | #ifdef USE_LINENOISE |
| 25085 | linenoiseHistoryAdd(line); |
| 25086 | #endif |
| 25087 | } |
| 25088 | |
| 25089 | void Jim_HistorySave(const char *filename) |
| 25090 | { |
| 25091 | #ifdef USE_LINENOISE |
| 25092 | #ifdef HAVE_UMASK |
| 25093 | mode_t mask; |
| 25094 | |
| 25095 | mask = umask(S_IXUSR | S_IRWXG | S_IRWXO); |
| 25096 | #endif |
| 25097 | linenoiseHistorySave(filename); |
| 25098 | #ifdef HAVE_UMASK |
| 25099 | umask(mask); |
| 25100 | #endif |
| 25101 | #endif |
| 25102 | } |
| 25103 | |
| 25104 | void Jim_HistoryShow(void) |
| 25105 | { |
| 25106 | #ifdef USE_LINENOISE |
| 25107 | |
| 25108 | int i; |
| 25109 | int len; |
| 25110 | char **history = linenoiseHistory(&len); |
| 25111 | for (i = 0; i < len; i++) { |
| 25112 | printf("%4d %s\n", i + 1, history[i]); |
| 25113 | } |
| 25114 | #endif |
| 25115 | } |
| 25116 | |
| 25117 | void Jim_HistorySetMaxLen(int length) |
| 25118 | { |
| 25119 | #ifdef USE_LINENOISE |
| 25120 | linenoiseHistorySetMaxLen(length); |
| 25121 | #endif |
| 25122 | } |
| 25123 | |
| 25124 | int Jim_HistoryGetMaxLen(void) |
| 25125 | { |
| 25126 | #ifdef USE_LINENOISE |
| 25127 | return linenoiseHistoryGetMaxLen(); |
| 25128 | #endif |
| 25129 | return 0; |
| 25130 | } |
| 25131 | |
| 25132 | #ifdef USE_LINENOISE |
| 25133 | static void JimCompletionCallback(const char *prefix, linenoiseCompletions *comp, void *userdata) |
| 25134 | { |
| 25135 | struct JimCompletionInfo *info = (struct JimCompletionInfo *)userdata; |
| 25136 | Jim_Obj *objv[2]; |
| 25137 | int ret; |
| 25138 | |
| 25139 | objv[0] = info->completion_command; |
| 25140 | objv[1] = Jim_NewStringObj(info->interp, prefix, -1); |
| 25141 | |
| 25142 | ret = Jim_EvalObjVector(info->interp, 2, objv); |
| 25143 | |
| 25144 | |
| 25145 | if (ret == JIM_OK) { |
| 25146 | int i; |
| 25147 | Jim_Obj *listObj = Jim_GetResult(info->interp); |
| 25148 | int len = Jim_ListLength(info->interp, listObj); |
| 25149 | for (i = 0; i < len; i++) { |
| 25150 | linenoiseAddCompletion(comp, Jim_String(Jim_ListGetIndex(info->interp, listObj, i))); |
| 25151 | } |
| 25152 | } |
| 25153 | } |
| 25154 | |
| 25155 | static char *JimHintsCallback(const char *prefix, int *color, int *bold, void *userdata) |
| 25156 | { |
| 25157 | struct JimCompletionInfo *info = (struct JimCompletionInfo *)userdata; |
| 25158 | Jim_Obj *objv[2]; |
| 25159 | int ret; |
| 25160 | char *result = NULL; |
| 25161 | |
| 25162 | objv[0] = info->hints_command; |
| 25163 | objv[1] = Jim_NewStringObj(info->interp, prefix, -1); |
| 25164 | |
| 25165 | ret = Jim_EvalObjVector(info->interp, 2, objv); |
| 25166 | |
| 25167 | |
| 25168 | if (ret == JIM_OK) { |
| 25169 | Jim_Obj *listObj = Jim_GetResult(info->interp); |
| 25170 | Jim_IncrRefCount(listObj); |
| 25171 | |
| 25172 | int len = Jim_ListLength(info->interp, listObj); |
| 25173 | if (len >= 1) { |
| 25174 | long x; |
| 25175 | result = Jim_StrDup(Jim_String(Jim_ListGetIndex(info->interp, listObj, 0))); |
| 25176 | if (len >= 2 && Jim_GetLong(info->interp, Jim_ListGetIndex(info->interp, listObj, 1), &x) == JIM_OK) { |
| 25177 | *color = x; |
| 25178 | } |
| 25179 | if (len >= 3 && Jim_GetLong(info->interp, Jim_ListGetIndex(info->interp, listObj, 2), &x) == JIM_OK) { |
| 25180 | *bold = x; |
| 25181 | } |
| 25182 | } |
| 25183 | Jim_DecrRefCount(info->interp, listObj); |
| 25184 | } |
| 25185 | return result; |
| 25186 | } |
| 25187 | |
| 25188 | static void JimFreeHintsCallback(void *hint, void *userdata) |
| 25189 | { |
| 25190 | Jim_Free(hint); |
| 25191 | } |
| 25192 | |
| 25193 | static void JimHistoryFreeCompletion(Jim_Interp *interp, void *data) |
| 25194 | { |
| 25195 | struct JimCompletionInfo *compinfo = data; |
| 25196 | |
| 25197 | if (compinfo->completion_command) { |
| 25198 | Jim_DecrRefCount(interp, compinfo->completion_command); |
| 25199 | } |
| 25200 | if (compinfo->hints_command) { |
| 25201 | Jim_DecrRefCount(interp, compinfo->hints_command); |
| 25202 | } |
| 25203 | |
| 25204 | Jim_Free(compinfo); |
| 25205 | } |
| 25206 | |
| 25207 | static struct JimCompletionInfo *JimGetCompletionInfo(Jim_Interp *interp) |
| 25208 | { |
| 25209 | struct JimCompletionInfo *compinfo = Jim_GetAssocData(interp, completion_callback_assoc_key); |
| 25210 | if (compinfo == NULL) { |
| 25211 | compinfo = Jim_Alloc(sizeof(*compinfo)); |
| 25212 | compinfo->interp = interp; |
| 25213 | compinfo->completion_command = NULL; |
| 25214 | compinfo->hints_command = NULL; |
| 25215 | Jim_SetAssocData(interp, completion_callback_assoc_key, JimHistoryFreeCompletion, compinfo); |
| 25216 | } |
| 25217 | return compinfo; |
| 25218 | } |
| 25219 | #endif |
| 25220 | |
| 25221 | void Jim_HistorySetCompletion(Jim_Interp *interp, Jim_Obj *completionCommandObj) |
| 25222 | { |
| 25223 | #ifdef USE_LINENOISE |
| 25224 | struct JimCompletionInfo *compinfo = JimGetCompletionInfo(interp); |
| 25225 | |
| 25226 | if (completionCommandObj) { |
| 25227 | |
| 25228 | Jim_IncrRefCount(completionCommandObj); |
| 25229 | } |
| 25230 | if (compinfo->completion_command) { |
| 25231 | Jim_DecrRefCount(interp, compinfo->completion_command); |
| 25232 | } |
| 25233 | compinfo->completion_command = completionCommandObj; |
| 25234 | #endif |
| 25235 | } |
| 25236 | |
| 25237 | void Jim_HistorySetHints(Jim_Interp *interp, Jim_Obj *hintsCommandObj) |
| 25238 | { |
| 25239 | #ifdef USE_LINENOISE |
| 25240 | struct JimCompletionInfo *compinfo = JimGetCompletionInfo(interp); |
| 25241 | |
| 25242 | if (hintsCommandObj) { |
| 25243 | |
| 25244 | Jim_IncrRefCount(hintsCommandObj); |
| 25245 | } |
| 25246 | if (compinfo->hints_command) { |
| 25247 | Jim_DecrRefCount(interp, compinfo->hints_command); |
| 25248 | } |
| 25249 | compinfo->hints_command = hintsCommandObj; |
| 25250 | #endif |
| 25251 | } |
| 25252 | |
| 25253 | int Jim_InteractivePrompt(Jim_Interp *interp) |
| 25254 | { |
| 25255 | int retcode = JIM_OK; |
| 25256 | char *history_file = NULL; |
| 25257 | #ifdef USE_LINENOISE |
| 25258 | const char *home; |
| 25259 | |
| 25260 | home = getenv("HOME"); |
| 25261 | if (home && isatty(STDIN_FILENO)) { |
| 25262 | int history_len = strlen(home) + sizeof("/.jim_history"); |
| 25263 | history_file = Jim_Alloc(history_len); |
| 25264 | snprintf(history_file, history_len, "%s/.jim_history", home); |
| 25265 | Jim_HistoryLoad(history_file); |
| 25266 | } |
| 25267 | |
| 25268 | Jim_HistorySetCompletion(interp, Jim_NewStringObj(interp, "tcl::autocomplete", -1)); |
| 25269 | Jim_HistorySetHints(interp, Jim_NewStringObj(interp, "tcl::stdhint", -1)); |
| 25270 | #endif |
| 25271 | |
| 25272 | printf("Welcome to Jim version %d.%d\n", |
| 25273 | JIM_VERSION / 100, JIM_VERSION % 100); |
| 25274 | Jim_SetVariableStrWithStr(interp, JIM_INTERACTIVE, "1"); |
| 25275 | |
| 25276 | while (1) { |
| 25277 | Jim_Obj *scriptObjPtr; |
| 25278 | const char *result; |
| 25279 | int reslen; |
| 25280 | char prompt[20]; |
| 25281 | |
| 25282 | if (retcode != JIM_OK) { |
| 25283 | const char *retcodestr = Jim_ReturnCode(retcode); |
| 25284 | |
| 25285 | if (*retcodestr == '?') { |
| 25286 | snprintf(prompt, sizeof(prompt) - 3, "[%d] . ", retcode); |
| 25287 | } |
| 25288 | else { |
| 25289 | snprintf(prompt, sizeof(prompt) - 3, "[%s] . ", retcodestr); |
| 25290 | } |
| 25291 | } |
| 25292 | else { |
| 25293 | strcpy(prompt, ". "); |
| 25294 | } |
| 25295 | |
| 25296 | scriptObjPtr = Jim_NewStringObj(interp, "", 0); |
| 25297 | Jim_IncrRefCount(scriptObjPtr); |
| 25298 | while (1) { |
| 25299 | char state; |
| 25300 | char *line; |
| 25301 | |
| 25302 | line = Jim_HistoryGetline(interp, prompt); |
| 25303 | if (line == NULL) { |
| 25304 | if (errno == EINTR) { |
| 25305 | continue; |
| 25306 | } |
| 25307 | Jim_DecrRefCount(interp, scriptObjPtr); |
| 25308 | retcode = JIM_OK; |
| 25309 | goto out; |
| 25310 | } |
| 25311 | if (Jim_Length(scriptObjPtr) != 0) { |
| 25312 | |
| 25313 | Jim_AppendString(interp, scriptObjPtr, "\n", 1); |
| 25314 | } |
| 25315 | Jim_AppendString(interp, scriptObjPtr, line, -1); |
| 25316 | Jim_Free(line); |
| 25317 | if (Jim_ScriptIsComplete(interp, scriptObjPtr, &state)) |
| 25318 | break; |
| 25319 | |
| 25320 | snprintf(prompt, sizeof(prompt), "%c> ", state); |
| 25321 | } |
| 25322 | #ifdef USE_LINENOISE |
| 25323 | if (strcmp(Jim_String(scriptObjPtr), "h") == 0) { |
| 25324 | |
| 25325 | Jim_HistoryShow(); |
| 25326 | Jim_DecrRefCount(interp, scriptObjPtr); |
| 25327 | continue; |
| 25328 | } |
| 25329 | |
| 25330 | Jim_HistoryAdd(Jim_String(scriptObjPtr)); |
| 25331 | if (history_file) { |
| 25332 | Jim_HistorySave(history_file); |
| 25333 | } |
| 25334 | #endif |
| 25335 | retcode = Jim_EvalObj(interp, scriptObjPtr); |
| 25336 | Jim_DecrRefCount(interp, scriptObjPtr); |
| 25337 | |
| 25338 | if (retcode == JIM_EXIT) { |
| 25339 | break; |
| 25340 | } |
| 25341 | if (retcode == JIM_ERR) { |
| 25342 | Jim_MakeErrorMessage(interp); |
| 25343 | } |
| 25344 | result = Jim_GetString(Jim_GetResult(interp), &reslen); |
| 25345 | if (reslen) { |
| 25346 | if (fwrite(result, reslen, 1, stdout) == 0) { |
| 25347 | |
| 25348 | } |
| 25349 | putchar('\n'); |
| 25350 | } |
| 25351 | } |
| 25352 | out: |
| 25353 | Jim_Free(history_file); |
| 25354 | |
| 25355 | return retcode; |
| 25356 | } |
| 25357 | |
| 25358 | #include <stdio.h> |
| 25359 | #include <stdlib.h> |
| 25360 | #include <string.h> |
| 25361 | |
| 25362 | |
| 25363 | |
| 25364 | extern int Jim_initjimshInit(Jim_Interp *interp); |
| 25365 | |
| 25366 | static void JimSetArgv(Jim_Interp *interp, int argc, char *const argv[]) |
| 25367 | { |
| 25368 | int n; |
| 25369 | Jim_Obj *listObj = Jim_NewListObj(interp, NULL, 0); |
| 25370 | |
| 25371 | |
| 25372 | for (n = 0; n < argc; n++) { |
| 25373 | Jim_Obj *obj = Jim_NewStringObj(interp, argv[n], -1); |
| 25374 | |
| 25375 | Jim_ListAppendElement(interp, listObj, obj); |
| 25376 | } |
| 25377 | |
| 25378 | Jim_SetVariableStr(interp, "argv", listObj); |
| 25379 | Jim_SetVariableStr(interp, "argc", Jim_NewIntObj(interp, argc)); |
| 25380 | } |
| 25381 | |
| 25382 | static void JimPrintErrorMessage(Jim_Interp *interp) |
| 25383 | { |
| 25384 | Jim_MakeErrorMessage(interp); |
| 25385 | fprintf(stderr, "%s\n", Jim_String(Jim_GetResult(interp))); |
| 25386 | } |
| 25387 | |
| 25388 | void usage(const char* executable_name) |
| 25389 | { |
| 25390 | printf("jimsh version %d.%d\n", JIM_VERSION / 100, JIM_VERSION % 100); |
| 25391 | printf("Usage: %s\n", executable_name); |
| 25392 | printf("or : %s [options] [filename]\n", executable_name); |
| 25393 | printf("\n"); |
| 25394 | printf("Without options: Interactive mode\n"); |
| 25395 | printf("\n"); |
| 25396 | printf("Options:\n"); |
| 25397 | printf(" --version : prints the version string\n"); |
| 25398 | printf(" --help : prints this text\n"); |
| 25399 | printf(" -e CMD : executes command CMD\n"); |
| 25400 | printf(" NOTE: all subsequent options will be passed as arguments to the command\n"); |
| 25401 | printf(" [filename|-] : executes the script contained in the named file, or from stdin if \"-\"\n"); |
| 25402 | printf(" NOTE: all subsequent options will be passed to the script\n\n"); |
| 25403 | } |
| 25404 | |
| 25405 | int main(int argc, char *const argv[]) |
| 25406 | { |
| 25407 | int retcode; |
| 25408 | Jim_Interp *interp; |
| 25409 | char *const orig_argv0 = argv[0]; |
| 25410 | |
| 25411 | |
| 25412 | if (argc > 1 && strcmp(argv[1], "--version") == 0) { |
| 25413 | printf("%d.%d\n", JIM_VERSION / 100, JIM_VERSION % 100); |
| 25414 | return 0; |
| 25415 | } |
| 25416 | else if (argc > 1 && strcmp(argv[1], "--help") == 0) { |
| 25417 | usage(argv[0]); |
| 25418 | return 0; |
| 25419 | } |
| 25420 | |
| 25421 | |
| 25422 | interp = Jim_CreateInterp(); |
| 25423 | Jim_RegisterCoreCommands(interp); |
| 25424 | |
| 25425 | |
| 25426 | if (Jim_InitStaticExtensions(interp) != JIM_OK) { |
| 25427 | JimPrintErrorMessage(interp); |
| 25428 | } |
| 25429 | |
| 25430 | Jim_SetVariableStrWithStr(interp, "jim::argv0", orig_argv0); |
| 25431 | Jim_SetVariableStrWithStr(interp, JIM_INTERACTIVE, argc == 1 ? "1" : "0"); |
| 25432 | #ifdef USE_LINENOISE |
| 25433 | Jim_SetVariableStrWithStr(interp, "jim::lineedit", "1"); |
| 25434 | #else |
| 25435 | Jim_SetVariableStrWithStr(interp, "jim::lineedit", "0"); |
| 25436 | #endif |
| 25437 | retcode = Jim_initjimshInit(interp); |
| 25438 | |
| 25439 | if (argc == 1) { |
| 25440 | |
| 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 | |
| 25455 | if (argc > 2 && strcmp(argv[1], "-e") == 0) { |
| 25456 | |
| 25457 | JimSetArgv(interp, argc - 3, argv + 3); |
| 25458 | retcode = Jim_Eval(interp, argv[2]); |
| 25459 | if (retcode != JIM_ERR) { |
| 25460 | int len; |
| 25461 | const char *msg = Jim_GetString(Jim_GetResult(interp), &len); |
| 25462 | if (fwrite(msg, len, 1, stdout) == 0) { |
| 25463 | |
| 25464 | } |
| 25465 | putchar('\n'); |
| 25466 | } |
| 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 | if (retcode == JIM_ERR) { |
| 25479 | JimPrintErrorMessage(interp); |
| 25480 | } |
| 25481 | } |
| 25482 | if (retcode == JIM_EXIT) { |
| 25483 | retcode = Jim_GetExitCode(interp); |
| 25484 | } |
| 25485 | else if (retcode == JIM_ERR) { |
| 25486 | retcode = 1; |
| 25487 | } |
| 25488 | else { |
| 25489 | retcode = 0; |
| 25490 | } |
| 25491 | Jim_FreeInterp(interp); |
| 25492 | return retcode; |
| 25493 | } |
| 25494 | #endif |