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