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