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