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