blob: d16f0e6df85de8934c0b07f7318791ca6dd63c04 [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;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001086 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001087 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
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001112 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001113 {
1114 if (rettv != NULL)
1115 semsg(_(e_unknown_option), *arg);
1116 ret = FAIL;
1117 }
1118 else if (rettv != NULL)
1119 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001120 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001121 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001122 {
1123 rettv->v_type = VAR_STRING;
1124 rettv->vval.v_string = NULL;
1125 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001126 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001127 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001128 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1129 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001130 rettv->vval.v_number = 0;
1131 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001132 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001133 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001134 if (in_vim9script() && opt_type == gov_bool)
1135 {
1136 rettv->v_type = VAR_BOOL;
1137 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1138 }
1139 else
1140 {
1141 rettv->v_type = VAR_NUMBER;
1142 rettv->vval.v_number = numval;
1143 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001144 }
1145 else // string option
1146 {
1147 rettv->v_type = VAR_STRING;
1148 rettv->vval.v_string = stringval;
1149 }
1150 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001151 else if (working && (opt_type == gov_hidden_bool
1152 || opt_type == gov_hidden_number
1153 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001154 ret = FAIL;
1155
1156 *option_end = c; // put back for error messages
1157 *arg = option_end;
1158
1159 return ret;
1160}
1161
1162/*
1163 * Allocate a variable for a number constant. Also deals with "0z" for blob.
1164 * Return OK or FAIL.
1165 */
1166 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001167eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001168 char_u **arg,
1169 typval_T *rettv,
1170 int evaluate,
1171 int want_string UNUSED)
1172{
1173 int len;
1174#ifdef FEAT_FLOAT
1175 char_u *p;
1176 int get_float = FALSE;
1177
1178 // We accept a float when the format matches
1179 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
1180 // strict to avoid backwards compatibility problems.
1181 // With script version 2 and later the leading digit can be
1182 // omitted.
1183 // Don't look for a float after the "." operator, so that
1184 // ":let vers = 1.2.3" doesn't fail.
1185 if (**arg == '.')
1186 p = *arg;
1187 else
1188 p = skipdigits(*arg + 1);
1189 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
1190 {
1191 get_float = TRUE;
1192 p = skipdigits(p + 2);
1193 if (*p == 'e' || *p == 'E')
1194 {
1195 ++p;
1196 if (*p == '-' || *p == '+')
1197 ++p;
1198 if (!vim_isdigit(*p))
1199 get_float = FALSE;
1200 else
1201 p = skipdigits(p + 1);
1202 }
1203 if (ASCII_ISALPHA(*p) || *p == '.')
1204 get_float = FALSE;
1205 }
1206 if (get_float)
1207 {
1208 float_T f;
1209
1210 *arg += string2float(*arg, &f);
1211 if (evaluate)
1212 {
1213 rettv->v_type = VAR_FLOAT;
1214 rettv->vval.v_float = f;
1215 }
1216 }
1217 else
1218#endif
1219 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
1220 {
1221 char_u *bp;
1222 blob_T *blob = NULL; // init for gcc
1223
1224 // Blob constant: 0z0123456789abcdef
1225 if (evaluate)
1226 blob = blob_alloc();
1227 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
1228 {
1229 if (!vim_isxdigit(bp[1]))
1230 {
1231 if (blob != NULL)
1232 {
1233 emsg(_("E973: Blob literal should have an even number of hex characters"));
1234 ga_clear(&blob->bv_ga);
1235 VIM_CLEAR(blob);
1236 }
1237 return FAIL;
1238 }
1239 if (blob != NULL)
1240 ga_append(&blob->bv_ga,
1241 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
1242 if (bp[2] == '.' && vim_isxdigit(bp[3]))
1243 ++bp;
1244 }
1245 if (blob != NULL)
1246 rettv_blob_set(rettv, blob);
1247 *arg = bp;
1248 }
1249 else
1250 {
1251 varnumber_T n;
1252
1253 // decimal, hex or octal number
1254 vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4
1255 ? STR2NR_NO_OCT + STR2NR_QUOTE
1256 : STR2NR_ALL, &n, NULL, 0, TRUE);
1257 if (len == 0)
1258 {
1259 semsg(_(e_invexpr2), *arg);
1260 return FAIL;
1261 }
1262 *arg += len;
1263 if (evaluate)
1264 {
1265 rettv->v_type = VAR_NUMBER;
1266 rettv->vval.v_number = n;
1267 }
1268 }
1269 return OK;
1270}
1271
1272/*
1273 * Allocate a variable for a string constant.
1274 * Return OK or FAIL.
1275 */
1276 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001277eval_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001278{
1279 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001280 char_u *end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001281 int extra = 0;
1282 int len;
1283
1284 // Find the end of the string, skipping backslashed characters.
1285 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
1286 {
1287 if (*p == '\\' && p[1] != NUL)
1288 {
1289 ++p;
1290 // A "\<x>" form occupies at least 4 characters, and produces up
1291 // to 21 characters (3 * 6 for the char and 3 for a modifier):
1292 // reserve space for 18 extra.
1293 // Each byte in the char could be encoded as K_SPECIAL K_EXTRA x.
1294 if (*p == '<')
1295 extra += 18;
1296 }
1297 }
1298
1299 if (*p != '"')
1300 {
1301 semsg(_("E114: Missing quote: %s"), *arg);
1302 return FAIL;
1303 }
1304
1305 // If only parsing, set *arg and return here
1306 if (!evaluate)
1307 {
1308 *arg = p + 1;
1309 return OK;
1310 }
1311
1312 // Copy the string into allocated memory, handling backslashed
1313 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001314 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001315 len = (int)(p - *arg + extra);
1316 rettv->vval.v_string = alloc(len);
1317 if (rettv->vval.v_string == NULL)
1318 return FAIL;
1319 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001320
1321 for (p = *arg + 1; *p != NUL && *p != '"'; )
1322 {
1323 if (*p == '\\')
1324 {
1325 switch (*++p)
1326 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001327 case 'b': *end++ = BS; ++p; break;
1328 case 'e': *end++ = ESC; ++p; break;
1329 case 'f': *end++ = FF; ++p; break;
1330 case 'n': *end++ = NL; ++p; break;
1331 case 'r': *end++ = CAR; ++p; break;
1332 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001333
1334 case 'X': // hex: "\x1", "\x12"
1335 case 'x':
1336 case 'u': // Unicode: "\u0023"
1337 case 'U':
1338 if (vim_isxdigit(p[1]))
1339 {
1340 int n, nr;
1341 int c = toupper(*p);
1342
1343 if (c == 'X')
1344 n = 2;
1345 else if (*p == 'u')
1346 n = 4;
1347 else
1348 n = 8;
1349 nr = 0;
1350 while (--n >= 0 && vim_isxdigit(p[1]))
1351 {
1352 ++p;
1353 nr = (nr << 4) + hex2nr(*p);
1354 }
1355 ++p;
1356 // For "\u" store the number according to
1357 // 'encoding'.
1358 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001359 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001360 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001361 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001362 }
1363 break;
1364
1365 // octal: "\1", "\12", "\123"
1366 case '0':
1367 case '1':
1368 case '2':
1369 case '3':
1370 case '4':
1371 case '5':
1372 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001373 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001374 if (*p >= '0' && *p <= '7')
1375 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001376 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001377 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001378 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001379 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001380 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001381 break;
1382
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001383 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001384 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001385 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001386 int flags = FSK_KEYCODE | FSK_IN_STRING;
1387
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001388 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001389 flags |= FSK_SIMPLIFY;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001390 extra = trans_special(&p, end, flags, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001391 if (extra != 0)
1392 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001393 end += extra;
1394 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001395 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001396 break;
1397 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001398 }
1399 // FALLTHROUGH
1400
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001401 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001402 break;
1403 }
1404 }
1405 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001406 MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001407 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001408 *end = NUL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001409 if (*p != NUL) // just in case
1410 ++p;
1411 *arg = p;
1412
1413 return OK;
1414}
1415
1416/*
1417 * Allocate a variable for a 'str''ing' constant.
1418 * Return OK or FAIL.
1419 */
1420 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001421eval_lit_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001422{
1423 char_u *p;
1424 char_u *str;
1425 int reduce = 0;
1426
1427 // Find the end of the string, skipping ''.
1428 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
1429 {
1430 if (*p == '\'')
1431 {
1432 if (p[1] != '\'')
1433 break;
1434 ++reduce;
1435 ++p;
1436 }
1437 }
1438
1439 if (*p != '\'')
1440 {
1441 semsg(_("E115: Missing quote: %s"), *arg);
1442 return FAIL;
1443 }
1444
1445 // If only parsing return after setting "*arg"
1446 if (!evaluate)
1447 {
1448 *arg = p + 1;
1449 return OK;
1450 }
1451
1452 // Copy the string into allocated memory, handling '' to ' reduction.
1453 str = alloc((p - *arg) - reduce);
1454 if (str == NULL)
1455 return FAIL;
1456 rettv->v_type = VAR_STRING;
1457 rettv->vval.v_string = str;
1458
1459 for (p = *arg + 1; *p != NUL; )
1460 {
1461 if (*p == '\'')
1462 {
1463 if (p[1] != '\'')
1464 break;
1465 ++p;
1466 }
1467 MB_COPY_CHAR(p, str);
1468 }
1469 *str = NUL;
1470 *arg = p + 1;
1471
1472 return OK;
1473}
1474
1475/*
1476 * Return a string with the string representation of a variable.
1477 * If the memory is allocated "tofree" is set to it, otherwise NULL.
1478 * "numbuf" is used for a number.
1479 * Puts quotes around strings, so that they can be parsed back by eval().
1480 * May return NULL.
1481 */
1482 char_u *
1483tv2string(
1484 typval_T *tv,
1485 char_u **tofree,
1486 char_u *numbuf,
1487 int copyID)
1488{
1489 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
1490}
1491
1492/*
1493 * Get the value of an environment variable.
1494 * "arg" is pointing to the '$'. It is advanced to after the name.
1495 * If the environment variable was not set, silently assume it is empty.
1496 * Return FAIL if the name is invalid.
1497 */
1498 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001499eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001500{
1501 char_u *string = NULL;
1502 int len;
1503 int cc;
1504 char_u *name;
1505 int mustfree = FALSE;
1506
1507 ++*arg;
1508 name = *arg;
1509 len = get_env_len(arg);
1510 if (evaluate)
1511 {
1512 if (len == 0)
1513 return FAIL; // invalid empty name
1514
1515 cc = name[len];
1516 name[len] = NUL;
1517 // first try vim_getenv(), fast for normal environment vars
1518 string = vim_getenv(name, &mustfree);
1519 if (string != NULL && *string != NUL)
1520 {
1521 if (!mustfree)
1522 string = vim_strsave(string);
1523 }
1524 else
1525 {
1526 if (mustfree)
1527 vim_free(string);
1528
1529 // next try expanding things like $VIM and ${HOME}
1530 string = expand_env_save(name - 1);
1531 if (string != NULL && *string == '$')
1532 VIM_CLEAR(string);
1533 }
1534 name[len] = cc;
1535
1536 rettv->v_type = VAR_STRING;
1537 rettv->vval.v_string = string;
1538 }
1539
1540 return OK;
1541}
1542
1543/*
1544 * Get the lnum from the first argument.
1545 * Also accepts ".", "$", etc., but that only works for the current buffer.
1546 * Returns -1 on error.
1547 */
1548 linenr_T
1549tv_get_lnum(typval_T *argvars)
1550{
Bram Moolenaar9a963372020-12-21 21:58:46 +01001551 linenr_T lnum = -1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001552
Bram Moolenaar56acb092020-08-16 14:48:19 +02001553 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
1554 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar9a963372020-12-21 21:58:46 +01001555 if (lnum <= 0) // no valid number, try using arg like line()
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001556 {
1557 int fnum;
1558 pos_T *fp = var2fpos(&argvars[0], TRUE, &fnum);
1559
1560 if (fp != NULL)
1561 lnum = fp->lnum;
1562 }
1563 return lnum;
1564}
1565
1566/*
1567 * Get the lnum from the first argument.
1568 * Also accepts "$", then "buf" is used.
1569 * Returns 0 on error.
1570 */
1571 linenr_T
1572tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1573{
1574 if (argvars[0].v_type == VAR_STRING
1575 && argvars[0].vval.v_string != NULL
1576 && argvars[0].vval.v_string[0] == '$'
1577 && buf != NULL)
1578 return buf->b_ml.ml_line_count;
1579 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1580}
1581
1582/*
1583 * Get buffer by number or pattern.
1584 */
1585 buf_T *
1586tv_get_buf(typval_T *tv, int curtab_only)
1587{
1588 char_u *name = tv->vval.v_string;
1589 buf_T *buf;
1590
1591 if (tv->v_type == VAR_NUMBER)
1592 return buflist_findnr((int)tv->vval.v_number);
1593 if (tv->v_type != VAR_STRING)
1594 return NULL;
1595 if (name == NULL || *name == NUL)
1596 return curbuf;
1597 if (name[0] == '$' && name[1] == NUL)
1598 return lastbuf;
1599
1600 buf = buflist_find_by_name(name, curtab_only);
1601
1602 // If not found, try expanding the name, like done for bufexists().
1603 if (buf == NULL)
1604 buf = find_buffer(tv);
1605
1606 return buf;
1607}
1608
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02001609/*
1610 * Like tv_get_buf() but give an error message is the type is wrong.
1611 */
1612 buf_T *
1613tv_get_buf_from_arg(typval_T *tv)
1614{
1615 buf_T *buf;
1616
1617 ++emsg_off;
1618 buf = tv_get_buf(tv, FALSE);
1619 --emsg_off;
1620 if (buf == NULL
1621 && tv->v_type != VAR_NUMBER
1622 && tv->v_type != VAR_STRING)
1623 // issue errmsg for type error
1624 (void)tv_get_number(tv);
1625 return buf;
1626}
1627
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001628#endif // FEAT_EVAL