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