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