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