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