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