1 /* vi:set ts=8 sts=4 sw=4 noet: 2 * 3 * VIM - Vi IMproved by Bram Moolenaar 4 * 5 * Do ":help uganda" in Vim to read copying and usage conditions. 6 * Do ":help credits" in Vim to see a list of people who contributed. 7 * See README.txt for an overview of the Vim source code. 8 */ 9 10 /* 11 * eval.c: Expression evaluation. 12 */ 13 #define USING_FLOAT_STUFF 14 15 #include "vim.h" 16 17 #if defined(FEAT_EVAL) || defined(PROTO) 18 19 #ifdef VMS 20 # include <float.h> 21 #endif 22 23 static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary"); 24 #ifdef FEAT_FLOAT 25 static char *e_float_as_string = N_("E806: using Float as a String"); 26 #endif 27 28 #define NAMESPACE_CHAR (char_u *)"abglstvw" 29 30 /* 31 * When recursively copying lists and dicts we need to remember which ones we 32 * have done to avoid endless recursiveness. This unique ID is used for that. 33 * The last bit is used for previous_funccal, ignored when comparing. 34 */ 35 static int current_copyID = 0; 36 37 /* 38 * Info used by a ":for" loop. 39 */ 40 typedef struct 41 { 42 int fi_semicolon; // TRUE if ending in '; var]' 43 int fi_varcount; // nr of variables in the list 44 listwatch_T fi_lw; // keep an eye on the item used. 45 list_T *fi_list; // list being used 46 int fi_bi; // index of blob 47 blob_T *fi_blob; // blob being used 48 } forinfo_T; 49 50 static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op); 51 static int eval2(char_u **arg, typval_T *rettv, int evaluate); 52 static int eval3(char_u **arg, typval_T *rettv, int evaluate); 53 static int eval4(char_u **arg, typval_T *rettv, int evaluate); 54 static int eval5(char_u **arg, typval_T *rettv, int evaluate); 55 static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string); 56 static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string); 57 static int eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp); 58 59 static int free_unref_items(int copyID); 60 static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end); 61 static int tv_check_lock(typval_T *tv, char_u *name, int use_gettext); 62 63 /* 64 * Return "n1" divided by "n2", taking care of dividing by zero. 65 */ 66 varnumber_T 67 num_divide(varnumber_T n1, varnumber_T n2) 68 { 69 varnumber_T result; 70 71 if (n2 == 0) // give an error message? 72 { 73 if (n1 == 0) 74 result = VARNUM_MIN; // similar to NaN 75 else if (n1 < 0) 76 result = -VARNUM_MAX; 77 else 78 result = VARNUM_MAX; 79 } 80 else 81 result = n1 / n2; 82 83 return result; 84 } 85 86 /* 87 * Return "n1" modulus "n2", taking care of dividing by zero. 88 */ 89 varnumber_T 90 num_modulus(varnumber_T n1, varnumber_T n2) 91 { 92 // Give an error when n2 is 0? 93 return (n2 == 0) ? 0 : (n1 % n2); 94 } 95 96 #if defined(EBCDIC) || defined(PROTO) 97 /* 98 * Compare struct fst by function name. 99 */ 100 static int 101 compare_func_name(const void *s1, const void *s2) 102 { 103 struct fst *p1 = (struct fst *)s1; 104 struct fst *p2 = (struct fst *)s2; 105 106 return STRCMP(p1->f_name, p2->f_name); 107 } 108 109 /* 110 * Sort the function table by function name. 111 * The sorting of the table above is ASCII dependent. 112 * On machines using EBCDIC we have to sort it. 113 */ 114 static void 115 sortFunctions(void) 116 { 117 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1; 118 119 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name); 120 } 121 #endif 122 123 /* 124 * Initialize the global and v: variables. 125 */ 126 void 127 eval_init(void) 128 { 129 evalvars_init(); 130 func_init(); 131 132 #ifdef EBCDIC 133 /* 134 * Sort the function table, to enable binary search. 135 */ 136 sortFunctions(); 137 #endif 138 } 139 140 #if defined(EXITFREE) || defined(PROTO) 141 void 142 eval_clear(void) 143 { 144 evalvars_clear(); 145 free_scriptnames(); // must come after evalvars_clear(). 146 free_locales(); 147 148 // autoloaded script names 149 free_autoload_scriptnames(); 150 151 // unreferenced lists and dicts 152 (void)garbage_collect(FALSE); 153 154 // functions not garbage collected 155 free_all_functions(); 156 } 157 #endif 158 159 /* 160 * Top level evaluation function, returning a boolean. 161 * Sets "error" to TRUE if there was an error. 162 * Return TRUE or FALSE. 163 */ 164 int 165 eval_to_bool( 166 char_u *arg, 167 int *error, 168 char_u **nextcmd, 169 int skip) // only parse, don't execute 170 { 171 typval_T tv; 172 varnumber_T retval = FALSE; 173 174 if (skip) 175 ++emsg_skip; 176 if (eval0(arg, &tv, nextcmd, !skip) == FAIL) 177 *error = TRUE; 178 else 179 { 180 *error = FALSE; 181 if (!skip) 182 { 183 retval = (tv_get_number_chk(&tv, error) != 0); 184 clear_tv(&tv); 185 } 186 } 187 if (skip) 188 --emsg_skip; 189 190 return (int)retval; 191 } 192 193 /* 194 * Call eval1() and give an error message if not done at a lower level. 195 */ 196 static int 197 eval1_emsg(char_u **arg, typval_T *rettv, int evaluate) 198 { 199 char_u *start = *arg; 200 int ret; 201 int did_emsg_before = did_emsg; 202 int called_emsg_before = called_emsg; 203 204 ret = eval1(arg, rettv, evaluate); 205 if (ret == FAIL) 206 { 207 // Report the invalid expression unless the expression evaluation has 208 // been cancelled due to an aborting error, an interrupt, or an 209 // exception, or we already gave a more specific error. 210 // Also check called_emsg for when using assert_fails(). 211 if (!aborting() && did_emsg == did_emsg_before 212 && called_emsg == called_emsg_before) 213 semsg(_(e_invexpr2), start); 214 } 215 return ret; 216 } 217 218 /* 219 * Evaluate an expression, which can be a function, partial or string. 220 * Pass arguments "argv[argc]". 221 * Return the result in "rettv" and OK or FAIL. 222 */ 223 int 224 eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv) 225 { 226 char_u *s; 227 char_u buf[NUMBUFLEN]; 228 funcexe_T funcexe; 229 230 if (expr->v_type == VAR_FUNC) 231 { 232 s = expr->vval.v_string; 233 if (s == NULL || *s == NUL) 234 return FAIL; 235 CLEAR_FIELD(funcexe); 236 funcexe.evaluate = TRUE; 237 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL) 238 return FAIL; 239 } 240 else if (expr->v_type == VAR_PARTIAL) 241 { 242 partial_T *partial = expr->vval.v_partial; 243 244 if (partial->pt_func != NULL && partial->pt_func->uf_dfunc_idx >= 0) 245 { 246 if (call_def_function(partial->pt_func, argc, argv, rettv) == FAIL) 247 return FAIL; 248 } 249 else 250 { 251 s = partial_name(partial); 252 if (s == NULL || *s == NUL) 253 return FAIL; 254 CLEAR_FIELD(funcexe); 255 funcexe.evaluate = TRUE; 256 funcexe.partial = partial; 257 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL) 258 return FAIL; 259 } 260 } 261 else 262 { 263 s = tv_get_string_buf_chk(expr, buf); 264 if (s == NULL) 265 return FAIL; 266 s = skipwhite(s); 267 if (eval1_emsg(&s, rettv, TRUE) == FAIL) 268 return FAIL; 269 if (*s != NUL) // check for trailing chars after expr 270 { 271 clear_tv(rettv); 272 semsg(_(e_invexpr2), s); 273 return FAIL; 274 } 275 } 276 return OK; 277 } 278 279 /* 280 * Like eval_to_bool() but using a typval_T instead of a string. 281 * Works for string, funcref and partial. 282 */ 283 int 284 eval_expr_to_bool(typval_T *expr, int *error) 285 { 286 typval_T rettv; 287 int res; 288 289 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL) 290 { 291 *error = TRUE; 292 return FALSE; 293 } 294 res = (tv_get_number_chk(&rettv, error) != 0); 295 clear_tv(&rettv); 296 return res; 297 } 298 299 /* 300 * Top level evaluation function, returning a string. If "skip" is TRUE, 301 * only parsing to "nextcmd" is done, without reporting errors. Return 302 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE. 303 */ 304 char_u * 305 eval_to_string_skip( 306 char_u *arg, 307 char_u **nextcmd, 308 int skip) // only parse, don't execute 309 { 310 typval_T tv; 311 char_u *retval; 312 313 if (skip) 314 ++emsg_skip; 315 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip) 316 retval = NULL; 317 else 318 { 319 retval = vim_strsave(tv_get_string(&tv)); 320 clear_tv(&tv); 321 } 322 if (skip) 323 --emsg_skip; 324 325 return retval; 326 } 327 328 /* 329 * Skip over an expression at "*pp". 330 * Return FAIL for an error, OK otherwise. 331 */ 332 int 333 skip_expr(char_u **pp) 334 { 335 typval_T rettv; 336 337 *pp = skipwhite(*pp); 338 return eval1(pp, &rettv, FALSE); 339 } 340 341 /* 342 * Top level evaluation function, returning a string. 343 * When "convert" is TRUE convert a List into a sequence of lines and convert 344 * a Float to a String. 345 * Return pointer to allocated memory, or NULL for failure. 346 */ 347 char_u * 348 eval_to_string( 349 char_u *arg, 350 char_u **nextcmd, 351 int convert) 352 { 353 typval_T tv; 354 char_u *retval; 355 garray_T ga; 356 #ifdef FEAT_FLOAT 357 char_u numbuf[NUMBUFLEN]; 358 #endif 359 360 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL) 361 retval = NULL; 362 else 363 { 364 if (convert && tv.v_type == VAR_LIST) 365 { 366 ga_init2(&ga, (int)sizeof(char), 80); 367 if (tv.vval.v_list != NULL) 368 { 369 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0); 370 if (tv.vval.v_list->lv_len > 0) 371 ga_append(&ga, NL); 372 } 373 ga_append(&ga, NUL); 374 retval = (char_u *)ga.ga_data; 375 } 376 #ifdef FEAT_FLOAT 377 else if (convert && tv.v_type == VAR_FLOAT) 378 { 379 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float); 380 retval = vim_strsave(numbuf); 381 } 382 #endif 383 else 384 retval = vim_strsave(tv_get_string(&tv)); 385 clear_tv(&tv); 386 } 387 388 return retval; 389 } 390 391 /* 392 * Call eval_to_string() without using current local variables and using 393 * textlock. When "use_sandbox" is TRUE use the sandbox. 394 */ 395 char_u * 396 eval_to_string_safe( 397 char_u *arg, 398 char_u **nextcmd, 399 int use_sandbox) 400 { 401 char_u *retval; 402 funccal_entry_T funccal_entry; 403 404 save_funccal(&funccal_entry); 405 if (use_sandbox) 406 ++sandbox; 407 ++textlock; 408 retval = eval_to_string(arg, nextcmd, FALSE); 409 if (use_sandbox) 410 --sandbox; 411 --textlock; 412 restore_funccal(); 413 return retval; 414 } 415 416 /* 417 * Top level evaluation function, returning a number. 418 * Evaluates "expr" silently. 419 * Returns -1 for an error. 420 */ 421 varnumber_T 422 eval_to_number(char_u *expr) 423 { 424 typval_T rettv; 425 varnumber_T retval; 426 char_u *p = skipwhite(expr); 427 428 ++emsg_off; 429 430 if (eval1(&p, &rettv, TRUE) == FAIL) 431 retval = -1; 432 else 433 { 434 retval = tv_get_number_chk(&rettv, NULL); 435 clear_tv(&rettv); 436 } 437 --emsg_off; 438 439 return retval; 440 } 441 442 /* 443 * Top level evaluation function. 444 * Returns an allocated typval_T with the result. 445 * Returns NULL when there is an error. 446 */ 447 typval_T * 448 eval_expr(char_u *arg, char_u **nextcmd) 449 { 450 typval_T *tv; 451 452 tv = ALLOC_ONE(typval_T); 453 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL) 454 VIM_CLEAR(tv); 455 456 return tv; 457 } 458 459 /* 460 * Call some Vim script function and return the result in "*rettv". 461 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] 462 * should have type VAR_UNKNOWN. 463 * Returns OK or FAIL. 464 */ 465 int 466 call_vim_function( 467 char_u *func, 468 int argc, 469 typval_T *argv, 470 typval_T *rettv) 471 { 472 int ret; 473 funcexe_T funcexe; 474 475 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this 476 CLEAR_FIELD(funcexe); 477 funcexe.firstline = curwin->w_cursor.lnum; 478 funcexe.lastline = curwin->w_cursor.lnum; 479 funcexe.evaluate = TRUE; 480 ret = call_func(func, -1, rettv, argc, argv, &funcexe); 481 if (ret == FAIL) 482 clear_tv(rettv); 483 484 return ret; 485 } 486 487 /* 488 * Call Vim script function "func" and return the result as a number. 489 * Returns -1 when calling the function fails. 490 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should 491 * have type VAR_UNKNOWN. 492 */ 493 varnumber_T 494 call_func_retnr( 495 char_u *func, 496 int argc, 497 typval_T *argv) 498 { 499 typval_T rettv; 500 varnumber_T retval; 501 502 if (call_vim_function(func, argc, argv, &rettv) == FAIL) 503 return -1; 504 505 retval = tv_get_number_chk(&rettv, NULL); 506 clear_tv(&rettv); 507 return retval; 508 } 509 510 /* 511 * Call Vim script function "func" and return the result as a string. 512 * Returns NULL when calling the function fails. 513 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should 514 * have type VAR_UNKNOWN. 515 */ 516 void * 517 call_func_retstr( 518 char_u *func, 519 int argc, 520 typval_T *argv) 521 { 522 typval_T rettv; 523 char_u *retval; 524 525 if (call_vim_function(func, argc, argv, &rettv) == FAIL) 526 return NULL; 527 528 retval = vim_strsave(tv_get_string(&rettv)); 529 clear_tv(&rettv); 530 return retval; 531 } 532 533 /* 534 * Call Vim script function "func" and return the result as a List. 535 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should 536 * have type VAR_UNKNOWN. 537 * Returns NULL when there is something wrong. 538 */ 539 void * 540 call_func_retlist( 541 char_u *func, 542 int argc, 543 typval_T *argv) 544 { 545 typval_T rettv; 546 547 if (call_vim_function(func, argc, argv, &rettv) == FAIL) 548 return NULL; 549 550 if (rettv.v_type != VAR_LIST) 551 { 552 clear_tv(&rettv); 553 return NULL; 554 } 555 556 return rettv.vval.v_list; 557 } 558 559 #ifdef FEAT_FOLDING 560 /* 561 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding 562 * it in "*cp". Doesn't give error messages. 563 */ 564 int 565 eval_foldexpr(char_u *arg, int *cp) 566 { 567 typval_T tv; 568 varnumber_T retval; 569 char_u *s; 570 int use_sandbox = was_set_insecurely((char_u *)"foldexpr", 571 OPT_LOCAL); 572 573 ++emsg_off; 574 if (use_sandbox) 575 ++sandbox; 576 ++textlock; 577 *cp = NUL; 578 if (eval0(arg, &tv, NULL, TRUE) == FAIL) 579 retval = 0; 580 else 581 { 582 // If the result is a number, just return the number. 583 if (tv.v_type == VAR_NUMBER) 584 retval = tv.vval.v_number; 585 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL) 586 retval = 0; 587 else 588 { 589 // If the result is a string, check if there is a non-digit before 590 // the number. 591 s = tv.vval.v_string; 592 if (!VIM_ISDIGIT(*s) && *s != '-') 593 *cp = *s++; 594 retval = atol((char *)s); 595 } 596 clear_tv(&tv); 597 } 598 --emsg_off; 599 if (use_sandbox) 600 --sandbox; 601 --textlock; 602 603 return (int)retval; 604 } 605 #endif 606 607 /* 608 * Get an lval: variable, Dict item or List item that can be assigned a value 609 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]", 610 * "name.key", "name.key[expr]" etc. 611 * Indexing only works if "name" is an existing List or Dictionary. 612 * "name" points to the start of the name. 613 * If "rettv" is not NULL it points to the value to be assigned. 614 * "unlet" is TRUE for ":unlet": slightly different behavior when something is 615 * wrong; must end in space or cmd separator. 616 * 617 * flags: 618 * GLV_QUIET: do not give error messages 619 * GLV_READ_ONLY: will not change the variable 620 * GLV_NO_AUTOLOAD: do not use script autoloading 621 * 622 * Returns a pointer to just after the name, including indexes. 623 * When an evaluation error occurs "lp->ll_name" is NULL; 624 * Returns NULL for a parsing error. Still need to free items in "lp"! 625 */ 626 char_u * 627 get_lval( 628 char_u *name, 629 typval_T *rettv, 630 lval_T *lp, 631 int unlet, 632 int skip, 633 int flags, // GLV_ values 634 int fne_flags) // flags for find_name_end() 635 { 636 char_u *p; 637 char_u *expr_start, *expr_end; 638 int cc; 639 dictitem_T *v; 640 typval_T var1; 641 typval_T var2; 642 int empty1 = FALSE; 643 listitem_T *ni; 644 char_u *key = NULL; 645 int len; 646 hashtab_T *ht; 647 int quiet = flags & GLV_QUIET; 648 649 // Clear everything in "lp". 650 CLEAR_POINTER(lp); 651 652 if (skip) 653 { 654 // When skipping just find the end of the name. 655 lp->ll_name = name; 656 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags); 657 } 658 659 // Find the end of the name. 660 p = find_name_end(name, &expr_start, &expr_end, fne_flags); 661 lp->ll_name_end = p; 662 if (expr_start != NULL) 663 { 664 // Don't expand the name when we already know there is an error. 665 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p) 666 && *p != '[' && *p != '.') 667 { 668 emsg(_(e_trailing)); 669 return NULL; 670 } 671 672 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p); 673 if (lp->ll_exp_name == NULL) 674 { 675 // Report an invalid expression in braces, unless the 676 // expression evaluation has been cancelled due to an 677 // aborting error, an interrupt, or an exception. 678 if (!aborting() && !quiet) 679 { 680 emsg_severe = TRUE; 681 semsg(_(e_invarg2), name); 682 return NULL; 683 } 684 } 685 lp->ll_name = lp->ll_exp_name; 686 } 687 else 688 { 689 lp->ll_name = name; 690 691 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && *p == ':') 692 { 693 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); 694 char_u *tp = skipwhite(p + 1); 695 696 // parse the type after the name 697 lp->ll_type = parse_type(&tp, &si->sn_type_list); 698 lp->ll_name_end = tp; 699 } 700 } 701 702 // Without [idx] or .key we are done. 703 if ((*p != '[' && *p != '.') || lp->ll_name == NULL) 704 return p; 705 706 cc = *p; 707 *p = NUL; 708 // Only pass &ht when we would write to the variable, it prevents autoload 709 // as well. 710 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht, 711 flags & GLV_NO_AUTOLOAD); 712 if (v == NULL && !quiet) 713 semsg(_(e_undefvar), lp->ll_name); 714 *p = cc; 715 if (v == NULL) 716 return NULL; 717 718 /* 719 * Loop until no more [idx] or .key is following. 720 */ 721 lp->ll_tv = &v->di_tv; 722 var1.v_type = VAR_UNKNOWN; 723 var2.v_type = VAR_UNKNOWN; 724 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT)) 725 { 726 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL) 727 && !(lp->ll_tv->v_type == VAR_DICT 728 && lp->ll_tv->vval.v_dict != NULL) 729 && !(lp->ll_tv->v_type == VAR_BLOB 730 && lp->ll_tv->vval.v_blob != NULL)) 731 { 732 if (!quiet) 733 emsg(_("E689: Can only index a List, Dictionary or Blob")); 734 return NULL; 735 } 736 if (lp->ll_range) 737 { 738 if (!quiet) 739 emsg(_("E708: [:] must come last")); 740 return NULL; 741 } 742 743 len = -1; 744 if (*p == '.') 745 { 746 key = p + 1; 747 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len) 748 ; 749 if (len == 0) 750 { 751 if (!quiet) 752 emsg(_(e_emptykey)); 753 return NULL; 754 } 755 p = key + len; 756 } 757 else 758 { 759 // Get the index [expr] or the first index [expr: ]. 760 p = skipwhite(p + 1); 761 if (*p == ':') 762 empty1 = TRUE; 763 else 764 { 765 empty1 = FALSE; 766 if (eval1(&p, &var1, TRUE) == FAIL) // recursive! 767 return NULL; 768 if (tv_get_string_chk(&var1) == NULL) 769 { 770 // not a number or string 771 clear_tv(&var1); 772 return NULL; 773 } 774 } 775 776 // Optionally get the second index [ :expr]. 777 if (*p == ':') 778 { 779 if (lp->ll_tv->v_type == VAR_DICT) 780 { 781 if (!quiet) 782 emsg(_(e_dictrange)); 783 clear_tv(&var1); 784 return NULL; 785 } 786 if (rettv != NULL 787 && !(rettv->v_type == VAR_LIST 788 && rettv->vval.v_list != NULL) 789 && !(rettv->v_type == VAR_BLOB 790 && rettv->vval.v_blob != NULL)) 791 { 792 if (!quiet) 793 emsg(_("E709: [:] requires a List or Blob value")); 794 clear_tv(&var1); 795 return NULL; 796 } 797 p = skipwhite(p + 1); 798 if (*p == ']') 799 lp->ll_empty2 = TRUE; 800 else 801 { 802 lp->ll_empty2 = FALSE; 803 if (eval1(&p, &var2, TRUE) == FAIL) // recursive! 804 { 805 clear_tv(&var1); 806 return NULL; 807 } 808 if (tv_get_string_chk(&var2) == NULL) 809 { 810 // not a number or string 811 clear_tv(&var1); 812 clear_tv(&var2); 813 return NULL; 814 } 815 } 816 lp->ll_range = TRUE; 817 } 818 else 819 lp->ll_range = FALSE; 820 821 if (*p != ']') 822 { 823 if (!quiet) 824 emsg(_(e_missbrac)); 825 clear_tv(&var1); 826 clear_tv(&var2); 827 return NULL; 828 } 829 830 // Skip to past ']'. 831 ++p; 832 } 833 834 if (lp->ll_tv->v_type == VAR_DICT) 835 { 836 if (len == -1) 837 { 838 // "[key]": get key from "var1" 839 key = tv_get_string_chk(&var1); // is number or string 840 if (key == NULL) 841 { 842 clear_tv(&var1); 843 return NULL; 844 } 845 } 846 lp->ll_list = NULL; 847 lp->ll_dict = lp->ll_tv->vval.v_dict; 848 lp->ll_di = dict_find(lp->ll_dict, key, len); 849 850 // When assigning to a scope dictionary check that a function and 851 // variable name is valid (only variable name unless it is l: or 852 // g: dictionary). Disallow overwriting a builtin function. 853 if (rettv != NULL && lp->ll_dict->dv_scope != 0) 854 { 855 int prevval; 856 int wrong; 857 858 if (len != -1) 859 { 860 prevval = key[len]; 861 key[len] = NUL; 862 } 863 else 864 prevval = 0; // avoid compiler warning 865 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE 866 && rettv->v_type == VAR_FUNC 867 && var_check_func_name(key, lp->ll_di == NULL)) 868 || !valid_varname(key); 869 if (len != -1) 870 key[len] = prevval; 871 if (wrong) 872 { 873 clear_tv(&var1); 874 return NULL; 875 } 876 } 877 878 if (lp->ll_di == NULL) 879 { 880 // Can't add "v:" or "a:" variable. 881 if (lp->ll_dict == get_vimvar_dict() 882 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht()) 883 { 884 semsg(_(e_illvar), name); 885 clear_tv(&var1); 886 return NULL; 887 } 888 889 // Key does not exist in dict: may need to add it. 890 if (*p == '[' || *p == '.' || unlet) 891 { 892 if (!quiet) 893 semsg(_(e_dictkey), key); 894 clear_tv(&var1); 895 return NULL; 896 } 897 if (len == -1) 898 lp->ll_newkey = vim_strsave(key); 899 else 900 lp->ll_newkey = vim_strnsave(key, len); 901 clear_tv(&var1); 902 if (lp->ll_newkey == NULL) 903 p = NULL; 904 break; 905 } 906 // existing variable, need to check if it can be changed 907 else if ((flags & GLV_READ_ONLY) == 0 908 && var_check_ro(lp->ll_di->di_flags, name, FALSE)) 909 { 910 clear_tv(&var1); 911 return NULL; 912 } 913 914 clear_tv(&var1); 915 lp->ll_tv = &lp->ll_di->di_tv; 916 } 917 else if (lp->ll_tv->v_type == VAR_BLOB) 918 { 919 long bloblen = blob_len(lp->ll_tv->vval.v_blob); 920 921 /* 922 * Get the number and item for the only or first index of the List. 923 */ 924 if (empty1) 925 lp->ll_n1 = 0; 926 else 927 // is number or string 928 lp->ll_n1 = (long)tv_get_number(&var1); 929 clear_tv(&var1); 930 931 if (lp->ll_n1 < 0 932 || lp->ll_n1 > bloblen 933 || (lp->ll_range && lp->ll_n1 == bloblen)) 934 { 935 if (!quiet) 936 semsg(_(e_blobidx), lp->ll_n1); 937 clear_tv(&var2); 938 return NULL; 939 } 940 if (lp->ll_range && !lp->ll_empty2) 941 { 942 lp->ll_n2 = (long)tv_get_number(&var2); 943 clear_tv(&var2); 944 if (lp->ll_n2 < 0 945 || lp->ll_n2 >= bloblen 946 || lp->ll_n2 < lp->ll_n1) 947 { 948 if (!quiet) 949 semsg(_(e_blobidx), lp->ll_n2); 950 return NULL; 951 } 952 } 953 lp->ll_blob = lp->ll_tv->vval.v_blob; 954 lp->ll_tv = NULL; 955 break; 956 } 957 else 958 { 959 /* 960 * Get the number and item for the only or first index of the List. 961 */ 962 if (empty1) 963 lp->ll_n1 = 0; 964 else 965 // is number or string 966 lp->ll_n1 = (long)tv_get_number(&var1); 967 clear_tv(&var1); 968 969 lp->ll_dict = NULL; 970 lp->ll_list = lp->ll_tv->vval.v_list; 971 lp->ll_li = list_find(lp->ll_list, lp->ll_n1); 972 if (lp->ll_li == NULL) 973 { 974 if (lp->ll_n1 < 0) 975 { 976 lp->ll_n1 = 0; 977 lp->ll_li = list_find(lp->ll_list, lp->ll_n1); 978 } 979 } 980 if (lp->ll_li == NULL) 981 { 982 clear_tv(&var2); 983 if (!quiet) 984 semsg(_(e_listidx), lp->ll_n1); 985 return NULL; 986 } 987 988 /* 989 * May need to find the item or absolute index for the second 990 * index of a range. 991 * When no index given: "lp->ll_empty2" is TRUE. 992 * Otherwise "lp->ll_n2" is set to the second index. 993 */ 994 if (lp->ll_range && !lp->ll_empty2) 995 { 996 lp->ll_n2 = (long)tv_get_number(&var2); 997 // is number or string 998 clear_tv(&var2); 999 if (lp->ll_n2 < 0) 1000 { 1001 ni = list_find(lp->ll_list, lp->ll_n2); 1002 if (ni == NULL) 1003 { 1004 if (!quiet) 1005 semsg(_(e_listidx), lp->ll_n2); 1006 return NULL; 1007 } 1008 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni); 1009 } 1010 1011 // Check that lp->ll_n2 isn't before lp->ll_n1. 1012 if (lp->ll_n1 < 0) 1013 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li); 1014 if (lp->ll_n2 < lp->ll_n1) 1015 { 1016 if (!quiet) 1017 semsg(_(e_listidx), lp->ll_n2); 1018 return NULL; 1019 } 1020 } 1021 1022 lp->ll_tv = &lp->ll_li->li_tv; 1023 } 1024 } 1025 1026 clear_tv(&var1); 1027 lp->ll_name_end = p; 1028 return p; 1029 } 1030 1031 /* 1032 * Clear lval "lp" that was filled by get_lval(). 1033 */ 1034 void 1035 clear_lval(lval_T *lp) 1036 { 1037 vim_free(lp->ll_exp_name); 1038 vim_free(lp->ll_newkey); 1039 } 1040 1041 /* 1042 * Set a variable that was parsed by get_lval() to "rettv". 1043 * "endp" points to just after the parsed name. 1044 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=", 1045 * "%" for "%=", "." for ".=" or "=" for "=". 1046 */ 1047 void 1048 set_var_lval( 1049 lval_T *lp, 1050 char_u *endp, 1051 typval_T *rettv, 1052 int copy, 1053 int flags, // LET_IS_CONST and/or LET_NO_COMMAND 1054 char_u *op) 1055 { 1056 int cc; 1057 listitem_T *ri; 1058 dictitem_T *di; 1059 1060 if (lp->ll_tv == NULL) 1061 { 1062 cc = *endp; 1063 *endp = NUL; 1064 if (lp->ll_blob != NULL) 1065 { 1066 int error = FALSE, val; 1067 1068 if (op != NULL && *op != '=') 1069 { 1070 semsg(_(e_letwrong), op); 1071 return; 1072 } 1073 1074 if (lp->ll_range && rettv->v_type == VAR_BLOB) 1075 { 1076 int il, ir; 1077 1078 if (lp->ll_empty2) 1079 lp->ll_n2 = blob_len(lp->ll_blob) - 1; 1080 1081 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob)) 1082 { 1083 emsg(_("E972: Blob value does not have the right number of bytes")); 1084 return; 1085 } 1086 if (lp->ll_empty2) 1087 lp->ll_n2 = blob_len(lp->ll_blob); 1088 1089 ir = 0; 1090 for (il = lp->ll_n1; il <= lp->ll_n2; il++) 1091 blob_set(lp->ll_blob, il, 1092 blob_get(rettv->vval.v_blob, ir++)); 1093 } 1094 else 1095 { 1096 val = (int)tv_get_number_chk(rettv, &error); 1097 if (!error) 1098 { 1099 garray_T *gap = &lp->ll_blob->bv_ga; 1100 1101 // Allow for appending a byte. Setting a byte beyond 1102 // the end is an error otherwise. 1103 if (lp->ll_n1 < gap->ga_len 1104 || (lp->ll_n1 == gap->ga_len 1105 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK)) 1106 { 1107 blob_set(lp->ll_blob, lp->ll_n1, val); 1108 if (lp->ll_n1 == gap->ga_len) 1109 ++gap->ga_len; 1110 } 1111 // error for invalid range was already given in get_lval() 1112 } 1113 } 1114 } 1115 else if (op != NULL && *op != '=') 1116 { 1117 typval_T tv; 1118 1119 if (flags & LET_IS_CONST) 1120 { 1121 emsg(_(e_cannot_mod)); 1122 *endp = cc; 1123 return; 1124 } 1125 1126 // handle +=, -=, *=, /=, %= and .= 1127 di = NULL; 1128 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name), 1129 &tv, &di, TRUE, FALSE) == OK) 1130 { 1131 if ((di == NULL 1132 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE) 1133 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE))) 1134 && tv_op(&tv, rettv, op) == OK) 1135 set_var(lp->ll_name, &tv, FALSE); 1136 clear_tv(&tv); 1137 } 1138 } 1139 else 1140 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags); 1141 *endp = cc; 1142 } 1143 else if (var_check_lock(lp->ll_newkey == NULL 1144 ? lp->ll_tv->v_lock 1145 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE)) 1146 ; 1147 else if (lp->ll_range) 1148 { 1149 listitem_T *ll_li = lp->ll_li; 1150 int ll_n1 = lp->ll_n1; 1151 1152 if (flags & LET_IS_CONST) 1153 { 1154 emsg(_("E996: Cannot lock a range")); 1155 return; 1156 } 1157 1158 /* 1159 * Check whether any of the list items is locked 1160 */ 1161 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; ) 1162 { 1163 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE)) 1164 return; 1165 ri = ri->li_next; 1166 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1)) 1167 break; 1168 ll_li = ll_li->li_next; 1169 ++ll_n1; 1170 } 1171 1172 /* 1173 * Assign the List values to the list items. 1174 */ 1175 for (ri = rettv->vval.v_list->lv_first; ri != NULL; ) 1176 { 1177 if (op != NULL && *op != '=') 1178 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op); 1179 else 1180 { 1181 clear_tv(&lp->ll_li->li_tv); 1182 copy_tv(&ri->li_tv, &lp->ll_li->li_tv); 1183 } 1184 ri = ri->li_next; 1185 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1)) 1186 break; 1187 if (lp->ll_li->li_next == NULL) 1188 { 1189 // Need to add an empty item. 1190 if (list_append_number(lp->ll_list, 0) == FAIL) 1191 { 1192 ri = NULL; 1193 break; 1194 } 1195 } 1196 lp->ll_li = lp->ll_li->li_next; 1197 ++lp->ll_n1; 1198 } 1199 if (ri != NULL) 1200 emsg(_("E710: List value has more items than target")); 1201 else if (lp->ll_empty2 1202 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL) 1203 : lp->ll_n1 != lp->ll_n2) 1204 emsg(_("E711: List value has not enough items")); 1205 } 1206 else 1207 { 1208 /* 1209 * Assign to a List or Dictionary item. 1210 */ 1211 if (flags & LET_IS_CONST) 1212 { 1213 emsg(_("E996: Cannot lock a list or dict")); 1214 return; 1215 } 1216 if (lp->ll_newkey != NULL) 1217 { 1218 if (op != NULL && *op != '=') 1219 { 1220 semsg(_(e_letwrong), op); 1221 return; 1222 } 1223 1224 // Need to add an item to the Dictionary. 1225 di = dictitem_alloc(lp->ll_newkey); 1226 if (di == NULL) 1227 return; 1228 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL) 1229 { 1230 vim_free(di); 1231 return; 1232 } 1233 lp->ll_tv = &di->di_tv; 1234 } 1235 else if (op != NULL && *op != '=') 1236 { 1237 tv_op(lp->ll_tv, rettv, op); 1238 return; 1239 } 1240 else 1241 clear_tv(lp->ll_tv); 1242 1243 /* 1244 * Assign the value to the variable or list item. 1245 */ 1246 if (copy) 1247 copy_tv(rettv, lp->ll_tv); 1248 else 1249 { 1250 *lp->ll_tv = *rettv; 1251 lp->ll_tv->v_lock = 0; 1252 init_tv(rettv); 1253 } 1254 } 1255 } 1256 1257 /* 1258 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2" 1259 * and "tv1 .= tv2" 1260 * Returns OK or FAIL. 1261 */ 1262 static int 1263 tv_op(typval_T *tv1, typval_T *tv2, char_u *op) 1264 { 1265 varnumber_T n; 1266 char_u numbuf[NUMBUFLEN]; 1267 char_u *s; 1268 1269 // Can't do anything with a Funcref, Dict, v:true on the right. 1270 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT 1271 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL) 1272 { 1273 switch (tv1->v_type) 1274 { 1275 case VAR_UNKNOWN: 1276 case VAR_ANY: 1277 case VAR_VOID: 1278 case VAR_DICT: 1279 case VAR_FUNC: 1280 case VAR_PARTIAL: 1281 case VAR_BOOL: 1282 case VAR_SPECIAL: 1283 case VAR_JOB: 1284 case VAR_CHANNEL: 1285 break; 1286 1287 case VAR_BLOB: 1288 if (*op != '+' || tv2->v_type != VAR_BLOB) 1289 break; 1290 // BLOB += BLOB 1291 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL) 1292 { 1293 blob_T *b1 = tv1->vval.v_blob; 1294 blob_T *b2 = tv2->vval.v_blob; 1295 int i, len = blob_len(b2); 1296 for (i = 0; i < len; i++) 1297 ga_append(&b1->bv_ga, blob_get(b2, i)); 1298 } 1299 return OK; 1300 1301 case VAR_LIST: 1302 if (*op != '+' || tv2->v_type != VAR_LIST) 1303 break; 1304 // List += List 1305 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL) 1306 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL); 1307 return OK; 1308 1309 case VAR_NUMBER: 1310 case VAR_STRING: 1311 if (tv2->v_type == VAR_LIST) 1312 break; 1313 if (vim_strchr((char_u *)"+-*/%", *op) != NULL) 1314 { 1315 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr 1316 n = tv_get_number(tv1); 1317 #ifdef FEAT_FLOAT 1318 if (tv2->v_type == VAR_FLOAT) 1319 { 1320 float_T f = n; 1321 1322 if (*op == '%') 1323 break; 1324 switch (*op) 1325 { 1326 case '+': f += tv2->vval.v_float; break; 1327 case '-': f -= tv2->vval.v_float; break; 1328 case '*': f *= tv2->vval.v_float; break; 1329 case '/': f /= tv2->vval.v_float; break; 1330 } 1331 clear_tv(tv1); 1332 tv1->v_type = VAR_FLOAT; 1333 tv1->vval.v_float = f; 1334 } 1335 else 1336 #endif 1337 { 1338 switch (*op) 1339 { 1340 case '+': n += tv_get_number(tv2); break; 1341 case '-': n -= tv_get_number(tv2); break; 1342 case '*': n *= tv_get_number(tv2); break; 1343 case '/': n = num_divide(n, tv_get_number(tv2)); break; 1344 case '%': n = num_modulus(n, tv_get_number(tv2)); break; 1345 } 1346 clear_tv(tv1); 1347 tv1->v_type = VAR_NUMBER; 1348 tv1->vval.v_number = n; 1349 } 1350 } 1351 else 1352 { 1353 if (tv2->v_type == VAR_FLOAT) 1354 break; 1355 1356 // str .= str 1357 s = tv_get_string(tv1); 1358 s = concat_str(s, tv_get_string_buf(tv2, numbuf)); 1359 clear_tv(tv1); 1360 tv1->v_type = VAR_STRING; 1361 tv1->vval.v_string = s; 1362 } 1363 return OK; 1364 1365 case VAR_FLOAT: 1366 #ifdef FEAT_FLOAT 1367 { 1368 float_T f; 1369 1370 if (*op == '%' || *op == '.' 1371 || (tv2->v_type != VAR_FLOAT 1372 && tv2->v_type != VAR_NUMBER 1373 && tv2->v_type != VAR_STRING)) 1374 break; 1375 if (tv2->v_type == VAR_FLOAT) 1376 f = tv2->vval.v_float; 1377 else 1378 f = tv_get_number(tv2); 1379 switch (*op) 1380 { 1381 case '+': tv1->vval.v_float += f; break; 1382 case '-': tv1->vval.v_float -= f; break; 1383 case '*': tv1->vval.v_float *= f; break; 1384 case '/': tv1->vval.v_float /= f; break; 1385 } 1386 } 1387 #endif 1388 return OK; 1389 } 1390 } 1391 1392 semsg(_(e_letwrong), op); 1393 return FAIL; 1394 } 1395 1396 /* 1397 * Evaluate the expression used in a ":for var in expr" command. 1398 * "arg" points to "var". 1399 * Set "*errp" to TRUE for an error, FALSE otherwise; 1400 * Return a pointer that holds the info. Null when there is an error. 1401 */ 1402 void * 1403 eval_for_line( 1404 char_u *arg, 1405 int *errp, 1406 char_u **nextcmdp, 1407 int skip) 1408 { 1409 forinfo_T *fi; 1410 char_u *expr; 1411 typval_T tv; 1412 list_T *l; 1413 1414 *errp = TRUE; // default: there is an error 1415 1416 fi = ALLOC_CLEAR_ONE(forinfo_T); 1417 if (fi == NULL) 1418 return NULL; 1419 1420 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon); 1421 if (expr == NULL) 1422 return fi; 1423 1424 expr = skipwhite(expr); 1425 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2])) 1426 { 1427 emsg(_(e_missing_in)); 1428 return fi; 1429 } 1430 1431 if (skip) 1432 ++emsg_skip; 1433 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK) 1434 { 1435 *errp = FALSE; 1436 if (!skip) 1437 { 1438 if (tv.v_type == VAR_LIST) 1439 { 1440 l = tv.vval.v_list; 1441 if (l == NULL) 1442 { 1443 // a null list is like an empty list: do nothing 1444 clear_tv(&tv); 1445 } 1446 else 1447 { 1448 // Need a real list here. 1449 range_list_materialize(l); 1450 1451 // No need to increment the refcount, it's already set for 1452 // the list being used in "tv". 1453 fi->fi_list = l; 1454 list_add_watch(l, &fi->fi_lw); 1455 fi->fi_lw.lw_item = l->lv_first; 1456 } 1457 } 1458 else if (tv.v_type == VAR_BLOB) 1459 { 1460 fi->fi_bi = 0; 1461 if (tv.vval.v_blob != NULL) 1462 { 1463 typval_T btv; 1464 1465 // Make a copy, so that the iteration still works when the 1466 // blob is changed. 1467 blob_copy(tv.vval.v_blob, &btv); 1468 fi->fi_blob = btv.vval.v_blob; 1469 } 1470 clear_tv(&tv); 1471 } 1472 else 1473 { 1474 emsg(_(e_listreq)); 1475 clear_tv(&tv); 1476 } 1477 } 1478 } 1479 if (skip) 1480 --emsg_skip; 1481 1482 return fi; 1483 } 1484 1485 /* 1486 * Use the first item in a ":for" list. Advance to the next. 1487 * Assign the values to the variable (list). "arg" points to the first one. 1488 * Return TRUE when a valid item was found, FALSE when at end of list or 1489 * something wrong. 1490 */ 1491 int 1492 next_for_item(void *fi_void, char_u *arg) 1493 { 1494 forinfo_T *fi = (forinfo_T *)fi_void; 1495 int result; 1496 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ? 1497 LET_NO_COMMAND : 0; 1498 listitem_T *item; 1499 1500 if (fi->fi_blob != NULL) 1501 { 1502 typval_T tv; 1503 1504 if (fi->fi_bi >= blob_len(fi->fi_blob)) 1505 return FALSE; 1506 tv.v_type = VAR_NUMBER; 1507 tv.v_lock = VAR_FIXED; 1508 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi); 1509 ++fi->fi_bi; 1510 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon, 1511 fi->fi_varcount, flag, NULL) == OK; 1512 } 1513 1514 item = fi->fi_lw.lw_item; 1515 if (item == NULL) 1516 result = FALSE; 1517 else 1518 { 1519 fi->fi_lw.lw_item = item->li_next; 1520 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon, 1521 fi->fi_varcount, flag, NULL) == OK); 1522 } 1523 return result; 1524 } 1525 1526 /* 1527 * Free the structure used to store info used by ":for". 1528 */ 1529 void 1530 free_for_info(void *fi_void) 1531 { 1532 forinfo_T *fi = (forinfo_T *)fi_void; 1533 1534 if (fi != NULL && fi->fi_list != NULL) 1535 { 1536 list_rem_watch(fi->fi_list, &fi->fi_lw); 1537 list_unref(fi->fi_list); 1538 } 1539 if (fi != NULL && fi->fi_blob != NULL) 1540 blob_unref(fi->fi_blob); 1541 vim_free(fi); 1542 } 1543 1544 void 1545 set_context_for_expression( 1546 expand_T *xp, 1547 char_u *arg, 1548 cmdidx_T cmdidx) 1549 { 1550 int got_eq = FALSE; 1551 int c; 1552 char_u *p; 1553 1554 if (cmdidx == CMD_let || cmdidx == CMD_const) 1555 { 1556 xp->xp_context = EXPAND_USER_VARS; 1557 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL) 1558 { 1559 // ":let var1 var2 ...": find last space. 1560 for (p = arg + STRLEN(arg); p >= arg; ) 1561 { 1562 xp->xp_pattern = p; 1563 MB_PTR_BACK(arg, p); 1564 if (VIM_ISWHITE(*p)) 1565 break; 1566 } 1567 return; 1568 } 1569 } 1570 else 1571 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS 1572 : EXPAND_EXPRESSION; 1573 while ((xp->xp_pattern = vim_strpbrk(arg, 1574 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL) 1575 { 1576 c = *xp->xp_pattern; 1577 if (c == '&') 1578 { 1579 c = xp->xp_pattern[1]; 1580 if (c == '&') 1581 { 1582 ++xp->xp_pattern; 1583 xp->xp_context = cmdidx != CMD_let || got_eq 1584 ? EXPAND_EXPRESSION : EXPAND_NOTHING; 1585 } 1586 else if (c != ' ') 1587 { 1588 xp->xp_context = EXPAND_SETTINGS; 1589 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':') 1590 xp->xp_pattern += 2; 1591 1592 } 1593 } 1594 else if (c == '$') 1595 { 1596 // environment variable 1597 xp->xp_context = EXPAND_ENV_VARS; 1598 } 1599 else if (c == '=') 1600 { 1601 got_eq = TRUE; 1602 xp->xp_context = EXPAND_EXPRESSION; 1603 } 1604 else if (c == '#' 1605 && xp->xp_context == EXPAND_EXPRESSION) 1606 { 1607 // Autoload function/variable contains '#'. 1608 break; 1609 } 1610 else if ((c == '<' || c == '#') 1611 && xp->xp_context == EXPAND_FUNCTIONS 1612 && vim_strchr(xp->xp_pattern, '(') == NULL) 1613 { 1614 // Function name can start with "<SNR>" and contain '#'. 1615 break; 1616 } 1617 else if (cmdidx != CMD_let || got_eq) 1618 { 1619 if (c == '"') // string 1620 { 1621 while ((c = *++xp->xp_pattern) != NUL && c != '"') 1622 if (c == '\\' && xp->xp_pattern[1] != NUL) 1623 ++xp->xp_pattern; 1624 xp->xp_context = EXPAND_NOTHING; 1625 } 1626 else if (c == '\'') // literal string 1627 { 1628 // Trick: '' is like stopping and starting a literal string. 1629 while ((c = *++xp->xp_pattern) != NUL && c != '\'') 1630 /* skip */ ; 1631 xp->xp_context = EXPAND_NOTHING; 1632 } 1633 else if (c == '|') 1634 { 1635 if (xp->xp_pattern[1] == '|') 1636 { 1637 ++xp->xp_pattern; 1638 xp->xp_context = EXPAND_EXPRESSION; 1639 } 1640 else 1641 xp->xp_context = EXPAND_COMMANDS; 1642 } 1643 else 1644 xp->xp_context = EXPAND_EXPRESSION; 1645 } 1646 else 1647 // Doesn't look like something valid, expand as an expression 1648 // anyway. 1649 xp->xp_context = EXPAND_EXPRESSION; 1650 arg = xp->xp_pattern; 1651 if (*arg != NUL) 1652 while ((c = *++arg) != NUL && (c == ' ' || c == '\t')) 1653 /* skip */ ; 1654 } 1655 xp->xp_pattern = arg; 1656 } 1657 1658 /* 1659 * Return TRUE if "pat" matches "text". 1660 * Does not use 'cpo' and always uses 'magic'. 1661 */ 1662 int 1663 pattern_match(char_u *pat, char_u *text, int ic) 1664 { 1665 int matches = FALSE; 1666 char_u *save_cpo; 1667 regmatch_T regmatch; 1668 1669 // avoid 'l' flag in 'cpoptions' 1670 save_cpo = p_cpo; 1671 p_cpo = (char_u *)""; 1672 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING); 1673 if (regmatch.regprog != NULL) 1674 { 1675 regmatch.rm_ic = ic; 1676 matches = vim_regexec_nl(®match, text, (colnr_T)0); 1677 vim_regfree(regmatch.regprog); 1678 } 1679 p_cpo = save_cpo; 1680 return matches; 1681 } 1682 1683 /* 1684 * Handle a name followed by "(". Both for just "name(arg)" and for 1685 * "expr->name(arg)". 1686 * Returns OK or FAIL. 1687 */ 1688 static int 1689 eval_func( 1690 char_u **arg, // points to "(", will be advanced 1691 char_u *name, 1692 int name_len, 1693 typval_T *rettv, 1694 int evaluate, 1695 typval_T *basetv) // "expr" for "expr->name(arg)" 1696 { 1697 char_u *s = name; 1698 int len = name_len; 1699 partial_T *partial; 1700 int ret = OK; 1701 1702 if (!evaluate) 1703 check_vars(s, len); 1704 1705 // If "s" is the name of a variable of type VAR_FUNC 1706 // use its contents. 1707 s = deref_func_name(s, &len, &partial, !evaluate); 1708 1709 // Need to make a copy, in case evaluating the arguments makes 1710 // the name invalid. 1711 s = vim_strsave(s); 1712 if (s == NULL) 1713 ret = FAIL; 1714 else 1715 { 1716 funcexe_T funcexe; 1717 1718 // Invoke the function. 1719 CLEAR_FIELD(funcexe); 1720 funcexe.firstline = curwin->w_cursor.lnum; 1721 funcexe.lastline = curwin->w_cursor.lnum; 1722 funcexe.evaluate = evaluate; 1723 funcexe.partial = partial; 1724 funcexe.basetv = basetv; 1725 ret = get_func_tv(s, len, rettv, arg, &funcexe); 1726 } 1727 vim_free(s); 1728 1729 // If evaluate is FALSE rettv->v_type was not set in 1730 // get_func_tv, but it's needed in handle_subscript() to parse 1731 // what follows. So set it here. 1732 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(') 1733 { 1734 rettv->vval.v_string = NULL; 1735 rettv->v_type = VAR_FUNC; 1736 } 1737 1738 // Stop the expression evaluation when immediately 1739 // aborting on error, or when an interrupt occurred or 1740 // an exception was thrown but not caught. 1741 if (evaluate && aborting()) 1742 { 1743 if (ret == OK) 1744 clear_tv(rettv); 1745 ret = FAIL; 1746 } 1747 return ret; 1748 } 1749 1750 /* 1751 * The "evaluate" argument: When FALSE, the argument is only parsed but not 1752 * executed. The function may return OK, but the rettv will be of type 1753 * VAR_UNKNOWN. The function still returns FAIL for a syntax error. 1754 */ 1755 1756 /* 1757 * Handle zero level expression. 1758 * This calls eval1() and handles error message and nextcmd. 1759 * Put the result in "rettv" when returning OK and "evaluate" is TRUE. 1760 * Note: "rettv.v_lock" is not set. 1761 * Return OK or FAIL. 1762 */ 1763 int 1764 eval0( 1765 char_u *arg, 1766 typval_T *rettv, 1767 char_u **nextcmd, 1768 int evaluate) 1769 { 1770 int ret; 1771 char_u *p; 1772 int did_emsg_before = did_emsg; 1773 int called_emsg_before = called_emsg; 1774 1775 p = skipwhite(arg); 1776 ret = eval1(&p, rettv, evaluate); 1777 if (ret == FAIL || !ends_excmd2(arg, p)) 1778 { 1779 if (ret != FAIL) 1780 clear_tv(rettv); 1781 /* 1782 * Report the invalid expression unless the expression evaluation has 1783 * been cancelled due to an aborting error, an interrupt, or an 1784 * exception, or we already gave a more specific error. 1785 * Also check called_emsg for when using assert_fails(). 1786 */ 1787 if (!aborting() && did_emsg == did_emsg_before 1788 && called_emsg == called_emsg_before) 1789 semsg(_(e_invexpr2), arg); 1790 ret = FAIL; 1791 } 1792 if (nextcmd != NULL) 1793 *nextcmd = check_nextcmd(p); 1794 1795 return ret; 1796 } 1797 1798 /* 1799 * Handle top level expression: 1800 * expr2 ? expr1 : expr1 1801 * 1802 * "arg" must point to the first non-white of the expression. 1803 * "arg" is advanced to the next non-white after the recognized expression. 1804 * 1805 * Note: "rettv.v_lock" is not set. 1806 * 1807 * Return OK or FAIL. 1808 */ 1809 int 1810 eval1(char_u **arg, typval_T *rettv, int evaluate) 1811 { 1812 int result; 1813 typval_T var2; 1814 1815 /* 1816 * Get the first variable. 1817 */ 1818 if (eval2(arg, rettv, evaluate) == FAIL) 1819 return FAIL; 1820 1821 if ((*arg)[0] == '?') 1822 { 1823 result = FALSE; 1824 if (evaluate) 1825 { 1826 int error = FALSE; 1827 1828 if (tv_get_number_chk(rettv, &error) != 0) 1829 result = TRUE; 1830 clear_tv(rettv); 1831 if (error) 1832 return FAIL; 1833 } 1834 1835 /* 1836 * Get the second variable. 1837 */ 1838 *arg = skipwhite(*arg + 1); 1839 if (eval1(arg, rettv, evaluate && result) == FAIL) // recursive! 1840 return FAIL; 1841 1842 /* 1843 * Check for the ":". 1844 */ 1845 if ((*arg)[0] != ':') 1846 { 1847 emsg(_(e_missing_colon)); 1848 if (evaluate && result) 1849 clear_tv(rettv); 1850 return FAIL; 1851 } 1852 1853 /* 1854 * Get the third variable. 1855 */ 1856 *arg = skipwhite(*arg + 1); 1857 if (eval1(arg, &var2, evaluate && !result) == FAIL) // recursive! 1858 { 1859 if (evaluate && result) 1860 clear_tv(rettv); 1861 return FAIL; 1862 } 1863 if (evaluate && !result) 1864 *rettv = var2; 1865 } 1866 1867 return OK; 1868 } 1869 1870 /* 1871 * Handle first level expression: 1872 * expr2 || expr2 || expr2 logical OR 1873 * 1874 * "arg" must point to the first non-white of the expression. 1875 * "arg" is advanced to the next non-white after the recognized expression. 1876 * 1877 * Return OK or FAIL. 1878 */ 1879 static int 1880 eval2(char_u **arg, typval_T *rettv, int evaluate) 1881 { 1882 typval_T var2; 1883 long result; 1884 int first; 1885 int error = FALSE; 1886 1887 /* 1888 * Get the first variable. 1889 */ 1890 if (eval3(arg, rettv, evaluate) == FAIL) 1891 return FAIL; 1892 1893 /* 1894 * Repeat until there is no following "||". 1895 */ 1896 first = TRUE; 1897 result = FALSE; 1898 while ((*arg)[0] == '|' && (*arg)[1] == '|') 1899 { 1900 if (evaluate && first) 1901 { 1902 if (tv_get_number_chk(rettv, &error) != 0) 1903 result = TRUE; 1904 clear_tv(rettv); 1905 if (error) 1906 return FAIL; 1907 first = FALSE; 1908 } 1909 1910 /* 1911 * Get the second variable. 1912 */ 1913 *arg = skipwhite(*arg + 2); 1914 if (eval3(arg, &var2, evaluate && !result) == FAIL) 1915 return FAIL; 1916 1917 /* 1918 * Compute the result. 1919 */ 1920 if (evaluate && !result) 1921 { 1922 if (tv_get_number_chk(&var2, &error) != 0) 1923 result = TRUE; 1924 clear_tv(&var2); 1925 if (error) 1926 return FAIL; 1927 } 1928 if (evaluate) 1929 { 1930 rettv->v_type = VAR_NUMBER; 1931 rettv->vval.v_number = result; 1932 } 1933 } 1934 1935 return OK; 1936 } 1937 1938 /* 1939 * Handle second level expression: 1940 * expr3 && expr3 && expr3 logical AND 1941 * 1942 * "arg" must point to the first non-white of the expression. 1943 * "arg" is advanced to the next non-white after the recognized expression. 1944 * 1945 * Return OK or FAIL. 1946 */ 1947 static int 1948 eval3(char_u **arg, typval_T *rettv, int evaluate) 1949 { 1950 typval_T var2; 1951 long result; 1952 int first; 1953 int error = FALSE; 1954 1955 /* 1956 * Get the first variable. 1957 */ 1958 if (eval4(arg, rettv, evaluate) == FAIL) 1959 return FAIL; 1960 1961 /* 1962 * Repeat until there is no following "&&". 1963 */ 1964 first = TRUE; 1965 result = TRUE; 1966 while ((*arg)[0] == '&' && (*arg)[1] == '&') 1967 { 1968 if (evaluate && first) 1969 { 1970 if (tv_get_number_chk(rettv, &error) == 0) 1971 result = FALSE; 1972 clear_tv(rettv); 1973 if (error) 1974 return FAIL; 1975 first = FALSE; 1976 } 1977 1978 /* 1979 * Get the second variable. 1980 */ 1981 *arg = skipwhite(*arg + 2); 1982 if (eval4(arg, &var2, evaluate && result) == FAIL) 1983 return FAIL; 1984 1985 /* 1986 * Compute the result. 1987 */ 1988 if (evaluate && result) 1989 { 1990 if (tv_get_number_chk(&var2, &error) == 0) 1991 result = FALSE; 1992 clear_tv(&var2); 1993 if (error) 1994 return FAIL; 1995 } 1996 if (evaluate) 1997 { 1998 rettv->v_type = VAR_NUMBER; 1999 rettv->vval.v_number = result; 2000 } 2001 } 2002 2003 return OK; 2004 } 2005 2006 /* 2007 * Handle third level expression: 2008 * var1 == var2 2009 * var1 =~ var2 2010 * var1 != var2 2011 * var1 !~ var2 2012 * var1 > var2 2013 * var1 >= var2 2014 * var1 < var2 2015 * var1 <= var2 2016 * var1 is var2 2017 * var1 isnot var2 2018 * 2019 * "arg" must point to the first non-white of the expression. 2020 * "arg" is advanced to the next non-white after the recognized expression. 2021 * 2022 * Return OK or FAIL. 2023 */ 2024 static int 2025 eval4(char_u **arg, typval_T *rettv, int evaluate) 2026 { 2027 typval_T var2; 2028 char_u *p; 2029 int i; 2030 exptype_T type = EXPR_UNKNOWN; 2031 int len = 2; 2032 int ic; 2033 2034 /* 2035 * Get the first variable. 2036 */ 2037 if (eval5(arg, rettv, evaluate) == FAIL) 2038 return FAIL; 2039 2040 p = *arg; 2041 switch (p[0]) 2042 { 2043 case '=': if (p[1] == '=') 2044 type = EXPR_EQUAL; 2045 else if (p[1] == '~') 2046 type = EXPR_MATCH; 2047 break; 2048 case '!': if (p[1] == '=') 2049 type = EXPR_NEQUAL; 2050 else if (p[1] == '~') 2051 type = EXPR_NOMATCH; 2052 break; 2053 case '>': if (p[1] != '=') 2054 { 2055 type = EXPR_GREATER; 2056 len = 1; 2057 } 2058 else 2059 type = EXPR_GEQUAL; 2060 break; 2061 case '<': if (p[1] != '=') 2062 { 2063 type = EXPR_SMALLER; 2064 len = 1; 2065 } 2066 else 2067 type = EXPR_SEQUAL; 2068 break; 2069 case 'i': if (p[1] == 's') 2070 { 2071 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') 2072 len = 5; 2073 i = p[len]; 2074 if (!isalnum(i) && i != '_') 2075 type = len == 2 ? EXPR_IS : EXPR_ISNOT; 2076 } 2077 break; 2078 } 2079 2080 /* 2081 * If there is a comparative operator, use it. 2082 */ 2083 if (type != EXPR_UNKNOWN) 2084 { 2085 // extra question mark appended: ignore case 2086 if (p[len] == '?') 2087 { 2088 ic = TRUE; 2089 ++len; 2090 } 2091 // extra '#' appended: match case 2092 else if (p[len] == '#') 2093 { 2094 ic = FALSE; 2095 ++len; 2096 } 2097 // nothing appended: use 'ignorecase' 2098 else 2099 ic = p_ic; 2100 2101 /* 2102 * Get the second variable. 2103 */ 2104 *arg = skipwhite(p + len); 2105 if (eval5(arg, &var2, evaluate) == FAIL) 2106 { 2107 clear_tv(rettv); 2108 return FAIL; 2109 } 2110 if (evaluate) 2111 { 2112 int ret = typval_compare(rettv, &var2, type, ic); 2113 2114 clear_tv(&var2); 2115 return ret; 2116 } 2117 } 2118 2119 return OK; 2120 } 2121 2122 void 2123 eval_addblob(typval_T *tv1, typval_T *tv2) 2124 { 2125 blob_T *b1 = tv1->vval.v_blob; 2126 blob_T *b2 = tv2->vval.v_blob; 2127 blob_T *b = blob_alloc(); 2128 int i; 2129 2130 if (b != NULL) 2131 { 2132 for (i = 0; i < blob_len(b1); i++) 2133 ga_append(&b->bv_ga, blob_get(b1, i)); 2134 for (i = 0; i < blob_len(b2); i++) 2135 ga_append(&b->bv_ga, blob_get(b2, i)); 2136 2137 clear_tv(tv1); 2138 rettv_blob_set(tv1, b); 2139 } 2140 } 2141 2142 int 2143 eval_addlist(typval_T *tv1, typval_T *tv2) 2144 { 2145 typval_T var3; 2146 2147 // concatenate Lists 2148 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL) 2149 { 2150 clear_tv(tv1); 2151 clear_tv(tv2); 2152 return FAIL; 2153 } 2154 clear_tv(tv1); 2155 *tv1 = var3; 2156 return OK; 2157 } 2158 2159 /* 2160 * Handle fourth level expression: 2161 * + number addition 2162 * - number subtraction 2163 * . string concatenation (if script version is 1) 2164 * .. string concatenation 2165 * 2166 * "arg" must point to the first non-white of the expression. 2167 * "arg" is advanced to the next non-white after the recognized expression. 2168 * 2169 * Return OK or FAIL. 2170 */ 2171 static int 2172 eval5(char_u **arg, typval_T *rettv, int evaluate) 2173 { 2174 typval_T var2; 2175 int op; 2176 varnumber_T n1, n2; 2177 #ifdef FEAT_FLOAT 2178 float_T f1 = 0, f2 = 0; 2179 #endif 2180 char_u *s1, *s2; 2181 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN]; 2182 char_u *p; 2183 int concat; 2184 2185 /* 2186 * Get the first variable. 2187 */ 2188 if (eval6(arg, rettv, evaluate, FALSE) == FAIL) 2189 return FAIL; 2190 2191 /* 2192 * Repeat computing, until no '+', '-' or '.' is following. 2193 */ 2194 for (;;) 2195 { 2196 // "." is only string concatenation when scriptversion is 1 2197 op = **arg; 2198 concat = op == '.' 2199 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2); 2200 if (op != '+' && op != '-' && !concat) 2201 break; 2202 2203 if ((op != '+' || (rettv->v_type != VAR_LIST 2204 && rettv->v_type != VAR_BLOB)) 2205 #ifdef FEAT_FLOAT 2206 && (op == '.' || rettv->v_type != VAR_FLOAT) 2207 #endif 2208 ) 2209 { 2210 // For "list + ...", an illegal use of the first operand as 2211 // a number cannot be determined before evaluating the 2nd 2212 // operand: if this is also a list, all is ok. 2213 // For "something . ...", "something - ..." or "non-list + ...", 2214 // we know that the first operand needs to be a string or number 2215 // without evaluating the 2nd operand. So check before to avoid 2216 // side effects after an error. 2217 if (evaluate && tv_get_string_chk(rettv) == NULL) 2218 { 2219 clear_tv(rettv); 2220 return FAIL; 2221 } 2222 } 2223 2224 /* 2225 * Get the second variable. 2226 */ 2227 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation 2228 ++*arg; 2229 *arg = skipwhite(*arg + 1); 2230 if (eval6(arg, &var2, evaluate, op == '.') == FAIL) 2231 { 2232 clear_tv(rettv); 2233 return FAIL; 2234 } 2235 2236 if (evaluate) 2237 { 2238 /* 2239 * Compute the result. 2240 */ 2241 if (op == '.') 2242 { 2243 s1 = tv_get_string_buf(rettv, buf1); // already checked 2244 s2 = tv_get_string_buf_chk(&var2, buf2); 2245 if (s2 == NULL) // type error ? 2246 { 2247 clear_tv(rettv); 2248 clear_tv(&var2); 2249 return FAIL; 2250 } 2251 p = concat_str(s1, s2); 2252 clear_tv(rettv); 2253 rettv->v_type = VAR_STRING; 2254 rettv->vval.v_string = p; 2255 } 2256 else if (op == '+' && rettv->v_type == VAR_BLOB 2257 && var2.v_type == VAR_BLOB) 2258 eval_addblob(rettv, &var2); 2259 else if (op == '+' && rettv->v_type == VAR_LIST 2260 && var2.v_type == VAR_LIST) 2261 { 2262 if (eval_addlist(rettv, &var2) == FAIL) 2263 return FAIL; 2264 } 2265 else 2266 { 2267 int error = FALSE; 2268 2269 #ifdef FEAT_FLOAT 2270 if (rettv->v_type == VAR_FLOAT) 2271 { 2272 f1 = rettv->vval.v_float; 2273 n1 = 0; 2274 } 2275 else 2276 #endif 2277 { 2278 n1 = tv_get_number_chk(rettv, &error); 2279 if (error) 2280 { 2281 // This can only happen for "list + non-list". For 2282 // "non-list + ..." or "something - ...", we returned 2283 // before evaluating the 2nd operand. 2284 clear_tv(rettv); 2285 return FAIL; 2286 } 2287 #ifdef FEAT_FLOAT 2288 if (var2.v_type == VAR_FLOAT) 2289 f1 = n1; 2290 #endif 2291 } 2292 #ifdef FEAT_FLOAT 2293 if (var2.v_type == VAR_FLOAT) 2294 { 2295 f2 = var2.vval.v_float; 2296 n2 = 0; 2297 } 2298 else 2299 #endif 2300 { 2301 n2 = tv_get_number_chk(&var2, &error); 2302 if (error) 2303 { 2304 clear_tv(rettv); 2305 clear_tv(&var2); 2306 return FAIL; 2307 } 2308 #ifdef FEAT_FLOAT 2309 if (rettv->v_type == VAR_FLOAT) 2310 f2 = n2; 2311 #endif 2312 } 2313 clear_tv(rettv); 2314 2315 #ifdef FEAT_FLOAT 2316 // If there is a float on either side the result is a float. 2317 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT) 2318 { 2319 if (op == '+') 2320 f1 = f1 + f2; 2321 else 2322 f1 = f1 - f2; 2323 rettv->v_type = VAR_FLOAT; 2324 rettv->vval.v_float = f1; 2325 } 2326 else 2327 #endif 2328 { 2329 if (op == '+') 2330 n1 = n1 + n2; 2331 else 2332 n1 = n1 - n2; 2333 rettv->v_type = VAR_NUMBER; 2334 rettv->vval.v_number = n1; 2335 } 2336 } 2337 clear_tv(&var2); 2338 } 2339 } 2340 return OK; 2341 } 2342 2343 /* 2344 * Handle fifth level expression: 2345 * * number multiplication 2346 * / number division 2347 * % number modulo 2348 * 2349 * "arg" must point to the first non-white of the expression. 2350 * "arg" is advanced to the next non-white after the recognized expression. 2351 * 2352 * Return OK or FAIL. 2353 */ 2354 static int 2355 eval6( 2356 char_u **arg, 2357 typval_T *rettv, 2358 int evaluate, 2359 int want_string) // after "." operator 2360 { 2361 typval_T var2; 2362 int op; 2363 varnumber_T n1, n2; 2364 #ifdef FEAT_FLOAT 2365 int use_float = FALSE; 2366 float_T f1 = 0, f2 = 0; 2367 #endif 2368 int error = FALSE; 2369 2370 /* 2371 * Get the first variable. 2372 */ 2373 if (eval7(arg, rettv, evaluate, want_string) == FAIL) 2374 return FAIL; 2375 2376 /* 2377 * Repeat computing, until no '*', '/' or '%' is following. 2378 */ 2379 for (;;) 2380 { 2381 op = **arg; 2382 if (op != '*' && op != '/' && op != '%') 2383 break; 2384 2385 if (evaluate) 2386 { 2387 #ifdef FEAT_FLOAT 2388 if (rettv->v_type == VAR_FLOAT) 2389 { 2390 f1 = rettv->vval.v_float; 2391 use_float = TRUE; 2392 n1 = 0; 2393 } 2394 else 2395 #endif 2396 n1 = tv_get_number_chk(rettv, &error); 2397 clear_tv(rettv); 2398 if (error) 2399 return FAIL; 2400 } 2401 else 2402 n1 = 0; 2403 2404 /* 2405 * Get the second variable. 2406 */ 2407 *arg = skipwhite(*arg + 1); 2408 if (eval7(arg, &var2, evaluate, FALSE) == FAIL) 2409 return FAIL; 2410 2411 if (evaluate) 2412 { 2413 #ifdef FEAT_FLOAT 2414 if (var2.v_type == VAR_FLOAT) 2415 { 2416 if (!use_float) 2417 { 2418 f1 = n1; 2419 use_float = TRUE; 2420 } 2421 f2 = var2.vval.v_float; 2422 n2 = 0; 2423 } 2424 else 2425 #endif 2426 { 2427 n2 = tv_get_number_chk(&var2, &error); 2428 clear_tv(&var2); 2429 if (error) 2430 return FAIL; 2431 #ifdef FEAT_FLOAT 2432 if (use_float) 2433 f2 = n2; 2434 #endif 2435 } 2436 2437 /* 2438 * Compute the result. 2439 * When either side is a float the result is a float. 2440 */ 2441 #ifdef FEAT_FLOAT 2442 if (use_float) 2443 { 2444 if (op == '*') 2445 f1 = f1 * f2; 2446 else if (op == '/') 2447 { 2448 # ifdef VMS 2449 // VMS crashes on divide by zero, work around it 2450 if (f2 == 0.0) 2451 { 2452 if (f1 == 0) 2453 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN 2454 else if (f1 < 0) 2455 f1 = -1 * __F_FLT_MAX; 2456 else 2457 f1 = __F_FLT_MAX; 2458 } 2459 else 2460 f1 = f1 / f2; 2461 # else 2462 // We rely on the floating point library to handle divide 2463 // by zero to result in "inf" and not a crash. 2464 f1 = f1 / f2; 2465 # endif 2466 } 2467 else 2468 { 2469 emsg(_(e_modulus)); 2470 return FAIL; 2471 } 2472 rettv->v_type = VAR_FLOAT; 2473 rettv->vval.v_float = f1; 2474 } 2475 else 2476 #endif 2477 { 2478 if (op == '*') 2479 n1 = n1 * n2; 2480 else if (op == '/') 2481 n1 = num_divide(n1, n2); 2482 else 2483 n1 = num_modulus(n1, n2); 2484 2485 rettv->v_type = VAR_NUMBER; 2486 rettv->vval.v_number = n1; 2487 } 2488 } 2489 } 2490 2491 return OK; 2492 } 2493 2494 /* 2495 * Handle sixth level expression: 2496 * number number constant 2497 * 0zFFFFFFFF Blob constant 2498 * "string" string constant 2499 * 'string' literal string constant 2500 * &option-name option value 2501 * @r register contents 2502 * identifier variable value 2503 * function() function call 2504 * $VAR environment variable 2505 * (expression) nested expression 2506 * [expr, expr] List 2507 * {arg, arg -> expr} Lambda 2508 * {key: val, key: val} Dictionary 2509 * #{key: val, key: val} Dictionary with literal keys 2510 * 2511 * Also handle: 2512 * ! in front logical NOT 2513 * - in front unary minus 2514 * + in front unary plus (ignored) 2515 * trailing [] subscript in String or List 2516 * trailing .name entry in Dictionary 2517 * trailing ->name() method call 2518 * 2519 * "arg" must point to the first non-white of the expression. 2520 * "arg" is advanced to the next non-white after the recognized expression. 2521 * 2522 * Return OK or FAIL. 2523 */ 2524 static int 2525 eval7( 2526 char_u **arg, 2527 typval_T *rettv, 2528 int evaluate, 2529 int want_string) // after "." operator 2530 { 2531 int len; 2532 char_u *s; 2533 char_u *start_leader, *end_leader; 2534 int ret = OK; 2535 char_u *alias; 2536 2537 /* 2538 * Initialise variable so that clear_tv() can't mistake this for a 2539 * string and free a string that isn't there. 2540 */ 2541 rettv->v_type = VAR_UNKNOWN; 2542 2543 /* 2544 * Skip '!', '-' and '+' characters. They are handled later. 2545 */ 2546 start_leader = *arg; 2547 while (**arg == '!' || **arg == '-' || **arg == '+') 2548 *arg = skipwhite(*arg + 1); 2549 end_leader = *arg; 2550 2551 if (**arg == '.' && (!isdigit(*(*arg + 1)) 2552 #ifdef FEAT_FLOAT 2553 || current_sctx.sc_version < 2 2554 #endif 2555 )) 2556 { 2557 semsg(_(e_invexpr2), *arg); 2558 ++*arg; 2559 return FAIL; 2560 } 2561 2562 switch (**arg) 2563 { 2564 /* 2565 * Number constant. 2566 */ 2567 case '0': 2568 case '1': 2569 case '2': 2570 case '3': 2571 case '4': 2572 case '5': 2573 case '6': 2574 case '7': 2575 case '8': 2576 case '9': 2577 case '.': ret = get_number_tv(arg, rettv, evaluate, want_string); 2578 break; 2579 2580 /* 2581 * String constant: "string". 2582 */ 2583 case '"': ret = get_string_tv(arg, rettv, evaluate); 2584 break; 2585 2586 /* 2587 * Literal string constant: 'str''ing'. 2588 */ 2589 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate); 2590 break; 2591 2592 /* 2593 * List: [expr, expr] 2594 */ 2595 case '[': ret = get_list_tv(arg, rettv, evaluate, TRUE); 2596 break; 2597 2598 /* 2599 * Dictionary: #{key: val, key: val} 2600 */ 2601 case '#': if ((*arg)[1] == '{') 2602 { 2603 ++*arg; 2604 ret = eval_dict(arg, rettv, evaluate, TRUE); 2605 } 2606 else 2607 ret = NOTDONE; 2608 break; 2609 2610 /* 2611 * Lambda: {arg, arg -> expr} 2612 * Dictionary: {'key': val, 'key': val} 2613 */ 2614 case '{': ret = get_lambda_tv(arg, rettv, evaluate); 2615 if (ret == NOTDONE) 2616 ret = eval_dict(arg, rettv, evaluate, FALSE); 2617 break; 2618 2619 /* 2620 * Option value: &name 2621 */ 2622 case '&': ret = get_option_tv(arg, rettv, evaluate); 2623 break; 2624 2625 /* 2626 * Environment variable: $VAR. 2627 */ 2628 case '$': ret = get_env_tv(arg, rettv, evaluate); 2629 break; 2630 2631 /* 2632 * Register contents: @r. 2633 */ 2634 case '@': ++*arg; 2635 if (evaluate) 2636 { 2637 rettv->v_type = VAR_STRING; 2638 rettv->vval.v_string = get_reg_contents(**arg, 2639 GREG_EXPR_SRC); 2640 } 2641 if (**arg != NUL) 2642 ++*arg; 2643 break; 2644 2645 /* 2646 * nested expression: (expression). 2647 */ 2648 case '(': *arg = skipwhite(*arg + 1); 2649 ret = eval1(arg, rettv, evaluate); // recursive! 2650 if (**arg == ')') 2651 ++*arg; 2652 else if (ret == OK) 2653 { 2654 emsg(_(e_missing_close)); 2655 clear_tv(rettv); 2656 ret = FAIL; 2657 } 2658 break; 2659 2660 default: ret = NOTDONE; 2661 break; 2662 } 2663 2664 if (ret == NOTDONE) 2665 { 2666 /* 2667 * Must be a variable or function name. 2668 * Can also be a curly-braces kind of name: {expr}. 2669 */ 2670 s = *arg; 2671 len = get_name_len(arg, &alias, evaluate, TRUE); 2672 if (alias != NULL) 2673 s = alias; 2674 2675 if (len <= 0) 2676 ret = FAIL; 2677 else 2678 { 2679 if (**arg == '(') // recursive! 2680 ret = eval_func(arg, s, len, rettv, evaluate, NULL); 2681 else if (evaluate) 2682 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE); 2683 else 2684 { 2685 check_vars(s, len); 2686 ret = OK; 2687 } 2688 } 2689 vim_free(alias); 2690 } 2691 2692 *arg = skipwhite(*arg); 2693 2694 // Handle following '[', '(' and '.' for expr[expr], expr.name, 2695 // expr(expr), expr->name(expr) 2696 if (ret == OK) 2697 ret = handle_subscript(arg, rettv, evaluate, TRUE, 2698 start_leader, &end_leader); 2699 2700 /* 2701 * Apply logical NOT and unary '-', from right to left, ignore '+'. 2702 */ 2703 if (ret == OK && evaluate && end_leader > start_leader) 2704 ret = eval7_leader(rettv, start_leader, &end_leader); 2705 return ret; 2706 } 2707 2708 /* 2709 * Apply the leading "!" and "-" before an eval7 expression to "rettv". 2710 * Adjusts "end_leaderp" until it is at "start_leader". 2711 */ 2712 static int 2713 eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp) 2714 { 2715 char_u *end_leader = *end_leaderp; 2716 int ret = OK; 2717 int error = FALSE; 2718 varnumber_T val = 0; 2719 #ifdef FEAT_FLOAT 2720 float_T f = 0.0; 2721 2722 if (rettv->v_type == VAR_FLOAT) 2723 f = rettv->vval.v_float; 2724 else 2725 #endif 2726 val = tv_get_number_chk(rettv, &error); 2727 if (error) 2728 { 2729 clear_tv(rettv); 2730 ret = FAIL; 2731 } 2732 else 2733 { 2734 while (end_leader > start_leader) 2735 { 2736 --end_leader; 2737 if (*end_leader == '!') 2738 { 2739 #ifdef FEAT_FLOAT 2740 if (rettv->v_type == VAR_FLOAT) 2741 f = !f; 2742 else 2743 #endif 2744 val = !val; 2745 } 2746 else if (*end_leader == '-') 2747 { 2748 #ifdef FEAT_FLOAT 2749 if (rettv->v_type == VAR_FLOAT) 2750 f = -f; 2751 else 2752 #endif 2753 val = -val; 2754 } 2755 } 2756 #ifdef FEAT_FLOAT 2757 if (rettv->v_type == VAR_FLOAT) 2758 { 2759 clear_tv(rettv); 2760 rettv->vval.v_float = f; 2761 } 2762 else 2763 #endif 2764 { 2765 clear_tv(rettv); 2766 rettv->v_type = VAR_NUMBER; 2767 rettv->vval.v_number = val; 2768 } 2769 } 2770 *end_leaderp = end_leader; 2771 return ret; 2772 } 2773 2774 /* 2775 * Call the function referred to in "rettv". 2776 */ 2777 static int 2778 call_func_rettv( 2779 char_u **arg, 2780 typval_T *rettv, 2781 int evaluate, 2782 dict_T *selfdict, 2783 typval_T *basetv) 2784 { 2785 partial_T *pt = NULL; 2786 funcexe_T funcexe; 2787 typval_T functv; 2788 char_u *s; 2789 int ret; 2790 2791 // need to copy the funcref so that we can clear rettv 2792 if (evaluate) 2793 { 2794 functv = *rettv; 2795 rettv->v_type = VAR_UNKNOWN; 2796 2797 // Invoke the function. Recursive! 2798 if (functv.v_type == VAR_PARTIAL) 2799 { 2800 pt = functv.vval.v_partial; 2801 s = partial_name(pt); 2802 } 2803 else 2804 s = functv.vval.v_string; 2805 } 2806 else 2807 s = (char_u *)""; 2808 2809 CLEAR_FIELD(funcexe); 2810 funcexe.firstline = curwin->w_cursor.lnum; 2811 funcexe.lastline = curwin->w_cursor.lnum; 2812 funcexe.evaluate = evaluate; 2813 funcexe.partial = pt; 2814 funcexe.selfdict = selfdict; 2815 funcexe.basetv = basetv; 2816 ret = get_func_tv(s, -1, rettv, arg, &funcexe); 2817 2818 // Clear the funcref afterwards, so that deleting it while 2819 // evaluating the arguments is possible (see test55). 2820 if (evaluate) 2821 clear_tv(&functv); 2822 2823 return ret; 2824 } 2825 2826 /* 2827 * Evaluate "->method()". 2828 * "*arg" points to the '-'. 2829 * Returns FAIL or OK. "*arg" is advanced to after the ')'. 2830 */ 2831 static int 2832 eval_lambda( 2833 char_u **arg, 2834 typval_T *rettv, 2835 int evaluate, 2836 int verbose) // give error messages 2837 { 2838 typval_T base = *rettv; 2839 int ret; 2840 2841 // Skip over the ->. 2842 *arg += 2; 2843 rettv->v_type = VAR_UNKNOWN; 2844 2845 ret = get_lambda_tv(arg, rettv, evaluate); 2846 if (ret != OK) 2847 return FAIL; 2848 else if (**arg != '(') 2849 { 2850 if (verbose) 2851 { 2852 if (*skipwhite(*arg) == '(') 2853 emsg(_(e_nowhitespace)); 2854 else 2855 semsg(_(e_missing_paren), "lambda"); 2856 } 2857 clear_tv(rettv); 2858 ret = FAIL; 2859 } 2860 else 2861 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base); 2862 2863 // Clear the funcref afterwards, so that deleting it while 2864 // evaluating the arguments is possible (see test55). 2865 if (evaluate) 2866 clear_tv(&base); 2867 2868 return ret; 2869 } 2870 2871 /* 2872 * Evaluate "->method()". 2873 * "*arg" points to the '-'. 2874 * Returns FAIL or OK. "*arg" is advanced to after the ')'. 2875 */ 2876 static int 2877 eval_method( 2878 char_u **arg, 2879 typval_T *rettv, 2880 int evaluate, 2881 int verbose) // give error messages 2882 { 2883 char_u *name; 2884 long len; 2885 char_u *alias; 2886 typval_T base = *rettv; 2887 int ret; 2888 2889 // Skip over the ->. 2890 *arg += 2; 2891 rettv->v_type = VAR_UNKNOWN; 2892 2893 name = *arg; 2894 len = get_name_len(arg, &alias, evaluate, TRUE); 2895 if (alias != NULL) 2896 name = alias; 2897 2898 if (len <= 0) 2899 { 2900 if (verbose) 2901 emsg(_("E260: Missing name after ->")); 2902 ret = FAIL; 2903 } 2904 else 2905 { 2906 if (**arg != '(') 2907 { 2908 if (verbose) 2909 semsg(_(e_missing_paren), name); 2910 ret = FAIL; 2911 } 2912 else if (VIM_ISWHITE((*arg)[-1])) 2913 { 2914 if (verbose) 2915 emsg(_(e_nowhitespace)); 2916 ret = FAIL; 2917 } 2918 else 2919 ret = eval_func(arg, name, len, rettv, evaluate, &base); 2920 } 2921 2922 // Clear the funcref afterwards, so that deleting it while 2923 // evaluating the arguments is possible (see test55). 2924 if (evaluate) 2925 clear_tv(&base); 2926 2927 return ret; 2928 } 2929 2930 /* 2931 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key". 2932 * "*arg" points to the '[' or '.'. 2933 * Returns FAIL or OK. "*arg" is advanced to after the ']'. 2934 */ 2935 static int 2936 eval_index( 2937 char_u **arg, 2938 typval_T *rettv, 2939 int evaluate, 2940 int verbose) // give error messages 2941 { 2942 int empty1 = FALSE, empty2 = FALSE; 2943 typval_T var1, var2; 2944 long i; 2945 long n1, n2 = 0; 2946 long len = -1; 2947 int range = FALSE; 2948 char_u *s; 2949 char_u *key = NULL; 2950 2951 switch (rettv->v_type) 2952 { 2953 case VAR_FUNC: 2954 case VAR_PARTIAL: 2955 if (verbose) 2956 emsg(_("E695: Cannot index a Funcref")); 2957 return FAIL; 2958 case VAR_FLOAT: 2959 #ifdef FEAT_FLOAT 2960 if (verbose) 2961 emsg(_(e_float_as_string)); 2962 return FAIL; 2963 #endif 2964 case VAR_BOOL: 2965 case VAR_SPECIAL: 2966 case VAR_JOB: 2967 case VAR_CHANNEL: 2968 if (verbose) 2969 emsg(_("E909: Cannot index a special variable")); 2970 return FAIL; 2971 case VAR_UNKNOWN: 2972 case VAR_ANY: 2973 case VAR_VOID: 2974 if (evaluate) 2975 return FAIL; 2976 // FALLTHROUGH 2977 2978 case VAR_STRING: 2979 case VAR_NUMBER: 2980 case VAR_LIST: 2981 case VAR_DICT: 2982 case VAR_BLOB: 2983 break; 2984 } 2985 2986 init_tv(&var1); 2987 init_tv(&var2); 2988 if (**arg == '.') 2989 { 2990 /* 2991 * dict.name 2992 */ 2993 key = *arg + 1; 2994 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len) 2995 ; 2996 if (len == 0) 2997 return FAIL; 2998 *arg = skipwhite(key + len); 2999 } 3000 else 3001 { 3002 /* 3003 * something[idx] 3004 * 3005 * Get the (first) variable from inside the []. 3006 */ 3007 *arg = skipwhite(*arg + 1); 3008 if (**arg == ':') 3009 empty1 = TRUE; 3010 else if (eval1(arg, &var1, evaluate) == FAIL) // recursive! 3011 return FAIL; 3012 else if (evaluate && tv_get_string_chk(&var1) == NULL) 3013 { 3014 // not a number or string 3015 clear_tv(&var1); 3016 return FAIL; 3017 } 3018 3019 /* 3020 * Get the second variable from inside the [:]. 3021 */ 3022 if (**arg == ':') 3023 { 3024 range = TRUE; 3025 *arg = skipwhite(*arg + 1); 3026 if (**arg == ']') 3027 empty2 = TRUE; 3028 else if (eval1(arg, &var2, evaluate) == FAIL) // recursive! 3029 { 3030 if (!empty1) 3031 clear_tv(&var1); 3032 return FAIL; 3033 } 3034 else if (evaluate && tv_get_string_chk(&var2) == NULL) 3035 { 3036 // not a number or string 3037 if (!empty1) 3038 clear_tv(&var1); 3039 clear_tv(&var2); 3040 return FAIL; 3041 } 3042 } 3043 3044 // Check for the ']'. 3045 if (**arg != ']') 3046 { 3047 if (verbose) 3048 emsg(_(e_missbrac)); 3049 clear_tv(&var1); 3050 if (range) 3051 clear_tv(&var2); 3052 return FAIL; 3053 } 3054 *arg = skipwhite(*arg + 1); // skip the ']' 3055 } 3056 3057 if (evaluate) 3058 { 3059 n1 = 0; 3060 if (!empty1 && rettv->v_type != VAR_DICT) 3061 { 3062 n1 = tv_get_number(&var1); 3063 clear_tv(&var1); 3064 } 3065 if (range) 3066 { 3067 if (empty2) 3068 n2 = -1; 3069 else 3070 { 3071 n2 = tv_get_number(&var2); 3072 clear_tv(&var2); 3073 } 3074 } 3075 3076 switch (rettv->v_type) 3077 { 3078 case VAR_UNKNOWN: 3079 case VAR_ANY: 3080 case VAR_VOID: 3081 case VAR_FUNC: 3082 case VAR_PARTIAL: 3083 case VAR_FLOAT: 3084 case VAR_BOOL: 3085 case VAR_SPECIAL: 3086 case VAR_JOB: 3087 case VAR_CHANNEL: 3088 break; // not evaluating, skipping over subscript 3089 3090 case VAR_NUMBER: 3091 case VAR_STRING: 3092 s = tv_get_string(rettv); 3093 len = (long)STRLEN(s); 3094 if (range) 3095 { 3096 // The resulting variable is a substring. If the indexes 3097 // are out of range the result is empty. 3098 if (n1 < 0) 3099 { 3100 n1 = len + n1; 3101 if (n1 < 0) 3102 n1 = 0; 3103 } 3104 if (n2 < 0) 3105 n2 = len + n2; 3106 else if (n2 >= len) 3107 n2 = len; 3108 if (n1 >= len || n2 < 0 || n1 > n2) 3109 s = NULL; 3110 else 3111 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1)); 3112 } 3113 else 3114 { 3115 // The resulting variable is a string of a single 3116 // character. If the index is too big or negative the 3117 // result is empty. 3118 if (n1 >= len || n1 < 0) 3119 s = NULL; 3120 else 3121 s = vim_strnsave(s + n1, 1); 3122 } 3123 clear_tv(rettv); 3124 rettv->v_type = VAR_STRING; 3125 rettv->vval.v_string = s; 3126 break; 3127 3128 case VAR_BLOB: 3129 len = blob_len(rettv->vval.v_blob); 3130 if (range) 3131 { 3132 // The resulting variable is a sub-blob. If the indexes 3133 // are out of range the result is empty. 3134 if (n1 < 0) 3135 { 3136 n1 = len + n1; 3137 if (n1 < 0) 3138 n1 = 0; 3139 } 3140 if (n2 < 0) 3141 n2 = len + n2; 3142 else if (n2 >= len) 3143 n2 = len - 1; 3144 if (n1 >= len || n2 < 0 || n1 > n2) 3145 { 3146 clear_tv(rettv); 3147 rettv->v_type = VAR_BLOB; 3148 rettv->vval.v_blob = NULL; 3149 } 3150 else 3151 { 3152 blob_T *blob = blob_alloc(); 3153 3154 if (blob != NULL) 3155 { 3156 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL) 3157 { 3158 blob_free(blob); 3159 return FAIL; 3160 } 3161 blob->bv_ga.ga_len = n2 - n1 + 1; 3162 for (i = n1; i <= n2; i++) 3163 blob_set(blob, i - n1, 3164 blob_get(rettv->vval.v_blob, i)); 3165 3166 clear_tv(rettv); 3167 rettv_blob_set(rettv, blob); 3168 } 3169 } 3170 } 3171 else 3172 { 3173 // The resulting variable is a byte value. 3174 // If the index is too big or negative that is an error. 3175 if (n1 < 0) 3176 n1 = len + n1; 3177 if (n1 < len && n1 >= 0) 3178 { 3179 int v = blob_get(rettv->vval.v_blob, n1); 3180 3181 clear_tv(rettv); 3182 rettv->v_type = VAR_NUMBER; 3183 rettv->vval.v_number = v; 3184 } 3185 else 3186 semsg(_(e_blobidx), n1); 3187 } 3188 break; 3189 3190 case VAR_LIST: 3191 len = list_len(rettv->vval.v_list); 3192 if (n1 < 0) 3193 n1 = len + n1; 3194 if (!empty1 && (n1 < 0 || n1 >= len)) 3195 { 3196 // For a range we allow invalid values and return an empty 3197 // list. A list index out of range is an error. 3198 if (!range) 3199 { 3200 if (verbose) 3201 semsg(_(e_listidx), n1); 3202 return FAIL; 3203 } 3204 n1 = len; 3205 } 3206 if (range) 3207 { 3208 list_T *l; 3209 listitem_T *item; 3210 3211 if (n2 < 0) 3212 n2 = len + n2; 3213 else if (n2 >= len) 3214 n2 = len - 1; 3215 if (!empty2 && (n2 < 0 || n2 + 1 < n1)) 3216 n2 = -1; 3217 l = list_alloc(); 3218 if (l == NULL) 3219 return FAIL; 3220 for (item = list_find(rettv->vval.v_list, n1); 3221 n1 <= n2; ++n1) 3222 { 3223 if (list_append_tv(l, &item->li_tv) == FAIL) 3224 { 3225 list_free(l); 3226 return FAIL; 3227 } 3228 item = item->li_next; 3229 } 3230 clear_tv(rettv); 3231 rettv_list_set(rettv, l); 3232 } 3233 else 3234 { 3235 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1); 3236 clear_tv(rettv); 3237 *rettv = var1; 3238 } 3239 break; 3240 3241 case VAR_DICT: 3242 if (range) 3243 { 3244 if (verbose) 3245 emsg(_(e_dictrange)); 3246 if (len == -1) 3247 clear_tv(&var1); 3248 return FAIL; 3249 } 3250 { 3251 dictitem_T *item; 3252 3253 if (len == -1) 3254 { 3255 key = tv_get_string_chk(&var1); 3256 if (key == NULL) 3257 { 3258 clear_tv(&var1); 3259 return FAIL; 3260 } 3261 } 3262 3263 item = dict_find(rettv->vval.v_dict, key, (int)len); 3264 3265 if (item == NULL && verbose) 3266 semsg(_(e_dictkey), key); 3267 if (len == -1) 3268 clear_tv(&var1); 3269 if (item == NULL) 3270 return FAIL; 3271 3272 copy_tv(&item->di_tv, &var1); 3273 clear_tv(rettv); 3274 *rettv = var1; 3275 } 3276 break; 3277 } 3278 } 3279 3280 return OK; 3281 } 3282 3283 /* 3284 * Get an option value. 3285 * "arg" points to the '&' or '+' before the option name. 3286 * "arg" is advanced to character after the option name. 3287 * Return OK or FAIL. 3288 */ 3289 int 3290 get_option_tv( 3291 char_u **arg, 3292 typval_T *rettv, // when NULL, only check if option exists 3293 int evaluate) 3294 { 3295 char_u *option_end; 3296 long numval; 3297 char_u *stringval; 3298 int opt_type; 3299 int c; 3300 int working = (**arg == '+'); // has("+option") 3301 int ret = OK; 3302 int opt_flags; 3303 3304 /* 3305 * Isolate the option name and find its value. 3306 */ 3307 option_end = find_option_end(arg, &opt_flags); 3308 if (option_end == NULL) 3309 { 3310 if (rettv != NULL) 3311 semsg(_("E112: Option name missing: %s"), *arg); 3312 return FAIL; 3313 } 3314 3315 if (!evaluate) 3316 { 3317 *arg = option_end; 3318 return OK; 3319 } 3320 3321 c = *option_end; 3322 *option_end = NUL; 3323 opt_type = get_option_value(*arg, &numval, 3324 rettv == NULL ? NULL : &stringval, opt_flags); 3325 3326 if (opt_type == -3) // invalid name 3327 { 3328 if (rettv != NULL) 3329 semsg(_(e_unknown_option), *arg); 3330 ret = FAIL; 3331 } 3332 else if (rettv != NULL) 3333 { 3334 if (opt_type == -2) // hidden string option 3335 { 3336 rettv->v_type = VAR_STRING; 3337 rettv->vval.v_string = NULL; 3338 } 3339 else if (opt_type == -1) // hidden number option 3340 { 3341 rettv->v_type = VAR_NUMBER; 3342 rettv->vval.v_number = 0; 3343 } 3344 else if (opt_type == 1) // number option 3345 { 3346 rettv->v_type = VAR_NUMBER; 3347 rettv->vval.v_number = numval; 3348 } 3349 else // string option 3350 { 3351 rettv->v_type = VAR_STRING; 3352 rettv->vval.v_string = stringval; 3353 } 3354 } 3355 else if (working && (opt_type == -2 || opt_type == -1)) 3356 ret = FAIL; 3357 3358 *option_end = c; // put back for error messages 3359 *arg = option_end; 3360 3361 return ret; 3362 } 3363 3364 /* 3365 * Allocate a variable for a number constant. Also deals with "0z" for blob. 3366 * Return OK or FAIL. 3367 */ 3368 int 3369 get_number_tv( 3370 char_u **arg, 3371 typval_T *rettv, 3372 int evaluate, 3373 int want_string UNUSED) 3374 { 3375 int len; 3376 #ifdef FEAT_FLOAT 3377 char_u *p; 3378 int get_float = FALSE; 3379 3380 // We accept a float when the format matches 3381 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very 3382 // strict to avoid backwards compatibility problems. 3383 // With script version 2 and later the leading digit can be 3384 // omitted. 3385 // Don't look for a float after the "." operator, so that 3386 // ":let vers = 1.2.3" doesn't fail. 3387 if (**arg == '.') 3388 p = *arg; 3389 else 3390 p = skipdigits(*arg + 1); 3391 if (!want_string && p[0] == '.' && vim_isdigit(p[1])) 3392 { 3393 get_float = TRUE; 3394 p = skipdigits(p + 2); 3395 if (*p == 'e' || *p == 'E') 3396 { 3397 ++p; 3398 if (*p == '-' || *p == '+') 3399 ++p; 3400 if (!vim_isdigit(*p)) 3401 get_float = FALSE; 3402 else 3403 p = skipdigits(p + 1); 3404 } 3405 if (ASCII_ISALPHA(*p) || *p == '.') 3406 get_float = FALSE; 3407 } 3408 if (get_float) 3409 { 3410 float_T f; 3411 3412 *arg += string2float(*arg, &f); 3413 if (evaluate) 3414 { 3415 rettv->v_type = VAR_FLOAT; 3416 rettv->vval.v_float = f; 3417 } 3418 } 3419 else 3420 #endif 3421 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z')) 3422 { 3423 char_u *bp; 3424 blob_T *blob = NULL; // init for gcc 3425 3426 // Blob constant: 0z0123456789abcdef 3427 if (evaluate) 3428 blob = blob_alloc(); 3429 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2) 3430 { 3431 if (!vim_isxdigit(bp[1])) 3432 { 3433 if (blob != NULL) 3434 { 3435 emsg(_("E973: Blob literal should have an even number of hex characters")); 3436 ga_clear(&blob->bv_ga); 3437 VIM_CLEAR(blob); 3438 } 3439 return FAIL; 3440 } 3441 if (blob != NULL) 3442 ga_append(&blob->bv_ga, 3443 (hex2nr(*bp) << 4) + hex2nr(*(bp+1))); 3444 if (bp[2] == '.' && vim_isxdigit(bp[3])) 3445 ++bp; 3446 } 3447 if (blob != NULL) 3448 rettv_blob_set(rettv, blob); 3449 *arg = bp; 3450 } 3451 else 3452 { 3453 varnumber_T n; 3454 3455 // decimal, hex or octal number 3456 vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4 3457 ? STR2NR_NO_OCT + STR2NR_QUOTE 3458 : STR2NR_ALL, &n, NULL, 0, TRUE); 3459 if (len == 0) 3460 { 3461 semsg(_(e_invexpr2), *arg); 3462 return FAIL; 3463 } 3464 *arg += len; 3465 if (evaluate) 3466 { 3467 rettv->v_type = VAR_NUMBER; 3468 rettv->vval.v_number = n; 3469 } 3470 } 3471 return OK; 3472 } 3473 3474 /* 3475 * Allocate a variable for a string constant. 3476 * Return OK or FAIL. 3477 */ 3478 int 3479 get_string_tv(char_u **arg, typval_T *rettv, int evaluate) 3480 { 3481 char_u *p; 3482 char_u *name; 3483 int extra = 0; 3484 3485 /* 3486 * Find the end of the string, skipping backslashed characters. 3487 */ 3488 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p)) 3489 { 3490 if (*p == '\\' && p[1] != NUL) 3491 { 3492 ++p; 3493 // A "\<x>" form occupies at least 4 characters, and produces up 3494 // to 6 characters: reserve space for 2 extra 3495 if (*p == '<') 3496 extra += 2; 3497 } 3498 } 3499 3500 if (*p != '"') 3501 { 3502 semsg(_("E114: Missing quote: %s"), *arg); 3503 return FAIL; 3504 } 3505 3506 // If only parsing, set *arg and return here 3507 if (!evaluate) 3508 { 3509 *arg = p + 1; 3510 return OK; 3511 } 3512 3513 /* 3514 * Copy the string into allocated memory, handling backslashed 3515 * characters. 3516 */ 3517 name = alloc(p - *arg + extra); 3518 if (name == NULL) 3519 return FAIL; 3520 rettv->v_type = VAR_STRING; 3521 rettv->vval.v_string = name; 3522 3523 for (p = *arg + 1; *p != NUL && *p != '"'; ) 3524 { 3525 if (*p == '\\') 3526 { 3527 switch (*++p) 3528 { 3529 case 'b': *name++ = BS; ++p; break; 3530 case 'e': *name++ = ESC; ++p; break; 3531 case 'f': *name++ = FF; ++p; break; 3532 case 'n': *name++ = NL; ++p; break; 3533 case 'r': *name++ = CAR; ++p; break; 3534 case 't': *name++ = TAB; ++p; break; 3535 3536 case 'X': // hex: "\x1", "\x12" 3537 case 'x': 3538 case 'u': // Unicode: "\u0023" 3539 case 'U': 3540 if (vim_isxdigit(p[1])) 3541 { 3542 int n, nr; 3543 int c = toupper(*p); 3544 3545 if (c == 'X') 3546 n = 2; 3547 else if (*p == 'u') 3548 n = 4; 3549 else 3550 n = 8; 3551 nr = 0; 3552 while (--n >= 0 && vim_isxdigit(p[1])) 3553 { 3554 ++p; 3555 nr = (nr << 4) + hex2nr(*p); 3556 } 3557 ++p; 3558 // For "\u" store the number according to 3559 // 'encoding'. 3560 if (c != 'X') 3561 name += (*mb_char2bytes)(nr, name); 3562 else 3563 *name++ = nr; 3564 } 3565 break; 3566 3567 // octal: "\1", "\12", "\123" 3568 case '0': 3569 case '1': 3570 case '2': 3571 case '3': 3572 case '4': 3573 case '5': 3574 case '6': 3575 case '7': *name = *p++ - '0'; 3576 if (*p >= '0' && *p <= '7') 3577 { 3578 *name = (*name << 3) + *p++ - '0'; 3579 if (*p >= '0' && *p <= '7') 3580 *name = (*name << 3) + *p++ - '0'; 3581 } 3582 ++name; 3583 break; 3584 3585 // Special key, e.g.: "\<C-W>" 3586 case '<': extra = trans_special(&p, name, TRUE, TRUE, 3587 TRUE, NULL); 3588 if (extra != 0) 3589 { 3590 name += extra; 3591 break; 3592 } 3593 // FALLTHROUGH 3594 3595 default: MB_COPY_CHAR(p, name); 3596 break; 3597 } 3598 } 3599 else 3600 MB_COPY_CHAR(p, name); 3601 3602 } 3603 *name = NUL; 3604 if (*p != NUL) // just in case 3605 ++p; 3606 *arg = p; 3607 3608 return OK; 3609 } 3610 3611 /* 3612 * Allocate a variable for a 'str''ing' constant. 3613 * Return OK or FAIL. 3614 */ 3615 int 3616 get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate) 3617 { 3618 char_u *p; 3619 char_u *str; 3620 int reduce = 0; 3621 3622 /* 3623 * Find the end of the string, skipping ''. 3624 */ 3625 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p)) 3626 { 3627 if (*p == '\'') 3628 { 3629 if (p[1] != '\'') 3630 break; 3631 ++reduce; 3632 ++p; 3633 } 3634 } 3635 3636 if (*p != '\'') 3637 { 3638 semsg(_("E115: Missing quote: %s"), *arg); 3639 return FAIL; 3640 } 3641 3642 // If only parsing return after setting "*arg" 3643 if (!evaluate) 3644 { 3645 *arg = p + 1; 3646 return OK; 3647 } 3648 3649 /* 3650 * Copy the string into allocated memory, handling '' to ' reduction. 3651 */ 3652 str = alloc((p - *arg) - reduce); 3653 if (str == NULL) 3654 return FAIL; 3655 rettv->v_type = VAR_STRING; 3656 rettv->vval.v_string = str; 3657 3658 for (p = *arg + 1; *p != NUL; ) 3659 { 3660 if (*p == '\'') 3661 { 3662 if (p[1] != '\'') 3663 break; 3664 ++p; 3665 } 3666 MB_COPY_CHAR(p, str); 3667 } 3668 *str = NUL; 3669 *arg = p + 1; 3670 3671 return OK; 3672 } 3673 3674 /* 3675 * Return the function name of partial "pt". 3676 */ 3677 char_u * 3678 partial_name(partial_T *pt) 3679 { 3680 if (pt->pt_name != NULL) 3681 return pt->pt_name; 3682 return pt->pt_func->uf_name; 3683 } 3684 3685 static void 3686 partial_free(partial_T *pt) 3687 { 3688 int i; 3689 3690 for (i = 0; i < pt->pt_argc; ++i) 3691 clear_tv(&pt->pt_argv[i]); 3692 vim_free(pt->pt_argv); 3693 dict_unref(pt->pt_dict); 3694 if (pt->pt_name != NULL) 3695 { 3696 func_unref(pt->pt_name); 3697 vim_free(pt->pt_name); 3698 } 3699 else 3700 func_ptr_unref(pt->pt_func); 3701 vim_free(pt); 3702 } 3703 3704 /* 3705 * Unreference a closure: decrement the reference count and free it when it 3706 * becomes zero. 3707 */ 3708 void 3709 partial_unref(partial_T *pt) 3710 { 3711 if (pt != NULL && --pt->pt_refcount <= 0) 3712 partial_free(pt); 3713 } 3714 3715 static int tv_equal_recurse_limit; 3716 3717 static int 3718 func_equal( 3719 typval_T *tv1, 3720 typval_T *tv2, 3721 int ic) // ignore case 3722 { 3723 char_u *s1, *s2; 3724 dict_T *d1, *d2; 3725 int a1, a2; 3726 int i; 3727 3728 // empty and NULL function name considered the same 3729 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string 3730 : partial_name(tv1->vval.v_partial); 3731 if (s1 != NULL && *s1 == NUL) 3732 s1 = NULL; 3733 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string 3734 : partial_name(tv2->vval.v_partial); 3735 if (s2 != NULL && *s2 == NUL) 3736 s2 = NULL; 3737 if (s1 == NULL || s2 == NULL) 3738 { 3739 if (s1 != s2) 3740 return FALSE; 3741 } 3742 else if (STRCMP(s1, s2) != 0) 3743 return FALSE; 3744 3745 // empty dict and NULL dict is different 3746 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict; 3747 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict; 3748 if (d1 == NULL || d2 == NULL) 3749 { 3750 if (d1 != d2) 3751 return FALSE; 3752 } 3753 else if (!dict_equal(d1, d2, ic, TRUE)) 3754 return FALSE; 3755 3756 // empty list and no list considered the same 3757 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc; 3758 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc; 3759 if (a1 != a2) 3760 return FALSE; 3761 for (i = 0; i < a1; ++i) 3762 if (!tv_equal(tv1->vval.v_partial->pt_argv + i, 3763 tv2->vval.v_partial->pt_argv + i, ic, TRUE)) 3764 return FALSE; 3765 3766 return TRUE; 3767 } 3768 3769 /* 3770 * Return TRUE if "tv1" and "tv2" have the same value. 3771 * Compares the items just like "==" would compare them, but strings and 3772 * numbers are different. Floats and numbers are also different. 3773 */ 3774 int 3775 tv_equal( 3776 typval_T *tv1, 3777 typval_T *tv2, 3778 int ic, // ignore case 3779 int recursive) // TRUE when used recursively 3780 { 3781 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN]; 3782 char_u *s1, *s2; 3783 static int recursive_cnt = 0; // catch recursive loops 3784 int r; 3785 3786 // Catch lists and dicts that have an endless loop by limiting 3787 // recursiveness to a limit. We guess they are equal then. 3788 // A fixed limit has the problem of still taking an awful long time. 3789 // Reduce the limit every time running into it. That should work fine for 3790 // deeply linked structures that are not recursively linked and catch 3791 // recursiveness quickly. 3792 if (!recursive) 3793 tv_equal_recurse_limit = 1000; 3794 if (recursive_cnt >= tv_equal_recurse_limit) 3795 { 3796 --tv_equal_recurse_limit; 3797 return TRUE; 3798 } 3799 3800 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and 3801 // arguments. 3802 if ((tv1->v_type == VAR_FUNC 3803 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL)) 3804 && (tv2->v_type == VAR_FUNC 3805 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL))) 3806 { 3807 ++recursive_cnt; 3808 r = func_equal(tv1, tv2, ic); 3809 --recursive_cnt; 3810 return r; 3811 } 3812 3813 if (tv1->v_type != tv2->v_type) 3814 return FALSE; 3815 3816 switch (tv1->v_type) 3817 { 3818 case VAR_LIST: 3819 ++recursive_cnt; 3820 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE); 3821 --recursive_cnt; 3822 return r; 3823 3824 case VAR_DICT: 3825 ++recursive_cnt; 3826 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE); 3827 --recursive_cnt; 3828 return r; 3829 3830 case VAR_BLOB: 3831 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob); 3832 3833 case VAR_NUMBER: 3834 case VAR_BOOL: 3835 case VAR_SPECIAL: 3836 return tv1->vval.v_number == tv2->vval.v_number; 3837 3838 case VAR_STRING: 3839 s1 = tv_get_string_buf(tv1, buf1); 3840 s2 = tv_get_string_buf(tv2, buf2); 3841 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0); 3842 3843 case VAR_FLOAT: 3844 #ifdef FEAT_FLOAT 3845 return tv1->vval.v_float == tv2->vval.v_float; 3846 #endif 3847 case VAR_JOB: 3848 #ifdef FEAT_JOB_CHANNEL 3849 return tv1->vval.v_job == tv2->vval.v_job; 3850 #endif 3851 case VAR_CHANNEL: 3852 #ifdef FEAT_JOB_CHANNEL 3853 return tv1->vval.v_channel == tv2->vval.v_channel; 3854 #endif 3855 3856 case VAR_PARTIAL: 3857 return tv1->vval.v_partial == tv2->vval.v_partial; 3858 3859 case VAR_FUNC: 3860 return tv1->vval.v_string == tv2->vval.v_string; 3861 3862 case VAR_UNKNOWN: 3863 case VAR_ANY: 3864 case VAR_VOID: 3865 break; 3866 } 3867 3868 // VAR_UNKNOWN can be the result of a invalid expression, let's say it 3869 // does not equal anything, not even itself. 3870 return FALSE; 3871 } 3872 3873 /* 3874 * Return the next (unique) copy ID. 3875 * Used for serializing nested structures. 3876 */ 3877 int 3878 get_copyID(void) 3879 { 3880 current_copyID += COPYID_INC; 3881 return current_copyID; 3882 } 3883 3884 /* 3885 * Garbage collection for lists and dictionaries. 3886 * 3887 * We use reference counts to be able to free most items right away when they 3888 * are no longer used. But for composite items it's possible that it becomes 3889 * unused while the reference count is > 0: When there is a recursive 3890 * reference. Example: 3891 * :let l = [1, 2, 3] 3892 * :let d = {9: l} 3893 * :let l[1] = d 3894 * 3895 * Since this is quite unusual we handle this with garbage collection: every 3896 * once in a while find out which lists and dicts are not referenced from any 3897 * variable. 3898 * 3899 * Here is a good reference text about garbage collection (refers to Python 3900 * but it applies to all reference-counting mechanisms): 3901 * http://python.ca/nas/python/gc/ 3902 */ 3903 3904 /* 3905 * Do garbage collection for lists and dicts. 3906 * When "testing" is TRUE this is called from test_garbagecollect_now(). 3907 * Return TRUE if some memory was freed. 3908 */ 3909 int 3910 garbage_collect(int testing) 3911 { 3912 int copyID; 3913 int abort = FALSE; 3914 buf_T *buf; 3915 win_T *wp; 3916 int did_free = FALSE; 3917 tabpage_T *tp; 3918 3919 if (!testing) 3920 { 3921 // Only do this once. 3922 want_garbage_collect = FALSE; 3923 may_garbage_collect = FALSE; 3924 garbage_collect_at_exit = FALSE; 3925 } 3926 3927 // The execution stack can grow big, limit the size. 3928 if (exestack.ga_maxlen - exestack.ga_len > 500) 3929 { 3930 size_t new_len; 3931 char_u *pp; 3932 int n; 3933 3934 // Keep 150% of the current size, with a minimum of the growth size. 3935 n = exestack.ga_len / 2; 3936 if (n < exestack.ga_growsize) 3937 n = exestack.ga_growsize; 3938 3939 // Don't make it bigger though. 3940 if (exestack.ga_len + n < exestack.ga_maxlen) 3941 { 3942 new_len = exestack.ga_itemsize * (exestack.ga_len + n); 3943 pp = vim_realloc(exestack.ga_data, new_len); 3944 if (pp == NULL) 3945 return FAIL; 3946 exestack.ga_maxlen = exestack.ga_len + n; 3947 exestack.ga_data = pp; 3948 } 3949 } 3950 3951 // We advance by two because we add one for items referenced through 3952 // previous_funccal. 3953 copyID = get_copyID(); 3954 3955 /* 3956 * 1. Go through all accessible variables and mark all lists and dicts 3957 * with copyID. 3958 */ 3959 3960 // Don't free variables in the previous_funccal list unless they are only 3961 // referenced through previous_funccal. This must be first, because if 3962 // the item is referenced elsewhere the funccal must not be freed. 3963 abort = abort || set_ref_in_previous_funccal(copyID); 3964 3965 // script-local variables 3966 abort = abort || garbage_collect_scriptvars(copyID); 3967 3968 // buffer-local variables 3969 FOR_ALL_BUFFERS(buf) 3970 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID, 3971 NULL, NULL); 3972 3973 // window-local variables 3974 FOR_ALL_TAB_WINDOWS(tp, wp) 3975 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID, 3976 NULL, NULL); 3977 if (aucmd_win != NULL) 3978 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID, 3979 NULL, NULL); 3980 #ifdef FEAT_PROP_POPUP 3981 FOR_ALL_POPUPWINS(wp) 3982 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID, 3983 NULL, NULL); 3984 FOR_ALL_TABPAGES(tp) 3985 FOR_ALL_POPUPWINS_IN_TAB(tp, wp) 3986 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID, 3987 NULL, NULL); 3988 #endif 3989 3990 // tabpage-local variables 3991 FOR_ALL_TABPAGES(tp) 3992 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID, 3993 NULL, NULL); 3994 // global variables 3995 abort = abort || garbage_collect_globvars(copyID); 3996 3997 // function-local variables 3998 abort = abort || set_ref_in_call_stack(copyID); 3999 4000 // named functions (matters for closures) 4001 abort = abort || set_ref_in_functions(copyID); 4002 4003 // function call arguments, if v:testing is set. 4004 abort = abort || set_ref_in_func_args(copyID); 4005 4006 // v: vars 4007 abort = abort || garbage_collect_vimvars(copyID); 4008 4009 // callbacks in buffers 4010 abort = abort || set_ref_in_buffers(copyID); 4011 4012 #ifdef FEAT_LUA 4013 abort = abort || set_ref_in_lua(copyID); 4014 #endif 4015 4016 #ifdef FEAT_PYTHON 4017 abort = abort || set_ref_in_python(copyID); 4018 #endif 4019 4020 #ifdef FEAT_PYTHON3 4021 abort = abort || set_ref_in_python3(copyID); 4022 #endif 4023 4024 #ifdef FEAT_JOB_CHANNEL 4025 abort = abort || set_ref_in_channel(copyID); 4026 abort = abort || set_ref_in_job(copyID); 4027 #endif 4028 #ifdef FEAT_NETBEANS_INTG 4029 abort = abort || set_ref_in_nb_channel(copyID); 4030 #endif 4031 4032 #ifdef FEAT_TIMERS 4033 abort = abort || set_ref_in_timer(copyID); 4034 #endif 4035 4036 #ifdef FEAT_QUICKFIX 4037 abort = abort || set_ref_in_quickfix(copyID); 4038 #endif 4039 4040 #ifdef FEAT_TERMINAL 4041 abort = abort || set_ref_in_term(copyID); 4042 #endif 4043 4044 #ifdef FEAT_PROP_POPUP 4045 abort = abort || set_ref_in_popups(copyID); 4046 #endif 4047 4048 if (!abort) 4049 { 4050 /* 4051 * 2. Free lists and dictionaries that are not referenced. 4052 */ 4053 did_free = free_unref_items(copyID); 4054 4055 /* 4056 * 3. Check if any funccal can be freed now. 4057 * This may call us back recursively. 4058 */ 4059 free_unref_funccal(copyID, testing); 4060 } 4061 else if (p_verbose > 0) 4062 { 4063 verb_msg(_("Not enough memory to set references, garbage collection aborted!")); 4064 } 4065 4066 return did_free; 4067 } 4068 4069 /* 4070 * Free lists, dictionaries, channels and jobs that are no longer referenced. 4071 */ 4072 static int 4073 free_unref_items(int copyID) 4074 { 4075 int did_free = FALSE; 4076 4077 // Let all "free" functions know that we are here. This means no 4078 // dictionaries, lists, channels or jobs are to be freed, because we will 4079 // do that here. 4080 in_free_unref_items = TRUE; 4081 4082 /* 4083 * PASS 1: free the contents of the items. We don't free the items 4084 * themselves yet, so that it is possible to decrement refcount counters 4085 */ 4086 4087 // Go through the list of dicts and free items without the copyID. 4088 did_free |= dict_free_nonref(copyID); 4089 4090 // Go through the list of lists and free items without the copyID. 4091 did_free |= list_free_nonref(copyID); 4092 4093 #ifdef FEAT_JOB_CHANNEL 4094 // Go through the list of jobs and free items without the copyID. This 4095 // must happen before doing channels, because jobs refer to channels, but 4096 // the reference from the channel to the job isn't tracked. 4097 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK); 4098 4099 // Go through the list of channels and free items without the copyID. 4100 did_free |= free_unused_channels_contents(copyID, COPYID_MASK); 4101 #endif 4102 4103 /* 4104 * PASS 2: free the items themselves. 4105 */ 4106 dict_free_items(copyID); 4107 list_free_items(copyID); 4108 4109 #ifdef FEAT_JOB_CHANNEL 4110 // Go through the list of jobs and free items without the copyID. This 4111 // must happen before doing channels, because jobs refer to channels, but 4112 // the reference from the channel to the job isn't tracked. 4113 free_unused_jobs(copyID, COPYID_MASK); 4114 4115 // Go through the list of channels and free items without the copyID. 4116 free_unused_channels(copyID, COPYID_MASK); 4117 #endif 4118 4119 in_free_unref_items = FALSE; 4120 4121 return did_free; 4122 } 4123 4124 /* 4125 * Mark all lists and dicts referenced through hashtab "ht" with "copyID". 4126 * "list_stack" is used to add lists to be marked. Can be NULL. 4127 * 4128 * Returns TRUE if setting references failed somehow. 4129 */ 4130 int 4131 set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack) 4132 { 4133 int todo; 4134 int abort = FALSE; 4135 hashitem_T *hi; 4136 hashtab_T *cur_ht; 4137 ht_stack_T *ht_stack = NULL; 4138 ht_stack_T *tempitem; 4139 4140 cur_ht = ht; 4141 for (;;) 4142 { 4143 if (!abort) 4144 { 4145 // Mark each item in the hashtab. If the item contains a hashtab 4146 // it is added to ht_stack, if it contains a list it is added to 4147 // list_stack. 4148 todo = (int)cur_ht->ht_used; 4149 for (hi = cur_ht->ht_array; todo > 0; ++hi) 4150 if (!HASHITEM_EMPTY(hi)) 4151 { 4152 --todo; 4153 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID, 4154 &ht_stack, list_stack); 4155 } 4156 } 4157 4158 if (ht_stack == NULL) 4159 break; 4160 4161 // take an item from the stack 4162 cur_ht = ht_stack->ht; 4163 tempitem = ht_stack; 4164 ht_stack = ht_stack->prev; 4165 free(tempitem); 4166 } 4167 4168 return abort; 4169 } 4170 4171 /* 4172 * Mark a dict and its items with "copyID". 4173 * Returns TRUE if setting references failed somehow. 4174 */ 4175 int 4176 set_ref_in_dict(dict_T *d, int copyID) 4177 { 4178 if (d != NULL && d->dv_copyID != copyID) 4179 { 4180 d->dv_copyID = copyID; 4181 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL); 4182 } 4183 return FALSE; 4184 } 4185 4186 /* 4187 * Mark a list and its items with "copyID". 4188 * Returns TRUE if setting references failed somehow. 4189 */ 4190 int 4191 set_ref_in_list(list_T *ll, int copyID) 4192 { 4193 if (ll != NULL && ll->lv_copyID != copyID) 4194 { 4195 ll->lv_copyID = copyID; 4196 return set_ref_in_list_items(ll, copyID, NULL); 4197 } 4198 return FALSE; 4199 } 4200 4201 /* 4202 * Mark all lists and dicts referenced through list "l" with "copyID". 4203 * "ht_stack" is used to add hashtabs to be marked. Can be NULL. 4204 * 4205 * Returns TRUE if setting references failed somehow. 4206 */ 4207 int 4208 set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack) 4209 { 4210 listitem_T *li; 4211 int abort = FALSE; 4212 list_T *cur_l; 4213 list_stack_T *list_stack = NULL; 4214 list_stack_T *tempitem; 4215 4216 cur_l = l; 4217 for (;;) 4218 { 4219 if (!abort && cur_l->lv_first != &range_list_item) 4220 // Mark each item in the list. If the item contains a hashtab 4221 // it is added to ht_stack, if it contains a list it is added to 4222 // list_stack. 4223 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next) 4224 abort = abort || set_ref_in_item(&li->li_tv, copyID, 4225 ht_stack, &list_stack); 4226 if (list_stack == NULL) 4227 break; 4228 4229 // take an item from the stack 4230 cur_l = list_stack->list; 4231 tempitem = list_stack; 4232 list_stack = list_stack->prev; 4233 free(tempitem); 4234 } 4235 4236 return abort; 4237 } 4238 4239 /* 4240 * Mark all lists and dicts referenced through typval "tv" with "copyID". 4241 * "list_stack" is used to add lists to be marked. Can be NULL. 4242 * "ht_stack" is used to add hashtabs to be marked. Can be NULL. 4243 * 4244 * Returns TRUE if setting references failed somehow. 4245 */ 4246 int 4247 set_ref_in_item( 4248 typval_T *tv, 4249 int copyID, 4250 ht_stack_T **ht_stack, 4251 list_stack_T **list_stack) 4252 { 4253 int abort = FALSE; 4254 4255 if (tv->v_type == VAR_DICT) 4256 { 4257 dict_T *dd = tv->vval.v_dict; 4258 4259 if (dd != NULL && dd->dv_copyID != copyID) 4260 { 4261 // Didn't see this dict yet. 4262 dd->dv_copyID = copyID; 4263 if (ht_stack == NULL) 4264 { 4265 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack); 4266 } 4267 else 4268 { 4269 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T)); 4270 if (newitem == NULL) 4271 abort = TRUE; 4272 else 4273 { 4274 newitem->ht = &dd->dv_hashtab; 4275 newitem->prev = *ht_stack; 4276 *ht_stack = newitem; 4277 } 4278 } 4279 } 4280 } 4281 else if (tv->v_type == VAR_LIST) 4282 { 4283 list_T *ll = tv->vval.v_list; 4284 4285 if (ll != NULL && ll->lv_copyID != copyID) 4286 { 4287 // Didn't see this list yet. 4288 ll->lv_copyID = copyID; 4289 if (list_stack == NULL) 4290 { 4291 abort = set_ref_in_list_items(ll, copyID, ht_stack); 4292 } 4293 else 4294 { 4295 list_stack_T *newitem = (list_stack_T*)malloc( 4296 sizeof(list_stack_T)); 4297 if (newitem == NULL) 4298 abort = TRUE; 4299 else 4300 { 4301 newitem->list = ll; 4302 newitem->prev = *list_stack; 4303 *list_stack = newitem; 4304 } 4305 } 4306 } 4307 } 4308 else if (tv->v_type == VAR_FUNC) 4309 { 4310 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID); 4311 } 4312 else if (tv->v_type == VAR_PARTIAL) 4313 { 4314 partial_T *pt = tv->vval.v_partial; 4315 int i; 4316 4317 // A partial does not have a copyID, because it cannot contain itself. 4318 if (pt != NULL) 4319 { 4320 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID); 4321 4322 if (pt->pt_dict != NULL) 4323 { 4324 typval_T dtv; 4325 4326 dtv.v_type = VAR_DICT; 4327 dtv.vval.v_dict = pt->pt_dict; 4328 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4329 } 4330 4331 for (i = 0; i < pt->pt_argc; ++i) 4332 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID, 4333 ht_stack, list_stack); 4334 } 4335 } 4336 #ifdef FEAT_JOB_CHANNEL 4337 else if (tv->v_type == VAR_JOB) 4338 { 4339 job_T *job = tv->vval.v_job; 4340 typval_T dtv; 4341 4342 if (job != NULL && job->jv_copyID != copyID) 4343 { 4344 job->jv_copyID = copyID; 4345 if (job->jv_channel != NULL) 4346 { 4347 dtv.v_type = VAR_CHANNEL; 4348 dtv.vval.v_channel = job->jv_channel; 4349 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4350 } 4351 if (job->jv_exit_cb.cb_partial != NULL) 4352 { 4353 dtv.v_type = VAR_PARTIAL; 4354 dtv.vval.v_partial = job->jv_exit_cb.cb_partial; 4355 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4356 } 4357 } 4358 } 4359 else if (tv->v_type == VAR_CHANNEL) 4360 { 4361 channel_T *ch =tv->vval.v_channel; 4362 ch_part_T part; 4363 typval_T dtv; 4364 jsonq_T *jq; 4365 cbq_T *cq; 4366 4367 if (ch != NULL && ch->ch_copyID != copyID) 4368 { 4369 ch->ch_copyID = copyID; 4370 for (part = PART_SOCK; part < PART_COUNT; ++part) 4371 { 4372 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL; 4373 jq = jq->jq_next) 4374 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack); 4375 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL; 4376 cq = cq->cq_next) 4377 if (cq->cq_callback.cb_partial != NULL) 4378 { 4379 dtv.v_type = VAR_PARTIAL; 4380 dtv.vval.v_partial = cq->cq_callback.cb_partial; 4381 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4382 } 4383 if (ch->ch_part[part].ch_callback.cb_partial != NULL) 4384 { 4385 dtv.v_type = VAR_PARTIAL; 4386 dtv.vval.v_partial = 4387 ch->ch_part[part].ch_callback.cb_partial; 4388 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4389 } 4390 } 4391 if (ch->ch_callback.cb_partial != NULL) 4392 { 4393 dtv.v_type = VAR_PARTIAL; 4394 dtv.vval.v_partial = ch->ch_callback.cb_partial; 4395 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4396 } 4397 if (ch->ch_close_cb.cb_partial != NULL) 4398 { 4399 dtv.v_type = VAR_PARTIAL; 4400 dtv.vval.v_partial = ch->ch_close_cb.cb_partial; 4401 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4402 } 4403 } 4404 } 4405 #endif 4406 return abort; 4407 } 4408 4409 /* 4410 * Return a string with the string representation of a variable. 4411 * If the memory is allocated "tofree" is set to it, otherwise NULL. 4412 * "numbuf" is used for a number. 4413 * When "copyID" is not NULL replace recursive lists and dicts with "...". 4414 * When both "echo_style" and "composite_val" are FALSE, put quotes around 4415 * stings as "string()", otherwise does not put quotes around strings, as 4416 * ":echo" displays values. 4417 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists 4418 * are replaced with "...". 4419 * May return NULL. 4420 */ 4421 char_u * 4422 echo_string_core( 4423 typval_T *tv, 4424 char_u **tofree, 4425 char_u *numbuf, 4426 int copyID, 4427 int echo_style, 4428 int restore_copyID, 4429 int composite_val) 4430 { 4431 static int recurse = 0; 4432 char_u *r = NULL; 4433 4434 if (recurse >= DICT_MAXNEST) 4435 { 4436 if (!did_echo_string_emsg) 4437 { 4438 // Only give this message once for a recursive call to avoid 4439 // flooding the user with errors. And stop iterating over lists 4440 // and dicts. 4441 did_echo_string_emsg = TRUE; 4442 emsg(_("E724: variable nested too deep for displaying")); 4443 } 4444 *tofree = NULL; 4445 return (char_u *)"{E724}"; 4446 } 4447 ++recurse; 4448 4449 switch (tv->v_type) 4450 { 4451 case VAR_STRING: 4452 if (echo_style && !composite_val) 4453 { 4454 *tofree = NULL; 4455 r = tv->vval.v_string; 4456 if (r == NULL) 4457 r = (char_u *)""; 4458 } 4459 else 4460 { 4461 *tofree = string_quote(tv->vval.v_string, FALSE); 4462 r = *tofree; 4463 } 4464 break; 4465 4466 case VAR_FUNC: 4467 if (echo_style) 4468 { 4469 *tofree = NULL; 4470 r = tv->vval.v_string; 4471 } 4472 else 4473 { 4474 *tofree = string_quote(tv->vval.v_string, TRUE); 4475 r = *tofree; 4476 } 4477 break; 4478 4479 case VAR_PARTIAL: 4480 { 4481 partial_T *pt = tv->vval.v_partial; 4482 char_u *fname = string_quote(pt == NULL ? NULL 4483 : partial_name(pt), FALSE); 4484 garray_T ga; 4485 int i; 4486 char_u *tf; 4487 4488 ga_init2(&ga, 1, 100); 4489 ga_concat(&ga, (char_u *)"function("); 4490 if (fname != NULL) 4491 { 4492 ga_concat(&ga, fname); 4493 vim_free(fname); 4494 } 4495 if (pt != NULL && pt->pt_argc > 0) 4496 { 4497 ga_concat(&ga, (char_u *)", ["); 4498 for (i = 0; i < pt->pt_argc; ++i) 4499 { 4500 if (i > 0) 4501 ga_concat(&ga, (char_u *)", "); 4502 ga_concat(&ga, 4503 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID)); 4504 vim_free(tf); 4505 } 4506 ga_concat(&ga, (char_u *)"]"); 4507 } 4508 if (pt != NULL && pt->pt_dict != NULL) 4509 { 4510 typval_T dtv; 4511 4512 ga_concat(&ga, (char_u *)", "); 4513 dtv.v_type = VAR_DICT; 4514 dtv.vval.v_dict = pt->pt_dict; 4515 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID)); 4516 vim_free(tf); 4517 } 4518 ga_concat(&ga, (char_u *)")"); 4519 4520 *tofree = ga.ga_data; 4521 r = *tofree; 4522 break; 4523 } 4524 4525 case VAR_BLOB: 4526 r = blob2string(tv->vval.v_blob, tofree, numbuf); 4527 break; 4528 4529 case VAR_LIST: 4530 if (tv->vval.v_list == NULL) 4531 { 4532 // NULL list is equivalent to empty list. 4533 *tofree = NULL; 4534 r = (char_u *)"[]"; 4535 } 4536 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID 4537 && tv->vval.v_list->lv_len > 0) 4538 { 4539 *tofree = NULL; 4540 r = (char_u *)"[...]"; 4541 } 4542 else 4543 { 4544 int old_copyID = tv->vval.v_list->lv_copyID; 4545 4546 tv->vval.v_list->lv_copyID = copyID; 4547 *tofree = list2string(tv, copyID, restore_copyID); 4548 if (restore_copyID) 4549 tv->vval.v_list->lv_copyID = old_copyID; 4550 r = *tofree; 4551 } 4552 break; 4553 4554 case VAR_DICT: 4555 if (tv->vval.v_dict == NULL) 4556 { 4557 // NULL dict is equivalent to empty dict. 4558 *tofree = NULL; 4559 r = (char_u *)"{}"; 4560 } 4561 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID 4562 && tv->vval.v_dict->dv_hashtab.ht_used != 0) 4563 { 4564 *tofree = NULL; 4565 r = (char_u *)"{...}"; 4566 } 4567 else 4568 { 4569 int old_copyID = tv->vval.v_dict->dv_copyID; 4570 4571 tv->vval.v_dict->dv_copyID = copyID; 4572 *tofree = dict2string(tv, copyID, restore_copyID); 4573 if (restore_copyID) 4574 tv->vval.v_dict->dv_copyID = old_copyID; 4575 r = *tofree; 4576 } 4577 break; 4578 4579 case VAR_NUMBER: 4580 case VAR_UNKNOWN: 4581 case VAR_ANY: 4582 case VAR_VOID: 4583 *tofree = NULL; 4584 r = tv_get_string_buf(tv, numbuf); 4585 break; 4586 4587 case VAR_JOB: 4588 case VAR_CHANNEL: 4589 *tofree = NULL; 4590 r = tv_get_string_buf(tv, numbuf); 4591 if (composite_val) 4592 { 4593 *tofree = string_quote(r, FALSE); 4594 r = *tofree; 4595 } 4596 break; 4597 4598 case VAR_FLOAT: 4599 #ifdef FEAT_FLOAT 4600 *tofree = NULL; 4601 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float); 4602 r = numbuf; 4603 break; 4604 #endif 4605 4606 case VAR_BOOL: 4607 case VAR_SPECIAL: 4608 *tofree = NULL; 4609 r = (char_u *)get_var_special_name(tv->vval.v_number); 4610 break; 4611 } 4612 4613 if (--recurse == 0) 4614 did_echo_string_emsg = FALSE; 4615 return r; 4616 } 4617 4618 /* 4619 * Return a string with the string representation of a variable. 4620 * If the memory is allocated "tofree" is set to it, otherwise NULL. 4621 * "numbuf" is used for a number. 4622 * Does not put quotes around strings, as ":echo" displays values. 4623 * When "copyID" is not NULL replace recursive lists and dicts with "...". 4624 * May return NULL. 4625 */ 4626 char_u * 4627 echo_string( 4628 typval_T *tv, 4629 char_u **tofree, 4630 char_u *numbuf, 4631 int copyID) 4632 { 4633 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE); 4634 } 4635 4636 /* 4637 * Return a string with the string representation of a variable. 4638 * If the memory is allocated "tofree" is set to it, otherwise NULL. 4639 * "numbuf" is used for a number. 4640 * Puts quotes around strings, so that they can be parsed back by eval(). 4641 * May return NULL. 4642 */ 4643 char_u * 4644 tv2string( 4645 typval_T *tv, 4646 char_u **tofree, 4647 char_u *numbuf, 4648 int copyID) 4649 { 4650 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE); 4651 } 4652 4653 /* 4654 * Return string "str" in ' quotes, doubling ' characters. 4655 * If "str" is NULL an empty string is assumed. 4656 * If "function" is TRUE make it function('string'). 4657 */ 4658 char_u * 4659 string_quote(char_u *str, int function) 4660 { 4661 unsigned len; 4662 char_u *p, *r, *s; 4663 4664 len = (function ? 13 : 3); 4665 if (str != NULL) 4666 { 4667 len += (unsigned)STRLEN(str); 4668 for (p = str; *p != NUL; MB_PTR_ADV(p)) 4669 if (*p == '\'') 4670 ++len; 4671 } 4672 s = r = alloc(len); 4673 if (r != NULL) 4674 { 4675 if (function) 4676 { 4677 STRCPY(r, "function('"); 4678 r += 10; 4679 } 4680 else 4681 *r++ = '\''; 4682 if (str != NULL) 4683 for (p = str; *p != NUL; ) 4684 { 4685 if (*p == '\'') 4686 *r++ = '\''; 4687 MB_COPY_CHAR(p, r); 4688 } 4689 *r++ = '\''; 4690 if (function) 4691 *r++ = ')'; 4692 *r++ = NUL; 4693 } 4694 return s; 4695 } 4696 4697 #if defined(FEAT_FLOAT) || defined(PROTO) 4698 /* 4699 * Convert the string "text" to a floating point number. 4700 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure 4701 * this always uses a decimal point. 4702 * Returns the length of the text that was consumed. 4703 */ 4704 int 4705 string2float( 4706 char_u *text, 4707 float_T *value) // result stored here 4708 { 4709 char *s = (char *)text; 4710 float_T f; 4711 4712 // MS-Windows does not deal with "inf" and "nan" properly. 4713 if (STRNICMP(text, "inf", 3) == 0) 4714 { 4715 *value = INFINITY; 4716 return 3; 4717 } 4718 if (STRNICMP(text, "-inf", 3) == 0) 4719 { 4720 *value = -INFINITY; 4721 return 4; 4722 } 4723 if (STRNICMP(text, "nan", 3) == 0) 4724 { 4725 *value = NAN; 4726 return 3; 4727 } 4728 f = strtod(s, &s); 4729 *value = f; 4730 return (int)((char_u *)s - text); 4731 } 4732 #endif 4733 4734 /* 4735 * Get the value of an environment variable. 4736 * "arg" is pointing to the '$'. It is advanced to after the name. 4737 * If the environment variable was not set, silently assume it is empty. 4738 * Return FAIL if the name is invalid. 4739 */ 4740 int 4741 get_env_tv(char_u **arg, typval_T *rettv, int evaluate) 4742 { 4743 char_u *string = NULL; 4744 int len; 4745 int cc; 4746 char_u *name; 4747 int mustfree = FALSE; 4748 4749 ++*arg; 4750 name = *arg; 4751 len = get_env_len(arg); 4752 if (evaluate) 4753 { 4754 if (len == 0) 4755 return FAIL; // invalid empty name 4756 4757 cc = name[len]; 4758 name[len] = NUL; 4759 // first try vim_getenv(), fast for normal environment vars 4760 string = vim_getenv(name, &mustfree); 4761 if (string != NULL && *string != NUL) 4762 { 4763 if (!mustfree) 4764 string = vim_strsave(string); 4765 } 4766 else 4767 { 4768 if (mustfree) 4769 vim_free(string); 4770 4771 // next try expanding things like $VIM and ${HOME} 4772 string = expand_env_save(name - 1); 4773 if (string != NULL && *string == '$') 4774 VIM_CLEAR(string); 4775 } 4776 name[len] = cc; 4777 4778 rettv->v_type = VAR_STRING; 4779 rettv->vval.v_string = string; 4780 } 4781 4782 return OK; 4783 } 4784 4785 /* 4786 * Translate a String variable into a position. 4787 * Returns NULL when there is an error. 4788 */ 4789 pos_T * 4790 var2fpos( 4791 typval_T *varp, 4792 int dollar_lnum, // TRUE when $ is last line 4793 int *fnum) // set to fnum for '0, 'A, etc. 4794 { 4795 char_u *name; 4796 static pos_T pos; 4797 pos_T *pp; 4798 4799 // Argument can be [lnum, col, coladd]. 4800 if (varp->v_type == VAR_LIST) 4801 { 4802 list_T *l; 4803 int len; 4804 int error = FALSE; 4805 listitem_T *li; 4806 4807 l = varp->vval.v_list; 4808 if (l == NULL) 4809 return NULL; 4810 4811 // Get the line number 4812 pos.lnum = list_find_nr(l, 0L, &error); 4813 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count) 4814 return NULL; // invalid line number 4815 4816 // Get the column number 4817 pos.col = list_find_nr(l, 1L, &error); 4818 if (error) 4819 return NULL; 4820 len = (long)STRLEN(ml_get(pos.lnum)); 4821 4822 // We accept "$" for the column number: last column. 4823 li = list_find(l, 1L); 4824 if (li != NULL && li->li_tv.v_type == VAR_STRING 4825 && li->li_tv.vval.v_string != NULL 4826 && STRCMP(li->li_tv.vval.v_string, "$") == 0) 4827 pos.col = len + 1; 4828 4829 // Accept a position up to the NUL after the line. 4830 if (pos.col == 0 || (int)pos.col > len + 1) 4831 return NULL; // invalid column number 4832 --pos.col; 4833 4834 // Get the virtual offset. Defaults to zero. 4835 pos.coladd = list_find_nr(l, 2L, &error); 4836 if (error) 4837 pos.coladd = 0; 4838 4839 return &pos; 4840 } 4841 4842 name = tv_get_string_chk(varp); 4843 if (name == NULL) 4844 return NULL; 4845 if (name[0] == '.') // cursor 4846 return &curwin->w_cursor; 4847 if (name[0] == 'v' && name[1] == NUL) // Visual start 4848 { 4849 if (VIsual_active) 4850 return &VIsual; 4851 return &curwin->w_cursor; 4852 } 4853 if (name[0] == '\'') // mark 4854 { 4855 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum); 4856 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0) 4857 return NULL; 4858 return pp; 4859 } 4860 4861 pos.coladd = 0; 4862 4863 if (name[0] == 'w' && dollar_lnum) 4864 { 4865 pos.col = 0; 4866 if (name[1] == '0') // "w0": first visible line 4867 { 4868 update_topline(); 4869 // In silent Ex mode topline is zero, but that's not a valid line 4870 // number; use one instead. 4871 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1; 4872 return &pos; 4873 } 4874 else if (name[1] == '$') // "w$": last visible line 4875 { 4876 validate_botline(); 4877 // In silent Ex mode botline is zero, return zero then. 4878 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0; 4879 return &pos; 4880 } 4881 } 4882 else if (name[0] == '$') // last column or line 4883 { 4884 if (dollar_lnum) 4885 { 4886 pos.lnum = curbuf->b_ml.ml_line_count; 4887 pos.col = 0; 4888 } 4889 else 4890 { 4891 pos.lnum = curwin->w_cursor.lnum; 4892 pos.col = (colnr_T)STRLEN(ml_get_curline()); 4893 } 4894 return &pos; 4895 } 4896 return NULL; 4897 } 4898 4899 /* 4900 * Convert list in "arg" into a position and optional file number. 4901 * When "fnump" is NULL there is no file number, only 3 items. 4902 * Note that the column is passed on as-is, the caller may want to decrement 4903 * it to use 1 for the first column. 4904 * Return FAIL when conversion is not possible, doesn't check the position for 4905 * validity. 4906 */ 4907 int 4908 list2fpos( 4909 typval_T *arg, 4910 pos_T *posp, 4911 int *fnump, 4912 colnr_T *curswantp) 4913 { 4914 list_T *l = arg->vval.v_list; 4915 long i = 0; 4916 long n; 4917 4918 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only 4919 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional. 4920 if (arg->v_type != VAR_LIST 4921 || l == NULL 4922 || l->lv_len < (fnump == NULL ? 2 : 3) 4923 || l->lv_len > (fnump == NULL ? 4 : 5)) 4924 return FAIL; 4925 4926 if (fnump != NULL) 4927 { 4928 n = list_find_nr(l, i++, NULL); // fnum 4929 if (n < 0) 4930 return FAIL; 4931 if (n == 0) 4932 n = curbuf->b_fnum; // current buffer 4933 *fnump = n; 4934 } 4935 4936 n = list_find_nr(l, i++, NULL); // lnum 4937 if (n < 0) 4938 return FAIL; 4939 posp->lnum = n; 4940 4941 n = list_find_nr(l, i++, NULL); // col 4942 if (n < 0) 4943 return FAIL; 4944 posp->col = n; 4945 4946 n = list_find_nr(l, i, NULL); // off 4947 if (n < 0) 4948 posp->coladd = 0; 4949 else 4950 posp->coladd = n; 4951 4952 if (curswantp != NULL) 4953 *curswantp = list_find_nr(l, i + 1, NULL); // curswant 4954 4955 return OK; 4956 } 4957 4958 /* 4959 * Get the length of an environment variable name. 4960 * Advance "arg" to the first character after the name. 4961 * Return 0 for error. 4962 */ 4963 int 4964 get_env_len(char_u **arg) 4965 { 4966 char_u *p; 4967 int len; 4968 4969 for (p = *arg; vim_isIDc(*p); ++p) 4970 ; 4971 if (p == *arg) // no name found 4972 return 0; 4973 4974 len = (int)(p - *arg); 4975 *arg = p; 4976 return len; 4977 } 4978 4979 /* 4980 * Get the length of the name of a function or internal variable. 4981 * "arg" is advanced to the first non-white character after the name. 4982 * Return 0 if something is wrong. 4983 */ 4984 int 4985 get_id_len(char_u **arg) 4986 { 4987 char_u *p; 4988 int len; 4989 4990 // Find the end of the name. 4991 for (p = *arg; eval_isnamec(*p); ++p) 4992 { 4993 if (*p == ':') 4994 { 4995 // "s:" is start of "s:var", but "n:" is not and can be used in 4996 // slice "[n:]". Also "xx:" is not a namespace. 4997 len = (int)(p - *arg); 4998 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL) 4999 || len > 1) 5000 break; 5001 } 5002 } 5003 if (p == *arg) // no name found 5004 return 0; 5005 5006 len = (int)(p - *arg); 5007 *arg = skipwhite(p); 5008 5009 return len; 5010 } 5011 5012 /* 5013 * Get the length of the name of a variable or function. 5014 * Only the name is recognized, does not handle ".key" or "[idx]". 5015 * "arg" is advanced to the first non-white character after the name. 5016 * Return -1 if curly braces expansion failed. 5017 * Return 0 if something else is wrong. 5018 * If the name contains 'magic' {}'s, expand them and return the 5019 * expanded name in an allocated string via 'alias' - caller must free. 5020 */ 5021 int 5022 get_name_len( 5023 char_u **arg, 5024 char_u **alias, 5025 int evaluate, 5026 int verbose) 5027 { 5028 int len; 5029 char_u *p; 5030 char_u *expr_start; 5031 char_u *expr_end; 5032 5033 *alias = NULL; // default to no alias 5034 5035 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA 5036 && (*arg)[2] == (int)KE_SNR) 5037 { 5038 // hard coded <SNR>, already translated 5039 *arg += 3; 5040 return get_id_len(arg) + 3; 5041 } 5042 len = eval_fname_script(*arg); 5043 if (len > 0) 5044 { 5045 // literal "<SID>", "s:" or "<SNR>" 5046 *arg += len; 5047 } 5048 5049 /* 5050 * Find the end of the name; check for {} construction. 5051 */ 5052 p = find_name_end(*arg, &expr_start, &expr_end, 5053 len > 0 ? 0 : FNE_CHECK_START); 5054 if (expr_start != NULL) 5055 { 5056 char_u *temp_string; 5057 5058 if (!evaluate) 5059 { 5060 len += (int)(p - *arg); 5061 *arg = skipwhite(p); 5062 return len; 5063 } 5064 5065 /* 5066 * Include any <SID> etc in the expanded string: 5067 * Thus the -len here. 5068 */ 5069 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p); 5070 if (temp_string == NULL) 5071 return -1; 5072 *alias = temp_string; 5073 *arg = skipwhite(p); 5074 return (int)STRLEN(temp_string); 5075 } 5076 5077 len += get_id_len(arg); 5078 // Only give an error when there is something, otherwise it will be 5079 // reported at a higher level. 5080 if (len == 0 && verbose && **arg != NUL) 5081 semsg(_(e_invexpr2), *arg); 5082 5083 return len; 5084 } 5085 5086 /* 5087 * Find the end of a variable or function name, taking care of magic braces. 5088 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the 5089 * start and end of the first magic braces item. 5090 * "flags" can have FNE_INCL_BR and FNE_CHECK_START. 5091 * Return a pointer to just after the name. Equal to "arg" if there is no 5092 * valid name. 5093 */ 5094 char_u * 5095 find_name_end( 5096 char_u *arg, 5097 char_u **expr_start, 5098 char_u **expr_end, 5099 int flags) 5100 { 5101 int mb_nest = 0; 5102 int br_nest = 0; 5103 char_u *p; 5104 int len; 5105 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9; 5106 5107 if (expr_start != NULL) 5108 { 5109 *expr_start = NULL; 5110 *expr_end = NULL; 5111 } 5112 5113 // Quick check for valid starting character. 5114 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) 5115 && (*arg != '{' || vim9script)) 5116 return arg; 5117 5118 for (p = arg; *p != NUL 5119 && (eval_isnamec(*p) 5120 || (*p == '{' && !vim9script) 5121 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.')) 5122 || mb_nest != 0 5123 || br_nest != 0); MB_PTR_ADV(p)) 5124 { 5125 if (*p == '\'') 5126 { 5127 // skip over 'string' to avoid counting [ and ] inside it. 5128 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p)) 5129 ; 5130 if (*p == NUL) 5131 break; 5132 } 5133 else if (*p == '"') 5134 { 5135 // skip over "str\"ing" to avoid counting [ and ] inside it. 5136 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p)) 5137 if (*p == '\\' && p[1] != NUL) 5138 ++p; 5139 if (*p == NUL) 5140 break; 5141 } 5142 else if (br_nest == 0 && mb_nest == 0 && *p == ':') 5143 { 5144 // "s:" is start of "s:var", but "n:" is not and can be used in 5145 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. 5146 len = (int)(p - arg); 5147 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL) 5148 || (len > 1 && p[-1] != '}')) 5149 break; 5150 } 5151 5152 if (mb_nest == 0) 5153 { 5154 if (*p == '[') 5155 ++br_nest; 5156 else if (*p == ']') 5157 --br_nest; 5158 } 5159 5160 if (br_nest == 0 && !vim9script) 5161 { 5162 if (*p == '{') 5163 { 5164 mb_nest++; 5165 if (expr_start != NULL && *expr_start == NULL) 5166 *expr_start = p; 5167 } 5168 else if (*p == '}') 5169 { 5170 mb_nest--; 5171 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL) 5172 *expr_end = p; 5173 } 5174 } 5175 } 5176 5177 return p; 5178 } 5179 5180 /* 5181 * Expands out the 'magic' {}'s in a variable/function name. 5182 * Note that this can call itself recursively, to deal with 5183 * constructs like foo{bar}{baz}{bam} 5184 * The four pointer arguments point to "foo{expre}ss{ion}bar" 5185 * "in_start" ^ 5186 * "expr_start" ^ 5187 * "expr_end" ^ 5188 * "in_end" ^ 5189 * 5190 * Returns a new allocated string, which the caller must free. 5191 * Returns NULL for failure. 5192 */ 5193 static char_u * 5194 make_expanded_name( 5195 char_u *in_start, 5196 char_u *expr_start, 5197 char_u *expr_end, 5198 char_u *in_end) 5199 { 5200 char_u c1; 5201 char_u *retval = NULL; 5202 char_u *temp_result; 5203 char_u *nextcmd = NULL; 5204 5205 if (expr_end == NULL || in_end == NULL) 5206 return NULL; 5207 *expr_start = NUL; 5208 *expr_end = NUL; 5209 c1 = *in_end; 5210 *in_end = NUL; 5211 5212 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE); 5213 if (temp_result != NULL && nextcmd == NULL) 5214 { 5215 retval = alloc(STRLEN(temp_result) + (expr_start - in_start) 5216 + (in_end - expr_end) + 1); 5217 if (retval != NULL) 5218 { 5219 STRCPY(retval, in_start); 5220 STRCAT(retval, temp_result); 5221 STRCAT(retval, expr_end + 1); 5222 } 5223 } 5224 vim_free(temp_result); 5225 5226 *in_end = c1; // put char back for error messages 5227 *expr_start = '{'; 5228 *expr_end = '}'; 5229 5230 if (retval != NULL) 5231 { 5232 temp_result = find_name_end(retval, &expr_start, &expr_end, 0); 5233 if (expr_start != NULL) 5234 { 5235 // Further expansion! 5236 temp_result = make_expanded_name(retval, expr_start, 5237 expr_end, temp_result); 5238 vim_free(retval); 5239 retval = temp_result; 5240 } 5241 } 5242 5243 return retval; 5244 } 5245 5246 /* 5247 * Return TRUE if character "c" can be used in a variable or function name. 5248 * Does not include '{' or '}' for magic braces. 5249 */ 5250 int 5251 eval_isnamec(int c) 5252 { 5253 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR); 5254 } 5255 5256 /* 5257 * Return TRUE if character "c" can be used as the first character in a 5258 * variable or function name (excluding '{' and '}'). 5259 */ 5260 int 5261 eval_isnamec1(int c) 5262 { 5263 return (ASCII_ISALPHA(c) || c == '_'); 5264 } 5265 5266 /* 5267 * Handle: 5268 * - expr[expr], expr[expr:expr] subscript 5269 * - ".name" lookup 5270 * - function call with Funcref variable: func(expr) 5271 * - method call: var->method() 5272 * 5273 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len() 5274 */ 5275 int 5276 handle_subscript( 5277 char_u **arg, 5278 typval_T *rettv, 5279 int evaluate, // do more than finding the end 5280 int verbose, // give error messages 5281 char_u *start_leader, // start of '!' and '-' prefixes 5282 char_u **end_leaderp) // end of '!' and '-' prefixes 5283 { 5284 int ret = OK; 5285 dict_T *selfdict = NULL; 5286 5287 // "." is ".name" lookup when we found a dict or when evaluating and 5288 // scriptversion is at least 2, where string concatenation is "..". 5289 while (ret == OK 5290 && (((**arg == '[' 5291 || (**arg == '.' && (rettv->v_type == VAR_DICT 5292 || (!evaluate 5293 && (*arg)[1] != '.' 5294 && current_sctx.sc_version >= 2))) 5295 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC 5296 || rettv->v_type == VAR_PARTIAL))) 5297 && !VIM_ISWHITE(*(*arg - 1))) 5298 || (**arg == '-' && (*arg)[1] == '>'))) 5299 { 5300 if (**arg == '(') 5301 { 5302 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL); 5303 5304 // Stop the expression evaluation when immediately aborting on 5305 // error, or when an interrupt occurred or an exception was thrown 5306 // but not caught. 5307 if (aborting()) 5308 { 5309 if (ret == OK) 5310 clear_tv(rettv); 5311 ret = FAIL; 5312 } 5313 dict_unref(selfdict); 5314 selfdict = NULL; 5315 } 5316 else if (**arg == '-') 5317 { 5318 // Expression "-1.0->method()" applies the leader "-" before 5319 // applying ->. 5320 if (evaluate && *end_leaderp > start_leader) 5321 ret = eval7_leader(rettv, start_leader, end_leaderp); 5322 if (ret == OK) 5323 { 5324 if ((*arg)[2] == '{') 5325 // expr->{lambda}() 5326 ret = eval_lambda(arg, rettv, evaluate, verbose); 5327 else 5328 // expr->name() 5329 ret = eval_method(arg, rettv, evaluate, verbose); 5330 } 5331 } 5332 else // **arg == '[' || **arg == '.' 5333 { 5334 dict_unref(selfdict); 5335 if (rettv->v_type == VAR_DICT) 5336 { 5337 selfdict = rettv->vval.v_dict; 5338 if (selfdict != NULL) 5339 ++selfdict->dv_refcount; 5340 } 5341 else 5342 selfdict = NULL; 5343 if (eval_index(arg, rettv, evaluate, verbose) == FAIL) 5344 { 5345 clear_tv(rettv); 5346 ret = FAIL; 5347 } 5348 } 5349 } 5350 5351 // Turn "dict.Func" into a partial for "Func" bound to "dict". 5352 // Don't do this when "Func" is already a partial that was bound 5353 // explicitly (pt_auto is FALSE). 5354 if (selfdict != NULL 5355 && (rettv->v_type == VAR_FUNC 5356 || (rettv->v_type == VAR_PARTIAL 5357 && (rettv->vval.v_partial->pt_auto 5358 || rettv->vval.v_partial->pt_dict == NULL)))) 5359 selfdict = make_partial(selfdict, rettv); 5360 5361 dict_unref(selfdict); 5362 return ret; 5363 } 5364 5365 /* 5366 * Allocate memory for a variable type-value, and make it empty (0 or NULL 5367 * value). 5368 */ 5369 typval_T * 5370 alloc_tv(void) 5371 { 5372 return ALLOC_CLEAR_ONE(typval_T); 5373 } 5374 5375 /* 5376 * Allocate memory for a variable type-value, and assign a string to it. 5377 * The string "s" must have been allocated, it is consumed. 5378 * Return NULL for out of memory, the variable otherwise. 5379 */ 5380 typval_T * 5381 alloc_string_tv(char_u *s) 5382 { 5383 typval_T *rettv; 5384 5385 rettv = alloc_tv(); 5386 if (rettv != NULL) 5387 { 5388 rettv->v_type = VAR_STRING; 5389 rettv->vval.v_string = s; 5390 } 5391 else 5392 vim_free(s); 5393 return rettv; 5394 } 5395 5396 /* 5397 * Free the memory for a variable type-value. 5398 */ 5399 void 5400 free_tv(typval_T *varp) 5401 { 5402 if (varp != NULL) 5403 { 5404 switch (varp->v_type) 5405 { 5406 case VAR_FUNC: 5407 func_unref(varp->vval.v_string); 5408 // FALLTHROUGH 5409 case VAR_STRING: 5410 vim_free(varp->vval.v_string); 5411 break; 5412 case VAR_PARTIAL: 5413 partial_unref(varp->vval.v_partial); 5414 break; 5415 case VAR_BLOB: 5416 blob_unref(varp->vval.v_blob); 5417 break; 5418 case VAR_LIST: 5419 list_unref(varp->vval.v_list); 5420 break; 5421 case VAR_DICT: 5422 dict_unref(varp->vval.v_dict); 5423 break; 5424 case VAR_JOB: 5425 #ifdef FEAT_JOB_CHANNEL 5426 job_unref(varp->vval.v_job); 5427 break; 5428 #endif 5429 case VAR_CHANNEL: 5430 #ifdef FEAT_JOB_CHANNEL 5431 channel_unref(varp->vval.v_channel); 5432 break; 5433 #endif 5434 case VAR_NUMBER: 5435 case VAR_FLOAT: 5436 case VAR_ANY: 5437 case VAR_UNKNOWN: 5438 case VAR_VOID: 5439 case VAR_BOOL: 5440 case VAR_SPECIAL: 5441 break; 5442 } 5443 vim_free(varp); 5444 } 5445 } 5446 5447 /* 5448 * Free the memory for a variable value and set the value to NULL or 0. 5449 */ 5450 void 5451 clear_tv(typval_T *varp) 5452 { 5453 if (varp != NULL) 5454 { 5455 switch (varp->v_type) 5456 { 5457 case VAR_FUNC: 5458 func_unref(varp->vval.v_string); 5459 // FALLTHROUGH 5460 case VAR_STRING: 5461 VIM_CLEAR(varp->vval.v_string); 5462 break; 5463 case VAR_PARTIAL: 5464 partial_unref(varp->vval.v_partial); 5465 varp->vval.v_partial = NULL; 5466 break; 5467 case VAR_BLOB: 5468 blob_unref(varp->vval.v_blob); 5469 varp->vval.v_blob = NULL; 5470 break; 5471 case VAR_LIST: 5472 list_unref(varp->vval.v_list); 5473 varp->vval.v_list = NULL; 5474 break; 5475 case VAR_DICT: 5476 dict_unref(varp->vval.v_dict); 5477 varp->vval.v_dict = NULL; 5478 break; 5479 case VAR_NUMBER: 5480 case VAR_BOOL: 5481 case VAR_SPECIAL: 5482 varp->vval.v_number = 0; 5483 break; 5484 case VAR_FLOAT: 5485 #ifdef FEAT_FLOAT 5486 varp->vval.v_float = 0.0; 5487 break; 5488 #endif 5489 case VAR_JOB: 5490 #ifdef FEAT_JOB_CHANNEL 5491 job_unref(varp->vval.v_job); 5492 varp->vval.v_job = NULL; 5493 #endif 5494 break; 5495 case VAR_CHANNEL: 5496 #ifdef FEAT_JOB_CHANNEL 5497 channel_unref(varp->vval.v_channel); 5498 varp->vval.v_channel = NULL; 5499 #endif 5500 case VAR_UNKNOWN: 5501 case VAR_ANY: 5502 case VAR_VOID: 5503 break; 5504 } 5505 varp->v_lock = 0; 5506 } 5507 } 5508 5509 /* 5510 * Set the value of a variable to NULL without freeing items. 5511 */ 5512 void 5513 init_tv(typval_T *varp) 5514 { 5515 if (varp != NULL) 5516 CLEAR_POINTER(varp); 5517 } 5518 5519 /* 5520 * Get the number value of a variable. 5521 * If it is a String variable, uses vim_str2nr(). 5522 * For incompatible types, return 0. 5523 * tv_get_number_chk() is similar to tv_get_number(), but informs the 5524 * caller of incompatible types: it sets *denote to TRUE if "denote" 5525 * is not NULL or returns -1 otherwise. 5526 */ 5527 varnumber_T 5528 tv_get_number(typval_T *varp) 5529 { 5530 int error = FALSE; 5531 5532 return tv_get_number_chk(varp, &error); // return 0L on error 5533 } 5534 5535 varnumber_T 5536 tv_get_number_chk(typval_T *varp, int *denote) 5537 { 5538 varnumber_T n = 0L; 5539 5540 switch (varp->v_type) 5541 { 5542 case VAR_NUMBER: 5543 return varp->vval.v_number; 5544 case VAR_FLOAT: 5545 #ifdef FEAT_FLOAT 5546 emsg(_("E805: Using a Float as a Number")); 5547 break; 5548 #endif 5549 case VAR_FUNC: 5550 case VAR_PARTIAL: 5551 emsg(_("E703: Using a Funcref as a Number")); 5552 break; 5553 case VAR_STRING: 5554 if (varp->vval.v_string != NULL) 5555 vim_str2nr(varp->vval.v_string, NULL, NULL, 5556 STR2NR_ALL, &n, NULL, 0, FALSE); 5557 return n; 5558 case VAR_LIST: 5559 emsg(_("E745: Using a List as a Number")); 5560 break; 5561 case VAR_DICT: 5562 emsg(_("E728: Using a Dictionary as a Number")); 5563 break; 5564 case VAR_BOOL: 5565 case VAR_SPECIAL: 5566 return varp->vval.v_number == VVAL_TRUE ? 1 : 0; 5567 case VAR_JOB: 5568 #ifdef FEAT_JOB_CHANNEL 5569 emsg(_("E910: Using a Job as a Number")); 5570 break; 5571 #endif 5572 case VAR_CHANNEL: 5573 #ifdef FEAT_JOB_CHANNEL 5574 emsg(_("E913: Using a Channel as a Number")); 5575 break; 5576 #endif 5577 case VAR_BLOB: 5578 emsg(_("E974: Using a Blob as a Number")); 5579 break; 5580 case VAR_UNKNOWN: 5581 case VAR_ANY: 5582 case VAR_VOID: 5583 internal_error_no_abort("tv_get_number(UNKNOWN)"); 5584 break; 5585 } 5586 if (denote == NULL) // useful for values that must be unsigned 5587 n = -1; 5588 else 5589 *denote = TRUE; 5590 return n; 5591 } 5592 5593 #ifdef FEAT_FLOAT 5594 float_T 5595 tv_get_float(typval_T *varp) 5596 { 5597 switch (varp->v_type) 5598 { 5599 case VAR_NUMBER: 5600 return (float_T)(varp->vval.v_number); 5601 case VAR_FLOAT: 5602 return varp->vval.v_float; 5603 case VAR_FUNC: 5604 case VAR_PARTIAL: 5605 emsg(_("E891: Using a Funcref as a Float")); 5606 break; 5607 case VAR_STRING: 5608 emsg(_("E892: Using a String as a Float")); 5609 break; 5610 case VAR_LIST: 5611 emsg(_("E893: Using a List as a Float")); 5612 break; 5613 case VAR_DICT: 5614 emsg(_("E894: Using a Dictionary as a Float")); 5615 break; 5616 case VAR_BOOL: 5617 emsg(_("E362: Using a boolean value as a Float")); 5618 break; 5619 case VAR_SPECIAL: 5620 emsg(_("E907: Using a special value as a Float")); 5621 break; 5622 case VAR_JOB: 5623 # ifdef FEAT_JOB_CHANNEL 5624 emsg(_("E911: Using a Job as a Float")); 5625 break; 5626 # endif 5627 case VAR_CHANNEL: 5628 # ifdef FEAT_JOB_CHANNEL 5629 emsg(_("E914: Using a Channel as a Float")); 5630 break; 5631 # endif 5632 case VAR_BLOB: 5633 emsg(_("E975: Using a Blob as a Float")); 5634 break; 5635 case VAR_UNKNOWN: 5636 case VAR_ANY: 5637 case VAR_VOID: 5638 internal_error_no_abort("tv_get_float(UNKNOWN)"); 5639 break; 5640 } 5641 return 0; 5642 } 5643 #endif 5644 5645 /* 5646 * Get the string value of a variable. 5647 * If it is a Number variable, the number is converted into a string. 5648 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE! 5649 * tv_get_string_buf() uses a given buffer. 5650 * If the String variable has never been set, return an empty string. 5651 * Never returns NULL; 5652 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return 5653 * NULL on error. 5654 */ 5655 char_u * 5656 tv_get_string(typval_T *varp) 5657 { 5658 static char_u mybuf[NUMBUFLEN]; 5659 5660 return tv_get_string_buf(varp, mybuf); 5661 } 5662 5663 char_u * 5664 tv_get_string_buf(typval_T *varp, char_u *buf) 5665 { 5666 char_u *res = tv_get_string_buf_chk(varp, buf); 5667 5668 return res != NULL ? res : (char_u *)""; 5669 } 5670 5671 /* 5672 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE! 5673 */ 5674 char_u * 5675 tv_get_string_chk(typval_T *varp) 5676 { 5677 static char_u mybuf[NUMBUFLEN]; 5678 5679 return tv_get_string_buf_chk(varp, mybuf); 5680 } 5681 5682 char_u * 5683 tv_get_string_buf_chk(typval_T *varp, char_u *buf) 5684 { 5685 switch (varp->v_type) 5686 { 5687 case VAR_NUMBER: 5688 vim_snprintf((char *)buf, NUMBUFLEN, "%lld", 5689 (varnumber_T)varp->vval.v_number); 5690 return buf; 5691 case VAR_FUNC: 5692 case VAR_PARTIAL: 5693 emsg(_("E729: using Funcref as a String")); 5694 break; 5695 case VAR_LIST: 5696 emsg(_("E730: using List as a String")); 5697 break; 5698 case VAR_DICT: 5699 emsg(_("E731: using Dictionary as a String")); 5700 break; 5701 case VAR_FLOAT: 5702 #ifdef FEAT_FLOAT 5703 emsg(_(e_float_as_string)); 5704 break; 5705 #endif 5706 case VAR_STRING: 5707 if (varp->vval.v_string != NULL) 5708 return varp->vval.v_string; 5709 return (char_u *)""; 5710 case VAR_BOOL: 5711 case VAR_SPECIAL: 5712 STRCPY(buf, get_var_special_name(varp->vval.v_number)); 5713 return buf; 5714 case VAR_BLOB: 5715 emsg(_("E976: using Blob as a String")); 5716 break; 5717 case VAR_JOB: 5718 #ifdef FEAT_JOB_CHANNEL 5719 { 5720 job_T *job = varp->vval.v_job; 5721 char *status; 5722 5723 if (job == NULL) 5724 return (char_u *)"no process"; 5725 status = job->jv_status == JOB_FAILED ? "fail" 5726 : job->jv_status >= JOB_ENDED ? "dead" 5727 : "run"; 5728 # ifdef UNIX 5729 vim_snprintf((char *)buf, NUMBUFLEN, 5730 "process %ld %s", (long)job->jv_pid, status); 5731 # elif defined(MSWIN) 5732 vim_snprintf((char *)buf, NUMBUFLEN, 5733 "process %ld %s", 5734 (long)job->jv_proc_info.dwProcessId, 5735 status); 5736 # else 5737 // fall-back 5738 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status); 5739 # endif 5740 return buf; 5741 } 5742 #endif 5743 break; 5744 case VAR_CHANNEL: 5745 #ifdef FEAT_JOB_CHANNEL 5746 { 5747 channel_T *channel = varp->vval.v_channel; 5748 char *status = channel_status(channel, -1); 5749 5750 if (channel == NULL) 5751 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status); 5752 else 5753 vim_snprintf((char *)buf, NUMBUFLEN, 5754 "channel %d %s", channel->ch_id, status); 5755 return buf; 5756 } 5757 #endif 5758 break; 5759 case VAR_UNKNOWN: 5760 case VAR_ANY: 5761 case VAR_VOID: 5762 emsg(_(e_inval_string)); 5763 break; 5764 } 5765 return NULL; 5766 } 5767 5768 /* 5769 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses 5770 * string() on Dict, List, etc. 5771 */ 5772 static char_u * 5773 tv_stringify(typval_T *varp, char_u *buf) 5774 { 5775 if (varp->v_type == VAR_LIST 5776 || varp->v_type == VAR_DICT 5777 || varp->v_type == VAR_BLOB 5778 || varp->v_type == VAR_FUNC 5779 || varp->v_type == VAR_PARTIAL 5780 || varp->v_type == VAR_FLOAT) 5781 { 5782 typval_T tmp; 5783 5784 f_string(varp, &tmp); 5785 tv_get_string_buf(&tmp, buf); 5786 clear_tv(varp); 5787 *varp = tmp; 5788 return tmp.vval.v_string; 5789 } 5790 return tv_get_string_buf(varp, buf); 5791 } 5792 5793 /* 5794 * Return TRUE if typeval "tv" and its value are set to be locked (immutable). 5795 * Also give an error message, using "name" or _("name") when use_gettext is 5796 * TRUE. 5797 */ 5798 static int 5799 tv_check_lock(typval_T *tv, char_u *name, int use_gettext) 5800 { 5801 int lock = 0; 5802 5803 switch (tv->v_type) 5804 { 5805 case VAR_BLOB: 5806 if (tv->vval.v_blob != NULL) 5807 lock = tv->vval.v_blob->bv_lock; 5808 break; 5809 case VAR_LIST: 5810 if (tv->vval.v_list != NULL) 5811 lock = tv->vval.v_list->lv_lock; 5812 break; 5813 case VAR_DICT: 5814 if (tv->vval.v_dict != NULL) 5815 lock = tv->vval.v_dict->dv_lock; 5816 break; 5817 default: 5818 break; 5819 } 5820 return var_check_lock(tv->v_lock, name, use_gettext) 5821 || (lock != 0 && var_check_lock(lock, name, use_gettext)); 5822 } 5823 5824 /* 5825 * Copy the values from typval_T "from" to typval_T "to". 5826 * When needed allocates string or increases reference count. 5827 * Does not make a copy of a list, blob or dict but copies the reference! 5828 * It is OK for "from" and "to" to point to the same item. This is used to 5829 * make a copy later. 5830 */ 5831 void 5832 copy_tv(typval_T *from, typval_T *to) 5833 { 5834 to->v_type = from->v_type; 5835 to->v_lock = 0; 5836 switch (from->v_type) 5837 { 5838 case VAR_NUMBER: 5839 case VAR_BOOL: 5840 case VAR_SPECIAL: 5841 to->vval.v_number = from->vval.v_number; 5842 break; 5843 case VAR_FLOAT: 5844 #ifdef FEAT_FLOAT 5845 to->vval.v_float = from->vval.v_float; 5846 break; 5847 #endif 5848 case VAR_JOB: 5849 #ifdef FEAT_JOB_CHANNEL 5850 to->vval.v_job = from->vval.v_job; 5851 if (to->vval.v_job != NULL) 5852 ++to->vval.v_job->jv_refcount; 5853 break; 5854 #endif 5855 case VAR_CHANNEL: 5856 #ifdef FEAT_JOB_CHANNEL 5857 to->vval.v_channel = from->vval.v_channel; 5858 if (to->vval.v_channel != NULL) 5859 ++to->vval.v_channel->ch_refcount; 5860 break; 5861 #endif 5862 case VAR_STRING: 5863 case VAR_FUNC: 5864 if (from->vval.v_string == NULL) 5865 to->vval.v_string = NULL; 5866 else 5867 { 5868 to->vval.v_string = vim_strsave(from->vval.v_string); 5869 if (from->v_type == VAR_FUNC) 5870 func_ref(to->vval.v_string); 5871 } 5872 break; 5873 case VAR_PARTIAL: 5874 if (from->vval.v_partial == NULL) 5875 to->vval.v_partial = NULL; 5876 else 5877 { 5878 to->vval.v_partial = from->vval.v_partial; 5879 ++to->vval.v_partial->pt_refcount; 5880 } 5881 break; 5882 case VAR_BLOB: 5883 if (from->vval.v_blob == NULL) 5884 to->vval.v_blob = NULL; 5885 else 5886 { 5887 to->vval.v_blob = from->vval.v_blob; 5888 ++to->vval.v_blob->bv_refcount; 5889 } 5890 break; 5891 case VAR_LIST: 5892 if (from->vval.v_list == NULL) 5893 to->vval.v_list = NULL; 5894 else 5895 { 5896 to->vval.v_list = from->vval.v_list; 5897 ++to->vval.v_list->lv_refcount; 5898 } 5899 break; 5900 case VAR_DICT: 5901 if (from->vval.v_dict == NULL) 5902 to->vval.v_dict = NULL; 5903 else 5904 { 5905 to->vval.v_dict = from->vval.v_dict; 5906 ++to->vval.v_dict->dv_refcount; 5907 } 5908 break; 5909 case VAR_UNKNOWN: 5910 case VAR_ANY: 5911 case VAR_VOID: 5912 internal_error_no_abort("copy_tv(UNKNOWN)"); 5913 break; 5914 } 5915 } 5916 5917 /* 5918 * Make a copy of an item. 5919 * Lists and Dictionaries are also copied. A deep copy if "deep" is set. 5920 * For deepcopy() "copyID" is zero for a full copy or the ID for when a 5921 * reference to an already copied list/dict can be used. 5922 * Returns FAIL or OK. 5923 */ 5924 int 5925 item_copy( 5926 typval_T *from, 5927 typval_T *to, 5928 int deep, 5929 int copyID) 5930 { 5931 static int recurse = 0; 5932 int ret = OK; 5933 5934 if (recurse >= DICT_MAXNEST) 5935 { 5936 emsg(_("E698: variable nested too deep for making a copy")); 5937 return FAIL; 5938 } 5939 ++recurse; 5940 5941 switch (from->v_type) 5942 { 5943 case VAR_NUMBER: 5944 case VAR_FLOAT: 5945 case VAR_STRING: 5946 case VAR_FUNC: 5947 case VAR_PARTIAL: 5948 case VAR_BOOL: 5949 case VAR_SPECIAL: 5950 case VAR_JOB: 5951 case VAR_CHANNEL: 5952 copy_tv(from, to); 5953 break; 5954 case VAR_LIST: 5955 to->v_type = VAR_LIST; 5956 to->v_lock = 0; 5957 if (from->vval.v_list == NULL) 5958 to->vval.v_list = NULL; 5959 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID) 5960 { 5961 // use the copy made earlier 5962 to->vval.v_list = from->vval.v_list->lv_copylist; 5963 ++to->vval.v_list->lv_refcount; 5964 } 5965 else 5966 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID); 5967 if (to->vval.v_list == NULL) 5968 ret = FAIL; 5969 break; 5970 case VAR_BLOB: 5971 ret = blob_copy(from->vval.v_blob, to); 5972 break; 5973 case VAR_DICT: 5974 to->v_type = VAR_DICT; 5975 to->v_lock = 0; 5976 if (from->vval.v_dict == NULL) 5977 to->vval.v_dict = NULL; 5978 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID) 5979 { 5980 // use the copy made earlier 5981 to->vval.v_dict = from->vval.v_dict->dv_copydict; 5982 ++to->vval.v_dict->dv_refcount; 5983 } 5984 else 5985 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID); 5986 if (to->vval.v_dict == NULL) 5987 ret = FAIL; 5988 break; 5989 case VAR_UNKNOWN: 5990 case VAR_ANY: 5991 case VAR_VOID: 5992 internal_error_no_abort("item_copy(UNKNOWN)"); 5993 ret = FAIL; 5994 } 5995 --recurse; 5996 return ret; 5997 } 5998 5999 void 6000 echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr) 6001 { 6002 char_u *tofree; 6003 char_u numbuf[NUMBUFLEN]; 6004 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID()); 6005 6006 if (*atstart) 6007 { 6008 *atstart = FALSE; 6009 // Call msg_start() after eval1(), evaluating the expression 6010 // may cause a message to appear. 6011 if (with_space) 6012 { 6013 // Mark the saved text as finishing the line, so that what 6014 // follows is displayed on a new line when scrolling back 6015 // at the more prompt. 6016 msg_sb_eol(); 6017 msg_start(); 6018 } 6019 } 6020 else if (with_space) 6021 msg_puts_attr(" ", echo_attr); 6022 6023 if (p != NULL) 6024 for ( ; *p != NUL && !got_int; ++p) 6025 { 6026 if (*p == '\n' || *p == '\r' || *p == TAB) 6027 { 6028 if (*p != TAB && *needclr) 6029 { 6030 // remove any text still there from the command 6031 msg_clr_eos(); 6032 *needclr = FALSE; 6033 } 6034 msg_putchar_attr(*p, echo_attr); 6035 } 6036 else 6037 { 6038 if (has_mbyte) 6039 { 6040 int i = (*mb_ptr2len)(p); 6041 6042 (void)msg_outtrans_len_attr(p, i, echo_attr); 6043 p += i - 1; 6044 } 6045 else 6046 (void)msg_outtrans_len_attr(p, 1, echo_attr); 6047 } 6048 } 6049 vim_free(tofree); 6050 } 6051 6052 /* 6053 * ":echo expr1 ..." print each argument separated with a space, add a 6054 * newline at the end. 6055 * ":echon expr1 ..." print each argument plain. 6056 */ 6057 void 6058 ex_echo(exarg_T *eap) 6059 { 6060 char_u *arg = eap->arg; 6061 typval_T rettv; 6062 char_u *p; 6063 int needclr = TRUE; 6064 int atstart = TRUE; 6065 int did_emsg_before = did_emsg; 6066 int called_emsg_before = called_emsg; 6067 6068 if (eap->skip) 6069 ++emsg_skip; 6070 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int) 6071 { 6072 // If eval1() causes an error message the text from the command may 6073 // still need to be cleared. E.g., "echo 22,44". 6074 need_clr_eos = needclr; 6075 6076 p = arg; 6077 if (eval1(&arg, &rettv, !eap->skip) == FAIL) 6078 { 6079 /* 6080 * Report the invalid expression unless the expression evaluation 6081 * has been cancelled due to an aborting error, an interrupt, or an 6082 * exception. 6083 */ 6084 if (!aborting() && did_emsg == did_emsg_before 6085 && called_emsg == called_emsg_before) 6086 semsg(_(e_invexpr2), p); 6087 need_clr_eos = FALSE; 6088 break; 6089 } 6090 need_clr_eos = FALSE; 6091 6092 if (!eap->skip) 6093 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr); 6094 6095 clear_tv(&rettv); 6096 arg = skipwhite(arg); 6097 } 6098 eap->nextcmd = check_nextcmd(arg); 6099 6100 if (eap->skip) 6101 --emsg_skip; 6102 else 6103 { 6104 // remove text that may still be there from the command 6105 if (needclr) 6106 msg_clr_eos(); 6107 if (eap->cmdidx == CMD_echo) 6108 msg_end(); 6109 } 6110 } 6111 6112 /* 6113 * ":echohl {name}". 6114 */ 6115 void 6116 ex_echohl(exarg_T *eap) 6117 { 6118 echo_attr = syn_name2attr(eap->arg); 6119 } 6120 6121 /* 6122 * Returns the :echo attribute 6123 */ 6124 int 6125 get_echo_attr(void) 6126 { 6127 return echo_attr; 6128 } 6129 6130 /* 6131 * ":execute expr1 ..." execute the result of an expression. 6132 * ":echomsg expr1 ..." Print a message 6133 * ":echoerr expr1 ..." Print an error 6134 * Each gets spaces around each argument and a newline at the end for 6135 * echo commands 6136 */ 6137 void 6138 ex_execute(exarg_T *eap) 6139 { 6140 char_u *arg = eap->arg; 6141 typval_T rettv; 6142 int ret = OK; 6143 char_u *p; 6144 garray_T ga; 6145 int len; 6146 6147 ga_init2(&ga, 1, 80); 6148 6149 if (eap->skip) 6150 ++emsg_skip; 6151 while (!ends_excmd2(eap->cmd, arg) || *arg == '"') 6152 { 6153 ret = eval1_emsg(&arg, &rettv, !eap->skip); 6154 if (ret == FAIL) 6155 break; 6156 6157 if (!eap->skip) 6158 { 6159 char_u buf[NUMBUFLEN]; 6160 6161 if (eap->cmdidx == CMD_execute) 6162 { 6163 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB) 6164 { 6165 emsg(_(e_inval_string)); 6166 p = NULL; 6167 } 6168 else 6169 p = tv_get_string_buf(&rettv, buf); 6170 } 6171 else 6172 p = tv_stringify(&rettv, buf); 6173 if (p == NULL) 6174 { 6175 clear_tv(&rettv); 6176 ret = FAIL; 6177 break; 6178 } 6179 len = (int)STRLEN(p); 6180 if (ga_grow(&ga, len + 2) == FAIL) 6181 { 6182 clear_tv(&rettv); 6183 ret = FAIL; 6184 break; 6185 } 6186 if (ga.ga_len) 6187 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' '; 6188 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p); 6189 ga.ga_len += len; 6190 } 6191 6192 clear_tv(&rettv); 6193 arg = skipwhite(arg); 6194 } 6195 6196 if (ret != FAIL && ga.ga_data != NULL) 6197 { 6198 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr) 6199 { 6200 // Mark the already saved text as finishing the line, so that what 6201 // follows is displayed on a new line when scrolling back at the 6202 // more prompt. 6203 msg_sb_eol(); 6204 } 6205 6206 if (eap->cmdidx == CMD_echomsg) 6207 { 6208 msg_attr(ga.ga_data, echo_attr); 6209 out_flush(); 6210 } 6211 else if (eap->cmdidx == CMD_echoerr) 6212 { 6213 int save_did_emsg = did_emsg; 6214 6215 // We don't want to abort following commands, restore did_emsg. 6216 emsg(ga.ga_data); 6217 if (!force_abort) 6218 did_emsg = save_did_emsg; 6219 } 6220 else if (eap->cmdidx == CMD_execute) 6221 do_cmdline((char_u *)ga.ga_data, 6222 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE); 6223 } 6224 6225 ga_clear(&ga); 6226 6227 if (eap->skip) 6228 --emsg_skip; 6229 6230 eap->nextcmd = check_nextcmd(arg); 6231 } 6232 6233 /* 6234 * Skip over the name of an option: "&option", "&g:option" or "&l:option". 6235 * "arg" points to the "&" or '+' when called, to "option" when returning. 6236 * Returns NULL when no option name found. Otherwise pointer to the char 6237 * after the option name. 6238 */ 6239 char_u * 6240 find_option_end(char_u **arg, int *opt_flags) 6241 { 6242 char_u *p = *arg; 6243 6244 ++p; 6245 if (*p == 'g' && p[1] == ':') 6246 { 6247 *opt_flags = OPT_GLOBAL; 6248 p += 2; 6249 } 6250 else if (*p == 'l' && p[1] == ':') 6251 { 6252 *opt_flags = OPT_LOCAL; 6253 p += 2; 6254 } 6255 else 6256 *opt_flags = 0; 6257 6258 if (!ASCII_ISALPHA(*p)) 6259 return NULL; 6260 *arg = p; 6261 6262 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL) 6263 p += 4; // termcap option 6264 else 6265 while (ASCII_ISALPHA(*p)) 6266 ++p; 6267 return p; 6268 } 6269 6270 /* 6271 * Display script name where an item was last set. 6272 * Should only be invoked when 'verbose' is non-zero. 6273 */ 6274 void 6275 last_set_msg(sctx_T script_ctx) 6276 { 6277 char_u *p; 6278 6279 if (script_ctx.sc_sid != 0) 6280 { 6281 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid)); 6282 if (p != NULL) 6283 { 6284 verbose_enter(); 6285 msg_puts(_("\n\tLast set from ")); 6286 msg_puts((char *)p); 6287 if (script_ctx.sc_lnum > 0) 6288 { 6289 msg_puts(_(line_msg)); 6290 msg_outnum((long)script_ctx.sc_lnum); 6291 } 6292 verbose_leave(); 6293 vim_free(p); 6294 } 6295 } 6296 } 6297 6298 /* 6299 * Compare "typ1" and "typ2". Put the result in "typ1". 6300 */ 6301 int 6302 typval_compare( 6303 typval_T *typ1, // first operand 6304 typval_T *typ2, // second operand 6305 exptype_T type, // operator 6306 int ic) // ignore case 6307 { 6308 int i; 6309 varnumber_T n1, n2; 6310 char_u *s1, *s2; 6311 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN]; 6312 int type_is = type == EXPR_IS || type == EXPR_ISNOT; 6313 6314 if (type_is && typ1->v_type != typ2->v_type) 6315 { 6316 // For "is" a different type always means FALSE, for "notis" 6317 // it means TRUE. 6318 n1 = (type == EXPR_ISNOT); 6319 } 6320 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB) 6321 { 6322 if (type_is) 6323 { 6324 n1 = (typ1->v_type == typ2->v_type 6325 && typ1->vval.v_blob == typ2->vval.v_blob); 6326 if (type == EXPR_ISNOT) 6327 n1 = !n1; 6328 } 6329 else if (typ1->v_type != typ2->v_type 6330 || (type != EXPR_EQUAL && type != EXPR_NEQUAL)) 6331 { 6332 if (typ1->v_type != typ2->v_type) 6333 emsg(_("E977: Can only compare Blob with Blob")); 6334 else 6335 emsg(_(e_invalblob)); 6336 clear_tv(typ1); 6337 return FAIL; 6338 } 6339 else 6340 { 6341 // Compare two Blobs for being equal or unequal. 6342 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob); 6343 if (type == EXPR_NEQUAL) 6344 n1 = !n1; 6345 } 6346 } 6347 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST) 6348 { 6349 if (type_is) 6350 { 6351 n1 = (typ1->v_type == typ2->v_type 6352 && typ1->vval.v_list == typ2->vval.v_list); 6353 if (type == EXPR_ISNOT) 6354 n1 = !n1; 6355 } 6356 else if (typ1->v_type != typ2->v_type 6357 || (type != EXPR_EQUAL && type != EXPR_NEQUAL)) 6358 { 6359 if (typ1->v_type != typ2->v_type) 6360 emsg(_("E691: Can only compare List with List")); 6361 else 6362 emsg(_("E692: Invalid operation for List")); 6363 clear_tv(typ1); 6364 return FAIL; 6365 } 6366 else 6367 { 6368 // Compare two Lists for being equal or unequal. 6369 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list, 6370 ic, FALSE); 6371 if (type == EXPR_NEQUAL) 6372 n1 = !n1; 6373 } 6374 } 6375 6376 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT) 6377 { 6378 if (type_is) 6379 { 6380 n1 = (typ1->v_type == typ2->v_type 6381 && typ1->vval.v_dict == typ2->vval.v_dict); 6382 if (type == EXPR_ISNOT) 6383 n1 = !n1; 6384 } 6385 else if (typ1->v_type != typ2->v_type 6386 || (type != EXPR_EQUAL && type != EXPR_NEQUAL)) 6387 { 6388 if (typ1->v_type != typ2->v_type) 6389 emsg(_("E735: Can only compare Dictionary with Dictionary")); 6390 else 6391 emsg(_("E736: Invalid operation for Dictionary")); 6392 clear_tv(typ1); 6393 return FAIL; 6394 } 6395 else 6396 { 6397 // Compare two Dictionaries for being equal or unequal. 6398 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict, 6399 ic, FALSE); 6400 if (type == EXPR_NEQUAL) 6401 n1 = !n1; 6402 } 6403 } 6404 6405 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC 6406 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL) 6407 { 6408 if (type != EXPR_EQUAL && type != EXPR_NEQUAL 6409 && type != EXPR_IS && type != EXPR_ISNOT) 6410 { 6411 emsg(_("E694: Invalid operation for Funcrefs")); 6412 clear_tv(typ1); 6413 return FAIL; 6414 } 6415 if ((typ1->v_type == VAR_PARTIAL 6416 && typ1->vval.v_partial == NULL) 6417 || (typ2->v_type == VAR_PARTIAL 6418 && typ2->vval.v_partial == NULL)) 6419 // when a partial is NULL assume not equal 6420 n1 = FALSE; 6421 else if (type_is) 6422 { 6423 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC) 6424 // strings are considered the same if their value is 6425 // the same 6426 n1 = tv_equal(typ1, typ2, ic, FALSE); 6427 else if (typ1->v_type == VAR_PARTIAL 6428 && typ2->v_type == VAR_PARTIAL) 6429 n1 = (typ1->vval.v_partial == typ2->vval.v_partial); 6430 else 6431 n1 = FALSE; 6432 } 6433 else 6434 n1 = tv_equal(typ1, typ2, ic, FALSE); 6435 if (type == EXPR_NEQUAL || type == EXPR_ISNOT) 6436 n1 = !n1; 6437 } 6438 6439 #ifdef FEAT_FLOAT 6440 /* 6441 * If one of the two variables is a float, compare as a float. 6442 * When using "=~" or "!~", always compare as string. 6443 */ 6444 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT) 6445 && type != EXPR_MATCH && type != EXPR_NOMATCH) 6446 { 6447 float_T f1, f2; 6448 6449 f1 = tv_get_float(typ1); 6450 f2 = tv_get_float(typ2); 6451 n1 = FALSE; 6452 switch (type) 6453 { 6454 case EXPR_IS: 6455 case EXPR_EQUAL: n1 = (f1 == f2); break; 6456 case EXPR_ISNOT: 6457 case EXPR_NEQUAL: n1 = (f1 != f2); break; 6458 case EXPR_GREATER: n1 = (f1 > f2); break; 6459 case EXPR_GEQUAL: n1 = (f1 >= f2); break; 6460 case EXPR_SMALLER: n1 = (f1 < f2); break; 6461 case EXPR_SEQUAL: n1 = (f1 <= f2); break; 6462 case EXPR_UNKNOWN: 6463 case EXPR_MATCH: 6464 default: break; // avoid gcc warning 6465 } 6466 } 6467 #endif 6468 6469 /* 6470 * If one of the two variables is a number, compare as a number. 6471 * When using "=~" or "!~", always compare as string. 6472 */ 6473 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER) 6474 && type != EXPR_MATCH && type != EXPR_NOMATCH) 6475 { 6476 n1 = tv_get_number(typ1); 6477 n2 = tv_get_number(typ2); 6478 switch (type) 6479 { 6480 case EXPR_IS: 6481 case EXPR_EQUAL: n1 = (n1 == n2); break; 6482 case EXPR_ISNOT: 6483 case EXPR_NEQUAL: n1 = (n1 != n2); break; 6484 case EXPR_GREATER: n1 = (n1 > n2); break; 6485 case EXPR_GEQUAL: n1 = (n1 >= n2); break; 6486 case EXPR_SMALLER: n1 = (n1 < n2); break; 6487 case EXPR_SEQUAL: n1 = (n1 <= n2); break; 6488 case EXPR_UNKNOWN: 6489 case EXPR_MATCH: 6490 default: break; // avoid gcc warning 6491 } 6492 } 6493 else 6494 { 6495 s1 = tv_get_string_buf(typ1, buf1); 6496 s2 = tv_get_string_buf(typ2, buf2); 6497 if (type != EXPR_MATCH && type != EXPR_NOMATCH) 6498 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2); 6499 else 6500 i = 0; 6501 n1 = FALSE; 6502 switch (type) 6503 { 6504 case EXPR_IS: 6505 case EXPR_EQUAL: n1 = (i == 0); break; 6506 case EXPR_ISNOT: 6507 case EXPR_NEQUAL: n1 = (i != 0); break; 6508 case EXPR_GREATER: n1 = (i > 0); break; 6509 case EXPR_GEQUAL: n1 = (i >= 0); break; 6510 case EXPR_SMALLER: n1 = (i < 0); break; 6511 case EXPR_SEQUAL: n1 = (i <= 0); break; 6512 6513 case EXPR_MATCH: 6514 case EXPR_NOMATCH: 6515 n1 = pattern_match(s2, s1, ic); 6516 if (type == EXPR_NOMATCH) 6517 n1 = !n1; 6518 break; 6519 6520 default: break; // avoid gcc warning 6521 } 6522 } 6523 clear_tv(typ1); 6524 typ1->v_type = VAR_NUMBER; 6525 typ1->vval.v_number = n1; 6526 6527 return OK; 6528 } 6529 6530 char_u * 6531 typval_tostring(typval_T *arg) 6532 { 6533 char_u *tofree; 6534 char_u numbuf[NUMBUFLEN]; 6535 char_u *ret = NULL; 6536 6537 if (arg == NULL) 6538 return vim_strsave((char_u *)"(does not exist)"); 6539 ret = tv2string(arg, &tofree, numbuf, 0); 6540 // Make a copy if we have a value but it's not in allocated memory. 6541 if (ret != NULL && tofree == NULL) 6542 ret = vim_strsave(ret); 6543 return ret; 6544 } 6545 6546 #endif // FEAT_EVAL 6547 6548 /* 6549 * Perform a substitution on "str" with pattern "pat" and substitute "sub". 6550 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL. 6551 * "flags" can be "g" to do a global substitute. 6552 * Returns an allocated string, NULL for error. 6553 */ 6554 char_u * 6555 do_string_sub( 6556 char_u *str, 6557 char_u *pat, 6558 char_u *sub, 6559 typval_T *expr, 6560 char_u *flags) 6561 { 6562 int sublen; 6563 regmatch_T regmatch; 6564 int i; 6565 int do_all; 6566 char_u *tail; 6567 char_u *end; 6568 garray_T ga; 6569 char_u *ret; 6570 char_u *save_cpo; 6571 char_u *zero_width = NULL; 6572 6573 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here 6574 save_cpo = p_cpo; 6575 p_cpo = empty_option; 6576 6577 ga_init2(&ga, 1, 200); 6578 6579 do_all = (flags[0] == 'g'); 6580 6581 regmatch.rm_ic = p_ic; 6582 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING); 6583 if (regmatch.regprog != NULL) 6584 { 6585 tail = str; 6586 end = str + STRLEN(str); 6587 while (vim_regexec_nl(®match, str, (colnr_T)(tail - str))) 6588 { 6589 // Skip empty match except for first match. 6590 if (regmatch.startp[0] == regmatch.endp[0]) 6591 { 6592 if (zero_width == regmatch.startp[0]) 6593 { 6594 // avoid getting stuck on a match with an empty string 6595 i = mb_ptr2len(tail); 6596 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, 6597 (size_t)i); 6598 ga.ga_len += i; 6599 tail += i; 6600 continue; 6601 } 6602 zero_width = regmatch.startp[0]; 6603 } 6604 6605 /* 6606 * Get some space for a temporary buffer to do the substitution 6607 * into. It will contain: 6608 * - The text up to where the match is. 6609 * - The substituted text. 6610 * - The text after the match. 6611 */ 6612 sublen = vim_regsub(®match, sub, expr, tail, FALSE, TRUE, FALSE); 6613 if (ga_grow(&ga, (int)((end - tail) + sublen - 6614 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL) 6615 { 6616 ga_clear(&ga); 6617 break; 6618 } 6619 6620 // copy the text up to where the match is 6621 i = (int)(regmatch.startp[0] - tail); 6622 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i); 6623 // add the substituted text 6624 (void)vim_regsub(®match, sub, expr, (char_u *)ga.ga_data 6625 + ga.ga_len + i, TRUE, TRUE, FALSE); 6626 ga.ga_len += i + sublen - 1; 6627 tail = regmatch.endp[0]; 6628 if (*tail == NUL) 6629 break; 6630 if (!do_all) 6631 break; 6632 } 6633 6634 if (ga.ga_data != NULL) 6635 STRCPY((char *)ga.ga_data + ga.ga_len, tail); 6636 6637 vim_regfree(regmatch.regprog); 6638 } 6639 6640 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data); 6641 ga_clear(&ga); 6642 if (p_cpo == empty_option) 6643 p_cpo = save_cpo; 6644 else 6645 // Darn, evaluating {sub} expression or {expr} changed the value. 6646 free_string_option(save_cpo); 6647 6648 return ret; 6649 } 6650