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