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 assignment 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 break; 2858 2859 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE); 2860 oplen = (concat && p[1] == '.') ? 2 : 1; 2861 if (getnext) 2862 *arg = eval_next_line(evalarg); 2863 else 2864 { 2865 if (evaluate && vim9script && !VIM_ISWHITE(**arg)) 2866 { 2867 error_white_both(p, oplen); 2868 clear_tv(rettv); 2869 return FAIL; 2870 } 2871 *arg = p; 2872 } 2873 if ((op != '+' || (rettv->v_type != VAR_LIST 2874 && rettv->v_type != VAR_BLOB)) 2875 #ifdef FEAT_FLOAT 2876 && (op == '.' || rettv->v_type != VAR_FLOAT) 2877 #endif 2878 && evaluate) 2879 { 2880 int error = FALSE; 2881 2882 // For "list + ...", an illegal use of the first operand as 2883 // a number cannot be determined before evaluating the 2nd 2884 // operand: if this is also a list, all is ok. 2885 // For "something . ...", "something - ..." or "non-list + ...", 2886 // we know that the first operand needs to be a string or number 2887 // without evaluating the 2nd operand. So check before to avoid 2888 // side effects after an error. 2889 if (op != '.') 2890 tv_get_number_chk(rettv, &error); 2891 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error) 2892 { 2893 clear_tv(rettv); 2894 return FAIL; 2895 } 2896 } 2897 2898 /* 2899 * Get the second variable. 2900 */ 2901 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen])) 2902 { 2903 error_white_both(p, oplen); 2904 clear_tv(rettv); 2905 return FAIL; 2906 } 2907 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg); 2908 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL) 2909 { 2910 clear_tv(rettv); 2911 return FAIL; 2912 } 2913 2914 if (evaluate) 2915 { 2916 /* 2917 * Compute the result. 2918 */ 2919 if (op == '.') 2920 { 2921 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN]; 2922 char_u *s1 = tv_get_string_buf(rettv, buf1); 2923 char_u *s2 = NULL; 2924 2925 if (vim9script && (var2.v_type == VAR_VOID 2926 || var2.v_type == VAR_CHANNEL 2927 || var2.v_type == VAR_JOB)) 2928 emsg(_(e_inval_string)); 2929 #ifdef FEAT_FLOAT 2930 else if (vim9script && var2.v_type == VAR_FLOAT) 2931 { 2932 vim_snprintf((char *)buf2, NUMBUFLEN, "%g", 2933 var2.vval.v_float); 2934 s2 = buf2; 2935 } 2936 #endif 2937 else 2938 s2 = tv_get_string_buf_chk(&var2, buf2); 2939 if (s2 == NULL) // type error ? 2940 { 2941 clear_tv(rettv); 2942 clear_tv(&var2); 2943 return FAIL; 2944 } 2945 p = concat_str(s1, s2); 2946 clear_tv(rettv); 2947 rettv->v_type = VAR_STRING; 2948 rettv->vval.v_string = p; 2949 } 2950 else if (op == '+' && rettv->v_type == VAR_BLOB 2951 && var2.v_type == VAR_BLOB) 2952 eval_addblob(rettv, &var2); 2953 else if (op == '+' && rettv->v_type == VAR_LIST 2954 && var2.v_type == VAR_LIST) 2955 { 2956 if (eval_addlist(rettv, &var2) == FAIL) 2957 return FAIL; 2958 } 2959 else 2960 { 2961 int error = FALSE; 2962 varnumber_T n1, n2; 2963 #ifdef FEAT_FLOAT 2964 float_T f1 = 0, f2 = 0; 2965 2966 if (rettv->v_type == VAR_FLOAT) 2967 { 2968 f1 = rettv->vval.v_float; 2969 n1 = 0; 2970 } 2971 else 2972 #endif 2973 { 2974 n1 = tv_get_number_chk(rettv, &error); 2975 if (error) 2976 { 2977 // This can only happen for "list + non-list". For 2978 // "non-list + ..." or "something - ...", we returned 2979 // before evaluating the 2nd operand. 2980 clear_tv(rettv); 2981 return FAIL; 2982 } 2983 #ifdef FEAT_FLOAT 2984 if (var2.v_type == VAR_FLOAT) 2985 f1 = n1; 2986 #endif 2987 } 2988 #ifdef FEAT_FLOAT 2989 if (var2.v_type == VAR_FLOAT) 2990 { 2991 f2 = var2.vval.v_float; 2992 n2 = 0; 2993 } 2994 else 2995 #endif 2996 { 2997 n2 = tv_get_number_chk(&var2, &error); 2998 if (error) 2999 { 3000 clear_tv(rettv); 3001 clear_tv(&var2); 3002 return FAIL; 3003 } 3004 #ifdef FEAT_FLOAT 3005 if (rettv->v_type == VAR_FLOAT) 3006 f2 = n2; 3007 #endif 3008 } 3009 clear_tv(rettv); 3010 3011 #ifdef FEAT_FLOAT 3012 // If there is a float on either side the result is a float. 3013 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT) 3014 { 3015 if (op == '+') 3016 f1 = f1 + f2; 3017 else 3018 f1 = f1 - f2; 3019 rettv->v_type = VAR_FLOAT; 3020 rettv->vval.v_float = f1; 3021 } 3022 else 3023 #endif 3024 { 3025 if (op == '+') 3026 n1 = n1 + n2; 3027 else 3028 n1 = n1 - n2; 3029 rettv->v_type = VAR_NUMBER; 3030 rettv->vval.v_number = n1; 3031 } 3032 } 3033 clear_tv(&var2); 3034 } 3035 } 3036 return OK; 3037 } 3038 3039 /* 3040 * Handle fifth level expression: 3041 * * number multiplication 3042 * / number division 3043 * % number modulo 3044 * 3045 * "arg" must point to the first non-white of the expression. 3046 * "arg" is advanced to just after the recognized expression. 3047 * 3048 * Return OK or FAIL. 3049 */ 3050 static int 3051 eval6( 3052 char_u **arg, 3053 typval_T *rettv, 3054 evalarg_T *evalarg, 3055 int want_string) // after "." operator 3056 { 3057 #ifdef FEAT_FLOAT 3058 int use_float = FALSE; 3059 #endif 3060 3061 /* 3062 * Get the first variable. 3063 */ 3064 if (eval7(arg, rettv, evalarg, want_string) == FAIL) 3065 return FAIL; 3066 3067 /* 3068 * Repeat computing, until no '*', '/' or '%' is following. 3069 */ 3070 for (;;) 3071 { 3072 int evaluate; 3073 int getnext; 3074 typval_T var2; 3075 char_u *p; 3076 int op; 3077 varnumber_T n1, n2; 3078 #ifdef FEAT_FLOAT 3079 float_T f1, f2; 3080 #endif 3081 int error; 3082 3083 p = eval_next_non_blank(*arg, evalarg, &getnext); 3084 op = *p; 3085 if (op != '*' && op != '/' && op != '%') 3086 break; 3087 3088 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE); 3089 if (getnext) 3090 *arg = eval_next_line(evalarg); 3091 else 3092 { 3093 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg)) 3094 { 3095 error_white_both(p, 1); 3096 clear_tv(rettv); 3097 return FAIL; 3098 } 3099 *arg = p; 3100 } 3101 3102 #ifdef FEAT_FLOAT 3103 f1 = 0; 3104 f2 = 0; 3105 #endif 3106 error = FALSE; 3107 if (evaluate) 3108 { 3109 #ifdef FEAT_FLOAT 3110 if (rettv->v_type == VAR_FLOAT) 3111 { 3112 f1 = rettv->vval.v_float; 3113 use_float = TRUE; 3114 n1 = 0; 3115 } 3116 else 3117 #endif 3118 n1 = tv_get_number_chk(rettv, &error); 3119 clear_tv(rettv); 3120 if (error) 3121 return FAIL; 3122 } 3123 else 3124 n1 = 0; 3125 3126 /* 3127 * Get the second variable. 3128 */ 3129 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1])) 3130 { 3131 error_white_both(p, 1); 3132 clear_tv(rettv); 3133 return FAIL; 3134 } 3135 *arg = skipwhite_and_linebreak(*arg + 1, evalarg); 3136 if (eval7(arg, &var2, evalarg, FALSE) == FAIL) 3137 return FAIL; 3138 3139 if (evaluate) 3140 { 3141 #ifdef FEAT_FLOAT 3142 if (var2.v_type == VAR_FLOAT) 3143 { 3144 if (!use_float) 3145 { 3146 f1 = n1; 3147 use_float = TRUE; 3148 } 3149 f2 = var2.vval.v_float; 3150 n2 = 0; 3151 } 3152 else 3153 #endif 3154 { 3155 n2 = tv_get_number_chk(&var2, &error); 3156 clear_tv(&var2); 3157 if (error) 3158 return FAIL; 3159 #ifdef FEAT_FLOAT 3160 if (use_float) 3161 f2 = n2; 3162 #endif 3163 } 3164 3165 /* 3166 * Compute the result. 3167 * When either side is a float the result is a float. 3168 */ 3169 #ifdef FEAT_FLOAT 3170 if (use_float) 3171 { 3172 if (op == '*') 3173 f1 = f1 * f2; 3174 else if (op == '/') 3175 { 3176 # ifdef VMS 3177 // VMS crashes on divide by zero, work around it 3178 if (f2 == 0.0) 3179 { 3180 if (f1 == 0) 3181 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN 3182 else if (f1 < 0) 3183 f1 = -1 * __F_FLT_MAX; 3184 else 3185 f1 = __F_FLT_MAX; 3186 } 3187 else 3188 f1 = f1 / f2; 3189 # else 3190 // We rely on the floating point library to handle divide 3191 // by zero to result in "inf" and not a crash. 3192 f1 = f1 / f2; 3193 # endif 3194 } 3195 else 3196 { 3197 emsg(_(e_modulus)); 3198 return FAIL; 3199 } 3200 rettv->v_type = VAR_FLOAT; 3201 rettv->vval.v_float = f1; 3202 } 3203 else 3204 #endif 3205 { 3206 int failed = FALSE; 3207 3208 if (op == '*') 3209 n1 = n1 * n2; 3210 else if (op == '/') 3211 n1 = num_divide(n1, n2, &failed); 3212 else 3213 n1 = num_modulus(n1, n2, &failed); 3214 if (failed) 3215 return FAIL; 3216 3217 rettv->v_type = VAR_NUMBER; 3218 rettv->vval.v_number = n1; 3219 } 3220 } 3221 } 3222 3223 return OK; 3224 } 3225 3226 int 3227 eval_leader(char_u **arg, int vim9) 3228 { 3229 char_u *s = *arg; 3230 char_u *p = *arg; 3231 3232 while (*p == '!' || *p == '-' || *p == '+') 3233 { 3234 char_u *n = skipwhite(p + 1); 3235 3236 // ++, --, -+ and +- are not accepted in Vim9 script 3237 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+')) 3238 { 3239 semsg(_(e_invexpr2), s); 3240 return FAIL; 3241 } 3242 p = n; 3243 } 3244 *arg = p; 3245 return OK; 3246 } 3247 3248 /* 3249 * Handle sixth level expression: 3250 * number number constant 3251 * 0zFFFFFFFF Blob constant 3252 * "string" string constant 3253 * 'string' literal string constant 3254 * &option-name option value 3255 * @r register contents 3256 * identifier variable value 3257 * function() function call 3258 * $VAR environment variable 3259 * (expression) nested expression 3260 * [expr, expr] List 3261 * {arg, arg -> expr} Lambda 3262 * {key: val, key: val} Dictionary 3263 * #{key: val, key: val} Dictionary with literal keys 3264 * 3265 * Also handle: 3266 * ! in front logical NOT 3267 * - in front unary minus 3268 * + in front unary plus (ignored) 3269 * trailing [] subscript in String or List 3270 * trailing .name entry in Dictionary 3271 * trailing ->name() method call 3272 * 3273 * "arg" must point to the first non-white of the expression. 3274 * "arg" is advanced to just after the recognized expression. 3275 * 3276 * Return OK or FAIL. 3277 */ 3278 static int 3279 eval7( 3280 char_u **arg, 3281 typval_T *rettv, 3282 evalarg_T *evalarg, 3283 int want_string) // after "." operator 3284 { 3285 int evaluate = evalarg != NULL 3286 && (evalarg->eval_flags & EVAL_EVALUATE); 3287 int len; 3288 char_u *s; 3289 char_u *start_leader, *end_leader; 3290 int ret = OK; 3291 char_u *alias; 3292 3293 /* 3294 * Initialise variable so that clear_tv() can't mistake this for a 3295 * string and free a string that isn't there. 3296 */ 3297 rettv->v_type = VAR_UNKNOWN; 3298 3299 /* 3300 * Skip '!', '-' and '+' characters. They are handled later. 3301 */ 3302 start_leader = *arg; 3303 if (eval_leader(arg, in_vim9script()) == FAIL) 3304 return FAIL; 3305 end_leader = *arg; 3306 3307 if (**arg == '.' && (!isdigit(*(*arg + 1)) 3308 #ifdef FEAT_FLOAT 3309 || current_sctx.sc_version < 2 3310 #endif 3311 )) 3312 { 3313 semsg(_(e_invexpr2), *arg); 3314 ++*arg; 3315 return FAIL; 3316 } 3317 3318 switch (**arg) 3319 { 3320 /* 3321 * Number constant. 3322 */ 3323 case '0': 3324 case '1': 3325 case '2': 3326 case '3': 3327 case '4': 3328 case '5': 3329 case '6': 3330 case '7': 3331 case '8': 3332 case '9': 3333 case '.': ret = eval_number(arg, rettv, evaluate, want_string); 3334 3335 // Apply prefixed "-" and "+" now. Matters especially when 3336 // "->" follows. 3337 if (ret == OK && evaluate && end_leader > start_leader 3338 && rettv->v_type != VAR_BLOB) 3339 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader); 3340 break; 3341 3342 /* 3343 * String constant: "string". 3344 */ 3345 case '"': ret = eval_string(arg, rettv, evaluate); 3346 break; 3347 3348 /* 3349 * Literal string constant: 'str''ing'. 3350 */ 3351 case '\'': ret = eval_lit_string(arg, rettv, evaluate); 3352 break; 3353 3354 /* 3355 * List: [expr, expr] 3356 */ 3357 case '[': ret = eval_list(arg, rettv, evalarg, TRUE); 3358 break; 3359 3360 /* 3361 * Dictionary: #{key: val, key: val} 3362 */ 3363 case '#': if (!in_vim9script() && (*arg)[1] == '{') 3364 { 3365 ++*arg; 3366 ret = eval_dict(arg, rettv, evalarg, TRUE); 3367 } 3368 else 3369 ret = NOTDONE; 3370 break; 3371 3372 /* 3373 * Lambda: {arg, arg -> expr} 3374 * Dictionary: {'key': val, 'key': val} 3375 */ 3376 case '{': if (in_vim9script()) 3377 ret = NOTDONE; 3378 else 3379 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg); 3380 if (ret == NOTDONE) 3381 ret = eval_dict(arg, rettv, evalarg, FALSE); 3382 break; 3383 3384 /* 3385 * Option value: &name 3386 */ 3387 case '&': ret = eval_option(arg, rettv, evaluate); 3388 break; 3389 3390 /* 3391 * Environment variable: $VAR. 3392 */ 3393 case '$': ret = eval_env_var(arg, rettv, evaluate); 3394 break; 3395 3396 /* 3397 * Register contents: @r. 3398 */ 3399 case '@': ++*arg; 3400 if (evaluate) 3401 { 3402 rettv->v_type = VAR_STRING; 3403 rettv->vval.v_string = get_reg_contents(**arg, 3404 GREG_EXPR_SRC); 3405 } 3406 if (**arg != NUL) 3407 ++*arg; 3408 break; 3409 3410 /* 3411 * nested expression: (expression). 3412 * lambda: (arg) => expr 3413 */ 3414 case '(': ret = NOTDONE; 3415 if (in_vim9script()) 3416 { 3417 ret = get_lambda_tv(arg, rettv, TRUE, evalarg); 3418 if (ret == OK && evaluate) 3419 { 3420 ufunc_T *ufunc = rettv->vval.v_partial->pt_func; 3421 3422 // compile it here to get the return type 3423 if (compile_def_function(ufunc, 3424 TRUE, PROFILING(ufunc), NULL) == FAIL) 3425 { 3426 clear_tv(rettv); 3427 ret = FAIL; 3428 } 3429 } 3430 } 3431 if (ret == NOTDONE) 3432 { 3433 *arg = skipwhite_and_linebreak(*arg + 1, evalarg); 3434 ret = eval1(arg, rettv, evalarg); // recursive! 3435 3436 *arg = skipwhite_and_linebreak(*arg, evalarg); 3437 if (**arg == ')') 3438 ++*arg; 3439 else if (ret == OK) 3440 { 3441 emsg(_(e_missing_close)); 3442 clear_tv(rettv); 3443 ret = FAIL; 3444 } 3445 } 3446 break; 3447 3448 default: ret = NOTDONE; 3449 break; 3450 } 3451 3452 if (ret == NOTDONE) 3453 { 3454 /* 3455 * Must be a variable or function name. 3456 * Can also be a curly-braces kind of name: {expr}. 3457 */ 3458 s = *arg; 3459 len = get_name_len(arg, &alias, evaluate, TRUE); 3460 if (alias != NULL) 3461 s = alias; 3462 3463 if (len <= 0) 3464 ret = FAIL; 3465 else 3466 { 3467 int flags = evalarg == NULL ? 0 : evalarg->eval_flags; 3468 3469 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(') 3470 { 3471 // "name(..." recursive! 3472 *arg = skipwhite(*arg); 3473 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL); 3474 } 3475 else if (flags & EVAL_CONSTANT) 3476 ret = FAIL; 3477 else if (evaluate) 3478 { 3479 // get the value of "true", "false" or a variable 3480 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0) 3481 { 3482 rettv->v_type = VAR_BOOL; 3483 rettv->vval.v_number = VVAL_TRUE; 3484 ret = OK; 3485 } 3486 else if (len == 5 && in_vim9script() 3487 && STRNCMP(s, "false", 5) == 0) 3488 { 3489 rettv->v_type = VAR_BOOL; 3490 rettv->vval.v_number = VVAL_FALSE; 3491 ret = OK; 3492 } 3493 else if (len == 4 && in_vim9script() 3494 && STRNCMP(s, "null", 4) == 0) 3495 { 3496 rettv->v_type = VAR_SPECIAL; 3497 rettv->vval.v_number = VVAL_NULL; 3498 ret = OK; 3499 } 3500 else 3501 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE); 3502 } 3503 else 3504 { 3505 // skip the name 3506 check_vars(s, len); 3507 ret = OK; 3508 } 3509 } 3510 vim_free(alias); 3511 } 3512 3513 // Handle following '[', '(' and '.' for expr[expr], expr.name, 3514 // expr(expr), expr->name(expr) 3515 if (ret == OK) 3516 ret = handle_subscript(arg, rettv, evalarg, TRUE); 3517 3518 /* 3519 * Apply logical NOT and unary '-', from right to left, ignore '+'. 3520 */ 3521 if (ret == OK && evaluate && end_leader > start_leader) 3522 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader); 3523 return ret; 3524 } 3525 3526 /* 3527 * Apply the leading "!" and "-" before an eval7 expression to "rettv". 3528 * When "numeric_only" is TRUE only handle "+" and "-". 3529 * Adjusts "end_leaderp" until it is at "start_leader". 3530 */ 3531 static int 3532 eval7_leader( 3533 typval_T *rettv, 3534 int numeric_only, 3535 char_u *start_leader, 3536 char_u **end_leaderp) 3537 { 3538 char_u *end_leader = *end_leaderp; 3539 int ret = OK; 3540 int error = FALSE; 3541 varnumber_T val = 0; 3542 vartype_T type = rettv->v_type; 3543 #ifdef FEAT_FLOAT 3544 float_T f = 0.0; 3545 3546 if (rettv->v_type == VAR_FLOAT) 3547 f = rettv->vval.v_float; 3548 else 3549 #endif 3550 { 3551 while (VIM_ISWHITE(end_leader[-1])) 3552 --end_leader; 3553 if (in_vim9script() && end_leader[-1] == '!') 3554 val = tv2bool(rettv); 3555 else 3556 val = tv_get_number_chk(rettv, &error); 3557 } 3558 if (error) 3559 { 3560 clear_tv(rettv); 3561 ret = FAIL; 3562 } 3563 else 3564 { 3565 while (end_leader > start_leader) 3566 { 3567 --end_leader; 3568 if (*end_leader == '!') 3569 { 3570 if (numeric_only) 3571 { 3572 ++end_leader; 3573 break; 3574 } 3575 #ifdef FEAT_FLOAT 3576 if (rettv->v_type == VAR_FLOAT) 3577 { 3578 if (in_vim9script()) 3579 { 3580 rettv->v_type = VAR_BOOL; 3581 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE; 3582 } 3583 else 3584 f = !f; 3585 } 3586 else 3587 #endif 3588 { 3589 val = !val; 3590 type = VAR_BOOL; 3591 } 3592 } 3593 else if (*end_leader == '-') 3594 { 3595 #ifdef FEAT_FLOAT 3596 if (rettv->v_type == VAR_FLOAT) 3597 f = -f; 3598 else 3599 #endif 3600 { 3601 val = -val; 3602 type = VAR_NUMBER; 3603 } 3604 } 3605 } 3606 #ifdef FEAT_FLOAT 3607 if (rettv->v_type == VAR_FLOAT) 3608 { 3609 clear_tv(rettv); 3610 rettv->vval.v_float = f; 3611 } 3612 else 3613 #endif 3614 { 3615 clear_tv(rettv); 3616 if (in_vim9script()) 3617 rettv->v_type = type; 3618 else 3619 rettv->v_type = VAR_NUMBER; 3620 rettv->vval.v_number = val; 3621 } 3622 } 3623 *end_leaderp = end_leader; 3624 return ret; 3625 } 3626 3627 /* 3628 * Call the function referred to in "rettv". 3629 */ 3630 static int 3631 call_func_rettv( 3632 char_u **arg, 3633 evalarg_T *evalarg, 3634 typval_T *rettv, 3635 int evaluate, 3636 dict_T *selfdict, 3637 typval_T *basetv) 3638 { 3639 partial_T *pt = NULL; 3640 funcexe_T funcexe; 3641 typval_T functv; 3642 char_u *s; 3643 int ret; 3644 3645 // need to copy the funcref so that we can clear rettv 3646 if (evaluate) 3647 { 3648 functv = *rettv; 3649 rettv->v_type = VAR_UNKNOWN; 3650 3651 // Invoke the function. Recursive! 3652 if (functv.v_type == VAR_PARTIAL) 3653 { 3654 pt = functv.vval.v_partial; 3655 s = partial_name(pt); 3656 } 3657 else 3658 s = functv.vval.v_string; 3659 } 3660 else 3661 s = (char_u *)""; 3662 3663 CLEAR_FIELD(funcexe); 3664 funcexe.firstline = curwin->w_cursor.lnum; 3665 funcexe.lastline = curwin->w_cursor.lnum; 3666 funcexe.evaluate = evaluate; 3667 funcexe.partial = pt; 3668 funcexe.selfdict = selfdict; 3669 funcexe.basetv = basetv; 3670 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe); 3671 3672 // Clear the funcref afterwards, so that deleting it while 3673 // evaluating the arguments is possible (see test55). 3674 if (evaluate) 3675 clear_tv(&functv); 3676 3677 return ret; 3678 } 3679 3680 /* 3681 * Evaluate "->method()". 3682 * "*arg" points to "method". 3683 * Returns FAIL or OK. "*arg" is advanced to after the ')'. 3684 */ 3685 static int 3686 eval_lambda( 3687 char_u **arg, 3688 typval_T *rettv, 3689 evalarg_T *evalarg, 3690 int verbose) // give error messages 3691 { 3692 int evaluate = evalarg != NULL 3693 && (evalarg->eval_flags & EVAL_EVALUATE); 3694 typval_T base = *rettv; 3695 int ret; 3696 3697 rettv->v_type = VAR_UNKNOWN; 3698 3699 if (**arg == '{') 3700 { 3701 // ->{lambda}() 3702 ret = get_lambda_tv(arg, rettv, FALSE, evalarg); 3703 } 3704 else 3705 { 3706 // ->(lambda)() 3707 ++*arg; 3708 ret = eval1(arg, rettv, evalarg); 3709 *arg = skipwhite_and_linebreak(*arg, evalarg); 3710 if (**arg != ')') 3711 { 3712 emsg(_(e_missing_close)); 3713 ret = FAIL; 3714 } 3715 ++*arg; 3716 } 3717 if (ret != OK) 3718 return FAIL; 3719 else if (**arg != '(') 3720 { 3721 if (verbose) 3722 { 3723 if (*skipwhite(*arg) == '(') 3724 emsg(_(e_nowhitespace)); 3725 else 3726 semsg(_(e_missing_paren), "lambda"); 3727 } 3728 clear_tv(rettv); 3729 ret = FAIL; 3730 } 3731 else 3732 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base); 3733 3734 // Clear the funcref afterwards, so that deleting it while 3735 // evaluating the arguments is possible (see test55). 3736 if (evaluate) 3737 clear_tv(&base); 3738 3739 return ret; 3740 } 3741 3742 /* 3743 * Evaluate "->method()". 3744 * "*arg" points to "method". 3745 * Returns FAIL or OK. "*arg" is advanced to after the ')'. 3746 */ 3747 static int 3748 eval_method( 3749 char_u **arg, 3750 typval_T *rettv, 3751 evalarg_T *evalarg, 3752 int verbose) // give error messages 3753 { 3754 char_u *name; 3755 long len; 3756 char_u *alias; 3757 typval_T base = *rettv; 3758 int ret; 3759 int evaluate = evalarg != NULL 3760 && (evalarg->eval_flags & EVAL_EVALUATE); 3761 3762 rettv->v_type = VAR_UNKNOWN; 3763 3764 name = *arg; 3765 len = get_name_len(arg, &alias, evaluate, TRUE); 3766 if (alias != NULL) 3767 name = alias; 3768 3769 if (len <= 0) 3770 { 3771 if (verbose) 3772 emsg(_("E260: Missing name after ->")); 3773 ret = FAIL; 3774 } 3775 else 3776 { 3777 *arg = skipwhite(*arg); 3778 if (**arg != '(') 3779 { 3780 if (verbose) 3781 semsg(_(e_missing_paren), name); 3782 ret = FAIL; 3783 } 3784 else if (VIM_ISWHITE((*arg)[-1])) 3785 { 3786 if (verbose) 3787 emsg(_(e_nowhitespace)); 3788 ret = FAIL; 3789 } 3790 else 3791 ret = eval_func(arg, evalarg, name, len, rettv, 3792 evaluate ? EVAL_EVALUATE : 0, &base); 3793 } 3794 3795 // Clear the funcref afterwards, so that deleting it while 3796 // evaluating the arguments is possible (see test55). 3797 if (evaluate) 3798 clear_tv(&base); 3799 3800 return ret; 3801 } 3802 3803 /* 3804 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key". 3805 * "*arg" points to the '[' or '.'. 3806 * Returns FAIL or OK. "*arg" is advanced to after the ']'. 3807 */ 3808 static int 3809 eval_index( 3810 char_u **arg, 3811 typval_T *rettv, 3812 evalarg_T *evalarg, 3813 int verbose) // give error messages 3814 { 3815 int evaluate = evalarg != NULL 3816 && (evalarg->eval_flags & EVAL_EVALUATE); 3817 int empty1 = FALSE, empty2 = FALSE; 3818 typval_T var1, var2; 3819 int range = FALSE; 3820 char_u *key = NULL; 3821 int keylen = -1; 3822 int vim9 = in_vim9script(); 3823 3824 if (check_can_index(rettv, evaluate, verbose) == FAIL) 3825 return FAIL; 3826 3827 init_tv(&var1); 3828 init_tv(&var2); 3829 if (**arg == '.') 3830 { 3831 /* 3832 * dict.name 3833 */ 3834 key = *arg + 1; 3835 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen) 3836 ; 3837 if (keylen == 0) 3838 return FAIL; 3839 *arg = key + keylen; 3840 } 3841 else 3842 { 3843 /* 3844 * something[idx] 3845 * 3846 * Get the (first) variable from inside the []. 3847 */ 3848 *arg = skipwhite_and_linebreak(*arg + 1, evalarg); 3849 if (**arg == ':') 3850 empty1 = TRUE; 3851 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive! 3852 return FAIL; 3853 else if (vim9 && **arg == ':') 3854 { 3855 semsg(_(e_white_space_required_before_and_after_str_at_str), 3856 ":", *arg); 3857 clear_tv(&var1); 3858 return FAIL; 3859 } 3860 else if (evaluate) 3861 { 3862 #ifdef FEAT_FLOAT 3863 // allow for indexing with float 3864 if (vim9 && rettv->v_type == VAR_DICT 3865 && var1.v_type == VAR_FLOAT) 3866 { 3867 var1.vval.v_string = typval_tostring(&var1, TRUE); 3868 var1.v_type = VAR_STRING; 3869 } 3870 #endif 3871 if (tv_get_string_chk(&var1) == NULL) 3872 { 3873 // not a number or string 3874 clear_tv(&var1); 3875 return FAIL; 3876 } 3877 } 3878 3879 /* 3880 * Get the second variable from inside the [:]. 3881 */ 3882 *arg = skipwhite_and_linebreak(*arg, evalarg); 3883 if (**arg == ':') 3884 { 3885 range = TRUE; 3886 ++*arg; 3887 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']') 3888 { 3889 semsg(_(e_white_space_required_before_and_after_str_at_str), 3890 ":", *arg - 1); 3891 if (!empty1) 3892 clear_tv(&var1); 3893 return FAIL; 3894 } 3895 *arg = skipwhite_and_linebreak(*arg, evalarg); 3896 if (**arg == ']') 3897 empty2 = TRUE; 3898 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive! 3899 { 3900 if (!empty1) 3901 clear_tv(&var1); 3902 return FAIL; 3903 } 3904 else if (evaluate && tv_get_string_chk(&var2) == NULL) 3905 { 3906 // not a number or string 3907 if (!empty1) 3908 clear_tv(&var1); 3909 clear_tv(&var2); 3910 return FAIL; 3911 } 3912 } 3913 3914 // Check for the ']'. 3915 *arg = skipwhite_and_linebreak(*arg, evalarg); 3916 if (**arg != ']') 3917 { 3918 if (verbose) 3919 emsg(_(e_missbrac)); 3920 clear_tv(&var1); 3921 if (range) 3922 clear_tv(&var2); 3923 return FAIL; 3924 } 3925 *arg = *arg + 1; // skip over the ']' 3926 } 3927 3928 if (evaluate) 3929 { 3930 int res = eval_index_inner(rettv, range, 3931 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE, 3932 key, keylen, verbose); 3933 3934 if (!empty1) 3935 clear_tv(&var1); 3936 if (range) 3937 clear_tv(&var2); 3938 return res; 3939 } 3940 return OK; 3941 } 3942 3943 /* 3944 * Check if "rettv" can have an [index] or [sli:ce] 3945 */ 3946 int 3947 check_can_index(typval_T *rettv, int evaluate, int verbose) 3948 { 3949 switch (rettv->v_type) 3950 { 3951 case VAR_FUNC: 3952 case VAR_PARTIAL: 3953 if (verbose) 3954 emsg(_("E695: Cannot index a Funcref")); 3955 return FAIL; 3956 case VAR_FLOAT: 3957 #ifdef FEAT_FLOAT 3958 if (verbose) 3959 emsg(_(e_float_as_string)); 3960 return FAIL; 3961 #endif 3962 case VAR_BOOL: 3963 case VAR_SPECIAL: 3964 case VAR_JOB: 3965 case VAR_CHANNEL: 3966 if (verbose) 3967 emsg(_(e_cannot_index_special_variable)); 3968 return FAIL; 3969 case VAR_UNKNOWN: 3970 case VAR_ANY: 3971 case VAR_VOID: 3972 if (evaluate) 3973 { 3974 emsg(_(e_cannot_index_special_variable)); 3975 return FAIL; 3976 } 3977 // FALLTHROUGH 3978 3979 case VAR_STRING: 3980 case VAR_LIST: 3981 case VAR_DICT: 3982 case VAR_BLOB: 3983 break; 3984 case VAR_NUMBER: 3985 if (in_vim9script()) 3986 emsg(_(e_cannot_index_number)); 3987 break; 3988 } 3989 return OK; 3990 } 3991 3992 /* 3993 * slice() function 3994 */ 3995 void 3996 f_slice(typval_T *argvars, typval_T *rettv) 3997 { 3998 if (check_can_index(argvars, TRUE, FALSE) == OK) 3999 { 4000 copy_tv(argvars, rettv); 4001 eval_index_inner(rettv, TRUE, argvars + 1, 4002 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2, 4003 TRUE, NULL, 0, FALSE); 4004 } 4005 } 4006 4007 /* 4008 * Apply index or range to "rettv". 4009 * "var1" is the first index, NULL for [:expr]. 4010 * "var2" is the second index, NULL for [expr] and [expr: ] 4011 * "exclusive" is TRUE for slice(): second index is exclusive, use character 4012 * index for string. 4013 * Alternatively, "key" is not NULL, then key[keylen] is the dict index. 4014 */ 4015 int 4016 eval_index_inner( 4017 typval_T *rettv, 4018 int is_range, 4019 typval_T *var1, 4020 typval_T *var2, 4021 int exclusive, 4022 char_u *key, 4023 int keylen, 4024 int verbose) 4025 { 4026 varnumber_T n1, n2 = 0; 4027 long len; 4028 4029 n1 = 0; 4030 if (var1 != NULL && rettv->v_type != VAR_DICT) 4031 n1 = tv_get_number(var1); 4032 4033 if (is_range) 4034 { 4035 if (rettv->v_type == VAR_DICT) 4036 { 4037 if (verbose) 4038 emsg(_(e_cannot_slice_dictionary)); 4039 return FAIL; 4040 } 4041 if (var2 != NULL) 4042 n2 = tv_get_number(var2); 4043 else 4044 n2 = VARNUM_MAX; 4045 } 4046 4047 switch (rettv->v_type) 4048 { 4049 case VAR_UNKNOWN: 4050 case VAR_ANY: 4051 case VAR_VOID: 4052 case VAR_FUNC: 4053 case VAR_PARTIAL: 4054 case VAR_FLOAT: 4055 case VAR_BOOL: 4056 case VAR_SPECIAL: 4057 case VAR_JOB: 4058 case VAR_CHANNEL: 4059 break; // not evaluating, skipping over subscript 4060 4061 case VAR_NUMBER: 4062 case VAR_STRING: 4063 { 4064 char_u *s = tv_get_string(rettv); 4065 4066 len = (long)STRLEN(s); 4067 if (in_vim9script() || exclusive) 4068 { 4069 if (is_range) 4070 s = string_slice(s, n1, n2, exclusive); 4071 else 4072 s = char_from_string(s, n1); 4073 } 4074 else if (is_range) 4075 { 4076 // The resulting variable is a substring. If the indexes 4077 // are out of range the result is empty. 4078 if (n1 < 0) 4079 { 4080 n1 = len + n1; 4081 if (n1 < 0) 4082 n1 = 0; 4083 } 4084 if (n2 < 0) 4085 n2 = len + n2; 4086 else if (n2 >= len) 4087 n2 = len; 4088 if (n1 >= len || n2 < 0 || n1 > n2) 4089 s = NULL; 4090 else 4091 s = vim_strnsave(s + n1, n2 - n1 + 1); 4092 } 4093 else 4094 { 4095 // The resulting variable is a string of a single 4096 // character. If the index is too big or negative the 4097 // result is empty. 4098 if (n1 >= len || n1 < 0) 4099 s = NULL; 4100 else 4101 s = vim_strnsave(s + n1, 1); 4102 } 4103 clear_tv(rettv); 4104 rettv->v_type = VAR_STRING; 4105 rettv->vval.v_string = s; 4106 } 4107 break; 4108 4109 case VAR_BLOB: 4110 len = blob_len(rettv->vval.v_blob); 4111 if (is_range) 4112 { 4113 // The resulting variable is a sub-blob. If the indexes 4114 // are out of range the result is empty. 4115 if (n1 < 0) 4116 { 4117 n1 = len + n1; 4118 if (n1 < 0) 4119 n1 = 0; 4120 } 4121 if (n2 < 0) 4122 n2 = len + n2; 4123 else if (n2 >= len) 4124 n2 = len - (exclusive ? 0 : 1); 4125 if (exclusive) 4126 --n2; 4127 if (n1 >= len || n2 < 0 || n1 > n2) 4128 { 4129 clear_tv(rettv); 4130 rettv->v_type = VAR_BLOB; 4131 rettv->vval.v_blob = NULL; 4132 } 4133 else 4134 { 4135 blob_T *blob = blob_alloc(); 4136 long i; 4137 4138 if (blob != NULL) 4139 { 4140 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL) 4141 { 4142 blob_free(blob); 4143 return FAIL; 4144 } 4145 blob->bv_ga.ga_len = n2 - n1 + 1; 4146 for (i = n1; i <= n2; i++) 4147 blob_set(blob, i - n1, 4148 blob_get(rettv->vval.v_blob, i)); 4149 4150 clear_tv(rettv); 4151 rettv_blob_set(rettv, blob); 4152 } 4153 } 4154 } 4155 else 4156 { 4157 // The resulting variable is a byte value. 4158 // If the index is too big or negative that is an error. 4159 if (n1 < 0) 4160 n1 = len + n1; 4161 if (n1 < len && n1 >= 0) 4162 { 4163 int v = blob_get(rettv->vval.v_blob, n1); 4164 4165 clear_tv(rettv); 4166 rettv->v_type = VAR_NUMBER; 4167 rettv->vval.v_number = v; 4168 } 4169 else 4170 semsg(_(e_blobidx), n1); 4171 } 4172 break; 4173 4174 case VAR_LIST: 4175 if (var1 == NULL) 4176 n1 = 0; 4177 if (var2 == NULL) 4178 n2 = VARNUM_MAX; 4179 if (list_slice_or_index(rettv->vval.v_list, 4180 is_range, n1, n2, exclusive, rettv, verbose) == FAIL) 4181 return FAIL; 4182 break; 4183 4184 case VAR_DICT: 4185 { 4186 dictitem_T *item; 4187 typval_T tmp; 4188 4189 if (key == NULL) 4190 { 4191 key = tv_get_string_chk(var1); 4192 if (key == NULL) 4193 return FAIL; 4194 } 4195 4196 item = dict_find(rettv->vval.v_dict, key, (int)keylen); 4197 4198 if (item == NULL && verbose) 4199 semsg(_(e_dictkey), key); 4200 if (item == NULL) 4201 return FAIL; 4202 4203 copy_tv(&item->di_tv, &tmp); 4204 clear_tv(rettv); 4205 *rettv = tmp; 4206 } 4207 break; 4208 } 4209 return OK; 4210 } 4211 4212 /* 4213 * Return the function name of partial "pt". 4214 */ 4215 char_u * 4216 partial_name(partial_T *pt) 4217 { 4218 if (pt->pt_name != NULL) 4219 return pt->pt_name; 4220 if (pt->pt_func != NULL) 4221 return pt->pt_func->uf_name; 4222 return (char_u *)""; 4223 } 4224 4225 static void 4226 partial_free(partial_T *pt) 4227 { 4228 int i; 4229 4230 for (i = 0; i < pt->pt_argc; ++i) 4231 clear_tv(&pt->pt_argv[i]); 4232 vim_free(pt->pt_argv); 4233 dict_unref(pt->pt_dict); 4234 if (pt->pt_name != NULL) 4235 { 4236 func_unref(pt->pt_name); 4237 vim_free(pt->pt_name); 4238 } 4239 else 4240 func_ptr_unref(pt->pt_func); 4241 4242 // Decrease the reference count for the context of a closure. If down 4243 // to the minimum it may be time to free it. 4244 if (pt->pt_funcstack != NULL) 4245 { 4246 --pt->pt_funcstack->fs_refcount; 4247 funcstack_check_refcount(pt->pt_funcstack); 4248 } 4249 4250 vim_free(pt); 4251 } 4252 4253 /* 4254 * Unreference a closure: decrement the reference count and free it when it 4255 * becomes zero. 4256 */ 4257 void 4258 partial_unref(partial_T *pt) 4259 { 4260 if (pt != NULL) 4261 { 4262 if (--pt->pt_refcount <= 0) 4263 partial_free(pt); 4264 4265 // If the reference count goes down to one, the funcstack may be the 4266 // only reference and can be freed if no other partials reference it. 4267 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL) 4268 funcstack_check_refcount(pt->pt_funcstack); 4269 } 4270 } 4271 4272 /* 4273 * Return the next (unique) copy ID. 4274 * Used for serializing nested structures. 4275 */ 4276 int 4277 get_copyID(void) 4278 { 4279 current_copyID += COPYID_INC; 4280 return current_copyID; 4281 } 4282 4283 /* 4284 * Garbage collection for lists and dictionaries. 4285 * 4286 * We use reference counts to be able to free most items right away when they 4287 * are no longer used. But for composite items it's possible that it becomes 4288 * unused while the reference count is > 0: When there is a recursive 4289 * reference. Example: 4290 * :let l = [1, 2, 3] 4291 * :let d = {9: l} 4292 * :let l[1] = d 4293 * 4294 * Since this is quite unusual we handle this with garbage collection: every 4295 * once in a while find out which lists and dicts are not referenced from any 4296 * variable. 4297 * 4298 * Here is a good reference text about garbage collection (refers to Python 4299 * but it applies to all reference-counting mechanisms): 4300 * http://python.ca/nas/python/gc/ 4301 */ 4302 4303 /* 4304 * Do garbage collection for lists and dicts. 4305 * When "testing" is TRUE this is called from test_garbagecollect_now(). 4306 * Return TRUE if some memory was freed. 4307 */ 4308 int 4309 garbage_collect(int testing) 4310 { 4311 int copyID; 4312 int abort = FALSE; 4313 buf_T *buf; 4314 win_T *wp; 4315 int did_free = FALSE; 4316 tabpage_T *tp; 4317 4318 if (!testing) 4319 { 4320 // Only do this once. 4321 want_garbage_collect = FALSE; 4322 may_garbage_collect = FALSE; 4323 garbage_collect_at_exit = FALSE; 4324 } 4325 4326 // The execution stack can grow big, limit the size. 4327 if (exestack.ga_maxlen - exestack.ga_len > 500) 4328 { 4329 size_t new_len; 4330 char_u *pp; 4331 int n; 4332 4333 // Keep 150% of the current size, with a minimum of the growth size. 4334 n = exestack.ga_len / 2; 4335 if (n < exestack.ga_growsize) 4336 n = exestack.ga_growsize; 4337 4338 // Don't make it bigger though. 4339 if (exestack.ga_len + n < exestack.ga_maxlen) 4340 { 4341 new_len = exestack.ga_itemsize * (exestack.ga_len + n); 4342 pp = vim_realloc(exestack.ga_data, new_len); 4343 if (pp == NULL) 4344 return FAIL; 4345 exestack.ga_maxlen = exestack.ga_len + n; 4346 exestack.ga_data = pp; 4347 } 4348 } 4349 4350 // We advance by two because we add one for items referenced through 4351 // previous_funccal. 4352 copyID = get_copyID(); 4353 4354 /* 4355 * 1. Go through all accessible variables and mark all lists and dicts 4356 * with copyID. 4357 */ 4358 4359 // Don't free variables in the previous_funccal list unless they are only 4360 // referenced through previous_funccal. This must be first, because if 4361 // the item is referenced elsewhere the funccal must not be freed. 4362 abort = abort || set_ref_in_previous_funccal(copyID); 4363 4364 // script-local variables 4365 abort = abort || garbage_collect_scriptvars(copyID); 4366 4367 // buffer-local variables 4368 FOR_ALL_BUFFERS(buf) 4369 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID, 4370 NULL, NULL); 4371 4372 // window-local variables 4373 FOR_ALL_TAB_WINDOWS(tp, wp) 4374 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID, 4375 NULL, NULL); 4376 if (aucmd_win != NULL) 4377 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID, 4378 NULL, NULL); 4379 #ifdef FEAT_PROP_POPUP 4380 FOR_ALL_POPUPWINS(wp) 4381 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID, 4382 NULL, NULL); 4383 FOR_ALL_TABPAGES(tp) 4384 FOR_ALL_POPUPWINS_IN_TAB(tp, wp) 4385 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID, 4386 NULL, NULL); 4387 #endif 4388 4389 // tabpage-local variables 4390 FOR_ALL_TABPAGES(tp) 4391 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID, 4392 NULL, NULL); 4393 // global variables 4394 abort = abort || garbage_collect_globvars(copyID); 4395 4396 // function-local variables 4397 abort = abort || set_ref_in_call_stack(copyID); 4398 4399 // named functions (matters for closures) 4400 abort = abort || set_ref_in_functions(copyID); 4401 4402 // function call arguments, if v:testing is set. 4403 abort = abort || set_ref_in_func_args(copyID); 4404 4405 // v: vars 4406 abort = abort || garbage_collect_vimvars(copyID); 4407 4408 // callbacks in buffers 4409 abort = abort || set_ref_in_buffers(copyID); 4410 4411 #ifdef FEAT_LUA 4412 abort = abort || set_ref_in_lua(copyID); 4413 #endif 4414 4415 #ifdef FEAT_PYTHON 4416 abort = abort || set_ref_in_python(copyID); 4417 #endif 4418 4419 #ifdef FEAT_PYTHON3 4420 abort = abort || set_ref_in_python3(copyID); 4421 #endif 4422 4423 #ifdef FEAT_JOB_CHANNEL 4424 abort = abort || set_ref_in_channel(copyID); 4425 abort = abort || set_ref_in_job(copyID); 4426 #endif 4427 #ifdef FEAT_NETBEANS_INTG 4428 abort = abort || set_ref_in_nb_channel(copyID); 4429 #endif 4430 4431 #ifdef FEAT_TIMERS 4432 abort = abort || set_ref_in_timer(copyID); 4433 #endif 4434 4435 #ifdef FEAT_QUICKFIX 4436 abort = abort || set_ref_in_quickfix(copyID); 4437 #endif 4438 4439 #ifdef FEAT_TERMINAL 4440 abort = abort || set_ref_in_term(copyID); 4441 #endif 4442 4443 #ifdef FEAT_PROP_POPUP 4444 abort = abort || set_ref_in_popups(copyID); 4445 #endif 4446 4447 if (!abort) 4448 { 4449 /* 4450 * 2. Free lists and dictionaries that are not referenced. 4451 */ 4452 did_free = free_unref_items(copyID); 4453 4454 /* 4455 * 3. Check if any funccal can be freed now. 4456 * This may call us back recursively. 4457 */ 4458 free_unref_funccal(copyID, testing); 4459 } 4460 else if (p_verbose > 0) 4461 { 4462 verb_msg(_("Not enough memory to set references, garbage collection aborted!")); 4463 } 4464 4465 return did_free; 4466 } 4467 4468 /* 4469 * Free lists, dictionaries, channels and jobs that are no longer referenced. 4470 */ 4471 static int 4472 free_unref_items(int copyID) 4473 { 4474 int did_free = FALSE; 4475 4476 // Let all "free" functions know that we are here. This means no 4477 // dictionaries, lists, channels or jobs are to be freed, because we will 4478 // do that here. 4479 in_free_unref_items = TRUE; 4480 4481 /* 4482 * PASS 1: free the contents of the items. We don't free the items 4483 * themselves yet, so that it is possible to decrement refcount counters 4484 */ 4485 4486 // Go through the list of dicts and free items without the copyID. 4487 did_free |= dict_free_nonref(copyID); 4488 4489 // Go through the list of lists and free items without the copyID. 4490 did_free |= list_free_nonref(copyID); 4491 4492 #ifdef FEAT_JOB_CHANNEL 4493 // Go through the list of jobs and free items without the copyID. This 4494 // must happen before doing channels, because jobs refer to channels, but 4495 // the reference from the channel to the job isn't tracked. 4496 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK); 4497 4498 // Go through the list of channels and free items without the copyID. 4499 did_free |= free_unused_channels_contents(copyID, COPYID_MASK); 4500 #endif 4501 4502 /* 4503 * PASS 2: free the items themselves. 4504 */ 4505 dict_free_items(copyID); 4506 list_free_items(copyID); 4507 4508 #ifdef FEAT_JOB_CHANNEL 4509 // Go through the list of jobs and free items without the copyID. This 4510 // must happen before doing channels, because jobs refer to channels, but 4511 // the reference from the channel to the job isn't tracked. 4512 free_unused_jobs(copyID, COPYID_MASK); 4513 4514 // Go through the list of channels and free items without the copyID. 4515 free_unused_channels(copyID, COPYID_MASK); 4516 #endif 4517 4518 in_free_unref_items = FALSE; 4519 4520 return did_free; 4521 } 4522 4523 /* 4524 * Mark all lists and dicts referenced through hashtab "ht" with "copyID". 4525 * "list_stack" is used to add lists to be marked. Can be NULL. 4526 * 4527 * Returns TRUE if setting references failed somehow. 4528 */ 4529 int 4530 set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack) 4531 { 4532 int todo; 4533 int abort = FALSE; 4534 hashitem_T *hi; 4535 hashtab_T *cur_ht; 4536 ht_stack_T *ht_stack = NULL; 4537 ht_stack_T *tempitem; 4538 4539 cur_ht = ht; 4540 for (;;) 4541 { 4542 if (!abort) 4543 { 4544 // Mark each item in the hashtab. If the item contains a hashtab 4545 // it is added to ht_stack, if it contains a list it is added to 4546 // list_stack. 4547 todo = (int)cur_ht->ht_used; 4548 for (hi = cur_ht->ht_array; todo > 0; ++hi) 4549 if (!HASHITEM_EMPTY(hi)) 4550 { 4551 --todo; 4552 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID, 4553 &ht_stack, list_stack); 4554 } 4555 } 4556 4557 if (ht_stack == NULL) 4558 break; 4559 4560 // take an item from the stack 4561 cur_ht = ht_stack->ht; 4562 tempitem = ht_stack; 4563 ht_stack = ht_stack->prev; 4564 free(tempitem); 4565 } 4566 4567 return abort; 4568 } 4569 4570 /* 4571 * Mark a dict and its items with "copyID". 4572 * Returns TRUE if setting references failed somehow. 4573 */ 4574 int 4575 set_ref_in_dict(dict_T *d, int copyID) 4576 { 4577 if (d != NULL && d->dv_copyID != copyID) 4578 { 4579 d->dv_copyID = copyID; 4580 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL); 4581 } 4582 return FALSE; 4583 } 4584 4585 /* 4586 * Mark a list and its items with "copyID". 4587 * Returns TRUE if setting references failed somehow. 4588 */ 4589 int 4590 set_ref_in_list(list_T *ll, int copyID) 4591 { 4592 if (ll != NULL && ll->lv_copyID != copyID) 4593 { 4594 ll->lv_copyID = copyID; 4595 return set_ref_in_list_items(ll, copyID, NULL); 4596 } 4597 return FALSE; 4598 } 4599 4600 /* 4601 * Mark all lists and dicts referenced through list "l" with "copyID". 4602 * "ht_stack" is used to add hashtabs to be marked. Can be NULL. 4603 * 4604 * Returns TRUE if setting references failed somehow. 4605 */ 4606 int 4607 set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack) 4608 { 4609 listitem_T *li; 4610 int abort = FALSE; 4611 list_T *cur_l; 4612 list_stack_T *list_stack = NULL; 4613 list_stack_T *tempitem; 4614 4615 cur_l = l; 4616 for (;;) 4617 { 4618 if (!abort && cur_l->lv_first != &range_list_item) 4619 // Mark each item in the list. If the item contains a hashtab 4620 // it is added to ht_stack, if it contains a list it is added to 4621 // list_stack. 4622 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next) 4623 abort = abort || set_ref_in_item(&li->li_tv, copyID, 4624 ht_stack, &list_stack); 4625 if (list_stack == NULL) 4626 break; 4627 4628 // take an item from the stack 4629 cur_l = list_stack->list; 4630 tempitem = list_stack; 4631 list_stack = list_stack->prev; 4632 free(tempitem); 4633 } 4634 4635 return abort; 4636 } 4637 4638 /* 4639 * Mark all lists and dicts referenced through typval "tv" with "copyID". 4640 * "list_stack" is used to add lists to be marked. Can be NULL. 4641 * "ht_stack" is used to add hashtabs to be marked. Can be NULL. 4642 * 4643 * Returns TRUE if setting references failed somehow. 4644 */ 4645 int 4646 set_ref_in_item( 4647 typval_T *tv, 4648 int copyID, 4649 ht_stack_T **ht_stack, 4650 list_stack_T **list_stack) 4651 { 4652 int abort = FALSE; 4653 4654 if (tv->v_type == VAR_DICT) 4655 { 4656 dict_T *dd = tv->vval.v_dict; 4657 4658 if (dd != NULL && dd->dv_copyID != copyID) 4659 { 4660 // Didn't see this dict yet. 4661 dd->dv_copyID = copyID; 4662 if (ht_stack == NULL) 4663 { 4664 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack); 4665 } 4666 else 4667 { 4668 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T); 4669 4670 if (newitem == NULL) 4671 abort = TRUE; 4672 else 4673 { 4674 newitem->ht = &dd->dv_hashtab; 4675 newitem->prev = *ht_stack; 4676 *ht_stack = newitem; 4677 } 4678 } 4679 } 4680 } 4681 else if (tv->v_type == VAR_LIST) 4682 { 4683 list_T *ll = tv->vval.v_list; 4684 4685 if (ll != NULL && ll->lv_copyID != copyID) 4686 { 4687 // Didn't see this list yet. 4688 ll->lv_copyID = copyID; 4689 if (list_stack == NULL) 4690 { 4691 abort = set_ref_in_list_items(ll, copyID, ht_stack); 4692 } 4693 else 4694 { 4695 list_stack_T *newitem = ALLOC_ONE(list_stack_T); 4696 4697 if (newitem == NULL) 4698 abort = TRUE; 4699 else 4700 { 4701 newitem->list = ll; 4702 newitem->prev = *list_stack; 4703 *list_stack = newitem; 4704 } 4705 } 4706 } 4707 } 4708 else if (tv->v_type == VAR_FUNC) 4709 { 4710 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID); 4711 } 4712 else if (tv->v_type == VAR_PARTIAL) 4713 { 4714 partial_T *pt = tv->vval.v_partial; 4715 int i; 4716 4717 if (pt != NULL && pt->pt_copyID != copyID) 4718 { 4719 // Didn't see this partial yet. 4720 pt->pt_copyID = copyID; 4721 4722 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID); 4723 4724 if (pt->pt_dict != NULL) 4725 { 4726 typval_T dtv; 4727 4728 dtv.v_type = VAR_DICT; 4729 dtv.vval.v_dict = pt->pt_dict; 4730 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4731 } 4732 4733 for (i = 0; i < pt->pt_argc; ++i) 4734 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID, 4735 ht_stack, list_stack); 4736 if (pt->pt_funcstack != NULL) 4737 { 4738 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data; 4739 4740 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i) 4741 abort = abort || set_ref_in_item(stack + i, copyID, 4742 ht_stack, list_stack); 4743 } 4744 4745 } 4746 } 4747 #ifdef FEAT_JOB_CHANNEL 4748 else if (tv->v_type == VAR_JOB) 4749 { 4750 job_T *job = tv->vval.v_job; 4751 typval_T dtv; 4752 4753 if (job != NULL && job->jv_copyID != copyID) 4754 { 4755 job->jv_copyID = copyID; 4756 if (job->jv_channel != NULL) 4757 { 4758 dtv.v_type = VAR_CHANNEL; 4759 dtv.vval.v_channel = job->jv_channel; 4760 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4761 } 4762 if (job->jv_exit_cb.cb_partial != NULL) 4763 { 4764 dtv.v_type = VAR_PARTIAL; 4765 dtv.vval.v_partial = job->jv_exit_cb.cb_partial; 4766 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4767 } 4768 } 4769 } 4770 else if (tv->v_type == VAR_CHANNEL) 4771 { 4772 channel_T *ch =tv->vval.v_channel; 4773 ch_part_T part; 4774 typval_T dtv; 4775 jsonq_T *jq; 4776 cbq_T *cq; 4777 4778 if (ch != NULL && ch->ch_copyID != copyID) 4779 { 4780 ch->ch_copyID = copyID; 4781 for (part = PART_SOCK; part < PART_COUNT; ++part) 4782 { 4783 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL; 4784 jq = jq->jq_next) 4785 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack); 4786 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL; 4787 cq = cq->cq_next) 4788 if (cq->cq_callback.cb_partial != NULL) 4789 { 4790 dtv.v_type = VAR_PARTIAL; 4791 dtv.vval.v_partial = cq->cq_callback.cb_partial; 4792 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4793 } 4794 if (ch->ch_part[part].ch_callback.cb_partial != NULL) 4795 { 4796 dtv.v_type = VAR_PARTIAL; 4797 dtv.vval.v_partial = 4798 ch->ch_part[part].ch_callback.cb_partial; 4799 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4800 } 4801 } 4802 if (ch->ch_callback.cb_partial != NULL) 4803 { 4804 dtv.v_type = VAR_PARTIAL; 4805 dtv.vval.v_partial = ch->ch_callback.cb_partial; 4806 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4807 } 4808 if (ch->ch_close_cb.cb_partial != NULL) 4809 { 4810 dtv.v_type = VAR_PARTIAL; 4811 dtv.vval.v_partial = ch->ch_close_cb.cb_partial; 4812 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4813 } 4814 } 4815 } 4816 #endif 4817 return abort; 4818 } 4819 4820 /* 4821 * Return a string with the string representation of a variable. 4822 * If the memory is allocated "tofree" is set to it, otherwise NULL. 4823 * "numbuf" is used for a number. 4824 * When "copyID" is not NULL replace recursive lists and dicts with "...". 4825 * When both "echo_style" and "composite_val" are FALSE, put quotes around 4826 * strings as "string()", otherwise does not put quotes around strings, as 4827 * ":echo" displays values. 4828 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists 4829 * are replaced with "...". 4830 * May return NULL. 4831 */ 4832 char_u * 4833 echo_string_core( 4834 typval_T *tv, 4835 char_u **tofree, 4836 char_u *numbuf, 4837 int copyID, 4838 int echo_style, 4839 int restore_copyID, 4840 int composite_val) 4841 { 4842 static int recurse = 0; 4843 char_u *r = NULL; 4844 4845 if (recurse >= DICT_MAXNEST) 4846 { 4847 if (!did_echo_string_emsg) 4848 { 4849 // Only give this message once for a recursive call to avoid 4850 // flooding the user with errors. And stop iterating over lists 4851 // and dicts. 4852 did_echo_string_emsg = TRUE; 4853 emsg(_("E724: variable nested too deep for displaying")); 4854 } 4855 *tofree = NULL; 4856 return (char_u *)"{E724}"; 4857 } 4858 ++recurse; 4859 4860 switch (tv->v_type) 4861 { 4862 case VAR_STRING: 4863 if (echo_style && !composite_val) 4864 { 4865 *tofree = NULL; 4866 r = tv->vval.v_string; 4867 if (r == NULL) 4868 r = (char_u *)""; 4869 } 4870 else 4871 { 4872 *tofree = string_quote(tv->vval.v_string, FALSE); 4873 r = *tofree; 4874 } 4875 break; 4876 4877 case VAR_FUNC: 4878 if (echo_style) 4879 { 4880 *tofree = NULL; 4881 r = tv->vval.v_string; 4882 } 4883 else 4884 { 4885 *tofree = string_quote(tv->vval.v_string, TRUE); 4886 r = *tofree; 4887 } 4888 break; 4889 4890 case VAR_PARTIAL: 4891 { 4892 partial_T *pt = tv->vval.v_partial; 4893 char_u *fname = string_quote(pt == NULL ? NULL 4894 : partial_name(pt), FALSE); 4895 garray_T ga; 4896 int i; 4897 char_u *tf; 4898 4899 ga_init2(&ga, 1, 100); 4900 ga_concat(&ga, (char_u *)"function("); 4901 if (fname != NULL) 4902 { 4903 ga_concat(&ga, fname); 4904 vim_free(fname); 4905 } 4906 if (pt != NULL && pt->pt_argc > 0) 4907 { 4908 ga_concat(&ga, (char_u *)", ["); 4909 for (i = 0; i < pt->pt_argc; ++i) 4910 { 4911 if (i > 0) 4912 ga_concat(&ga, (char_u *)", "); 4913 ga_concat(&ga, 4914 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID)); 4915 vim_free(tf); 4916 } 4917 ga_concat(&ga, (char_u *)"]"); 4918 } 4919 if (pt != NULL && pt->pt_dict != NULL) 4920 { 4921 typval_T dtv; 4922 4923 ga_concat(&ga, (char_u *)", "); 4924 dtv.v_type = VAR_DICT; 4925 dtv.vval.v_dict = pt->pt_dict; 4926 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID)); 4927 vim_free(tf); 4928 } 4929 ga_concat(&ga, (char_u *)")"); 4930 4931 *tofree = ga.ga_data; 4932 r = *tofree; 4933 break; 4934 } 4935 4936 case VAR_BLOB: 4937 r = blob2string(tv->vval.v_blob, tofree, numbuf); 4938 break; 4939 4940 case VAR_LIST: 4941 if (tv->vval.v_list == NULL) 4942 { 4943 // NULL list is equivalent to empty list. 4944 *tofree = NULL; 4945 r = (char_u *)"[]"; 4946 } 4947 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID 4948 && tv->vval.v_list->lv_len > 0) 4949 { 4950 *tofree = NULL; 4951 r = (char_u *)"[...]"; 4952 } 4953 else 4954 { 4955 int old_copyID = tv->vval.v_list->lv_copyID; 4956 4957 tv->vval.v_list->lv_copyID = copyID; 4958 *tofree = list2string(tv, copyID, restore_copyID); 4959 if (restore_copyID) 4960 tv->vval.v_list->lv_copyID = old_copyID; 4961 r = *tofree; 4962 } 4963 break; 4964 4965 case VAR_DICT: 4966 if (tv->vval.v_dict == NULL) 4967 { 4968 // NULL dict is equivalent to empty dict. 4969 *tofree = NULL; 4970 r = (char_u *)"{}"; 4971 } 4972 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID 4973 && tv->vval.v_dict->dv_hashtab.ht_used != 0) 4974 { 4975 *tofree = NULL; 4976 r = (char_u *)"{...}"; 4977 } 4978 else 4979 { 4980 int old_copyID = tv->vval.v_dict->dv_copyID; 4981 4982 tv->vval.v_dict->dv_copyID = copyID; 4983 *tofree = dict2string(tv, copyID, restore_copyID); 4984 if (restore_copyID) 4985 tv->vval.v_dict->dv_copyID = old_copyID; 4986 r = *tofree; 4987 } 4988 break; 4989 4990 case VAR_NUMBER: 4991 case VAR_UNKNOWN: 4992 case VAR_ANY: 4993 case VAR_VOID: 4994 *tofree = NULL; 4995 r = tv_get_string_buf(tv, numbuf); 4996 break; 4997 4998 case VAR_JOB: 4999 case VAR_CHANNEL: 5000 *tofree = NULL; 5001 r = tv_get_string_buf(tv, numbuf); 5002 if (composite_val) 5003 { 5004 *tofree = string_quote(r, FALSE); 5005 r = *tofree; 5006 } 5007 break; 5008 5009 case VAR_FLOAT: 5010 #ifdef FEAT_FLOAT 5011 *tofree = NULL; 5012 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float); 5013 r = numbuf; 5014 break; 5015 #endif 5016 5017 case VAR_BOOL: 5018 case VAR_SPECIAL: 5019 *tofree = NULL; 5020 r = (char_u *)get_var_special_name(tv->vval.v_number); 5021 break; 5022 } 5023 5024 if (--recurse == 0) 5025 did_echo_string_emsg = FALSE; 5026 return r; 5027 } 5028 5029 /* 5030 * Return a string with the string representation of a variable. 5031 * If the memory is allocated "tofree" is set to it, otherwise NULL. 5032 * "numbuf" is used for a number. 5033 * Does not put quotes around strings, as ":echo" displays values. 5034 * When "copyID" is not NULL replace recursive lists and dicts with "...". 5035 * May return NULL. 5036 */ 5037 char_u * 5038 echo_string( 5039 typval_T *tv, 5040 char_u **tofree, 5041 char_u *numbuf, 5042 int copyID) 5043 { 5044 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE); 5045 } 5046 5047 /* 5048 * Return string "str" in ' quotes, doubling ' characters. 5049 * If "str" is NULL an empty string is assumed. 5050 * If "function" is TRUE make it function('string'). 5051 */ 5052 char_u * 5053 string_quote(char_u *str, int function) 5054 { 5055 unsigned len; 5056 char_u *p, *r, *s; 5057 5058 len = (function ? 13 : 3); 5059 if (str != NULL) 5060 { 5061 len += (unsigned)STRLEN(str); 5062 for (p = str; *p != NUL; MB_PTR_ADV(p)) 5063 if (*p == '\'') 5064 ++len; 5065 } 5066 s = r = alloc(len); 5067 if (r != NULL) 5068 { 5069 if (function) 5070 { 5071 STRCPY(r, "function('"); 5072 r += 10; 5073 } 5074 else 5075 *r++ = '\''; 5076 if (str != NULL) 5077 for (p = str; *p != NUL; ) 5078 { 5079 if (*p == '\'') 5080 *r++ = '\''; 5081 MB_COPY_CHAR(p, r); 5082 } 5083 *r++ = '\''; 5084 if (function) 5085 *r++ = ')'; 5086 *r++ = NUL; 5087 } 5088 return s; 5089 } 5090 5091 #if defined(FEAT_FLOAT) || defined(PROTO) 5092 /* 5093 * Convert the string "text" to a floating point number. 5094 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure 5095 * this always uses a decimal point. 5096 * Returns the length of the text that was consumed. 5097 */ 5098 int 5099 string2float( 5100 char_u *text, 5101 float_T *value) // result stored here 5102 { 5103 char *s = (char *)text; 5104 float_T f; 5105 5106 // MS-Windows does not deal with "inf" and "nan" properly. 5107 if (STRNICMP(text, "inf", 3) == 0) 5108 { 5109 *value = INFINITY; 5110 return 3; 5111 } 5112 if (STRNICMP(text, "-inf", 3) == 0) 5113 { 5114 *value = -INFINITY; 5115 return 4; 5116 } 5117 if (STRNICMP(text, "nan", 3) == 0) 5118 { 5119 *value = NAN; 5120 return 3; 5121 } 5122 f = strtod(s, &s); 5123 *value = f; 5124 return (int)((char_u *)s - text); 5125 } 5126 #endif 5127 5128 /* 5129 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a 5130 * character index. Works only for loaded buffers. Returns -1 on failure. 5131 * The index of the first byte and the first character is zero. 5132 */ 5133 int 5134 buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx) 5135 { 5136 char_u *str; 5137 char_u *t; 5138 int count; 5139 5140 if (buf == NULL || buf->b_ml.ml_mfp == NULL) 5141 return -1; 5142 5143 if (lnum > buf->b_ml.ml_line_count) 5144 lnum = buf->b_ml.ml_line_count; 5145 5146 str = ml_get_buf(buf, lnum, FALSE); 5147 if (str == NULL) 5148 return -1; 5149 5150 if (*str == NUL) 5151 return 0; 5152 5153 // count the number of characters 5154 t = str; 5155 for (count = 0; *t != NUL && t <= str + byteidx; count++) 5156 t += mb_ptr2len(t); 5157 5158 // In insert mode, when the cursor is at the end of a non-empty line, 5159 // byteidx points to the NUL character immediately past the end of the 5160 // string. In this case, add one to the character count. 5161 if (*t == NUL && byteidx != 0 && t == str + byteidx) 5162 count++; 5163 5164 return count - 1; 5165 } 5166 5167 /* 5168 * Convert the specified character index of line 'lnum' in buffer 'buf' to a 5169 * byte index. Works only for loaded buffers. Returns -1 on failure. 5170 * The index of the first byte and the first character is zero. 5171 */ 5172 int 5173 buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx) 5174 { 5175 char_u *str; 5176 char_u *t; 5177 5178 if (buf == NULL || buf->b_ml.ml_mfp == NULL) 5179 return -1; 5180 5181 if (lnum > buf->b_ml.ml_line_count) 5182 lnum = buf->b_ml.ml_line_count; 5183 5184 str = ml_get_buf(buf, lnum, FALSE); 5185 if (str == NULL) 5186 return -1; 5187 5188 // Convert the character offset to a byte offset 5189 t = str; 5190 while (*t != NUL && --charidx > 0) 5191 t += mb_ptr2len(t); 5192 5193 return t - str; 5194 } 5195 5196 /* 5197 * Translate a String variable into a position. 5198 * Returns NULL when there is an error. 5199 */ 5200 pos_T * 5201 var2fpos( 5202 typval_T *varp, 5203 int dollar_lnum, // TRUE when $ is last line 5204 int *fnum, // set to fnum for '0, 'A, etc. 5205 int charcol) // return character column 5206 { 5207 char_u *name; 5208 static pos_T pos; 5209 pos_T *pp; 5210 5211 // Argument can be [lnum, col, coladd]. 5212 if (varp->v_type == VAR_LIST) 5213 { 5214 list_T *l; 5215 int len; 5216 int error = FALSE; 5217 listitem_T *li; 5218 5219 l = varp->vval.v_list; 5220 if (l == NULL) 5221 return NULL; 5222 5223 // Get the line number 5224 pos.lnum = list_find_nr(l, 0L, &error); 5225 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count) 5226 return NULL; // invalid line number 5227 if (charcol) 5228 len = (long)mb_charlen(ml_get(pos.lnum)); 5229 else 5230 len = (long)STRLEN(ml_get(pos.lnum)); 5231 5232 // Get the column number 5233 // We accept "$" for the column number: last column. 5234 li = list_find(l, 1L); 5235 if (li != NULL && li->li_tv.v_type == VAR_STRING 5236 && li->li_tv.vval.v_string != NULL 5237 && STRCMP(li->li_tv.vval.v_string, "$") == 0) 5238 { 5239 pos.col = len + 1; 5240 } 5241 else 5242 { 5243 pos.col = list_find_nr(l, 1L, &error); 5244 if (error) 5245 return NULL; 5246 } 5247 5248 // Accept a position up to the NUL after the line. 5249 if (pos.col == 0 || (int)pos.col > len + 1) 5250 return NULL; // invalid column number 5251 --pos.col; 5252 5253 // Get the virtual offset. Defaults to zero. 5254 pos.coladd = list_find_nr(l, 2L, &error); 5255 if (error) 5256 pos.coladd = 0; 5257 5258 return &pos; 5259 } 5260 5261 name = tv_get_string_chk(varp); 5262 if (name == NULL) 5263 return NULL; 5264 if (name[0] == '.') // cursor 5265 { 5266 pos = curwin->w_cursor; 5267 if (charcol) 5268 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col); 5269 return &pos; 5270 } 5271 if (name[0] == 'v' && name[1] == NUL) // Visual start 5272 { 5273 if (VIsual_active) 5274 pos = VIsual; 5275 else 5276 pos = curwin->w_cursor; 5277 if (charcol) 5278 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col); 5279 return &pos; 5280 } 5281 if (name[0] == '\'') // mark 5282 { 5283 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum); 5284 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0) 5285 return NULL; 5286 if (charcol) 5287 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col); 5288 return pp; 5289 } 5290 5291 pos.coladd = 0; 5292 5293 if (name[0] == 'w' && dollar_lnum) 5294 { 5295 pos.col = 0; 5296 if (name[1] == '0') // "w0": first visible line 5297 { 5298 update_topline(); 5299 // In silent Ex mode topline is zero, but that's not a valid line 5300 // number; use one instead. 5301 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1; 5302 return &pos; 5303 } 5304 else if (name[1] == '$') // "w$": last visible line 5305 { 5306 validate_botline(); 5307 // In silent Ex mode botline is zero, return zero then. 5308 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0; 5309 return &pos; 5310 } 5311 } 5312 else if (name[0] == '$') // last column or line 5313 { 5314 if (dollar_lnum) 5315 { 5316 pos.lnum = curbuf->b_ml.ml_line_count; 5317 pos.col = 0; 5318 } 5319 else 5320 { 5321 pos.lnum = curwin->w_cursor.lnum; 5322 if (charcol) 5323 pos.col = (colnr_T)mb_charlen(ml_get_curline()); 5324 else 5325 pos.col = (colnr_T)STRLEN(ml_get_curline()); 5326 } 5327 return &pos; 5328 } 5329 return NULL; 5330 } 5331 5332 /* 5333 * Convert list in "arg" into a position and optional file number. 5334 * When "fnump" is NULL there is no file number, only 3 items. 5335 * Note that the column is passed on as-is, the caller may want to decrement 5336 * it to use 1 for the first column. 5337 * Return FAIL when conversion is not possible, doesn't check the position for 5338 * validity. 5339 */ 5340 int 5341 list2fpos( 5342 typval_T *arg, 5343 pos_T *posp, 5344 int *fnump, 5345 colnr_T *curswantp, 5346 int charcol) 5347 { 5348 list_T *l = arg->vval.v_list; 5349 long i = 0; 5350 long n; 5351 5352 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only 5353 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional. 5354 if (arg->v_type != VAR_LIST 5355 || l == NULL 5356 || l->lv_len < (fnump == NULL ? 2 : 3) 5357 || l->lv_len > (fnump == NULL ? 4 : 5)) 5358 return FAIL; 5359 5360 if (fnump != NULL) 5361 { 5362 n = list_find_nr(l, i++, NULL); // fnum 5363 if (n < 0) 5364 return FAIL; 5365 if (n == 0) 5366 n = curbuf->b_fnum; // current buffer 5367 *fnump = n; 5368 } 5369 5370 n = list_find_nr(l, i++, NULL); // lnum 5371 if (n < 0) 5372 return FAIL; 5373 posp->lnum = n; 5374 5375 n = list_find_nr(l, i++, NULL); // col 5376 if (n < 0) 5377 return FAIL; 5378 // If character position is specified, then convert to byte position 5379 if (charcol) 5380 { 5381 buf_T *buf; 5382 5383 // Get the text for the specified line in a loaded buffer 5384 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump); 5385 if (buf == NULL || buf->b_ml.ml_mfp == NULL) 5386 return FAIL; 5387 5388 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1; 5389 } 5390 posp->col = n; 5391 5392 n = list_find_nr(l, i, NULL); // off 5393 if (n < 0) 5394 posp->coladd = 0; 5395 else 5396 posp->coladd = n; 5397 5398 if (curswantp != NULL) 5399 *curswantp = list_find_nr(l, i + 1, NULL); // curswant 5400 5401 return OK; 5402 } 5403 5404 /* 5405 * Get the length of an environment variable name. 5406 * Advance "arg" to the first character after the name. 5407 * Return 0 for error. 5408 */ 5409 int 5410 get_env_len(char_u **arg) 5411 { 5412 char_u *p; 5413 int len; 5414 5415 for (p = *arg; vim_isIDc(*p); ++p) 5416 ; 5417 if (p == *arg) // no name found 5418 return 0; 5419 5420 len = (int)(p - *arg); 5421 *arg = p; 5422 return len; 5423 } 5424 5425 /* 5426 * Get the length of the name of a function or internal variable. 5427 * "arg" is advanced to after the name. 5428 * Return 0 if something is wrong. 5429 */ 5430 int 5431 get_id_len(char_u **arg) 5432 { 5433 char_u *p; 5434 int len; 5435 5436 // Find the end of the name. 5437 for (p = *arg; eval_isnamec(*p); ++p) 5438 { 5439 if (*p == ':') 5440 { 5441 // "s:" is start of "s:var", but "n:" is not and can be used in 5442 // slice "[n:]". Also "xx:" is not a namespace. 5443 len = (int)(p - *arg); 5444 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL) 5445 || len > 1) 5446 break; 5447 } 5448 } 5449 if (p == *arg) // no name found 5450 return 0; 5451 5452 len = (int)(p - *arg); 5453 *arg = p; 5454 5455 return len; 5456 } 5457 5458 /* 5459 * Get the length of the name of a variable or function. 5460 * Only the name is recognized, does not handle ".key" or "[idx]". 5461 * "arg" is advanced to the first non-white character after the name. 5462 * Return -1 if curly braces expansion failed. 5463 * Return 0 if something else is wrong. 5464 * If the name contains 'magic' {}'s, expand them and return the 5465 * expanded name in an allocated string via 'alias' - caller must free. 5466 */ 5467 int 5468 get_name_len( 5469 char_u **arg, 5470 char_u **alias, 5471 int evaluate, 5472 int verbose) 5473 { 5474 int len; 5475 char_u *p; 5476 char_u *expr_start; 5477 char_u *expr_end; 5478 5479 *alias = NULL; // default to no alias 5480 5481 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA 5482 && (*arg)[2] == (int)KE_SNR) 5483 { 5484 // hard coded <SNR>, already translated 5485 *arg += 3; 5486 return get_id_len(arg) + 3; 5487 } 5488 len = eval_fname_script(*arg); 5489 if (len > 0) 5490 { 5491 // literal "<SID>", "s:" or "<SNR>" 5492 *arg += len; 5493 } 5494 5495 /* 5496 * Find the end of the name; check for {} construction. 5497 */ 5498 p = find_name_end(*arg, &expr_start, &expr_end, 5499 len > 0 ? 0 : FNE_CHECK_START); 5500 if (expr_start != NULL) 5501 { 5502 char_u *temp_string; 5503 5504 if (!evaluate) 5505 { 5506 len += (int)(p - *arg); 5507 *arg = skipwhite(p); 5508 return len; 5509 } 5510 5511 /* 5512 * Include any <SID> etc in the expanded string: 5513 * Thus the -len here. 5514 */ 5515 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p); 5516 if (temp_string == NULL) 5517 return -1; 5518 *alias = temp_string; 5519 *arg = skipwhite(p); 5520 return (int)STRLEN(temp_string); 5521 } 5522 5523 len += get_id_len(arg); 5524 // Only give an error when there is something, otherwise it will be 5525 // reported at a higher level. 5526 if (len == 0 && verbose && **arg != NUL) 5527 semsg(_(e_invexpr2), *arg); 5528 5529 return len; 5530 } 5531 5532 /* 5533 * Find the end of a variable or function name, taking care of magic braces. 5534 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the 5535 * start and end of the first magic braces item. 5536 * "flags" can have FNE_INCL_BR and FNE_CHECK_START. 5537 * Return a pointer to just after the name. Equal to "arg" if there is no 5538 * valid name. 5539 */ 5540 char_u * 5541 find_name_end( 5542 char_u *arg, 5543 char_u **expr_start, 5544 char_u **expr_end, 5545 int flags) 5546 { 5547 int mb_nest = 0; 5548 int br_nest = 0; 5549 char_u *p; 5550 int len; 5551 int vim9script = in_vim9script(); 5552 5553 if (expr_start != NULL) 5554 { 5555 *expr_start = NULL; 5556 *expr_end = NULL; 5557 } 5558 5559 // Quick check for valid starting character. 5560 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) 5561 && (*arg != '{' || vim9script)) 5562 return arg; 5563 5564 for (p = arg; *p != NUL 5565 && (eval_isnamec(*p) 5566 || (*p == '{' && !vim9script) 5567 || ((flags & FNE_INCL_BR) && (*p == '[' 5568 || (*p == '.' && eval_isdictc(p[1])))) 5569 || mb_nest != 0 5570 || br_nest != 0); MB_PTR_ADV(p)) 5571 { 5572 if (*p == '\'') 5573 { 5574 // skip over 'string' to avoid counting [ and ] inside it. 5575 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p)) 5576 ; 5577 if (*p == NUL) 5578 break; 5579 } 5580 else if (*p == '"') 5581 { 5582 // skip over "str\"ing" to avoid counting [ and ] inside it. 5583 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p)) 5584 if (*p == '\\' && p[1] != NUL) 5585 ++p; 5586 if (*p == NUL) 5587 break; 5588 } 5589 else if (br_nest == 0 && mb_nest == 0 && *p == ':') 5590 { 5591 // "s:" is start of "s:var", but "n:" is not and can be used in 5592 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. 5593 len = (int)(p - arg); 5594 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL) 5595 || (len > 1 && p[-1] != '}')) 5596 break; 5597 } 5598 5599 if (mb_nest == 0) 5600 { 5601 if (*p == '[') 5602 ++br_nest; 5603 else if (*p == ']') 5604 --br_nest; 5605 } 5606 5607 if (br_nest == 0 && !vim9script) 5608 { 5609 if (*p == '{') 5610 { 5611 mb_nest++; 5612 if (expr_start != NULL && *expr_start == NULL) 5613 *expr_start = p; 5614 } 5615 else if (*p == '}') 5616 { 5617 mb_nest--; 5618 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL) 5619 *expr_end = p; 5620 } 5621 } 5622 } 5623 5624 return p; 5625 } 5626 5627 /* 5628 * Expands out the 'magic' {}'s in a variable/function name. 5629 * Note that this can call itself recursively, to deal with 5630 * constructs like foo{bar}{baz}{bam} 5631 * The four pointer arguments point to "foo{expre}ss{ion}bar" 5632 * "in_start" ^ 5633 * "expr_start" ^ 5634 * "expr_end" ^ 5635 * "in_end" ^ 5636 * 5637 * Returns a new allocated string, which the caller must free. 5638 * Returns NULL for failure. 5639 */ 5640 static char_u * 5641 make_expanded_name( 5642 char_u *in_start, 5643 char_u *expr_start, 5644 char_u *expr_end, 5645 char_u *in_end) 5646 { 5647 char_u c1; 5648 char_u *retval = NULL; 5649 char_u *temp_result; 5650 5651 if (expr_end == NULL || in_end == NULL) 5652 return NULL; 5653 *expr_start = NUL; 5654 *expr_end = NUL; 5655 c1 = *in_end; 5656 *in_end = NUL; 5657 5658 temp_result = eval_to_string(expr_start + 1, FALSE); 5659 if (temp_result != NULL) 5660 { 5661 retval = alloc(STRLEN(temp_result) + (expr_start - in_start) 5662 + (in_end - expr_end) + 1); 5663 if (retval != NULL) 5664 { 5665 STRCPY(retval, in_start); 5666 STRCAT(retval, temp_result); 5667 STRCAT(retval, expr_end + 1); 5668 } 5669 } 5670 vim_free(temp_result); 5671 5672 *in_end = c1; // put char back for error messages 5673 *expr_start = '{'; 5674 *expr_end = '}'; 5675 5676 if (retval != NULL) 5677 { 5678 temp_result = find_name_end(retval, &expr_start, &expr_end, 0); 5679 if (expr_start != NULL) 5680 { 5681 // Further expansion! 5682 temp_result = make_expanded_name(retval, expr_start, 5683 expr_end, temp_result); 5684 vim_free(retval); 5685 retval = temp_result; 5686 } 5687 } 5688 5689 return retval; 5690 } 5691 5692 /* 5693 * Return TRUE if character "c" can be used in a variable or function name. 5694 * Does not include '{' or '}' for magic braces. 5695 */ 5696 int 5697 eval_isnamec(int c) 5698 { 5699 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR; 5700 } 5701 5702 /* 5703 * Return TRUE if character "c" can be used as the first character in a 5704 * variable or function name (excluding '{' and '}'). 5705 */ 5706 int 5707 eval_isnamec1(int c) 5708 { 5709 return ASCII_ISALPHA(c) || c == '_'; 5710 } 5711 5712 /* 5713 * Return TRUE if character "c" can be used as the first character of a 5714 * dictionary key. 5715 */ 5716 int 5717 eval_isdictc(int c) 5718 { 5719 return ASCII_ISALNUM(c) || c == '_'; 5720 } 5721 5722 /* 5723 * Handle: 5724 * - expr[expr], expr[expr:expr] subscript 5725 * - ".name" lookup 5726 * - function call with Funcref variable: func(expr) 5727 * - method call: var->method() 5728 * 5729 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len() 5730 */ 5731 int 5732 handle_subscript( 5733 char_u **arg, 5734 typval_T *rettv, 5735 evalarg_T *evalarg, 5736 int verbose) // give error messages 5737 { 5738 int evaluate = evalarg != NULL 5739 && (evalarg->eval_flags & EVAL_EVALUATE); 5740 int ret = OK; 5741 dict_T *selfdict = NULL; 5742 int check_white = TRUE; 5743 int getnext; 5744 char_u *p; 5745 5746 while (ret == OK) 5747 { 5748 // When at the end of the line and ".name" or "->{" or "->X" follows in 5749 // the next line then consume the line break. 5750 p = eval_next_non_blank(*arg, evalarg, &getnext); 5751 if (getnext 5752 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1])) 5753 || (p[0] == '-' && p[1] == '>' 5754 && (p[2] == '{' || ASCII_ISALPHA(p[2]))))) 5755 { 5756 *arg = eval_next_line(evalarg); 5757 p = *arg; 5758 check_white = FALSE; 5759 } 5760 5761 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC 5762 || rettv->v_type == VAR_PARTIAL)) 5763 && (!check_white || !VIM_ISWHITE(*(*arg - 1)))) 5764 { 5765 ret = call_func_rettv(arg, evalarg, rettv, evaluate, 5766 selfdict, NULL); 5767 5768 // Stop the expression evaluation when immediately aborting on 5769 // error, or when an interrupt occurred or an exception was thrown 5770 // but not caught. 5771 if (aborting()) 5772 { 5773 if (ret == OK) 5774 clear_tv(rettv); 5775 ret = FAIL; 5776 } 5777 dict_unref(selfdict); 5778 selfdict = NULL; 5779 } 5780 else if (p[0] == '-' && p[1] == '>') 5781 { 5782 *arg = skipwhite(p + 2); 5783 if (ret == OK) 5784 { 5785 if ((**arg == '{' && !in_vim9script()) || **arg == '(') 5786 // expr->{lambda}() or expr->(lambda)() 5787 ret = eval_lambda(arg, rettv, evalarg, verbose); 5788 else 5789 // expr->name() 5790 ret = eval_method(arg, rettv, evalarg, verbose); 5791 } 5792 } 5793 // "." is ".name" lookup when we found a dict or when evaluating and 5794 // scriptversion is at least 2, where string concatenation is "..". 5795 else if (**arg == '[' 5796 || (**arg == '.' && (rettv->v_type == VAR_DICT 5797 || (!evaluate 5798 && (*arg)[1] != '.' 5799 && current_sctx.sc_version >= 2)))) 5800 { 5801 dict_unref(selfdict); 5802 if (rettv->v_type == VAR_DICT) 5803 { 5804 selfdict = rettv->vval.v_dict; 5805 if (selfdict != NULL) 5806 ++selfdict->dv_refcount; 5807 } 5808 else 5809 selfdict = NULL; 5810 if (eval_index(arg, rettv, evalarg, verbose) == FAIL) 5811 { 5812 clear_tv(rettv); 5813 ret = FAIL; 5814 } 5815 } 5816 else 5817 break; 5818 } 5819 5820 // Turn "dict.Func" into a partial for "Func" bound to "dict". 5821 // Don't do this when "Func" is already a partial that was bound 5822 // explicitly (pt_auto is FALSE). 5823 if (selfdict != NULL 5824 && (rettv->v_type == VAR_FUNC 5825 || (rettv->v_type == VAR_PARTIAL 5826 && (rettv->vval.v_partial->pt_auto 5827 || rettv->vval.v_partial->pt_dict == NULL)))) 5828 selfdict = make_partial(selfdict, rettv); 5829 5830 dict_unref(selfdict); 5831 return ret; 5832 } 5833 5834 /* 5835 * Make a copy of an item. 5836 * Lists and Dictionaries are also copied. A deep copy if "deep" is set. 5837 * For deepcopy() "copyID" is zero for a full copy or the ID for when a 5838 * reference to an already copied list/dict can be used. 5839 * Returns FAIL or OK. 5840 */ 5841 int 5842 item_copy( 5843 typval_T *from, 5844 typval_T *to, 5845 int deep, 5846 int copyID) 5847 { 5848 static int recurse = 0; 5849 int ret = OK; 5850 5851 if (recurse >= DICT_MAXNEST) 5852 { 5853 emsg(_("E698: variable nested too deep for making a copy")); 5854 return FAIL; 5855 } 5856 ++recurse; 5857 5858 switch (from->v_type) 5859 { 5860 case VAR_NUMBER: 5861 case VAR_FLOAT: 5862 case VAR_STRING: 5863 case VAR_FUNC: 5864 case VAR_PARTIAL: 5865 case VAR_BOOL: 5866 case VAR_SPECIAL: 5867 case VAR_JOB: 5868 case VAR_CHANNEL: 5869 copy_tv(from, to); 5870 break; 5871 case VAR_LIST: 5872 to->v_type = VAR_LIST; 5873 to->v_lock = 0; 5874 if (from->vval.v_list == NULL) 5875 to->vval.v_list = NULL; 5876 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID) 5877 { 5878 // use the copy made earlier 5879 to->vval.v_list = from->vval.v_list->lv_copylist; 5880 ++to->vval.v_list->lv_refcount; 5881 } 5882 else 5883 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID); 5884 if (to->vval.v_list == NULL) 5885 ret = FAIL; 5886 break; 5887 case VAR_BLOB: 5888 ret = blob_copy(from->vval.v_blob, to); 5889 break; 5890 case VAR_DICT: 5891 to->v_type = VAR_DICT; 5892 to->v_lock = 0; 5893 if (from->vval.v_dict == NULL) 5894 to->vval.v_dict = NULL; 5895 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID) 5896 { 5897 // use the copy made earlier 5898 to->vval.v_dict = from->vval.v_dict->dv_copydict; 5899 ++to->vval.v_dict->dv_refcount; 5900 } 5901 else 5902 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID); 5903 if (to->vval.v_dict == NULL) 5904 ret = FAIL; 5905 break; 5906 case VAR_UNKNOWN: 5907 case VAR_ANY: 5908 case VAR_VOID: 5909 internal_error_no_abort("item_copy(UNKNOWN)"); 5910 ret = FAIL; 5911 } 5912 --recurse; 5913 return ret; 5914 } 5915 5916 void 5917 echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr) 5918 { 5919 char_u *tofree; 5920 char_u numbuf[NUMBUFLEN]; 5921 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID()); 5922 5923 if (*atstart) 5924 { 5925 *atstart = FALSE; 5926 // Call msg_start() after eval1(), evaluating the expression 5927 // may cause a message to appear. 5928 if (with_space) 5929 { 5930 // Mark the saved text as finishing the line, so that what 5931 // follows is displayed on a new line when scrolling back 5932 // at the more prompt. 5933 msg_sb_eol(); 5934 msg_start(); 5935 } 5936 } 5937 else if (with_space) 5938 msg_puts_attr(" ", echo_attr); 5939 5940 if (p != NULL) 5941 for ( ; *p != NUL && !got_int; ++p) 5942 { 5943 if (*p == '\n' || *p == '\r' || *p == TAB) 5944 { 5945 if (*p != TAB && *needclr) 5946 { 5947 // remove any text still there from the command 5948 msg_clr_eos(); 5949 *needclr = FALSE; 5950 } 5951 msg_putchar_attr(*p, echo_attr); 5952 } 5953 else 5954 { 5955 if (has_mbyte) 5956 { 5957 int i = (*mb_ptr2len)(p); 5958 5959 (void)msg_outtrans_len_attr(p, i, echo_attr); 5960 p += i - 1; 5961 } 5962 else 5963 (void)msg_outtrans_len_attr(p, 1, echo_attr); 5964 } 5965 } 5966 vim_free(tofree); 5967 } 5968 5969 /* 5970 * ":echo expr1 ..." print each argument separated with a space, add a 5971 * newline at the end. 5972 * ":echon expr1 ..." print each argument plain. 5973 */ 5974 void 5975 ex_echo(exarg_T *eap) 5976 { 5977 char_u *arg = eap->arg; 5978 typval_T rettv; 5979 char_u *p; 5980 int needclr = TRUE; 5981 int atstart = TRUE; 5982 int did_emsg_before = did_emsg; 5983 int called_emsg_before = called_emsg; 5984 evalarg_T evalarg; 5985 5986 fill_evalarg_from_eap(&evalarg, eap, eap->skip); 5987 5988 if (eap->skip) 5989 ++emsg_skip; 5990 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int) 5991 { 5992 // If eval1() causes an error message the text from the command may 5993 // still need to be cleared. E.g., "echo 22,44". 5994 need_clr_eos = needclr; 5995 5996 p = arg; 5997 if (eval1(&arg, &rettv, &evalarg) == FAIL) 5998 { 5999 /* 6000 * Report the invalid expression unless the expression evaluation 6001 * has been cancelled due to an aborting error, an interrupt, or an 6002 * exception. 6003 */ 6004 if (!aborting() && did_emsg == did_emsg_before 6005 && called_emsg == called_emsg_before) 6006 semsg(_(e_invexpr2), p); 6007 need_clr_eos = FALSE; 6008 break; 6009 } 6010 need_clr_eos = FALSE; 6011 6012 if (!eap->skip) 6013 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr); 6014 6015 clear_tv(&rettv); 6016 arg = skipwhite(arg); 6017 } 6018 eap->nextcmd = check_nextcmd(arg); 6019 clear_evalarg(&evalarg, eap); 6020 6021 if (eap->skip) 6022 --emsg_skip; 6023 else 6024 { 6025 // remove text that may still be there from the command 6026 if (needclr) 6027 msg_clr_eos(); 6028 if (eap->cmdidx == CMD_echo) 6029 msg_end(); 6030 } 6031 } 6032 6033 /* 6034 * ":echohl {name}". 6035 */ 6036 void 6037 ex_echohl(exarg_T *eap) 6038 { 6039 echo_attr = syn_name2attr(eap->arg); 6040 } 6041 6042 /* 6043 * Returns the :echo attribute 6044 */ 6045 int 6046 get_echo_attr(void) 6047 { 6048 return echo_attr; 6049 } 6050 6051 /* 6052 * ":execute expr1 ..." execute the result of an expression. 6053 * ":echomsg expr1 ..." Print a message 6054 * ":echoerr expr1 ..." Print an error 6055 * Each gets spaces around each argument and a newline at the end for 6056 * echo commands 6057 */ 6058 void 6059 ex_execute(exarg_T *eap) 6060 { 6061 char_u *arg = eap->arg; 6062 typval_T rettv; 6063 int ret = OK; 6064 char_u *p; 6065 garray_T ga; 6066 int len; 6067 6068 ga_init2(&ga, 1, 80); 6069 6070 if (eap->skip) 6071 ++emsg_skip; 6072 while (!ends_excmd2(eap->cmd, arg) || *arg == '"') 6073 { 6074 ret = eval1_emsg(&arg, &rettv, eap); 6075 if (ret == FAIL) 6076 break; 6077 6078 if (!eap->skip) 6079 { 6080 char_u buf[NUMBUFLEN]; 6081 6082 if (eap->cmdidx == CMD_execute) 6083 { 6084 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB) 6085 { 6086 emsg(_(e_inval_string)); 6087 p = NULL; 6088 } 6089 else 6090 p = tv_get_string_buf(&rettv, buf); 6091 } 6092 else 6093 p = tv_stringify(&rettv, buf); 6094 if (p == NULL) 6095 { 6096 clear_tv(&rettv); 6097 ret = FAIL; 6098 break; 6099 } 6100 len = (int)STRLEN(p); 6101 if (ga_grow(&ga, len + 2) == FAIL) 6102 { 6103 clear_tv(&rettv); 6104 ret = FAIL; 6105 break; 6106 } 6107 if (ga.ga_len) 6108 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' '; 6109 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p); 6110 ga.ga_len += len; 6111 } 6112 6113 clear_tv(&rettv); 6114 arg = skipwhite(arg); 6115 } 6116 6117 if (ret != FAIL && ga.ga_data != NULL) 6118 { 6119 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr) 6120 { 6121 // Mark the already saved text as finishing the line, so that what 6122 // follows is displayed on a new line when scrolling back at the 6123 // more prompt. 6124 msg_sb_eol(); 6125 } 6126 6127 if (eap->cmdidx == CMD_echomsg) 6128 { 6129 msg_attr(ga.ga_data, echo_attr); 6130 out_flush(); 6131 } 6132 else if (eap->cmdidx == CMD_echoerr) 6133 { 6134 int save_did_emsg = did_emsg; 6135 6136 // We don't want to abort following commands, restore did_emsg. 6137 emsg(ga.ga_data); 6138 if (!force_abort) 6139 did_emsg = save_did_emsg; 6140 } 6141 else if (eap->cmdidx == CMD_execute) 6142 do_cmdline((char_u *)ga.ga_data, 6143 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE); 6144 } 6145 6146 ga_clear(&ga); 6147 6148 if (eap->skip) 6149 --emsg_skip; 6150 6151 eap->nextcmd = check_nextcmd(arg); 6152 } 6153 6154 /* 6155 * Skip over the name of an option: "&option", "&g:option" or "&l:option". 6156 * "arg" points to the "&" or '+' when called, to "option" when returning. 6157 * Returns NULL when no option name found. Otherwise pointer to the char 6158 * after the option name. 6159 */ 6160 char_u * 6161 find_option_end(char_u **arg, int *opt_flags) 6162 { 6163 char_u *p = *arg; 6164 6165 ++p; 6166 if (*p == 'g' && p[1] == ':') 6167 { 6168 *opt_flags = OPT_GLOBAL; 6169 p += 2; 6170 } 6171 else if (*p == 'l' && p[1] == ':') 6172 { 6173 *opt_flags = OPT_LOCAL; 6174 p += 2; 6175 } 6176 else 6177 *opt_flags = 0; 6178 6179 if (!ASCII_ISALPHA(*p)) 6180 return NULL; 6181 *arg = p; 6182 6183 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL) 6184 p += 4; // termcap option 6185 else 6186 while (ASCII_ISALPHA(*p)) 6187 ++p; 6188 return p; 6189 } 6190 6191 /* 6192 * Display script name where an item was last set. 6193 * Should only be invoked when 'verbose' is non-zero. 6194 */ 6195 void 6196 last_set_msg(sctx_T script_ctx) 6197 { 6198 char_u *p; 6199 6200 if (script_ctx.sc_sid != 0) 6201 { 6202 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid)); 6203 if (p != NULL) 6204 { 6205 verbose_enter(); 6206 msg_puts(_("\n\tLast set from ")); 6207 msg_puts((char *)p); 6208 if (script_ctx.sc_lnum > 0) 6209 { 6210 msg_puts(_(line_msg)); 6211 msg_outnum((long)script_ctx.sc_lnum); 6212 } 6213 verbose_leave(); 6214 vim_free(p); 6215 } 6216 } 6217 } 6218 6219 #endif // FEAT_EVAL 6220 6221 /* 6222 * Perform a substitution on "str" with pattern "pat" and substitute "sub". 6223 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL. 6224 * "flags" can be "g" to do a global substitute. 6225 * Returns an allocated string, NULL for error. 6226 */ 6227 char_u * 6228 do_string_sub( 6229 char_u *str, 6230 char_u *pat, 6231 char_u *sub, 6232 typval_T *expr, 6233 char_u *flags) 6234 { 6235 int sublen; 6236 regmatch_T regmatch; 6237 int i; 6238 int do_all; 6239 char_u *tail; 6240 char_u *end; 6241 garray_T ga; 6242 char_u *ret; 6243 char_u *save_cpo; 6244 char_u *zero_width = NULL; 6245 6246 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here 6247 save_cpo = p_cpo; 6248 p_cpo = empty_option; 6249 6250 ga_init2(&ga, 1, 200); 6251 6252 do_all = (flags[0] == 'g'); 6253 6254 regmatch.rm_ic = p_ic; 6255 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING); 6256 if (regmatch.regprog != NULL) 6257 { 6258 tail = str; 6259 end = str + STRLEN(str); 6260 while (vim_regexec_nl(®match, str, (colnr_T)(tail - str))) 6261 { 6262 // Skip empty match except for first match. 6263 if (regmatch.startp[0] == regmatch.endp[0]) 6264 { 6265 if (zero_width == regmatch.startp[0]) 6266 { 6267 // avoid getting stuck on a match with an empty string 6268 i = mb_ptr2len(tail); 6269 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, 6270 (size_t)i); 6271 ga.ga_len += i; 6272 tail += i; 6273 continue; 6274 } 6275 zero_width = regmatch.startp[0]; 6276 } 6277 6278 /* 6279 * Get some space for a temporary buffer to do the substitution 6280 * into. It will contain: 6281 * - The text up to where the match is. 6282 * - The substituted text. 6283 * - The text after the match. 6284 */ 6285 sublen = vim_regsub(®match, sub, expr, tail, FALSE, TRUE, FALSE); 6286 if (ga_grow(&ga, (int)((end - tail) + sublen - 6287 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL) 6288 { 6289 ga_clear(&ga); 6290 break; 6291 } 6292 6293 // copy the text up to where the match is 6294 i = (int)(regmatch.startp[0] - tail); 6295 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i); 6296 // add the substituted text 6297 (void)vim_regsub(®match, sub, expr, (char_u *)ga.ga_data 6298 + ga.ga_len + i, TRUE, TRUE, FALSE); 6299 ga.ga_len += i + sublen - 1; 6300 tail = regmatch.endp[0]; 6301 if (*tail == NUL) 6302 break; 6303 if (!do_all) 6304 break; 6305 } 6306 6307 if (ga.ga_data != NULL) 6308 STRCPY((char *)ga.ga_data + ga.ga_len, tail); 6309 6310 vim_regfree(regmatch.regprog); 6311 } 6312 6313 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data); 6314 ga_clear(&ga); 6315 if (p_cpo == empty_option) 6316 p_cpo = save_cpo; 6317 else 6318 { 6319 // Darn, evaluating {sub} expression or {expr} changed the value. 6320 // If it's still empty it was changed and restored, need to restore in 6321 // the complicated way. 6322 if (*p_cpo == NUL) 6323 set_option_value((char_u *)"cpo", 0L, save_cpo, 0); 6324 free_string_option(save_cpo); 6325 } 6326 6327 return ret; 6328 } 6329