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