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