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