blob: a17dbef7efaa8cc40f4af334dc461f195564df9b [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{
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200417 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200418
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
Bram Moolenaar7a2217b2021-06-06 12:33:49 +0200465 if (strict)
466 {
467 emsg(_(e_float_as_string));
468 break;
469 }
470 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
471 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200472#endif
473 case VAR_STRING:
474 if (varp->vval.v_string != NULL)
475 return varp->vval.v_string;
476 return (char_u *)"";
477 case VAR_BOOL:
478 case VAR_SPECIAL:
479 STRCPY(buf, get_var_special_name(varp->vval.v_number));
480 return buf;
481 case VAR_BLOB:
482 emsg(_("E976: using Blob as a String"));
483 break;
484 case VAR_JOB:
485#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200486 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200487 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200488 semsg(_(e_using_invalid_value_as_string_str), "job");
489 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200490 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200491 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200492#endif
493 break;
494 case VAR_CHANNEL:
495#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200496 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200497 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200498 semsg(_(e_using_invalid_value_as_string_str), "channel");
499 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200500 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200501 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200502#endif
503 break;
504 case VAR_UNKNOWN:
505 case VAR_ANY:
506 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200507 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +0200508 semsg(_(e_using_invalid_value_as_string_str),
509 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200510 break;
511 }
512 return NULL;
513}
514
515/*
516 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
517 * string() on Dict, List, etc.
518 */
519 char_u *
520tv_stringify(typval_T *varp, char_u *buf)
521{
522 if (varp->v_type == VAR_LIST
523 || varp->v_type == VAR_DICT
524 || varp->v_type == VAR_BLOB
525 || varp->v_type == VAR_FUNC
526 || varp->v_type == VAR_PARTIAL
527 || varp->v_type == VAR_FLOAT)
528 {
529 typval_T tmp;
530
531 f_string(varp, &tmp);
532 tv_get_string_buf(&tmp, buf);
533 clear_tv(varp);
534 *varp = tmp;
535 return tmp.vval.v_string;
536 }
537 return tv_get_string_buf(varp, buf);
538}
539
540/*
541 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
542 * Also give an error message, using "name" or _("name") when use_gettext is
543 * TRUE.
544 */
545 int
546tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
547{
548 int lock = 0;
549
550 switch (tv->v_type)
551 {
552 case VAR_BLOB:
553 if (tv->vval.v_blob != NULL)
554 lock = tv->vval.v_blob->bv_lock;
555 break;
556 case VAR_LIST:
557 if (tv->vval.v_list != NULL)
558 lock = tv->vval.v_list->lv_lock;
559 break;
560 case VAR_DICT:
561 if (tv->vval.v_dict != NULL)
562 lock = tv->vval.v_dict->dv_lock;
563 break;
564 default:
565 break;
566 }
Bram Moolenaara187c432020-09-16 21:08:28 +0200567 return value_check_lock(tv->v_lock, name, use_gettext)
568 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200569}
570
571/*
572 * Copy the values from typval_T "from" to typval_T "to".
573 * When needed allocates string or increases reference count.
574 * Does not make a copy of a list, blob or dict but copies the reference!
575 * It is OK for "from" and "to" to point to the same item. This is used to
576 * make a copy later.
577 */
578 void
579copy_tv(typval_T *from, typval_T *to)
580{
581 to->v_type = from->v_type;
582 to->v_lock = 0;
583 switch (from->v_type)
584 {
585 case VAR_NUMBER:
586 case VAR_BOOL:
587 case VAR_SPECIAL:
588 to->vval.v_number = from->vval.v_number;
589 break;
590 case VAR_FLOAT:
591#ifdef FEAT_FLOAT
592 to->vval.v_float = from->vval.v_float;
593 break;
594#endif
595 case VAR_JOB:
596#ifdef FEAT_JOB_CHANNEL
597 to->vval.v_job = from->vval.v_job;
598 if (to->vval.v_job != NULL)
599 ++to->vval.v_job->jv_refcount;
600 break;
601#endif
602 case VAR_CHANNEL:
603#ifdef FEAT_JOB_CHANNEL
604 to->vval.v_channel = from->vval.v_channel;
605 if (to->vval.v_channel != NULL)
606 ++to->vval.v_channel->ch_refcount;
607 break;
608#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200609 case VAR_INSTR:
610 to->vval.v_instr = from->vval.v_instr;
611 break;
612
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200613 case VAR_STRING:
614 case VAR_FUNC:
615 if (from->vval.v_string == NULL)
616 to->vval.v_string = NULL;
617 else
618 {
619 to->vval.v_string = vim_strsave(from->vval.v_string);
620 if (from->v_type == VAR_FUNC)
621 func_ref(to->vval.v_string);
622 }
623 break;
624 case VAR_PARTIAL:
625 if (from->vval.v_partial == NULL)
626 to->vval.v_partial = NULL;
627 else
628 {
629 to->vval.v_partial = from->vval.v_partial;
630 ++to->vval.v_partial->pt_refcount;
631 }
632 break;
633 case VAR_BLOB:
634 if (from->vval.v_blob == NULL)
635 to->vval.v_blob = NULL;
636 else
637 {
638 to->vval.v_blob = from->vval.v_blob;
639 ++to->vval.v_blob->bv_refcount;
640 }
641 break;
642 case VAR_LIST:
643 if (from->vval.v_list == NULL)
644 to->vval.v_list = NULL;
645 else
646 {
647 to->vval.v_list = from->vval.v_list;
648 ++to->vval.v_list->lv_refcount;
649 }
650 break;
651 case VAR_DICT:
652 if (from->vval.v_dict == NULL)
653 to->vval.v_dict = NULL;
654 else
655 {
656 to->vval.v_dict = from->vval.v_dict;
657 ++to->vval.v_dict->dv_refcount;
658 }
659 break;
660 case VAR_UNKNOWN:
661 case VAR_ANY:
662 case VAR_VOID:
663 internal_error_no_abort("copy_tv(UNKNOWN)");
664 break;
665 }
666}
667
668/*
669 * Compare "typ1" and "typ2". Put the result in "typ1".
670 */
671 int
672typval_compare(
673 typval_T *typ1, // first operand
674 typval_T *typ2, // second operand
Bram Moolenaar657137c2021-01-09 15:45:23 +0100675 exprtype_T type, // operator
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200676 int ic) // ignore case
677{
678 int i;
679 varnumber_T n1, n2;
680 char_u *s1, *s2;
681 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
682 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
683
684 if (type_is && typ1->v_type != typ2->v_type)
685 {
686 // For "is" a different type always means FALSE, for "notis"
687 // it means TRUE.
688 n1 = (type == EXPR_ISNOT);
689 }
690 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
691 {
692 if (type_is)
693 {
694 n1 = (typ1->v_type == typ2->v_type
695 && typ1->vval.v_blob == typ2->vval.v_blob);
696 if (type == EXPR_ISNOT)
697 n1 = !n1;
698 }
699 else if (typ1->v_type != typ2->v_type
700 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
701 {
702 if (typ1->v_type != typ2->v_type)
703 emsg(_("E977: Can only compare Blob with Blob"));
704 else
705 emsg(_(e_invalblob));
706 clear_tv(typ1);
707 return FAIL;
708 }
709 else
710 {
711 // Compare two Blobs for being equal or unequal.
712 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
713 if (type == EXPR_NEQUAL)
714 n1 = !n1;
715 }
716 }
717 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
718 {
719 if (type_is)
720 {
721 n1 = (typ1->v_type == typ2->v_type
722 && typ1->vval.v_list == typ2->vval.v_list);
723 if (type == EXPR_ISNOT)
724 n1 = !n1;
725 }
726 else if (typ1->v_type != typ2->v_type
727 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
728 {
729 if (typ1->v_type != typ2->v_type)
730 emsg(_("E691: Can only compare List with List"));
731 else
732 emsg(_("E692: Invalid operation for List"));
733 clear_tv(typ1);
734 return FAIL;
735 }
736 else
737 {
738 // Compare two Lists for being equal or unequal.
739 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
740 ic, FALSE);
741 if (type == EXPR_NEQUAL)
742 n1 = !n1;
743 }
744 }
745
746 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
747 {
748 if (type_is)
749 {
750 n1 = (typ1->v_type == typ2->v_type
751 && typ1->vval.v_dict == typ2->vval.v_dict);
752 if (type == EXPR_ISNOT)
753 n1 = !n1;
754 }
755 else if (typ1->v_type != typ2->v_type
756 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
757 {
758 if (typ1->v_type != typ2->v_type)
759 emsg(_("E735: Can only compare Dictionary with Dictionary"));
760 else
761 emsg(_("E736: Invalid operation for Dictionary"));
762 clear_tv(typ1);
763 return FAIL;
764 }
765 else
766 {
767 // Compare two Dictionaries for being equal or unequal.
768 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
769 ic, FALSE);
770 if (type == EXPR_NEQUAL)
771 n1 = !n1;
772 }
773 }
774
775 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
776 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
777 {
778 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
779 && type != EXPR_IS && type != EXPR_ISNOT)
780 {
781 emsg(_("E694: Invalid operation for Funcrefs"));
782 clear_tv(typ1);
783 return FAIL;
784 }
785 if ((typ1->v_type == VAR_PARTIAL
786 && typ1->vval.v_partial == NULL)
787 || (typ2->v_type == VAR_PARTIAL
788 && typ2->vval.v_partial == NULL))
789 // When both partials are NULL, then they are equal.
790 // Otherwise they are not equal.
791 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
792 else if (type_is)
793 {
794 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
795 // strings are considered the same if their value is
796 // the same
797 n1 = tv_equal(typ1, typ2, ic, FALSE);
798 else if (typ1->v_type == VAR_PARTIAL
799 && typ2->v_type == VAR_PARTIAL)
800 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
801 else
802 n1 = FALSE;
803 }
804 else
805 n1 = tv_equal(typ1, typ2, ic, FALSE);
806 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
807 n1 = !n1;
808 }
809
810#ifdef FEAT_FLOAT
811 // If one of the two variables is a float, compare as a float.
812 // When using "=~" or "!~", always compare as string.
813 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
814 && type != EXPR_MATCH && type != EXPR_NOMATCH)
815 {
816 float_T f1, f2;
817
818 f1 = tv_get_float(typ1);
819 f2 = tv_get_float(typ2);
820 n1 = FALSE;
821 switch (type)
822 {
823 case EXPR_IS:
824 case EXPR_EQUAL: n1 = (f1 == f2); break;
825 case EXPR_ISNOT:
826 case EXPR_NEQUAL: n1 = (f1 != f2); break;
827 case EXPR_GREATER: n1 = (f1 > f2); break;
828 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
829 case EXPR_SMALLER: n1 = (f1 < f2); break;
830 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
831 case EXPR_UNKNOWN:
832 case EXPR_MATCH:
833 default: break; // avoid gcc warning
834 }
835 }
836#endif
837
838 // If one of the two variables is a number, compare as a number.
839 // When using "=~" or "!~", always compare as string.
840 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
841 && type != EXPR_MATCH && type != EXPR_NOMATCH)
842 {
843 n1 = tv_get_number(typ1);
844 n2 = tv_get_number(typ2);
845 switch (type)
846 {
847 case EXPR_IS:
848 case EXPR_EQUAL: n1 = (n1 == n2); break;
849 case EXPR_ISNOT:
850 case EXPR_NEQUAL: n1 = (n1 != n2); break;
851 case EXPR_GREATER: n1 = (n1 > n2); break;
852 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
853 case EXPR_SMALLER: n1 = (n1 < n2); break;
854 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
855 case EXPR_UNKNOWN:
856 case EXPR_MATCH:
857 default: break; // avoid gcc warning
858 }
859 }
Bram Moolenaarcff40ff2021-01-09 16:21:37 +0100860 else if (in_vim9script() && (typ1->v_type == VAR_BOOL
861 || typ2->v_type == VAR_BOOL))
862 {
863 if (typ1->v_type != typ2->v_type)
864 {
865 semsg(_(e_cannot_compare_str_with_str),
866 vartype_name(typ1->v_type), vartype_name(typ2->v_type));
867 clear_tv(typ1);
868 return FAIL;
869 }
870 n1 = typ1->vval.v_number;
871 n2 = typ2->vval.v_number;
872 switch (type)
873 {
874 case EXPR_IS:
875 case EXPR_EQUAL: n1 = (n1 == n2); break;
876 case EXPR_ISNOT:
877 case EXPR_NEQUAL: n1 = (n1 != n2); break;
878 default:
879 emsg(_(e_invalid_operation_for_bool));
880 clear_tv(typ1);
881 return FAIL;
882 }
883 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200884 else
885 {
886 s1 = tv_get_string_buf(typ1, buf1);
887 s2 = tv_get_string_buf(typ2, buf2);
888 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
889 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
890 else
891 i = 0;
892 n1 = FALSE;
893 switch (type)
894 {
895 case EXPR_IS:
896 case EXPR_EQUAL: n1 = (i == 0); break;
897 case EXPR_ISNOT:
898 case EXPR_NEQUAL: n1 = (i != 0); break;
899 case EXPR_GREATER: n1 = (i > 0); break;
900 case EXPR_GEQUAL: n1 = (i >= 0); break;
901 case EXPR_SMALLER: n1 = (i < 0); break;
902 case EXPR_SEQUAL: n1 = (i <= 0); break;
903
904 case EXPR_MATCH:
905 case EXPR_NOMATCH:
906 n1 = pattern_match(s2, s1, ic);
907 if (type == EXPR_NOMATCH)
908 n1 = !n1;
909 break;
910
911 default: break; // avoid gcc warning
912 }
913 }
914 clear_tv(typ1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +0200915 if (in_vim9script())
916 {
917 typ1->v_type = VAR_BOOL;
918 typ1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
919 }
920 else
921 {
922 typ1->v_type = VAR_NUMBER;
923 typ1->vval.v_number = n1;
924 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200925
926 return OK;
927}
928
Bram Moolenaar34453202021-01-31 13:08:38 +0100929/*
930 * Convert any type to a string, never give an error.
931 * When "quotes" is TRUE add quotes to a string.
932 * Returns an allocated string.
933 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200934 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +0100935typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200936{
937 char_u *tofree;
938 char_u numbuf[NUMBUFLEN];
939 char_u *ret = NULL;
940
941 if (arg == NULL)
942 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +0100943 if (!quotes && arg->v_type == VAR_STRING)
944 {
945 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
946 : arg->vval.v_string);
947 }
948 else
949 {
950 ret = tv2string(arg, &tofree, numbuf, 0);
951 // Make a copy if we have a value but it's not in allocated memory.
952 if (ret != NULL && tofree == NULL)
953 ret = vim_strsave(ret);
954 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200955 return ret;
956}
957
958/*
959 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
960 * or it refers to a List or Dictionary that is locked.
961 */
962 int
963tv_islocked(typval_T *tv)
964{
965 return (tv->v_lock & VAR_LOCKED)
966 || (tv->v_type == VAR_LIST
967 && tv->vval.v_list != NULL
968 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
969 || (tv->v_type == VAR_DICT
970 && tv->vval.v_dict != NULL
971 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
972}
973
974 static int
975func_equal(
976 typval_T *tv1,
977 typval_T *tv2,
978 int ic) // ignore case
979{
980 char_u *s1, *s2;
981 dict_T *d1, *d2;
982 int a1, a2;
983 int i;
984
985 // empty and NULL function name considered the same
986 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
987 : partial_name(tv1->vval.v_partial);
988 if (s1 != NULL && *s1 == NUL)
989 s1 = NULL;
990 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
991 : partial_name(tv2->vval.v_partial);
992 if (s2 != NULL && *s2 == NUL)
993 s2 = NULL;
994 if (s1 == NULL || s2 == NULL)
995 {
996 if (s1 != s2)
997 return FALSE;
998 }
999 else if (STRCMP(s1, s2) != 0)
1000 return FALSE;
1001
1002 // empty dict and NULL dict is different
1003 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1004 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1005 if (d1 == NULL || d2 == NULL)
1006 {
1007 if (d1 != d2)
1008 return FALSE;
1009 }
1010 else if (!dict_equal(d1, d2, ic, TRUE))
1011 return FALSE;
1012
1013 // empty list and no list considered the same
1014 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1015 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1016 if (a1 != a2)
1017 return FALSE;
1018 for (i = 0; i < a1; ++i)
1019 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1020 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1021 return FALSE;
1022
1023 return TRUE;
1024}
1025
1026/*
1027 * Return TRUE if "tv1" and "tv2" have the same value.
1028 * Compares the items just like "==" would compare them, but strings and
1029 * numbers are different. Floats and numbers are also different.
1030 */
1031 int
1032tv_equal(
1033 typval_T *tv1,
1034 typval_T *tv2,
1035 int ic, // ignore case
1036 int recursive) // TRUE when used recursively
1037{
1038 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1039 char_u *s1, *s2;
1040 static int recursive_cnt = 0; // catch recursive loops
1041 int r;
1042 static int tv_equal_recurse_limit;
1043
1044 // Catch lists and dicts that have an endless loop by limiting
1045 // recursiveness to a limit. We guess they are equal then.
1046 // A fixed limit has the problem of still taking an awful long time.
1047 // Reduce the limit every time running into it. That should work fine for
1048 // deeply linked structures that are not recursively linked and catch
1049 // recursiveness quickly.
1050 if (!recursive)
1051 tv_equal_recurse_limit = 1000;
1052 if (recursive_cnt >= tv_equal_recurse_limit)
1053 {
1054 --tv_equal_recurse_limit;
1055 return TRUE;
1056 }
1057
1058 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1059 // arguments.
1060 if ((tv1->v_type == VAR_FUNC
1061 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1062 && (tv2->v_type == VAR_FUNC
1063 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1064 {
1065 ++recursive_cnt;
1066 r = func_equal(tv1, tv2, ic);
1067 --recursive_cnt;
1068 return r;
1069 }
1070
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001071 if (tv1->v_type != tv2->v_type
1072 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1073 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001074 return FALSE;
1075
1076 switch (tv1->v_type)
1077 {
1078 case VAR_LIST:
1079 ++recursive_cnt;
1080 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1081 --recursive_cnt;
1082 return r;
1083
1084 case VAR_DICT:
1085 ++recursive_cnt;
1086 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1087 --recursive_cnt;
1088 return r;
1089
1090 case VAR_BLOB:
1091 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1092
1093 case VAR_NUMBER:
1094 case VAR_BOOL:
1095 case VAR_SPECIAL:
1096 return tv1->vval.v_number == tv2->vval.v_number;
1097
1098 case VAR_STRING:
1099 s1 = tv_get_string_buf(tv1, buf1);
1100 s2 = tv_get_string_buf(tv2, buf2);
1101 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1102
1103 case VAR_FLOAT:
1104#ifdef FEAT_FLOAT
1105 return tv1->vval.v_float == tv2->vval.v_float;
1106#endif
1107 case VAR_JOB:
1108#ifdef FEAT_JOB_CHANNEL
1109 return tv1->vval.v_job == tv2->vval.v_job;
1110#endif
1111 case VAR_CHANNEL:
1112#ifdef FEAT_JOB_CHANNEL
1113 return tv1->vval.v_channel == tv2->vval.v_channel;
1114#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001115 case VAR_INSTR:
1116 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001117
1118 case VAR_PARTIAL:
1119 return tv1->vval.v_partial == tv2->vval.v_partial;
1120
1121 case VAR_FUNC:
1122 return tv1->vval.v_string == tv2->vval.v_string;
1123
1124 case VAR_UNKNOWN:
1125 case VAR_ANY:
1126 case VAR_VOID:
1127 break;
1128 }
1129
1130 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1131 // does not equal anything, not even itself.
1132 return FALSE;
1133}
1134
1135/*
1136 * Get an option value.
1137 * "arg" points to the '&' or '+' before the option name.
1138 * "arg" is advanced to character after the option name.
1139 * Return OK or FAIL.
1140 */
1141 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001142eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001143 char_u **arg,
1144 typval_T *rettv, // when NULL, only check if option exists
1145 int evaluate)
1146{
1147 char_u *option_end;
1148 long numval;
1149 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001150 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001151 int c;
1152 int working = (**arg == '+'); // has("+option")
1153 int ret = OK;
1154 int opt_flags;
1155
1156 // Isolate the option name and find its value.
1157 option_end = find_option_end(arg, &opt_flags);
1158 if (option_end == NULL)
1159 {
1160 if (rettv != NULL)
1161 semsg(_("E112: Option name missing: %s"), *arg);
1162 return FAIL;
1163 }
1164
1165 if (!evaluate)
1166 {
1167 *arg = option_end;
1168 return OK;
1169 }
1170
1171 c = *option_end;
1172 *option_end = NUL;
1173 opt_type = get_option_value(*arg, &numval,
1174 rettv == NULL ? NULL : &stringval, opt_flags);
1175
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001176 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001177 {
1178 if (rettv != NULL)
1179 semsg(_(e_unknown_option), *arg);
1180 ret = FAIL;
1181 }
1182 else if (rettv != NULL)
1183 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001184 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001185 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001186 {
1187 rettv->v_type = VAR_STRING;
1188 rettv->vval.v_string = NULL;
1189 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001190 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001191 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001192 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1193 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001194 rettv->vval.v_number = 0;
1195 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001196 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001197 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001198 if (in_vim9script() && opt_type == gov_bool)
1199 {
1200 rettv->v_type = VAR_BOOL;
1201 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1202 }
1203 else
1204 {
1205 rettv->v_type = VAR_NUMBER;
1206 rettv->vval.v_number = numval;
1207 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001208 }
1209 else // string option
1210 {
1211 rettv->v_type = VAR_STRING;
1212 rettv->vval.v_string = stringval;
1213 }
1214 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001215 else if (working && (opt_type == gov_hidden_bool
1216 || opt_type == gov_hidden_number
1217 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001218 ret = FAIL;
1219
1220 *option_end = c; // put back for error messages
1221 *arg = option_end;
1222
1223 return ret;
1224}
1225
1226/*
1227 * Allocate a variable for a number constant. Also deals with "0z" for blob.
1228 * Return OK or FAIL.
1229 */
1230 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001231eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001232 char_u **arg,
1233 typval_T *rettv,
1234 int evaluate,
1235 int want_string UNUSED)
1236{
1237 int len;
1238#ifdef FEAT_FLOAT
1239 char_u *p;
1240 int get_float = FALSE;
1241
1242 // We accept a float when the format matches
1243 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
1244 // strict to avoid backwards compatibility problems.
1245 // With script version 2 and later the leading digit can be
1246 // omitted.
1247 // Don't look for a float after the "." operator, so that
1248 // ":let vers = 1.2.3" doesn't fail.
1249 if (**arg == '.')
1250 p = *arg;
1251 else
1252 p = skipdigits(*arg + 1);
1253 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
1254 {
1255 get_float = TRUE;
1256 p = skipdigits(p + 2);
1257 if (*p == 'e' || *p == 'E')
1258 {
1259 ++p;
1260 if (*p == '-' || *p == '+')
1261 ++p;
1262 if (!vim_isdigit(*p))
1263 get_float = FALSE;
1264 else
1265 p = skipdigits(p + 1);
1266 }
1267 if (ASCII_ISALPHA(*p) || *p == '.')
1268 get_float = FALSE;
1269 }
1270 if (get_float)
1271 {
1272 float_T f;
1273
1274 *arg += string2float(*arg, &f);
1275 if (evaluate)
1276 {
1277 rettv->v_type = VAR_FLOAT;
1278 rettv->vval.v_float = f;
1279 }
1280 }
1281 else
1282#endif
1283 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
1284 {
1285 char_u *bp;
1286 blob_T *blob = NULL; // init for gcc
1287
1288 // Blob constant: 0z0123456789abcdef
1289 if (evaluate)
1290 blob = blob_alloc();
1291 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
1292 {
1293 if (!vim_isxdigit(bp[1]))
1294 {
1295 if (blob != NULL)
1296 {
1297 emsg(_("E973: Blob literal should have an even number of hex characters"));
1298 ga_clear(&blob->bv_ga);
1299 VIM_CLEAR(blob);
1300 }
1301 return FAIL;
1302 }
1303 if (blob != NULL)
1304 ga_append(&blob->bv_ga,
1305 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
1306 if (bp[2] == '.' && vim_isxdigit(bp[3]))
1307 ++bp;
1308 }
1309 if (blob != NULL)
1310 rettv_blob_set(rettv, blob);
1311 *arg = bp;
1312 }
1313 else
1314 {
1315 varnumber_T n;
1316
1317 // decimal, hex or octal number
1318 vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4
1319 ? STR2NR_NO_OCT + STR2NR_QUOTE
1320 : STR2NR_ALL, &n, NULL, 0, TRUE);
1321 if (len == 0)
1322 {
1323 semsg(_(e_invexpr2), *arg);
1324 return FAIL;
1325 }
1326 *arg += len;
1327 if (evaluate)
1328 {
1329 rettv->v_type = VAR_NUMBER;
1330 rettv->vval.v_number = n;
1331 }
1332 }
1333 return OK;
1334}
1335
1336/*
1337 * Allocate a variable for a string constant.
1338 * Return OK or FAIL.
1339 */
1340 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001341eval_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001342{
1343 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001344 char_u *end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001345 int extra = 0;
1346 int len;
1347
1348 // Find the end of the string, skipping backslashed characters.
1349 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
1350 {
1351 if (*p == '\\' && p[1] != NUL)
1352 {
1353 ++p;
1354 // A "\<x>" form occupies at least 4 characters, and produces up
1355 // to 21 characters (3 * 6 for the char and 3 for a modifier):
1356 // reserve space for 18 extra.
1357 // Each byte in the char could be encoded as K_SPECIAL K_EXTRA x.
1358 if (*p == '<')
1359 extra += 18;
1360 }
1361 }
1362
1363 if (*p != '"')
1364 {
1365 semsg(_("E114: Missing quote: %s"), *arg);
1366 return FAIL;
1367 }
1368
1369 // If only parsing, set *arg and return here
1370 if (!evaluate)
1371 {
1372 *arg = p + 1;
1373 return OK;
1374 }
1375
1376 // Copy the string into allocated memory, handling backslashed
1377 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001378 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001379 len = (int)(p - *arg + extra);
1380 rettv->vval.v_string = alloc(len);
1381 if (rettv->vval.v_string == NULL)
1382 return FAIL;
1383 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001384
1385 for (p = *arg + 1; *p != NUL && *p != '"'; )
1386 {
1387 if (*p == '\\')
1388 {
1389 switch (*++p)
1390 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001391 case 'b': *end++ = BS; ++p; break;
1392 case 'e': *end++ = ESC; ++p; break;
1393 case 'f': *end++ = FF; ++p; break;
1394 case 'n': *end++ = NL; ++p; break;
1395 case 'r': *end++ = CAR; ++p; break;
1396 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001397
1398 case 'X': // hex: "\x1", "\x12"
1399 case 'x':
1400 case 'u': // Unicode: "\u0023"
1401 case 'U':
1402 if (vim_isxdigit(p[1]))
1403 {
1404 int n, nr;
1405 int c = toupper(*p);
1406
1407 if (c == 'X')
1408 n = 2;
1409 else if (*p == 'u')
1410 n = 4;
1411 else
1412 n = 8;
1413 nr = 0;
1414 while (--n >= 0 && vim_isxdigit(p[1]))
1415 {
1416 ++p;
1417 nr = (nr << 4) + hex2nr(*p);
1418 }
1419 ++p;
1420 // For "\u" store the number according to
1421 // 'encoding'.
1422 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001423 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001424 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001425 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001426 }
1427 break;
1428
1429 // octal: "\1", "\12", "\123"
1430 case '0':
1431 case '1':
1432 case '2':
1433 case '3':
1434 case '4':
1435 case '5':
1436 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001437 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001438 if (*p >= '0' && *p <= '7')
1439 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001440 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001441 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001442 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001443 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001444 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001445 break;
1446
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001447 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001448 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001449 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001450 int flags = FSK_KEYCODE | FSK_IN_STRING;
1451
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001452 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001453 flags |= FSK_SIMPLIFY;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001454 extra = trans_special(&p, end, flags, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001455 if (extra != 0)
1456 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001457 end += extra;
1458 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001459 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001460 break;
1461 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001462 }
1463 // FALLTHROUGH
1464
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001465 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001466 break;
1467 }
1468 }
1469 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001470 MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001471 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001472 *end = NUL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001473 if (*p != NUL) // just in case
1474 ++p;
1475 *arg = p;
1476
1477 return OK;
1478}
1479
1480/*
1481 * Allocate a variable for a 'str''ing' constant.
1482 * Return OK or FAIL.
1483 */
1484 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001485eval_lit_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001486{
1487 char_u *p;
1488 char_u *str;
1489 int reduce = 0;
1490
1491 // Find the end of the string, skipping ''.
1492 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
1493 {
1494 if (*p == '\'')
1495 {
1496 if (p[1] != '\'')
1497 break;
1498 ++reduce;
1499 ++p;
1500 }
1501 }
1502
1503 if (*p != '\'')
1504 {
1505 semsg(_("E115: Missing quote: %s"), *arg);
1506 return FAIL;
1507 }
1508
1509 // If only parsing return after setting "*arg"
1510 if (!evaluate)
1511 {
1512 *arg = p + 1;
1513 return OK;
1514 }
1515
1516 // Copy the string into allocated memory, handling '' to ' reduction.
1517 str = alloc((p - *arg) - reduce);
1518 if (str == NULL)
1519 return FAIL;
1520 rettv->v_type = VAR_STRING;
1521 rettv->vval.v_string = str;
1522
1523 for (p = *arg + 1; *p != NUL; )
1524 {
1525 if (*p == '\'')
1526 {
1527 if (p[1] != '\'')
1528 break;
1529 ++p;
1530 }
1531 MB_COPY_CHAR(p, str);
1532 }
1533 *str = NUL;
1534 *arg = p + 1;
1535
1536 return OK;
1537}
1538
1539/*
1540 * Return a string with the string representation of a variable.
1541 * If the memory is allocated "tofree" is set to it, otherwise NULL.
1542 * "numbuf" is used for a number.
1543 * Puts quotes around strings, so that they can be parsed back by eval().
1544 * May return NULL.
1545 */
1546 char_u *
1547tv2string(
1548 typval_T *tv,
1549 char_u **tofree,
1550 char_u *numbuf,
1551 int copyID)
1552{
1553 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
1554}
1555
1556/*
1557 * Get the value of an environment variable.
1558 * "arg" is pointing to the '$'. It is advanced to after the name.
1559 * If the environment variable was not set, silently assume it is empty.
1560 * Return FAIL if the name is invalid.
1561 */
1562 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001563eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001564{
1565 char_u *string = NULL;
1566 int len;
1567 int cc;
1568 char_u *name;
1569 int mustfree = FALSE;
1570
1571 ++*arg;
1572 name = *arg;
1573 len = get_env_len(arg);
1574 if (evaluate)
1575 {
1576 if (len == 0)
1577 return FAIL; // invalid empty name
1578
1579 cc = name[len];
1580 name[len] = NUL;
1581 // first try vim_getenv(), fast for normal environment vars
1582 string = vim_getenv(name, &mustfree);
1583 if (string != NULL && *string != NUL)
1584 {
1585 if (!mustfree)
1586 string = vim_strsave(string);
1587 }
1588 else
1589 {
1590 if (mustfree)
1591 vim_free(string);
1592
1593 // next try expanding things like $VIM and ${HOME}
1594 string = expand_env_save(name - 1);
1595 if (string != NULL && *string == '$')
1596 VIM_CLEAR(string);
1597 }
1598 name[len] = cc;
1599
1600 rettv->v_type = VAR_STRING;
1601 rettv->vval.v_string = string;
1602 }
1603
1604 return OK;
1605}
1606
1607/*
1608 * Get the lnum from the first argument.
1609 * Also accepts ".", "$", etc., but that only works for the current buffer.
1610 * Returns -1 on error.
1611 */
1612 linenr_T
1613tv_get_lnum(typval_T *argvars)
1614{
Bram Moolenaar9a963372020-12-21 21:58:46 +01001615 linenr_T lnum = -1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001616
Bram Moolenaar56acb092020-08-16 14:48:19 +02001617 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
1618 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02001619 if (lnum <= 0 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001620 {
1621 int fnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001622 pos_T *fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001623
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02001624 // no valid number, try using arg like line()
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001625 if (fp != NULL)
1626 lnum = fp->lnum;
1627 }
1628 return lnum;
1629}
1630
1631/*
1632 * Get the lnum from the first argument.
1633 * Also accepts "$", then "buf" is used.
1634 * Returns 0 on error.
1635 */
1636 linenr_T
1637tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1638{
1639 if (argvars[0].v_type == VAR_STRING
1640 && argvars[0].vval.v_string != NULL
1641 && argvars[0].vval.v_string[0] == '$'
1642 && buf != NULL)
1643 return buf->b_ml.ml_line_count;
1644 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1645}
1646
1647/*
1648 * Get buffer by number or pattern.
1649 */
1650 buf_T *
1651tv_get_buf(typval_T *tv, int curtab_only)
1652{
1653 char_u *name = tv->vval.v_string;
1654 buf_T *buf;
1655
1656 if (tv->v_type == VAR_NUMBER)
1657 return buflist_findnr((int)tv->vval.v_number);
1658 if (tv->v_type != VAR_STRING)
1659 return NULL;
1660 if (name == NULL || *name == NUL)
1661 return curbuf;
1662 if (name[0] == '$' && name[1] == NUL)
1663 return lastbuf;
1664
1665 buf = buflist_find_by_name(name, curtab_only);
1666
1667 // If not found, try expanding the name, like done for bufexists().
1668 if (buf == NULL)
1669 buf = find_buffer(tv);
1670
1671 return buf;
1672}
1673
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02001674/*
1675 * Like tv_get_buf() but give an error message is the type is wrong.
1676 */
1677 buf_T *
1678tv_get_buf_from_arg(typval_T *tv)
1679{
1680 buf_T *buf;
1681
1682 ++emsg_off;
1683 buf = tv_get_buf(tv, FALSE);
1684 --emsg_off;
1685 if (buf == NULL
1686 && tv->v_type != VAR_NUMBER
1687 && tv->v_type != VAR_STRING)
1688 // issue errmsg for type error
1689 (void)tv_get_number(tv);
1690 return buf;
1691}
1692
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001693#endif // FEAT_EVAL