blob: b223318311448742cd7063e6835315302bc7a30a [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;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200241 case VAR_VOID:
242 emsg(_(e_cannot_use_void_value));
243 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200244 case VAR_UNKNOWN:
245 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200246 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200247 internal_error_no_abort("tv_get_number(UNKNOWN)");
248 break;
249 }
250 if (denote == NULL) // useful for values that must be unsigned
251 n = -1;
252 else
253 *denote = TRUE;
254 return n;
255}
256
Bram Moolenaar36967b32020-08-17 21:41:02 +0200257/*
258 * Get the number value of a variable.
259 * If it is a String variable, uses vim_str2nr().
260 * For incompatible types, return 0.
261 * tv_get_number_chk() is similar to tv_get_number(), but informs the
262 * caller of incompatible types: it sets *denote to TRUE if "denote"
263 * is not NULL or returns -1 otherwise.
264 */
265 varnumber_T
266tv_get_number(typval_T *varp)
267{
268 int error = FALSE;
269
270 return tv_get_number_chk(varp, &error); // return 0L on error
271}
272
273 varnumber_T
274tv_get_number_chk(typval_T *varp, int *denote)
275{
276 return tv_get_bool_or_number_chk(varp, denote, FALSE);
277}
278
279/*
280 * Get the boolean value of "varp". This is like tv_get_number_chk(),
Bram Moolenaard70840e2020-08-22 15:06:35 +0200281 * but in Vim9 script accepts Number (0 and 1) and Bool/Special.
Bram Moolenaar36967b32020-08-17 21:41:02 +0200282 */
283 varnumber_T
284tv_get_bool(typval_T *varp)
285{
286 return tv_get_bool_or_number_chk(varp, NULL, TRUE);
Bram Moolenaar36967b32020-08-17 21:41:02 +0200287}
288
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200289/*
290 * Get the boolean value of "varp". This is like tv_get_number_chk(),
291 * but in Vim9 script accepts Number and Bool.
292 */
293 varnumber_T
294tv_get_bool_chk(typval_T *varp, int *denote)
295{
296 return tv_get_bool_or_number_chk(varp, denote, TRUE);
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200297}
298
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200299#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200300 float_T
301tv_get_float(typval_T *varp)
302{
303 switch (varp->v_type)
304 {
305 case VAR_NUMBER:
306 return (float_T)(varp->vval.v_number);
307 case VAR_FLOAT:
308 return varp->vval.v_float;
309 case VAR_FUNC:
310 case VAR_PARTIAL:
311 emsg(_("E891: Using a Funcref as a Float"));
312 break;
313 case VAR_STRING:
314 emsg(_("E892: Using a String as a Float"));
315 break;
316 case VAR_LIST:
317 emsg(_("E893: Using a List as a Float"));
318 break;
319 case VAR_DICT:
320 emsg(_("E894: Using a Dictionary as a Float"));
321 break;
322 case VAR_BOOL:
323 emsg(_("E362: Using a boolean value as a Float"));
324 break;
325 case VAR_SPECIAL:
326 emsg(_("E907: Using a special value as a Float"));
327 break;
328 case VAR_JOB:
329# ifdef FEAT_JOB_CHANNEL
330 emsg(_("E911: Using a Job as a Float"));
331 break;
332# endif
333 case VAR_CHANNEL:
334# ifdef FEAT_JOB_CHANNEL
335 emsg(_("E914: Using a Channel as a Float"));
336 break;
337# endif
338 case VAR_BLOB:
339 emsg(_("E975: Using a Blob as a Float"));
340 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200341 case VAR_VOID:
342 emsg(_(e_cannot_use_void_value));
343 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200344 case VAR_UNKNOWN:
345 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200346 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200347 internal_error_no_abort("tv_get_float(UNKNOWN)");
348 break;
349 }
350 return 0;
351}
352#endif
353
354/*
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100355 * Give an error and return FAIL unless "tv" is a string.
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100356 */
357 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100358check_for_string_arg(typval_T *args, int idx)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100359{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100360 if (args[idx].v_type != VAR_STRING)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100361 {
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100362 if (idx >= 0)
363 semsg(_(e_string_required_for_argument_nr), idx + 1);
Bram Moolenaarf28f2ac2021-03-22 22:21:26 +0100364 else
365 emsg(_(e_stringreq));
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100366 return FAIL;
367 }
368 return OK;
369}
370
371/*
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100372 * Give an error and return FAIL unless "args[idx]" is a non-empty string.
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100373 */
374 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100375check_for_nonempty_string_arg(typval_T *args, int idx)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100376{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100377 if (check_for_string_arg(args, idx) == FAIL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100378 return FAIL;
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100379 if (args[idx].vval.v_string == NULL || *args[idx].vval.v_string == NUL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100380 {
Bram Moolenaare8e30782021-04-10 21:46:05 +0200381 semsg(_(e_non_empty_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100382 return FAIL;
383 }
384 return OK;
385}
386
387/*
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200388 * Give an error and return FAIL unless "tv" is a dict.
389 */
390 int
391check_for_dict_arg(typval_T *args, int idx)
392{
393 if (args[idx].v_type != VAR_DICT)
394 {
395 if (idx >= 0)
396 semsg(_(e_dict_required_for_argument_nr), idx + 1);
397 else
398 emsg(_(e_dictreq));
399 return FAIL;
400 }
401 return OK;
402}
403
404/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200405 * Get the string value of a variable.
406 * If it is a Number variable, the number is converted into a string.
407 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
408 * tv_get_string_buf() uses a given buffer.
409 * If the String variable has never been set, return an empty string.
410 * Never returns NULL;
411 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
412 * NULL on error.
413 */
414 char_u *
415tv_get_string(typval_T *varp)
416{
417 static char_u mybuf[NUMBUFLEN];
418
419 return tv_get_string_buf(varp, mybuf);
420}
421
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100422/*
423 * Like tv_get_string() but don't allow number to string conversion for Vim9.
424 */
425 char_u *
426tv_get_string_strict(typval_T *varp)
427{
428 static char_u mybuf[NUMBUFLEN];
429 char_u *res = tv_get_string_buf_chk_strict(
430 varp, mybuf, in_vim9script());
431
432 return res != NULL ? res : (char_u *)"";
433}
434
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200435 char_u *
436tv_get_string_buf(typval_T *varp, char_u *buf)
437{
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200438 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200439
440 return res != NULL ? res : (char_u *)"";
441}
442
443/*
444 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
445 */
446 char_u *
447tv_get_string_chk(typval_T *varp)
448{
449 static char_u mybuf[NUMBUFLEN];
450
451 return tv_get_string_buf_chk(varp, mybuf);
452}
453
454 char_u *
455tv_get_string_buf_chk(typval_T *varp, char_u *buf)
456{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100457 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
458}
459
460 char_u *
461tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
462{
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200463 switch (varp->v_type)
464 {
465 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100466 if (strict)
467 {
468 emsg(_(e_using_number_as_string));
469 break;
470 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200471 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
472 (varnumber_T)varp->vval.v_number);
473 return buf;
474 case VAR_FUNC:
475 case VAR_PARTIAL:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200476 emsg(_("E729: Using a Funcref as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200477 break;
478 case VAR_LIST:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200479 emsg(_("E730: Using a List as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200480 break;
481 case VAR_DICT:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200482 emsg(_("E731: Using a Dictionary as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200483 break;
484 case VAR_FLOAT:
485#ifdef FEAT_FLOAT
Bram Moolenaar7a2217b2021-06-06 12:33:49 +0200486 if (strict)
487 {
488 emsg(_(e_float_as_string));
489 break;
490 }
491 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
492 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200493#endif
494 case VAR_STRING:
495 if (varp->vval.v_string != NULL)
496 return varp->vval.v_string;
497 return (char_u *)"";
498 case VAR_BOOL:
499 case VAR_SPECIAL:
500 STRCPY(buf, get_var_special_name(varp->vval.v_number));
501 return buf;
502 case VAR_BLOB:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200503 emsg(_("E976: Using a Blob as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200504 break;
505 case VAR_JOB:
506#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200507 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200508 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200509 semsg(_(e_using_invalid_value_as_string_str), "job");
510 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200511 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200512 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200513#endif
514 break;
515 case VAR_CHANNEL:
516#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200517 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200518 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200519 semsg(_(e_using_invalid_value_as_string_str), "channel");
520 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200521 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200522 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200523#endif
524 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200525 case VAR_VOID:
526 emsg(_(e_cannot_use_void_value));
527 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200528 case VAR_UNKNOWN:
529 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200530 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +0200531 semsg(_(e_using_invalid_value_as_string_str),
532 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200533 break;
534 }
535 return NULL;
536}
537
538/*
539 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
540 * string() on Dict, List, etc.
541 */
542 char_u *
543tv_stringify(typval_T *varp, char_u *buf)
544{
545 if (varp->v_type == VAR_LIST
546 || varp->v_type == VAR_DICT
547 || varp->v_type == VAR_BLOB
548 || varp->v_type == VAR_FUNC
549 || varp->v_type == VAR_PARTIAL
550 || varp->v_type == VAR_FLOAT)
551 {
552 typval_T tmp;
553
554 f_string(varp, &tmp);
555 tv_get_string_buf(&tmp, buf);
556 clear_tv(varp);
557 *varp = tmp;
558 return tmp.vval.v_string;
559 }
560 return tv_get_string_buf(varp, buf);
561}
562
563/*
564 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
565 * Also give an error message, using "name" or _("name") when use_gettext is
566 * TRUE.
567 */
568 int
569tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
570{
571 int lock = 0;
572
573 switch (tv->v_type)
574 {
575 case VAR_BLOB:
576 if (tv->vval.v_blob != NULL)
577 lock = tv->vval.v_blob->bv_lock;
578 break;
579 case VAR_LIST:
580 if (tv->vval.v_list != NULL)
581 lock = tv->vval.v_list->lv_lock;
582 break;
583 case VAR_DICT:
584 if (tv->vval.v_dict != NULL)
585 lock = tv->vval.v_dict->dv_lock;
586 break;
587 default:
588 break;
589 }
Bram Moolenaara187c432020-09-16 21:08:28 +0200590 return value_check_lock(tv->v_lock, name, use_gettext)
591 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200592}
593
594/*
595 * Copy the values from typval_T "from" to typval_T "to".
596 * When needed allocates string or increases reference count.
597 * Does not make a copy of a list, blob or dict but copies the reference!
598 * It is OK for "from" and "to" to point to the same item. This is used to
599 * make a copy later.
600 */
601 void
602copy_tv(typval_T *from, typval_T *to)
603{
604 to->v_type = from->v_type;
605 to->v_lock = 0;
606 switch (from->v_type)
607 {
608 case VAR_NUMBER:
609 case VAR_BOOL:
610 case VAR_SPECIAL:
611 to->vval.v_number = from->vval.v_number;
612 break;
613 case VAR_FLOAT:
614#ifdef FEAT_FLOAT
615 to->vval.v_float = from->vval.v_float;
616 break;
617#endif
618 case VAR_JOB:
619#ifdef FEAT_JOB_CHANNEL
620 to->vval.v_job = from->vval.v_job;
621 if (to->vval.v_job != NULL)
622 ++to->vval.v_job->jv_refcount;
623 break;
624#endif
625 case VAR_CHANNEL:
626#ifdef FEAT_JOB_CHANNEL
627 to->vval.v_channel = from->vval.v_channel;
628 if (to->vval.v_channel != NULL)
629 ++to->vval.v_channel->ch_refcount;
630 break;
631#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200632 case VAR_INSTR:
633 to->vval.v_instr = from->vval.v_instr;
634 break;
635
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200636 case VAR_STRING:
637 case VAR_FUNC:
638 if (from->vval.v_string == NULL)
639 to->vval.v_string = NULL;
640 else
641 {
642 to->vval.v_string = vim_strsave(from->vval.v_string);
643 if (from->v_type == VAR_FUNC)
644 func_ref(to->vval.v_string);
645 }
646 break;
647 case VAR_PARTIAL:
648 if (from->vval.v_partial == NULL)
649 to->vval.v_partial = NULL;
650 else
651 {
652 to->vval.v_partial = from->vval.v_partial;
653 ++to->vval.v_partial->pt_refcount;
654 }
655 break;
656 case VAR_BLOB:
657 if (from->vval.v_blob == NULL)
658 to->vval.v_blob = NULL;
659 else
660 {
661 to->vval.v_blob = from->vval.v_blob;
662 ++to->vval.v_blob->bv_refcount;
663 }
664 break;
665 case VAR_LIST:
666 if (from->vval.v_list == NULL)
667 to->vval.v_list = NULL;
668 else
669 {
670 to->vval.v_list = from->vval.v_list;
671 ++to->vval.v_list->lv_refcount;
672 }
673 break;
674 case VAR_DICT:
675 if (from->vval.v_dict == NULL)
676 to->vval.v_dict = NULL;
677 else
678 {
679 to->vval.v_dict = from->vval.v_dict;
680 ++to->vval.v_dict->dv_refcount;
681 }
682 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +0200683 case VAR_VOID:
684 emsg(_(e_cannot_use_void_value));
685 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200686 case VAR_UNKNOWN:
687 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200688 internal_error_no_abort("copy_tv(UNKNOWN)");
689 break;
690 }
691}
692
693/*
694 * Compare "typ1" and "typ2". Put the result in "typ1".
695 */
696 int
697typval_compare(
698 typval_T *typ1, // first operand
699 typval_T *typ2, // second operand
Bram Moolenaar657137c2021-01-09 15:45:23 +0100700 exprtype_T type, // operator
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200701 int ic) // ignore case
702{
703 int i;
704 varnumber_T n1, n2;
705 char_u *s1, *s2;
706 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
707 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
708
709 if (type_is && typ1->v_type != typ2->v_type)
710 {
711 // For "is" a different type always means FALSE, for "notis"
712 // it means TRUE.
713 n1 = (type == EXPR_ISNOT);
714 }
715 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
716 {
717 if (type_is)
718 {
719 n1 = (typ1->v_type == typ2->v_type
720 && typ1->vval.v_blob == typ2->vval.v_blob);
721 if (type == EXPR_ISNOT)
722 n1 = !n1;
723 }
724 else if (typ1->v_type != typ2->v_type
725 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
726 {
727 if (typ1->v_type != typ2->v_type)
728 emsg(_("E977: Can only compare Blob with Blob"));
729 else
730 emsg(_(e_invalblob));
731 clear_tv(typ1);
732 return FAIL;
733 }
734 else
735 {
736 // Compare two Blobs for being equal or unequal.
737 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
738 if (type == EXPR_NEQUAL)
739 n1 = !n1;
740 }
741 }
742 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
743 {
744 if (type_is)
745 {
746 n1 = (typ1->v_type == typ2->v_type
747 && typ1->vval.v_list == typ2->vval.v_list);
748 if (type == EXPR_ISNOT)
749 n1 = !n1;
750 }
751 else if (typ1->v_type != typ2->v_type
752 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
753 {
754 if (typ1->v_type != typ2->v_type)
755 emsg(_("E691: Can only compare List with List"));
756 else
757 emsg(_("E692: Invalid operation for List"));
758 clear_tv(typ1);
759 return FAIL;
760 }
761 else
762 {
763 // Compare two Lists for being equal or unequal.
764 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
765 ic, FALSE);
766 if (type == EXPR_NEQUAL)
767 n1 = !n1;
768 }
769 }
770
771 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
772 {
773 if (type_is)
774 {
775 n1 = (typ1->v_type == typ2->v_type
776 && typ1->vval.v_dict == typ2->vval.v_dict);
777 if (type == EXPR_ISNOT)
778 n1 = !n1;
779 }
780 else if (typ1->v_type != typ2->v_type
781 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
782 {
783 if (typ1->v_type != typ2->v_type)
784 emsg(_("E735: Can only compare Dictionary with Dictionary"));
785 else
786 emsg(_("E736: Invalid operation for Dictionary"));
787 clear_tv(typ1);
788 return FAIL;
789 }
790 else
791 {
792 // Compare two Dictionaries for being equal or unequal.
793 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
794 ic, FALSE);
795 if (type == EXPR_NEQUAL)
796 n1 = !n1;
797 }
798 }
799
800 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
801 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
802 {
803 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
804 && type != EXPR_IS && type != EXPR_ISNOT)
805 {
806 emsg(_("E694: Invalid operation for Funcrefs"));
807 clear_tv(typ1);
808 return FAIL;
809 }
810 if ((typ1->v_type == VAR_PARTIAL
811 && typ1->vval.v_partial == NULL)
812 || (typ2->v_type == VAR_PARTIAL
813 && typ2->vval.v_partial == NULL))
814 // When both partials are NULL, then they are equal.
815 // Otherwise they are not equal.
816 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
817 else if (type_is)
818 {
819 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
820 // strings are considered the same if their value is
821 // the same
822 n1 = tv_equal(typ1, typ2, ic, FALSE);
823 else if (typ1->v_type == VAR_PARTIAL
824 && typ2->v_type == VAR_PARTIAL)
825 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
826 else
827 n1 = FALSE;
828 }
829 else
830 n1 = tv_equal(typ1, typ2, ic, FALSE);
831 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
832 n1 = !n1;
833 }
834
835#ifdef FEAT_FLOAT
836 // If one of the two variables is a float, compare as a float.
837 // When using "=~" or "!~", always compare as string.
838 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
839 && type != EXPR_MATCH && type != EXPR_NOMATCH)
840 {
841 float_T f1, f2;
842
843 f1 = tv_get_float(typ1);
844 f2 = tv_get_float(typ2);
845 n1 = FALSE;
846 switch (type)
847 {
848 case EXPR_IS:
849 case EXPR_EQUAL: n1 = (f1 == f2); break;
850 case EXPR_ISNOT:
851 case EXPR_NEQUAL: n1 = (f1 != f2); break;
852 case EXPR_GREATER: n1 = (f1 > f2); break;
853 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
854 case EXPR_SMALLER: n1 = (f1 < f2); break;
855 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
856 case EXPR_UNKNOWN:
857 case EXPR_MATCH:
858 default: break; // avoid gcc warning
859 }
860 }
861#endif
862
863 // If one of the two variables is a number, compare as a number.
864 // When using "=~" or "!~", always compare as string.
865 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
866 && type != EXPR_MATCH && type != EXPR_NOMATCH)
867 {
868 n1 = tv_get_number(typ1);
869 n2 = tv_get_number(typ2);
870 switch (type)
871 {
872 case EXPR_IS:
873 case EXPR_EQUAL: n1 = (n1 == n2); break;
874 case EXPR_ISNOT:
875 case EXPR_NEQUAL: n1 = (n1 != n2); break;
876 case EXPR_GREATER: n1 = (n1 > n2); break;
877 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
878 case EXPR_SMALLER: n1 = (n1 < n2); break;
879 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
880 case EXPR_UNKNOWN:
881 case EXPR_MATCH:
882 default: break; // avoid gcc warning
883 }
884 }
Bram Moolenaarcff40ff2021-01-09 16:21:37 +0100885 else if (in_vim9script() && (typ1->v_type == VAR_BOOL
886 || typ2->v_type == VAR_BOOL))
887 {
888 if (typ1->v_type != typ2->v_type)
889 {
890 semsg(_(e_cannot_compare_str_with_str),
891 vartype_name(typ1->v_type), vartype_name(typ2->v_type));
892 clear_tv(typ1);
893 return FAIL;
894 }
895 n1 = typ1->vval.v_number;
896 n2 = typ2->vval.v_number;
897 switch (type)
898 {
899 case EXPR_IS:
900 case EXPR_EQUAL: n1 = (n1 == n2); break;
901 case EXPR_ISNOT:
902 case EXPR_NEQUAL: n1 = (n1 != n2); break;
903 default:
904 emsg(_(e_invalid_operation_for_bool));
905 clear_tv(typ1);
906 return FAIL;
907 }
908 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200909 else
910 {
911 s1 = tv_get_string_buf(typ1, buf1);
912 s2 = tv_get_string_buf(typ2, buf2);
913 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
914 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
915 else
916 i = 0;
917 n1 = FALSE;
918 switch (type)
919 {
920 case EXPR_IS:
921 case EXPR_EQUAL: n1 = (i == 0); break;
922 case EXPR_ISNOT:
923 case EXPR_NEQUAL: n1 = (i != 0); break;
924 case EXPR_GREATER: n1 = (i > 0); break;
925 case EXPR_GEQUAL: n1 = (i >= 0); break;
926 case EXPR_SMALLER: n1 = (i < 0); break;
927 case EXPR_SEQUAL: n1 = (i <= 0); break;
928
929 case EXPR_MATCH:
930 case EXPR_NOMATCH:
931 n1 = pattern_match(s2, s1, ic);
932 if (type == EXPR_NOMATCH)
933 n1 = !n1;
934 break;
935
936 default: break; // avoid gcc warning
937 }
938 }
939 clear_tv(typ1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +0200940 if (in_vim9script())
941 {
942 typ1->v_type = VAR_BOOL;
943 typ1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
944 }
945 else
946 {
947 typ1->v_type = VAR_NUMBER;
948 typ1->vval.v_number = n1;
949 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200950
951 return OK;
952}
953
Bram Moolenaar34453202021-01-31 13:08:38 +0100954/*
955 * Convert any type to a string, never give an error.
956 * When "quotes" is TRUE add quotes to a string.
957 * Returns an allocated string.
958 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200959 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +0100960typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200961{
962 char_u *tofree;
963 char_u numbuf[NUMBUFLEN];
964 char_u *ret = NULL;
965
966 if (arg == NULL)
967 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +0100968 if (!quotes && arg->v_type == VAR_STRING)
969 {
970 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
971 : arg->vval.v_string);
972 }
973 else
974 {
975 ret = tv2string(arg, &tofree, numbuf, 0);
976 // Make a copy if we have a value but it's not in allocated memory.
977 if (ret != NULL && tofree == NULL)
978 ret = vim_strsave(ret);
979 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200980 return ret;
981}
982
983/*
984 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
985 * or it refers to a List or Dictionary that is locked.
986 */
987 int
988tv_islocked(typval_T *tv)
989{
990 return (tv->v_lock & VAR_LOCKED)
991 || (tv->v_type == VAR_LIST
992 && tv->vval.v_list != NULL
993 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
994 || (tv->v_type == VAR_DICT
995 && tv->vval.v_dict != NULL
996 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
997}
998
999 static int
1000func_equal(
1001 typval_T *tv1,
1002 typval_T *tv2,
1003 int ic) // ignore case
1004{
1005 char_u *s1, *s2;
1006 dict_T *d1, *d2;
1007 int a1, a2;
1008 int i;
1009
1010 // empty and NULL function name considered the same
1011 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1012 : partial_name(tv1->vval.v_partial);
1013 if (s1 != NULL && *s1 == NUL)
1014 s1 = NULL;
1015 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1016 : partial_name(tv2->vval.v_partial);
1017 if (s2 != NULL && *s2 == NUL)
1018 s2 = NULL;
1019 if (s1 == NULL || s2 == NULL)
1020 {
1021 if (s1 != s2)
1022 return FALSE;
1023 }
1024 else if (STRCMP(s1, s2) != 0)
1025 return FALSE;
1026
1027 // empty dict and NULL dict is different
1028 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1029 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1030 if (d1 == NULL || d2 == NULL)
1031 {
1032 if (d1 != d2)
1033 return FALSE;
1034 }
1035 else if (!dict_equal(d1, d2, ic, TRUE))
1036 return FALSE;
1037
1038 // empty list and no list considered the same
1039 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1040 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1041 if (a1 != a2)
1042 return FALSE;
1043 for (i = 0; i < a1; ++i)
1044 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1045 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1046 return FALSE;
1047
1048 return TRUE;
1049}
1050
1051/*
1052 * Return TRUE if "tv1" and "tv2" have the same value.
1053 * Compares the items just like "==" would compare them, but strings and
1054 * numbers are different. Floats and numbers are also different.
1055 */
1056 int
1057tv_equal(
1058 typval_T *tv1,
1059 typval_T *tv2,
1060 int ic, // ignore case
1061 int recursive) // TRUE when used recursively
1062{
1063 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1064 char_u *s1, *s2;
1065 static int recursive_cnt = 0; // catch recursive loops
1066 int r;
1067 static int tv_equal_recurse_limit;
1068
1069 // Catch lists and dicts that have an endless loop by limiting
1070 // recursiveness to a limit. We guess they are equal then.
1071 // A fixed limit has the problem of still taking an awful long time.
1072 // Reduce the limit every time running into it. That should work fine for
1073 // deeply linked structures that are not recursively linked and catch
1074 // recursiveness quickly.
1075 if (!recursive)
1076 tv_equal_recurse_limit = 1000;
1077 if (recursive_cnt >= tv_equal_recurse_limit)
1078 {
1079 --tv_equal_recurse_limit;
1080 return TRUE;
1081 }
1082
1083 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1084 // arguments.
1085 if ((tv1->v_type == VAR_FUNC
1086 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1087 && (tv2->v_type == VAR_FUNC
1088 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1089 {
1090 ++recursive_cnt;
1091 r = func_equal(tv1, tv2, ic);
1092 --recursive_cnt;
1093 return r;
1094 }
1095
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001096 if (tv1->v_type != tv2->v_type
1097 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1098 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001099 return FALSE;
1100
1101 switch (tv1->v_type)
1102 {
1103 case VAR_LIST:
1104 ++recursive_cnt;
1105 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1106 --recursive_cnt;
1107 return r;
1108
1109 case VAR_DICT:
1110 ++recursive_cnt;
1111 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1112 --recursive_cnt;
1113 return r;
1114
1115 case VAR_BLOB:
1116 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1117
1118 case VAR_NUMBER:
1119 case VAR_BOOL:
1120 case VAR_SPECIAL:
1121 return tv1->vval.v_number == tv2->vval.v_number;
1122
1123 case VAR_STRING:
1124 s1 = tv_get_string_buf(tv1, buf1);
1125 s2 = tv_get_string_buf(tv2, buf2);
1126 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1127
1128 case VAR_FLOAT:
1129#ifdef FEAT_FLOAT
1130 return tv1->vval.v_float == tv2->vval.v_float;
1131#endif
1132 case VAR_JOB:
1133#ifdef FEAT_JOB_CHANNEL
1134 return tv1->vval.v_job == tv2->vval.v_job;
1135#endif
1136 case VAR_CHANNEL:
1137#ifdef FEAT_JOB_CHANNEL
1138 return tv1->vval.v_channel == tv2->vval.v_channel;
1139#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001140 case VAR_INSTR:
1141 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001142
1143 case VAR_PARTIAL:
1144 return tv1->vval.v_partial == tv2->vval.v_partial;
1145
1146 case VAR_FUNC:
1147 return tv1->vval.v_string == tv2->vval.v_string;
1148
1149 case VAR_UNKNOWN:
1150 case VAR_ANY:
1151 case VAR_VOID:
1152 break;
1153 }
1154
1155 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1156 // does not equal anything, not even itself.
1157 return FALSE;
1158}
1159
1160/*
1161 * Get an option value.
1162 * "arg" points to the '&' or '+' before the option name.
1163 * "arg" is advanced to character after the option name.
1164 * Return OK or FAIL.
1165 */
1166 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001167eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001168 char_u **arg,
1169 typval_T *rettv, // when NULL, only check if option exists
1170 int evaluate)
1171{
1172 char_u *option_end;
1173 long numval;
1174 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001175 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001176 int c;
1177 int working = (**arg == '+'); // has("+option")
1178 int ret = OK;
1179 int opt_flags;
1180
1181 // Isolate the option name and find its value.
1182 option_end = find_option_end(arg, &opt_flags);
1183 if (option_end == NULL)
1184 {
1185 if (rettv != NULL)
1186 semsg(_("E112: Option name missing: %s"), *arg);
1187 return FAIL;
1188 }
1189
1190 if (!evaluate)
1191 {
1192 *arg = option_end;
1193 return OK;
1194 }
1195
1196 c = *option_end;
1197 *option_end = NUL;
1198 opt_type = get_option_value(*arg, &numval,
1199 rettv == NULL ? NULL : &stringval, opt_flags);
1200
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001201 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001202 {
1203 if (rettv != NULL)
1204 semsg(_(e_unknown_option), *arg);
1205 ret = FAIL;
1206 }
1207 else if (rettv != NULL)
1208 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001209 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001210 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001211 {
1212 rettv->v_type = VAR_STRING;
1213 rettv->vval.v_string = NULL;
1214 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001215 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001216 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001217 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1218 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001219 rettv->vval.v_number = 0;
1220 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001221 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001222 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001223 if (in_vim9script() && opt_type == gov_bool)
1224 {
1225 rettv->v_type = VAR_BOOL;
1226 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1227 }
1228 else
1229 {
1230 rettv->v_type = VAR_NUMBER;
1231 rettv->vval.v_number = numval;
1232 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001233 }
1234 else // string option
1235 {
1236 rettv->v_type = VAR_STRING;
1237 rettv->vval.v_string = stringval;
1238 }
1239 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001240 else if (working && (opt_type == gov_hidden_bool
1241 || opt_type == gov_hidden_number
1242 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001243 ret = FAIL;
1244
1245 *option_end = c; // put back for error messages
1246 *arg = option_end;
1247
1248 return ret;
1249}
1250
1251/*
1252 * Allocate a variable for a number constant. Also deals with "0z" for blob.
1253 * Return OK or FAIL.
1254 */
1255 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001256eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001257 char_u **arg,
1258 typval_T *rettv,
1259 int evaluate,
1260 int want_string UNUSED)
1261{
1262 int len;
1263#ifdef FEAT_FLOAT
1264 char_u *p;
1265 int get_float = FALSE;
1266
1267 // We accept a float when the format matches
1268 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
1269 // strict to avoid backwards compatibility problems.
1270 // With script version 2 and later the leading digit can be
1271 // omitted.
1272 // Don't look for a float after the "." operator, so that
1273 // ":let vers = 1.2.3" doesn't fail.
1274 if (**arg == '.')
1275 p = *arg;
1276 else
1277 p = skipdigits(*arg + 1);
1278 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
1279 {
1280 get_float = TRUE;
1281 p = skipdigits(p + 2);
1282 if (*p == 'e' || *p == 'E')
1283 {
1284 ++p;
1285 if (*p == '-' || *p == '+')
1286 ++p;
1287 if (!vim_isdigit(*p))
1288 get_float = FALSE;
1289 else
1290 p = skipdigits(p + 1);
1291 }
1292 if (ASCII_ISALPHA(*p) || *p == '.')
1293 get_float = FALSE;
1294 }
1295 if (get_float)
1296 {
1297 float_T f;
1298
1299 *arg += string2float(*arg, &f);
1300 if (evaluate)
1301 {
1302 rettv->v_type = VAR_FLOAT;
1303 rettv->vval.v_float = f;
1304 }
1305 }
1306 else
1307#endif
1308 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
1309 {
1310 char_u *bp;
1311 blob_T *blob = NULL; // init for gcc
1312
1313 // Blob constant: 0z0123456789abcdef
1314 if (evaluate)
1315 blob = blob_alloc();
1316 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
1317 {
1318 if (!vim_isxdigit(bp[1]))
1319 {
1320 if (blob != NULL)
1321 {
1322 emsg(_("E973: Blob literal should have an even number of hex characters"));
1323 ga_clear(&blob->bv_ga);
1324 VIM_CLEAR(blob);
1325 }
1326 return FAIL;
1327 }
1328 if (blob != NULL)
1329 ga_append(&blob->bv_ga,
1330 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
1331 if (bp[2] == '.' && vim_isxdigit(bp[3]))
1332 ++bp;
1333 }
1334 if (blob != NULL)
1335 rettv_blob_set(rettv, blob);
1336 *arg = bp;
1337 }
1338 else
1339 {
1340 varnumber_T n;
1341
1342 // decimal, hex or octal number
1343 vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4
1344 ? STR2NR_NO_OCT + STR2NR_QUOTE
1345 : STR2NR_ALL, &n, NULL, 0, TRUE);
1346 if (len == 0)
1347 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001348 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001349 return FAIL;
1350 }
1351 *arg += len;
1352 if (evaluate)
1353 {
1354 rettv->v_type = VAR_NUMBER;
1355 rettv->vval.v_number = n;
1356 }
1357 }
1358 return OK;
1359}
1360
1361/*
1362 * Allocate a variable for a string constant.
1363 * Return OK or FAIL.
1364 */
1365 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001366eval_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001367{
1368 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001369 char_u *end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001370 int extra = 0;
1371 int len;
1372
1373 // Find the end of the string, skipping backslashed characters.
1374 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
1375 {
1376 if (*p == '\\' && p[1] != NUL)
1377 {
1378 ++p;
1379 // A "\<x>" form occupies at least 4 characters, and produces up
1380 // to 21 characters (3 * 6 for the char and 3 for a modifier):
1381 // reserve space for 18 extra.
1382 // Each byte in the char could be encoded as K_SPECIAL K_EXTRA x.
1383 if (*p == '<')
1384 extra += 18;
1385 }
1386 }
1387
1388 if (*p != '"')
1389 {
1390 semsg(_("E114: Missing quote: %s"), *arg);
1391 return FAIL;
1392 }
1393
1394 // If only parsing, set *arg and return here
1395 if (!evaluate)
1396 {
1397 *arg = p + 1;
1398 return OK;
1399 }
1400
1401 // Copy the string into allocated memory, handling backslashed
1402 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001403 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001404 len = (int)(p - *arg + extra);
1405 rettv->vval.v_string = alloc(len);
1406 if (rettv->vval.v_string == NULL)
1407 return FAIL;
1408 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001409
1410 for (p = *arg + 1; *p != NUL && *p != '"'; )
1411 {
1412 if (*p == '\\')
1413 {
1414 switch (*++p)
1415 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001416 case 'b': *end++ = BS; ++p; break;
1417 case 'e': *end++ = ESC; ++p; break;
1418 case 'f': *end++ = FF; ++p; break;
1419 case 'n': *end++ = NL; ++p; break;
1420 case 'r': *end++ = CAR; ++p; break;
1421 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001422
1423 case 'X': // hex: "\x1", "\x12"
1424 case 'x':
1425 case 'u': // Unicode: "\u0023"
1426 case 'U':
1427 if (vim_isxdigit(p[1]))
1428 {
1429 int n, nr;
1430 int c = toupper(*p);
1431
1432 if (c == 'X')
1433 n = 2;
1434 else if (*p == 'u')
1435 n = 4;
1436 else
1437 n = 8;
1438 nr = 0;
1439 while (--n >= 0 && vim_isxdigit(p[1]))
1440 {
1441 ++p;
1442 nr = (nr << 4) + hex2nr(*p);
1443 }
1444 ++p;
1445 // For "\u" store the number according to
1446 // 'encoding'.
1447 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001448 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001449 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001450 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001451 }
1452 break;
1453
1454 // octal: "\1", "\12", "\123"
1455 case '0':
1456 case '1':
1457 case '2':
1458 case '3':
1459 case '4':
1460 case '5':
1461 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001462 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001463 if (*p >= '0' && *p <= '7')
1464 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001465 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001466 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001467 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001468 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001469 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001470 break;
1471
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001472 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001473 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001474 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001475 int flags = FSK_KEYCODE | FSK_IN_STRING;
1476
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001477 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001478 flags |= FSK_SIMPLIFY;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001479 extra = trans_special(&p, end, flags, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001480 if (extra != 0)
1481 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001482 end += extra;
1483 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001484 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001485 break;
1486 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001487 }
1488 // FALLTHROUGH
1489
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001490 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001491 break;
1492 }
1493 }
1494 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001495 MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001496 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001497 *end = NUL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001498 if (*p != NUL) // just in case
1499 ++p;
1500 *arg = p;
1501
1502 return OK;
1503}
1504
1505/*
1506 * Allocate a variable for a 'str''ing' constant.
1507 * Return OK or FAIL.
1508 */
1509 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001510eval_lit_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001511{
1512 char_u *p;
1513 char_u *str;
1514 int reduce = 0;
1515
1516 // Find the end of the string, skipping ''.
1517 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
1518 {
1519 if (*p == '\'')
1520 {
1521 if (p[1] != '\'')
1522 break;
1523 ++reduce;
1524 ++p;
1525 }
1526 }
1527
1528 if (*p != '\'')
1529 {
1530 semsg(_("E115: Missing quote: %s"), *arg);
1531 return FAIL;
1532 }
1533
1534 // If only parsing return after setting "*arg"
1535 if (!evaluate)
1536 {
1537 *arg = p + 1;
1538 return OK;
1539 }
1540
1541 // Copy the string into allocated memory, handling '' to ' reduction.
1542 str = alloc((p - *arg) - reduce);
1543 if (str == NULL)
1544 return FAIL;
1545 rettv->v_type = VAR_STRING;
1546 rettv->vval.v_string = str;
1547
1548 for (p = *arg + 1; *p != NUL; )
1549 {
1550 if (*p == '\'')
1551 {
1552 if (p[1] != '\'')
1553 break;
1554 ++p;
1555 }
1556 MB_COPY_CHAR(p, str);
1557 }
1558 *str = NUL;
1559 *arg = p + 1;
1560
1561 return OK;
1562}
1563
1564/*
1565 * Return a string with the string representation of a variable.
1566 * If the memory is allocated "tofree" is set to it, otherwise NULL.
1567 * "numbuf" is used for a number.
1568 * Puts quotes around strings, so that they can be parsed back by eval().
1569 * May return NULL.
1570 */
1571 char_u *
1572tv2string(
1573 typval_T *tv,
1574 char_u **tofree,
1575 char_u *numbuf,
1576 int copyID)
1577{
1578 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
1579}
1580
1581/*
1582 * Get the value of an environment variable.
1583 * "arg" is pointing to the '$'. It is advanced to after the name.
1584 * If the environment variable was not set, silently assume it is empty.
1585 * Return FAIL if the name is invalid.
1586 */
1587 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001588eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001589{
1590 char_u *string = NULL;
1591 int len;
1592 int cc;
1593 char_u *name;
1594 int mustfree = FALSE;
1595
1596 ++*arg;
1597 name = *arg;
1598 len = get_env_len(arg);
1599 if (evaluate)
1600 {
1601 if (len == 0)
1602 return FAIL; // invalid empty name
1603
1604 cc = name[len];
1605 name[len] = NUL;
1606 // first try vim_getenv(), fast for normal environment vars
1607 string = vim_getenv(name, &mustfree);
1608 if (string != NULL && *string != NUL)
1609 {
1610 if (!mustfree)
1611 string = vim_strsave(string);
1612 }
1613 else
1614 {
1615 if (mustfree)
1616 vim_free(string);
1617
1618 // next try expanding things like $VIM and ${HOME}
1619 string = expand_env_save(name - 1);
1620 if (string != NULL && *string == '$')
1621 VIM_CLEAR(string);
1622 }
1623 name[len] = cc;
1624
1625 rettv->v_type = VAR_STRING;
1626 rettv->vval.v_string = string;
1627 }
1628
1629 return OK;
1630}
1631
1632/*
1633 * Get the lnum from the first argument.
1634 * Also accepts ".", "$", etc., but that only works for the current buffer.
1635 * Returns -1 on error.
1636 */
1637 linenr_T
1638tv_get_lnum(typval_T *argvars)
1639{
Bram Moolenaar9a963372020-12-21 21:58:46 +01001640 linenr_T lnum = -1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001641
Bram Moolenaar56acb092020-08-16 14:48:19 +02001642 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
1643 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02001644 if (lnum <= 0 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001645 {
1646 int fnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001647 pos_T *fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001648
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02001649 // no valid number, try using arg like line()
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001650 if (fp != NULL)
1651 lnum = fp->lnum;
1652 }
1653 return lnum;
1654}
1655
1656/*
1657 * Get the lnum from the first argument.
1658 * Also accepts "$", then "buf" is used.
1659 * Returns 0 on error.
1660 */
1661 linenr_T
1662tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1663{
1664 if (argvars[0].v_type == VAR_STRING
1665 && argvars[0].vval.v_string != NULL
1666 && argvars[0].vval.v_string[0] == '$'
1667 && buf != NULL)
1668 return buf->b_ml.ml_line_count;
1669 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1670}
1671
1672/*
1673 * Get buffer by number or pattern.
1674 */
1675 buf_T *
1676tv_get_buf(typval_T *tv, int curtab_only)
1677{
1678 char_u *name = tv->vval.v_string;
1679 buf_T *buf;
1680
1681 if (tv->v_type == VAR_NUMBER)
1682 return buflist_findnr((int)tv->vval.v_number);
1683 if (tv->v_type != VAR_STRING)
1684 return NULL;
1685 if (name == NULL || *name == NUL)
1686 return curbuf;
1687 if (name[0] == '$' && name[1] == NUL)
1688 return lastbuf;
1689
1690 buf = buflist_find_by_name(name, curtab_only);
1691
1692 // If not found, try expanding the name, like done for bufexists().
1693 if (buf == NULL)
1694 buf = find_buffer(tv);
1695
1696 return buf;
1697}
1698
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02001699/*
1700 * Like tv_get_buf() but give an error message is the type is wrong.
1701 */
1702 buf_T *
1703tv_get_buf_from_arg(typval_T *tv)
1704{
1705 buf_T *buf;
1706
1707 ++emsg_off;
1708 buf = tv_get_buf(tv, FALSE);
1709 --emsg_off;
1710 if (buf == NULL
1711 && tv->v_type != VAR_NUMBER
1712 && tv->v_type != VAR_STRING)
1713 // issue errmsg for type error
1714 (void)tv_get_number(tv);
1715 return buf;
1716}
1717
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001718#endif // FEAT_EVAL