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