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