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