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 * typval.c: functions that deal with a typval 12 */ 13 14 #include "vim.h" 15 16 #if defined(FEAT_EVAL) || defined(PROTO) 17 18 /* 19 * Allocate memory for a variable type-value, and make it empty (0 or NULL 20 * value). 21 */ 22 typval_T * 23 alloc_tv(void) 24 { 25 return ALLOC_CLEAR_ONE(typval_T); 26 } 27 28 /* 29 * Allocate memory for a variable type-value, and assign a string to it. 30 * The string "s" must have been allocated, it is consumed. 31 * Return NULL for out of memory, the variable otherwise. 32 */ 33 typval_T * 34 alloc_string_tv(char_u *s) 35 { 36 typval_T *rettv; 37 38 rettv = alloc_tv(); 39 if (rettv != NULL) 40 { 41 rettv->v_type = VAR_STRING; 42 rettv->vval.v_string = s; 43 } 44 else 45 vim_free(s); 46 return rettv; 47 } 48 49 /* 50 * Free the memory for a variable type-value. 51 */ 52 void 53 free_tv(typval_T *varp) 54 { 55 if (varp != NULL) 56 { 57 switch (varp->v_type) 58 { 59 case VAR_FUNC: 60 func_unref(varp->vval.v_string); 61 // FALLTHROUGH 62 case VAR_STRING: 63 vim_free(varp->vval.v_string); 64 break; 65 case VAR_PARTIAL: 66 partial_unref(varp->vval.v_partial); 67 break; 68 case VAR_BLOB: 69 blob_unref(varp->vval.v_blob); 70 break; 71 case VAR_LIST: 72 list_unref(varp->vval.v_list); 73 break; 74 case VAR_DICT: 75 dict_unref(varp->vval.v_dict); 76 break; 77 case VAR_JOB: 78 #ifdef FEAT_JOB_CHANNEL 79 job_unref(varp->vval.v_job); 80 break; 81 #endif 82 case VAR_CHANNEL: 83 #ifdef FEAT_JOB_CHANNEL 84 channel_unref(varp->vval.v_channel); 85 break; 86 #endif 87 case VAR_NUMBER: 88 case VAR_FLOAT: 89 case VAR_ANY: 90 case VAR_UNKNOWN: 91 case VAR_VOID: 92 case VAR_BOOL: 93 case VAR_SPECIAL: 94 break; 95 } 96 vim_free(varp); 97 } 98 } 99 100 /* 101 * Free the memory for a variable value and set the value to NULL or 0. 102 */ 103 void 104 clear_tv(typval_T *varp) 105 { 106 if (varp != NULL) 107 { 108 switch (varp->v_type) 109 { 110 case VAR_FUNC: 111 func_unref(varp->vval.v_string); 112 // FALLTHROUGH 113 case VAR_STRING: 114 VIM_CLEAR(varp->vval.v_string); 115 break; 116 case VAR_PARTIAL: 117 partial_unref(varp->vval.v_partial); 118 varp->vval.v_partial = NULL; 119 break; 120 case VAR_BLOB: 121 blob_unref(varp->vval.v_blob); 122 varp->vval.v_blob = NULL; 123 break; 124 case VAR_LIST: 125 list_unref(varp->vval.v_list); 126 varp->vval.v_list = NULL; 127 break; 128 case VAR_DICT: 129 dict_unref(varp->vval.v_dict); 130 varp->vval.v_dict = NULL; 131 break; 132 case VAR_NUMBER: 133 case VAR_BOOL: 134 case VAR_SPECIAL: 135 varp->vval.v_number = 0; 136 break; 137 case VAR_FLOAT: 138 #ifdef FEAT_FLOAT 139 varp->vval.v_float = 0.0; 140 break; 141 #endif 142 case VAR_JOB: 143 #ifdef FEAT_JOB_CHANNEL 144 job_unref(varp->vval.v_job); 145 varp->vval.v_job = NULL; 146 #endif 147 break; 148 case VAR_CHANNEL: 149 #ifdef FEAT_JOB_CHANNEL 150 channel_unref(varp->vval.v_channel); 151 varp->vval.v_channel = NULL; 152 #endif 153 case VAR_UNKNOWN: 154 case VAR_ANY: 155 case VAR_VOID: 156 break; 157 } 158 varp->v_lock = 0; 159 } 160 } 161 162 /* 163 * Set the value of a variable to NULL without freeing items. 164 */ 165 void 166 init_tv(typval_T *varp) 167 { 168 if (varp != NULL) 169 CLEAR_POINTER(varp); 170 } 171 172 /* 173 * Get the number value of a variable. 174 * If it is a String variable, uses vim_str2nr(). 175 * For incompatible types, return 0. 176 * tv_get_number_chk() is similar to tv_get_number(), but informs the 177 * caller of incompatible types: it sets *denote to TRUE if "denote" 178 * is not NULL or returns -1 otherwise. 179 */ 180 varnumber_T 181 tv_get_number(typval_T *varp) 182 { 183 int error = FALSE; 184 185 return tv_get_number_chk(varp, &error); // return 0L on error 186 } 187 188 varnumber_T 189 tv_get_number_chk(typval_T *varp, int *denote) 190 { 191 varnumber_T n = 0L; 192 193 switch (varp->v_type) 194 { 195 case VAR_NUMBER: 196 return varp->vval.v_number; 197 case VAR_FLOAT: 198 #ifdef FEAT_FLOAT 199 emsg(_("E805: Using a Float as a Number")); 200 break; 201 #endif 202 case VAR_FUNC: 203 case VAR_PARTIAL: 204 emsg(_("E703: Using a Funcref as a Number")); 205 break; 206 case VAR_STRING: 207 if (varp->vval.v_string != NULL) 208 vim_str2nr(varp->vval.v_string, NULL, NULL, 209 STR2NR_ALL, &n, NULL, 0, FALSE); 210 return n; 211 case VAR_LIST: 212 emsg(_("E745: Using a List as a Number")); 213 break; 214 case VAR_DICT: 215 emsg(_("E728: Using a Dictionary as a Number")); 216 break; 217 case VAR_BOOL: 218 case VAR_SPECIAL: 219 return varp->vval.v_number == VVAL_TRUE ? 1 : 0; 220 case VAR_JOB: 221 #ifdef FEAT_JOB_CHANNEL 222 emsg(_("E910: Using a Job as a Number")); 223 break; 224 #endif 225 case VAR_CHANNEL: 226 #ifdef FEAT_JOB_CHANNEL 227 emsg(_("E913: Using a Channel as a Number")); 228 break; 229 #endif 230 case VAR_BLOB: 231 emsg(_("E974: Using a Blob as a Number")); 232 break; 233 case VAR_UNKNOWN: 234 case VAR_ANY: 235 case VAR_VOID: 236 internal_error_no_abort("tv_get_number(UNKNOWN)"); 237 break; 238 } 239 if (denote == NULL) // useful for values that must be unsigned 240 n = -1; 241 else 242 *denote = TRUE; 243 return n; 244 } 245 246 #ifdef FEAT_FLOAT 247 float_T 248 tv_get_float(typval_T *varp) 249 { 250 switch (varp->v_type) 251 { 252 case VAR_NUMBER: 253 return (float_T)(varp->vval.v_number); 254 case VAR_FLOAT: 255 return varp->vval.v_float; 256 case VAR_FUNC: 257 case VAR_PARTIAL: 258 emsg(_("E891: Using a Funcref as a Float")); 259 break; 260 case VAR_STRING: 261 emsg(_("E892: Using a String as a Float")); 262 break; 263 case VAR_LIST: 264 emsg(_("E893: Using a List as a Float")); 265 break; 266 case VAR_DICT: 267 emsg(_("E894: Using a Dictionary as a Float")); 268 break; 269 case VAR_BOOL: 270 emsg(_("E362: Using a boolean value as a Float")); 271 break; 272 case VAR_SPECIAL: 273 emsg(_("E907: Using a special value as a Float")); 274 break; 275 case VAR_JOB: 276 # ifdef FEAT_JOB_CHANNEL 277 emsg(_("E911: Using a Job as a Float")); 278 break; 279 # endif 280 case VAR_CHANNEL: 281 # ifdef FEAT_JOB_CHANNEL 282 emsg(_("E914: Using a Channel as a Float")); 283 break; 284 # endif 285 case VAR_BLOB: 286 emsg(_("E975: Using a Blob as a Float")); 287 break; 288 case VAR_UNKNOWN: 289 case VAR_ANY: 290 case VAR_VOID: 291 internal_error_no_abort("tv_get_float(UNKNOWN)"); 292 break; 293 } 294 return 0; 295 } 296 #endif 297 298 /* 299 * Get the string value of a variable. 300 * If it is a Number variable, the number is converted into a string. 301 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE! 302 * tv_get_string_buf() uses a given buffer. 303 * If the String variable has never been set, return an empty string. 304 * Never returns NULL; 305 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return 306 * NULL on error. 307 */ 308 char_u * 309 tv_get_string(typval_T *varp) 310 { 311 static char_u mybuf[NUMBUFLEN]; 312 313 return tv_get_string_buf(varp, mybuf); 314 } 315 316 char_u * 317 tv_get_string_buf(typval_T *varp, char_u *buf) 318 { 319 char_u *res = tv_get_string_buf_chk(varp, buf); 320 321 return res != NULL ? res : (char_u *)""; 322 } 323 324 /* 325 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE! 326 */ 327 char_u * 328 tv_get_string_chk(typval_T *varp) 329 { 330 static char_u mybuf[NUMBUFLEN]; 331 332 return tv_get_string_buf_chk(varp, mybuf); 333 } 334 335 char_u * 336 tv_get_string_buf_chk(typval_T *varp, char_u *buf) 337 { 338 switch (varp->v_type) 339 { 340 case VAR_NUMBER: 341 vim_snprintf((char *)buf, NUMBUFLEN, "%lld", 342 (varnumber_T)varp->vval.v_number); 343 return buf; 344 case VAR_FUNC: 345 case VAR_PARTIAL: 346 emsg(_("E729: using Funcref as a String")); 347 break; 348 case VAR_LIST: 349 emsg(_("E730: using List as a String")); 350 break; 351 case VAR_DICT: 352 emsg(_("E731: using Dictionary as a String")); 353 break; 354 case VAR_FLOAT: 355 #ifdef FEAT_FLOAT 356 emsg(_(e_float_as_string)); 357 break; 358 #endif 359 case VAR_STRING: 360 if (varp->vval.v_string != NULL) 361 return varp->vval.v_string; 362 return (char_u *)""; 363 case VAR_BOOL: 364 case VAR_SPECIAL: 365 STRCPY(buf, get_var_special_name(varp->vval.v_number)); 366 return buf; 367 case VAR_BLOB: 368 emsg(_("E976: using Blob as a String")); 369 break; 370 case VAR_JOB: 371 #ifdef FEAT_JOB_CHANNEL 372 { 373 job_T *job = varp->vval.v_job; 374 char *status; 375 376 if (job == NULL) 377 return (char_u *)"no process"; 378 status = job->jv_status == JOB_FAILED ? "fail" 379 : job->jv_status >= JOB_ENDED ? "dead" 380 : "run"; 381 # ifdef UNIX 382 vim_snprintf((char *)buf, NUMBUFLEN, 383 "process %ld %s", (long)job->jv_pid, status); 384 # elif defined(MSWIN) 385 vim_snprintf((char *)buf, NUMBUFLEN, 386 "process %ld %s", 387 (long)job->jv_proc_info.dwProcessId, 388 status); 389 # else 390 // fall-back 391 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status); 392 # endif 393 return buf; 394 } 395 #endif 396 break; 397 case VAR_CHANNEL: 398 #ifdef FEAT_JOB_CHANNEL 399 { 400 channel_T *channel = varp->vval.v_channel; 401 char *status = channel_status(channel, -1); 402 403 if (channel == NULL) 404 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status); 405 else 406 vim_snprintf((char *)buf, NUMBUFLEN, 407 "channel %d %s", channel->ch_id, status); 408 return buf; 409 } 410 #endif 411 break; 412 case VAR_UNKNOWN: 413 case VAR_ANY: 414 case VAR_VOID: 415 emsg(_(e_inval_string)); 416 break; 417 } 418 return NULL; 419 } 420 421 /* 422 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses 423 * string() on Dict, List, etc. 424 */ 425 char_u * 426 tv_stringify(typval_T *varp, char_u *buf) 427 { 428 if (varp->v_type == VAR_LIST 429 || varp->v_type == VAR_DICT 430 || varp->v_type == VAR_BLOB 431 || varp->v_type == VAR_FUNC 432 || varp->v_type == VAR_PARTIAL 433 || varp->v_type == VAR_FLOAT) 434 { 435 typval_T tmp; 436 437 f_string(varp, &tmp); 438 tv_get_string_buf(&tmp, buf); 439 clear_tv(varp); 440 *varp = tmp; 441 return tmp.vval.v_string; 442 } 443 return tv_get_string_buf(varp, buf); 444 } 445 446 /* 447 * Return TRUE if typeval "tv" and its value are set to be locked (immutable). 448 * Also give an error message, using "name" or _("name") when use_gettext is 449 * TRUE. 450 */ 451 int 452 tv_check_lock(typval_T *tv, char_u *name, int use_gettext) 453 { 454 int lock = 0; 455 456 switch (tv->v_type) 457 { 458 case VAR_BLOB: 459 if (tv->vval.v_blob != NULL) 460 lock = tv->vval.v_blob->bv_lock; 461 break; 462 case VAR_LIST: 463 if (tv->vval.v_list != NULL) 464 lock = tv->vval.v_list->lv_lock; 465 break; 466 case VAR_DICT: 467 if (tv->vval.v_dict != NULL) 468 lock = tv->vval.v_dict->dv_lock; 469 break; 470 default: 471 break; 472 } 473 return var_check_lock(tv->v_lock, name, use_gettext) 474 || (lock != 0 && var_check_lock(lock, name, use_gettext)); 475 } 476 477 /* 478 * Copy the values from typval_T "from" to typval_T "to". 479 * When needed allocates string or increases reference count. 480 * Does not make a copy of a list, blob or dict but copies the reference! 481 * It is OK for "from" and "to" to point to the same item. This is used to 482 * make a copy later. 483 */ 484 void 485 copy_tv(typval_T *from, typval_T *to) 486 { 487 to->v_type = from->v_type; 488 to->v_lock = 0; 489 switch (from->v_type) 490 { 491 case VAR_NUMBER: 492 case VAR_BOOL: 493 case VAR_SPECIAL: 494 to->vval.v_number = from->vval.v_number; 495 break; 496 case VAR_FLOAT: 497 #ifdef FEAT_FLOAT 498 to->vval.v_float = from->vval.v_float; 499 break; 500 #endif 501 case VAR_JOB: 502 #ifdef FEAT_JOB_CHANNEL 503 to->vval.v_job = from->vval.v_job; 504 if (to->vval.v_job != NULL) 505 ++to->vval.v_job->jv_refcount; 506 break; 507 #endif 508 case VAR_CHANNEL: 509 #ifdef FEAT_JOB_CHANNEL 510 to->vval.v_channel = from->vval.v_channel; 511 if (to->vval.v_channel != NULL) 512 ++to->vval.v_channel->ch_refcount; 513 break; 514 #endif 515 case VAR_STRING: 516 case VAR_FUNC: 517 if (from->vval.v_string == NULL) 518 to->vval.v_string = NULL; 519 else 520 { 521 to->vval.v_string = vim_strsave(from->vval.v_string); 522 if (from->v_type == VAR_FUNC) 523 func_ref(to->vval.v_string); 524 } 525 break; 526 case VAR_PARTIAL: 527 if (from->vval.v_partial == NULL) 528 to->vval.v_partial = NULL; 529 else 530 { 531 to->vval.v_partial = from->vval.v_partial; 532 ++to->vval.v_partial->pt_refcount; 533 } 534 break; 535 case VAR_BLOB: 536 if (from->vval.v_blob == NULL) 537 to->vval.v_blob = NULL; 538 else 539 { 540 to->vval.v_blob = from->vval.v_blob; 541 ++to->vval.v_blob->bv_refcount; 542 } 543 break; 544 case VAR_LIST: 545 if (from->vval.v_list == NULL) 546 to->vval.v_list = NULL; 547 else 548 { 549 to->vval.v_list = from->vval.v_list; 550 ++to->vval.v_list->lv_refcount; 551 } 552 break; 553 case VAR_DICT: 554 if (from->vval.v_dict == NULL) 555 to->vval.v_dict = NULL; 556 else 557 { 558 to->vval.v_dict = from->vval.v_dict; 559 ++to->vval.v_dict->dv_refcount; 560 } 561 break; 562 case VAR_UNKNOWN: 563 case VAR_ANY: 564 case VAR_VOID: 565 internal_error_no_abort("copy_tv(UNKNOWN)"); 566 break; 567 } 568 } 569 570 /* 571 * Compare "typ1" and "typ2". Put the result in "typ1". 572 */ 573 int 574 typval_compare( 575 typval_T *typ1, // first operand 576 typval_T *typ2, // second operand 577 exptype_T type, // operator 578 int ic) // ignore case 579 { 580 int i; 581 varnumber_T n1, n2; 582 char_u *s1, *s2; 583 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN]; 584 int type_is = type == EXPR_IS || type == EXPR_ISNOT; 585 586 if (type_is && typ1->v_type != typ2->v_type) 587 { 588 // For "is" a different type always means FALSE, for "notis" 589 // it means TRUE. 590 n1 = (type == EXPR_ISNOT); 591 } 592 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB) 593 { 594 if (type_is) 595 { 596 n1 = (typ1->v_type == typ2->v_type 597 && typ1->vval.v_blob == typ2->vval.v_blob); 598 if (type == EXPR_ISNOT) 599 n1 = !n1; 600 } 601 else if (typ1->v_type != typ2->v_type 602 || (type != EXPR_EQUAL && type != EXPR_NEQUAL)) 603 { 604 if (typ1->v_type != typ2->v_type) 605 emsg(_("E977: Can only compare Blob with Blob")); 606 else 607 emsg(_(e_invalblob)); 608 clear_tv(typ1); 609 return FAIL; 610 } 611 else 612 { 613 // Compare two Blobs for being equal or unequal. 614 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob); 615 if (type == EXPR_NEQUAL) 616 n1 = !n1; 617 } 618 } 619 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST) 620 { 621 if (type_is) 622 { 623 n1 = (typ1->v_type == typ2->v_type 624 && typ1->vval.v_list == typ2->vval.v_list); 625 if (type == EXPR_ISNOT) 626 n1 = !n1; 627 } 628 else if (typ1->v_type != typ2->v_type 629 || (type != EXPR_EQUAL && type != EXPR_NEQUAL)) 630 { 631 if (typ1->v_type != typ2->v_type) 632 emsg(_("E691: Can only compare List with List")); 633 else 634 emsg(_("E692: Invalid operation for List")); 635 clear_tv(typ1); 636 return FAIL; 637 } 638 else 639 { 640 // Compare two Lists for being equal or unequal. 641 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list, 642 ic, FALSE); 643 if (type == EXPR_NEQUAL) 644 n1 = !n1; 645 } 646 } 647 648 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT) 649 { 650 if (type_is) 651 { 652 n1 = (typ1->v_type == typ2->v_type 653 && typ1->vval.v_dict == typ2->vval.v_dict); 654 if (type == EXPR_ISNOT) 655 n1 = !n1; 656 } 657 else if (typ1->v_type != typ2->v_type 658 || (type != EXPR_EQUAL && type != EXPR_NEQUAL)) 659 { 660 if (typ1->v_type != typ2->v_type) 661 emsg(_("E735: Can only compare Dictionary with Dictionary")); 662 else 663 emsg(_("E736: Invalid operation for Dictionary")); 664 clear_tv(typ1); 665 return FAIL; 666 } 667 else 668 { 669 // Compare two Dictionaries for being equal or unequal. 670 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict, 671 ic, FALSE); 672 if (type == EXPR_NEQUAL) 673 n1 = !n1; 674 } 675 } 676 677 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC 678 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL) 679 { 680 if (type != EXPR_EQUAL && type != EXPR_NEQUAL 681 && type != EXPR_IS && type != EXPR_ISNOT) 682 { 683 emsg(_("E694: Invalid operation for Funcrefs")); 684 clear_tv(typ1); 685 return FAIL; 686 } 687 if ((typ1->v_type == VAR_PARTIAL 688 && typ1->vval.v_partial == NULL) 689 || (typ2->v_type == VAR_PARTIAL 690 && typ2->vval.v_partial == NULL)) 691 // When both partials are NULL, then they are equal. 692 // Otherwise they are not equal. 693 n1 = (typ1->vval.v_partial == typ2->vval.v_partial); 694 else if (type_is) 695 { 696 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC) 697 // strings are considered the same if their value is 698 // the same 699 n1 = tv_equal(typ1, typ2, ic, FALSE); 700 else if (typ1->v_type == VAR_PARTIAL 701 && typ2->v_type == VAR_PARTIAL) 702 n1 = (typ1->vval.v_partial == typ2->vval.v_partial); 703 else 704 n1 = FALSE; 705 } 706 else 707 n1 = tv_equal(typ1, typ2, ic, FALSE); 708 if (type == EXPR_NEQUAL || type == EXPR_ISNOT) 709 n1 = !n1; 710 } 711 712 #ifdef FEAT_FLOAT 713 // If one of the two variables is a float, compare as a float. 714 // When using "=~" or "!~", always compare as string. 715 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT) 716 && type != EXPR_MATCH && type != EXPR_NOMATCH) 717 { 718 float_T f1, f2; 719 720 f1 = tv_get_float(typ1); 721 f2 = tv_get_float(typ2); 722 n1 = FALSE; 723 switch (type) 724 { 725 case EXPR_IS: 726 case EXPR_EQUAL: n1 = (f1 == f2); break; 727 case EXPR_ISNOT: 728 case EXPR_NEQUAL: n1 = (f1 != f2); break; 729 case EXPR_GREATER: n1 = (f1 > f2); break; 730 case EXPR_GEQUAL: n1 = (f1 >= f2); break; 731 case EXPR_SMALLER: n1 = (f1 < f2); break; 732 case EXPR_SEQUAL: n1 = (f1 <= f2); break; 733 case EXPR_UNKNOWN: 734 case EXPR_MATCH: 735 default: break; // avoid gcc warning 736 } 737 } 738 #endif 739 740 // If one of the two variables is a number, compare as a number. 741 // When using "=~" or "!~", always compare as string. 742 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER) 743 && type != EXPR_MATCH && type != EXPR_NOMATCH) 744 { 745 n1 = tv_get_number(typ1); 746 n2 = tv_get_number(typ2); 747 switch (type) 748 { 749 case EXPR_IS: 750 case EXPR_EQUAL: n1 = (n1 == n2); break; 751 case EXPR_ISNOT: 752 case EXPR_NEQUAL: n1 = (n1 != n2); break; 753 case EXPR_GREATER: n1 = (n1 > n2); break; 754 case EXPR_GEQUAL: n1 = (n1 >= n2); break; 755 case EXPR_SMALLER: n1 = (n1 < n2); break; 756 case EXPR_SEQUAL: n1 = (n1 <= n2); break; 757 case EXPR_UNKNOWN: 758 case EXPR_MATCH: 759 default: break; // avoid gcc warning 760 } 761 } 762 else 763 { 764 s1 = tv_get_string_buf(typ1, buf1); 765 s2 = tv_get_string_buf(typ2, buf2); 766 if (type != EXPR_MATCH && type != EXPR_NOMATCH) 767 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2); 768 else 769 i = 0; 770 n1 = FALSE; 771 switch (type) 772 { 773 case EXPR_IS: 774 case EXPR_EQUAL: n1 = (i == 0); break; 775 case EXPR_ISNOT: 776 case EXPR_NEQUAL: n1 = (i != 0); break; 777 case EXPR_GREATER: n1 = (i > 0); break; 778 case EXPR_GEQUAL: n1 = (i >= 0); break; 779 case EXPR_SMALLER: n1 = (i < 0); break; 780 case EXPR_SEQUAL: n1 = (i <= 0); break; 781 782 case EXPR_MATCH: 783 case EXPR_NOMATCH: 784 n1 = pattern_match(s2, s1, ic); 785 if (type == EXPR_NOMATCH) 786 n1 = !n1; 787 break; 788 789 default: break; // avoid gcc warning 790 } 791 } 792 clear_tv(typ1); 793 if (in_vim9script()) 794 { 795 typ1->v_type = VAR_BOOL; 796 typ1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE; 797 } 798 else 799 { 800 typ1->v_type = VAR_NUMBER; 801 typ1->vval.v_number = n1; 802 } 803 804 return OK; 805 } 806 807 char_u * 808 typval_tostring(typval_T *arg) 809 { 810 char_u *tofree; 811 char_u numbuf[NUMBUFLEN]; 812 char_u *ret = NULL; 813 814 if (arg == NULL) 815 return vim_strsave((char_u *)"(does not exist)"); 816 ret = tv2string(arg, &tofree, numbuf, 0); 817 // Make a copy if we have a value but it's not in allocated memory. 818 if (ret != NULL && tofree == NULL) 819 ret = vim_strsave(ret); 820 return ret; 821 } 822 823 /* 824 * Return TRUE if typeval "tv" is locked: Either that value is locked itself 825 * or it refers to a List or Dictionary that is locked. 826 */ 827 int 828 tv_islocked(typval_T *tv) 829 { 830 return (tv->v_lock & VAR_LOCKED) 831 || (tv->v_type == VAR_LIST 832 && tv->vval.v_list != NULL 833 && (tv->vval.v_list->lv_lock & VAR_LOCKED)) 834 || (tv->v_type == VAR_DICT 835 && tv->vval.v_dict != NULL 836 && (tv->vval.v_dict->dv_lock & VAR_LOCKED)); 837 } 838 839 static int 840 func_equal( 841 typval_T *tv1, 842 typval_T *tv2, 843 int ic) // ignore case 844 { 845 char_u *s1, *s2; 846 dict_T *d1, *d2; 847 int a1, a2; 848 int i; 849 850 // empty and NULL function name considered the same 851 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string 852 : partial_name(tv1->vval.v_partial); 853 if (s1 != NULL && *s1 == NUL) 854 s1 = NULL; 855 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string 856 : partial_name(tv2->vval.v_partial); 857 if (s2 != NULL && *s2 == NUL) 858 s2 = NULL; 859 if (s1 == NULL || s2 == NULL) 860 { 861 if (s1 != s2) 862 return FALSE; 863 } 864 else if (STRCMP(s1, s2) != 0) 865 return FALSE; 866 867 // empty dict and NULL dict is different 868 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict; 869 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict; 870 if (d1 == NULL || d2 == NULL) 871 { 872 if (d1 != d2) 873 return FALSE; 874 } 875 else if (!dict_equal(d1, d2, ic, TRUE)) 876 return FALSE; 877 878 // empty list and no list considered the same 879 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc; 880 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc; 881 if (a1 != a2) 882 return FALSE; 883 for (i = 0; i < a1; ++i) 884 if (!tv_equal(tv1->vval.v_partial->pt_argv + i, 885 tv2->vval.v_partial->pt_argv + i, ic, TRUE)) 886 return FALSE; 887 888 return TRUE; 889 } 890 891 /* 892 * Return TRUE if "tv1" and "tv2" have the same value. 893 * Compares the items just like "==" would compare them, but strings and 894 * numbers are different. Floats and numbers are also different. 895 */ 896 int 897 tv_equal( 898 typval_T *tv1, 899 typval_T *tv2, 900 int ic, // ignore case 901 int recursive) // TRUE when used recursively 902 { 903 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN]; 904 char_u *s1, *s2; 905 static int recursive_cnt = 0; // catch recursive loops 906 int r; 907 static int tv_equal_recurse_limit; 908 909 // Catch lists and dicts that have an endless loop by limiting 910 // recursiveness to a limit. We guess they are equal then. 911 // A fixed limit has the problem of still taking an awful long time. 912 // Reduce the limit every time running into it. That should work fine for 913 // deeply linked structures that are not recursively linked and catch 914 // recursiveness quickly. 915 if (!recursive) 916 tv_equal_recurse_limit = 1000; 917 if (recursive_cnt >= tv_equal_recurse_limit) 918 { 919 --tv_equal_recurse_limit; 920 return TRUE; 921 } 922 923 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and 924 // arguments. 925 if ((tv1->v_type == VAR_FUNC 926 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL)) 927 && (tv2->v_type == VAR_FUNC 928 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL))) 929 { 930 ++recursive_cnt; 931 r = func_equal(tv1, tv2, ic); 932 --recursive_cnt; 933 return r; 934 } 935 936 if (tv1->v_type != tv2->v_type) 937 return FALSE; 938 939 switch (tv1->v_type) 940 { 941 case VAR_LIST: 942 ++recursive_cnt; 943 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE); 944 --recursive_cnt; 945 return r; 946 947 case VAR_DICT: 948 ++recursive_cnt; 949 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE); 950 --recursive_cnt; 951 return r; 952 953 case VAR_BLOB: 954 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob); 955 956 case VAR_NUMBER: 957 case VAR_BOOL: 958 case VAR_SPECIAL: 959 return tv1->vval.v_number == tv2->vval.v_number; 960 961 case VAR_STRING: 962 s1 = tv_get_string_buf(tv1, buf1); 963 s2 = tv_get_string_buf(tv2, buf2); 964 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0); 965 966 case VAR_FLOAT: 967 #ifdef FEAT_FLOAT 968 return tv1->vval.v_float == tv2->vval.v_float; 969 #endif 970 case VAR_JOB: 971 #ifdef FEAT_JOB_CHANNEL 972 return tv1->vval.v_job == tv2->vval.v_job; 973 #endif 974 case VAR_CHANNEL: 975 #ifdef FEAT_JOB_CHANNEL 976 return tv1->vval.v_channel == tv2->vval.v_channel; 977 #endif 978 979 case VAR_PARTIAL: 980 return tv1->vval.v_partial == tv2->vval.v_partial; 981 982 case VAR_FUNC: 983 return tv1->vval.v_string == tv2->vval.v_string; 984 985 case VAR_UNKNOWN: 986 case VAR_ANY: 987 case VAR_VOID: 988 break; 989 } 990 991 // VAR_UNKNOWN can be the result of a invalid expression, let's say it 992 // does not equal anything, not even itself. 993 return FALSE; 994 } 995 996 /* 997 * Get an option value. 998 * "arg" points to the '&' or '+' before the option name. 999 * "arg" is advanced to character after the option name. 1000 * Return OK or FAIL. 1001 */ 1002 int 1003 eval_option( 1004 char_u **arg, 1005 typval_T *rettv, // when NULL, only check if option exists 1006 int evaluate) 1007 { 1008 char_u *option_end; 1009 long numval; 1010 char_u *stringval; 1011 int opt_type; 1012 int c; 1013 int working = (**arg == '+'); // has("+option") 1014 int ret = OK; 1015 int opt_flags; 1016 1017 // Isolate the option name and find its value. 1018 option_end = find_option_end(arg, &opt_flags); 1019 if (option_end == NULL) 1020 { 1021 if (rettv != NULL) 1022 semsg(_("E112: Option name missing: %s"), *arg); 1023 return FAIL; 1024 } 1025 1026 if (!evaluate) 1027 { 1028 *arg = option_end; 1029 return OK; 1030 } 1031 1032 c = *option_end; 1033 *option_end = NUL; 1034 opt_type = get_option_value(*arg, &numval, 1035 rettv == NULL ? NULL : &stringval, opt_flags); 1036 1037 if (opt_type == -3) // invalid name 1038 { 1039 if (rettv != NULL) 1040 semsg(_(e_unknown_option), *arg); 1041 ret = FAIL; 1042 } 1043 else if (rettv != NULL) 1044 { 1045 if (opt_type == -2) // hidden string option 1046 { 1047 rettv->v_type = VAR_STRING; 1048 rettv->vval.v_string = NULL; 1049 } 1050 else if (opt_type == -1) // hidden number option 1051 { 1052 rettv->v_type = VAR_NUMBER; 1053 rettv->vval.v_number = 0; 1054 } 1055 else if (opt_type == 1) // number option 1056 { 1057 rettv->v_type = VAR_NUMBER; 1058 rettv->vval.v_number = numval; 1059 } 1060 else // string option 1061 { 1062 rettv->v_type = VAR_STRING; 1063 rettv->vval.v_string = stringval; 1064 } 1065 } 1066 else if (working && (opt_type == -2 || opt_type == -1)) 1067 ret = FAIL; 1068 1069 *option_end = c; // put back for error messages 1070 *arg = option_end; 1071 1072 return ret; 1073 } 1074 1075 /* 1076 * Allocate a variable for a number constant. Also deals with "0z" for blob. 1077 * Return OK or FAIL. 1078 */ 1079 int 1080 eval_number( 1081 char_u **arg, 1082 typval_T *rettv, 1083 int evaluate, 1084 int want_string UNUSED) 1085 { 1086 int len; 1087 #ifdef FEAT_FLOAT 1088 char_u *p; 1089 int get_float = FALSE; 1090 1091 // We accept a float when the format matches 1092 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very 1093 // strict to avoid backwards compatibility problems. 1094 // With script version 2 and later the leading digit can be 1095 // omitted. 1096 // Don't look for a float after the "." operator, so that 1097 // ":let vers = 1.2.3" doesn't fail. 1098 if (**arg == '.') 1099 p = *arg; 1100 else 1101 p = skipdigits(*arg + 1); 1102 if (!want_string && p[0] == '.' && vim_isdigit(p[1])) 1103 { 1104 get_float = TRUE; 1105 p = skipdigits(p + 2); 1106 if (*p == 'e' || *p == 'E') 1107 { 1108 ++p; 1109 if (*p == '-' || *p == '+') 1110 ++p; 1111 if (!vim_isdigit(*p)) 1112 get_float = FALSE; 1113 else 1114 p = skipdigits(p + 1); 1115 } 1116 if (ASCII_ISALPHA(*p) || *p == '.') 1117 get_float = FALSE; 1118 } 1119 if (get_float) 1120 { 1121 float_T f; 1122 1123 *arg += string2float(*arg, &f); 1124 if (evaluate) 1125 { 1126 rettv->v_type = VAR_FLOAT; 1127 rettv->vval.v_float = f; 1128 } 1129 } 1130 else 1131 #endif 1132 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z')) 1133 { 1134 char_u *bp; 1135 blob_T *blob = NULL; // init for gcc 1136 1137 // Blob constant: 0z0123456789abcdef 1138 if (evaluate) 1139 blob = blob_alloc(); 1140 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2) 1141 { 1142 if (!vim_isxdigit(bp[1])) 1143 { 1144 if (blob != NULL) 1145 { 1146 emsg(_("E973: Blob literal should have an even number of hex characters")); 1147 ga_clear(&blob->bv_ga); 1148 VIM_CLEAR(blob); 1149 } 1150 return FAIL; 1151 } 1152 if (blob != NULL) 1153 ga_append(&blob->bv_ga, 1154 (hex2nr(*bp) << 4) + hex2nr(*(bp+1))); 1155 if (bp[2] == '.' && vim_isxdigit(bp[3])) 1156 ++bp; 1157 } 1158 if (blob != NULL) 1159 rettv_blob_set(rettv, blob); 1160 *arg = bp; 1161 } 1162 else 1163 { 1164 varnumber_T n; 1165 1166 // decimal, hex or octal number 1167 vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4 1168 ? STR2NR_NO_OCT + STR2NR_QUOTE 1169 : STR2NR_ALL, &n, NULL, 0, TRUE); 1170 if (len == 0) 1171 { 1172 semsg(_(e_invexpr2), *arg); 1173 return FAIL; 1174 } 1175 *arg += len; 1176 if (evaluate) 1177 { 1178 rettv->v_type = VAR_NUMBER; 1179 rettv->vval.v_number = n; 1180 } 1181 } 1182 return OK; 1183 } 1184 1185 /* 1186 * Allocate a variable for a string constant. 1187 * Return OK or FAIL. 1188 */ 1189 int 1190 eval_string(char_u **arg, typval_T *rettv, int evaluate) 1191 { 1192 char_u *p; 1193 char_u *end; 1194 int extra = 0; 1195 int len; 1196 1197 // Find the end of the string, skipping backslashed characters. 1198 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p)) 1199 { 1200 if (*p == '\\' && p[1] != NUL) 1201 { 1202 ++p; 1203 // A "\<x>" form occupies at least 4 characters, and produces up 1204 // to 21 characters (3 * 6 for the char and 3 for a modifier): 1205 // reserve space for 18 extra. 1206 // Each byte in the char could be encoded as K_SPECIAL K_EXTRA x. 1207 if (*p == '<') 1208 extra += 18; 1209 } 1210 } 1211 1212 if (*p != '"') 1213 { 1214 semsg(_("E114: Missing quote: %s"), *arg); 1215 return FAIL; 1216 } 1217 1218 // If only parsing, set *arg and return here 1219 if (!evaluate) 1220 { 1221 *arg = p + 1; 1222 return OK; 1223 } 1224 1225 // Copy the string into allocated memory, handling backslashed 1226 // characters. 1227 rettv->v_type = VAR_STRING; 1228 len = (int)(p - *arg + extra); 1229 rettv->vval.v_string = alloc(len); 1230 if (rettv->vval.v_string == NULL) 1231 return FAIL; 1232 end = rettv->vval.v_string; 1233 1234 for (p = *arg + 1; *p != NUL && *p != '"'; ) 1235 { 1236 if (*p == '\\') 1237 { 1238 switch (*++p) 1239 { 1240 case 'b': *end++ = BS; ++p; break; 1241 case 'e': *end++ = ESC; ++p; break; 1242 case 'f': *end++ = FF; ++p; break; 1243 case 'n': *end++ = NL; ++p; break; 1244 case 'r': *end++ = CAR; ++p; break; 1245 case 't': *end++ = TAB; ++p; break; 1246 1247 case 'X': // hex: "\x1", "\x12" 1248 case 'x': 1249 case 'u': // Unicode: "\u0023" 1250 case 'U': 1251 if (vim_isxdigit(p[1])) 1252 { 1253 int n, nr; 1254 int c = toupper(*p); 1255 1256 if (c == 'X') 1257 n = 2; 1258 else if (*p == 'u') 1259 n = 4; 1260 else 1261 n = 8; 1262 nr = 0; 1263 while (--n >= 0 && vim_isxdigit(p[1])) 1264 { 1265 ++p; 1266 nr = (nr << 4) + hex2nr(*p); 1267 } 1268 ++p; 1269 // For "\u" store the number according to 1270 // 'encoding'. 1271 if (c != 'X') 1272 end += (*mb_char2bytes)(nr, end); 1273 else 1274 *end++ = nr; 1275 } 1276 break; 1277 1278 // octal: "\1", "\12", "\123" 1279 case '0': 1280 case '1': 1281 case '2': 1282 case '3': 1283 case '4': 1284 case '5': 1285 case '6': 1286 case '7': *end = *p++ - '0'; 1287 if (*p >= '0' && *p <= '7') 1288 { 1289 *end = (*end << 3) + *p++ - '0'; 1290 if (*p >= '0' && *p <= '7') 1291 *end = (*end << 3) + *p++ - '0'; 1292 } 1293 ++end; 1294 break; 1295 1296 // Special key, e.g.: "\<C-W>" 1297 case '<': 1298 { 1299 int flags = FSK_KEYCODE | FSK_IN_STRING; 1300 1301 if (p[1] != '*') 1302 flags |= FSK_SIMPLIFY; 1303 extra = trans_special(&p, end, flags, NULL); 1304 if (extra != 0) 1305 { 1306 end += extra; 1307 if (end >= rettv->vval.v_string + len) 1308 iemsg("eval_string() used more space than allocated"); 1309 break; 1310 } 1311 } 1312 // FALLTHROUGH 1313 1314 default: MB_COPY_CHAR(p, end); 1315 break; 1316 } 1317 } 1318 else 1319 MB_COPY_CHAR(p, end); 1320 } 1321 *end = NUL; 1322 if (*p != NUL) // just in case 1323 ++p; 1324 *arg = p; 1325 1326 return OK; 1327 } 1328 1329 /* 1330 * Allocate a variable for a 'str''ing' constant. 1331 * Return OK or FAIL. 1332 */ 1333 int 1334 eval_lit_string(char_u **arg, typval_T *rettv, int evaluate) 1335 { 1336 char_u *p; 1337 char_u *str; 1338 int reduce = 0; 1339 1340 // Find the end of the string, skipping ''. 1341 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p)) 1342 { 1343 if (*p == '\'') 1344 { 1345 if (p[1] != '\'') 1346 break; 1347 ++reduce; 1348 ++p; 1349 } 1350 } 1351 1352 if (*p != '\'') 1353 { 1354 semsg(_("E115: Missing quote: %s"), *arg); 1355 return FAIL; 1356 } 1357 1358 // If only parsing return after setting "*arg" 1359 if (!evaluate) 1360 { 1361 *arg = p + 1; 1362 return OK; 1363 } 1364 1365 // Copy the string into allocated memory, handling '' to ' reduction. 1366 str = alloc((p - *arg) - reduce); 1367 if (str == NULL) 1368 return FAIL; 1369 rettv->v_type = VAR_STRING; 1370 rettv->vval.v_string = str; 1371 1372 for (p = *arg + 1; *p != NUL; ) 1373 { 1374 if (*p == '\'') 1375 { 1376 if (p[1] != '\'') 1377 break; 1378 ++p; 1379 } 1380 MB_COPY_CHAR(p, str); 1381 } 1382 *str = NUL; 1383 *arg = p + 1; 1384 1385 return OK; 1386 } 1387 1388 /* 1389 * Return a string with the string representation of a variable. 1390 * If the memory is allocated "tofree" is set to it, otherwise NULL. 1391 * "numbuf" is used for a number. 1392 * Puts quotes around strings, so that they can be parsed back by eval(). 1393 * May return NULL. 1394 */ 1395 char_u * 1396 tv2string( 1397 typval_T *tv, 1398 char_u **tofree, 1399 char_u *numbuf, 1400 int copyID) 1401 { 1402 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE); 1403 } 1404 1405 /* 1406 * Get the value of an environment variable. 1407 * "arg" is pointing to the '$'. It is advanced to after the name. 1408 * If the environment variable was not set, silently assume it is empty. 1409 * Return FAIL if the name is invalid. 1410 */ 1411 int 1412 eval_env_var(char_u **arg, typval_T *rettv, int evaluate) 1413 { 1414 char_u *string = NULL; 1415 int len; 1416 int cc; 1417 char_u *name; 1418 int mustfree = FALSE; 1419 1420 ++*arg; 1421 name = *arg; 1422 len = get_env_len(arg); 1423 if (evaluate) 1424 { 1425 if (len == 0) 1426 return FAIL; // invalid empty name 1427 1428 cc = name[len]; 1429 name[len] = NUL; 1430 // first try vim_getenv(), fast for normal environment vars 1431 string = vim_getenv(name, &mustfree); 1432 if (string != NULL && *string != NUL) 1433 { 1434 if (!mustfree) 1435 string = vim_strsave(string); 1436 } 1437 else 1438 { 1439 if (mustfree) 1440 vim_free(string); 1441 1442 // next try expanding things like $VIM and ${HOME} 1443 string = expand_env_save(name - 1); 1444 if (string != NULL && *string == '$') 1445 VIM_CLEAR(string); 1446 } 1447 name[len] = cc; 1448 1449 rettv->v_type = VAR_STRING; 1450 rettv->vval.v_string = string; 1451 } 1452 1453 return OK; 1454 } 1455 1456 /* 1457 * Get the lnum from the first argument. 1458 * Also accepts ".", "$", etc., but that only works for the current buffer. 1459 * Returns -1 on error. 1460 */ 1461 linenr_T 1462 tv_get_lnum(typval_T *argvars) 1463 { 1464 linenr_T lnum; 1465 1466 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL); 1467 if (lnum == 0) // no valid number, try using arg like line() 1468 { 1469 int fnum; 1470 pos_T *fp = var2fpos(&argvars[0], TRUE, &fnum); 1471 1472 if (fp != NULL) 1473 lnum = fp->lnum; 1474 } 1475 return lnum; 1476 } 1477 1478 /* 1479 * Get the lnum from the first argument. 1480 * Also accepts "$", then "buf" is used. 1481 * Returns 0 on error. 1482 */ 1483 linenr_T 1484 tv_get_lnum_buf(typval_T *argvars, buf_T *buf) 1485 { 1486 if (argvars[0].v_type == VAR_STRING 1487 && argvars[0].vval.v_string != NULL 1488 && argvars[0].vval.v_string[0] == '$' 1489 && buf != NULL) 1490 return buf->b_ml.ml_line_count; 1491 return (linenr_T)tv_get_number_chk(&argvars[0], NULL); 1492 } 1493 1494 /* 1495 * Get buffer by number or pattern. 1496 */ 1497 buf_T * 1498 tv_get_buf(typval_T *tv, int curtab_only) 1499 { 1500 char_u *name = tv->vval.v_string; 1501 buf_T *buf; 1502 1503 if (tv->v_type == VAR_NUMBER) 1504 return buflist_findnr((int)tv->vval.v_number); 1505 if (tv->v_type != VAR_STRING) 1506 return NULL; 1507 if (name == NULL || *name == NUL) 1508 return curbuf; 1509 if (name[0] == '$' && name[1] == NUL) 1510 return lastbuf; 1511 1512 buf = buflist_find_by_name(name, curtab_only); 1513 1514 // If not found, try expanding the name, like done for bufexists(). 1515 if (buf == NULL) 1516 buf = find_buffer(tv); 1517 1518 return buf; 1519 } 1520 1521 #endif // FEAT_EVAL 1522