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