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