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