blob: 7e4e63d17dd8305b393971c9ef2a634cb83d3983 [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
Bram Moolenaar657137c2021-01-09 15:45:23 +0100652 exprtype_T type, // operator
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200653 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 }
Bram Moolenaarcff40ff2021-01-09 16:21:37 +0100837 else if (in_vim9script() && (typ1->v_type == VAR_BOOL
838 || typ2->v_type == VAR_BOOL))
839 {
840 if (typ1->v_type != typ2->v_type)
841 {
842 semsg(_(e_cannot_compare_str_with_str),
843 vartype_name(typ1->v_type), vartype_name(typ2->v_type));
844 clear_tv(typ1);
845 return FAIL;
846 }
847 n1 = typ1->vval.v_number;
848 n2 = typ2->vval.v_number;
849 switch (type)
850 {
851 case EXPR_IS:
852 case EXPR_EQUAL: n1 = (n1 == n2); break;
853 case EXPR_ISNOT:
854 case EXPR_NEQUAL: n1 = (n1 != n2); break;
855 default:
856 emsg(_(e_invalid_operation_for_bool));
857 clear_tv(typ1);
858 return FAIL;
859 }
860 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200861 else
862 {
863 s1 = tv_get_string_buf(typ1, buf1);
864 s2 = tv_get_string_buf(typ2, buf2);
865 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
866 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
867 else
868 i = 0;
869 n1 = FALSE;
870 switch (type)
871 {
872 case EXPR_IS:
873 case EXPR_EQUAL: n1 = (i == 0); break;
874 case EXPR_ISNOT:
875 case EXPR_NEQUAL: n1 = (i != 0); break;
876 case EXPR_GREATER: n1 = (i > 0); break;
877 case EXPR_GEQUAL: n1 = (i >= 0); break;
878 case EXPR_SMALLER: n1 = (i < 0); break;
879 case EXPR_SEQUAL: n1 = (i <= 0); break;
880
881 case EXPR_MATCH:
882 case EXPR_NOMATCH:
883 n1 = pattern_match(s2, s1, ic);
884 if (type == EXPR_NOMATCH)
885 n1 = !n1;
886 break;
887
888 default: break; // avoid gcc warning
889 }
890 }
891 clear_tv(typ1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +0200892 if (in_vim9script())
893 {
894 typ1->v_type = VAR_BOOL;
895 typ1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
896 }
897 else
898 {
899 typ1->v_type = VAR_NUMBER;
900 typ1->vval.v_number = n1;
901 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200902
903 return OK;
904}
905
906 char_u *
907typval_tostring(typval_T *arg)
908{
909 char_u *tofree;
910 char_u numbuf[NUMBUFLEN];
911 char_u *ret = NULL;
912
913 if (arg == NULL)
914 return vim_strsave((char_u *)"(does not exist)");
915 ret = tv2string(arg, &tofree, numbuf, 0);
916 // Make a copy if we have a value but it's not in allocated memory.
917 if (ret != NULL && tofree == NULL)
918 ret = vim_strsave(ret);
919 return ret;
920}
921
922/*
923 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
924 * or it refers to a List or Dictionary that is locked.
925 */
926 int
927tv_islocked(typval_T *tv)
928{
929 return (tv->v_lock & VAR_LOCKED)
930 || (tv->v_type == VAR_LIST
931 && tv->vval.v_list != NULL
932 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
933 || (tv->v_type == VAR_DICT
934 && tv->vval.v_dict != NULL
935 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
936}
937
938 static int
939func_equal(
940 typval_T *tv1,
941 typval_T *tv2,
942 int ic) // ignore case
943{
944 char_u *s1, *s2;
945 dict_T *d1, *d2;
946 int a1, a2;
947 int i;
948
949 // empty and NULL function name considered the same
950 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
951 : partial_name(tv1->vval.v_partial);
952 if (s1 != NULL && *s1 == NUL)
953 s1 = NULL;
954 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
955 : partial_name(tv2->vval.v_partial);
956 if (s2 != NULL && *s2 == NUL)
957 s2 = NULL;
958 if (s1 == NULL || s2 == NULL)
959 {
960 if (s1 != s2)
961 return FALSE;
962 }
963 else if (STRCMP(s1, s2) != 0)
964 return FALSE;
965
966 // empty dict and NULL dict is different
967 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
968 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
969 if (d1 == NULL || d2 == NULL)
970 {
971 if (d1 != d2)
972 return FALSE;
973 }
974 else if (!dict_equal(d1, d2, ic, TRUE))
975 return FALSE;
976
977 // empty list and no list considered the same
978 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
979 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
980 if (a1 != a2)
981 return FALSE;
982 for (i = 0; i < a1; ++i)
983 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
984 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
985 return FALSE;
986
987 return TRUE;
988}
989
990/*
991 * Return TRUE if "tv1" and "tv2" have the same value.
992 * Compares the items just like "==" would compare them, but strings and
993 * numbers are different. Floats and numbers are also different.
994 */
995 int
996tv_equal(
997 typval_T *tv1,
998 typval_T *tv2,
999 int ic, // ignore case
1000 int recursive) // TRUE when used recursively
1001{
1002 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1003 char_u *s1, *s2;
1004 static int recursive_cnt = 0; // catch recursive loops
1005 int r;
1006 static int tv_equal_recurse_limit;
1007
1008 // Catch lists and dicts that have an endless loop by limiting
1009 // recursiveness to a limit. We guess they are equal then.
1010 // A fixed limit has the problem of still taking an awful long time.
1011 // Reduce the limit every time running into it. That should work fine for
1012 // deeply linked structures that are not recursively linked and catch
1013 // recursiveness quickly.
1014 if (!recursive)
1015 tv_equal_recurse_limit = 1000;
1016 if (recursive_cnt >= tv_equal_recurse_limit)
1017 {
1018 --tv_equal_recurse_limit;
1019 return TRUE;
1020 }
1021
1022 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1023 // arguments.
1024 if ((tv1->v_type == VAR_FUNC
1025 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1026 && (tv2->v_type == VAR_FUNC
1027 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1028 {
1029 ++recursive_cnt;
1030 r = func_equal(tv1, tv2, ic);
1031 --recursive_cnt;
1032 return r;
1033 }
1034
1035 if (tv1->v_type != tv2->v_type)
1036 return FALSE;
1037
1038 switch (tv1->v_type)
1039 {
1040 case VAR_LIST:
1041 ++recursive_cnt;
1042 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1043 --recursive_cnt;
1044 return r;
1045
1046 case VAR_DICT:
1047 ++recursive_cnt;
1048 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1049 --recursive_cnt;
1050 return r;
1051
1052 case VAR_BLOB:
1053 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1054
1055 case VAR_NUMBER:
1056 case VAR_BOOL:
1057 case VAR_SPECIAL:
1058 return tv1->vval.v_number == tv2->vval.v_number;
1059
1060 case VAR_STRING:
1061 s1 = tv_get_string_buf(tv1, buf1);
1062 s2 = tv_get_string_buf(tv2, buf2);
1063 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1064
1065 case VAR_FLOAT:
1066#ifdef FEAT_FLOAT
1067 return tv1->vval.v_float == tv2->vval.v_float;
1068#endif
1069 case VAR_JOB:
1070#ifdef FEAT_JOB_CHANNEL
1071 return tv1->vval.v_job == tv2->vval.v_job;
1072#endif
1073 case VAR_CHANNEL:
1074#ifdef FEAT_JOB_CHANNEL
1075 return tv1->vval.v_channel == tv2->vval.v_channel;
1076#endif
1077
1078 case VAR_PARTIAL:
1079 return tv1->vval.v_partial == tv2->vval.v_partial;
1080
1081 case VAR_FUNC:
1082 return tv1->vval.v_string == tv2->vval.v_string;
1083
1084 case VAR_UNKNOWN:
1085 case VAR_ANY:
1086 case VAR_VOID:
1087 break;
1088 }
1089
1090 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1091 // does not equal anything, not even itself.
1092 return FALSE;
1093}
1094
1095/*
1096 * Get an option value.
1097 * "arg" points to the '&' or '+' before the option name.
1098 * "arg" is advanced to character after the option name.
1099 * Return OK or FAIL.
1100 */
1101 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001102eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001103 char_u **arg,
1104 typval_T *rettv, // when NULL, only check if option exists
1105 int evaluate)
1106{
1107 char_u *option_end;
1108 long numval;
1109 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001110 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001111 int c;
1112 int working = (**arg == '+'); // has("+option")
1113 int ret = OK;
1114 int opt_flags;
1115
1116 // Isolate the option name and find its value.
1117 option_end = find_option_end(arg, &opt_flags);
1118 if (option_end == NULL)
1119 {
1120 if (rettv != NULL)
1121 semsg(_("E112: Option name missing: %s"), *arg);
1122 return FAIL;
1123 }
1124
1125 if (!evaluate)
1126 {
1127 *arg = option_end;
1128 return OK;
1129 }
1130
1131 c = *option_end;
1132 *option_end = NUL;
1133 opt_type = get_option_value(*arg, &numval,
1134 rettv == NULL ? NULL : &stringval, opt_flags);
1135
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001136 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001137 {
1138 if (rettv != NULL)
1139 semsg(_(e_unknown_option), *arg);
1140 ret = FAIL;
1141 }
1142 else if (rettv != NULL)
1143 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001144 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001145 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001146 {
1147 rettv->v_type = VAR_STRING;
1148 rettv->vval.v_string = NULL;
1149 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001150 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001151 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001152 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1153 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001154 rettv->vval.v_number = 0;
1155 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001156 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001157 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001158 if (in_vim9script() && opt_type == gov_bool)
1159 {
1160 rettv->v_type = VAR_BOOL;
1161 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1162 }
1163 else
1164 {
1165 rettv->v_type = VAR_NUMBER;
1166 rettv->vval.v_number = numval;
1167 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001168 }
1169 else // string option
1170 {
1171 rettv->v_type = VAR_STRING;
1172 rettv->vval.v_string = stringval;
1173 }
1174 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001175 else if (working && (opt_type == gov_hidden_bool
1176 || opt_type == gov_hidden_number
1177 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001178 ret = FAIL;
1179
1180 *option_end = c; // put back for error messages
1181 *arg = option_end;
1182
1183 return ret;
1184}
1185
1186/*
1187 * Allocate a variable for a number constant. Also deals with "0z" for blob.
1188 * Return OK or FAIL.
1189 */
1190 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001191eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001192 char_u **arg,
1193 typval_T *rettv,
1194 int evaluate,
1195 int want_string UNUSED)
1196{
1197 int len;
1198#ifdef FEAT_FLOAT
1199 char_u *p;
1200 int get_float = FALSE;
1201
1202 // We accept a float when the format matches
1203 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
1204 // strict to avoid backwards compatibility problems.
1205 // With script version 2 and later the leading digit can be
1206 // omitted.
1207 // Don't look for a float after the "." operator, so that
1208 // ":let vers = 1.2.3" doesn't fail.
1209 if (**arg == '.')
1210 p = *arg;
1211 else
1212 p = skipdigits(*arg + 1);
1213 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
1214 {
1215 get_float = TRUE;
1216 p = skipdigits(p + 2);
1217 if (*p == 'e' || *p == 'E')
1218 {
1219 ++p;
1220 if (*p == '-' || *p == '+')
1221 ++p;
1222 if (!vim_isdigit(*p))
1223 get_float = FALSE;
1224 else
1225 p = skipdigits(p + 1);
1226 }
1227 if (ASCII_ISALPHA(*p) || *p == '.')
1228 get_float = FALSE;
1229 }
1230 if (get_float)
1231 {
1232 float_T f;
1233
1234 *arg += string2float(*arg, &f);
1235 if (evaluate)
1236 {
1237 rettv->v_type = VAR_FLOAT;
1238 rettv->vval.v_float = f;
1239 }
1240 }
1241 else
1242#endif
1243 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
1244 {
1245 char_u *bp;
1246 blob_T *blob = NULL; // init for gcc
1247
1248 // Blob constant: 0z0123456789abcdef
1249 if (evaluate)
1250 blob = blob_alloc();
1251 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
1252 {
1253 if (!vim_isxdigit(bp[1]))
1254 {
1255 if (blob != NULL)
1256 {
1257 emsg(_("E973: Blob literal should have an even number of hex characters"));
1258 ga_clear(&blob->bv_ga);
1259 VIM_CLEAR(blob);
1260 }
1261 return FAIL;
1262 }
1263 if (blob != NULL)
1264 ga_append(&blob->bv_ga,
1265 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
1266 if (bp[2] == '.' && vim_isxdigit(bp[3]))
1267 ++bp;
1268 }
1269 if (blob != NULL)
1270 rettv_blob_set(rettv, blob);
1271 *arg = bp;
1272 }
1273 else
1274 {
1275 varnumber_T n;
1276
1277 // decimal, hex or octal number
1278 vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4
1279 ? STR2NR_NO_OCT + STR2NR_QUOTE
1280 : STR2NR_ALL, &n, NULL, 0, TRUE);
1281 if (len == 0)
1282 {
1283 semsg(_(e_invexpr2), *arg);
1284 return FAIL;
1285 }
1286 *arg += len;
1287 if (evaluate)
1288 {
1289 rettv->v_type = VAR_NUMBER;
1290 rettv->vval.v_number = n;
1291 }
1292 }
1293 return OK;
1294}
1295
1296/*
1297 * Allocate a variable for a string constant.
1298 * Return OK or FAIL.
1299 */
1300 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001301eval_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001302{
1303 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001304 char_u *end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001305 int extra = 0;
1306 int len;
1307
1308 // Find the end of the string, skipping backslashed characters.
1309 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
1310 {
1311 if (*p == '\\' && p[1] != NUL)
1312 {
1313 ++p;
1314 // A "\<x>" form occupies at least 4 characters, and produces up
1315 // to 21 characters (3 * 6 for the char and 3 for a modifier):
1316 // reserve space for 18 extra.
1317 // Each byte in the char could be encoded as K_SPECIAL K_EXTRA x.
1318 if (*p == '<')
1319 extra += 18;
1320 }
1321 }
1322
1323 if (*p != '"')
1324 {
1325 semsg(_("E114: Missing quote: %s"), *arg);
1326 return FAIL;
1327 }
1328
1329 // If only parsing, set *arg and return here
1330 if (!evaluate)
1331 {
1332 *arg = p + 1;
1333 return OK;
1334 }
1335
1336 // Copy the string into allocated memory, handling backslashed
1337 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001338 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001339 len = (int)(p - *arg + extra);
1340 rettv->vval.v_string = alloc(len);
1341 if (rettv->vval.v_string == NULL)
1342 return FAIL;
1343 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001344
1345 for (p = *arg + 1; *p != NUL && *p != '"'; )
1346 {
1347 if (*p == '\\')
1348 {
1349 switch (*++p)
1350 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001351 case 'b': *end++ = BS; ++p; break;
1352 case 'e': *end++ = ESC; ++p; break;
1353 case 'f': *end++ = FF; ++p; break;
1354 case 'n': *end++ = NL; ++p; break;
1355 case 'r': *end++ = CAR; ++p; break;
1356 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001357
1358 case 'X': // hex: "\x1", "\x12"
1359 case 'x':
1360 case 'u': // Unicode: "\u0023"
1361 case 'U':
1362 if (vim_isxdigit(p[1]))
1363 {
1364 int n, nr;
1365 int c = toupper(*p);
1366
1367 if (c == 'X')
1368 n = 2;
1369 else if (*p == 'u')
1370 n = 4;
1371 else
1372 n = 8;
1373 nr = 0;
1374 while (--n >= 0 && vim_isxdigit(p[1]))
1375 {
1376 ++p;
1377 nr = (nr << 4) + hex2nr(*p);
1378 }
1379 ++p;
1380 // For "\u" store the number according to
1381 // 'encoding'.
1382 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001383 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001384 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001385 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001386 }
1387 break;
1388
1389 // octal: "\1", "\12", "\123"
1390 case '0':
1391 case '1':
1392 case '2':
1393 case '3':
1394 case '4':
1395 case '5':
1396 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001397 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001398 if (*p >= '0' && *p <= '7')
1399 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001400 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001401 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001402 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001403 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001404 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001405 break;
1406
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001407 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001408 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001409 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001410 int flags = FSK_KEYCODE | FSK_IN_STRING;
1411
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001412 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001413 flags |= FSK_SIMPLIFY;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001414 extra = trans_special(&p, end, flags, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001415 if (extra != 0)
1416 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001417 end += extra;
1418 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001419 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001420 break;
1421 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001422 }
1423 // FALLTHROUGH
1424
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001425 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001426 break;
1427 }
1428 }
1429 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001430 MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001431 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001432 *end = NUL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001433 if (*p != NUL) // just in case
1434 ++p;
1435 *arg = p;
1436
1437 return OK;
1438}
1439
1440/*
1441 * Allocate a variable for a 'str''ing' constant.
1442 * Return OK or FAIL.
1443 */
1444 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001445eval_lit_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001446{
1447 char_u *p;
1448 char_u *str;
1449 int reduce = 0;
1450
1451 // Find the end of the string, skipping ''.
1452 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
1453 {
1454 if (*p == '\'')
1455 {
1456 if (p[1] != '\'')
1457 break;
1458 ++reduce;
1459 ++p;
1460 }
1461 }
1462
1463 if (*p != '\'')
1464 {
1465 semsg(_("E115: Missing quote: %s"), *arg);
1466 return FAIL;
1467 }
1468
1469 // If only parsing return after setting "*arg"
1470 if (!evaluate)
1471 {
1472 *arg = p + 1;
1473 return OK;
1474 }
1475
1476 // Copy the string into allocated memory, handling '' to ' reduction.
1477 str = alloc((p - *arg) - reduce);
1478 if (str == NULL)
1479 return FAIL;
1480 rettv->v_type = VAR_STRING;
1481 rettv->vval.v_string = str;
1482
1483 for (p = *arg + 1; *p != NUL; )
1484 {
1485 if (*p == '\'')
1486 {
1487 if (p[1] != '\'')
1488 break;
1489 ++p;
1490 }
1491 MB_COPY_CHAR(p, str);
1492 }
1493 *str = NUL;
1494 *arg = p + 1;
1495
1496 return OK;
1497}
1498
1499/*
1500 * Return a string with the string representation of a variable.
1501 * If the memory is allocated "tofree" is set to it, otherwise NULL.
1502 * "numbuf" is used for a number.
1503 * Puts quotes around strings, so that they can be parsed back by eval().
1504 * May return NULL.
1505 */
1506 char_u *
1507tv2string(
1508 typval_T *tv,
1509 char_u **tofree,
1510 char_u *numbuf,
1511 int copyID)
1512{
1513 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
1514}
1515
1516/*
1517 * Get the value of an environment variable.
1518 * "arg" is pointing to the '$'. It is advanced to after the name.
1519 * If the environment variable was not set, silently assume it is empty.
1520 * Return FAIL if the name is invalid.
1521 */
1522 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001523eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001524{
1525 char_u *string = NULL;
1526 int len;
1527 int cc;
1528 char_u *name;
1529 int mustfree = FALSE;
1530
1531 ++*arg;
1532 name = *arg;
1533 len = get_env_len(arg);
1534 if (evaluate)
1535 {
1536 if (len == 0)
1537 return FAIL; // invalid empty name
1538
1539 cc = name[len];
1540 name[len] = NUL;
1541 // first try vim_getenv(), fast for normal environment vars
1542 string = vim_getenv(name, &mustfree);
1543 if (string != NULL && *string != NUL)
1544 {
1545 if (!mustfree)
1546 string = vim_strsave(string);
1547 }
1548 else
1549 {
1550 if (mustfree)
1551 vim_free(string);
1552
1553 // next try expanding things like $VIM and ${HOME}
1554 string = expand_env_save(name - 1);
1555 if (string != NULL && *string == '$')
1556 VIM_CLEAR(string);
1557 }
1558 name[len] = cc;
1559
1560 rettv->v_type = VAR_STRING;
1561 rettv->vval.v_string = string;
1562 }
1563
1564 return OK;
1565}
1566
1567/*
1568 * Get the lnum from the first argument.
1569 * Also accepts ".", "$", etc., but that only works for the current buffer.
1570 * Returns -1 on error.
1571 */
1572 linenr_T
1573tv_get_lnum(typval_T *argvars)
1574{
Bram Moolenaar9a963372020-12-21 21:58:46 +01001575 linenr_T lnum = -1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001576
Bram Moolenaar56acb092020-08-16 14:48:19 +02001577 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
1578 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar9a963372020-12-21 21:58:46 +01001579 if (lnum <= 0) // no valid number, try using arg like line()
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001580 {
1581 int fnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001582 pos_T *fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001583
1584 if (fp != NULL)
1585 lnum = fp->lnum;
1586 }
1587 return lnum;
1588}
1589
1590/*
1591 * Get the lnum from the first argument.
1592 * Also accepts "$", then "buf" is used.
1593 * Returns 0 on error.
1594 */
1595 linenr_T
1596tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1597{
1598 if (argvars[0].v_type == VAR_STRING
1599 && argvars[0].vval.v_string != NULL
1600 && argvars[0].vval.v_string[0] == '$'
1601 && buf != NULL)
1602 return buf->b_ml.ml_line_count;
1603 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1604}
1605
1606/*
1607 * Get buffer by number or pattern.
1608 */
1609 buf_T *
1610tv_get_buf(typval_T *tv, int curtab_only)
1611{
1612 char_u *name = tv->vval.v_string;
1613 buf_T *buf;
1614
1615 if (tv->v_type == VAR_NUMBER)
1616 return buflist_findnr((int)tv->vval.v_number);
1617 if (tv->v_type != VAR_STRING)
1618 return NULL;
1619 if (name == NULL || *name == NUL)
1620 return curbuf;
1621 if (name[0] == '$' && name[1] == NUL)
1622 return lastbuf;
1623
1624 buf = buflist_find_by_name(name, curtab_only);
1625
1626 // If not found, try expanding the name, like done for bufexists().
1627 if (buf == NULL)
1628 buf = find_buffer(tv);
1629
1630 return buf;
1631}
1632
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02001633/*
1634 * Like tv_get_buf() but give an error message is the type is wrong.
1635 */
1636 buf_T *
1637tv_get_buf_from_arg(typval_T *tv)
1638{
1639 buf_T *buf;
1640
1641 ++emsg_off;
1642 buf = tv_get_buf(tv, FALSE);
1643 --emsg_off;
1644 if (buf == NULL
1645 && tv->v_type != VAR_NUMBER
1646 && tv->v_type != VAR_STRING)
1647 // issue errmsg for type error
1648 (void)tv_get_number(tv);
1649 return buf;
1650}
1651
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001652#endif // FEAT_EVAL