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