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