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