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