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 p = eval_next_non_blank(*arg, evalarg, &getnext); 2773 op = *p; 2774 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2); 2775 if (op != '+' && op != '-' && !concat) 2776 break; 2777 2778 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE); 2779 oplen = (concat && p[1] == '.') ? 2 : 1; 2780 if (getnext) 2781 *arg = eval_next_line(evalarg); 2782 else 2783 { 2784 if (evaluate && vim9script && !VIM_ISWHITE(**arg)) 2785 { 2786 error_white_both(p, oplen); 2787 clear_tv(rettv); 2788 return FAIL; 2789 } 2790 *arg = p; 2791 } 2792 if ((op != '+' || (rettv->v_type != VAR_LIST 2793 && rettv->v_type != VAR_BLOB)) 2794 #ifdef FEAT_FLOAT 2795 && (op == '.' || rettv->v_type != VAR_FLOAT) 2796 #endif 2797 && evaluate) 2798 { 2799 int error = FALSE; 2800 2801 // For "list + ...", an illegal use of the first operand as 2802 // a number cannot be determined before evaluating the 2nd 2803 // operand: if this is also a list, all is ok. 2804 // For "something . ...", "something - ..." or "non-list + ...", 2805 // we know that the first operand needs to be a string or number 2806 // without evaluating the 2nd operand. So check before to avoid 2807 // side effects after an error. 2808 if (op != '.') 2809 tv_get_number_chk(rettv, &error); 2810 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error) 2811 { 2812 clear_tv(rettv); 2813 return FAIL; 2814 } 2815 } 2816 2817 /* 2818 * Get the second variable. 2819 */ 2820 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen])) 2821 { 2822 error_white_both(p, oplen); 2823 clear_tv(rettv); 2824 return FAIL; 2825 } 2826 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg); 2827 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL) 2828 { 2829 clear_tv(rettv); 2830 return FAIL; 2831 } 2832 2833 if (evaluate) 2834 { 2835 /* 2836 * Compute the result. 2837 */ 2838 if (op == '.') 2839 { 2840 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN]; 2841 char_u *s1 = tv_get_string_buf(rettv, buf1); 2842 char_u *s2 = NULL; 2843 2844 if (vim9script && (var2.v_type == VAR_VOID 2845 || var2.v_type == VAR_CHANNEL 2846 || var2.v_type == VAR_JOB)) 2847 emsg(_(e_inval_string)); 2848 #ifdef FEAT_FLOAT 2849 else if (vim9script && var2.v_type == VAR_FLOAT) 2850 { 2851 vim_snprintf((char *)buf2, NUMBUFLEN, "%g", 2852 var2.vval.v_float); 2853 s2 = buf2; 2854 } 2855 #endif 2856 else 2857 s2 = tv_get_string_buf_chk(&var2, buf2); 2858 if (s2 == NULL) // type error ? 2859 { 2860 clear_tv(rettv); 2861 clear_tv(&var2); 2862 return FAIL; 2863 } 2864 p = concat_str(s1, s2); 2865 clear_tv(rettv); 2866 rettv->v_type = VAR_STRING; 2867 rettv->vval.v_string = p; 2868 } 2869 else if (op == '+' && rettv->v_type == VAR_BLOB 2870 && var2.v_type == VAR_BLOB) 2871 eval_addblob(rettv, &var2); 2872 else if (op == '+' && rettv->v_type == VAR_LIST 2873 && var2.v_type == VAR_LIST) 2874 { 2875 if (eval_addlist(rettv, &var2) == FAIL) 2876 return FAIL; 2877 } 2878 else 2879 { 2880 int error = FALSE; 2881 varnumber_T n1, n2; 2882 #ifdef FEAT_FLOAT 2883 float_T f1 = 0, f2 = 0; 2884 2885 if (rettv->v_type == VAR_FLOAT) 2886 { 2887 f1 = rettv->vval.v_float; 2888 n1 = 0; 2889 } 2890 else 2891 #endif 2892 { 2893 n1 = tv_get_number_chk(rettv, &error); 2894 if (error) 2895 { 2896 // This can only happen for "list + non-list". For 2897 // "non-list + ..." or "something - ...", we returned 2898 // before evaluating the 2nd operand. 2899 clear_tv(rettv); 2900 return FAIL; 2901 } 2902 #ifdef FEAT_FLOAT 2903 if (var2.v_type == VAR_FLOAT) 2904 f1 = n1; 2905 #endif 2906 } 2907 #ifdef FEAT_FLOAT 2908 if (var2.v_type == VAR_FLOAT) 2909 { 2910 f2 = var2.vval.v_float; 2911 n2 = 0; 2912 } 2913 else 2914 #endif 2915 { 2916 n2 = tv_get_number_chk(&var2, &error); 2917 if (error) 2918 { 2919 clear_tv(rettv); 2920 clear_tv(&var2); 2921 return FAIL; 2922 } 2923 #ifdef FEAT_FLOAT 2924 if (rettv->v_type == VAR_FLOAT) 2925 f2 = n2; 2926 #endif 2927 } 2928 clear_tv(rettv); 2929 2930 #ifdef FEAT_FLOAT 2931 // If there is a float on either side the result is a float. 2932 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT) 2933 { 2934 if (op == '+') 2935 f1 = f1 + f2; 2936 else 2937 f1 = f1 - f2; 2938 rettv->v_type = VAR_FLOAT; 2939 rettv->vval.v_float = f1; 2940 } 2941 else 2942 #endif 2943 { 2944 if (op == '+') 2945 n1 = n1 + n2; 2946 else 2947 n1 = n1 - n2; 2948 rettv->v_type = VAR_NUMBER; 2949 rettv->vval.v_number = n1; 2950 } 2951 } 2952 clear_tv(&var2); 2953 } 2954 } 2955 return OK; 2956 } 2957 2958 /* 2959 * Handle fifth level expression: 2960 * * number multiplication 2961 * / number division 2962 * % number modulo 2963 * 2964 * "arg" must point to the first non-white of the expression. 2965 * "arg" is advanced to just after the recognized expression. 2966 * 2967 * Return OK or FAIL. 2968 */ 2969 static int 2970 eval6( 2971 char_u **arg, 2972 typval_T *rettv, 2973 evalarg_T *evalarg, 2974 int want_string) // after "." operator 2975 { 2976 #ifdef FEAT_FLOAT 2977 int use_float = FALSE; 2978 #endif 2979 2980 /* 2981 * Get the first variable. 2982 */ 2983 if (eval7(arg, rettv, evalarg, want_string) == FAIL) 2984 return FAIL; 2985 2986 /* 2987 * Repeat computing, until no '*', '/' or '%' is following. 2988 */ 2989 for (;;) 2990 { 2991 int evaluate; 2992 int getnext; 2993 typval_T var2; 2994 char_u *p; 2995 int op; 2996 varnumber_T n1, n2; 2997 #ifdef FEAT_FLOAT 2998 float_T f1, f2; 2999 #endif 3000 int error; 3001 3002 p = eval_next_non_blank(*arg, evalarg, &getnext); 3003 op = *p; 3004 if (op != '*' && op != '/' && op != '%') 3005 break; 3006 3007 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE); 3008 if (getnext) 3009 *arg = eval_next_line(evalarg); 3010 else 3011 { 3012 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg)) 3013 { 3014 error_white_both(p, 1); 3015 clear_tv(rettv); 3016 return FAIL; 3017 } 3018 *arg = p; 3019 } 3020 3021 #ifdef FEAT_FLOAT 3022 f1 = 0; 3023 f2 = 0; 3024 #endif 3025 error = FALSE; 3026 if (evaluate) 3027 { 3028 #ifdef FEAT_FLOAT 3029 if (rettv->v_type == VAR_FLOAT) 3030 { 3031 f1 = rettv->vval.v_float; 3032 use_float = TRUE; 3033 n1 = 0; 3034 } 3035 else 3036 #endif 3037 n1 = tv_get_number_chk(rettv, &error); 3038 clear_tv(rettv); 3039 if (error) 3040 return FAIL; 3041 } 3042 else 3043 n1 = 0; 3044 3045 /* 3046 * Get the second variable. 3047 */ 3048 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1])) 3049 { 3050 error_white_both(p, 1); 3051 clear_tv(rettv); 3052 return FAIL; 3053 } 3054 *arg = skipwhite_and_linebreak(*arg + 1, evalarg); 3055 if (eval7(arg, &var2, evalarg, FALSE) == FAIL) 3056 return FAIL; 3057 3058 if (evaluate) 3059 { 3060 #ifdef FEAT_FLOAT 3061 if (var2.v_type == VAR_FLOAT) 3062 { 3063 if (!use_float) 3064 { 3065 f1 = n1; 3066 use_float = TRUE; 3067 } 3068 f2 = var2.vval.v_float; 3069 n2 = 0; 3070 } 3071 else 3072 #endif 3073 { 3074 n2 = tv_get_number_chk(&var2, &error); 3075 clear_tv(&var2); 3076 if (error) 3077 return FAIL; 3078 #ifdef FEAT_FLOAT 3079 if (use_float) 3080 f2 = n2; 3081 #endif 3082 } 3083 3084 /* 3085 * Compute the result. 3086 * When either side is a float the result is a float. 3087 */ 3088 #ifdef FEAT_FLOAT 3089 if (use_float) 3090 { 3091 if (op == '*') 3092 f1 = f1 * f2; 3093 else if (op == '/') 3094 { 3095 # ifdef VMS 3096 // VMS crashes on divide by zero, work around it 3097 if (f2 == 0.0) 3098 { 3099 if (f1 == 0) 3100 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN 3101 else if (f1 < 0) 3102 f1 = -1 * __F_FLT_MAX; 3103 else 3104 f1 = __F_FLT_MAX; 3105 } 3106 else 3107 f1 = f1 / f2; 3108 # else 3109 // We rely on the floating point library to handle divide 3110 // by zero to result in "inf" and not a crash. 3111 f1 = f1 / f2; 3112 # endif 3113 } 3114 else 3115 { 3116 emsg(_(e_modulus)); 3117 return FAIL; 3118 } 3119 rettv->v_type = VAR_FLOAT; 3120 rettv->vval.v_float = f1; 3121 } 3122 else 3123 #endif 3124 { 3125 if (op == '*') 3126 n1 = n1 * n2; 3127 else if (op == '/') 3128 n1 = num_divide(n1, n2); 3129 else 3130 n1 = num_modulus(n1, n2); 3131 3132 rettv->v_type = VAR_NUMBER; 3133 rettv->vval.v_number = n1; 3134 } 3135 } 3136 } 3137 3138 return OK; 3139 } 3140 3141 /* 3142 * Handle sixth level expression: 3143 * number number constant 3144 * 0zFFFFFFFF Blob constant 3145 * "string" string constant 3146 * 'string' literal string constant 3147 * &option-name option value 3148 * @r register contents 3149 * identifier variable value 3150 * function() function call 3151 * $VAR environment variable 3152 * (expression) nested expression 3153 * [expr, expr] List 3154 * {arg, arg -> expr} Lambda 3155 * {key: val, key: val} Dictionary 3156 * #{key: val, key: val} Dictionary with literal keys 3157 * 3158 * Also handle: 3159 * ! in front logical NOT 3160 * - in front unary minus 3161 * + in front unary plus (ignored) 3162 * trailing [] subscript in String or List 3163 * trailing .name entry in Dictionary 3164 * trailing ->name() method call 3165 * 3166 * "arg" must point to the first non-white of the expression. 3167 * "arg" is advanced to just after the recognized expression. 3168 * 3169 * Return OK or FAIL. 3170 */ 3171 static int 3172 eval7( 3173 char_u **arg, 3174 typval_T *rettv, 3175 evalarg_T *evalarg, 3176 int want_string) // after "." operator 3177 { 3178 int evaluate = evalarg != NULL 3179 && (evalarg->eval_flags & EVAL_EVALUATE); 3180 int len; 3181 char_u *s; 3182 char_u *start_leader, *end_leader; 3183 int ret = OK; 3184 char_u *alias; 3185 3186 /* 3187 * Initialise variable so that clear_tv() can't mistake this for a 3188 * string and free a string that isn't there. 3189 */ 3190 rettv->v_type = VAR_UNKNOWN; 3191 3192 /* 3193 * Skip '!', '-' and '+' characters. They are handled later. 3194 */ 3195 start_leader = *arg; 3196 while (**arg == '!' || **arg == '-' || **arg == '+') 3197 *arg = skipwhite(*arg + 1); 3198 end_leader = *arg; 3199 3200 if (**arg == '.' && (!isdigit(*(*arg + 1)) 3201 #ifdef FEAT_FLOAT 3202 || current_sctx.sc_version < 2 3203 #endif 3204 )) 3205 { 3206 semsg(_(e_invexpr2), *arg); 3207 ++*arg; 3208 return FAIL; 3209 } 3210 3211 switch (**arg) 3212 { 3213 /* 3214 * Number constant. 3215 */ 3216 case '0': 3217 case '1': 3218 case '2': 3219 case '3': 3220 case '4': 3221 case '5': 3222 case '6': 3223 case '7': 3224 case '8': 3225 case '9': 3226 case '.': ret = eval_number(arg, rettv, evaluate, want_string); 3227 3228 // Apply prefixed "-" and "+" now. Matters especially when 3229 // "->" follows. 3230 if (ret == OK && evaluate && end_leader > start_leader 3231 && rettv->v_type != VAR_BLOB) 3232 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader); 3233 break; 3234 3235 /* 3236 * String constant: "string". 3237 */ 3238 case '"': ret = eval_string(arg, rettv, evaluate); 3239 break; 3240 3241 /* 3242 * Literal string constant: 'str''ing'. 3243 */ 3244 case '\'': ret = eval_lit_string(arg, rettv, evaluate); 3245 break; 3246 3247 /* 3248 * List: [expr, expr] 3249 */ 3250 case '[': ret = eval_list(arg, rettv, evalarg, TRUE); 3251 break; 3252 3253 /* 3254 * Dictionary: #{key: val, key: val} 3255 */ 3256 case '#': if ((*arg)[1] == '{') 3257 { 3258 ++*arg; 3259 ret = eval_dict(arg, rettv, evalarg, TRUE); 3260 } 3261 else 3262 ret = NOTDONE; 3263 break; 3264 3265 /* 3266 * Lambda: {arg, arg -> expr} 3267 * Dictionary: {'key': val, 'key': val} 3268 */ 3269 case '{': ret = get_lambda_tv(arg, rettv, FALSE, evalarg); 3270 if (ret == NOTDONE) 3271 ret = eval_dict(arg, rettv, evalarg, FALSE); 3272 break; 3273 3274 /* 3275 * Option value: &name 3276 */ 3277 case '&': ret = eval_option(arg, rettv, evaluate); 3278 break; 3279 3280 /* 3281 * Environment variable: $VAR. 3282 */ 3283 case '$': ret = eval_env_var(arg, rettv, evaluate); 3284 break; 3285 3286 /* 3287 * Register contents: @r. 3288 */ 3289 case '@': ++*arg; 3290 if (evaluate) 3291 { 3292 rettv->v_type = VAR_STRING; 3293 rettv->vval.v_string = get_reg_contents(**arg, 3294 GREG_EXPR_SRC); 3295 } 3296 if (**arg != NUL) 3297 ++*arg; 3298 break; 3299 3300 /* 3301 * nested expression: (expression). 3302 */ 3303 case '(': { 3304 *arg = skipwhite_and_linebreak(*arg + 1, evalarg); 3305 ret = eval1(arg, rettv, evalarg); // recursive! 3306 3307 *arg = skipwhite_and_linebreak(*arg, evalarg); 3308 if (**arg == ')') 3309 ++*arg; 3310 else if (ret == OK) 3311 { 3312 emsg(_(e_missing_close)); 3313 clear_tv(rettv); 3314 ret = FAIL; 3315 } 3316 } 3317 break; 3318 3319 default: ret = NOTDONE; 3320 break; 3321 } 3322 3323 if (ret == NOTDONE) 3324 { 3325 /* 3326 * Must be a variable or function name. 3327 * Can also be a curly-braces kind of name: {expr}. 3328 */ 3329 s = *arg; 3330 len = get_name_len(arg, &alias, evaluate, TRUE); 3331 if (alias != NULL) 3332 s = alias; 3333 3334 if (len <= 0) 3335 ret = FAIL; 3336 else 3337 { 3338 int flags = evalarg == NULL ? 0 : evalarg->eval_flags; 3339 3340 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(') 3341 { 3342 // "name(..." recursive! 3343 *arg = skipwhite(*arg); 3344 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL); 3345 } 3346 else if (flags & EVAL_CONSTANT) 3347 ret = FAIL; 3348 else if (evaluate) 3349 { 3350 // get the value of "true", "false" or a variable 3351 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0) 3352 { 3353 rettv->v_type = VAR_BOOL; 3354 rettv->vval.v_number = VVAL_TRUE; 3355 ret = OK; 3356 } 3357 else if (len == 5 && in_vim9script() 3358 && STRNCMP(s, "false", 4) == 0) 3359 { 3360 rettv->v_type = VAR_BOOL; 3361 rettv->vval.v_number = VVAL_FALSE; 3362 ret = OK; 3363 } 3364 else 3365 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE); 3366 } 3367 else 3368 { 3369 // skip the name 3370 check_vars(s, len); 3371 ret = OK; 3372 } 3373 } 3374 vim_free(alias); 3375 } 3376 3377 // Handle following '[', '(' and '.' for expr[expr], expr.name, 3378 // expr(expr), expr->name(expr) 3379 if (ret == OK) 3380 ret = handle_subscript(arg, rettv, evalarg, TRUE); 3381 3382 /* 3383 * Apply logical NOT and unary '-', from right to left, ignore '+'. 3384 */ 3385 if (ret == OK && evaluate && end_leader > start_leader) 3386 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader); 3387 return ret; 3388 } 3389 3390 /* 3391 * Apply the leading "!" and "-" before an eval7 expression to "rettv". 3392 * When "numeric_only" is TRUE only handle "+" and "-". 3393 * Adjusts "end_leaderp" until it is at "start_leader". 3394 */ 3395 static int 3396 eval7_leader( 3397 typval_T *rettv, 3398 int numeric_only, 3399 char_u *start_leader, 3400 char_u **end_leaderp) 3401 { 3402 char_u *end_leader = *end_leaderp; 3403 int ret = OK; 3404 int error = FALSE; 3405 varnumber_T val = 0; 3406 vartype_T type = rettv->v_type; 3407 #ifdef FEAT_FLOAT 3408 float_T f = 0.0; 3409 3410 if (rettv->v_type == VAR_FLOAT) 3411 f = rettv->vval.v_float; 3412 else 3413 #endif 3414 { 3415 while (VIM_ISWHITE(end_leader[-1])) 3416 --end_leader; 3417 if (in_vim9script() && end_leader[-1] == '!') 3418 val = tv2bool(rettv); 3419 else 3420 val = tv_get_number_chk(rettv, &error); 3421 } 3422 if (error) 3423 { 3424 clear_tv(rettv); 3425 ret = FAIL; 3426 } 3427 else 3428 { 3429 while (end_leader > start_leader) 3430 { 3431 --end_leader; 3432 if (*end_leader == '!') 3433 { 3434 if (numeric_only) 3435 { 3436 ++end_leader; 3437 break; 3438 } 3439 #ifdef FEAT_FLOAT 3440 if (rettv->v_type == VAR_FLOAT) 3441 { 3442 if (in_vim9script()) 3443 { 3444 rettv->v_type = VAR_BOOL; 3445 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE; 3446 } 3447 else 3448 f = !f; 3449 } 3450 else 3451 #endif 3452 { 3453 val = !val; 3454 type = VAR_BOOL; 3455 } 3456 } 3457 else if (*end_leader == '-') 3458 { 3459 #ifdef FEAT_FLOAT 3460 if (rettv->v_type == VAR_FLOAT) 3461 f = -f; 3462 else 3463 #endif 3464 { 3465 val = -val; 3466 type = VAR_NUMBER; 3467 } 3468 } 3469 } 3470 #ifdef FEAT_FLOAT 3471 if (rettv->v_type == VAR_FLOAT) 3472 { 3473 clear_tv(rettv); 3474 rettv->vval.v_float = f; 3475 } 3476 else 3477 #endif 3478 { 3479 clear_tv(rettv); 3480 if (in_vim9script()) 3481 rettv->v_type = type; 3482 else 3483 rettv->v_type = VAR_NUMBER; 3484 rettv->vval.v_number = val; 3485 } 3486 } 3487 *end_leaderp = end_leader; 3488 return ret; 3489 } 3490 3491 /* 3492 * Call the function referred to in "rettv". 3493 */ 3494 static int 3495 call_func_rettv( 3496 char_u **arg, 3497 evalarg_T *evalarg, 3498 typval_T *rettv, 3499 int evaluate, 3500 dict_T *selfdict, 3501 typval_T *basetv) 3502 { 3503 partial_T *pt = NULL; 3504 funcexe_T funcexe; 3505 typval_T functv; 3506 char_u *s; 3507 int ret; 3508 3509 // need to copy the funcref so that we can clear rettv 3510 if (evaluate) 3511 { 3512 functv = *rettv; 3513 rettv->v_type = VAR_UNKNOWN; 3514 3515 // Invoke the function. Recursive! 3516 if (functv.v_type == VAR_PARTIAL) 3517 { 3518 pt = functv.vval.v_partial; 3519 s = partial_name(pt); 3520 } 3521 else 3522 s = functv.vval.v_string; 3523 } 3524 else 3525 s = (char_u *)""; 3526 3527 CLEAR_FIELD(funcexe); 3528 funcexe.firstline = curwin->w_cursor.lnum; 3529 funcexe.lastline = curwin->w_cursor.lnum; 3530 funcexe.evaluate = evaluate; 3531 funcexe.partial = pt; 3532 funcexe.selfdict = selfdict; 3533 funcexe.basetv = basetv; 3534 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe); 3535 3536 // Clear the funcref afterwards, so that deleting it while 3537 // evaluating the arguments is possible (see test55). 3538 if (evaluate) 3539 clear_tv(&functv); 3540 3541 return ret; 3542 } 3543 3544 /* 3545 * Evaluate "->method()". 3546 * "*arg" points to the '-'. 3547 * Returns FAIL or OK. "*arg" is advanced to after the ')'. 3548 */ 3549 static int 3550 eval_lambda( 3551 char_u **arg, 3552 typval_T *rettv, 3553 evalarg_T *evalarg, 3554 int verbose) // give error messages 3555 { 3556 int evaluate = evalarg != NULL 3557 && (evalarg->eval_flags & EVAL_EVALUATE); 3558 typval_T base = *rettv; 3559 int ret; 3560 3561 // Skip over the ->. 3562 *arg += 2; 3563 rettv->v_type = VAR_UNKNOWN; 3564 3565 ret = get_lambda_tv(arg, rettv, FALSE, evalarg); 3566 if (ret != OK) 3567 return FAIL; 3568 else if (**arg != '(') 3569 { 3570 if (verbose) 3571 { 3572 if (*skipwhite(*arg) == '(') 3573 emsg(_(e_nowhitespace)); 3574 else 3575 semsg(_(e_missing_paren), "lambda"); 3576 } 3577 clear_tv(rettv); 3578 ret = FAIL; 3579 } 3580 else 3581 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base); 3582 3583 // Clear the funcref afterwards, so that deleting it while 3584 // evaluating the arguments is possible (see test55). 3585 if (evaluate) 3586 clear_tv(&base); 3587 3588 return ret; 3589 } 3590 3591 /* 3592 * Evaluate "->method()". 3593 * "*arg" points to the '-'. 3594 * Returns FAIL or OK. "*arg" is advanced to after the ')'. 3595 */ 3596 static int 3597 eval_method( 3598 char_u **arg, 3599 typval_T *rettv, 3600 evalarg_T *evalarg, 3601 int verbose) // give error messages 3602 { 3603 char_u *name; 3604 long len; 3605 char_u *alias; 3606 typval_T base = *rettv; 3607 int ret; 3608 int evaluate = evalarg != NULL 3609 && (evalarg->eval_flags & EVAL_EVALUATE); 3610 3611 // Skip over the ->. 3612 *arg += 2; 3613 rettv->v_type = VAR_UNKNOWN; 3614 3615 name = *arg; 3616 len = get_name_len(arg, &alias, evaluate, TRUE); 3617 if (alias != NULL) 3618 name = alias; 3619 3620 if (len <= 0) 3621 { 3622 if (verbose) 3623 emsg(_("E260: Missing name after ->")); 3624 ret = FAIL; 3625 } 3626 else 3627 { 3628 *arg = skipwhite(*arg); 3629 if (**arg != '(') 3630 { 3631 if (verbose) 3632 semsg(_(e_missing_paren), name); 3633 ret = FAIL; 3634 } 3635 else if (VIM_ISWHITE((*arg)[-1])) 3636 { 3637 if (verbose) 3638 emsg(_(e_nowhitespace)); 3639 ret = FAIL; 3640 } 3641 else 3642 ret = eval_func(arg, evalarg, name, len, rettv, 3643 evaluate ? EVAL_EVALUATE : 0, &base); 3644 } 3645 3646 // Clear the funcref afterwards, so that deleting it while 3647 // evaluating the arguments is possible (see test55). 3648 if (evaluate) 3649 clear_tv(&base); 3650 3651 return ret; 3652 } 3653 3654 /* 3655 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key". 3656 * "*arg" points to the '[' or '.'. 3657 * Returns FAIL or OK. "*arg" is advanced to after the ']'. 3658 */ 3659 static int 3660 eval_index( 3661 char_u **arg, 3662 typval_T *rettv, 3663 evalarg_T *evalarg, 3664 int verbose) // give error messages 3665 { 3666 int evaluate = evalarg != NULL 3667 && (evalarg->eval_flags & EVAL_EVALUATE); 3668 int empty1 = FALSE, empty2 = FALSE; 3669 typval_T var1, var2; 3670 int range = FALSE; 3671 char_u *key = NULL; 3672 int keylen = -1; 3673 3674 if (check_can_index(rettv, evaluate, verbose) == FAIL) 3675 return FAIL; 3676 3677 init_tv(&var1); 3678 init_tv(&var2); 3679 if (**arg == '.') 3680 { 3681 /* 3682 * dict.name 3683 */ 3684 key = *arg + 1; 3685 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen) 3686 ; 3687 if (keylen == 0) 3688 return FAIL; 3689 *arg = key + keylen; 3690 } 3691 else 3692 { 3693 /* 3694 * something[idx] 3695 * 3696 * Get the (first) variable from inside the []. 3697 */ 3698 *arg = skipwhite_and_linebreak(*arg + 1, evalarg); 3699 if (**arg == ':') 3700 empty1 = TRUE; 3701 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive! 3702 return FAIL; 3703 else if (evaluate && tv_get_string_chk(&var1) == NULL) 3704 { 3705 // not a number or string 3706 clear_tv(&var1); 3707 return FAIL; 3708 } 3709 3710 /* 3711 * Get the second variable from inside the [:]. 3712 */ 3713 *arg = skipwhite_and_linebreak(*arg, evalarg); 3714 if (**arg == ':') 3715 { 3716 range = TRUE; 3717 *arg = skipwhite_and_linebreak(*arg + 1, evalarg); 3718 if (**arg == ']') 3719 empty2 = TRUE; 3720 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive! 3721 { 3722 if (!empty1) 3723 clear_tv(&var1); 3724 return FAIL; 3725 } 3726 else if (evaluate && tv_get_string_chk(&var2) == NULL) 3727 { 3728 // not a number or string 3729 if (!empty1) 3730 clear_tv(&var1); 3731 clear_tv(&var2); 3732 return FAIL; 3733 } 3734 } 3735 3736 // Check for the ']'. 3737 *arg = skipwhite_and_linebreak(*arg, evalarg); 3738 if (**arg != ']') 3739 { 3740 if (verbose) 3741 emsg(_(e_missbrac)); 3742 clear_tv(&var1); 3743 if (range) 3744 clear_tv(&var2); 3745 return FAIL; 3746 } 3747 *arg = *arg + 1; // skip over the ']' 3748 } 3749 3750 if (evaluate) 3751 { 3752 int res = eval_index_inner(rettv, range, 3753 empty1 ? NULL : &var1, empty2 ? NULL : &var2, 3754 key, keylen, verbose); 3755 if (!empty1) 3756 clear_tv(&var1); 3757 if (range) 3758 clear_tv(&var2); 3759 return res; 3760 } 3761 return OK; 3762 } 3763 3764 /* 3765 * Check if "rettv" can have an [index] or [sli:ce] 3766 */ 3767 int 3768 check_can_index(typval_T *rettv, int evaluate, int verbose) 3769 { 3770 switch (rettv->v_type) 3771 { 3772 case VAR_FUNC: 3773 case VAR_PARTIAL: 3774 if (verbose) 3775 emsg(_("E695: Cannot index a Funcref")); 3776 return FAIL; 3777 case VAR_FLOAT: 3778 #ifdef FEAT_FLOAT 3779 if (verbose) 3780 emsg(_(e_float_as_string)); 3781 return FAIL; 3782 #endif 3783 case VAR_BOOL: 3784 case VAR_SPECIAL: 3785 case VAR_JOB: 3786 case VAR_CHANNEL: 3787 if (verbose) 3788 emsg(_(e_cannot_index_special_variable)); 3789 return FAIL; 3790 case VAR_UNKNOWN: 3791 case VAR_ANY: 3792 case VAR_VOID: 3793 if (evaluate) 3794 { 3795 emsg(_(e_cannot_index_special_variable)); 3796 return FAIL; 3797 } 3798 // FALLTHROUGH 3799 3800 case VAR_STRING: 3801 case VAR_LIST: 3802 case VAR_DICT: 3803 case VAR_BLOB: 3804 break; 3805 case VAR_NUMBER: 3806 if (in_vim9script()) 3807 emsg(_(e_cannot_index_number)); 3808 break; 3809 } 3810 return OK; 3811 } 3812 3813 /* 3814 * Apply index or range to "rettv". 3815 * "var1" is the first index, NULL for [:expr]. 3816 * "var2" is the second index, NULL for [expr] and [expr: ] 3817 * Alternatively, "key" is not NULL, then key[keylen] is the dict index. 3818 */ 3819 int 3820 eval_index_inner( 3821 typval_T *rettv, 3822 int is_range, 3823 typval_T *var1, 3824 typval_T *var2, 3825 char_u *key, 3826 int keylen, 3827 int verbose) 3828 { 3829 long n1, n2 = 0; 3830 long len; 3831 3832 n1 = 0; 3833 if (var1 != NULL && rettv->v_type != VAR_DICT) 3834 n1 = tv_get_number(var1); 3835 3836 if (is_range) 3837 { 3838 if (rettv->v_type == VAR_DICT) 3839 { 3840 if (verbose) 3841 emsg(_(e_cannot_slice_dictionary)); 3842 return FAIL; 3843 } 3844 if (var2 == NULL) 3845 n2 = -1; 3846 else 3847 n2 = tv_get_number(var2); 3848 } 3849 3850 switch (rettv->v_type) 3851 { 3852 case VAR_UNKNOWN: 3853 case VAR_ANY: 3854 case VAR_VOID: 3855 case VAR_FUNC: 3856 case VAR_PARTIAL: 3857 case VAR_FLOAT: 3858 case VAR_BOOL: 3859 case VAR_SPECIAL: 3860 case VAR_JOB: 3861 case VAR_CHANNEL: 3862 break; // not evaluating, skipping over subscript 3863 3864 case VAR_NUMBER: 3865 case VAR_STRING: 3866 { 3867 char_u *s = tv_get_string(rettv); 3868 3869 len = (long)STRLEN(s); 3870 if (in_vim9script()) 3871 { 3872 if (is_range) 3873 s = string_slice(s, n1, n2); 3874 else 3875 s = char_from_string(s, n1); 3876 } 3877 else if (is_range) 3878 { 3879 // The resulting variable is a substring. If the indexes 3880 // are out of range the result is empty. 3881 if (n1 < 0) 3882 { 3883 n1 = len + n1; 3884 if (n1 < 0) 3885 n1 = 0; 3886 } 3887 if (n2 < 0) 3888 n2 = len + n2; 3889 else if (n2 >= len) 3890 n2 = len; 3891 if (n1 >= len || n2 < 0 || n1 > n2) 3892 s = NULL; 3893 else 3894 s = vim_strnsave(s + n1, n2 - n1 + 1); 3895 } 3896 else 3897 { 3898 // The resulting variable is a string of a single 3899 // character. If the index is too big or negative the 3900 // result is empty. 3901 if (n1 >= len || n1 < 0) 3902 s = NULL; 3903 else 3904 s = vim_strnsave(s + n1, 1); 3905 } 3906 clear_tv(rettv); 3907 rettv->v_type = VAR_STRING; 3908 rettv->vval.v_string = s; 3909 } 3910 break; 3911 3912 case VAR_BLOB: 3913 len = blob_len(rettv->vval.v_blob); 3914 if (is_range) 3915 { 3916 // The resulting variable is a sub-blob. If the indexes 3917 // are out of range the result is empty. 3918 if (n1 < 0) 3919 { 3920 n1 = len + n1; 3921 if (n1 < 0) 3922 n1 = 0; 3923 } 3924 if (n2 < 0) 3925 n2 = len + n2; 3926 else if (n2 >= len) 3927 n2 = len - 1; 3928 if (n1 >= len || n2 < 0 || n1 > n2) 3929 { 3930 clear_tv(rettv); 3931 rettv->v_type = VAR_BLOB; 3932 rettv->vval.v_blob = NULL; 3933 } 3934 else 3935 { 3936 blob_T *blob = blob_alloc(); 3937 long i; 3938 3939 if (blob != NULL) 3940 { 3941 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL) 3942 { 3943 blob_free(blob); 3944 return FAIL; 3945 } 3946 blob->bv_ga.ga_len = n2 - n1 + 1; 3947 for (i = n1; i <= n2; i++) 3948 blob_set(blob, i - n1, 3949 blob_get(rettv->vval.v_blob, i)); 3950 3951 clear_tv(rettv); 3952 rettv_blob_set(rettv, blob); 3953 } 3954 } 3955 } 3956 else 3957 { 3958 // The resulting variable is a byte value. 3959 // If the index is too big or negative that is an error. 3960 if (n1 < 0) 3961 n1 = len + n1; 3962 if (n1 < len && n1 >= 0) 3963 { 3964 int v = blob_get(rettv->vval.v_blob, n1); 3965 3966 clear_tv(rettv); 3967 rettv->v_type = VAR_NUMBER; 3968 rettv->vval.v_number = v; 3969 } 3970 else 3971 semsg(_(e_blobidx), n1); 3972 } 3973 break; 3974 3975 case VAR_LIST: 3976 if (var1 == NULL) 3977 n1 = 0; 3978 if (var2 == NULL) 3979 n2 = -1; 3980 if (list_slice_or_index(rettv->vval.v_list, 3981 is_range, n1, n2, rettv, verbose) == FAIL) 3982 return FAIL; 3983 break; 3984 3985 case VAR_DICT: 3986 { 3987 dictitem_T *item; 3988 typval_T tmp; 3989 3990 if (key == NULL) 3991 { 3992 key = tv_get_string_chk(var1); 3993 if (key == NULL) 3994 return FAIL; 3995 } 3996 3997 item = dict_find(rettv->vval.v_dict, key, (int)keylen); 3998 3999 if (item == NULL && verbose) 4000 semsg(_(e_dictkey), key); 4001 if (item == NULL) 4002 return FAIL; 4003 4004 copy_tv(&item->di_tv, &tmp); 4005 clear_tv(rettv); 4006 *rettv = tmp; 4007 } 4008 break; 4009 } 4010 return OK; 4011 } 4012 4013 /* 4014 * Return the function name of partial "pt". 4015 */ 4016 char_u * 4017 partial_name(partial_T *pt) 4018 { 4019 if (pt->pt_name != NULL) 4020 return pt->pt_name; 4021 if (pt->pt_func != NULL) 4022 return pt->pt_func->uf_name; 4023 return (char_u *)""; 4024 } 4025 4026 static void 4027 partial_free(partial_T *pt) 4028 { 4029 int i; 4030 4031 for (i = 0; i < pt->pt_argc; ++i) 4032 clear_tv(&pt->pt_argv[i]); 4033 vim_free(pt->pt_argv); 4034 dict_unref(pt->pt_dict); 4035 if (pt->pt_name != NULL) 4036 { 4037 func_unref(pt->pt_name); 4038 vim_free(pt->pt_name); 4039 } 4040 else 4041 func_ptr_unref(pt->pt_func); 4042 4043 // Decrease the reference count for the context of a closure. If down 4044 // to the minimum it may be time to free it. 4045 if (pt->pt_funcstack != NULL) 4046 { 4047 --pt->pt_funcstack->fs_refcount; 4048 funcstack_check_refcount(pt->pt_funcstack); 4049 } 4050 4051 vim_free(pt); 4052 } 4053 4054 /* 4055 * Unreference a closure: decrement the reference count and free it when it 4056 * becomes zero. 4057 */ 4058 void 4059 partial_unref(partial_T *pt) 4060 { 4061 if (pt != NULL) 4062 { 4063 if (--pt->pt_refcount <= 0) 4064 partial_free(pt); 4065 4066 // If the reference count goes down to one, the funcstack may be the 4067 // only reference and can be freed if no other partials reference it. 4068 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL) 4069 funcstack_check_refcount(pt->pt_funcstack); 4070 } 4071 } 4072 4073 /* 4074 * Return the next (unique) copy ID. 4075 * Used for serializing nested structures. 4076 */ 4077 int 4078 get_copyID(void) 4079 { 4080 current_copyID += COPYID_INC; 4081 return current_copyID; 4082 } 4083 4084 /* 4085 * Garbage collection for lists and dictionaries. 4086 * 4087 * We use reference counts to be able to free most items right away when they 4088 * are no longer used. But for composite items it's possible that it becomes 4089 * unused while the reference count is > 0: When there is a recursive 4090 * reference. Example: 4091 * :let l = [1, 2, 3] 4092 * :let d = {9: l} 4093 * :let l[1] = d 4094 * 4095 * Since this is quite unusual we handle this with garbage collection: every 4096 * once in a while find out which lists and dicts are not referenced from any 4097 * variable. 4098 * 4099 * Here is a good reference text about garbage collection (refers to Python 4100 * but it applies to all reference-counting mechanisms): 4101 * http://python.ca/nas/python/gc/ 4102 */ 4103 4104 /* 4105 * Do garbage collection for lists and dicts. 4106 * When "testing" is TRUE this is called from test_garbagecollect_now(). 4107 * Return TRUE if some memory was freed. 4108 */ 4109 int 4110 garbage_collect(int testing) 4111 { 4112 int copyID; 4113 int abort = FALSE; 4114 buf_T *buf; 4115 win_T *wp; 4116 int did_free = FALSE; 4117 tabpage_T *tp; 4118 4119 if (!testing) 4120 { 4121 // Only do this once. 4122 want_garbage_collect = FALSE; 4123 may_garbage_collect = FALSE; 4124 garbage_collect_at_exit = FALSE; 4125 } 4126 4127 // The execution stack can grow big, limit the size. 4128 if (exestack.ga_maxlen - exestack.ga_len > 500) 4129 { 4130 size_t new_len; 4131 char_u *pp; 4132 int n; 4133 4134 // Keep 150% of the current size, with a minimum of the growth size. 4135 n = exestack.ga_len / 2; 4136 if (n < exestack.ga_growsize) 4137 n = exestack.ga_growsize; 4138 4139 // Don't make it bigger though. 4140 if (exestack.ga_len + n < exestack.ga_maxlen) 4141 { 4142 new_len = exestack.ga_itemsize * (exestack.ga_len + n); 4143 pp = vim_realloc(exestack.ga_data, new_len); 4144 if (pp == NULL) 4145 return FAIL; 4146 exestack.ga_maxlen = exestack.ga_len + n; 4147 exestack.ga_data = pp; 4148 } 4149 } 4150 4151 // We advance by two because we add one for items referenced through 4152 // previous_funccal. 4153 copyID = get_copyID(); 4154 4155 /* 4156 * 1. Go through all accessible variables and mark all lists and dicts 4157 * with copyID. 4158 */ 4159 4160 // Don't free variables in the previous_funccal list unless they are only 4161 // referenced through previous_funccal. This must be first, because if 4162 // the item is referenced elsewhere the funccal must not be freed. 4163 abort = abort || set_ref_in_previous_funccal(copyID); 4164 4165 // script-local variables 4166 abort = abort || garbage_collect_scriptvars(copyID); 4167 4168 // buffer-local variables 4169 FOR_ALL_BUFFERS(buf) 4170 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID, 4171 NULL, NULL); 4172 4173 // window-local variables 4174 FOR_ALL_TAB_WINDOWS(tp, wp) 4175 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID, 4176 NULL, NULL); 4177 if (aucmd_win != NULL) 4178 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID, 4179 NULL, NULL); 4180 #ifdef FEAT_PROP_POPUP 4181 FOR_ALL_POPUPWINS(wp) 4182 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID, 4183 NULL, NULL); 4184 FOR_ALL_TABPAGES(tp) 4185 FOR_ALL_POPUPWINS_IN_TAB(tp, wp) 4186 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID, 4187 NULL, NULL); 4188 #endif 4189 4190 // tabpage-local variables 4191 FOR_ALL_TABPAGES(tp) 4192 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID, 4193 NULL, NULL); 4194 // global variables 4195 abort = abort || garbage_collect_globvars(copyID); 4196 4197 // function-local variables 4198 abort = abort || set_ref_in_call_stack(copyID); 4199 4200 // named functions (matters for closures) 4201 abort = abort || set_ref_in_functions(copyID); 4202 4203 // function call arguments, if v:testing is set. 4204 abort = abort || set_ref_in_func_args(copyID); 4205 4206 // v: vars 4207 abort = abort || garbage_collect_vimvars(copyID); 4208 4209 // callbacks in buffers 4210 abort = abort || set_ref_in_buffers(copyID); 4211 4212 #ifdef FEAT_LUA 4213 abort = abort || set_ref_in_lua(copyID); 4214 #endif 4215 4216 #ifdef FEAT_PYTHON 4217 abort = abort || set_ref_in_python(copyID); 4218 #endif 4219 4220 #ifdef FEAT_PYTHON3 4221 abort = abort || set_ref_in_python3(copyID); 4222 #endif 4223 4224 #ifdef FEAT_JOB_CHANNEL 4225 abort = abort || set_ref_in_channel(copyID); 4226 abort = abort || set_ref_in_job(copyID); 4227 #endif 4228 #ifdef FEAT_NETBEANS_INTG 4229 abort = abort || set_ref_in_nb_channel(copyID); 4230 #endif 4231 4232 #ifdef FEAT_TIMERS 4233 abort = abort || set_ref_in_timer(copyID); 4234 #endif 4235 4236 #ifdef FEAT_QUICKFIX 4237 abort = abort || set_ref_in_quickfix(copyID); 4238 #endif 4239 4240 #ifdef FEAT_TERMINAL 4241 abort = abort || set_ref_in_term(copyID); 4242 #endif 4243 4244 #ifdef FEAT_PROP_POPUP 4245 abort = abort || set_ref_in_popups(copyID); 4246 #endif 4247 4248 if (!abort) 4249 { 4250 /* 4251 * 2. Free lists and dictionaries that are not referenced. 4252 */ 4253 did_free = free_unref_items(copyID); 4254 4255 /* 4256 * 3. Check if any funccal can be freed now. 4257 * This may call us back recursively. 4258 */ 4259 free_unref_funccal(copyID, testing); 4260 } 4261 else if (p_verbose > 0) 4262 { 4263 verb_msg(_("Not enough memory to set references, garbage collection aborted!")); 4264 } 4265 4266 return did_free; 4267 } 4268 4269 /* 4270 * Free lists, dictionaries, channels and jobs that are no longer referenced. 4271 */ 4272 static int 4273 free_unref_items(int copyID) 4274 { 4275 int did_free = FALSE; 4276 4277 // Let all "free" functions know that we are here. This means no 4278 // dictionaries, lists, channels or jobs are to be freed, because we will 4279 // do that here. 4280 in_free_unref_items = TRUE; 4281 4282 /* 4283 * PASS 1: free the contents of the items. We don't free the items 4284 * themselves yet, so that it is possible to decrement refcount counters 4285 */ 4286 4287 // Go through the list of dicts and free items without the copyID. 4288 did_free |= dict_free_nonref(copyID); 4289 4290 // Go through the list of lists and free items without the copyID. 4291 did_free |= list_free_nonref(copyID); 4292 4293 #ifdef FEAT_JOB_CHANNEL 4294 // Go through the list of jobs and free items without the copyID. This 4295 // must happen before doing channels, because jobs refer to channels, but 4296 // the reference from the channel to the job isn't tracked. 4297 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK); 4298 4299 // Go through the list of channels and free items without the copyID. 4300 did_free |= free_unused_channels_contents(copyID, COPYID_MASK); 4301 #endif 4302 4303 /* 4304 * PASS 2: free the items themselves. 4305 */ 4306 dict_free_items(copyID); 4307 list_free_items(copyID); 4308 4309 #ifdef FEAT_JOB_CHANNEL 4310 // Go through the list of jobs and free items without the copyID. This 4311 // must happen before doing channels, because jobs refer to channels, but 4312 // the reference from the channel to the job isn't tracked. 4313 free_unused_jobs(copyID, COPYID_MASK); 4314 4315 // Go through the list of channels and free items without the copyID. 4316 free_unused_channels(copyID, COPYID_MASK); 4317 #endif 4318 4319 in_free_unref_items = FALSE; 4320 4321 return did_free; 4322 } 4323 4324 /* 4325 * Mark all lists and dicts referenced through hashtab "ht" with "copyID". 4326 * "list_stack" is used to add lists to be marked. Can be NULL. 4327 * 4328 * Returns TRUE if setting references failed somehow. 4329 */ 4330 int 4331 set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack) 4332 { 4333 int todo; 4334 int abort = FALSE; 4335 hashitem_T *hi; 4336 hashtab_T *cur_ht; 4337 ht_stack_T *ht_stack = NULL; 4338 ht_stack_T *tempitem; 4339 4340 cur_ht = ht; 4341 for (;;) 4342 { 4343 if (!abort) 4344 { 4345 // Mark each item in the hashtab. If the item contains a hashtab 4346 // it is added to ht_stack, if it contains a list it is added to 4347 // list_stack. 4348 todo = (int)cur_ht->ht_used; 4349 for (hi = cur_ht->ht_array; todo > 0; ++hi) 4350 if (!HASHITEM_EMPTY(hi)) 4351 { 4352 --todo; 4353 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID, 4354 &ht_stack, list_stack); 4355 } 4356 } 4357 4358 if (ht_stack == NULL) 4359 break; 4360 4361 // take an item from the stack 4362 cur_ht = ht_stack->ht; 4363 tempitem = ht_stack; 4364 ht_stack = ht_stack->prev; 4365 free(tempitem); 4366 } 4367 4368 return abort; 4369 } 4370 4371 /* 4372 * Mark a dict and its items with "copyID". 4373 * Returns TRUE if setting references failed somehow. 4374 */ 4375 int 4376 set_ref_in_dict(dict_T *d, int copyID) 4377 { 4378 if (d != NULL && d->dv_copyID != copyID) 4379 { 4380 d->dv_copyID = copyID; 4381 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL); 4382 } 4383 return FALSE; 4384 } 4385 4386 /* 4387 * Mark a list and its items with "copyID". 4388 * Returns TRUE if setting references failed somehow. 4389 */ 4390 int 4391 set_ref_in_list(list_T *ll, int copyID) 4392 { 4393 if (ll != NULL && ll->lv_copyID != copyID) 4394 { 4395 ll->lv_copyID = copyID; 4396 return set_ref_in_list_items(ll, copyID, NULL); 4397 } 4398 return FALSE; 4399 } 4400 4401 /* 4402 * Mark all lists and dicts referenced through list "l" with "copyID". 4403 * "ht_stack" is used to add hashtabs to be marked. Can be NULL. 4404 * 4405 * Returns TRUE if setting references failed somehow. 4406 */ 4407 int 4408 set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack) 4409 { 4410 listitem_T *li; 4411 int abort = FALSE; 4412 list_T *cur_l; 4413 list_stack_T *list_stack = NULL; 4414 list_stack_T *tempitem; 4415 4416 cur_l = l; 4417 for (;;) 4418 { 4419 if (!abort && cur_l->lv_first != &range_list_item) 4420 // Mark each item in the list. If the item contains a hashtab 4421 // it is added to ht_stack, if it contains a list it is added to 4422 // list_stack. 4423 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next) 4424 abort = abort || set_ref_in_item(&li->li_tv, copyID, 4425 ht_stack, &list_stack); 4426 if (list_stack == NULL) 4427 break; 4428 4429 // take an item from the stack 4430 cur_l = list_stack->list; 4431 tempitem = list_stack; 4432 list_stack = list_stack->prev; 4433 free(tempitem); 4434 } 4435 4436 return abort; 4437 } 4438 4439 /* 4440 * Mark all lists and dicts referenced through typval "tv" with "copyID". 4441 * "list_stack" is used to add lists to be marked. Can be NULL. 4442 * "ht_stack" is used to add hashtabs to be marked. Can be NULL. 4443 * 4444 * Returns TRUE if setting references failed somehow. 4445 */ 4446 int 4447 set_ref_in_item( 4448 typval_T *tv, 4449 int copyID, 4450 ht_stack_T **ht_stack, 4451 list_stack_T **list_stack) 4452 { 4453 int abort = FALSE; 4454 4455 if (tv->v_type == VAR_DICT) 4456 { 4457 dict_T *dd = tv->vval.v_dict; 4458 4459 if (dd != NULL && dd->dv_copyID != copyID) 4460 { 4461 // Didn't see this dict yet. 4462 dd->dv_copyID = copyID; 4463 if (ht_stack == NULL) 4464 { 4465 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack); 4466 } 4467 else 4468 { 4469 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T); 4470 4471 if (newitem == NULL) 4472 abort = TRUE; 4473 else 4474 { 4475 newitem->ht = &dd->dv_hashtab; 4476 newitem->prev = *ht_stack; 4477 *ht_stack = newitem; 4478 } 4479 } 4480 } 4481 } 4482 else if (tv->v_type == VAR_LIST) 4483 { 4484 list_T *ll = tv->vval.v_list; 4485 4486 if (ll != NULL && ll->lv_copyID != copyID) 4487 { 4488 // Didn't see this list yet. 4489 ll->lv_copyID = copyID; 4490 if (list_stack == NULL) 4491 { 4492 abort = set_ref_in_list_items(ll, copyID, ht_stack); 4493 } 4494 else 4495 { 4496 list_stack_T *newitem = ALLOC_ONE(list_stack_T); 4497 4498 if (newitem == NULL) 4499 abort = TRUE; 4500 else 4501 { 4502 newitem->list = ll; 4503 newitem->prev = *list_stack; 4504 *list_stack = newitem; 4505 } 4506 } 4507 } 4508 } 4509 else if (tv->v_type == VAR_FUNC) 4510 { 4511 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID); 4512 } 4513 else if (tv->v_type == VAR_PARTIAL) 4514 { 4515 partial_T *pt = tv->vval.v_partial; 4516 int i; 4517 4518 if (pt != NULL && pt->pt_copyID != copyID) 4519 { 4520 // Didn't see this partial yet. 4521 pt->pt_copyID = copyID; 4522 4523 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID); 4524 4525 if (pt->pt_dict != NULL) 4526 { 4527 typval_T dtv; 4528 4529 dtv.v_type = VAR_DICT; 4530 dtv.vval.v_dict = pt->pt_dict; 4531 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4532 } 4533 4534 for (i = 0; i < pt->pt_argc; ++i) 4535 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID, 4536 ht_stack, list_stack); 4537 if (pt->pt_funcstack != NULL) 4538 { 4539 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data; 4540 4541 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i) 4542 abort = abort || set_ref_in_item(stack + i, copyID, 4543 ht_stack, list_stack); 4544 } 4545 4546 } 4547 } 4548 #ifdef FEAT_JOB_CHANNEL 4549 else if (tv->v_type == VAR_JOB) 4550 { 4551 job_T *job = tv->vval.v_job; 4552 typval_T dtv; 4553 4554 if (job != NULL && job->jv_copyID != copyID) 4555 { 4556 job->jv_copyID = copyID; 4557 if (job->jv_channel != NULL) 4558 { 4559 dtv.v_type = VAR_CHANNEL; 4560 dtv.vval.v_channel = job->jv_channel; 4561 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4562 } 4563 if (job->jv_exit_cb.cb_partial != NULL) 4564 { 4565 dtv.v_type = VAR_PARTIAL; 4566 dtv.vval.v_partial = job->jv_exit_cb.cb_partial; 4567 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4568 } 4569 } 4570 } 4571 else if (tv->v_type == VAR_CHANNEL) 4572 { 4573 channel_T *ch =tv->vval.v_channel; 4574 ch_part_T part; 4575 typval_T dtv; 4576 jsonq_T *jq; 4577 cbq_T *cq; 4578 4579 if (ch != NULL && ch->ch_copyID != copyID) 4580 { 4581 ch->ch_copyID = copyID; 4582 for (part = PART_SOCK; part < PART_COUNT; ++part) 4583 { 4584 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL; 4585 jq = jq->jq_next) 4586 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack); 4587 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL; 4588 cq = cq->cq_next) 4589 if (cq->cq_callback.cb_partial != NULL) 4590 { 4591 dtv.v_type = VAR_PARTIAL; 4592 dtv.vval.v_partial = cq->cq_callback.cb_partial; 4593 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4594 } 4595 if (ch->ch_part[part].ch_callback.cb_partial != NULL) 4596 { 4597 dtv.v_type = VAR_PARTIAL; 4598 dtv.vval.v_partial = 4599 ch->ch_part[part].ch_callback.cb_partial; 4600 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4601 } 4602 } 4603 if (ch->ch_callback.cb_partial != NULL) 4604 { 4605 dtv.v_type = VAR_PARTIAL; 4606 dtv.vval.v_partial = ch->ch_callback.cb_partial; 4607 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4608 } 4609 if (ch->ch_close_cb.cb_partial != NULL) 4610 { 4611 dtv.v_type = VAR_PARTIAL; 4612 dtv.vval.v_partial = ch->ch_close_cb.cb_partial; 4613 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 4614 } 4615 } 4616 } 4617 #endif 4618 return abort; 4619 } 4620 4621 /* 4622 * Return a string with the string representation of a variable. 4623 * If the memory is allocated "tofree" is set to it, otherwise NULL. 4624 * "numbuf" is used for a number. 4625 * When "copyID" is not NULL replace recursive lists and dicts with "...". 4626 * When both "echo_style" and "composite_val" are FALSE, put quotes around 4627 * stings as "string()", otherwise does not put quotes around strings, as 4628 * ":echo" displays values. 4629 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists 4630 * are replaced with "...". 4631 * May return NULL. 4632 */ 4633 char_u * 4634 echo_string_core( 4635 typval_T *tv, 4636 char_u **tofree, 4637 char_u *numbuf, 4638 int copyID, 4639 int echo_style, 4640 int restore_copyID, 4641 int composite_val) 4642 { 4643 static int recurse = 0; 4644 char_u *r = NULL; 4645 4646 if (recurse >= DICT_MAXNEST) 4647 { 4648 if (!did_echo_string_emsg) 4649 { 4650 // Only give this message once for a recursive call to avoid 4651 // flooding the user with errors. And stop iterating over lists 4652 // and dicts. 4653 did_echo_string_emsg = TRUE; 4654 emsg(_("E724: variable nested too deep for displaying")); 4655 } 4656 *tofree = NULL; 4657 return (char_u *)"{E724}"; 4658 } 4659 ++recurse; 4660 4661 switch (tv->v_type) 4662 { 4663 case VAR_STRING: 4664 if (echo_style && !composite_val) 4665 { 4666 *tofree = NULL; 4667 r = tv->vval.v_string; 4668 if (r == NULL) 4669 r = (char_u *)""; 4670 } 4671 else 4672 { 4673 *tofree = string_quote(tv->vval.v_string, FALSE); 4674 r = *tofree; 4675 } 4676 break; 4677 4678 case VAR_FUNC: 4679 if (echo_style) 4680 { 4681 *tofree = NULL; 4682 r = tv->vval.v_string; 4683 } 4684 else 4685 { 4686 *tofree = string_quote(tv->vval.v_string, TRUE); 4687 r = *tofree; 4688 } 4689 break; 4690 4691 case VAR_PARTIAL: 4692 { 4693 partial_T *pt = tv->vval.v_partial; 4694 char_u *fname = string_quote(pt == NULL ? NULL 4695 : partial_name(pt), FALSE); 4696 garray_T ga; 4697 int i; 4698 char_u *tf; 4699 4700 ga_init2(&ga, 1, 100); 4701 ga_concat(&ga, (char_u *)"function("); 4702 if (fname != NULL) 4703 { 4704 ga_concat(&ga, fname); 4705 vim_free(fname); 4706 } 4707 if (pt != NULL && pt->pt_argc > 0) 4708 { 4709 ga_concat(&ga, (char_u *)", ["); 4710 for (i = 0; i < pt->pt_argc; ++i) 4711 { 4712 if (i > 0) 4713 ga_concat(&ga, (char_u *)", "); 4714 ga_concat(&ga, 4715 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID)); 4716 vim_free(tf); 4717 } 4718 ga_concat(&ga, (char_u *)"]"); 4719 } 4720 if (pt != NULL && pt->pt_dict != NULL) 4721 { 4722 typval_T dtv; 4723 4724 ga_concat(&ga, (char_u *)", "); 4725 dtv.v_type = VAR_DICT; 4726 dtv.vval.v_dict = pt->pt_dict; 4727 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID)); 4728 vim_free(tf); 4729 } 4730 ga_concat(&ga, (char_u *)")"); 4731 4732 *tofree = ga.ga_data; 4733 r = *tofree; 4734 break; 4735 } 4736 4737 case VAR_BLOB: 4738 r = blob2string(tv->vval.v_blob, tofree, numbuf); 4739 break; 4740 4741 case VAR_LIST: 4742 if (tv->vval.v_list == NULL) 4743 { 4744 // NULL list is equivalent to empty list. 4745 *tofree = NULL; 4746 r = (char_u *)"[]"; 4747 } 4748 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID 4749 && tv->vval.v_list->lv_len > 0) 4750 { 4751 *tofree = NULL; 4752 r = (char_u *)"[...]"; 4753 } 4754 else 4755 { 4756 int old_copyID = tv->vval.v_list->lv_copyID; 4757 4758 tv->vval.v_list->lv_copyID = copyID; 4759 *tofree = list2string(tv, copyID, restore_copyID); 4760 if (restore_copyID) 4761 tv->vval.v_list->lv_copyID = old_copyID; 4762 r = *tofree; 4763 } 4764 break; 4765 4766 case VAR_DICT: 4767 if (tv->vval.v_dict == NULL) 4768 { 4769 // NULL dict is equivalent to empty dict. 4770 *tofree = NULL; 4771 r = (char_u *)"{}"; 4772 } 4773 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID 4774 && tv->vval.v_dict->dv_hashtab.ht_used != 0) 4775 { 4776 *tofree = NULL; 4777 r = (char_u *)"{...}"; 4778 } 4779 else 4780 { 4781 int old_copyID = tv->vval.v_dict->dv_copyID; 4782 4783 tv->vval.v_dict->dv_copyID = copyID; 4784 *tofree = dict2string(tv, copyID, restore_copyID); 4785 if (restore_copyID) 4786 tv->vval.v_dict->dv_copyID = old_copyID; 4787 r = *tofree; 4788 } 4789 break; 4790 4791 case VAR_NUMBER: 4792 case VAR_UNKNOWN: 4793 case VAR_ANY: 4794 case VAR_VOID: 4795 *tofree = NULL; 4796 r = tv_get_string_buf(tv, numbuf); 4797 break; 4798 4799 case VAR_JOB: 4800 case VAR_CHANNEL: 4801 *tofree = NULL; 4802 r = tv_get_string_buf(tv, numbuf); 4803 if (composite_val) 4804 { 4805 *tofree = string_quote(r, FALSE); 4806 r = *tofree; 4807 } 4808 break; 4809 4810 case VAR_FLOAT: 4811 #ifdef FEAT_FLOAT 4812 *tofree = NULL; 4813 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float); 4814 r = numbuf; 4815 break; 4816 #endif 4817 4818 case VAR_BOOL: 4819 case VAR_SPECIAL: 4820 *tofree = NULL; 4821 r = (char_u *)get_var_special_name(tv->vval.v_number); 4822 break; 4823 } 4824 4825 if (--recurse == 0) 4826 did_echo_string_emsg = FALSE; 4827 return r; 4828 } 4829 4830 /* 4831 * Return a string with the string representation of a variable. 4832 * If the memory is allocated "tofree" is set to it, otherwise NULL. 4833 * "numbuf" is used for a number. 4834 * Does not put quotes around strings, as ":echo" displays values. 4835 * When "copyID" is not NULL replace recursive lists and dicts with "...". 4836 * May return NULL. 4837 */ 4838 char_u * 4839 echo_string( 4840 typval_T *tv, 4841 char_u **tofree, 4842 char_u *numbuf, 4843 int copyID) 4844 { 4845 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE); 4846 } 4847 4848 /* 4849 * Return string "str" in ' quotes, doubling ' characters. 4850 * If "str" is NULL an empty string is assumed. 4851 * If "function" is TRUE make it function('string'). 4852 */ 4853 char_u * 4854 string_quote(char_u *str, int function) 4855 { 4856 unsigned len; 4857 char_u *p, *r, *s; 4858 4859 len = (function ? 13 : 3); 4860 if (str != NULL) 4861 { 4862 len += (unsigned)STRLEN(str); 4863 for (p = str; *p != NUL; MB_PTR_ADV(p)) 4864 if (*p == '\'') 4865 ++len; 4866 } 4867 s = r = alloc(len); 4868 if (r != NULL) 4869 { 4870 if (function) 4871 { 4872 STRCPY(r, "function('"); 4873 r += 10; 4874 } 4875 else 4876 *r++ = '\''; 4877 if (str != NULL) 4878 for (p = str; *p != NUL; ) 4879 { 4880 if (*p == '\'') 4881 *r++ = '\''; 4882 MB_COPY_CHAR(p, r); 4883 } 4884 *r++ = '\''; 4885 if (function) 4886 *r++ = ')'; 4887 *r++ = NUL; 4888 } 4889 return s; 4890 } 4891 4892 #if defined(FEAT_FLOAT) || defined(PROTO) 4893 /* 4894 * Convert the string "text" to a floating point number. 4895 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure 4896 * this always uses a decimal point. 4897 * Returns the length of the text that was consumed. 4898 */ 4899 int 4900 string2float( 4901 char_u *text, 4902 float_T *value) // result stored here 4903 { 4904 char *s = (char *)text; 4905 float_T f; 4906 4907 // MS-Windows does not deal with "inf" and "nan" properly. 4908 if (STRNICMP(text, "inf", 3) == 0) 4909 { 4910 *value = INFINITY; 4911 return 3; 4912 } 4913 if (STRNICMP(text, "-inf", 3) == 0) 4914 { 4915 *value = -INFINITY; 4916 return 4; 4917 } 4918 if (STRNICMP(text, "nan", 3) == 0) 4919 { 4920 *value = NAN; 4921 return 3; 4922 } 4923 f = strtod(s, &s); 4924 *value = f; 4925 return (int)((char_u *)s - text); 4926 } 4927 #endif 4928 4929 /* 4930 * Translate a String variable into a position. 4931 * Returns NULL when there is an error. 4932 */ 4933 pos_T * 4934 var2fpos( 4935 typval_T *varp, 4936 int dollar_lnum, // TRUE when $ is last line 4937 int *fnum) // set to fnum for '0, 'A, etc. 4938 { 4939 char_u *name; 4940 static pos_T pos; 4941 pos_T *pp; 4942 4943 // Argument can be [lnum, col, coladd]. 4944 if (varp->v_type == VAR_LIST) 4945 { 4946 list_T *l; 4947 int len; 4948 int error = FALSE; 4949 listitem_T *li; 4950 4951 l = varp->vval.v_list; 4952 if (l == NULL) 4953 return NULL; 4954 4955 // Get the line number 4956 pos.lnum = list_find_nr(l, 0L, &error); 4957 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count) 4958 return NULL; // invalid line number 4959 len = (long)STRLEN(ml_get(pos.lnum)); 4960 4961 // Get the column number 4962 // We accept "$" for the column number: last column. 4963 li = list_find(l, 1L); 4964 if (li != NULL && li->li_tv.v_type == VAR_STRING 4965 && li->li_tv.vval.v_string != NULL 4966 && STRCMP(li->li_tv.vval.v_string, "$") == 0) 4967 { 4968 pos.col = len + 1; 4969 } 4970 else 4971 { 4972 pos.col = list_find_nr(l, 1L, &error); 4973 if (error) 4974 return NULL; 4975 } 4976 4977 // Accept a position up to the NUL after the line. 4978 if (pos.col == 0 || (int)pos.col > len + 1) 4979 return NULL; // invalid column number 4980 --pos.col; 4981 4982 // Get the virtual offset. Defaults to zero. 4983 pos.coladd = list_find_nr(l, 2L, &error); 4984 if (error) 4985 pos.coladd = 0; 4986 4987 return &pos; 4988 } 4989 4990 name = tv_get_string_chk(varp); 4991 if (name == NULL) 4992 return NULL; 4993 if (name[0] == '.') // cursor 4994 return &curwin->w_cursor; 4995 if (name[0] == 'v' && name[1] == NUL) // Visual start 4996 { 4997 if (VIsual_active) 4998 return &VIsual; 4999 return &curwin->w_cursor; 5000 } 5001 if (name[0] == '\'') // mark 5002 { 5003 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum); 5004 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0) 5005 return NULL; 5006 return pp; 5007 } 5008 5009 pos.coladd = 0; 5010 5011 if (name[0] == 'w' && dollar_lnum) 5012 { 5013 pos.col = 0; 5014 if (name[1] == '0') // "w0": first visible line 5015 { 5016 update_topline(); 5017 // In silent Ex mode topline is zero, but that's not a valid line 5018 // number; use one instead. 5019 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1; 5020 return &pos; 5021 } 5022 else if (name[1] == '$') // "w$": last visible line 5023 { 5024 validate_botline(); 5025 // In silent Ex mode botline is zero, return zero then. 5026 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0; 5027 return &pos; 5028 } 5029 } 5030 else if (name[0] == '$') // last column or line 5031 { 5032 if (dollar_lnum) 5033 { 5034 pos.lnum = curbuf->b_ml.ml_line_count; 5035 pos.col = 0; 5036 } 5037 else 5038 { 5039 pos.lnum = curwin->w_cursor.lnum; 5040 pos.col = (colnr_T)STRLEN(ml_get_curline()); 5041 } 5042 return &pos; 5043 } 5044 return NULL; 5045 } 5046 5047 /* 5048 * Convert list in "arg" into a position and optional file number. 5049 * When "fnump" is NULL there is no file number, only 3 items. 5050 * Note that the column is passed on as-is, the caller may want to decrement 5051 * it to use 1 for the first column. 5052 * Return FAIL when conversion is not possible, doesn't check the position for 5053 * validity. 5054 */ 5055 int 5056 list2fpos( 5057 typval_T *arg, 5058 pos_T *posp, 5059 int *fnump, 5060 colnr_T *curswantp) 5061 { 5062 list_T *l = arg->vval.v_list; 5063 long i = 0; 5064 long n; 5065 5066 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only 5067 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional. 5068 if (arg->v_type != VAR_LIST 5069 || l == NULL 5070 || l->lv_len < (fnump == NULL ? 2 : 3) 5071 || l->lv_len > (fnump == NULL ? 4 : 5)) 5072 return FAIL; 5073 5074 if (fnump != NULL) 5075 { 5076 n = list_find_nr(l, i++, NULL); // fnum 5077 if (n < 0) 5078 return FAIL; 5079 if (n == 0) 5080 n = curbuf->b_fnum; // current buffer 5081 *fnump = n; 5082 } 5083 5084 n = list_find_nr(l, i++, NULL); // lnum 5085 if (n < 0) 5086 return FAIL; 5087 posp->lnum = n; 5088 5089 n = list_find_nr(l, i++, NULL); // col 5090 if (n < 0) 5091 return FAIL; 5092 posp->col = n; 5093 5094 n = list_find_nr(l, i, NULL); // off 5095 if (n < 0) 5096 posp->coladd = 0; 5097 else 5098 posp->coladd = n; 5099 5100 if (curswantp != NULL) 5101 *curswantp = list_find_nr(l, i + 1, NULL); // curswant 5102 5103 return OK; 5104 } 5105 5106 /* 5107 * Get the length of an environment variable name. 5108 * Advance "arg" to the first character after the name. 5109 * Return 0 for error. 5110 */ 5111 int 5112 get_env_len(char_u **arg) 5113 { 5114 char_u *p; 5115 int len; 5116 5117 for (p = *arg; vim_isIDc(*p); ++p) 5118 ; 5119 if (p == *arg) // no name found 5120 return 0; 5121 5122 len = (int)(p - *arg); 5123 *arg = p; 5124 return len; 5125 } 5126 5127 /* 5128 * Get the length of the name of a function or internal variable. 5129 * "arg" is advanced to after the name. 5130 * Return 0 if something is wrong. 5131 */ 5132 int 5133 get_id_len(char_u **arg) 5134 { 5135 char_u *p; 5136 int len; 5137 5138 // Find the end of the name. 5139 for (p = *arg; eval_isnamec(*p); ++p) 5140 { 5141 if (*p == ':') 5142 { 5143 // "s:" is start of "s:var", but "n:" is not and can be used in 5144 // slice "[n:]". Also "xx:" is not a namespace. 5145 len = (int)(p - *arg); 5146 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL) 5147 || len > 1) 5148 break; 5149 } 5150 } 5151 if (p == *arg) // no name found 5152 return 0; 5153 5154 len = (int)(p - *arg); 5155 *arg = p; 5156 5157 return len; 5158 } 5159 5160 /* 5161 * Get the length of the name of a variable or function. 5162 * Only the name is recognized, does not handle ".key" or "[idx]". 5163 * "arg" is advanced to the first non-white character after the name. 5164 * Return -1 if curly braces expansion failed. 5165 * Return 0 if something else is wrong. 5166 * If the name contains 'magic' {}'s, expand them and return the 5167 * expanded name in an allocated string via 'alias' - caller must free. 5168 */ 5169 int 5170 get_name_len( 5171 char_u **arg, 5172 char_u **alias, 5173 int evaluate, 5174 int verbose) 5175 { 5176 int len; 5177 char_u *p; 5178 char_u *expr_start; 5179 char_u *expr_end; 5180 5181 *alias = NULL; // default to no alias 5182 5183 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA 5184 && (*arg)[2] == (int)KE_SNR) 5185 { 5186 // hard coded <SNR>, already translated 5187 *arg += 3; 5188 return get_id_len(arg) + 3; 5189 } 5190 len = eval_fname_script(*arg); 5191 if (len > 0) 5192 { 5193 // literal "<SID>", "s:" or "<SNR>" 5194 *arg += len; 5195 } 5196 5197 /* 5198 * Find the end of the name; check for {} construction. 5199 */ 5200 p = find_name_end(*arg, &expr_start, &expr_end, 5201 len > 0 ? 0 : FNE_CHECK_START); 5202 if (expr_start != NULL) 5203 { 5204 char_u *temp_string; 5205 5206 if (!evaluate) 5207 { 5208 len += (int)(p - *arg); 5209 *arg = skipwhite(p); 5210 return len; 5211 } 5212 5213 /* 5214 * Include any <SID> etc in the expanded string: 5215 * Thus the -len here. 5216 */ 5217 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p); 5218 if (temp_string == NULL) 5219 return -1; 5220 *alias = temp_string; 5221 *arg = skipwhite(p); 5222 return (int)STRLEN(temp_string); 5223 } 5224 5225 len += get_id_len(arg); 5226 // Only give an error when there is something, otherwise it will be 5227 // reported at a higher level. 5228 if (len == 0 && verbose && **arg != NUL) 5229 semsg(_(e_invexpr2), *arg); 5230 5231 return len; 5232 } 5233 5234 /* 5235 * Find the end of a variable or function name, taking care of magic braces. 5236 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the 5237 * start and end of the first magic braces item. 5238 * "flags" can have FNE_INCL_BR and FNE_CHECK_START. 5239 * Return a pointer to just after the name. Equal to "arg" if there is no 5240 * valid name. 5241 */ 5242 char_u * 5243 find_name_end( 5244 char_u *arg, 5245 char_u **expr_start, 5246 char_u **expr_end, 5247 int flags) 5248 { 5249 int mb_nest = 0; 5250 int br_nest = 0; 5251 char_u *p; 5252 int len; 5253 int vim9script = in_vim9script(); 5254 5255 if (expr_start != NULL) 5256 { 5257 *expr_start = NULL; 5258 *expr_end = NULL; 5259 } 5260 5261 // Quick check for valid starting character. 5262 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) 5263 && (*arg != '{' || vim9script)) 5264 return arg; 5265 5266 for (p = arg; *p != NUL 5267 && (eval_isnamec(*p) 5268 || (*p == '{' && !vim9script) 5269 || ((flags & FNE_INCL_BR) && (*p == '[' 5270 || (*p == '.' && eval_isdictc(p[1])))) 5271 || mb_nest != 0 5272 || br_nest != 0); MB_PTR_ADV(p)) 5273 { 5274 if (*p == '\'') 5275 { 5276 // skip over 'string' to avoid counting [ and ] inside it. 5277 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p)) 5278 ; 5279 if (*p == NUL) 5280 break; 5281 } 5282 else if (*p == '"') 5283 { 5284 // skip over "str\"ing" to avoid counting [ and ] inside it. 5285 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p)) 5286 if (*p == '\\' && p[1] != NUL) 5287 ++p; 5288 if (*p == NUL) 5289 break; 5290 } 5291 else if (br_nest == 0 && mb_nest == 0 && *p == ':') 5292 { 5293 // "s:" is start of "s:var", but "n:" is not and can be used in 5294 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. 5295 len = (int)(p - arg); 5296 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL) 5297 || (len > 1 && p[-1] != '}')) 5298 break; 5299 } 5300 5301 if (mb_nest == 0) 5302 { 5303 if (*p == '[') 5304 ++br_nest; 5305 else if (*p == ']') 5306 --br_nest; 5307 } 5308 5309 if (br_nest == 0 && !vim9script) 5310 { 5311 if (*p == '{') 5312 { 5313 mb_nest++; 5314 if (expr_start != NULL && *expr_start == NULL) 5315 *expr_start = p; 5316 } 5317 else if (*p == '}') 5318 { 5319 mb_nest--; 5320 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL) 5321 *expr_end = p; 5322 } 5323 } 5324 } 5325 5326 return p; 5327 } 5328 5329 /* 5330 * Expands out the 'magic' {}'s in a variable/function name. 5331 * Note that this can call itself recursively, to deal with 5332 * constructs like foo{bar}{baz}{bam} 5333 * The four pointer arguments point to "foo{expre}ss{ion}bar" 5334 * "in_start" ^ 5335 * "expr_start" ^ 5336 * "expr_end" ^ 5337 * "in_end" ^ 5338 * 5339 * Returns a new allocated string, which the caller must free. 5340 * Returns NULL for failure. 5341 */ 5342 static char_u * 5343 make_expanded_name( 5344 char_u *in_start, 5345 char_u *expr_start, 5346 char_u *expr_end, 5347 char_u *in_end) 5348 { 5349 char_u c1; 5350 char_u *retval = NULL; 5351 char_u *temp_result; 5352 5353 if (expr_end == NULL || in_end == NULL) 5354 return NULL; 5355 *expr_start = NUL; 5356 *expr_end = NUL; 5357 c1 = *in_end; 5358 *in_end = NUL; 5359 5360 temp_result = eval_to_string(expr_start + 1, FALSE); 5361 if (temp_result != NULL) 5362 { 5363 retval = alloc(STRLEN(temp_result) + (expr_start - in_start) 5364 + (in_end - expr_end) + 1); 5365 if (retval != NULL) 5366 { 5367 STRCPY(retval, in_start); 5368 STRCAT(retval, temp_result); 5369 STRCAT(retval, expr_end + 1); 5370 } 5371 } 5372 vim_free(temp_result); 5373 5374 *in_end = c1; // put char back for error messages 5375 *expr_start = '{'; 5376 *expr_end = '}'; 5377 5378 if (retval != NULL) 5379 { 5380 temp_result = find_name_end(retval, &expr_start, &expr_end, 0); 5381 if (expr_start != NULL) 5382 { 5383 // Further expansion! 5384 temp_result = make_expanded_name(retval, expr_start, 5385 expr_end, temp_result); 5386 vim_free(retval); 5387 retval = temp_result; 5388 } 5389 } 5390 5391 return retval; 5392 } 5393 5394 /* 5395 * Return TRUE if character "c" can be used in a variable or function name. 5396 * Does not include '{' or '}' for magic braces. 5397 */ 5398 int 5399 eval_isnamec(int c) 5400 { 5401 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR; 5402 } 5403 5404 /* 5405 * Return TRUE if character "c" can be used as the first character in a 5406 * variable or function name (excluding '{' and '}'). 5407 */ 5408 int 5409 eval_isnamec1(int c) 5410 { 5411 return ASCII_ISALPHA(c) || c == '_'; 5412 } 5413 5414 /* 5415 * Return TRUE if character "c" can be used as the first character of a 5416 * dictionary key. 5417 */ 5418 int 5419 eval_isdictc(int c) 5420 { 5421 return ASCII_ISALNUM(c) || c == '_'; 5422 } 5423 5424 /* 5425 * Return the character "str[index]" where "index" is the character index. If 5426 * "index" is out of range NULL is returned. 5427 */ 5428 char_u * 5429 char_from_string(char_u *str, varnumber_T index) 5430 { 5431 size_t nbyte = 0; 5432 varnumber_T nchar = index; 5433 size_t slen; 5434 5435 if (str == NULL || index < 0) 5436 return NULL; 5437 slen = STRLEN(str); 5438 while (nchar > 0 && nbyte < slen) 5439 { 5440 nbyte += MB_CPTR2LEN(str + nbyte); 5441 --nchar; 5442 } 5443 if (nbyte >= slen) 5444 return NULL; 5445 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte)); 5446 } 5447 5448 /* 5449 * Get the byte index for character index "idx" in string "str" with length 5450 * "str_len". 5451 * If going over the end return "str_len". 5452 * If "idx" is negative count from the end, -1 is the last character. 5453 * When going over the start return -1. 5454 */ 5455 static long 5456 char_idx2byte(char_u *str, size_t str_len, varnumber_T idx) 5457 { 5458 varnumber_T nchar = idx; 5459 size_t nbyte = 0; 5460 5461 if (nchar >= 0) 5462 { 5463 while (nchar > 0 && nbyte < str_len) 5464 { 5465 nbyte += MB_CPTR2LEN(str + nbyte); 5466 --nchar; 5467 } 5468 } 5469 else 5470 { 5471 nbyte = str_len; 5472 while (nchar < 0 && nbyte > 0) 5473 { 5474 --nbyte; 5475 nbyte -= mb_head_off(str, str + nbyte); 5476 ++nchar; 5477 } 5478 if (nchar < 0) 5479 return -1; 5480 } 5481 return (long)nbyte; 5482 } 5483 5484 /* 5485 * Return the slice "str[first:last]" using character indexes. 5486 * Return NULL when the result is empty. 5487 */ 5488 char_u * 5489 string_slice(char_u *str, varnumber_T first, varnumber_T last) 5490 { 5491 long start_byte, end_byte; 5492 size_t slen; 5493 5494 if (str == NULL) 5495 return NULL; 5496 slen = STRLEN(str); 5497 start_byte = char_idx2byte(str, slen, first); 5498 if (start_byte < 0) 5499 start_byte = 0; // first index very negative: use zero 5500 if (last == -1) 5501 end_byte = (long)slen; 5502 else 5503 { 5504 end_byte = char_idx2byte(str, slen, last); 5505 if (end_byte >= 0 && end_byte < (long)slen) 5506 // end index is inclusive 5507 end_byte += MB_CPTR2LEN(str + end_byte); 5508 } 5509 5510 if (start_byte >= (long)slen || end_byte <= start_byte) 5511 return NULL; 5512 return vim_strnsave(str + start_byte, end_byte - start_byte); 5513 } 5514 5515 /* 5516 * Handle: 5517 * - expr[expr], expr[expr:expr] subscript 5518 * - ".name" lookup 5519 * - function call with Funcref variable: func(expr) 5520 * - method call: var->method() 5521 * 5522 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len() 5523 */ 5524 int 5525 handle_subscript( 5526 char_u **arg, 5527 typval_T *rettv, 5528 evalarg_T *evalarg, 5529 int verbose) // give error messages 5530 { 5531 int evaluate = evalarg != NULL 5532 && (evalarg->eval_flags & EVAL_EVALUATE); 5533 int ret = OK; 5534 dict_T *selfdict = NULL; 5535 int check_white = TRUE; 5536 int getnext; 5537 char_u *p; 5538 5539 while (ret == OK) 5540 { 5541 // When at the end of the line and ".name" or "->{" or "->X" follows in 5542 // the next line then consume the line break. 5543 p = eval_next_non_blank(*arg, evalarg, &getnext); 5544 if (getnext 5545 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1])) 5546 || (p[0] == '-' && p[1] == '>' 5547 && (p[2] == '{' || ASCII_ISALPHA(p[2]))))) 5548 { 5549 *arg = eval_next_line(evalarg); 5550 p = *arg; 5551 check_white = FALSE; 5552 } 5553 5554 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC 5555 || rettv->v_type == VAR_PARTIAL)) 5556 && (!check_white || !VIM_ISWHITE(*(*arg - 1)))) 5557 { 5558 ret = call_func_rettv(arg, evalarg, rettv, evaluate, 5559 selfdict, NULL); 5560 5561 // Stop the expression evaluation when immediately aborting on 5562 // error, or when an interrupt occurred or an exception was thrown 5563 // but not caught. 5564 if (aborting()) 5565 { 5566 if (ret == OK) 5567 clear_tv(rettv); 5568 ret = FAIL; 5569 } 5570 dict_unref(selfdict); 5571 selfdict = NULL; 5572 } 5573 else if (p[0] == '-' && p[1] == '>') 5574 { 5575 *arg = p; 5576 if (ret == OK) 5577 { 5578 if ((*arg)[2] == '{') 5579 // expr->{lambda}() 5580 ret = eval_lambda(arg, rettv, evalarg, verbose); 5581 else 5582 // expr->name() 5583 ret = eval_method(arg, rettv, evalarg, verbose); 5584 } 5585 } 5586 // "." is ".name" lookup when we found a dict or when evaluating and 5587 // scriptversion is at least 2, where string concatenation is "..". 5588 else if (**arg == '[' 5589 || (**arg == '.' && (rettv->v_type == VAR_DICT 5590 || (!evaluate 5591 && (*arg)[1] != '.' 5592 && current_sctx.sc_version >= 2)))) 5593 { 5594 dict_unref(selfdict); 5595 if (rettv->v_type == VAR_DICT) 5596 { 5597 selfdict = rettv->vval.v_dict; 5598 if (selfdict != NULL) 5599 ++selfdict->dv_refcount; 5600 } 5601 else 5602 selfdict = NULL; 5603 if (eval_index(arg, rettv, evalarg, verbose) == FAIL) 5604 { 5605 clear_tv(rettv); 5606 ret = FAIL; 5607 } 5608 } 5609 else 5610 break; 5611 } 5612 5613 // Turn "dict.Func" into a partial for "Func" bound to "dict". 5614 // Don't do this when "Func" is already a partial that was bound 5615 // explicitly (pt_auto is FALSE). 5616 if (selfdict != NULL 5617 && (rettv->v_type == VAR_FUNC 5618 || (rettv->v_type == VAR_PARTIAL 5619 && (rettv->vval.v_partial->pt_auto 5620 || rettv->vval.v_partial->pt_dict == NULL)))) 5621 selfdict = make_partial(selfdict, rettv); 5622 5623 dict_unref(selfdict); 5624 return ret; 5625 } 5626 5627 /* 5628 * Make a copy of an item. 5629 * Lists and Dictionaries are also copied. A deep copy if "deep" is set. 5630 * For deepcopy() "copyID" is zero for a full copy or the ID for when a 5631 * reference to an already copied list/dict can be used. 5632 * Returns FAIL or OK. 5633 */ 5634 int 5635 item_copy( 5636 typval_T *from, 5637 typval_T *to, 5638 int deep, 5639 int copyID) 5640 { 5641 static int recurse = 0; 5642 int ret = OK; 5643 5644 if (recurse >= DICT_MAXNEST) 5645 { 5646 emsg(_("E698: variable nested too deep for making a copy")); 5647 return FAIL; 5648 } 5649 ++recurse; 5650 5651 switch (from->v_type) 5652 { 5653 case VAR_NUMBER: 5654 case VAR_FLOAT: 5655 case VAR_STRING: 5656 case VAR_FUNC: 5657 case VAR_PARTIAL: 5658 case VAR_BOOL: 5659 case VAR_SPECIAL: 5660 case VAR_JOB: 5661 case VAR_CHANNEL: 5662 copy_tv(from, to); 5663 break; 5664 case VAR_LIST: 5665 to->v_type = VAR_LIST; 5666 to->v_lock = 0; 5667 if (from->vval.v_list == NULL) 5668 to->vval.v_list = NULL; 5669 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID) 5670 { 5671 // use the copy made earlier 5672 to->vval.v_list = from->vval.v_list->lv_copylist; 5673 ++to->vval.v_list->lv_refcount; 5674 } 5675 else 5676 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID); 5677 if (to->vval.v_list == NULL) 5678 ret = FAIL; 5679 break; 5680 case VAR_BLOB: 5681 ret = blob_copy(from->vval.v_blob, to); 5682 break; 5683 case VAR_DICT: 5684 to->v_type = VAR_DICT; 5685 to->v_lock = 0; 5686 if (from->vval.v_dict == NULL) 5687 to->vval.v_dict = NULL; 5688 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID) 5689 { 5690 // use the copy made earlier 5691 to->vval.v_dict = from->vval.v_dict->dv_copydict; 5692 ++to->vval.v_dict->dv_refcount; 5693 } 5694 else 5695 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID); 5696 if (to->vval.v_dict == NULL) 5697 ret = FAIL; 5698 break; 5699 case VAR_UNKNOWN: 5700 case VAR_ANY: 5701 case VAR_VOID: 5702 internal_error_no_abort("item_copy(UNKNOWN)"); 5703 ret = FAIL; 5704 } 5705 --recurse; 5706 return ret; 5707 } 5708 5709 void 5710 echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr) 5711 { 5712 char_u *tofree; 5713 char_u numbuf[NUMBUFLEN]; 5714 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID()); 5715 5716 if (*atstart) 5717 { 5718 *atstart = FALSE; 5719 // Call msg_start() after eval1(), evaluating the expression 5720 // may cause a message to appear. 5721 if (with_space) 5722 { 5723 // Mark the saved text as finishing the line, so that what 5724 // follows is displayed on a new line when scrolling back 5725 // at the more prompt. 5726 msg_sb_eol(); 5727 msg_start(); 5728 } 5729 } 5730 else if (with_space) 5731 msg_puts_attr(" ", echo_attr); 5732 5733 if (p != NULL) 5734 for ( ; *p != NUL && !got_int; ++p) 5735 { 5736 if (*p == '\n' || *p == '\r' || *p == TAB) 5737 { 5738 if (*p != TAB && *needclr) 5739 { 5740 // remove any text still there from the command 5741 msg_clr_eos(); 5742 *needclr = FALSE; 5743 } 5744 msg_putchar_attr(*p, echo_attr); 5745 } 5746 else 5747 { 5748 if (has_mbyte) 5749 { 5750 int i = (*mb_ptr2len)(p); 5751 5752 (void)msg_outtrans_len_attr(p, i, echo_attr); 5753 p += i - 1; 5754 } 5755 else 5756 (void)msg_outtrans_len_attr(p, 1, echo_attr); 5757 } 5758 } 5759 vim_free(tofree); 5760 } 5761 5762 /* 5763 * ":echo expr1 ..." print each argument separated with a space, add a 5764 * newline at the end. 5765 * ":echon expr1 ..." print each argument plain. 5766 */ 5767 void 5768 ex_echo(exarg_T *eap) 5769 { 5770 char_u *arg = eap->arg; 5771 typval_T rettv; 5772 char_u *p; 5773 int needclr = TRUE; 5774 int atstart = TRUE; 5775 int did_emsg_before = did_emsg; 5776 int called_emsg_before = called_emsg; 5777 evalarg_T evalarg; 5778 5779 fill_evalarg_from_eap(&evalarg, eap, eap->skip); 5780 5781 if (eap->skip) 5782 ++emsg_skip; 5783 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int) 5784 { 5785 // If eval1() causes an error message the text from the command may 5786 // still need to be cleared. E.g., "echo 22,44". 5787 need_clr_eos = needclr; 5788 5789 p = arg; 5790 if (eval1(&arg, &rettv, &evalarg) == FAIL) 5791 { 5792 /* 5793 * Report the invalid expression unless the expression evaluation 5794 * has been cancelled due to an aborting error, an interrupt, or an 5795 * exception. 5796 */ 5797 if (!aborting() && did_emsg == did_emsg_before 5798 && called_emsg == called_emsg_before) 5799 semsg(_(e_invexpr2), p); 5800 need_clr_eos = FALSE; 5801 break; 5802 } 5803 need_clr_eos = FALSE; 5804 5805 if (!eap->skip) 5806 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr); 5807 5808 clear_tv(&rettv); 5809 arg = skipwhite(arg); 5810 } 5811 eap->nextcmd = check_nextcmd(arg); 5812 clear_evalarg(&evalarg, eap); 5813 5814 if (eap->skip) 5815 --emsg_skip; 5816 else 5817 { 5818 // remove text that may still be there from the command 5819 if (needclr) 5820 msg_clr_eos(); 5821 if (eap->cmdidx == CMD_echo) 5822 msg_end(); 5823 } 5824 } 5825 5826 /* 5827 * ":echohl {name}". 5828 */ 5829 void 5830 ex_echohl(exarg_T *eap) 5831 { 5832 echo_attr = syn_name2attr(eap->arg); 5833 } 5834 5835 /* 5836 * Returns the :echo attribute 5837 */ 5838 int 5839 get_echo_attr(void) 5840 { 5841 return echo_attr; 5842 } 5843 5844 /* 5845 * ":execute expr1 ..." execute the result of an expression. 5846 * ":echomsg expr1 ..." Print a message 5847 * ":echoerr expr1 ..." Print an error 5848 * Each gets spaces around each argument and a newline at the end for 5849 * echo commands 5850 */ 5851 void 5852 ex_execute(exarg_T *eap) 5853 { 5854 char_u *arg = eap->arg; 5855 typval_T rettv; 5856 int ret = OK; 5857 char_u *p; 5858 garray_T ga; 5859 int len; 5860 5861 ga_init2(&ga, 1, 80); 5862 5863 if (eap->skip) 5864 ++emsg_skip; 5865 while (!ends_excmd2(eap->cmd, arg) || *arg == '"') 5866 { 5867 ret = eval1_emsg(&arg, &rettv, eap); 5868 if (ret == FAIL) 5869 break; 5870 5871 if (!eap->skip) 5872 { 5873 char_u buf[NUMBUFLEN]; 5874 5875 if (eap->cmdidx == CMD_execute) 5876 { 5877 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB) 5878 { 5879 emsg(_(e_inval_string)); 5880 p = NULL; 5881 } 5882 else 5883 p = tv_get_string_buf(&rettv, buf); 5884 } 5885 else 5886 p = tv_stringify(&rettv, buf); 5887 if (p == NULL) 5888 { 5889 clear_tv(&rettv); 5890 ret = FAIL; 5891 break; 5892 } 5893 len = (int)STRLEN(p); 5894 if (ga_grow(&ga, len + 2) == FAIL) 5895 { 5896 clear_tv(&rettv); 5897 ret = FAIL; 5898 break; 5899 } 5900 if (ga.ga_len) 5901 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' '; 5902 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p); 5903 ga.ga_len += len; 5904 } 5905 5906 clear_tv(&rettv); 5907 arg = skipwhite(arg); 5908 } 5909 5910 if (ret != FAIL && ga.ga_data != NULL) 5911 { 5912 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr) 5913 { 5914 // Mark the already saved text as finishing the line, so that what 5915 // follows is displayed on a new line when scrolling back at the 5916 // more prompt. 5917 msg_sb_eol(); 5918 } 5919 5920 if (eap->cmdidx == CMD_echomsg) 5921 { 5922 msg_attr(ga.ga_data, echo_attr); 5923 out_flush(); 5924 } 5925 else if (eap->cmdidx == CMD_echoerr) 5926 { 5927 int save_did_emsg = did_emsg; 5928 5929 // We don't want to abort following commands, restore did_emsg. 5930 emsg(ga.ga_data); 5931 if (!force_abort) 5932 did_emsg = save_did_emsg; 5933 } 5934 else if (eap->cmdidx == CMD_execute) 5935 do_cmdline((char_u *)ga.ga_data, 5936 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE); 5937 } 5938 5939 ga_clear(&ga); 5940 5941 if (eap->skip) 5942 --emsg_skip; 5943 5944 eap->nextcmd = check_nextcmd(arg); 5945 } 5946 5947 /* 5948 * Skip over the name of an option: "&option", "&g:option" or "&l:option". 5949 * "arg" points to the "&" or '+' when called, to "option" when returning. 5950 * Returns NULL when no option name found. Otherwise pointer to the char 5951 * after the option name. 5952 */ 5953 char_u * 5954 find_option_end(char_u **arg, int *opt_flags) 5955 { 5956 char_u *p = *arg; 5957 5958 ++p; 5959 if (*p == 'g' && p[1] == ':') 5960 { 5961 *opt_flags = OPT_GLOBAL; 5962 p += 2; 5963 } 5964 else if (*p == 'l' && p[1] == ':') 5965 { 5966 *opt_flags = OPT_LOCAL; 5967 p += 2; 5968 } 5969 else 5970 *opt_flags = 0; 5971 5972 if (!ASCII_ISALPHA(*p)) 5973 return NULL; 5974 *arg = p; 5975 5976 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL) 5977 p += 4; // termcap option 5978 else 5979 while (ASCII_ISALPHA(*p)) 5980 ++p; 5981 return p; 5982 } 5983 5984 /* 5985 * Display script name where an item was last set. 5986 * Should only be invoked when 'verbose' is non-zero. 5987 */ 5988 void 5989 last_set_msg(sctx_T script_ctx) 5990 { 5991 char_u *p; 5992 5993 if (script_ctx.sc_sid != 0) 5994 { 5995 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid)); 5996 if (p != NULL) 5997 { 5998 verbose_enter(); 5999 msg_puts(_("\n\tLast set from ")); 6000 msg_puts((char *)p); 6001 if (script_ctx.sc_lnum > 0) 6002 { 6003 msg_puts(_(line_msg)); 6004 msg_outnum((long)script_ctx.sc_lnum); 6005 } 6006 verbose_leave(); 6007 vim_free(p); 6008 } 6009 } 6010 } 6011 6012 #endif // FEAT_EVAL 6013 6014 /* 6015 * Perform a substitution on "str" with pattern "pat" and substitute "sub". 6016 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL. 6017 * "flags" can be "g" to do a global substitute. 6018 * Returns an allocated string, NULL for error. 6019 */ 6020 char_u * 6021 do_string_sub( 6022 char_u *str, 6023 char_u *pat, 6024 char_u *sub, 6025 typval_T *expr, 6026 char_u *flags) 6027 { 6028 int sublen; 6029 regmatch_T regmatch; 6030 int i; 6031 int do_all; 6032 char_u *tail; 6033 char_u *end; 6034 garray_T ga; 6035 char_u *ret; 6036 char_u *save_cpo; 6037 char_u *zero_width = NULL; 6038 6039 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here 6040 save_cpo = p_cpo; 6041 p_cpo = empty_option; 6042 6043 ga_init2(&ga, 1, 200); 6044 6045 do_all = (flags[0] == 'g'); 6046 6047 regmatch.rm_ic = p_ic; 6048 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING); 6049 if (regmatch.regprog != NULL) 6050 { 6051 tail = str; 6052 end = str + STRLEN(str); 6053 while (vim_regexec_nl(®match, str, (colnr_T)(tail - str))) 6054 { 6055 // Skip empty match except for first match. 6056 if (regmatch.startp[0] == regmatch.endp[0]) 6057 { 6058 if (zero_width == regmatch.startp[0]) 6059 { 6060 // avoid getting stuck on a match with an empty string 6061 i = mb_ptr2len(tail); 6062 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, 6063 (size_t)i); 6064 ga.ga_len += i; 6065 tail += i; 6066 continue; 6067 } 6068 zero_width = regmatch.startp[0]; 6069 } 6070 6071 /* 6072 * Get some space for a temporary buffer to do the substitution 6073 * into. It will contain: 6074 * - The text up to where the match is. 6075 * - The substituted text. 6076 * - The text after the match. 6077 */ 6078 sublen = vim_regsub(®match, sub, expr, tail, FALSE, TRUE, FALSE); 6079 if (ga_grow(&ga, (int)((end - tail) + sublen - 6080 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL) 6081 { 6082 ga_clear(&ga); 6083 break; 6084 } 6085 6086 // copy the text up to where the match is 6087 i = (int)(regmatch.startp[0] - tail); 6088 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i); 6089 // add the substituted text 6090 (void)vim_regsub(®match, sub, expr, (char_u *)ga.ga_data 6091 + ga.ga_len + i, TRUE, TRUE, FALSE); 6092 ga.ga_len += i + sublen - 1; 6093 tail = regmatch.endp[0]; 6094 if (*tail == NUL) 6095 break; 6096 if (!do_all) 6097 break; 6098 } 6099 6100 if (ga.ga_data != NULL) 6101 STRCPY((char *)ga.ga_data + ga.ga_len, tail); 6102 6103 vim_regfree(regmatch.regprog); 6104 } 6105 6106 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data); 6107 ga_clear(&ga); 6108 if (p_cpo == empty_option) 6109 p_cpo = save_cpo; 6110 else 6111 // Darn, evaluating {sub} expression or {expr} changed the value. 6112 free_string_option(save_cpo); 6113 6114 return ret; 6115 } 6116