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