blob: 9ca35699d595bc7d9c94a9d36f573bc26b86b2ce [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:
Bram Moolenaarf18332f2021-05-07 17:55:55 +020094 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +020095 break;
96 }
97 vim_free(varp);
98 }
99}
100
101/*
102 * Free the memory for a variable value and set the value to NULL or 0.
103 */
104 void
105clear_tv(typval_T *varp)
106{
107 if (varp != NULL)
108 {
109 switch (varp->v_type)
110 {
111 case VAR_FUNC:
112 func_unref(varp->vval.v_string);
113 // FALLTHROUGH
114 case VAR_STRING:
115 VIM_CLEAR(varp->vval.v_string);
116 break;
117 case VAR_PARTIAL:
118 partial_unref(varp->vval.v_partial);
119 varp->vval.v_partial = NULL;
120 break;
121 case VAR_BLOB:
122 blob_unref(varp->vval.v_blob);
123 varp->vval.v_blob = NULL;
124 break;
125 case VAR_LIST:
126 list_unref(varp->vval.v_list);
127 varp->vval.v_list = NULL;
128 break;
129 case VAR_DICT:
130 dict_unref(varp->vval.v_dict);
131 varp->vval.v_dict = NULL;
132 break;
133 case VAR_NUMBER:
134 case VAR_BOOL:
135 case VAR_SPECIAL:
136 varp->vval.v_number = 0;
137 break;
138 case VAR_FLOAT:
139#ifdef FEAT_FLOAT
140 varp->vval.v_float = 0.0;
141 break;
142#endif
143 case VAR_JOB:
144#ifdef FEAT_JOB_CHANNEL
145 job_unref(varp->vval.v_job);
146 varp->vval.v_job = NULL;
147#endif
148 break;
149 case VAR_CHANNEL:
150#ifdef FEAT_JOB_CHANNEL
151 channel_unref(varp->vval.v_channel);
152 varp->vval.v_channel = NULL;
153#endif
Bram Moolenaar24f72092021-05-07 20:43:54 +0200154 break;
155 case VAR_INSTR:
156 VIM_CLEAR(varp->vval.v_instr);
157 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200158 case VAR_UNKNOWN:
159 case VAR_ANY:
160 case VAR_VOID:
161 break;
162 }
163 varp->v_lock = 0;
164 }
165}
166
167/*
168 * Set the value of a variable to NULL without freeing items.
169 */
170 void
171init_tv(typval_T *varp)
172{
173 if (varp != NULL)
174 CLEAR_POINTER(varp);
175}
176
Bram Moolenaar36967b32020-08-17 21:41:02 +0200177 static varnumber_T
178tv_get_bool_or_number_chk(typval_T *varp, int *denote, int want_bool)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200179{
180 varnumber_T n = 0L;
181
182 switch (varp->v_type)
183 {
184 case VAR_NUMBER:
Bram Moolenaarbade44e2020-09-26 22:39:24 +0200185 if (in_vim9script() && want_bool && varp->vval.v_number != 0
Bram Moolenaard70840e2020-08-22 15:06:35 +0200186 && varp->vval.v_number != 1)
187 {
188 semsg(_(e_using_number_as_bool_nr), varp->vval.v_number);
189 break;
190 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200191 return varp->vval.v_number;
192 case VAR_FLOAT:
193#ifdef FEAT_FLOAT
194 emsg(_("E805: Using a Float as a Number"));
195 break;
196#endif
197 case VAR_FUNC:
198 case VAR_PARTIAL:
199 emsg(_("E703: Using a Funcref as a Number"));
200 break;
201 case VAR_STRING:
Bram Moolenaar56acb092020-08-16 14:48:19 +0200202 if (in_vim9script())
203 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100204 emsg_using_string_as(varp, !want_bool);
Bram Moolenaar56acb092020-08-16 14:48:19 +0200205 break;
206 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200207 if (varp->vval.v_string != NULL)
208 vim_str2nr(varp->vval.v_string, NULL, NULL,
209 STR2NR_ALL, &n, NULL, 0, FALSE);
210 return n;
211 case VAR_LIST:
212 emsg(_("E745: Using a List as a Number"));
213 break;
214 case VAR_DICT:
215 emsg(_("E728: Using a Dictionary as a Number"));
216 break;
217 case VAR_BOOL:
218 case VAR_SPECIAL:
Bram Moolenaar36967b32020-08-17 21:41:02 +0200219 if (!want_bool && in_vim9script())
Bram Moolenaar56acb092020-08-16 14:48:19 +0200220 {
Bram Moolenaard92cc132020-11-18 17:17:15 +0100221 if (varp->v_type == VAR_BOOL)
222 emsg(_(e_using_bool_as_number));
223 else
224 emsg(_("E611: Using a Special as a Number"));
Bram Moolenaar56acb092020-08-16 14:48:19 +0200225 break;
226 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200227 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
228 case VAR_JOB:
229#ifdef FEAT_JOB_CHANNEL
230 emsg(_("E910: Using a Job as a Number"));
231 break;
232#endif
233 case VAR_CHANNEL:
234#ifdef FEAT_JOB_CHANNEL
235 emsg(_("E913: Using a Channel as a Number"));
236 break;
237#endif
238 case VAR_BLOB:
239 emsg(_("E974: Using a Blob as a Number"));
240 break;
241 case VAR_UNKNOWN:
242 case VAR_ANY:
243 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200244 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200245 internal_error_no_abort("tv_get_number(UNKNOWN)");
246 break;
247 }
248 if (denote == NULL) // useful for values that must be unsigned
249 n = -1;
250 else
251 *denote = TRUE;
252 return n;
253}
254
Bram Moolenaar36967b32020-08-17 21:41:02 +0200255/*
256 * Get the number value of a variable.
257 * If it is a String variable, uses vim_str2nr().
258 * For incompatible types, return 0.
259 * tv_get_number_chk() is similar to tv_get_number(), but informs the
260 * caller of incompatible types: it sets *denote to TRUE if "denote"
261 * is not NULL or returns -1 otherwise.
262 */
263 varnumber_T
264tv_get_number(typval_T *varp)
265{
266 int error = FALSE;
267
268 return tv_get_number_chk(varp, &error); // return 0L on error
269}
270
271 varnumber_T
272tv_get_number_chk(typval_T *varp, int *denote)
273{
274 return tv_get_bool_or_number_chk(varp, denote, FALSE);
275}
276
277/*
278 * Get the boolean value of "varp". This is like tv_get_number_chk(),
Bram Moolenaard70840e2020-08-22 15:06:35 +0200279 * but in Vim9 script accepts Number (0 and 1) and Bool/Special.
Bram Moolenaar36967b32020-08-17 21:41:02 +0200280 */
281 varnumber_T
282tv_get_bool(typval_T *varp)
283{
284 return tv_get_bool_or_number_chk(varp, NULL, TRUE);
Bram Moolenaar36967b32020-08-17 21:41:02 +0200285}
286
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200287/*
288 * Get the boolean value of "varp". This is like tv_get_number_chk(),
289 * but in Vim9 script accepts Number and Bool.
290 */
291 varnumber_T
292tv_get_bool_chk(typval_T *varp, int *denote)
293{
294 return tv_get_bool_or_number_chk(varp, denote, TRUE);
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200295}
296
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200297#ifdef FEAT_FLOAT
298 float_T
299tv_get_float(typval_T *varp)
300{
301 switch (varp->v_type)
302 {
303 case VAR_NUMBER:
304 return (float_T)(varp->vval.v_number);
305 case VAR_FLOAT:
306 return varp->vval.v_float;
307 case VAR_FUNC:
308 case VAR_PARTIAL:
309 emsg(_("E891: Using a Funcref as a Float"));
310 break;
311 case VAR_STRING:
312 emsg(_("E892: Using a String as a Float"));
313 break;
314 case VAR_LIST:
315 emsg(_("E893: Using a List as a Float"));
316 break;
317 case VAR_DICT:
318 emsg(_("E894: Using a Dictionary as a Float"));
319 break;
320 case VAR_BOOL:
321 emsg(_("E362: Using a boolean value as a Float"));
322 break;
323 case VAR_SPECIAL:
324 emsg(_("E907: Using a special value as a Float"));
325 break;
326 case VAR_JOB:
327# ifdef FEAT_JOB_CHANNEL
328 emsg(_("E911: Using a Job as a Float"));
329 break;
330# endif
331 case VAR_CHANNEL:
332# ifdef FEAT_JOB_CHANNEL
333 emsg(_("E914: Using a Channel as a Float"));
334 break;
335# endif
336 case VAR_BLOB:
337 emsg(_("E975: Using a Blob as a Float"));
338 break;
339 case VAR_UNKNOWN:
340 case VAR_ANY:
341 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200342 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200343 internal_error_no_abort("tv_get_float(UNKNOWN)");
344 break;
345 }
346 return 0;
347}
348#endif
349
350/*
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100351 * Give an error and return FAIL unless "tv" is a string.
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100352 */
353 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100354check_for_string_arg(typval_T *args, int idx)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100355{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100356 if (args[idx].v_type != VAR_STRING)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100357 {
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100358 if (idx >= 0)
359 semsg(_(e_string_required_for_argument_nr), idx + 1);
Bram Moolenaarf28f2ac2021-03-22 22:21:26 +0100360 else
361 emsg(_(e_stringreq));
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100362 return FAIL;
363 }
364 return OK;
365}
366
367/*
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100368 * Give an error and return FAIL unless "args[idx]" is a non-empty string.
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100369 */
370 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100371check_for_nonempty_string_arg(typval_T *args, int idx)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100372{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100373 if (check_for_string_arg(args, idx) == FAIL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100374 return FAIL;
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100375 if (args[idx].vval.v_string == NULL || *args[idx].vval.v_string == NUL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100376 {
Bram Moolenaare8e30782021-04-10 21:46:05 +0200377 semsg(_(e_non_empty_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100378 return FAIL;
379 }
380 return OK;
381}
382
383/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200384 * Get the string value of a variable.
385 * If it is a Number variable, the number is converted into a string.
386 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
387 * tv_get_string_buf() uses a given buffer.
388 * If the String variable has never been set, return an empty string.
389 * Never returns NULL;
390 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
391 * NULL on error.
392 */
393 char_u *
394tv_get_string(typval_T *varp)
395{
396 static char_u mybuf[NUMBUFLEN];
397
398 return tv_get_string_buf(varp, mybuf);
399}
400
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100401/*
402 * Like tv_get_string() but don't allow number to string conversion for Vim9.
403 */
404 char_u *
405tv_get_string_strict(typval_T *varp)
406{
407 static char_u mybuf[NUMBUFLEN];
408 char_u *res = tv_get_string_buf_chk_strict(
409 varp, mybuf, in_vim9script());
410
411 return res != NULL ? res : (char_u *)"";
412}
413
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200414 char_u *
415tv_get_string_buf(typval_T *varp, char_u *buf)
416{
417 char_u *res = tv_get_string_buf_chk(varp, buf);
418
419 return res != NULL ? res : (char_u *)"";
420}
421
422/*
423 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
424 */
425 char_u *
426tv_get_string_chk(typval_T *varp)
427{
428 static char_u mybuf[NUMBUFLEN];
429
430 return tv_get_string_buf_chk(varp, mybuf);
431}
432
433 char_u *
434tv_get_string_buf_chk(typval_T *varp, char_u *buf)
435{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100436 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
437}
438
439 char_u *
440tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
441{
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200442 switch (varp->v_type)
443 {
444 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100445 if (strict)
446 {
447 emsg(_(e_using_number_as_string));
448 break;
449 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200450 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
451 (varnumber_T)varp->vval.v_number);
452 return buf;
453 case VAR_FUNC:
454 case VAR_PARTIAL:
455 emsg(_("E729: using Funcref as a String"));
456 break;
457 case VAR_LIST:
458 emsg(_("E730: using List as a String"));
459 break;
460 case VAR_DICT:
461 emsg(_("E731: using Dictionary as a String"));
462 break;
463 case VAR_FLOAT:
464#ifdef FEAT_FLOAT
465 emsg(_(e_float_as_string));
466 break;
467#endif
468 case VAR_STRING:
469 if (varp->vval.v_string != NULL)
470 return varp->vval.v_string;
471 return (char_u *)"";
472 case VAR_BOOL:
473 case VAR_SPECIAL:
474 STRCPY(buf, get_var_special_name(varp->vval.v_number));
475 return buf;
476 case VAR_BLOB:
477 emsg(_("E976: using Blob as a String"));
478 break;
479 case VAR_JOB:
480#ifdef FEAT_JOB_CHANNEL
481 {
482 job_T *job = varp->vval.v_job;
483 char *status;
484
485 if (job == NULL)
486 return (char_u *)"no process";
487 status = job->jv_status == JOB_FAILED ? "fail"
488 : job->jv_status >= JOB_ENDED ? "dead"
489 : "run";
490# ifdef UNIX
491 vim_snprintf((char *)buf, NUMBUFLEN,
492 "process %ld %s", (long)job->jv_pid, status);
493# elif defined(MSWIN)
494 vim_snprintf((char *)buf, NUMBUFLEN,
495 "process %ld %s",
496 (long)job->jv_proc_info.dwProcessId,
497 status);
498# else
499 // fall-back
500 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
501# endif
502 return buf;
503 }
504#endif
505 break;
506 case VAR_CHANNEL:
507#ifdef FEAT_JOB_CHANNEL
508 {
509 channel_T *channel = varp->vval.v_channel;
510 char *status = channel_status(channel, -1);
511
512 if (channel == NULL)
513 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
514 else
515 vim_snprintf((char *)buf, NUMBUFLEN,
516 "channel %d %s", channel->ch_id, status);
517 return buf;
518 }
519#endif
520 break;
521 case VAR_UNKNOWN:
522 case VAR_ANY:
523 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200524 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200525 emsg(_(e_inval_string));
526 break;
527 }
528 return NULL;
529}
530
531/*
532 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
533 * string() on Dict, List, etc.
534 */
535 char_u *
536tv_stringify(typval_T *varp, char_u *buf)
537{
538 if (varp->v_type == VAR_LIST
539 || varp->v_type == VAR_DICT
540 || varp->v_type == VAR_BLOB
541 || varp->v_type == VAR_FUNC
542 || varp->v_type == VAR_PARTIAL
543 || varp->v_type == VAR_FLOAT)
544 {
545 typval_T tmp;
546
547 f_string(varp, &tmp);
548 tv_get_string_buf(&tmp, buf);
549 clear_tv(varp);
550 *varp = tmp;
551 return tmp.vval.v_string;
552 }
553 return tv_get_string_buf(varp, buf);
554}
555
556/*
557 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
558 * Also give an error message, using "name" or _("name") when use_gettext is
559 * TRUE.
560 */
561 int
562tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
563{
564 int lock = 0;
565
566 switch (tv->v_type)
567 {
568 case VAR_BLOB:
569 if (tv->vval.v_blob != NULL)
570 lock = tv->vval.v_blob->bv_lock;
571 break;
572 case VAR_LIST:
573 if (tv->vval.v_list != NULL)
574 lock = tv->vval.v_list->lv_lock;
575 break;
576 case VAR_DICT:
577 if (tv->vval.v_dict != NULL)
578 lock = tv->vval.v_dict->dv_lock;
579 break;
580 default:
581 break;
582 }
Bram Moolenaara187c432020-09-16 21:08:28 +0200583 return value_check_lock(tv->v_lock, name, use_gettext)
584 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200585}
586
587/*
588 * Copy the values from typval_T "from" to typval_T "to".
589 * When needed allocates string or increases reference count.
590 * Does not make a copy of a list, blob or dict but copies the reference!
591 * It is OK for "from" and "to" to point to the same item. This is used to
592 * make a copy later.
593 */
594 void
595copy_tv(typval_T *from, typval_T *to)
596{
597 to->v_type = from->v_type;
598 to->v_lock = 0;
599 switch (from->v_type)
600 {
601 case VAR_NUMBER:
602 case VAR_BOOL:
603 case VAR_SPECIAL:
604 to->vval.v_number = from->vval.v_number;
605 break;
606 case VAR_FLOAT:
607#ifdef FEAT_FLOAT
608 to->vval.v_float = from->vval.v_float;
609 break;
610#endif
611 case VAR_JOB:
612#ifdef FEAT_JOB_CHANNEL
613 to->vval.v_job = from->vval.v_job;
614 if (to->vval.v_job != NULL)
615 ++to->vval.v_job->jv_refcount;
616 break;
617#endif
618 case VAR_CHANNEL:
619#ifdef FEAT_JOB_CHANNEL
620 to->vval.v_channel = from->vval.v_channel;
621 if (to->vval.v_channel != NULL)
622 ++to->vval.v_channel->ch_refcount;
623 break;
624#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200625 case VAR_INSTR:
626 to->vval.v_instr = from->vval.v_instr;
627 break;
628
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200629 case VAR_STRING:
630 case VAR_FUNC:
631 if (from->vval.v_string == NULL)
632 to->vval.v_string = NULL;
633 else
634 {
635 to->vval.v_string = vim_strsave(from->vval.v_string);
636 if (from->v_type == VAR_FUNC)
637 func_ref(to->vval.v_string);
638 }
639 break;
640 case VAR_PARTIAL:
641 if (from->vval.v_partial == NULL)
642 to->vval.v_partial = NULL;
643 else
644 {
645 to->vval.v_partial = from->vval.v_partial;
646 ++to->vval.v_partial->pt_refcount;
647 }
648 break;
649 case VAR_BLOB:
650 if (from->vval.v_blob == NULL)
651 to->vval.v_blob = NULL;
652 else
653 {
654 to->vval.v_blob = from->vval.v_blob;
655 ++to->vval.v_blob->bv_refcount;
656 }
657 break;
658 case VAR_LIST:
659 if (from->vval.v_list == NULL)
660 to->vval.v_list = NULL;
661 else
662 {
663 to->vval.v_list = from->vval.v_list;
664 ++to->vval.v_list->lv_refcount;
665 }
666 break;
667 case VAR_DICT:
668 if (from->vval.v_dict == NULL)
669 to->vval.v_dict = NULL;
670 else
671 {
672 to->vval.v_dict = from->vval.v_dict;
673 ++to->vval.v_dict->dv_refcount;
674 }
675 break;
676 case VAR_UNKNOWN:
677 case VAR_ANY:
678 case VAR_VOID:
679 internal_error_no_abort("copy_tv(UNKNOWN)");
680 break;
681 }
682}
683
684/*
685 * Compare "typ1" and "typ2". Put the result in "typ1".
686 */
687 int
688typval_compare(
689 typval_T *typ1, // first operand
690 typval_T *typ2, // second operand
Bram Moolenaar657137c2021-01-09 15:45:23 +0100691 exprtype_T type, // operator
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200692 int ic) // ignore case
693{
694 int i;
695 varnumber_T n1, n2;
696 char_u *s1, *s2;
697 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
698 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
699
700 if (type_is && typ1->v_type != typ2->v_type)
701 {
702 // For "is" a different type always means FALSE, for "notis"
703 // it means TRUE.
704 n1 = (type == EXPR_ISNOT);
705 }
706 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
707 {
708 if (type_is)
709 {
710 n1 = (typ1->v_type == typ2->v_type
711 && typ1->vval.v_blob == typ2->vval.v_blob);
712 if (type == EXPR_ISNOT)
713 n1 = !n1;
714 }
715 else if (typ1->v_type != typ2->v_type
716 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
717 {
718 if (typ1->v_type != typ2->v_type)
719 emsg(_("E977: Can only compare Blob with Blob"));
720 else
721 emsg(_(e_invalblob));
722 clear_tv(typ1);
723 return FAIL;
724 }
725 else
726 {
727 // Compare two Blobs for being equal or unequal.
728 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
729 if (type == EXPR_NEQUAL)
730 n1 = !n1;
731 }
732 }
733 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
734 {
735 if (type_is)
736 {
737 n1 = (typ1->v_type == typ2->v_type
738 && typ1->vval.v_list == typ2->vval.v_list);
739 if (type == EXPR_ISNOT)
740 n1 = !n1;
741 }
742 else if (typ1->v_type != typ2->v_type
743 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
744 {
745 if (typ1->v_type != typ2->v_type)
746 emsg(_("E691: Can only compare List with List"));
747 else
748 emsg(_("E692: Invalid operation for List"));
749 clear_tv(typ1);
750 return FAIL;
751 }
752 else
753 {
754 // Compare two Lists for being equal or unequal.
755 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
756 ic, FALSE);
757 if (type == EXPR_NEQUAL)
758 n1 = !n1;
759 }
760 }
761
762 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
763 {
764 if (type_is)
765 {
766 n1 = (typ1->v_type == typ2->v_type
767 && typ1->vval.v_dict == typ2->vval.v_dict);
768 if (type == EXPR_ISNOT)
769 n1 = !n1;
770 }
771 else if (typ1->v_type != typ2->v_type
772 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
773 {
774 if (typ1->v_type != typ2->v_type)
775 emsg(_("E735: Can only compare Dictionary with Dictionary"));
776 else
777 emsg(_("E736: Invalid operation for Dictionary"));
778 clear_tv(typ1);
779 return FAIL;
780 }
781 else
782 {
783 // Compare two Dictionaries for being equal or unequal.
784 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
785 ic, FALSE);
786 if (type == EXPR_NEQUAL)
787 n1 = !n1;
788 }
789 }
790
791 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
792 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
793 {
794 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
795 && type != EXPR_IS && type != EXPR_ISNOT)
796 {
797 emsg(_("E694: Invalid operation for Funcrefs"));
798 clear_tv(typ1);
799 return FAIL;
800 }
801 if ((typ1->v_type == VAR_PARTIAL
802 && typ1->vval.v_partial == NULL)
803 || (typ2->v_type == VAR_PARTIAL
804 && typ2->vval.v_partial == NULL))
805 // When both partials are NULL, then they are equal.
806 // Otherwise they are not equal.
807 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
808 else if (type_is)
809 {
810 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
811 // strings are considered the same if their value is
812 // the same
813 n1 = tv_equal(typ1, typ2, ic, FALSE);
814 else if (typ1->v_type == VAR_PARTIAL
815 && typ2->v_type == VAR_PARTIAL)
816 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
817 else
818 n1 = FALSE;
819 }
820 else
821 n1 = tv_equal(typ1, typ2, ic, FALSE);
822 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
823 n1 = !n1;
824 }
825
826#ifdef FEAT_FLOAT
827 // If one of the two variables is a float, compare as a float.
828 // When using "=~" or "!~", always compare as string.
829 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
830 && type != EXPR_MATCH && type != EXPR_NOMATCH)
831 {
832 float_T f1, f2;
833
834 f1 = tv_get_float(typ1);
835 f2 = tv_get_float(typ2);
836 n1 = FALSE;
837 switch (type)
838 {
839 case EXPR_IS:
840 case EXPR_EQUAL: n1 = (f1 == f2); break;
841 case EXPR_ISNOT:
842 case EXPR_NEQUAL: n1 = (f1 != f2); break;
843 case EXPR_GREATER: n1 = (f1 > f2); break;
844 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
845 case EXPR_SMALLER: n1 = (f1 < f2); break;
846 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
847 case EXPR_UNKNOWN:
848 case EXPR_MATCH:
849 default: break; // avoid gcc warning
850 }
851 }
852#endif
853
854 // If one of the two variables is a number, compare as a number.
855 // When using "=~" or "!~", always compare as string.
856 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
857 && type != EXPR_MATCH && type != EXPR_NOMATCH)
858 {
859 n1 = tv_get_number(typ1);
860 n2 = tv_get_number(typ2);
861 switch (type)
862 {
863 case EXPR_IS:
864 case EXPR_EQUAL: n1 = (n1 == n2); break;
865 case EXPR_ISNOT:
866 case EXPR_NEQUAL: n1 = (n1 != n2); break;
867 case EXPR_GREATER: n1 = (n1 > n2); break;
868 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
869 case EXPR_SMALLER: n1 = (n1 < n2); break;
870 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
871 case EXPR_UNKNOWN:
872 case EXPR_MATCH:
873 default: break; // avoid gcc warning
874 }
875 }
Bram Moolenaarcff40ff2021-01-09 16:21:37 +0100876 else if (in_vim9script() && (typ1->v_type == VAR_BOOL
877 || typ2->v_type == VAR_BOOL))
878 {
879 if (typ1->v_type != typ2->v_type)
880 {
881 semsg(_(e_cannot_compare_str_with_str),
882 vartype_name(typ1->v_type), vartype_name(typ2->v_type));
883 clear_tv(typ1);
884 return FAIL;
885 }
886 n1 = typ1->vval.v_number;
887 n2 = typ2->vval.v_number;
888 switch (type)
889 {
890 case EXPR_IS:
891 case EXPR_EQUAL: n1 = (n1 == n2); break;
892 case EXPR_ISNOT:
893 case EXPR_NEQUAL: n1 = (n1 != n2); break;
894 default:
895 emsg(_(e_invalid_operation_for_bool));
896 clear_tv(typ1);
897 return FAIL;
898 }
899 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200900 else
901 {
902 s1 = tv_get_string_buf(typ1, buf1);
903 s2 = tv_get_string_buf(typ2, buf2);
904 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
905 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
906 else
907 i = 0;
908 n1 = FALSE;
909 switch (type)
910 {
911 case EXPR_IS:
912 case EXPR_EQUAL: n1 = (i == 0); break;
913 case EXPR_ISNOT:
914 case EXPR_NEQUAL: n1 = (i != 0); break;
915 case EXPR_GREATER: n1 = (i > 0); break;
916 case EXPR_GEQUAL: n1 = (i >= 0); break;
917 case EXPR_SMALLER: n1 = (i < 0); break;
918 case EXPR_SEQUAL: n1 = (i <= 0); break;
919
920 case EXPR_MATCH:
921 case EXPR_NOMATCH:
922 n1 = pattern_match(s2, s1, ic);
923 if (type == EXPR_NOMATCH)
924 n1 = !n1;
925 break;
926
927 default: break; // avoid gcc warning
928 }
929 }
930 clear_tv(typ1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +0200931 if (in_vim9script())
932 {
933 typ1->v_type = VAR_BOOL;
934 typ1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
935 }
936 else
937 {
938 typ1->v_type = VAR_NUMBER;
939 typ1->vval.v_number = n1;
940 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200941
942 return OK;
943}
944
Bram Moolenaar34453202021-01-31 13:08:38 +0100945/*
946 * Convert any type to a string, never give an error.
947 * When "quotes" is TRUE add quotes to a string.
948 * Returns an allocated string.
949 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200950 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +0100951typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200952{
953 char_u *tofree;
954 char_u numbuf[NUMBUFLEN];
955 char_u *ret = NULL;
956
957 if (arg == NULL)
958 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +0100959 if (!quotes && arg->v_type == VAR_STRING)
960 {
961 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
962 : arg->vval.v_string);
963 }
964 else
965 {
966 ret = tv2string(arg, &tofree, numbuf, 0);
967 // Make a copy if we have a value but it's not in allocated memory.
968 if (ret != NULL && tofree == NULL)
969 ret = vim_strsave(ret);
970 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200971 return ret;
972}
973
974/*
975 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
976 * or it refers to a List or Dictionary that is locked.
977 */
978 int
979tv_islocked(typval_T *tv)
980{
981 return (tv->v_lock & VAR_LOCKED)
982 || (tv->v_type == VAR_LIST
983 && tv->vval.v_list != NULL
984 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
985 || (tv->v_type == VAR_DICT
986 && tv->vval.v_dict != NULL
987 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
988}
989
990 static int
991func_equal(
992 typval_T *tv1,
993 typval_T *tv2,
994 int ic) // ignore case
995{
996 char_u *s1, *s2;
997 dict_T *d1, *d2;
998 int a1, a2;
999 int i;
1000
1001 // empty and NULL function name considered the same
1002 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1003 : partial_name(tv1->vval.v_partial);
1004 if (s1 != NULL && *s1 == NUL)
1005 s1 = NULL;
1006 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1007 : partial_name(tv2->vval.v_partial);
1008 if (s2 != NULL && *s2 == NUL)
1009 s2 = NULL;
1010 if (s1 == NULL || s2 == NULL)
1011 {
1012 if (s1 != s2)
1013 return FALSE;
1014 }
1015 else if (STRCMP(s1, s2) != 0)
1016 return FALSE;
1017
1018 // empty dict and NULL dict is different
1019 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1020 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1021 if (d1 == NULL || d2 == NULL)
1022 {
1023 if (d1 != d2)
1024 return FALSE;
1025 }
1026 else if (!dict_equal(d1, d2, ic, TRUE))
1027 return FALSE;
1028
1029 // empty list and no list considered the same
1030 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1031 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1032 if (a1 != a2)
1033 return FALSE;
1034 for (i = 0; i < a1; ++i)
1035 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1036 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1037 return FALSE;
1038
1039 return TRUE;
1040}
1041
1042/*
1043 * Return TRUE if "tv1" and "tv2" have the same value.
1044 * Compares the items just like "==" would compare them, but strings and
1045 * numbers are different. Floats and numbers are also different.
1046 */
1047 int
1048tv_equal(
1049 typval_T *tv1,
1050 typval_T *tv2,
1051 int ic, // ignore case
1052 int recursive) // TRUE when used recursively
1053{
1054 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1055 char_u *s1, *s2;
1056 static int recursive_cnt = 0; // catch recursive loops
1057 int r;
1058 static int tv_equal_recurse_limit;
1059
1060 // Catch lists and dicts that have an endless loop by limiting
1061 // recursiveness to a limit. We guess they are equal then.
1062 // A fixed limit has the problem of still taking an awful long time.
1063 // Reduce the limit every time running into it. That should work fine for
1064 // deeply linked structures that are not recursively linked and catch
1065 // recursiveness quickly.
1066 if (!recursive)
1067 tv_equal_recurse_limit = 1000;
1068 if (recursive_cnt >= tv_equal_recurse_limit)
1069 {
1070 --tv_equal_recurse_limit;
1071 return TRUE;
1072 }
1073
1074 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1075 // arguments.
1076 if ((tv1->v_type == VAR_FUNC
1077 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1078 && (tv2->v_type == VAR_FUNC
1079 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1080 {
1081 ++recursive_cnt;
1082 r = func_equal(tv1, tv2, ic);
1083 --recursive_cnt;
1084 return r;
1085 }
1086
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001087 if (tv1->v_type != tv2->v_type
1088 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1089 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001090 return FALSE;
1091
1092 switch (tv1->v_type)
1093 {
1094 case VAR_LIST:
1095 ++recursive_cnt;
1096 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1097 --recursive_cnt;
1098 return r;
1099
1100 case VAR_DICT:
1101 ++recursive_cnt;
1102 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1103 --recursive_cnt;
1104 return r;
1105
1106 case VAR_BLOB:
1107 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1108
1109 case VAR_NUMBER:
1110 case VAR_BOOL:
1111 case VAR_SPECIAL:
1112 return tv1->vval.v_number == tv2->vval.v_number;
1113
1114 case VAR_STRING:
1115 s1 = tv_get_string_buf(tv1, buf1);
1116 s2 = tv_get_string_buf(tv2, buf2);
1117 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1118
1119 case VAR_FLOAT:
1120#ifdef FEAT_FLOAT
1121 return tv1->vval.v_float == tv2->vval.v_float;
1122#endif
1123 case VAR_JOB:
1124#ifdef FEAT_JOB_CHANNEL
1125 return tv1->vval.v_job == tv2->vval.v_job;
1126#endif
1127 case VAR_CHANNEL:
1128#ifdef FEAT_JOB_CHANNEL
1129 return tv1->vval.v_channel == tv2->vval.v_channel;
1130#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001131 case VAR_INSTR:
1132 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001133
1134 case VAR_PARTIAL:
1135 return tv1->vval.v_partial == tv2->vval.v_partial;
1136
1137 case VAR_FUNC:
1138 return tv1->vval.v_string == tv2->vval.v_string;
1139
1140 case VAR_UNKNOWN:
1141 case VAR_ANY:
1142 case VAR_VOID:
1143 break;
1144 }
1145
1146 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1147 // does not equal anything, not even itself.
1148 return FALSE;
1149}
1150
1151/*
1152 * Get an option value.
1153 * "arg" points to the '&' or '+' before the option name.
1154 * "arg" is advanced to character after the option name.
1155 * Return OK or FAIL.
1156 */
1157 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001158eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001159 char_u **arg,
1160 typval_T *rettv, // when NULL, only check if option exists
1161 int evaluate)
1162{
1163 char_u *option_end;
1164 long numval;
1165 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001166 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001167 int c;
1168 int working = (**arg == '+'); // has("+option")
1169 int ret = OK;
1170 int opt_flags;
1171
1172 // Isolate the option name and find its value.
1173 option_end = find_option_end(arg, &opt_flags);
1174 if (option_end == NULL)
1175 {
1176 if (rettv != NULL)
1177 semsg(_("E112: Option name missing: %s"), *arg);
1178 return FAIL;
1179 }
1180
1181 if (!evaluate)
1182 {
1183 *arg = option_end;
1184 return OK;
1185 }
1186
1187 c = *option_end;
1188 *option_end = NUL;
1189 opt_type = get_option_value(*arg, &numval,
1190 rettv == NULL ? NULL : &stringval, opt_flags);
1191
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001192 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001193 {
1194 if (rettv != NULL)
1195 semsg(_(e_unknown_option), *arg);
1196 ret = FAIL;
1197 }
1198 else if (rettv != NULL)
1199 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001200 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001201 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001202 {
1203 rettv->v_type = VAR_STRING;
1204 rettv->vval.v_string = NULL;
1205 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001206 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001207 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001208 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1209 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001210 rettv->vval.v_number = 0;
1211 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001212 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001213 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001214 if (in_vim9script() && opt_type == gov_bool)
1215 {
1216 rettv->v_type = VAR_BOOL;
1217 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1218 }
1219 else
1220 {
1221 rettv->v_type = VAR_NUMBER;
1222 rettv->vval.v_number = numval;
1223 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001224 }
1225 else // string option
1226 {
1227 rettv->v_type = VAR_STRING;
1228 rettv->vval.v_string = stringval;
1229 }
1230 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001231 else if (working && (opt_type == gov_hidden_bool
1232 || opt_type == gov_hidden_number
1233 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001234 ret = FAIL;
1235
1236 *option_end = c; // put back for error messages
1237 *arg = option_end;
1238
1239 return ret;
1240}
1241
1242/*
1243 * Allocate a variable for a number constant. Also deals with "0z" for blob.
1244 * Return OK or FAIL.
1245 */
1246 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001247eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001248 char_u **arg,
1249 typval_T *rettv,
1250 int evaluate,
1251 int want_string UNUSED)
1252{
1253 int len;
1254#ifdef FEAT_FLOAT
1255 char_u *p;
1256 int get_float = FALSE;
1257
1258 // We accept a float when the format matches
1259 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
1260 // strict to avoid backwards compatibility problems.
1261 // With script version 2 and later the leading digit can be
1262 // omitted.
1263 // Don't look for a float after the "." operator, so that
1264 // ":let vers = 1.2.3" doesn't fail.
1265 if (**arg == '.')
1266 p = *arg;
1267 else
1268 p = skipdigits(*arg + 1);
1269 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
1270 {
1271 get_float = TRUE;
1272 p = skipdigits(p + 2);
1273 if (*p == 'e' || *p == 'E')
1274 {
1275 ++p;
1276 if (*p == '-' || *p == '+')
1277 ++p;
1278 if (!vim_isdigit(*p))
1279 get_float = FALSE;
1280 else
1281 p = skipdigits(p + 1);
1282 }
1283 if (ASCII_ISALPHA(*p) || *p == '.')
1284 get_float = FALSE;
1285 }
1286 if (get_float)
1287 {
1288 float_T f;
1289
1290 *arg += string2float(*arg, &f);
1291 if (evaluate)
1292 {
1293 rettv->v_type = VAR_FLOAT;
1294 rettv->vval.v_float = f;
1295 }
1296 }
1297 else
1298#endif
1299 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
1300 {
1301 char_u *bp;
1302 blob_T *blob = NULL; // init for gcc
1303
1304 // Blob constant: 0z0123456789abcdef
1305 if (evaluate)
1306 blob = blob_alloc();
1307 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
1308 {
1309 if (!vim_isxdigit(bp[1]))
1310 {
1311 if (blob != NULL)
1312 {
1313 emsg(_("E973: Blob literal should have an even number of hex characters"));
1314 ga_clear(&blob->bv_ga);
1315 VIM_CLEAR(blob);
1316 }
1317 return FAIL;
1318 }
1319 if (blob != NULL)
1320 ga_append(&blob->bv_ga,
1321 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
1322 if (bp[2] == '.' && vim_isxdigit(bp[3]))
1323 ++bp;
1324 }
1325 if (blob != NULL)
1326 rettv_blob_set(rettv, blob);
1327 *arg = bp;
1328 }
1329 else
1330 {
1331 varnumber_T n;
1332
1333 // decimal, hex or octal number
1334 vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4
1335 ? STR2NR_NO_OCT + STR2NR_QUOTE
1336 : STR2NR_ALL, &n, NULL, 0, TRUE);
1337 if (len == 0)
1338 {
1339 semsg(_(e_invexpr2), *arg);
1340 return FAIL;
1341 }
1342 *arg += len;
1343 if (evaluate)
1344 {
1345 rettv->v_type = VAR_NUMBER;
1346 rettv->vval.v_number = n;
1347 }
1348 }
1349 return OK;
1350}
1351
1352/*
1353 * Allocate a variable for a string constant.
1354 * Return OK or FAIL.
1355 */
1356 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001357eval_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001358{
1359 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001360 char_u *end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001361 int extra = 0;
1362 int len;
1363
1364 // Find the end of the string, skipping backslashed characters.
1365 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
1366 {
1367 if (*p == '\\' && p[1] != NUL)
1368 {
1369 ++p;
1370 // A "\<x>" form occupies at least 4 characters, and produces up
1371 // to 21 characters (3 * 6 for the char and 3 for a modifier):
1372 // reserve space for 18 extra.
1373 // Each byte in the char could be encoded as K_SPECIAL K_EXTRA x.
1374 if (*p == '<')
1375 extra += 18;
1376 }
1377 }
1378
1379 if (*p != '"')
1380 {
1381 semsg(_("E114: Missing quote: %s"), *arg);
1382 return FAIL;
1383 }
1384
1385 // If only parsing, set *arg and return here
1386 if (!evaluate)
1387 {
1388 *arg = p + 1;
1389 return OK;
1390 }
1391
1392 // Copy the string into allocated memory, handling backslashed
1393 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001394 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001395 len = (int)(p - *arg + extra);
1396 rettv->vval.v_string = alloc(len);
1397 if (rettv->vval.v_string == NULL)
1398 return FAIL;
1399 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001400
1401 for (p = *arg + 1; *p != NUL && *p != '"'; )
1402 {
1403 if (*p == '\\')
1404 {
1405 switch (*++p)
1406 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001407 case 'b': *end++ = BS; ++p; break;
1408 case 'e': *end++ = ESC; ++p; break;
1409 case 'f': *end++ = FF; ++p; break;
1410 case 'n': *end++ = NL; ++p; break;
1411 case 'r': *end++ = CAR; ++p; break;
1412 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001413
1414 case 'X': // hex: "\x1", "\x12"
1415 case 'x':
1416 case 'u': // Unicode: "\u0023"
1417 case 'U':
1418 if (vim_isxdigit(p[1]))
1419 {
1420 int n, nr;
1421 int c = toupper(*p);
1422
1423 if (c == 'X')
1424 n = 2;
1425 else if (*p == 'u')
1426 n = 4;
1427 else
1428 n = 8;
1429 nr = 0;
1430 while (--n >= 0 && vim_isxdigit(p[1]))
1431 {
1432 ++p;
1433 nr = (nr << 4) + hex2nr(*p);
1434 }
1435 ++p;
1436 // For "\u" store the number according to
1437 // 'encoding'.
1438 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001439 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001440 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001441 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001442 }
1443 break;
1444
1445 // octal: "\1", "\12", "\123"
1446 case '0':
1447 case '1':
1448 case '2':
1449 case '3':
1450 case '4':
1451 case '5':
1452 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001453 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001454 if (*p >= '0' && *p <= '7')
1455 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001456 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001457 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001458 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001459 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001460 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001461 break;
1462
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001463 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001464 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001465 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001466 int flags = FSK_KEYCODE | FSK_IN_STRING;
1467
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001468 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001469 flags |= FSK_SIMPLIFY;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001470 extra = trans_special(&p, end, flags, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001471 if (extra != 0)
1472 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001473 end += extra;
1474 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001475 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001476 break;
1477 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001478 }
1479 // FALLTHROUGH
1480
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001481 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001482 break;
1483 }
1484 }
1485 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001486 MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001487 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001488 *end = NUL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001489 if (*p != NUL) // just in case
1490 ++p;
1491 *arg = p;
1492
1493 return OK;
1494}
1495
1496/*
1497 * Allocate a variable for a 'str''ing' constant.
1498 * Return OK or FAIL.
1499 */
1500 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001501eval_lit_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001502{
1503 char_u *p;
1504 char_u *str;
1505 int reduce = 0;
1506
1507 // Find the end of the string, skipping ''.
1508 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
1509 {
1510 if (*p == '\'')
1511 {
1512 if (p[1] != '\'')
1513 break;
1514 ++reduce;
1515 ++p;
1516 }
1517 }
1518
1519 if (*p != '\'')
1520 {
1521 semsg(_("E115: Missing quote: %s"), *arg);
1522 return FAIL;
1523 }
1524
1525 // If only parsing return after setting "*arg"
1526 if (!evaluate)
1527 {
1528 *arg = p + 1;
1529 return OK;
1530 }
1531
1532 // Copy the string into allocated memory, handling '' to ' reduction.
1533 str = alloc((p - *arg) - reduce);
1534 if (str == NULL)
1535 return FAIL;
1536 rettv->v_type = VAR_STRING;
1537 rettv->vval.v_string = str;
1538
1539 for (p = *arg + 1; *p != NUL; )
1540 {
1541 if (*p == '\'')
1542 {
1543 if (p[1] != '\'')
1544 break;
1545 ++p;
1546 }
1547 MB_COPY_CHAR(p, str);
1548 }
1549 *str = NUL;
1550 *arg = p + 1;
1551
1552 return OK;
1553}
1554
1555/*
1556 * Return a string with the string representation of a variable.
1557 * If the memory is allocated "tofree" is set to it, otherwise NULL.
1558 * "numbuf" is used for a number.
1559 * Puts quotes around strings, so that they can be parsed back by eval().
1560 * May return NULL.
1561 */
1562 char_u *
1563tv2string(
1564 typval_T *tv,
1565 char_u **tofree,
1566 char_u *numbuf,
1567 int copyID)
1568{
1569 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
1570}
1571
1572/*
1573 * Get the value of an environment variable.
1574 * "arg" is pointing to the '$'. It is advanced to after the name.
1575 * If the environment variable was not set, silently assume it is empty.
1576 * Return FAIL if the name is invalid.
1577 */
1578 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001579eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001580{
1581 char_u *string = NULL;
1582 int len;
1583 int cc;
1584 char_u *name;
1585 int mustfree = FALSE;
1586
1587 ++*arg;
1588 name = *arg;
1589 len = get_env_len(arg);
1590 if (evaluate)
1591 {
1592 if (len == 0)
1593 return FAIL; // invalid empty name
1594
1595 cc = name[len];
1596 name[len] = NUL;
1597 // first try vim_getenv(), fast for normal environment vars
1598 string = vim_getenv(name, &mustfree);
1599 if (string != NULL && *string != NUL)
1600 {
1601 if (!mustfree)
1602 string = vim_strsave(string);
1603 }
1604 else
1605 {
1606 if (mustfree)
1607 vim_free(string);
1608
1609 // next try expanding things like $VIM and ${HOME}
1610 string = expand_env_save(name - 1);
1611 if (string != NULL && *string == '$')
1612 VIM_CLEAR(string);
1613 }
1614 name[len] = cc;
1615
1616 rettv->v_type = VAR_STRING;
1617 rettv->vval.v_string = string;
1618 }
1619
1620 return OK;
1621}
1622
1623/*
1624 * Get the lnum from the first argument.
1625 * Also accepts ".", "$", etc., but that only works for the current buffer.
1626 * Returns -1 on error.
1627 */
1628 linenr_T
1629tv_get_lnum(typval_T *argvars)
1630{
Bram Moolenaar9a963372020-12-21 21:58:46 +01001631 linenr_T lnum = -1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001632
Bram Moolenaar56acb092020-08-16 14:48:19 +02001633 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
1634 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02001635 if (lnum <= 0 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001636 {
1637 int fnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001638 pos_T *fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001639
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02001640 // no valid number, try using arg like line()
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001641 if (fp != NULL)
1642 lnum = fp->lnum;
1643 }
1644 return lnum;
1645}
1646
1647/*
1648 * Get the lnum from the first argument.
1649 * Also accepts "$", then "buf" is used.
1650 * Returns 0 on error.
1651 */
1652 linenr_T
1653tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1654{
1655 if (argvars[0].v_type == VAR_STRING
1656 && argvars[0].vval.v_string != NULL
1657 && argvars[0].vval.v_string[0] == '$'
1658 && buf != NULL)
1659 return buf->b_ml.ml_line_count;
1660 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1661}
1662
1663/*
1664 * Get buffer by number or pattern.
1665 */
1666 buf_T *
1667tv_get_buf(typval_T *tv, int curtab_only)
1668{
1669 char_u *name = tv->vval.v_string;
1670 buf_T *buf;
1671
1672 if (tv->v_type == VAR_NUMBER)
1673 return buflist_findnr((int)tv->vval.v_number);
1674 if (tv->v_type != VAR_STRING)
1675 return NULL;
1676 if (name == NULL || *name == NUL)
1677 return curbuf;
1678 if (name[0] == '$' && name[1] == NUL)
1679 return lastbuf;
1680
1681 buf = buflist_find_by_name(name, curtab_only);
1682
1683 // If not found, try expanding the name, like done for bufexists().
1684 if (buf == NULL)
1685 buf = find_buffer(tv);
1686
1687 return buf;
1688}
1689
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02001690/*
1691 * Like tv_get_buf() but give an error message is the type is wrong.
1692 */
1693 buf_T *
1694tv_get_buf_from_arg(typval_T *tv)
1695{
1696 buf_T *buf;
1697
1698 ++emsg_off;
1699 buf = tv_get_buf(tv, FALSE);
1700 --emsg_off;
1701 if (buf == NULL
1702 && tv->v_type != VAR_NUMBER
1703 && tv->v_type != VAR_STRING)
1704 // issue errmsg for type error
1705 (void)tv_get_number(tv);
1706 return buf;
1707}
1708
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001709#endif // FEAT_EVAL