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