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