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 = current_sctx.sc_version == SCRIPT_VERSION_VIM9; 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 emsg(_(e_trailing)); 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 (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && *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 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags); 1274 *endp = cc; 1275 } 1276 else if (var_check_lock(lp->ll_newkey == NULL 1277 ? lp->ll_tv->v_lock 1278 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE)) 1279 ; 1280 else if (lp->ll_range) 1281 { 1282 listitem_T *ll_li = lp->ll_li; 1283 int ll_n1 = lp->ll_n1; 1284 1285 if (flags & LET_IS_CONST) 1286 { 1287 emsg(_("E996: Cannot lock a range")); 1288 return; 1289 } 1290 1291 /* 1292 * Check whether any of the list items is locked 1293 */ 1294 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; ) 1295 { 1296 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE)) 1297 return; 1298 ri = ri->li_next; 1299 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1)) 1300 break; 1301 ll_li = ll_li->li_next; 1302 ++ll_n1; 1303 } 1304 1305 /* 1306 * Assign the List values to the list items. 1307 */ 1308 for (ri = rettv->vval.v_list->lv_first; ri != NULL; ) 1309 { 1310 if (op != NULL && *op != '=') 1311 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op); 1312 else 1313 { 1314 clear_tv(&lp->ll_li->li_tv); 1315 copy_tv(&ri->li_tv, &lp->ll_li->li_tv); 1316 } 1317 ri = ri->li_next; 1318 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1)) 1319 break; 1320 if (lp->ll_li->li_next == NULL) 1321 { 1322 // Need to add an empty item. 1323 if (list_append_number(lp->ll_list, 0) == FAIL) 1324 { 1325 ri = NULL; 1326 break; 1327 } 1328 } 1329 lp->ll_li = lp->ll_li->li_next; 1330 ++lp->ll_n1; 1331 } 1332 if (ri != NULL) 1333 emsg(_("E710: List value has more items than target")); 1334 else if (lp->ll_empty2 1335 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL) 1336 : lp->ll_n1 != lp->ll_n2) 1337 emsg(_("E711: List value has not enough items")); 1338 } 1339 else 1340 { 1341 /* 1342 * Assign to a List or Dictionary item. 1343 */ 1344 if (flags & LET_IS_CONST) 1345 { 1346 emsg(_("E996: Cannot lock a list or dict")); 1347 return; 1348 } 1349 if (lp->ll_newkey != NULL) 1350 { 1351 if (op != NULL && *op != '=') 1352 { 1353 semsg(_(e_letwrong), op); 1354 return; 1355 } 1356 1357 // Need to add an item to the Dictionary. 1358 di = dictitem_alloc(lp->ll_newkey); 1359 if (di == NULL) 1360 return; 1361 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL) 1362 { 1363 vim_free(di); 1364 return; 1365 } 1366 lp->ll_tv = &di->di_tv; 1367 } 1368 else if (op != NULL && *op != '=') 1369 { 1370 tv_op(lp->ll_tv, rettv, op); 1371 return; 1372 } 1373 else 1374 clear_tv(lp->ll_tv); 1375 1376 /* 1377 * Assign the value to the variable or list item. 1378 */ 1379 if (copy) 1380 copy_tv(rettv, lp->ll_tv); 1381 else 1382 { 1383 *lp->ll_tv = *rettv; 1384 lp->ll_tv->v_lock = 0; 1385 init_tv(rettv); 1386 } 1387 } 1388 } 1389 1390 /* 1391 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2" 1392 * and "tv1 .= tv2" 1393 * Returns OK or FAIL. 1394 */ 1395 static int 1396 tv_op(typval_T *tv1, typval_T *tv2, char_u *op) 1397 { 1398 varnumber_T n; 1399 char_u numbuf[NUMBUFLEN]; 1400 char_u *s; 1401 1402 // Can't do anything with a Funcref, Dict, v:true on the right. 1403 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT 1404 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL) 1405 { 1406 switch (tv1->v_type) 1407 { 1408 case VAR_UNKNOWN: 1409 case VAR_ANY: 1410 case VAR_VOID: 1411 case VAR_DICT: 1412 case VAR_FUNC: 1413 case VAR_PARTIAL: 1414 case VAR_BOOL: 1415 case VAR_SPECIAL: 1416 case VAR_JOB: 1417 case VAR_CHANNEL: 1418 break; 1419 1420 case VAR_BLOB: 1421 if (*op != '+' || tv2->v_type != VAR_BLOB) 1422 break; 1423 // BLOB += BLOB 1424 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL) 1425 { 1426 blob_T *b1 = tv1->vval.v_blob; 1427 blob_T *b2 = tv2->vval.v_blob; 1428 int i, len = blob_len(b2); 1429 for (i = 0; i < len; i++) 1430 ga_append(&b1->bv_ga, blob_get(b2, i)); 1431 } 1432 return OK; 1433 1434 case VAR_LIST: 1435 if (*op != '+' || tv2->v_type != VAR_LIST) 1436 break; 1437 // List += List 1438 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL) 1439 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL); 1440 return OK; 1441 1442 case VAR_NUMBER: 1443 case VAR_STRING: 1444 if (tv2->v_type == VAR_LIST) 1445 break; 1446 if (vim_strchr((char_u *)"+-*/%", *op) != NULL) 1447 { 1448 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr 1449 n = tv_get_number(tv1); 1450 #ifdef FEAT_FLOAT 1451 if (tv2->v_type == VAR_FLOAT) 1452 { 1453 float_T f = n; 1454 1455 if (*op == '%') 1456 break; 1457 switch (*op) 1458 { 1459 case '+': f += tv2->vval.v_float; break; 1460 case '-': f -= tv2->vval.v_float; break; 1461 case '*': f *= tv2->vval.v_float; break; 1462 case '/': f /= tv2->vval.v_float; break; 1463 } 1464 clear_tv(tv1); 1465 tv1->v_type = VAR_FLOAT; 1466 tv1->vval.v_float = f; 1467 } 1468 else 1469 #endif 1470 { 1471 switch (*op) 1472 { 1473 case '+': n += tv_get_number(tv2); break; 1474 case '-': n -= tv_get_number(tv2); break; 1475 case '*': n *= tv_get_number(tv2); break; 1476 case '/': n = num_divide(n, tv_get_number(tv2)); break; 1477 case '%': n = num_modulus(n, tv_get_number(tv2)); break; 1478 } 1479 clear_tv(tv1); 1480 tv1->v_type = VAR_NUMBER; 1481 tv1->vval.v_number = n; 1482 } 1483 } 1484 else 1485 { 1486 if (tv2->v_type == VAR_FLOAT) 1487 break; 1488 1489 // str .= str 1490 s = tv_get_string(tv1); 1491 s = concat_str(s, tv_get_string_buf(tv2, numbuf)); 1492 clear_tv(tv1); 1493 tv1->v_type = VAR_STRING; 1494 tv1->vval.v_string = s; 1495 } 1496 return OK; 1497 1498 case VAR_FLOAT: 1499 #ifdef FEAT_FLOAT 1500 { 1501 float_T f; 1502 1503 if (*op == '%' || *op == '.' 1504 || (tv2->v_type != VAR_FLOAT 1505 && tv2->v_type != VAR_NUMBER 1506 && tv2->v_type != VAR_STRING)) 1507 break; 1508 if (tv2->v_type == VAR_FLOAT) 1509 f = tv2->vval.v_float; 1510 else 1511 f = tv_get_number(tv2); 1512 switch (*op) 1513 { 1514 case '+': tv1->vval.v_float += f; break; 1515 case '-': tv1->vval.v_float -= f; break; 1516 case '*': tv1->vval.v_float *= f; break; 1517 case '/': tv1->vval.v_float /= f; break; 1518 } 1519 } 1520 #endif 1521 return OK; 1522 } 1523 } 1524 1525 semsg(_(e_letwrong), op); 1526 return FAIL; 1527 } 1528 1529 /* 1530 * Evaluate the expression used in a ":for var in expr" command. 1531 * "arg" points to "var". 1532 * Set "*errp" to TRUE for an error, FALSE otherwise; 1533 * Return a pointer that holds the info. Null when there is an error. 1534 */ 1535 void * 1536 eval_for_line( 1537 char_u *arg, 1538 int *errp, 1539 exarg_T *eap, 1540 evalarg_T *evalarg) 1541 { 1542 forinfo_T *fi; 1543 char_u *expr; 1544 typval_T tv; 1545 list_T *l; 1546 int skip = !(evalarg->eval_flags & EVAL_EVALUATE); 1547 1548 *errp = TRUE; // default: there is an error 1549 1550 fi = ALLOC_CLEAR_ONE(forinfo_T); 1551 if (fi == NULL) 1552 return NULL; 1553 1554 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE); 1555 if (expr == NULL) 1556 return fi; 1557 1558 expr = skipwhite_and_linebreak(expr, evalarg); 1559 if (expr[0] != 'i' || expr[1] != 'n' 1560 || !(expr[2] == NUL || VIM_ISWHITE(expr[2]))) 1561 { 1562 emsg(_(e_missing_in)); 1563 return fi; 1564 } 1565 1566 if (skip) 1567 ++emsg_skip; 1568 expr = skipwhite_and_linebreak(expr + 2, evalarg); 1569 if (eval0(expr, &tv, eap, evalarg) == OK) 1570 { 1571 *errp = FALSE; 1572 if (!skip) 1573 { 1574 if (tv.v_type == VAR_LIST) 1575 { 1576 l = tv.vval.v_list; 1577 if (l == NULL) 1578 { 1579 // a null list is like an empty list: do nothing 1580 clear_tv(&tv); 1581 } 1582 else 1583 { 1584 // Need a real list here. 1585 CHECK_LIST_MATERIALIZE(l); 1586 1587 // No need to increment the refcount, it's already set for 1588 // the list being used in "tv". 1589 fi->fi_list = l; 1590 list_add_watch(l, &fi->fi_lw); 1591 fi->fi_lw.lw_item = l->lv_first; 1592 } 1593 } 1594 else if (tv.v_type == VAR_BLOB) 1595 { 1596 fi->fi_bi = 0; 1597 if (tv.vval.v_blob != NULL) 1598 { 1599 typval_T btv; 1600 1601 // Make a copy, so that the iteration still works when the 1602 // blob is changed. 1603 blob_copy(tv.vval.v_blob, &btv); 1604 fi->fi_blob = btv.vval.v_blob; 1605 } 1606 clear_tv(&tv); 1607 } 1608 else 1609 { 1610 emsg(_(e_listreq)); 1611 clear_tv(&tv); 1612 } 1613 } 1614 } 1615 if (skip) 1616 --emsg_skip; 1617 fi->fi_break_count = evalarg->eval_break_count; 1618 1619 return fi; 1620 } 1621 1622 /* 1623 * Used when looping over a :for line, skip the "in expr" part. 1624 */ 1625 void 1626 skip_for_lines(void *fi_void, evalarg_T *evalarg) 1627 { 1628 forinfo_T *fi = (forinfo_T *)fi_void; 1629 int i; 1630 1631 for (i = 0; i < fi->fi_break_count; ++i) 1632 eval_next_line(evalarg); 1633 } 1634 1635 /* 1636 * Use the first item in a ":for" list. Advance to the next. 1637 * Assign the values to the variable (list). "arg" points to the first one. 1638 * Return TRUE when a valid item was found, FALSE when at end of list or 1639 * something wrong. 1640 */ 1641 int 1642 next_for_item(void *fi_void, char_u *arg) 1643 { 1644 forinfo_T *fi = (forinfo_T *)fi_void; 1645 int result; 1646 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ? 1647 LET_NO_COMMAND : 0; 1648 listitem_T *item; 1649 1650 if (fi->fi_blob != NULL) 1651 { 1652 typval_T tv; 1653 1654 if (fi->fi_bi >= blob_len(fi->fi_blob)) 1655 return FALSE; 1656 tv.v_type = VAR_NUMBER; 1657 tv.v_lock = VAR_FIXED; 1658 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi); 1659 ++fi->fi_bi; 1660 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon, 1661 fi->fi_varcount, flag, NULL) == OK; 1662 } 1663 1664 item = fi->fi_lw.lw_item; 1665 if (item == NULL) 1666 result = FALSE; 1667 else 1668 { 1669 fi->fi_lw.lw_item = item->li_next; 1670 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon, 1671 fi->fi_varcount, flag, NULL) == OK); 1672 } 1673 return result; 1674 } 1675 1676 /* 1677 * Free the structure used to store info used by ":for". 1678 */ 1679 void 1680 free_for_info(void *fi_void) 1681 { 1682 forinfo_T *fi = (forinfo_T *)fi_void; 1683 1684 if (fi != NULL && fi->fi_list != NULL) 1685 { 1686 list_rem_watch(fi->fi_list, &fi->fi_lw); 1687 list_unref(fi->fi_list); 1688 } 1689 if (fi != NULL && fi->fi_blob != NULL) 1690 blob_unref(fi->fi_blob); 1691 vim_free(fi); 1692 } 1693 1694 void 1695 set_context_for_expression( 1696 expand_T *xp, 1697 char_u *arg, 1698 cmdidx_T cmdidx) 1699 { 1700 int got_eq = FALSE; 1701 int c; 1702 char_u *p; 1703 1704 if (cmdidx == CMD_let || cmdidx == CMD_const) 1705 { 1706 xp->xp_context = EXPAND_USER_VARS; 1707 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL) 1708 { 1709 // ":let var1 var2 ...": find last space. 1710 for (p = arg + STRLEN(arg); p >= arg; ) 1711 { 1712 xp->xp_pattern = p; 1713 MB_PTR_BACK(arg, p); 1714 if (VIM_ISWHITE(*p)) 1715 break; 1716 } 1717 return; 1718 } 1719 } 1720 else 1721 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS 1722 : EXPAND_EXPRESSION; 1723 while ((xp->xp_pattern = vim_strpbrk(arg, 1724 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL) 1725 { 1726 c = *xp->xp_pattern; 1727 if (c == '&') 1728 { 1729 c = xp->xp_pattern[1]; 1730 if (c == '&') 1731 { 1732 ++xp->xp_pattern; 1733 xp->xp_context = cmdidx != CMD_let || got_eq 1734 ? EXPAND_EXPRESSION : EXPAND_NOTHING; 1735 } 1736 else if (c != ' ') 1737 { 1738 xp->xp_context = EXPAND_SETTINGS; 1739 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':') 1740 xp->xp_pattern += 2; 1741 1742 } 1743 } 1744 else if (c == '$') 1745 { 1746 // environment variable 1747 xp->xp_context = EXPAND_ENV_VARS; 1748 } 1749 else if (c == '=') 1750 { 1751 got_eq = TRUE; 1752 xp->xp_context = EXPAND_EXPRESSION; 1753 } 1754 else if (c == '#' 1755 && xp->xp_context == EXPAND_EXPRESSION) 1756 { 1757 // Autoload function/variable contains '#'. 1758 break; 1759 } 1760 else if ((c == '<' || c == '#') 1761 && xp->xp_context == EXPAND_FUNCTIONS 1762 && vim_strchr(xp->xp_pattern, '(') == NULL) 1763 { 1764 // Function name can start with "<SNR>" and contain '#'. 1765 break; 1766 } 1767 else if (cmdidx != CMD_let || got_eq) 1768 { 1769 if (c == '"') // string 1770 { 1771 while ((c = *++xp->xp_pattern) != NUL && c != '"') 1772 if (c == '\\' && xp->xp_pattern[1] != NUL) 1773 ++xp->xp_pattern; 1774 xp->xp_context = EXPAND_NOTHING; 1775 } 1776 else if (c == '\'') // literal string 1777 { 1778 // Trick: '' is like stopping and starting a literal string. 1779 while ((c = *++xp->xp_pattern) != NUL && c != '\'') 1780 /* skip */ ; 1781 xp->xp_context = EXPAND_NOTHING; 1782 } 1783 else if (c == '|') 1784 { 1785 if (xp->xp_pattern[1] == '|') 1786 { 1787 ++xp->xp_pattern; 1788 xp->xp_context = EXPAND_EXPRESSION; 1789 } 1790 else 1791 xp->xp_context = EXPAND_COMMANDS; 1792 } 1793 else 1794 xp->xp_context = EXPAND_EXPRESSION; 1795 } 1796 else 1797 // Doesn't look like something valid, expand as an expression 1798 // anyway. 1799 xp->xp_context = EXPAND_EXPRESSION; 1800 arg = xp->xp_pattern; 1801 if (*arg != NUL) 1802 while ((c = *++arg) != NUL && (c == ' ' || c == '\t')) 1803 /* skip */ ; 1804 } 1805 xp->xp_pattern = arg; 1806 } 1807 1808 /* 1809 * Return TRUE if "pat" matches "text". 1810 * Does not use 'cpo' and always uses 'magic'. 1811 */ 1812 int 1813 pattern_match(char_u *pat, char_u *text, int ic) 1814 { 1815 int matches = FALSE; 1816 char_u *save_cpo; 1817 regmatch_T regmatch; 1818 1819 // avoid 'l' flag in 'cpoptions' 1820 save_cpo = p_cpo; 1821 p_cpo = (char_u *)""; 1822 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING); 1823 if (regmatch.regprog != NULL) 1824 { 1825 regmatch.rm_ic = ic; 1826 matches = vim_regexec_nl(®match, text, (colnr_T)0); 1827 vim_regfree(regmatch.regprog); 1828 } 1829 p_cpo = save_cpo; 1830 return matches; 1831 } 1832 1833 /* 1834 * Handle a name followed by "(". Both for just "name(arg)" and for 1835 * "expr->name(arg)". 1836 * Returns OK or FAIL. 1837 */ 1838 static int 1839 eval_func( 1840 char_u **arg, // points to "(", will be advanced 1841 evalarg_T *evalarg, 1842 char_u *name, 1843 int name_len, 1844 typval_T *rettv, 1845 int flags, 1846 typval_T *basetv) // "expr" for "expr->name(arg)" 1847 { 1848 int evaluate = flags & EVAL_EVALUATE; 1849 char_u *s = name; 1850 int len = name_len; 1851 partial_T *partial; 1852 int ret = OK; 1853 1854 if (!evaluate) 1855 check_vars(s, len); 1856 1857 // If "s" is the name of a variable of type VAR_FUNC 1858 // use its contents. 1859 s = deref_func_name(s, &len, &partial, !evaluate); 1860 1861 // Need to make a copy, in case evaluating the arguments makes 1862 // the name invalid. 1863 s = vim_strsave(s); 1864 if (s == NULL || (flags & EVAL_CONSTANT)) 1865 ret = FAIL; 1866 else 1867 { 1868 funcexe_T funcexe; 1869 1870 // Invoke the function. 1871 CLEAR_FIELD(funcexe); 1872 funcexe.firstline = curwin->w_cursor.lnum; 1873 funcexe.lastline = curwin->w_cursor.lnum; 1874 funcexe.evaluate = evaluate; 1875 funcexe.partial = partial; 1876 funcexe.basetv = basetv; 1877 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe); 1878 } 1879 vim_free(s); 1880 1881 // If evaluate is FALSE rettv->v_type was not set in 1882 // get_func_tv, but it's needed in handle_subscript() to parse 1883 // what follows. So set it here. 1884 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(') 1885 { 1886 rettv->vval.v_string = NULL; 1887 rettv->v_type = VAR_FUNC; 1888 } 1889 1890 // Stop the expression evaluation when immediately 1891 // aborting on error, or when an interrupt occurred or 1892 // an exception was thrown but not caught. 1893 if (evaluate && aborting()) 1894 { 1895 if (ret == OK) 1896 clear_tv(rettv); 1897 ret = FAIL; 1898 } 1899 return ret; 1900 } 1901 1902 /* 1903 * If inside Vim9 script, "arg" points to the end of a line (ignoring a # 1904 * comment) and there is a next line, return the next line (skipping blanks) 1905 * and set "getnext". 1906 * Otherwise just return "arg" unmodified and set "getnext" to FALSE. 1907 * "arg" must point somewhere inside a line, not at the start. 1908 */ 1909 char_u * 1910 eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext) 1911 { 1912 *getnext = FALSE; 1913 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 1914 && evalarg != NULL 1915 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL) 1916 && (*arg == NUL || (VIM_ISWHITE(arg[-1]) 1917 && *arg == '#' && arg[1] != '{'))) 1918 { 1919 char_u *p; 1920 1921 if (evalarg->eval_cookie != NULL) 1922 p = getline_peek(evalarg->eval_getline, evalarg->eval_cookie); 1923 else 1924 p = peek_next_line_from_context(evalarg->eval_cctx); 1925 1926 if (p != NULL) 1927 { 1928 *getnext = TRUE; 1929 return skipwhite(p); 1930 } 1931 } 1932 return arg; 1933 } 1934 1935 /* 1936 * To be called after eval_next_non_blank() sets "getnext" to TRUE. 1937 */ 1938 char_u * 1939 eval_next_line(evalarg_T *evalarg) 1940 { 1941 garray_T *gap = &evalarg->eval_ga; 1942 char_u *line; 1943 1944 if (evalarg->eval_cookie != NULL) 1945 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0, TRUE); 1946 else 1947 line = next_line_from_context(evalarg->eval_cctx, TRUE); 1948 ++evalarg->eval_break_count; 1949 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK) 1950 { 1951 // Going to concatenate the lines after parsing. 1952 ((char_u **)gap->ga_data)[gap->ga_len] = line; 1953 ++gap->ga_len; 1954 } 1955 else if (evalarg->eval_cookie != NULL) 1956 { 1957 vim_free(evalarg->eval_tofree); 1958 evalarg->eval_tofree = line; 1959 } 1960 return skipwhite(line); 1961 } 1962 1963 /* 1964 * Call eval_next_non_blank() and get the next line if needed. 1965 */ 1966 char_u * 1967 skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg) 1968 { 1969 int getnext; 1970 char_u *p = skipwhite(arg); 1971 1972 if (evalarg == NULL) 1973 return skipwhite(arg); 1974 eval_next_non_blank(p, evalarg, &getnext); 1975 if (getnext) 1976 return eval_next_line(evalarg); 1977 return p; 1978 } 1979 1980 /* 1981 * After using "evalarg" filled from "eap": free the memory. 1982 */ 1983 void 1984 clear_evalarg(evalarg_T *evalarg, exarg_T *eap) 1985 { 1986 if (evalarg != NULL) 1987 { 1988 if (evalarg->eval_tofree != NULL) 1989 { 1990 if (eap != NULL) 1991 { 1992 // We may need to keep the original command line, e.g. for 1993 // ":let" it has the variable names. But we may also need the 1994 // new one, "nextcmd" points into it. Keep both. 1995 vim_free(eap->cmdline_tofree); 1996 eap->cmdline_tofree = *eap->cmdlinep; 1997 *eap->cmdlinep = evalarg->eval_tofree; 1998 } 1999 else 2000 vim_free(evalarg->eval_tofree); 2001 evalarg->eval_tofree = NULL; 2002 } 2003 2004 vim_free(evalarg->eval_tofree_lambda); 2005 evalarg->eval_tofree_lambda = NULL; 2006 } 2007 } 2008 2009 /* 2010 * The "evaluate" argument: When FALSE, the argument is only parsed but not 2011 * executed. The function may return OK, but the rettv will be of type 2012 * VAR_UNKNOWN. The function still returns FAIL for a syntax error. 2013 */ 2014 2015 /* 2016 * Handle zero level expression. 2017 * This calls eval1() and handles error message and nextcmd. 2018 * Put the result in "rettv" when returning OK and "evaluate" is TRUE. 2019 * Note: "rettv.v_lock" is not set. 2020 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer. 2021 * Return OK or FAIL. 2022 */ 2023 int 2024 eval0( 2025 char_u *arg, 2026 typval_T *rettv, 2027 exarg_T *eap, 2028 evalarg_T *evalarg) 2029 { 2030 int ret; 2031 char_u *p; 2032 int did_emsg_before = did_emsg; 2033 int called_emsg_before = called_emsg; 2034 int flags = evalarg == NULL ? 0 : evalarg->eval_flags; 2035 2036 p = skipwhite(arg); 2037 ret = eval1(&p, rettv, evalarg); 2038 2039 if (ret == FAIL || !ends_excmd2(arg, p)) 2040 { 2041 if (ret != FAIL) 2042 clear_tv(rettv); 2043 /* 2044 * Report the invalid expression unless the expression evaluation has 2045 * been cancelled due to an aborting error, an interrupt, or an 2046 * exception, or we already gave a more specific error. 2047 * Also check called_emsg for when using assert_fails(). 2048 */ 2049 if (!aborting() 2050 && did_emsg == did_emsg_before 2051 && called_emsg == called_emsg_before 2052 && (flags & EVAL_CONSTANT) == 0) 2053 semsg(_(e_invexpr2), arg); 2054 ret = FAIL; 2055 } 2056 2057 if (eap != NULL) 2058 eap->nextcmd = check_nextcmd(p); 2059 2060 return ret; 2061 } 2062 2063 /* 2064 * Handle top level expression: 2065 * expr2 ? expr1 : expr1 2066 * 2067 * "arg" must point to the first non-white of the expression. 2068 * "arg" is advanced to the next non-white after the recognized expression. 2069 * 2070 * Note: "rettv.v_lock" is not set. 2071 * 2072 * Return OK or FAIL. 2073 */ 2074 int 2075 eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg) 2076 { 2077 char_u *p; 2078 int getnext; 2079 2080 /* 2081 * Get the first variable. 2082 */ 2083 if (eval2(arg, rettv, evalarg) == FAIL) 2084 return FAIL; 2085 2086 p = eval_next_non_blank(*arg, evalarg, &getnext); 2087 if (*p == '?') 2088 { 2089 int result; 2090 typval_T var2; 2091 evalarg_T nested_evalarg; 2092 int orig_flags; 2093 int evaluate; 2094 2095 if (getnext) 2096 *arg = eval_next_line(evalarg); 2097 2098 if (evalarg == NULL) 2099 { 2100 CLEAR_FIELD(nested_evalarg); 2101 orig_flags = 0; 2102 } 2103 else 2104 { 2105 nested_evalarg = *evalarg; 2106 orig_flags = evalarg->eval_flags; 2107 } 2108 2109 evaluate = nested_evalarg.eval_flags & EVAL_EVALUATE; 2110 result = FALSE; 2111 if (evaluate) 2112 { 2113 int error = FALSE; 2114 2115 if (tv_get_number_chk(rettv, &error) != 0) 2116 result = TRUE; 2117 clear_tv(rettv); 2118 if (error) 2119 return FAIL; 2120 } 2121 2122 /* 2123 * Get the second variable. Recursive! 2124 */ 2125 *arg = skipwhite_and_linebreak(*arg + 1, evalarg); 2126 nested_evalarg.eval_flags = result ? orig_flags 2127 : orig_flags & ~EVAL_EVALUATE; 2128 if (eval1(arg, rettv, &nested_evalarg) == FAIL) 2129 return FAIL; 2130 2131 /* 2132 * Check for the ":". 2133 */ 2134 p = eval_next_non_blank(*arg, evalarg, &getnext); 2135 if (*p != ':') 2136 { 2137 emsg(_(e_missing_colon)); 2138 if (evaluate && result) 2139 clear_tv(rettv); 2140 return FAIL; 2141 } 2142 if (getnext) 2143 *arg = eval_next_line(evalarg); 2144 2145 /* 2146 * Get the third variable. Recursive! 2147 */ 2148 *arg = skipwhite_and_linebreak(*arg + 1, evalarg); 2149 nested_evalarg.eval_flags = !result ? orig_flags 2150 : orig_flags & ~EVAL_EVALUATE; 2151 if (eval1(arg, &var2, &nested_evalarg) == FAIL) 2152 { 2153 if (evaluate && result) 2154 clear_tv(rettv); 2155 return FAIL; 2156 } 2157 if (evaluate && !result) 2158 *rettv = var2; 2159 } 2160 2161 return OK; 2162 } 2163 2164 /* 2165 * Handle first level expression: 2166 * expr2 || expr2 || expr2 logical OR 2167 * 2168 * "arg" must point to the first non-white of the expression. 2169 * "arg" is advanced to the next non-white after the recognized expression. 2170 * 2171 * Return OK or FAIL. 2172 */ 2173 static int 2174 eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg) 2175 { 2176 char_u *p; 2177 int getnext; 2178 typval_T var2; 2179 long result; 2180 int first; 2181 int error = FALSE; 2182 2183 /* 2184 * Get the first variable. 2185 */ 2186 if (eval3(arg, rettv, evalarg) == FAIL) 2187 return FAIL; 2188 2189 /* 2190 * Repeat until there is no following "||". 2191 */ 2192 first = TRUE; 2193 result = FALSE; 2194 p = eval_next_non_blank(*arg, evalarg, &getnext); 2195 while (p[0] == '|' && p[1] == '|') 2196 { 2197 evalarg_T nested_evalarg; 2198 int evaluate; 2199 int orig_flags; 2200 2201 if (getnext) 2202 *arg = eval_next_line(evalarg); 2203 2204 if (evalarg == NULL) 2205 { 2206 CLEAR_FIELD(nested_evalarg); 2207 orig_flags = 0; 2208 evaluate = FALSE; 2209 } 2210 else 2211 { 2212 nested_evalarg = *evalarg; 2213 orig_flags = evalarg->eval_flags; 2214 evaluate = orig_flags & EVAL_EVALUATE; 2215 } 2216 2217 if (evaluate && first) 2218 { 2219 if (tv_get_number_chk(rettv, &error) != 0) 2220 result = TRUE; 2221 clear_tv(rettv); 2222 if (error) 2223 return FAIL; 2224 first = FALSE; 2225 } 2226 2227 /* 2228 * Get the second variable. 2229 */ 2230 *arg = skipwhite_and_linebreak(*arg + 2, evalarg); 2231 nested_evalarg.eval_flags = !result ? orig_flags 2232 : orig_flags & ~EVAL_EVALUATE; 2233 if (eval3(arg, &var2, &nested_evalarg) == FAIL) 2234 return FAIL; 2235 2236 /* 2237 * Compute the result. 2238 */ 2239 if (evaluate && !result) 2240 { 2241 if (tv_get_number_chk(&var2, &error) != 0) 2242 result = TRUE; 2243 clear_tv(&var2); 2244 if (error) 2245 return FAIL; 2246 } 2247 if (evaluate) 2248 { 2249 rettv->v_type = VAR_NUMBER; 2250 rettv->vval.v_number = result; 2251 } 2252 2253 p = eval_next_non_blank(*arg, evalarg, &getnext); 2254 } 2255 2256 return OK; 2257 } 2258 2259 /* 2260 * Handle second level expression: 2261 * expr3 && expr3 && expr3 logical AND 2262 * 2263 * "arg" must point to the first non-white of the expression. 2264 * "arg" is advanced to the next non-white after the recognized expression. 2265 * 2266 * Return OK or FAIL. 2267 */ 2268 static int 2269 eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg) 2270 { 2271 char_u *p; 2272 int getnext; 2273 typval_T var2; 2274 long result; 2275 int first; 2276 int error = FALSE; 2277 2278 /* 2279 * Get the first variable. 2280 */ 2281 if (eval4(arg, rettv, evalarg) == FAIL) 2282 return FAIL; 2283 2284 /* 2285 * Repeat until there is no following "&&". 2286 */ 2287 first = TRUE; 2288 result = TRUE; 2289 p = eval_next_non_blank(*arg, evalarg, &getnext); 2290 while (p[0] == '&' && p[1] == '&') 2291 { 2292 evalarg_T nested_evalarg; 2293 int orig_flags; 2294 int evaluate; 2295 2296 if (getnext) 2297 *arg = eval_next_line(evalarg); 2298 2299 if (evalarg == NULL) 2300 { 2301 CLEAR_FIELD(nested_evalarg); 2302 orig_flags = 0; 2303 evaluate = FALSE; 2304 } 2305 else 2306 { 2307 nested_evalarg = *evalarg; 2308 orig_flags = evalarg->eval_flags; 2309 evaluate = orig_flags & EVAL_EVALUATE; 2310 } 2311 if (evaluate && first) 2312 { 2313 if (tv_get_number_chk(rettv, &error) == 0) 2314 result = FALSE; 2315 clear_tv(rettv); 2316 if (error) 2317 return FAIL; 2318 first = FALSE; 2319 } 2320 2321 /* 2322 * Get the second variable. 2323 */ 2324 *arg = skipwhite_and_linebreak(*arg + 2, evalarg); 2325 nested_evalarg.eval_flags = result ? orig_flags 2326 : orig_flags & ~EVAL_EVALUATE; 2327 if (eval4(arg, &var2, &nested_evalarg) == FAIL) 2328 return FAIL; 2329 2330 /* 2331 * Compute the result. 2332 */ 2333 if (evaluate && result) 2334 { 2335 if (tv_get_number_chk(&var2, &error) == 0) 2336 result = FALSE; 2337 clear_tv(&var2); 2338 if (error) 2339 return FAIL; 2340 } 2341 if (evaluate) 2342 { 2343 rettv->v_type = VAR_NUMBER; 2344 rettv->vval.v_number = result; 2345 } 2346 2347 p = eval_next_non_blank(*arg, evalarg, &getnext); 2348 } 2349 2350 return OK; 2351 } 2352 2353 /* 2354 * Handle third level expression: 2355 * var1 == var2 2356 * var1 =~ var2 2357 * var1 != var2 2358 * var1 !~ var2 2359 * var1 > var2 2360 * var1 >= var2 2361 * var1 < var2 2362 * var1 <= var2 2363 * var1 is var2 2364 * var1 isnot var2 2365 * 2366 * "arg" must point to the first non-white of the expression. 2367 * "arg" is advanced to the next non-white after the recognized expression. 2368 * 2369 * Return OK or FAIL. 2370 */ 2371 static int 2372 eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg) 2373 { 2374 typval_T var2; 2375 char_u *p; 2376 int getnext; 2377 int i; 2378 exptype_T type = EXPR_UNKNOWN; 2379 int len = 2; 2380 int ic; 2381 2382 /* 2383 * Get the first variable. 2384 */ 2385 if (eval5(arg, rettv, evalarg) == FAIL) 2386 return FAIL; 2387 2388 p = eval_next_non_blank(*arg, evalarg, &getnext); 2389 switch (p[0]) 2390 { 2391 case '=': if (p[1] == '=') 2392 type = EXPR_EQUAL; 2393 else if (p[1] == '~') 2394 type = EXPR_MATCH; 2395 break; 2396 case '!': if (p[1] == '=') 2397 type = EXPR_NEQUAL; 2398 else if (p[1] == '~') 2399 type = EXPR_NOMATCH; 2400 break; 2401 case '>': if (p[1] != '=') 2402 { 2403 type = EXPR_GREATER; 2404 len = 1; 2405 } 2406 else 2407 type = EXPR_GEQUAL; 2408 break; 2409 case '<': if (p[1] != '=') 2410 { 2411 type = EXPR_SMALLER; 2412 len = 1; 2413 } 2414 else 2415 type = EXPR_SEQUAL; 2416 break; 2417 case 'i': if (p[1] == 's') 2418 { 2419 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') 2420 len = 5; 2421 i = p[len]; 2422 if (!isalnum(i) && i != '_') 2423 type = len == 2 ? EXPR_IS : EXPR_ISNOT; 2424 } 2425 break; 2426 } 2427 2428 /* 2429 * If there is a comparative operator, use it. 2430 */ 2431 if (type != EXPR_UNKNOWN) 2432 { 2433 if (getnext) 2434 *arg = eval_next_line(evalarg); 2435 2436 // extra question mark appended: ignore case 2437 if (p[len] == '?') 2438 { 2439 ic = TRUE; 2440 ++len; 2441 } 2442 // extra '#' appended: match case 2443 else if (p[len] == '#') 2444 { 2445 ic = FALSE; 2446 ++len; 2447 } 2448 // nothing appended: use 'ignorecase' 2449 else 2450 ic = p_ic; 2451 2452 /* 2453 * Get the second variable. 2454 */ 2455 *arg = skipwhite_and_linebreak(p + len, evalarg); 2456 if (eval5(arg, &var2, evalarg) == FAIL) 2457 { 2458 clear_tv(rettv); 2459 return FAIL; 2460 } 2461 if (evalarg != NULL && (evalarg->eval_flags & EVAL_EVALUATE)) 2462 { 2463 int ret = typval_compare(rettv, &var2, type, ic); 2464 2465 clear_tv(&var2); 2466 return ret; 2467 } 2468 } 2469 2470 return OK; 2471 } 2472 2473 void 2474 eval_addblob(typval_T *tv1, typval_T *tv2) 2475 { 2476 blob_T *b1 = tv1->vval.v_blob; 2477 blob_T *b2 = tv2->vval.v_blob; 2478 blob_T *b = blob_alloc(); 2479 int i; 2480 2481 if (b != NULL) 2482 { 2483 for (i = 0; i < blob_len(b1); i++) 2484 ga_append(&b->bv_ga, blob_get(b1, i)); 2485 for (i = 0; i < blob_len(b2); i++) 2486 ga_append(&b->bv_ga, blob_get(b2, i)); 2487 2488 clear_tv(tv1); 2489 rettv_blob_set(tv1, b); 2490 } 2491 } 2492 2493 int 2494 eval_addlist(typval_T *tv1, typval_T *tv2) 2495 { 2496 typval_T var3; 2497 2498 // concatenate Lists 2499 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL) 2500 { 2501 clear_tv(tv1); 2502 clear_tv(tv2); 2503 return FAIL; 2504 } 2505 clear_tv(tv1); 2506 *tv1 = var3; 2507 return OK; 2508 } 2509 2510 /* 2511 * Handle fourth level expression: 2512 * + number addition 2513 * - number subtraction 2514 * . string concatenation (if script version is 1) 2515 * .. string concatenation 2516 * 2517 * "arg" must point to the first non-white of the expression. 2518 * "arg" is advanced to the next non-white after the recognized expression. 2519 * 2520 * Return OK or FAIL. 2521 */ 2522 static int 2523 eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg) 2524 { 2525 int evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE); 2526 2527 /* 2528 * Get the first variable. 2529 */ 2530 if (eval6(arg, rettv, evalarg, FALSE) == FAIL) 2531 return FAIL; 2532 2533 /* 2534 * Repeat computing, until no '+', '-' or '.' is following. 2535 */ 2536 for (;;) 2537 { 2538 int getnext; 2539 char_u *p; 2540 int op; 2541 int concat; 2542 typval_T var2; 2543 2544 // "." is only string concatenation when scriptversion is 1 2545 p = eval_next_non_blank(*arg, evalarg, &getnext); 2546 op = *p; 2547 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2); 2548 if (op != '+' && op != '-' && !concat) 2549 break; 2550 if (getnext) 2551 *arg = eval_next_line(evalarg); 2552 2553 if ((op != '+' || (rettv->v_type != VAR_LIST 2554 && rettv->v_type != VAR_BLOB)) 2555 #ifdef FEAT_FLOAT 2556 && (op == '.' || rettv->v_type != VAR_FLOAT) 2557 #endif 2558 ) 2559 { 2560 // For "list + ...", an illegal use of the first operand as 2561 // a number cannot be determined before evaluating the 2nd 2562 // operand: if this is also a list, all is ok. 2563 // For "something . ...", "something - ..." or "non-list + ...", 2564 // we know that the first operand needs to be a string or number 2565 // without evaluating the 2nd operand. So check before to avoid 2566 // side effects after an error. 2567 if (evaluate && tv_get_string_chk(rettv) == NULL) 2568 { 2569 clear_tv(rettv); 2570 return FAIL; 2571 } 2572 } 2573 2574 /* 2575 * Get the second variable. 2576 */ 2577 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation 2578 ++*arg; 2579 *arg = skipwhite_and_linebreak(*arg + 1, evalarg); 2580 if (eval6(arg, &var2, evalarg, op == '.') == FAIL) 2581 { 2582 clear_tv(rettv); 2583 return FAIL; 2584 } 2585 2586 if (evaluate) 2587 { 2588 /* 2589 * Compute the result. 2590 */ 2591 if (op == '.') 2592 { 2593 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN]; 2594 char_u *s1 = tv_get_string_buf(rettv, buf1); 2595 char_u *s2 = tv_get_string_buf_chk(&var2, buf2); 2596 2597 if (s2 == NULL) // type error ? 2598 { 2599 clear_tv(rettv); 2600 clear_tv(&var2); 2601 return FAIL; 2602 } 2603 p = concat_str(s1, s2); 2604 clear_tv(rettv); 2605 rettv->v_type = VAR_STRING; 2606 rettv->vval.v_string = p; 2607 } 2608 else if (op == '+' && rettv->v_type == VAR_BLOB 2609 && var2.v_type == VAR_BLOB) 2610 eval_addblob(rettv, &var2); 2611 else if (op == '+' && rettv->v_type == VAR_LIST 2612 && var2.v_type == VAR_LIST) 2613 { 2614 if (eval_addlist(rettv, &var2) == FAIL) 2615 return FAIL; 2616 } 2617 else 2618 { 2619 int error = FALSE; 2620 varnumber_T n1, n2; 2621 #ifdef FEAT_FLOAT 2622 float_T f1 = 0, f2 = 0; 2623 2624 if (rettv->v_type == VAR_FLOAT) 2625 { 2626 f1 = rettv->vval.v_float; 2627 n1 = 0; 2628 } 2629 else 2630 #endif 2631 { 2632 n1 = tv_get_number_chk(rettv, &error); 2633 if (error) 2634 { 2635 // This can only happen for "list + non-list". For 2636 // "non-list + ..." or "something - ...", we returned 2637 // before evaluating the 2nd operand. 2638 clear_tv(rettv); 2639 return FAIL; 2640 } 2641 #ifdef FEAT_FLOAT 2642 if (var2.v_type == VAR_FLOAT) 2643 f1 = n1; 2644 #endif 2645 } 2646 #ifdef FEAT_FLOAT 2647 if (var2.v_type == VAR_FLOAT) 2648 { 2649 f2 = var2.vval.v_float; 2650 n2 = 0; 2651 } 2652 else 2653 #endif 2654 { 2655 n2 = tv_get_number_chk(&var2, &error); 2656 if (error) 2657 { 2658 clear_tv(rettv); 2659 clear_tv(&var2); 2660 return FAIL; 2661 } 2662 #ifdef FEAT_FLOAT 2663 if (rettv->v_type == VAR_FLOAT) 2664 f2 = n2; 2665 #endif 2666 } 2667 clear_tv(rettv); 2668 2669 #ifdef FEAT_FLOAT 2670 // If there is a float on either side the result is a float. 2671 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT) 2672 { 2673 if (op == '+') 2674 f1 = f1 + f2; 2675 else 2676 f1 = f1 - f2; 2677 rettv->v_type = VAR_FLOAT; 2678 rettv->vval.v_float = f1; 2679 } 2680 else 2681 #endif 2682 { 2683 if (op == '+') 2684 n1 = n1 + n2; 2685 else 2686 n1 = n1 - n2; 2687 rettv->v_type = VAR_NUMBER; 2688 rettv->vval.v_number = n1; 2689 } 2690 } 2691 clear_tv(&var2); 2692 } 2693 } 2694 return OK; 2695 } 2696 2697 /* 2698 * Handle fifth level expression: 2699 * * number multiplication 2700 * / number division 2701 * % number modulo 2702 * 2703 * "arg" must point to the first non-white of the expression. 2704 * "arg" is advanced to the next non-white after the recognized expression. 2705 * 2706 * Return OK or FAIL. 2707 */ 2708 static int 2709 eval6( 2710 char_u **arg, 2711 typval_T *rettv, 2712 evalarg_T *evalarg, 2713 int want_string) // after "." operator 2714 { 2715 typval_T var2; 2716 int op; 2717 varnumber_T n1, n2; 2718 #ifdef FEAT_FLOAT 2719 int use_float = FALSE; 2720 float_T f1 = 0, f2 = 0; 2721 #endif 2722 int error = FALSE; 2723 2724 /* 2725 * Get the first variable. 2726 */ 2727 if (eval7(arg, rettv, evalarg, want_string) == FAIL) 2728 return FAIL; 2729 2730 /* 2731 * Repeat computing, until no '*', '/' or '%' is following. 2732 */ 2733 for (;;) 2734 { 2735 int evaluate = evalarg == NULL ? 0 2736 : (evalarg->eval_flags & EVAL_EVALUATE); 2737 int getnext; 2738 2739 op = *eval_next_non_blank(*arg, evalarg, &getnext); 2740 if (op != '*' && op != '/' && op != '%') 2741 break; 2742 if (getnext) 2743 *arg = eval_next_line(evalarg); 2744 2745 if (evaluate) 2746 { 2747 #ifdef FEAT_FLOAT 2748 if (rettv->v_type == VAR_FLOAT) 2749 { 2750 f1 = rettv->vval.v_float; 2751 use_float = TRUE; 2752 n1 = 0; 2753 } 2754 else 2755 #endif 2756 n1 = tv_get_number_chk(rettv, &error); 2757 clear_tv(rettv); 2758 if (error) 2759 return FAIL; 2760 } 2761 else 2762 n1 = 0; 2763 2764 /* 2765 * Get the second variable. 2766 */ 2767 *arg = skipwhite(*arg + 1); 2768 if (eval7(arg, &var2, evalarg, FALSE) == FAIL) 2769 return FAIL; 2770 2771 if (evaluate) 2772 { 2773 #ifdef FEAT_FLOAT 2774 if (var2.v_type == VAR_FLOAT) 2775 { 2776 if (!use_float) 2777 { 2778 f1 = n1; 2779 use_float = TRUE; 2780 } 2781 f2 = var2.vval.v_float; 2782 n2 = 0; 2783 } 2784 else 2785 #endif 2786 { 2787 n2 = tv_get_number_chk(&var2, &error); 2788 clear_tv(&var2); 2789 if (error) 2790 return FAIL; 2791 #ifdef FEAT_FLOAT 2792 if (use_float) 2793 f2 = n2; 2794 #endif 2795 } 2796 2797 /* 2798 * Compute the result. 2799 * When either side is a float the result is a float. 2800 */ 2801 #ifdef FEAT_FLOAT 2802 if (use_float) 2803 { 2804 if (op == '*') 2805 f1 = f1 * f2; 2806 else if (op == '/') 2807 { 2808 # ifdef VMS 2809 // VMS crashes on divide by zero, work around it 2810 if (f2 == 0.0) 2811 { 2812 if (f1 == 0) 2813 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN 2814 else if (f1 < 0) 2815 f1 = -1 * __F_FLT_MAX; 2816 else 2817 f1 = __F_FLT_MAX; 2818 } 2819 else 2820 f1 = f1 / f2; 2821 # else 2822 // We rely on the floating point library to handle divide 2823 // by zero to result in "inf" and not a crash. 2824 f1 = f1 / f2; 2825 # endif 2826 } 2827 else 2828 { 2829 emsg(_(e_modulus)); 2830 return FAIL; 2831 } 2832 rettv->v_type = VAR_FLOAT; 2833 rettv->vval.v_float = f1; 2834 } 2835 else 2836 #endif 2837 { 2838 if (op == '*') 2839 n1 = n1 * n2; 2840 else if (op == '/') 2841 n1 = num_divide(n1, n2); 2842 else 2843 n1 = num_modulus(n1, n2); 2844 2845 rettv->v_type = VAR_NUMBER; 2846 rettv->vval.v_number = n1; 2847 } 2848 } 2849 } 2850 2851 return OK; 2852 } 2853 2854 /* 2855 * Handle sixth level expression: 2856 * number number constant 2857 * 0zFFFFFFFF Blob constant 2858 * "string" string constant 2859 * 'string' literal string constant 2860 * &option-name option value 2861 * @r register contents 2862 * identifier variable value 2863 * function() function call 2864 * $VAR environment variable 2865 * (expression) nested expression 2866 * [expr, expr] List 2867 * {arg, arg -> expr} Lambda 2868 * {key: val, key: val} Dictionary 2869 * #{key: val, key: val} Dictionary with literal keys 2870 * 2871 * Also handle: 2872 * ! in front logical NOT 2873 * - in front unary minus 2874 * + in front unary plus (ignored) 2875 * trailing [] subscript in String or List 2876 * trailing .name entry in Dictionary 2877 * trailing ->name() method call 2878 * 2879 * "arg" must point to the first non-white of the expression. 2880 * "arg" is advanced to the next non-white after the recognized expression. 2881 * 2882 * Return OK or FAIL. 2883 */ 2884 static int 2885 eval7( 2886 char_u **arg, 2887 typval_T *rettv, 2888 evalarg_T *evalarg, 2889 int want_string) // after "." operator 2890 { 2891 int flags = evalarg == NULL ? 0 : evalarg->eval_flags; 2892 int evaluate = evalarg != NULL 2893 && (evalarg->eval_flags & EVAL_EVALUATE); 2894 int len; 2895 char_u *s; 2896 char_u *start_leader, *end_leader; 2897 int ret = OK; 2898 char_u *alias; 2899 2900 /* 2901 * Initialise variable so that clear_tv() can't mistake this for a 2902 * string and free a string that isn't there. 2903 */ 2904 rettv->v_type = VAR_UNKNOWN; 2905 2906 /* 2907 * Skip '!', '-' and '+' characters. They are handled later. 2908 */ 2909 start_leader = *arg; 2910 while (**arg == '!' || **arg == '-' || **arg == '+') 2911 *arg = skipwhite(*arg + 1); 2912 end_leader = *arg; 2913 2914 if (**arg == '.' && (!isdigit(*(*arg + 1)) 2915 #ifdef FEAT_FLOAT 2916 || current_sctx.sc_version < 2 2917 #endif 2918 )) 2919 { 2920 semsg(_(e_invexpr2), *arg); 2921 ++*arg; 2922 return FAIL; 2923 } 2924 2925 switch (**arg) 2926 { 2927 /* 2928 * Number constant. 2929 */ 2930 case '0': 2931 case '1': 2932 case '2': 2933 case '3': 2934 case '4': 2935 case '5': 2936 case '6': 2937 case '7': 2938 case '8': 2939 case '9': 2940 case '.': ret = eval_number(arg, rettv, evaluate, want_string); 2941 2942 // Apply prefixed "-" and "+" now. Matters especially when 2943 // "->" follows. 2944 if (ret == OK && evaluate && end_leader > start_leader) 2945 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader); 2946 break; 2947 2948 /* 2949 * String constant: "string". 2950 */ 2951 case '"': ret = eval_string(arg, rettv, evaluate); 2952 break; 2953 2954 /* 2955 * Literal string constant: 'str''ing'. 2956 */ 2957 case '\'': ret = eval_lit_string(arg, rettv, evaluate); 2958 break; 2959 2960 /* 2961 * List: [expr, expr] 2962 */ 2963 case '[': ret = eval_list(arg, rettv, evalarg, TRUE); 2964 break; 2965 2966 /* 2967 * Dictionary: #{key: val, key: val} 2968 */ 2969 case '#': if ((*arg)[1] == '{') 2970 { 2971 ++*arg; 2972 ret = eval_dict(arg, rettv, evalarg, TRUE); 2973 } 2974 else 2975 ret = NOTDONE; 2976 break; 2977 2978 /* 2979 * Lambda: {arg, arg -> expr} 2980 * Dictionary: {'key': val, 'key': val} 2981 */ 2982 case '{': ret = get_lambda_tv(arg, rettv, evalarg); 2983 if (ret == NOTDONE) 2984 ret = eval_dict(arg, rettv, evalarg, FALSE); 2985 break; 2986 2987 /* 2988 * Option value: &name 2989 */ 2990 case '&': ret = eval_option(arg, rettv, evaluate); 2991 break; 2992 2993 /* 2994 * Environment variable: $VAR. 2995 */ 2996 case '$': ret = eval_env_var(arg, rettv, evaluate); 2997 break; 2998 2999 /* 3000 * Register contents: @r. 3001 */ 3002 case '@': ++*arg; 3003 if (evaluate) 3004 { 3005 rettv->v_type = VAR_STRING; 3006 rettv->vval.v_string = get_reg_contents(**arg, 3007 GREG_EXPR_SRC); 3008 } 3009 if (**arg != NUL) 3010 ++*arg; 3011 break; 3012 3013 /* 3014 * nested expression: (expression). 3015 */ 3016 case '(': { 3017 *arg = skipwhite_and_linebreak(*arg + 1, evalarg); 3018 ret = eval1(arg, rettv, evalarg); // recursive! 3019 3020 *arg = skipwhite_and_linebreak(*arg, evalarg); 3021 if (**arg == ')') 3022 ++*arg; 3023 else if (ret == OK) 3024 { 3025 emsg(_(e_missing_close)); 3026 clear_tv(rettv); 3027 ret = FAIL; 3028 } 3029 } 3030 break; 3031 3032 default: ret = NOTDONE; 3033 break; 3034 } 3035 3036 if (ret == NOTDONE) 3037 { 3038 /* 3039 * Must be a variable or function name. 3040 * Can also be a curly-braces kind of name: {expr}. 3041 */ 3042 s = *arg; 3043 len = get_name_len(arg, &alias, evaluate, TRUE); 3044 if (alias != NULL) 3045 s = alias; 3046 3047 if (len <= 0) 3048 ret = FAIL; 3049 else 3050 { 3051 if (**arg == '(') 3052 // "name(..." recursive! 3053 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL); 3054 else if (flags & EVAL_CONSTANT) 3055 ret = FAIL; 3056 else if (evaluate) 3057 // get value of variable 3058 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE); 3059 else 3060 { 3061 // skip the name 3062 check_vars(s, len); 3063 ret = OK; 3064 } 3065 } 3066 vim_free(alias); 3067 } 3068 3069 *arg = skipwhite(*arg); 3070 3071 // Handle following '[', '(' and '.' for expr[expr], expr.name, 3072 // expr(expr), expr->name(expr) 3073 if (ret == OK) 3074 ret = handle_subscript(arg, rettv, evalarg, TRUE); 3075 3076 /* 3077 * Apply logical NOT and unary '-', from right to left, ignore '+'. 3078 */ 3079 if (ret == OK && evaluate && end_leader > start_leader) 3080 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader); 3081 return ret; 3082 } 3083 3084 /* 3085 * Apply the leading "!" and "-" before an eval7 expression to "rettv". 3086 * When "numeric_only" is TRUE only handle "+" and "-". 3087 * Adjusts "end_leaderp" until it is at "start_leader". 3088 */ 3089 static int 3090 eval7_leader( 3091 typval_T *rettv, 3092 int numeric_only, 3093 char_u *start_leader, 3094 char_u **end_leaderp) 3095 { 3096 char_u *end_leader = *end_leaderp; 3097 int ret = OK; 3098 int error = FALSE; 3099 varnumber_T val = 0; 3100 #ifdef FEAT_FLOAT 3101 float_T f = 0.0; 3102 3103 if (rettv->v_type == VAR_FLOAT) 3104 f = rettv->vval.v_float; 3105 else 3106 #endif 3107 val = tv_get_number_chk(rettv, &error); 3108 if (error) 3109 { 3110 clear_tv(rettv); 3111 ret = FAIL; 3112 } 3113 else 3114 { 3115 while (end_leader > start_leader) 3116 { 3117 --end_leader; 3118 if (*end_leader == '!') 3119 { 3120 if (numeric_only) 3121 { 3122 ++end_leader; 3123 break; 3124 } 3125 #ifdef FEAT_FLOAT 3126 if (rettv->v_type == VAR_FLOAT) 3127 f = !f; 3128 else 3129 #endif 3130 val = !val; 3131 } 3132 else if (*end_leader == '-') 3133 { 3134 #ifdef FEAT_FLOAT 3135 if (rettv->v_type == VAR_FLOAT) 3136 f = -f; 3137 else 3138 #endif 3139 val = -val; 3140 } 3141 } 3142 #ifdef FEAT_FLOAT 3143 if (rettv->v_type == VAR_FLOAT) 3144 { 3145 clear_tv(rettv); 3146 rettv->vval.v_float = f; 3147 } 3148 else 3149 #endif 3150 { 3151 clear_tv(rettv); 3152 rettv->v_type = VAR_NUMBER; 3153 rettv->vval.v_number = val; 3154 } 3155 } 3156 *end_leaderp = end_leader; 3157 return ret; 3158 } 3159 3160 /* 3161 * Call the function referred to in "rettv". 3162 */ 3163 static int 3164 call_func_rettv( 3165 char_u **arg, 3166 evalarg_T *evalarg, 3167 typval_T *rettv, 3168 int evaluate, 3169 dict_T *selfdict, 3170 typval_T *basetv) 3171 { 3172 partial_T *pt = NULL; 3173 funcexe_T funcexe; 3174 typval_T functv; 3175 char_u *s; 3176 int ret; 3177 3178 // need to copy the funcref so that we can clear rettv 3179 if (evaluate) 3180 { 3181 functv = *rettv; 3182 rettv->v_type = VAR_UNKNOWN; 3183 3184 // Invoke the function. Recursive! 3185 if (functv.v_type == VAR_PARTIAL) 3186 { 3187 pt = functv.vval.v_partial; 3188 s = partial_name(pt); 3189 } 3190 else 3191 s = functv.vval.v_string; 3192 } 3193 else 3194 s = (char_u *)""; 3195 3196 CLEAR_FIELD(funcexe); 3197 funcexe.firstline = curwin->w_cursor.lnum; 3198 funcexe.lastline = curwin->w_cursor.lnum; 3199 funcexe.evaluate = evaluate; 3200 funcexe.partial = pt; 3201 funcexe.selfdict = selfdict; 3202 funcexe.basetv = basetv; 3203 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe); 3204 3205 // Clear the funcref afterwards, so that deleting it while 3206 // evaluating the arguments is possible (see test55). 3207 if (evaluate) 3208 clear_tv(&functv); 3209 3210 return ret; 3211 } 3212 3213 /* 3214 * Evaluate "->method()". 3215 * "*arg" points to the '-'. 3216 * Returns FAIL or OK. "*arg" is advanced to after the ')'. 3217 */ 3218 static int 3219 eval_lambda( 3220 char_u **arg, 3221 typval_T *rettv, 3222 evalarg_T *evalarg, 3223 int verbose) // give error messages 3224 { 3225 int evaluate = evalarg != NULL 3226 && (evalarg->eval_flags & EVAL_EVALUATE); 3227 typval_T base = *rettv; 3228 int ret; 3229 3230 // Skip over the ->. 3231 *arg += 2; 3232 rettv->v_type = VAR_UNKNOWN; 3233 3234 ret = get_lambda_tv(arg, rettv, evalarg); 3235 if (ret != OK) 3236 return FAIL; 3237 else if (**arg != '(') 3238 { 3239 if (verbose) 3240 { 3241 if (*skipwhite(*arg) == '(') 3242 emsg(_(e_nowhitespace)); 3243 else 3244 semsg(_(e_missing_paren), "lambda"); 3245 } 3246 clear_tv(rettv); 3247 ret = FAIL; 3248 } 3249 else 3250 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base); 3251 3252 // Clear the funcref afterwards, so that deleting it while 3253 // evaluating the arguments is possible (see test55). 3254 if (evaluate) 3255 clear_tv(&base); 3256 3257 return ret; 3258 } 3259 3260 /* 3261 * Evaluate "->method()". 3262 * "*arg" points to the '-'. 3263 * Returns FAIL or OK. "*arg" is advanced to after the ')'. 3264 */ 3265 static int 3266 eval_method( 3267 char_u **arg, 3268 typval_T *rettv, 3269 evalarg_T *evalarg, 3270 int verbose) // give error messages 3271 { 3272 char_u *name; 3273 long len; 3274 char_u *alias; 3275 typval_T base = *rettv; 3276 int ret; 3277 int evaluate = evalarg != NULL 3278 && (evalarg->eval_flags & EVAL_EVALUATE); 3279 3280 // Skip over the ->. 3281 *arg += 2; 3282 rettv->v_type = VAR_UNKNOWN; 3283 3284 name = *arg; 3285 len = get_name_len(arg, &alias, evaluate, TRUE); 3286 if (alias != NULL) 3287 name = alias; 3288 3289 if (len <= 0) 3290 { 3291 if (verbose) 3292 emsg(_("E260: Missing name after ->")); 3293 ret = FAIL; 3294 } 3295 else 3296 { 3297 if (**arg != '(') 3298 { 3299 if (verbose) 3300 semsg(_(e_missing_paren), name); 3301 ret = FAIL; 3302 } 3303 else if (VIM_ISWHITE((*arg)[-1])) 3304 { 3305 if (verbose) 3306 emsg(_(e_nowhitespace)); 3307 ret = FAIL; 3308 } 3309 else 3310 ret = eval_func(arg, evalarg, name, len, rettv, 3311 evaluate ? EVAL_EVALUATE : 0, &base); 3312 } 3313 3314 // Clear the funcref afterwards, so that deleting it while 3315 // evaluating the arguments is possible (see test55). 3316 if (evaluate) 3317 clear_tv(&base); 3318 3319 return ret; 3320 } 3321 3322 /* 3323 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key". 3324 * "*arg" points to the '[' or '.'. 3325 * Returns FAIL or OK. "*arg" is advanced to after the ']'. 3326 */ 3327 static int 3328 eval_index( 3329 char_u **arg, 3330 typval_T *rettv, 3331 evalarg_T *evalarg, 3332 int verbose) // give error messages 3333 { 3334 int evaluate = evalarg != NULL 3335 && (evalarg->eval_flags & EVAL_EVALUATE); 3336 int empty1 = FALSE, empty2 = FALSE; 3337 typval_T var1, var2; 3338 long i; 3339 long n1, n2 = 0; 3340 long len = -1; 3341 int range = FALSE; 3342 char_u *s; 3343 char_u *key = NULL; 3344 3345 switch (rettv->v_type) 3346 { 3347 case VAR_FUNC: 3348 case VAR_PARTIAL: 3349 if (verbose) 3350 emsg(_("E695: Cannot index a Funcref")); 3351 return FAIL; 3352 case VAR_FLOAT: 3353 #ifdef FEAT_FLOAT 3354 if (verbose) 3355 emsg(_(e_float_as_string)); 3356 return FAIL; 3357 #endif 3358 case VAR_BOOL: 3359 case VAR_SPECIAL: 3360 case VAR_JOB: 3361 case VAR_CHANNEL: 3362 if (verbose) 3363 emsg(_("E909: Cannot index a special variable")); 3364 return FAIL; 3365 case VAR_UNKNOWN: 3366 case VAR_ANY: 3367 case VAR_VOID: 3368 if (evaluate) 3369 return FAIL; 3370 // FALLTHROUGH 3371 3372 case VAR_STRING: 3373 case VAR_NUMBER: 3374 case VAR_LIST: 3375 case VAR_DICT: 3376 case VAR_BLOB: 3377 break; 3378 } 3379 3380 init_tv(&var1); 3381 init_tv(&var2); 3382 if (**arg == '.') 3383 { 3384 /* 3385 * dict.name 3386 */ 3387 key = *arg + 1; 3388 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len) 3389 ; 3390 if (len == 0) 3391 return FAIL; 3392 *arg = skipwhite(key + len); 3393 } 3394 else 3395 { 3396 /* 3397 * something[idx] 3398 * 3399 * Get the (first) variable from inside the []. 3400 */ 3401 *arg = skipwhite_and_linebreak(*arg + 1, evalarg); 3402 if (**arg == ':') 3403 empty1 = TRUE; 3404 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive! 3405 return FAIL; 3406 else if (evaluate && tv_get_string_chk(&var1) == NULL) 3407 { 3408 // not a number or string 3409 clear_tv(&var1); 3410 return FAIL; 3411 } 3412 3413 /* 3414 * Get the second variable from inside the [:]. 3415 */ 3416 *arg = skipwhite_and_linebreak(*arg, evalarg); 3417 if (**arg == ':') 3418 { 3419 range = TRUE; 3420 *arg = skipwhite_and_linebreak(*arg + 1, evalarg); 3421 if (**arg == ']') 3422 empty2 = TRUE; 3423 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive! 3424 { 3425 if (!empty1) 3426 clear_tv(&var1); 3427 return FAIL; 3428 } 3429 else if (evaluate && tv_get_string_chk(&var2) == NULL) 3430 { 3431 // not a number or string 3432 if (!empty1) 3433 clear_tv(&var1); 3434 clear_tv(&var2); 3435 return FAIL; 3436 } 3437 } 3438 3439 // Check for the ']'. 3440 *arg = skipwhite_and_linebreak(*arg, evalarg); 3441 if (**arg != ']') 3442 { 3443 if (verbose) 3444 emsg(_(e_missbrac)); 3445 clear_tv(&var1); 3446 if (range) 3447 clear_tv(&var2); 3448 return FAIL; 3449 } 3450 *arg = skipwhite(*arg + 1); // skip the ']' 3451 } 3452 3453 if (evaluate) 3454 { 3455 n1 = 0; 3456 if (!empty1 && rettv->v_type != VAR_DICT) 3457 { 3458 n1 = tv_get_number(&var1); 3459 clear_tv(&var1); 3460 } 3461 if (range) 3462 { 3463 if (empty2) 3464 n2 = -1; 3465 else 3466 { 3467 n2 = tv_get_number(&var2); 3468 clear_tv(&var2); 3469 } 3470 } 3471 3472 switch (rettv->v_type) 3473 { 3474 case VAR_UNKNOWN: 3475 case VAR_ANY: 3476 case VAR_VOID: 3477 case VAR_FUNC: 3478 case VAR_PARTIAL: 3479 case VAR_FLOAT: 3480 case VAR_BOOL: 3481 case VAR_SPECIAL: 3482 case VAR_JOB: 3483 case VAR_CHANNEL: 3484 break; // not evaluating, skipping over subscript 3485 3486 case VAR_NUMBER: 3487 case VAR_STRING: 3488 s = tv_get_string(rettv); 3489 len = (long)STRLEN(s); 3490 if (range) 3491 { 3492 // The resulting variable is a substring. If the indexes 3493 // are out of range the result is empty. 3494 if (n1 < 0) 3495 { 3496 n1 = len + n1; 3497 if (n1 < 0) 3498 n1 = 0; 3499 } 3500 if (n2 < 0) 3501 n2 = len + n2; 3502 else if (n2 >= len) 3503 n2 = len; 3504 if (n1 >= len || n2 < 0 || n1 > n2) 3505 s = NULL; 3506 else 3507 s = vim_strnsave(s + n1, n2 - n1 + 1); 3508 } 3509 else 3510 { 3511 // The resulting variable is a string of a single 3512 // character. If the index is too big or negative the 3513 // result is empty. 3514 if (n1 >= len || n1 < 0) 3515 s = NULL; 3516 else 3517 s = vim_strnsave(s + n1, 1); 3518 } 3519 clear_tv(rettv); 3520 rettv->v_type = VAR_STRING; 3521 rettv->vval.v_string = s; 3522 break; 3523 3524 case VAR_BLOB: 3525 len = blob_len(rettv->vval.v_blob); 3526 if (range) 3527 { 3528 // The resulting variable is a sub-blob. If the indexes 3529 // are out of range the result is empty. 3530 if (n1 < 0) 3531 { 3532 n1 = len + n1; 3533 if (n1 < 0) 3534 n1 = 0; 3535 } 3536 if (n2 < 0) 3537 n2 = len + n2; 3538 else if (n2 >= len) 3539 n2 = len - 1; 3540 if (n1 >= len || n2 < 0 || n1 > n2) 3541 { 3542 clear_tv(rettv); 3543 rettv->v_type = VAR_BLOB; 3544 rettv->vval.v_blob = NULL; 3545 } 3546 else 3547 { 3548 blob_T *blob = blob_alloc(); 3549 3550 if (blob != NULL) 3551 { 3552 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL) 3553 { 3554 blob_free(blob); 3555 return FAIL; 3556 } 3557 blob->bv_ga.ga_len = n2 - n1 + 1; 3558 for (i = n1; i <= n2; i++) 3559 blob_set(blob, i - n1, 3560 blob_get(rettv->vval.v_blob, i)); 3561 3562 clear_tv(rettv); 3563 rettv_blob_set(rettv, blob); 3564 } 3565 } 3566 } 3567 else 3568 { 3569 // The resulting variable is a byte value. 3570 // If the index is too big or negative that is an error. 3571 if (n1 < 0) 3572 n1 = len + n1; 3573 if (n1 < len && n1 >= 0) 3574 { 3575 int v = blob_get(rettv->vval.v_blob, n1); 3576 3577 clear_tv(rettv); 3578 rettv->v_type = VAR_NUMBER; 3579 rettv->vval.v_number = v; 3580 } 3581 else 3582 semsg(_(e_blobidx), n1); 3583 } 3584 break; 3585 3586 case VAR_LIST: 3587 len = list_len(rettv->vval.v_list); 3588 if (n1 < 0) 3589 n1 = len + n1; 3590 if (!empty1 && (n1 < 0 || n1 >= len)) 3591 { 3592 // For a range we allow invalid values and return an empty 3593 // list. A list index out of range is an error. 3594 if (!range) 3595 { 3596 if (verbose) 3597 semsg(_(e_listidx), n1); 3598 return FAIL; 3599 } 3600 n1 = len; 3601 } 3602 if (range) 3603 { 3604 list_T *l; 3605 3606 if (n2 < 0) 3607 n2 = len + n2; 3608 else if (n2 >= len) 3609 n2 = len - 1; 3610 if (!empty2 && (n2 < 0 || n2 + 1 < n1)) 3611 n2 = -1; 3612 l = list_slice(rettv->vval.v_list, n1, n2); 3613 if (l == NULL) 3614 return FAIL; 3615 clear_tv(rettv); 3616 rettv_list_set(rettv, l); 3617 } 3618 else 3619 { 3620 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1); 3621 clear_tv(rettv); 3622 *rettv = var1; 3623 } 3624 break; 3625 3626 case VAR_DICT: 3627 if (range) 3628 { 3629 if (verbose) 3630 emsg(_(e_dictrange)); 3631 if (len == -1) 3632 clear_tv(&var1); 3633 return FAIL; 3634 } 3635 { 3636 dictitem_T *item; 3637 3638 if (len == -1) 3639 { 3640 key = tv_get_string_chk(&var1); 3641 if (key == NULL) 3642 { 3643 clear_tv(&var1); 3644 return FAIL; 3645 } 3646 } 3647 3648 item = dict_find(rettv->vval.v_dict, key, (int)len); 3649 3650 if (item == NULL && verbose) 3651 semsg(_(e_dictkey), key); 3652 if (len == -1) 3653 clear_tv(&var1); 3654 if (item == NULL) 3655 return FAIL; 3656 3657 copy_tv(&item->di_tv, &var1); 3658 clear_tv(rettv); 3659 *rettv = var1; 3660 } 3661 break; 3662 } 3663 } 3664 3665 return OK; 3666 } 3667 3668 /* 3669 * Return the function name of partial "pt". 3670 */ 3671 char_u * 3672 partial_name(partial_T *pt) 3673 { 3674 if (pt->pt_name != NULL) 3675 return pt->pt_name; 3676 if (pt->pt_func != NULL) 3677 return pt->pt_func->uf_name; 3678 return (char_u *)""; 3679 } 3680 3681 static void 3682 partial_free(partial_T *pt) 3683 { 3684 int i; 3685 3686 for (i = 0; i < pt->pt_argc; ++i) 3687 clear_tv(&pt->pt_argv[i]); 3688 vim_free(pt->pt_argv); 3689 dict_unref(pt->pt_dict); 3690 if (pt->pt_name != NULL) 3691 { 3692 func_unref(pt->pt_name); 3693 vim_free(pt->pt_name); 3694 } 3695 else 3696 func_ptr_unref(pt->pt_func); 3697 3698 if (pt->pt_funcstack != NULL) 3699 { 3700 // Decrease the reference count for the context of a closure. If down 3701 // to zero free it and clear the variables on the stack. 3702 if (--pt->pt_funcstack->fs_refcount == 0) 3703 { 3704 garray_T *gap = &pt->pt_funcstack->fs_ga; 3705 typval_T *stack = gap->ga_data; 3706 3707 for (i = 0; i < gap->ga_len; ++i) 3708 clear_tv(stack + i); 3709 ga_clear(gap); 3710 vim_free(pt->pt_funcstack); 3711 } 3712 pt->pt_funcstack = NULL; 3713 } 3714 3715 vim_free(pt); 3716 } 3717 3718 /* 3719 * Unreference a closure: decrement the reference count and free it when it 3720 * becomes zero. 3721 */ 3722 void 3723 partial_unref(partial_T *pt) 3724 { 3725 if (pt != NULL && --pt->pt_refcount <= 0) 3726 partial_free(pt); 3727 } 3728 3729 /* 3730 * Return the next (unique) copy ID. 3731 * Used for serializing nested structures. 3732 */ 3733 int 3734 get_copyID(void) 3735 { 3736 current_copyID += COPYID_INC; 3737 return current_copyID; 3738 } 3739 3740 /* 3741 * Garbage collection for lists and dictionaries. 3742 * 3743 * We use reference counts to be able to free most items right away when they 3744 * are no longer used. But for composite items it's possible that it becomes 3745 * unused while the reference count is > 0: When there is a recursive 3746 * reference. Example: 3747 * :let l = [1, 2, 3] 3748 * :let d = {9: l} 3749 * :let l[1] = d 3750 * 3751 * Since this is quite unusual we handle this with garbage collection: every 3752 * once in a while find out which lists and dicts are not referenced from any 3753 * variable. 3754 * 3755 * Here is a good reference text about garbage collection (refers to Python 3756 * but it applies to all reference-counting mechanisms): 3757 * http://python.ca/nas/python/gc/ 3758 */ 3759 3760 /* 3761 * Do garbage collection for lists and dicts. 3762 * When "testing" is TRUE this is called from test_garbagecollect_now(). 3763 * Return TRUE if some memory was freed. 3764 */ 3765 int 3766 garbage_collect(int testing) 3767 { 3768 int copyID; 3769 int abort = FALSE; 3770 buf_T *buf; 3771 win_T *wp; 3772 int did_free = FALSE; 3773 tabpage_T *tp; 3774 3775 if (!testing) 3776 { 3777 // Only do this once. 3778 want_garbage_collect = FALSE; 3779 may_garbage_collect = FALSE; 3780 garbage_collect_at_exit = FALSE; 3781 } 3782 3783 // The execution stack can grow big, limit the size. 3784 if (exestack.ga_maxlen - exestack.ga_len > 500) 3785 { 3786 size_t new_len; 3787 char_u *pp; 3788 int n; 3789 3790 // Keep 150% of the current size, with a minimum of the growth size. 3791 n = exestack.ga_len / 2; 3792 if (n < exestack.ga_growsize) 3793 n = exestack.ga_growsize; 3794 3795 // Don't make it bigger though. 3796 if (exestack.ga_len + n < exestack.ga_maxlen) 3797 { 3798 new_len = exestack.ga_itemsize * (exestack.ga_len + n); 3799 pp = vim_realloc(exestack.ga_data, new_len); 3800 if (pp == NULL) 3801 return FAIL; 3802 exestack.ga_maxlen = exestack.ga_len + n; 3803 exestack.ga_data = pp; 3804 } 3805 } 3806 3807 // We advance by two because we add one for items referenced through 3808 // previous_funccal. 3809 copyID = get_copyID(); 3810 3811 /* 3812 * 1. Go through all accessible variables and mark all lists and dicts 3813 * with copyID. 3814 */ 3815 3816 // Don't free variables in the previous_funccal list unless they are only 3817 // referenced through previous_funccal. This must be first, because if 3818 // the item is referenced elsewhere the funccal must not be freed. 3819 abort = abort || set_ref_in_previous_funccal(copyID); 3820 3821 // script-local variables 3822 abort = abort || garbage_collect_scriptvars(copyID); 3823 3824 // buffer-local variables 3825 FOR_ALL_BUFFERS(buf) 3826 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID, 3827 NULL, NULL); 3828 3829 // window-local variables 3830 FOR_ALL_TAB_WINDOWS(tp, wp) 3831 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID, 3832 NULL, NULL); 3833 if (aucmd_win != NULL) 3834 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID, 3835 NULL, NULL); 3836 #ifdef FEAT_PROP_POPUP 3837 FOR_ALL_POPUPWINS(wp) 3838 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID, 3839 NULL, NULL); 3840 FOR_ALL_TABPAGES(tp) 3841 FOR_ALL_POPUPWINS_IN_TAB(tp, wp) 3842 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID, 3843 NULL, NULL); 3844 #endif 3845 3846 // tabpage-local variables 3847 FOR_ALL_TABPAGES(tp) 3848 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID, 3849 NULL, NULL); 3850 // global variables 3851 abort = abort || garbage_collect_globvars(copyID); 3852 3853 // function-local variables 3854 abort = abort || set_ref_in_call_stack(copyID); 3855 3856 // named functions (matters for closures) 3857 abort = abort || set_ref_in_functions(copyID); 3858 3859 // function call arguments, if v:testing is set. 3860 abort = abort || set_ref_in_func_args(copyID); 3861 3862 // v: vars 3863 abort = abort || garbage_collect_vimvars(copyID); 3864 3865 // callbacks in buffers 3866 abort = abort || set_ref_in_buffers(copyID); 3867 3868 #ifdef FEAT_LUA 3869 abort = abort || set_ref_in_lua(copyID); 3870 #endif 3871 3872 #ifdef FEAT_PYTHON 3873 abort = abort || set_ref_in_python(copyID); 3874 #endif 3875 3876 #ifdef FEAT_PYTHON3 3877 abort = abort || set_ref_in_python3(copyID); 3878 #endif 3879 3880 #ifdef FEAT_JOB_CHANNEL 3881 abort = abort || set_ref_in_channel(copyID); 3882 abort = abort || set_ref_in_job(copyID); 3883 #endif 3884 #ifdef FEAT_NETBEANS_INTG 3885 abort = abort || set_ref_in_nb_channel(copyID); 3886 #endif 3887 3888 #ifdef FEAT_TIMERS 3889 abort = abort || set_ref_in_timer(copyID); 3890 #endif 3891 3892 #ifdef FEAT_QUICKFIX 3893 abort = abort || set_ref_in_quickfix(copyID); 3894 #endif 3895 3896 #ifdef FEAT_TERMINAL 3897 abort = abort || set_ref_in_term(copyID); 3898 #endif 3899 3900 #ifdef FEAT_PROP_POPUP 3901 abort = abort || set_ref_in_popups(copyID); 3902 #endif 3903 3904 if (!abort) 3905 { 3906 /* 3907 * 2. Free lists and dictionaries that are not referenced. 3908 */ 3909 did_free = free_unref_items(copyID); 3910 3911 /* 3912 * 3. Check if any funccal can be freed now. 3913 * This may call us back recursively. 3914 */ 3915 free_unref_funccal(copyID, testing); 3916 } 3917 else if (p_verbose > 0) 3918 { 3919 verb_msg(_("Not enough memory to set references, garbage collection aborted!")); 3920 } 3921 3922 return did_free; 3923 } 3924 3925 /* 3926 * Free lists, dictionaries, channels and jobs that are no longer referenced. 3927 */ 3928 static int 3929 free_unref_items(int copyID) 3930 { 3931 int did_free = FALSE; 3932 3933 // Let all "free" functions know that we are here. This means no 3934 // dictionaries, lists, channels or jobs are to be freed, because we will 3935 // do that here. 3936 in_free_unref_items = TRUE; 3937 3938 /* 3939 * PASS 1: free the contents of the items. We don't free the items 3940 * themselves yet, so that it is possible to decrement refcount counters 3941 */ 3942 3943 // Go through the list of dicts and free items without the copyID. 3944 did_free |= dict_free_nonref(copyID); 3945 3946 // Go through the list of lists and free items without the copyID. 3947 did_free |= list_free_nonref(copyID); 3948 3949 #ifdef FEAT_JOB_CHANNEL 3950 // Go through the list of jobs and free items without the copyID. This 3951 // must happen before doing channels, because jobs refer to channels, but 3952 // the reference from the channel to the job isn't tracked. 3953 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK); 3954 3955 // Go through the list of channels and free items without the copyID. 3956 did_free |= free_unused_channels_contents(copyID, COPYID_MASK); 3957 #endif 3958 3959 /* 3960 * PASS 2: free the items themselves. 3961 */ 3962 dict_free_items(copyID); 3963 list_free_items(copyID); 3964 3965 #ifdef FEAT_JOB_CHANNEL 3966 // Go through the list of jobs and free items without the copyID. This 3967 // must happen before doing channels, because jobs refer to channels, but 3968 // the reference from the channel to the job isn't tracked. 3969 free_unused_jobs(copyID, COPYID_MASK); 3970 3971 // Go through the list of channels and free items without the copyID. 3972 free_unused_channels(copyID, COPYID_MASK); 3973 #endif 3974 3975 in_free_unref_items = FALSE; 3976 3977 return did_free; 3978 } 3979 3980 /* 3981 * Mark all lists and dicts referenced through hashtab "ht" with "copyID". 3982 * "list_stack" is used to add lists to be marked. Can be NULL. 3983 * 3984 * Returns TRUE if setting references failed somehow. 3985 */ 3986 int 3987 set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack) 3988 { 3989 int todo; 3990 int abort = FALSE; 3991 hashitem_T *hi; 3992 hashtab_T *cur_ht; 3993 ht_stack_T *ht_stack = NULL; 3994 ht_stack_T *tempitem; 3995 3996 cur_ht = ht; 3997 for (;;) 3998 { 3999 if (!abort) 4000 { 4001 // Mark each item in the hashtab. If the item contains a hashtab 4002 // it is added to ht_stack, if it contains a list it is added to 4003 // list_stack. 4004 todo = (int)cur_ht->ht_used; 4005 for (hi = cur_ht->ht_array; todo > 0; ++hi) 4006 if (!HASHITEM_EMPTY(hi)) 4007 { 4008 --todo; 4009 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID, 4010 &ht_stack, list_stack); 4011 } 4012 } 4013 4014 if (ht_stack == NULL) 4015 break; 4016 4017 // take an item from the stack 4018 cur_ht = ht_stack->ht; 4019 tempitem = ht_stack; 4020 ht_stack = ht_stack->prev; 4021 free(tempitem); 4022 } 4023 4024 return abort; 4025 } 4026 4027 /* 4028 * Mark a dict and its items with "copyID". 4029 * Returns TRUE if setting references failed somehow. 4030 */ 4031 int 4032 set_ref_in_dict(dict_T *d, int copyID) 4033 { 4034 if (d != NULL && d->dv_copyID != copyID) 4035 { 4036 d->dv_copyID = copyID; 4037 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL); 4038 } 4039 return FALSE; 4040 } 4041 4042 /* 4043 * Mark a list and its items with "copyID". 4044 * Returns TRUE if setting references failed somehow. 4045 */ 4046 int 4047 set_ref_in_list(list_T *ll, int copyID) 4048 { 4049 if (ll != NULL && ll->lv_copyID != copyID) 4050 { 4051 ll->lv_copyID = copyID; 4052 return set_ref_in_list_items(ll, copyID, NULL); 4053 } 4054 return FALSE; 4055 } 4056 4057 /* 4058 * Mark all lists and dicts referenced through list "l" with "copyID". 4059 * "ht_stack" is used to add hashtabs to be marked. Can be NULL. 4060 * 4061 * Returns TRUE if setting references failed somehow. 4062 */ 4063 int 4064 set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack) 4065 { 4066 listitem_T *li; 4067 int abort = FALSE; 4068 list_T *cur_l; 4069 list_stack_T *list_stack = NULL; 4070 list_stack_T *tempitem; 4071 4072 cur_l = l; 4073 for (;;) 4074 { 4075 if (!abort && cur_l->lv_first != &range_list_item) 4076 // Mark each item in the list. If the item contains a hashtab 4077 // it is added to ht_stack, if it contains a list it is added to 4078 // list_stack. 4079 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next) 4080 abort = abort || set_ref_in_item(&li->li_tv, copyID, 4081 ht_stack, &list_stack); 4082 if (list_stack == NULL) 4083 break; 4084 4085 // take an item from the stack 4086 cur_l = list_stack->list; 4087 tempitem = list_stack; 4088 list_stack = list_stack->prev; 4089 free(tempitem); 4090 } 4091 4092 return abort; 4093 } 4094 4095 /* 4096 * Mark all lists and dicts referenced through typval "tv" with "copyID". 4097 * "list_stack" is used to add lists to be marked. Can be NULL. 4098 * "ht_stack" is used to add hashtabs to be marked. Can be NULL. 4099 * 4100 * Returns TRUE if setting references failed somehow. 4101 */ 4102 int 4103 set_ref_in_item( 4104 typval_T *tv, 4105 int copyID, 4106 ht_stack_T **ht_stack, 4107 list_stack_T **list_stack) 4108 { 4109 int abort = FALSE; 4110 4111 if (tv->v_type == VAR_DICT) 4112 { 4113 dict_T *dd = tv->vval.v_dict; 4114 4115 if (dd != NULL && dd->dv_copyID != copyID) 4116 { 4117 // Didn't see this dict yet. 4118 dd->dv_copyID = copyID; 4119 if (ht_stack == NULL) 4120 { 4121 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack); 4122 } 4123 else 4124 { 4125 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T)); 4126 if (newitem == NULL) 4127 abort = TRUE; 4128 else 4129 { 4130 newitem->ht = &dd->dv_hashtab; 4131 newitem->prev = *ht_stack; 4132 *ht_stack = newitem; 4133 } 4134 } 4135 } 4136 } 4137 else if (tv->v_type == VAR_LIST) 4138 { 4139 list_T *ll = tv->vval.v_list; 4140 4141 if (ll != NULL && ll->lv_copyID != copyID) 4142 { 4143 // Didn't see this list yet. 4144 ll->lv_copyID = copyID; 4145 if (list_stack == NULL) 4146 { 4147 abort = set_ref_in_list_items(ll, copyID, ht_stack); 4148 } 4149 else 4150 { 4151 list_stack_T *newitem = (list_stack_T*)malloc( 4152 sizeof(list_stack_T)); 4153 if (newitem == NULL) 4154 abort = TRUE; 4155 else 4156 { 4157 newitem->list = ll; 4158 newitem->prev = *list_stack; 4159 *list_stack = newitem; 4160 } 4161 } 4162 } 4163 } 4164 else if (tv->v_type == VAR_FUNC) 4165 { 4166 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID); 4167 } 4168 else if (tv->v_type == VAR_PARTIAL) 4169 { 4170 partial_T *pt = tv->vval.v_partial; 4171 int i; 4172 4173 if (pt != NULL && pt->pt_copyID != copyID) 4174 { 4175 // Didn't see this partial yet. 4176 pt->pt_copyID = copyID; 4177 4178 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID); 4179 4180 if (pt->pt_dict != NULL) 4181 { 4182 typval_T dtv; 4183 4184 dtv.v_type = VAR_DICT; 4185 dtv.vval.v_dict = pt->pt_dict; 4186 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4187 } 4188 4189 for (i = 0; i < pt->pt_argc; ++i) 4190 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID, 4191 ht_stack, list_stack); 4192 if (pt->pt_funcstack != NULL) 4193 { 4194 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data; 4195 4196 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i) 4197 abort = abort || set_ref_in_item(stack + i, copyID, 4198 ht_stack, list_stack); 4199 } 4200 4201 } 4202 } 4203 #ifdef FEAT_JOB_CHANNEL 4204 else if (tv->v_type == VAR_JOB) 4205 { 4206 job_T *job = tv->vval.v_job; 4207 typval_T dtv; 4208 4209 if (job != NULL && job->jv_copyID != copyID) 4210 { 4211 job->jv_copyID = copyID; 4212 if (job->jv_channel != NULL) 4213 { 4214 dtv.v_type = VAR_CHANNEL; 4215 dtv.vval.v_channel = job->jv_channel; 4216 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4217 } 4218 if (job->jv_exit_cb.cb_partial != NULL) 4219 { 4220 dtv.v_type = VAR_PARTIAL; 4221 dtv.vval.v_partial = job->jv_exit_cb.cb_partial; 4222 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4223 } 4224 } 4225 } 4226 else if (tv->v_type == VAR_CHANNEL) 4227 { 4228 channel_T *ch =tv->vval.v_channel; 4229 ch_part_T part; 4230 typval_T dtv; 4231 jsonq_T *jq; 4232 cbq_T *cq; 4233 4234 if (ch != NULL && ch->ch_copyID != copyID) 4235 { 4236 ch->ch_copyID = copyID; 4237 for (part = PART_SOCK; part < PART_COUNT; ++part) 4238 { 4239 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL; 4240 jq = jq->jq_next) 4241 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack); 4242 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL; 4243 cq = cq->cq_next) 4244 if (cq->cq_callback.cb_partial != NULL) 4245 { 4246 dtv.v_type = VAR_PARTIAL; 4247 dtv.vval.v_partial = cq->cq_callback.cb_partial; 4248 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4249 } 4250 if (ch->ch_part[part].ch_callback.cb_partial != NULL) 4251 { 4252 dtv.v_type = VAR_PARTIAL; 4253 dtv.vval.v_partial = 4254 ch->ch_part[part].ch_callback.cb_partial; 4255 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4256 } 4257 } 4258 if (ch->ch_callback.cb_partial != NULL) 4259 { 4260 dtv.v_type = VAR_PARTIAL; 4261 dtv.vval.v_partial = ch->ch_callback.cb_partial; 4262 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4263 } 4264 if (ch->ch_close_cb.cb_partial != NULL) 4265 { 4266 dtv.v_type = VAR_PARTIAL; 4267 dtv.vval.v_partial = ch->ch_close_cb.cb_partial; 4268 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4269 } 4270 } 4271 } 4272 #endif 4273 return abort; 4274 } 4275 4276 /* 4277 * Return a string with the string representation of a variable. 4278 * If the memory is allocated "tofree" is set to it, otherwise NULL. 4279 * "numbuf" is used for a number. 4280 * When "copyID" is not NULL replace recursive lists and dicts with "...". 4281 * When both "echo_style" and "composite_val" are FALSE, put quotes around 4282 * stings as "string()", otherwise does not put quotes around strings, as 4283 * ":echo" displays values. 4284 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists 4285 * are replaced with "...". 4286 * May return NULL. 4287 */ 4288 char_u * 4289 echo_string_core( 4290 typval_T *tv, 4291 char_u **tofree, 4292 char_u *numbuf, 4293 int copyID, 4294 int echo_style, 4295 int restore_copyID, 4296 int composite_val) 4297 { 4298 static int recurse = 0; 4299 char_u *r = NULL; 4300 4301 if (recurse >= DICT_MAXNEST) 4302 { 4303 if (!did_echo_string_emsg) 4304 { 4305 // Only give this message once for a recursive call to avoid 4306 // flooding the user with errors. And stop iterating over lists 4307 // and dicts. 4308 did_echo_string_emsg = TRUE; 4309 emsg(_("E724: variable nested too deep for displaying")); 4310 } 4311 *tofree = NULL; 4312 return (char_u *)"{E724}"; 4313 } 4314 ++recurse; 4315 4316 switch (tv->v_type) 4317 { 4318 case VAR_STRING: 4319 if (echo_style && !composite_val) 4320 { 4321 *tofree = NULL; 4322 r = tv->vval.v_string; 4323 if (r == NULL) 4324 r = (char_u *)""; 4325 } 4326 else 4327 { 4328 *tofree = string_quote(tv->vval.v_string, FALSE); 4329 r = *tofree; 4330 } 4331 break; 4332 4333 case VAR_FUNC: 4334 if (echo_style) 4335 { 4336 *tofree = NULL; 4337 r = tv->vval.v_string; 4338 } 4339 else 4340 { 4341 *tofree = string_quote(tv->vval.v_string, TRUE); 4342 r = *tofree; 4343 } 4344 break; 4345 4346 case VAR_PARTIAL: 4347 { 4348 partial_T *pt = tv->vval.v_partial; 4349 char_u *fname = string_quote(pt == NULL ? NULL 4350 : partial_name(pt), FALSE); 4351 garray_T ga; 4352 int i; 4353 char_u *tf; 4354 4355 ga_init2(&ga, 1, 100); 4356 ga_concat(&ga, (char_u *)"function("); 4357 if (fname != NULL) 4358 { 4359 ga_concat(&ga, fname); 4360 vim_free(fname); 4361 } 4362 if (pt != NULL && pt->pt_argc > 0) 4363 { 4364 ga_concat(&ga, (char_u *)", ["); 4365 for (i = 0; i < pt->pt_argc; ++i) 4366 { 4367 if (i > 0) 4368 ga_concat(&ga, (char_u *)", "); 4369 ga_concat(&ga, 4370 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID)); 4371 vim_free(tf); 4372 } 4373 ga_concat(&ga, (char_u *)"]"); 4374 } 4375 if (pt != NULL && pt->pt_dict != NULL) 4376 { 4377 typval_T dtv; 4378 4379 ga_concat(&ga, (char_u *)", "); 4380 dtv.v_type = VAR_DICT; 4381 dtv.vval.v_dict = pt->pt_dict; 4382 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID)); 4383 vim_free(tf); 4384 } 4385 ga_concat(&ga, (char_u *)")"); 4386 4387 *tofree = ga.ga_data; 4388 r = *tofree; 4389 break; 4390 } 4391 4392 case VAR_BLOB: 4393 r = blob2string(tv->vval.v_blob, tofree, numbuf); 4394 break; 4395 4396 case VAR_LIST: 4397 if (tv->vval.v_list == NULL) 4398 { 4399 // NULL list is equivalent to empty list. 4400 *tofree = NULL; 4401 r = (char_u *)"[]"; 4402 } 4403 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID 4404 && tv->vval.v_list->lv_len > 0) 4405 { 4406 *tofree = NULL; 4407 r = (char_u *)"[...]"; 4408 } 4409 else 4410 { 4411 int old_copyID = tv->vval.v_list->lv_copyID; 4412 4413 tv->vval.v_list->lv_copyID = copyID; 4414 *tofree = list2string(tv, copyID, restore_copyID); 4415 if (restore_copyID) 4416 tv->vval.v_list->lv_copyID = old_copyID; 4417 r = *tofree; 4418 } 4419 break; 4420 4421 case VAR_DICT: 4422 if (tv->vval.v_dict == NULL) 4423 { 4424 // NULL dict is equivalent to empty dict. 4425 *tofree = NULL; 4426 r = (char_u *)"{}"; 4427 } 4428 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID 4429 && tv->vval.v_dict->dv_hashtab.ht_used != 0) 4430 { 4431 *tofree = NULL; 4432 r = (char_u *)"{...}"; 4433 } 4434 else 4435 { 4436 int old_copyID = tv->vval.v_dict->dv_copyID; 4437 4438 tv->vval.v_dict->dv_copyID = copyID; 4439 *tofree = dict2string(tv, copyID, restore_copyID); 4440 if (restore_copyID) 4441 tv->vval.v_dict->dv_copyID = old_copyID; 4442 r = *tofree; 4443 } 4444 break; 4445 4446 case VAR_NUMBER: 4447 case VAR_UNKNOWN: 4448 case VAR_ANY: 4449 case VAR_VOID: 4450 *tofree = NULL; 4451 r = tv_get_string_buf(tv, numbuf); 4452 break; 4453 4454 case VAR_JOB: 4455 case VAR_CHANNEL: 4456 *tofree = NULL; 4457 r = tv_get_string_buf(tv, numbuf); 4458 if (composite_val) 4459 { 4460 *tofree = string_quote(r, FALSE); 4461 r = *tofree; 4462 } 4463 break; 4464 4465 case VAR_FLOAT: 4466 #ifdef FEAT_FLOAT 4467 *tofree = NULL; 4468 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float); 4469 r = numbuf; 4470 break; 4471 #endif 4472 4473 case VAR_BOOL: 4474 case VAR_SPECIAL: 4475 *tofree = NULL; 4476 r = (char_u *)get_var_special_name(tv->vval.v_number); 4477 break; 4478 } 4479 4480 if (--recurse == 0) 4481 did_echo_string_emsg = FALSE; 4482 return r; 4483 } 4484 4485 /* 4486 * Return a string with the string representation of a variable. 4487 * If the memory is allocated "tofree" is set to it, otherwise NULL. 4488 * "numbuf" is used for a number. 4489 * Does not put quotes around strings, as ":echo" displays values. 4490 * When "copyID" is not NULL replace recursive lists and dicts with "...". 4491 * May return NULL. 4492 */ 4493 char_u * 4494 echo_string( 4495 typval_T *tv, 4496 char_u **tofree, 4497 char_u *numbuf, 4498 int copyID) 4499 { 4500 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE); 4501 } 4502 4503 /* 4504 * Return string "str" in ' quotes, doubling ' characters. 4505 * If "str" is NULL an empty string is assumed. 4506 * If "function" is TRUE make it function('string'). 4507 */ 4508 char_u * 4509 string_quote(char_u *str, int function) 4510 { 4511 unsigned len; 4512 char_u *p, *r, *s; 4513 4514 len = (function ? 13 : 3); 4515 if (str != NULL) 4516 { 4517 len += (unsigned)STRLEN(str); 4518 for (p = str; *p != NUL; MB_PTR_ADV(p)) 4519 if (*p == '\'') 4520 ++len; 4521 } 4522 s = r = alloc(len); 4523 if (r != NULL) 4524 { 4525 if (function) 4526 { 4527 STRCPY(r, "function('"); 4528 r += 10; 4529 } 4530 else 4531 *r++ = '\''; 4532 if (str != NULL) 4533 for (p = str; *p != NUL; ) 4534 { 4535 if (*p == '\'') 4536 *r++ = '\''; 4537 MB_COPY_CHAR(p, r); 4538 } 4539 *r++ = '\''; 4540 if (function) 4541 *r++ = ')'; 4542 *r++ = NUL; 4543 } 4544 return s; 4545 } 4546 4547 #if defined(FEAT_FLOAT) || defined(PROTO) 4548 /* 4549 * Convert the string "text" to a floating point number. 4550 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure 4551 * this always uses a decimal point. 4552 * Returns the length of the text that was consumed. 4553 */ 4554 int 4555 string2float( 4556 char_u *text, 4557 float_T *value) // result stored here 4558 { 4559 char *s = (char *)text; 4560 float_T f; 4561 4562 // MS-Windows does not deal with "inf" and "nan" properly. 4563 if (STRNICMP(text, "inf", 3) == 0) 4564 { 4565 *value = INFINITY; 4566 return 3; 4567 } 4568 if (STRNICMP(text, "-inf", 3) == 0) 4569 { 4570 *value = -INFINITY; 4571 return 4; 4572 } 4573 if (STRNICMP(text, "nan", 3) == 0) 4574 { 4575 *value = NAN; 4576 return 3; 4577 } 4578 f = strtod(s, &s); 4579 *value = f; 4580 return (int)((char_u *)s - text); 4581 } 4582 #endif 4583 4584 /* 4585 * Translate a String variable into a position. 4586 * Returns NULL when there is an error. 4587 */ 4588 pos_T * 4589 var2fpos( 4590 typval_T *varp, 4591 int dollar_lnum, // TRUE when $ is last line 4592 int *fnum) // set to fnum for '0, 'A, etc. 4593 { 4594 char_u *name; 4595 static pos_T pos; 4596 pos_T *pp; 4597 4598 // Argument can be [lnum, col, coladd]. 4599 if (varp->v_type == VAR_LIST) 4600 { 4601 list_T *l; 4602 int len; 4603 int error = FALSE; 4604 listitem_T *li; 4605 4606 l = varp->vval.v_list; 4607 if (l == NULL) 4608 return NULL; 4609 4610 // Get the line number 4611 pos.lnum = list_find_nr(l, 0L, &error); 4612 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count) 4613 return NULL; // invalid line number 4614 4615 // Get the column number 4616 pos.col = list_find_nr(l, 1L, &error); 4617 if (error) 4618 return NULL; 4619 len = (long)STRLEN(ml_get(pos.lnum)); 4620 4621 // We accept "$" for the column number: last column. 4622 li = list_find(l, 1L); 4623 if (li != NULL && li->li_tv.v_type == VAR_STRING 4624 && li->li_tv.vval.v_string != NULL 4625 && STRCMP(li->li_tv.vval.v_string, "$") == 0) 4626 pos.col = len + 1; 4627 4628 // Accept a position up to the NUL after the line. 4629 if (pos.col == 0 || (int)pos.col > len + 1) 4630 return NULL; // invalid column number 4631 --pos.col; 4632 4633 // Get the virtual offset. Defaults to zero. 4634 pos.coladd = list_find_nr(l, 2L, &error); 4635 if (error) 4636 pos.coladd = 0; 4637 4638 return &pos; 4639 } 4640 4641 name = tv_get_string_chk(varp); 4642 if (name == NULL) 4643 return NULL; 4644 if (name[0] == '.') // cursor 4645 return &curwin->w_cursor; 4646 if (name[0] == 'v' && name[1] == NUL) // Visual start 4647 { 4648 if (VIsual_active) 4649 return &VIsual; 4650 return &curwin->w_cursor; 4651 } 4652 if (name[0] == '\'') // mark 4653 { 4654 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum); 4655 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0) 4656 return NULL; 4657 return pp; 4658 } 4659 4660 pos.coladd = 0; 4661 4662 if (name[0] == 'w' && dollar_lnum) 4663 { 4664 pos.col = 0; 4665 if (name[1] == '0') // "w0": first visible line 4666 { 4667 update_topline(); 4668 // In silent Ex mode topline is zero, but that's not a valid line 4669 // number; use one instead. 4670 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1; 4671 return &pos; 4672 } 4673 else if (name[1] == '$') // "w$": last visible line 4674 { 4675 validate_botline(); 4676 // In silent Ex mode botline is zero, return zero then. 4677 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0; 4678 return &pos; 4679 } 4680 } 4681 else if (name[0] == '$') // last column or line 4682 { 4683 if (dollar_lnum) 4684 { 4685 pos.lnum = curbuf->b_ml.ml_line_count; 4686 pos.col = 0; 4687 } 4688 else 4689 { 4690 pos.lnum = curwin->w_cursor.lnum; 4691 pos.col = (colnr_T)STRLEN(ml_get_curline()); 4692 } 4693 return &pos; 4694 } 4695 return NULL; 4696 } 4697 4698 /* 4699 * Convert list in "arg" into a position and optional file number. 4700 * When "fnump" is NULL there is no file number, only 3 items. 4701 * Note that the column is passed on as-is, the caller may want to decrement 4702 * it to use 1 for the first column. 4703 * Return FAIL when conversion is not possible, doesn't check the position for 4704 * validity. 4705 */ 4706 int 4707 list2fpos( 4708 typval_T *arg, 4709 pos_T *posp, 4710 int *fnump, 4711 colnr_T *curswantp) 4712 { 4713 list_T *l = arg->vval.v_list; 4714 long i = 0; 4715 long n; 4716 4717 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only 4718 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional. 4719 if (arg->v_type != VAR_LIST 4720 || l == NULL 4721 || l->lv_len < (fnump == NULL ? 2 : 3) 4722 || l->lv_len > (fnump == NULL ? 4 : 5)) 4723 return FAIL; 4724 4725 if (fnump != NULL) 4726 { 4727 n = list_find_nr(l, i++, NULL); // fnum 4728 if (n < 0) 4729 return FAIL; 4730 if (n == 0) 4731 n = curbuf->b_fnum; // current buffer 4732 *fnump = n; 4733 } 4734 4735 n = list_find_nr(l, i++, NULL); // lnum 4736 if (n < 0) 4737 return FAIL; 4738 posp->lnum = n; 4739 4740 n = list_find_nr(l, i++, NULL); // col 4741 if (n < 0) 4742 return FAIL; 4743 posp->col = n; 4744 4745 n = list_find_nr(l, i, NULL); // off 4746 if (n < 0) 4747 posp->coladd = 0; 4748 else 4749 posp->coladd = n; 4750 4751 if (curswantp != NULL) 4752 *curswantp = list_find_nr(l, i + 1, NULL); // curswant 4753 4754 return OK; 4755 } 4756 4757 /* 4758 * Get the length of an environment variable name. 4759 * Advance "arg" to the first character after the name. 4760 * Return 0 for error. 4761 */ 4762 int 4763 get_env_len(char_u **arg) 4764 { 4765 char_u *p; 4766 int len; 4767 4768 for (p = *arg; vim_isIDc(*p); ++p) 4769 ; 4770 if (p == *arg) // no name found 4771 return 0; 4772 4773 len = (int)(p - *arg); 4774 *arg = p; 4775 return len; 4776 } 4777 4778 /* 4779 * Get the length of the name of a function or internal variable. 4780 * "arg" is advanced to the first non-white character after the name. 4781 * Return 0 if something is wrong. 4782 */ 4783 int 4784 get_id_len(char_u **arg) 4785 { 4786 char_u *p; 4787 int len; 4788 4789 // Find the end of the name. 4790 for (p = *arg; eval_isnamec(*p); ++p) 4791 { 4792 if (*p == ':') 4793 { 4794 // "s:" is start of "s:var", but "n:" is not and can be used in 4795 // slice "[n:]". Also "xx:" is not a namespace. 4796 len = (int)(p - *arg); 4797 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL) 4798 || len > 1) 4799 break; 4800 } 4801 } 4802 if (p == *arg) // no name found 4803 return 0; 4804 4805 len = (int)(p - *arg); 4806 *arg = skipwhite(p); 4807 4808 return len; 4809 } 4810 4811 /* 4812 * Get the length of the name of a variable or function. 4813 * Only the name is recognized, does not handle ".key" or "[idx]". 4814 * "arg" is advanced to the first non-white character after the name. 4815 * Return -1 if curly braces expansion failed. 4816 * Return 0 if something else is wrong. 4817 * If the name contains 'magic' {}'s, expand them and return the 4818 * expanded name in an allocated string via 'alias' - caller must free. 4819 */ 4820 int 4821 get_name_len( 4822 char_u **arg, 4823 char_u **alias, 4824 int evaluate, 4825 int verbose) 4826 { 4827 int len; 4828 char_u *p; 4829 char_u *expr_start; 4830 char_u *expr_end; 4831 4832 *alias = NULL; // default to no alias 4833 4834 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA 4835 && (*arg)[2] == (int)KE_SNR) 4836 { 4837 // hard coded <SNR>, already translated 4838 *arg += 3; 4839 return get_id_len(arg) + 3; 4840 } 4841 len = eval_fname_script(*arg); 4842 if (len > 0) 4843 { 4844 // literal "<SID>", "s:" or "<SNR>" 4845 *arg += len; 4846 } 4847 4848 /* 4849 * Find the end of the name; check for {} construction. 4850 */ 4851 p = find_name_end(*arg, &expr_start, &expr_end, 4852 len > 0 ? 0 : FNE_CHECK_START); 4853 if (expr_start != NULL) 4854 { 4855 char_u *temp_string; 4856 4857 if (!evaluate) 4858 { 4859 len += (int)(p - *arg); 4860 *arg = skipwhite(p); 4861 return len; 4862 } 4863 4864 /* 4865 * Include any <SID> etc in the expanded string: 4866 * Thus the -len here. 4867 */ 4868 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p); 4869 if (temp_string == NULL) 4870 return -1; 4871 *alias = temp_string; 4872 *arg = skipwhite(p); 4873 return (int)STRLEN(temp_string); 4874 } 4875 4876 len += get_id_len(arg); 4877 // Only give an error when there is something, otherwise it will be 4878 // reported at a higher level. 4879 if (len == 0 && verbose && **arg != NUL) 4880 semsg(_(e_invexpr2), *arg); 4881 4882 return len; 4883 } 4884 4885 /* 4886 * Find the end of a variable or function name, taking care of magic braces. 4887 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the 4888 * start and end of the first magic braces item. 4889 * "flags" can have FNE_INCL_BR and FNE_CHECK_START. 4890 * Return a pointer to just after the name. Equal to "arg" if there is no 4891 * valid name. 4892 */ 4893 char_u * 4894 find_name_end( 4895 char_u *arg, 4896 char_u **expr_start, 4897 char_u **expr_end, 4898 int flags) 4899 { 4900 int mb_nest = 0; 4901 int br_nest = 0; 4902 char_u *p; 4903 int len; 4904 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9; 4905 4906 if (expr_start != NULL) 4907 { 4908 *expr_start = NULL; 4909 *expr_end = NULL; 4910 } 4911 4912 // Quick check for valid starting character. 4913 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) 4914 && (*arg != '{' || vim9script)) 4915 return arg; 4916 4917 for (p = arg; *p != NUL 4918 && (eval_isnamec(*p) 4919 || (*p == '{' && !vim9script) 4920 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.')) 4921 || mb_nest != 0 4922 || br_nest != 0); MB_PTR_ADV(p)) 4923 { 4924 if (*p == '\'') 4925 { 4926 // skip over 'string' to avoid counting [ and ] inside it. 4927 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p)) 4928 ; 4929 if (*p == NUL) 4930 break; 4931 } 4932 else if (*p == '"') 4933 { 4934 // skip over "str\"ing" to avoid counting [ and ] inside it. 4935 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p)) 4936 if (*p == '\\' && p[1] != NUL) 4937 ++p; 4938 if (*p == NUL) 4939 break; 4940 } 4941 else if (br_nest == 0 && mb_nest == 0 && *p == ':') 4942 { 4943 // "s:" is start of "s:var", but "n:" is not and can be used in 4944 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. 4945 len = (int)(p - arg); 4946 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL) 4947 || (len > 1 && p[-1] != '}')) 4948 break; 4949 } 4950 4951 if (mb_nest == 0) 4952 { 4953 if (*p == '[') 4954 ++br_nest; 4955 else if (*p == ']') 4956 --br_nest; 4957 } 4958 4959 if (br_nest == 0 && !vim9script) 4960 { 4961 if (*p == '{') 4962 { 4963 mb_nest++; 4964 if (expr_start != NULL && *expr_start == NULL) 4965 *expr_start = p; 4966 } 4967 else if (*p == '}') 4968 { 4969 mb_nest--; 4970 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL) 4971 *expr_end = p; 4972 } 4973 } 4974 } 4975 4976 return p; 4977 } 4978 4979 /* 4980 * Expands out the 'magic' {}'s in a variable/function name. 4981 * Note that this can call itself recursively, to deal with 4982 * constructs like foo{bar}{baz}{bam} 4983 * The four pointer arguments point to "foo{expre}ss{ion}bar" 4984 * "in_start" ^ 4985 * "expr_start" ^ 4986 * "expr_end" ^ 4987 * "in_end" ^ 4988 * 4989 * Returns a new allocated string, which the caller must free. 4990 * Returns NULL for failure. 4991 */ 4992 static char_u * 4993 make_expanded_name( 4994 char_u *in_start, 4995 char_u *expr_start, 4996 char_u *expr_end, 4997 char_u *in_end) 4998 { 4999 char_u c1; 5000 char_u *retval = NULL; 5001 char_u *temp_result; 5002 5003 if (expr_end == NULL || in_end == NULL) 5004 return NULL; 5005 *expr_start = NUL; 5006 *expr_end = NUL; 5007 c1 = *in_end; 5008 *in_end = NUL; 5009 5010 temp_result = eval_to_string(expr_start + 1, FALSE); 5011 if (temp_result != NULL) 5012 { 5013 retval = alloc(STRLEN(temp_result) + (expr_start - in_start) 5014 + (in_end - expr_end) + 1); 5015 if (retval != NULL) 5016 { 5017 STRCPY(retval, in_start); 5018 STRCAT(retval, temp_result); 5019 STRCAT(retval, expr_end + 1); 5020 } 5021 } 5022 vim_free(temp_result); 5023 5024 *in_end = c1; // put char back for error messages 5025 *expr_start = '{'; 5026 *expr_end = '}'; 5027 5028 if (retval != NULL) 5029 { 5030 temp_result = find_name_end(retval, &expr_start, &expr_end, 0); 5031 if (expr_start != NULL) 5032 { 5033 // Further expansion! 5034 temp_result = make_expanded_name(retval, expr_start, 5035 expr_end, temp_result); 5036 vim_free(retval); 5037 retval = temp_result; 5038 } 5039 } 5040 5041 return retval; 5042 } 5043 5044 /* 5045 * Return TRUE if character "c" can be used in a variable or function name. 5046 * Does not include '{' or '}' for magic braces. 5047 */ 5048 int 5049 eval_isnamec(int c) 5050 { 5051 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR); 5052 } 5053 5054 /* 5055 * Return TRUE if character "c" can be used as the first character in a 5056 * variable or function name (excluding '{' and '}'). 5057 */ 5058 int 5059 eval_isnamec1(int c) 5060 { 5061 return (ASCII_ISALPHA(c) || c == '_'); 5062 } 5063 5064 /* 5065 * Handle: 5066 * - expr[expr], expr[expr:expr] subscript 5067 * - ".name" lookup 5068 * - function call with Funcref variable: func(expr) 5069 * - method call: var->method() 5070 * 5071 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len() 5072 */ 5073 int 5074 handle_subscript( 5075 char_u **arg, 5076 typval_T *rettv, 5077 evalarg_T *evalarg, 5078 int verbose) // give error messages 5079 { 5080 int evaluate = evalarg != NULL 5081 && (evalarg->eval_flags & EVAL_EVALUATE); 5082 int ret = OK; 5083 dict_T *selfdict = NULL; 5084 int check_white = TRUE; 5085 int getnext; 5086 char_u *p; 5087 5088 while (ret == OK) 5089 { 5090 // When at the end of the line and ".name" or "->{" or "->X" follows in 5091 // the next line then consume the line break. 5092 p = eval_next_non_blank(*arg, evalarg, &getnext); 5093 if (getnext 5094 && ((rettv->v_type == VAR_DICT && *p == '.' 5095 && ASCII_ISALPHA(p[1])) 5096 || (*p == '-' && p[1] == '>' 5097 && (p[2] == '{' || ASCII_ISALPHA(p[2]))))) 5098 { 5099 *arg = eval_next_line(evalarg); 5100 check_white = FALSE; 5101 } 5102 5103 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC 5104 || rettv->v_type == VAR_PARTIAL)) 5105 && (!check_white || !VIM_ISWHITE(*(*arg - 1)))) 5106 { 5107 ret = call_func_rettv(arg, evalarg, rettv, evaluate, 5108 selfdict, NULL); 5109 5110 // Stop the expression evaluation when immediately aborting on 5111 // error, or when an interrupt occurred or an exception was thrown 5112 // but not caught. 5113 if (aborting()) 5114 { 5115 if (ret == OK) 5116 clear_tv(rettv); 5117 ret = FAIL; 5118 } 5119 dict_unref(selfdict); 5120 selfdict = NULL; 5121 } 5122 else if (**arg == '-' && (*arg)[1] == '>') 5123 { 5124 if (ret == OK) 5125 { 5126 if ((*arg)[2] == '{') 5127 // expr->{lambda}() 5128 ret = eval_lambda(arg, rettv, evalarg, verbose); 5129 else 5130 // expr->name() 5131 ret = eval_method(arg, rettv, evalarg, verbose); 5132 } 5133 } 5134 // "." is ".name" lookup when we found a dict or when evaluating and 5135 // scriptversion is at least 2, where string concatenation is "..". 5136 else if (**arg == '[' 5137 || (**arg == '.' && (rettv->v_type == VAR_DICT 5138 || (!evaluate 5139 && (*arg)[1] != '.' 5140 && current_sctx.sc_version >= 2)))) 5141 { 5142 dict_unref(selfdict); 5143 if (rettv->v_type == VAR_DICT) 5144 { 5145 selfdict = rettv->vval.v_dict; 5146 if (selfdict != NULL) 5147 ++selfdict->dv_refcount; 5148 } 5149 else 5150 selfdict = NULL; 5151 if (eval_index(arg, rettv, evalarg, verbose) == FAIL) 5152 { 5153 clear_tv(rettv); 5154 ret = FAIL; 5155 } 5156 } 5157 else 5158 break; 5159 } 5160 5161 // Turn "dict.Func" into a partial for "Func" bound to "dict". 5162 // Don't do this when "Func" is already a partial that was bound 5163 // explicitly (pt_auto is FALSE). 5164 if (selfdict != NULL 5165 && (rettv->v_type == VAR_FUNC 5166 || (rettv->v_type == VAR_PARTIAL 5167 && (rettv->vval.v_partial->pt_auto 5168 || rettv->vval.v_partial->pt_dict == NULL)))) 5169 selfdict = make_partial(selfdict, rettv); 5170 5171 dict_unref(selfdict); 5172 return ret; 5173 } 5174 5175 /* 5176 * Make a copy of an item. 5177 * Lists and Dictionaries are also copied. A deep copy if "deep" is set. 5178 * For deepcopy() "copyID" is zero for a full copy or the ID for when a 5179 * reference to an already copied list/dict can be used. 5180 * Returns FAIL or OK. 5181 */ 5182 int 5183 item_copy( 5184 typval_T *from, 5185 typval_T *to, 5186 int deep, 5187 int copyID) 5188 { 5189 static int recurse = 0; 5190 int ret = OK; 5191 5192 if (recurse >= DICT_MAXNEST) 5193 { 5194 emsg(_("E698: variable nested too deep for making a copy")); 5195 return FAIL; 5196 } 5197 ++recurse; 5198 5199 switch (from->v_type) 5200 { 5201 case VAR_NUMBER: 5202 case VAR_FLOAT: 5203 case VAR_STRING: 5204 case VAR_FUNC: 5205 case VAR_PARTIAL: 5206 case VAR_BOOL: 5207 case VAR_SPECIAL: 5208 case VAR_JOB: 5209 case VAR_CHANNEL: 5210 copy_tv(from, to); 5211 break; 5212 case VAR_LIST: 5213 to->v_type = VAR_LIST; 5214 to->v_lock = 0; 5215 if (from->vval.v_list == NULL) 5216 to->vval.v_list = NULL; 5217 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID) 5218 { 5219 // use the copy made earlier 5220 to->vval.v_list = from->vval.v_list->lv_copylist; 5221 ++to->vval.v_list->lv_refcount; 5222 } 5223 else 5224 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID); 5225 if (to->vval.v_list == NULL) 5226 ret = FAIL; 5227 break; 5228 case VAR_BLOB: 5229 ret = blob_copy(from->vval.v_blob, to); 5230 break; 5231 case VAR_DICT: 5232 to->v_type = VAR_DICT; 5233 to->v_lock = 0; 5234 if (from->vval.v_dict == NULL) 5235 to->vval.v_dict = NULL; 5236 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID) 5237 { 5238 // use the copy made earlier 5239 to->vval.v_dict = from->vval.v_dict->dv_copydict; 5240 ++to->vval.v_dict->dv_refcount; 5241 } 5242 else 5243 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID); 5244 if (to->vval.v_dict == NULL) 5245 ret = FAIL; 5246 break; 5247 case VAR_UNKNOWN: 5248 case VAR_ANY: 5249 case VAR_VOID: 5250 internal_error_no_abort("item_copy(UNKNOWN)"); 5251 ret = FAIL; 5252 } 5253 --recurse; 5254 return ret; 5255 } 5256 5257 void 5258 echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr) 5259 { 5260 char_u *tofree; 5261 char_u numbuf[NUMBUFLEN]; 5262 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID()); 5263 5264 if (*atstart) 5265 { 5266 *atstart = FALSE; 5267 // Call msg_start() after eval1(), evaluating the expression 5268 // may cause a message to appear. 5269 if (with_space) 5270 { 5271 // Mark the saved text as finishing the line, so that what 5272 // follows is displayed on a new line when scrolling back 5273 // at the more prompt. 5274 msg_sb_eol(); 5275 msg_start(); 5276 } 5277 } 5278 else if (with_space) 5279 msg_puts_attr(" ", echo_attr); 5280 5281 if (p != NULL) 5282 for ( ; *p != NUL && !got_int; ++p) 5283 { 5284 if (*p == '\n' || *p == '\r' || *p == TAB) 5285 { 5286 if (*p != TAB && *needclr) 5287 { 5288 // remove any text still there from the command 5289 msg_clr_eos(); 5290 *needclr = FALSE; 5291 } 5292 msg_putchar_attr(*p, echo_attr); 5293 } 5294 else 5295 { 5296 if (has_mbyte) 5297 { 5298 int i = (*mb_ptr2len)(p); 5299 5300 (void)msg_outtrans_len_attr(p, i, echo_attr); 5301 p += i - 1; 5302 } 5303 else 5304 (void)msg_outtrans_len_attr(p, 1, echo_attr); 5305 } 5306 } 5307 vim_free(tofree); 5308 } 5309 5310 /* 5311 * ":echo expr1 ..." print each argument separated with a space, add a 5312 * newline at the end. 5313 * ":echon expr1 ..." print each argument plain. 5314 */ 5315 void 5316 ex_echo(exarg_T *eap) 5317 { 5318 char_u *arg = eap->arg; 5319 typval_T rettv; 5320 char_u *p; 5321 int needclr = TRUE; 5322 int atstart = TRUE; 5323 int did_emsg_before = did_emsg; 5324 int called_emsg_before = called_emsg; 5325 evalarg_T evalarg; 5326 5327 fill_evalarg_from_eap(&evalarg, eap, eap->skip); 5328 5329 if (eap->skip) 5330 ++emsg_skip; 5331 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int) 5332 { 5333 // If eval1() causes an error message the text from the command may 5334 // still need to be cleared. E.g., "echo 22,44". 5335 need_clr_eos = needclr; 5336 5337 p = arg; 5338 if (eval1(&arg, &rettv, &evalarg) == FAIL) 5339 { 5340 /* 5341 * Report the invalid expression unless the expression evaluation 5342 * has been cancelled due to an aborting error, an interrupt, or an 5343 * exception. 5344 */ 5345 if (!aborting() && did_emsg == did_emsg_before 5346 && called_emsg == called_emsg_before) 5347 semsg(_(e_invexpr2), p); 5348 need_clr_eos = FALSE; 5349 break; 5350 } 5351 need_clr_eos = FALSE; 5352 5353 if (!eap->skip) 5354 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr); 5355 5356 clear_tv(&rettv); 5357 arg = skipwhite(arg); 5358 } 5359 eap->nextcmd = check_nextcmd(arg); 5360 clear_evalarg(&evalarg, eap); 5361 5362 if (eap->skip) 5363 --emsg_skip; 5364 else 5365 { 5366 // remove text that may still be there from the command 5367 if (needclr) 5368 msg_clr_eos(); 5369 if (eap->cmdidx == CMD_echo) 5370 msg_end(); 5371 } 5372 } 5373 5374 /* 5375 * ":echohl {name}". 5376 */ 5377 void 5378 ex_echohl(exarg_T *eap) 5379 { 5380 echo_attr = syn_name2attr(eap->arg); 5381 } 5382 5383 /* 5384 * Returns the :echo attribute 5385 */ 5386 int 5387 get_echo_attr(void) 5388 { 5389 return echo_attr; 5390 } 5391 5392 /* 5393 * ":execute expr1 ..." execute the result of an expression. 5394 * ":echomsg expr1 ..." Print a message 5395 * ":echoerr expr1 ..." Print an error 5396 * Each gets spaces around each argument and a newline at the end for 5397 * echo commands 5398 */ 5399 void 5400 ex_execute(exarg_T *eap) 5401 { 5402 char_u *arg = eap->arg; 5403 typval_T rettv; 5404 int ret = OK; 5405 char_u *p; 5406 garray_T ga; 5407 int len; 5408 5409 ga_init2(&ga, 1, 80); 5410 5411 if (eap->skip) 5412 ++emsg_skip; 5413 while (!ends_excmd2(eap->cmd, arg) || *arg == '"') 5414 { 5415 ret = eval1_emsg(&arg, &rettv, eap); 5416 if (ret == FAIL) 5417 break; 5418 5419 if (!eap->skip) 5420 { 5421 char_u buf[NUMBUFLEN]; 5422 5423 if (eap->cmdidx == CMD_execute) 5424 { 5425 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB) 5426 { 5427 emsg(_(e_inval_string)); 5428 p = NULL; 5429 } 5430 else 5431 p = tv_get_string_buf(&rettv, buf); 5432 } 5433 else 5434 p = tv_stringify(&rettv, buf); 5435 if (p == NULL) 5436 { 5437 clear_tv(&rettv); 5438 ret = FAIL; 5439 break; 5440 } 5441 len = (int)STRLEN(p); 5442 if (ga_grow(&ga, len + 2) == FAIL) 5443 { 5444 clear_tv(&rettv); 5445 ret = FAIL; 5446 break; 5447 } 5448 if (ga.ga_len) 5449 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' '; 5450 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p); 5451 ga.ga_len += len; 5452 } 5453 5454 clear_tv(&rettv); 5455 arg = skipwhite(arg); 5456 } 5457 5458 if (ret != FAIL && ga.ga_data != NULL) 5459 { 5460 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr) 5461 { 5462 // Mark the already saved text as finishing the line, so that what 5463 // follows is displayed on a new line when scrolling back at the 5464 // more prompt. 5465 msg_sb_eol(); 5466 } 5467 5468 if (eap->cmdidx == CMD_echomsg) 5469 { 5470 msg_attr(ga.ga_data, echo_attr); 5471 out_flush(); 5472 } 5473 else if (eap->cmdidx == CMD_echoerr) 5474 { 5475 int save_did_emsg = did_emsg; 5476 5477 // We don't want to abort following commands, restore did_emsg. 5478 emsg(ga.ga_data); 5479 if (!force_abort) 5480 did_emsg = save_did_emsg; 5481 } 5482 else if (eap->cmdidx == CMD_execute) 5483 do_cmdline((char_u *)ga.ga_data, 5484 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE); 5485 } 5486 5487 ga_clear(&ga); 5488 5489 if (eap->skip) 5490 --emsg_skip; 5491 5492 eap->nextcmd = check_nextcmd(arg); 5493 } 5494 5495 /* 5496 * Skip over the name of an option: "&option", "&g:option" or "&l:option". 5497 * "arg" points to the "&" or '+' when called, to "option" when returning. 5498 * Returns NULL when no option name found. Otherwise pointer to the char 5499 * after the option name. 5500 */ 5501 char_u * 5502 find_option_end(char_u **arg, int *opt_flags) 5503 { 5504 char_u *p = *arg; 5505 5506 ++p; 5507 if (*p == 'g' && p[1] == ':') 5508 { 5509 *opt_flags = OPT_GLOBAL; 5510 p += 2; 5511 } 5512 else if (*p == 'l' && p[1] == ':') 5513 { 5514 *opt_flags = OPT_LOCAL; 5515 p += 2; 5516 } 5517 else 5518 *opt_flags = 0; 5519 5520 if (!ASCII_ISALPHA(*p)) 5521 return NULL; 5522 *arg = p; 5523 5524 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL) 5525 p += 4; // termcap option 5526 else 5527 while (ASCII_ISALPHA(*p)) 5528 ++p; 5529 return p; 5530 } 5531 5532 /* 5533 * Display script name where an item was last set. 5534 * Should only be invoked when 'verbose' is non-zero. 5535 */ 5536 void 5537 last_set_msg(sctx_T script_ctx) 5538 { 5539 char_u *p; 5540 5541 if (script_ctx.sc_sid != 0) 5542 { 5543 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid)); 5544 if (p != NULL) 5545 { 5546 verbose_enter(); 5547 msg_puts(_("\n\tLast set from ")); 5548 msg_puts((char *)p); 5549 if (script_ctx.sc_lnum > 0) 5550 { 5551 msg_puts(_(line_msg)); 5552 msg_outnum((long)script_ctx.sc_lnum); 5553 } 5554 verbose_leave(); 5555 vim_free(p); 5556 } 5557 } 5558 } 5559 5560 #endif // FEAT_EVAL 5561 5562 /* 5563 * Perform a substitution on "str" with pattern "pat" and substitute "sub". 5564 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL. 5565 * "flags" can be "g" to do a global substitute. 5566 * Returns an allocated string, NULL for error. 5567 */ 5568 char_u * 5569 do_string_sub( 5570 char_u *str, 5571 char_u *pat, 5572 char_u *sub, 5573 typval_T *expr, 5574 char_u *flags) 5575 { 5576 int sublen; 5577 regmatch_T regmatch; 5578 int i; 5579 int do_all; 5580 char_u *tail; 5581 char_u *end; 5582 garray_T ga; 5583 char_u *ret; 5584 char_u *save_cpo; 5585 char_u *zero_width = NULL; 5586 5587 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here 5588 save_cpo = p_cpo; 5589 p_cpo = empty_option; 5590 5591 ga_init2(&ga, 1, 200); 5592 5593 do_all = (flags[0] == 'g'); 5594 5595 regmatch.rm_ic = p_ic; 5596 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING); 5597 if (regmatch.regprog != NULL) 5598 { 5599 tail = str; 5600 end = str + STRLEN(str); 5601 while (vim_regexec_nl(®match, str, (colnr_T)(tail - str))) 5602 { 5603 // Skip empty match except for first match. 5604 if (regmatch.startp[0] == regmatch.endp[0]) 5605 { 5606 if (zero_width == regmatch.startp[0]) 5607 { 5608 // avoid getting stuck on a match with an empty string 5609 i = mb_ptr2len(tail); 5610 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, 5611 (size_t)i); 5612 ga.ga_len += i; 5613 tail += i; 5614 continue; 5615 } 5616 zero_width = regmatch.startp[0]; 5617 } 5618 5619 /* 5620 * Get some space for a temporary buffer to do the substitution 5621 * into. It will contain: 5622 * - The text up to where the match is. 5623 * - The substituted text. 5624 * - The text after the match. 5625 */ 5626 sublen = vim_regsub(®match, sub, expr, tail, FALSE, TRUE, FALSE); 5627 if (ga_grow(&ga, (int)((end - tail) + sublen - 5628 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL) 5629 { 5630 ga_clear(&ga); 5631 break; 5632 } 5633 5634 // copy the text up to where the match is 5635 i = (int)(regmatch.startp[0] - tail); 5636 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i); 5637 // add the substituted text 5638 (void)vim_regsub(®match, sub, expr, (char_u *)ga.ga_data 5639 + ga.ga_len + i, TRUE, TRUE, FALSE); 5640 ga.ga_len += i + sublen - 1; 5641 tail = regmatch.endp[0]; 5642 if (*tail == NUL) 5643 break; 5644 if (!do_all) 5645 break; 5646 } 5647 5648 if (ga.ga_data != NULL) 5649 STRCPY((char *)ga.ga_data + ga.ga_len, tail); 5650 5651 vim_regfree(regmatch.regprog); 5652 } 5653 5654 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data); 5655 ga_clear(&ga); 5656 if (p_cpo == empty_option) 5657 p_cpo = save_cpo; 5658 else 5659 // Darn, evaluating {sub} expression or {expr} changed the value. 5660 free_string_option(save_cpo); 5661 5662 return ret; 5663 } 5664