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