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