blob: 6e0b528e7992c8fa190847fa557afafbc16f4727 [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 Lakshmanan1a71d312021-07-15 12:49:58 +0200388 * Give an error and return FAIL unless "tv" is a number.
389 */
390 int
391check_for_number_arg(typval_T *args, int idx)
392{
393 if (args[idx].v_type != VAR_NUMBER)
394 {
395 if (idx >= 0)
396 semsg(_(e_number_required_for_argument_nr), idx + 1);
397 else
398 emsg(_(e_numberreq));
399 return FAIL;
400 }
401 return OK;
402}
403
404/*
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200405 * Give an error and return FAIL unless "tv" is a dict.
406 */
407 int
408check_for_dict_arg(typval_T *args, int idx)
409{
410 if (args[idx].v_type != VAR_DICT)
411 {
412 if (idx >= 0)
413 semsg(_(e_dict_required_for_argument_nr), idx + 1);
414 else
415 emsg(_(e_dictreq));
416 return FAIL;
417 }
418 return OK;
419}
420
421/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200422 * Get the string value of a variable.
423 * If it is a Number variable, the number is converted into a string.
424 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
425 * tv_get_string_buf() uses a given buffer.
426 * If the String variable has never been set, return an empty string.
427 * Never returns NULL;
428 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
429 * NULL on error.
430 */
431 char_u *
432tv_get_string(typval_T *varp)
433{
434 static char_u mybuf[NUMBUFLEN];
435
436 return tv_get_string_buf(varp, mybuf);
437}
438
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100439/*
440 * Like tv_get_string() but don't allow number to string conversion for Vim9.
441 */
442 char_u *
443tv_get_string_strict(typval_T *varp)
444{
445 static char_u mybuf[NUMBUFLEN];
446 char_u *res = tv_get_string_buf_chk_strict(
447 varp, mybuf, in_vim9script());
448
449 return res != NULL ? res : (char_u *)"";
450}
451
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200452 char_u *
453tv_get_string_buf(typval_T *varp, char_u *buf)
454{
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200455 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200456
457 return res != NULL ? res : (char_u *)"";
458}
459
460/*
461 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
462 */
463 char_u *
464tv_get_string_chk(typval_T *varp)
465{
466 static char_u mybuf[NUMBUFLEN];
467
468 return tv_get_string_buf_chk(varp, mybuf);
469}
470
471 char_u *
472tv_get_string_buf_chk(typval_T *varp, char_u *buf)
473{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100474 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
475}
476
477 char_u *
478tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
479{
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200480 switch (varp->v_type)
481 {
482 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100483 if (strict)
484 {
485 emsg(_(e_using_number_as_string));
486 break;
487 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200488 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
489 (varnumber_T)varp->vval.v_number);
490 return buf;
491 case VAR_FUNC:
492 case VAR_PARTIAL:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200493 emsg(_("E729: Using a Funcref as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200494 break;
495 case VAR_LIST:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200496 emsg(_("E730: Using a List as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200497 break;
498 case VAR_DICT:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200499 emsg(_("E731: Using a Dictionary as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200500 break;
501 case VAR_FLOAT:
502#ifdef FEAT_FLOAT
Bram Moolenaar7a2217b2021-06-06 12:33:49 +0200503 if (strict)
504 {
505 emsg(_(e_float_as_string));
506 break;
507 }
508 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
509 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200510#endif
511 case VAR_STRING:
512 if (varp->vval.v_string != NULL)
513 return varp->vval.v_string;
514 return (char_u *)"";
515 case VAR_BOOL:
516 case VAR_SPECIAL:
517 STRCPY(buf, get_var_special_name(varp->vval.v_number));
518 return buf;
519 case VAR_BLOB:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200520 emsg(_("E976: Using a Blob as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200521 break;
522 case VAR_JOB:
523#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200524 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200525 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200526 semsg(_(e_using_invalid_value_as_string_str), "job");
527 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200528 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200529 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200530#endif
531 break;
532 case VAR_CHANNEL:
533#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200534 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200535 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200536 semsg(_(e_using_invalid_value_as_string_str), "channel");
537 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200538 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200539 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200540#endif
541 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200542 case VAR_VOID:
543 emsg(_(e_cannot_use_void_value));
544 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200545 case VAR_UNKNOWN:
546 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200547 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +0200548 semsg(_(e_using_invalid_value_as_string_str),
549 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200550 break;
551 }
552 return NULL;
553}
554
555/*
556 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
557 * string() on Dict, List, etc.
558 */
559 char_u *
560tv_stringify(typval_T *varp, char_u *buf)
561{
562 if (varp->v_type == VAR_LIST
563 || varp->v_type == VAR_DICT
564 || varp->v_type == VAR_BLOB
565 || varp->v_type == VAR_FUNC
566 || varp->v_type == VAR_PARTIAL
567 || varp->v_type == VAR_FLOAT)
568 {
569 typval_T tmp;
570
571 f_string(varp, &tmp);
572 tv_get_string_buf(&tmp, buf);
573 clear_tv(varp);
574 *varp = tmp;
575 return tmp.vval.v_string;
576 }
577 return tv_get_string_buf(varp, buf);
578}
579
580/*
581 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
582 * Also give an error message, using "name" or _("name") when use_gettext is
583 * TRUE.
584 */
585 int
586tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
587{
588 int lock = 0;
589
590 switch (tv->v_type)
591 {
592 case VAR_BLOB:
593 if (tv->vval.v_blob != NULL)
594 lock = tv->vval.v_blob->bv_lock;
595 break;
596 case VAR_LIST:
597 if (tv->vval.v_list != NULL)
598 lock = tv->vval.v_list->lv_lock;
599 break;
600 case VAR_DICT:
601 if (tv->vval.v_dict != NULL)
602 lock = tv->vval.v_dict->dv_lock;
603 break;
604 default:
605 break;
606 }
Bram Moolenaara187c432020-09-16 21:08:28 +0200607 return value_check_lock(tv->v_lock, name, use_gettext)
608 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200609}
610
611/*
612 * Copy the values from typval_T "from" to typval_T "to".
613 * When needed allocates string or increases reference count.
614 * Does not make a copy of a list, blob or dict but copies the reference!
615 * It is OK for "from" and "to" to point to the same item. This is used to
616 * make a copy later.
617 */
618 void
619copy_tv(typval_T *from, typval_T *to)
620{
621 to->v_type = from->v_type;
622 to->v_lock = 0;
623 switch (from->v_type)
624 {
625 case VAR_NUMBER:
626 case VAR_BOOL:
627 case VAR_SPECIAL:
628 to->vval.v_number = from->vval.v_number;
629 break;
630 case VAR_FLOAT:
631#ifdef FEAT_FLOAT
632 to->vval.v_float = from->vval.v_float;
633 break;
634#endif
635 case VAR_JOB:
636#ifdef FEAT_JOB_CHANNEL
637 to->vval.v_job = from->vval.v_job;
638 if (to->vval.v_job != NULL)
639 ++to->vval.v_job->jv_refcount;
640 break;
641#endif
642 case VAR_CHANNEL:
643#ifdef FEAT_JOB_CHANNEL
644 to->vval.v_channel = from->vval.v_channel;
645 if (to->vval.v_channel != NULL)
646 ++to->vval.v_channel->ch_refcount;
647 break;
648#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200649 case VAR_INSTR:
650 to->vval.v_instr = from->vval.v_instr;
651 break;
652
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200653 case VAR_STRING:
654 case VAR_FUNC:
655 if (from->vval.v_string == NULL)
656 to->vval.v_string = NULL;
657 else
658 {
659 to->vval.v_string = vim_strsave(from->vval.v_string);
660 if (from->v_type == VAR_FUNC)
661 func_ref(to->vval.v_string);
662 }
663 break;
664 case VAR_PARTIAL:
665 if (from->vval.v_partial == NULL)
666 to->vval.v_partial = NULL;
667 else
668 {
669 to->vval.v_partial = from->vval.v_partial;
670 ++to->vval.v_partial->pt_refcount;
671 }
672 break;
673 case VAR_BLOB:
674 if (from->vval.v_blob == NULL)
675 to->vval.v_blob = NULL;
676 else
677 {
678 to->vval.v_blob = from->vval.v_blob;
679 ++to->vval.v_blob->bv_refcount;
680 }
681 break;
682 case VAR_LIST:
683 if (from->vval.v_list == NULL)
684 to->vval.v_list = NULL;
685 else
686 {
687 to->vval.v_list = from->vval.v_list;
688 ++to->vval.v_list->lv_refcount;
689 }
690 break;
691 case VAR_DICT:
692 if (from->vval.v_dict == NULL)
693 to->vval.v_dict = NULL;
694 else
695 {
696 to->vval.v_dict = from->vval.v_dict;
697 ++to->vval.v_dict->dv_refcount;
698 }
699 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +0200700 case VAR_VOID:
701 emsg(_(e_cannot_use_void_value));
702 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200703 case VAR_UNKNOWN:
704 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200705 internal_error_no_abort("copy_tv(UNKNOWN)");
706 break;
707 }
708}
709
710/*
711 * Compare "typ1" and "typ2". Put the result in "typ1".
712 */
713 int
714typval_compare(
715 typval_T *typ1, // first operand
716 typval_T *typ2, // second operand
Bram Moolenaar657137c2021-01-09 15:45:23 +0100717 exprtype_T type, // operator
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200718 int ic) // ignore case
719{
720 int i;
721 varnumber_T n1, n2;
722 char_u *s1, *s2;
723 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
724 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
725
726 if (type_is && typ1->v_type != typ2->v_type)
727 {
728 // For "is" a different type always means FALSE, for "notis"
729 // it means TRUE.
730 n1 = (type == EXPR_ISNOT);
731 }
732 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
733 {
734 if (type_is)
735 {
736 n1 = (typ1->v_type == typ2->v_type
737 && typ1->vval.v_blob == typ2->vval.v_blob);
738 if (type == EXPR_ISNOT)
739 n1 = !n1;
740 }
741 else if (typ1->v_type != typ2->v_type
742 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
743 {
744 if (typ1->v_type != typ2->v_type)
745 emsg(_("E977: Can only compare Blob with Blob"));
746 else
747 emsg(_(e_invalblob));
748 clear_tv(typ1);
749 return FAIL;
750 }
751 else
752 {
753 // Compare two Blobs for being equal or unequal.
754 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
755 if (type == EXPR_NEQUAL)
756 n1 = !n1;
757 }
758 }
759 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
760 {
761 if (type_is)
762 {
763 n1 = (typ1->v_type == typ2->v_type
764 && typ1->vval.v_list == typ2->vval.v_list);
765 if (type == EXPR_ISNOT)
766 n1 = !n1;
767 }
768 else if (typ1->v_type != typ2->v_type
769 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
770 {
771 if (typ1->v_type != typ2->v_type)
772 emsg(_("E691: Can only compare List with List"));
773 else
774 emsg(_("E692: Invalid operation for List"));
775 clear_tv(typ1);
776 return FAIL;
777 }
778 else
779 {
780 // Compare two Lists for being equal or unequal.
781 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
782 ic, FALSE);
783 if (type == EXPR_NEQUAL)
784 n1 = !n1;
785 }
786 }
787
788 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
789 {
790 if (type_is)
791 {
792 n1 = (typ1->v_type == typ2->v_type
793 && typ1->vval.v_dict == typ2->vval.v_dict);
794 if (type == EXPR_ISNOT)
795 n1 = !n1;
796 }
797 else if (typ1->v_type != typ2->v_type
798 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
799 {
800 if (typ1->v_type != typ2->v_type)
801 emsg(_("E735: Can only compare Dictionary with Dictionary"));
802 else
803 emsg(_("E736: Invalid operation for Dictionary"));
804 clear_tv(typ1);
805 return FAIL;
806 }
807 else
808 {
809 // Compare two Dictionaries for being equal or unequal.
810 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
811 ic, FALSE);
812 if (type == EXPR_NEQUAL)
813 n1 = !n1;
814 }
815 }
816
817 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
818 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
819 {
820 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
821 && type != EXPR_IS && type != EXPR_ISNOT)
822 {
823 emsg(_("E694: Invalid operation for Funcrefs"));
824 clear_tv(typ1);
825 return FAIL;
826 }
827 if ((typ1->v_type == VAR_PARTIAL
828 && typ1->vval.v_partial == NULL)
829 || (typ2->v_type == VAR_PARTIAL
830 && typ2->vval.v_partial == NULL))
831 // When both partials are NULL, then they are equal.
832 // Otherwise they are not equal.
833 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
834 else if (type_is)
835 {
836 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
837 // strings are considered the same if their value is
838 // the same
839 n1 = tv_equal(typ1, typ2, ic, FALSE);
840 else if (typ1->v_type == VAR_PARTIAL
841 && typ2->v_type == VAR_PARTIAL)
842 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
843 else
844 n1 = FALSE;
845 }
846 else
847 n1 = tv_equal(typ1, typ2, ic, FALSE);
848 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
849 n1 = !n1;
850 }
851
852#ifdef FEAT_FLOAT
853 // If one of the two variables is a float, compare as a float.
854 // When using "=~" or "!~", always compare as string.
855 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
856 && type != EXPR_MATCH && type != EXPR_NOMATCH)
857 {
858 float_T f1, f2;
859
860 f1 = tv_get_float(typ1);
861 f2 = tv_get_float(typ2);
862 n1 = FALSE;
863 switch (type)
864 {
865 case EXPR_IS:
866 case EXPR_EQUAL: n1 = (f1 == f2); break;
867 case EXPR_ISNOT:
868 case EXPR_NEQUAL: n1 = (f1 != f2); break;
869 case EXPR_GREATER: n1 = (f1 > f2); break;
870 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
871 case EXPR_SMALLER: n1 = (f1 < f2); break;
872 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
873 case EXPR_UNKNOWN:
874 case EXPR_MATCH:
875 default: break; // avoid gcc warning
876 }
877 }
878#endif
879
880 // If one of the two variables is a number, compare as a number.
881 // When using "=~" or "!~", always compare as string.
882 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
883 && type != EXPR_MATCH && type != EXPR_NOMATCH)
884 {
885 n1 = tv_get_number(typ1);
886 n2 = tv_get_number(typ2);
887 switch (type)
888 {
889 case EXPR_IS:
890 case EXPR_EQUAL: n1 = (n1 == n2); break;
891 case EXPR_ISNOT:
892 case EXPR_NEQUAL: n1 = (n1 != n2); break;
893 case EXPR_GREATER: n1 = (n1 > n2); break;
894 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
895 case EXPR_SMALLER: n1 = (n1 < n2); break;
896 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
897 case EXPR_UNKNOWN:
898 case EXPR_MATCH:
899 default: break; // avoid gcc warning
900 }
901 }
Bram Moolenaarcff40ff2021-01-09 16:21:37 +0100902 else if (in_vim9script() && (typ1->v_type == VAR_BOOL
903 || typ2->v_type == VAR_BOOL))
904 {
905 if (typ1->v_type != typ2->v_type)
906 {
907 semsg(_(e_cannot_compare_str_with_str),
908 vartype_name(typ1->v_type), vartype_name(typ2->v_type));
909 clear_tv(typ1);
910 return FAIL;
911 }
912 n1 = typ1->vval.v_number;
913 n2 = typ2->vval.v_number;
914 switch (type)
915 {
916 case EXPR_IS:
917 case EXPR_EQUAL: n1 = (n1 == n2); break;
918 case EXPR_ISNOT:
919 case EXPR_NEQUAL: n1 = (n1 != n2); break;
920 default:
921 emsg(_(e_invalid_operation_for_bool));
922 clear_tv(typ1);
923 return FAIL;
924 }
925 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200926 else
927 {
928 s1 = tv_get_string_buf(typ1, buf1);
929 s2 = tv_get_string_buf(typ2, buf2);
930 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
931 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
932 else
933 i = 0;
934 n1 = FALSE;
935 switch (type)
936 {
937 case EXPR_IS:
938 case EXPR_EQUAL: n1 = (i == 0); break;
939 case EXPR_ISNOT:
940 case EXPR_NEQUAL: n1 = (i != 0); break;
941 case EXPR_GREATER: n1 = (i > 0); break;
942 case EXPR_GEQUAL: n1 = (i >= 0); break;
943 case EXPR_SMALLER: n1 = (i < 0); break;
944 case EXPR_SEQUAL: n1 = (i <= 0); break;
945
946 case EXPR_MATCH:
947 case EXPR_NOMATCH:
948 n1 = pattern_match(s2, s1, ic);
949 if (type == EXPR_NOMATCH)
950 n1 = !n1;
951 break;
952
953 default: break; // avoid gcc warning
954 }
955 }
956 clear_tv(typ1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +0200957 if (in_vim9script())
958 {
959 typ1->v_type = VAR_BOOL;
960 typ1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
961 }
962 else
963 {
964 typ1->v_type = VAR_NUMBER;
965 typ1->vval.v_number = n1;
966 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200967
968 return OK;
969}
970
Bram Moolenaar34453202021-01-31 13:08:38 +0100971/*
972 * Convert any type to a string, never give an error.
973 * When "quotes" is TRUE add quotes to a string.
974 * Returns an allocated string.
975 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200976 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +0100977typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200978{
979 char_u *tofree;
980 char_u numbuf[NUMBUFLEN];
981 char_u *ret = NULL;
982
983 if (arg == NULL)
984 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +0100985 if (!quotes && arg->v_type == VAR_STRING)
986 {
987 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
988 : arg->vval.v_string);
989 }
990 else
991 {
992 ret = tv2string(arg, &tofree, numbuf, 0);
993 // Make a copy if we have a value but it's not in allocated memory.
994 if (ret != NULL && tofree == NULL)
995 ret = vim_strsave(ret);
996 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200997 return ret;
998}
999
1000/*
1001 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1002 * or it refers to a List or Dictionary that is locked.
1003 */
1004 int
1005tv_islocked(typval_T *tv)
1006{
1007 return (tv->v_lock & VAR_LOCKED)
1008 || (tv->v_type == VAR_LIST
1009 && tv->vval.v_list != NULL
1010 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1011 || (tv->v_type == VAR_DICT
1012 && tv->vval.v_dict != NULL
1013 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1014}
1015
1016 static int
1017func_equal(
1018 typval_T *tv1,
1019 typval_T *tv2,
1020 int ic) // ignore case
1021{
1022 char_u *s1, *s2;
1023 dict_T *d1, *d2;
1024 int a1, a2;
1025 int i;
1026
1027 // empty and NULL function name considered the same
1028 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1029 : partial_name(tv1->vval.v_partial);
1030 if (s1 != NULL && *s1 == NUL)
1031 s1 = NULL;
1032 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1033 : partial_name(tv2->vval.v_partial);
1034 if (s2 != NULL && *s2 == NUL)
1035 s2 = NULL;
1036 if (s1 == NULL || s2 == NULL)
1037 {
1038 if (s1 != s2)
1039 return FALSE;
1040 }
1041 else if (STRCMP(s1, s2) != 0)
1042 return FALSE;
1043
1044 // empty dict and NULL dict is different
1045 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1046 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1047 if (d1 == NULL || d2 == NULL)
1048 {
1049 if (d1 != d2)
1050 return FALSE;
1051 }
1052 else if (!dict_equal(d1, d2, ic, TRUE))
1053 return FALSE;
1054
1055 // empty list and no list considered the same
1056 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1057 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1058 if (a1 != a2)
1059 return FALSE;
1060 for (i = 0; i < a1; ++i)
1061 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1062 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1063 return FALSE;
1064
1065 return TRUE;
1066}
1067
1068/*
1069 * Return TRUE if "tv1" and "tv2" have the same value.
1070 * Compares the items just like "==" would compare them, but strings and
1071 * numbers are different. Floats and numbers are also different.
1072 */
1073 int
1074tv_equal(
1075 typval_T *tv1,
1076 typval_T *tv2,
1077 int ic, // ignore case
1078 int recursive) // TRUE when used recursively
1079{
1080 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1081 char_u *s1, *s2;
1082 static int recursive_cnt = 0; // catch recursive loops
1083 int r;
1084 static int tv_equal_recurse_limit;
1085
1086 // Catch lists and dicts that have an endless loop by limiting
1087 // recursiveness to a limit. We guess they are equal then.
1088 // A fixed limit has the problem of still taking an awful long time.
1089 // Reduce the limit every time running into it. That should work fine for
1090 // deeply linked structures that are not recursively linked and catch
1091 // recursiveness quickly.
1092 if (!recursive)
1093 tv_equal_recurse_limit = 1000;
1094 if (recursive_cnt >= tv_equal_recurse_limit)
1095 {
1096 --tv_equal_recurse_limit;
1097 return TRUE;
1098 }
1099
1100 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1101 // arguments.
1102 if ((tv1->v_type == VAR_FUNC
1103 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1104 && (tv2->v_type == VAR_FUNC
1105 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1106 {
1107 ++recursive_cnt;
1108 r = func_equal(tv1, tv2, ic);
1109 --recursive_cnt;
1110 return r;
1111 }
1112
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001113 if (tv1->v_type != tv2->v_type
1114 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1115 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001116 return FALSE;
1117
1118 switch (tv1->v_type)
1119 {
1120 case VAR_LIST:
1121 ++recursive_cnt;
1122 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1123 --recursive_cnt;
1124 return r;
1125
1126 case VAR_DICT:
1127 ++recursive_cnt;
1128 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1129 --recursive_cnt;
1130 return r;
1131
1132 case VAR_BLOB:
1133 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1134
1135 case VAR_NUMBER:
1136 case VAR_BOOL:
1137 case VAR_SPECIAL:
1138 return tv1->vval.v_number == tv2->vval.v_number;
1139
1140 case VAR_STRING:
1141 s1 = tv_get_string_buf(tv1, buf1);
1142 s2 = tv_get_string_buf(tv2, buf2);
1143 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1144
1145 case VAR_FLOAT:
1146#ifdef FEAT_FLOAT
1147 return tv1->vval.v_float == tv2->vval.v_float;
1148#endif
1149 case VAR_JOB:
1150#ifdef FEAT_JOB_CHANNEL
1151 return tv1->vval.v_job == tv2->vval.v_job;
1152#endif
1153 case VAR_CHANNEL:
1154#ifdef FEAT_JOB_CHANNEL
1155 return tv1->vval.v_channel == tv2->vval.v_channel;
1156#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001157 case VAR_INSTR:
1158 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001159
1160 case VAR_PARTIAL:
1161 return tv1->vval.v_partial == tv2->vval.v_partial;
1162
1163 case VAR_FUNC:
1164 return tv1->vval.v_string == tv2->vval.v_string;
1165
1166 case VAR_UNKNOWN:
1167 case VAR_ANY:
1168 case VAR_VOID:
1169 break;
1170 }
1171
1172 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1173 // does not equal anything, not even itself.
1174 return FALSE;
1175}
1176
1177/*
1178 * Get an option value.
1179 * "arg" points to the '&' or '+' before the option name.
1180 * "arg" is advanced to character after the option name.
1181 * Return OK or FAIL.
1182 */
1183 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001184eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001185 char_u **arg,
1186 typval_T *rettv, // when NULL, only check if option exists
1187 int evaluate)
1188{
1189 char_u *option_end;
1190 long numval;
1191 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001192 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001193 int c;
1194 int working = (**arg == '+'); // has("+option")
1195 int ret = OK;
1196 int opt_flags;
1197
1198 // Isolate the option name and find its value.
1199 option_end = find_option_end(arg, &opt_flags);
1200 if (option_end == NULL)
1201 {
1202 if (rettv != NULL)
1203 semsg(_("E112: Option name missing: %s"), *arg);
1204 return FAIL;
1205 }
1206
1207 if (!evaluate)
1208 {
1209 *arg = option_end;
1210 return OK;
1211 }
1212
1213 c = *option_end;
1214 *option_end = NUL;
1215 opt_type = get_option_value(*arg, &numval,
1216 rettv == NULL ? NULL : &stringval, opt_flags);
1217
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001218 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001219 {
1220 if (rettv != NULL)
1221 semsg(_(e_unknown_option), *arg);
1222 ret = FAIL;
1223 }
1224 else if (rettv != NULL)
1225 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001226 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001227 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001228 {
1229 rettv->v_type = VAR_STRING;
1230 rettv->vval.v_string = NULL;
1231 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001232 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001233 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001234 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1235 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001236 rettv->vval.v_number = 0;
1237 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001238 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001239 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001240 if (in_vim9script() && opt_type == gov_bool)
1241 {
1242 rettv->v_type = VAR_BOOL;
1243 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1244 }
1245 else
1246 {
1247 rettv->v_type = VAR_NUMBER;
1248 rettv->vval.v_number = numval;
1249 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001250 }
1251 else // string option
1252 {
1253 rettv->v_type = VAR_STRING;
1254 rettv->vval.v_string = stringval;
1255 }
1256 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001257 else if (working && (opt_type == gov_hidden_bool
1258 || opt_type == gov_hidden_number
1259 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001260 ret = FAIL;
1261
1262 *option_end = c; // put back for error messages
1263 *arg = option_end;
1264
1265 return ret;
1266}
1267
1268/*
1269 * Allocate a variable for a number constant. Also deals with "0z" for blob.
1270 * Return OK or FAIL.
1271 */
1272 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001273eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001274 char_u **arg,
1275 typval_T *rettv,
1276 int evaluate,
1277 int want_string UNUSED)
1278{
1279 int len;
1280#ifdef FEAT_FLOAT
1281 char_u *p;
1282 int get_float = FALSE;
1283
1284 // We accept a float when the format matches
1285 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
1286 // strict to avoid backwards compatibility problems.
1287 // With script version 2 and later the leading digit can be
1288 // omitted.
1289 // Don't look for a float after the "." operator, so that
1290 // ":let vers = 1.2.3" doesn't fail.
1291 if (**arg == '.')
1292 p = *arg;
1293 else
1294 p = skipdigits(*arg + 1);
1295 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
1296 {
1297 get_float = TRUE;
1298 p = skipdigits(p + 2);
1299 if (*p == 'e' || *p == 'E')
1300 {
1301 ++p;
1302 if (*p == '-' || *p == '+')
1303 ++p;
1304 if (!vim_isdigit(*p))
1305 get_float = FALSE;
1306 else
1307 p = skipdigits(p + 1);
1308 }
1309 if (ASCII_ISALPHA(*p) || *p == '.')
1310 get_float = FALSE;
1311 }
1312 if (get_float)
1313 {
1314 float_T f;
1315
1316 *arg += string2float(*arg, &f);
1317 if (evaluate)
1318 {
1319 rettv->v_type = VAR_FLOAT;
1320 rettv->vval.v_float = f;
1321 }
1322 }
1323 else
1324#endif
1325 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
1326 {
1327 char_u *bp;
1328 blob_T *blob = NULL; // init for gcc
1329
1330 // Blob constant: 0z0123456789abcdef
1331 if (evaluate)
1332 blob = blob_alloc();
1333 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
1334 {
1335 if (!vim_isxdigit(bp[1]))
1336 {
1337 if (blob != NULL)
1338 {
1339 emsg(_("E973: Blob literal should have an even number of hex characters"));
1340 ga_clear(&blob->bv_ga);
1341 VIM_CLEAR(blob);
1342 }
1343 return FAIL;
1344 }
1345 if (blob != NULL)
1346 ga_append(&blob->bv_ga,
1347 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
1348 if (bp[2] == '.' && vim_isxdigit(bp[3]))
1349 ++bp;
1350 }
1351 if (blob != NULL)
1352 rettv_blob_set(rettv, blob);
1353 *arg = bp;
1354 }
1355 else
1356 {
1357 varnumber_T n;
1358
1359 // decimal, hex or octal number
1360 vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4
1361 ? STR2NR_NO_OCT + STR2NR_QUOTE
1362 : STR2NR_ALL, &n, NULL, 0, TRUE);
1363 if (len == 0)
1364 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001365 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001366 return FAIL;
1367 }
1368 *arg += len;
1369 if (evaluate)
1370 {
1371 rettv->v_type = VAR_NUMBER;
1372 rettv->vval.v_number = n;
1373 }
1374 }
1375 return OK;
1376}
1377
1378/*
1379 * Allocate a variable for a string constant.
1380 * Return OK or FAIL.
1381 */
1382 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001383eval_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001384{
1385 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001386 char_u *end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001387 int extra = 0;
1388 int len;
1389
1390 // Find the end of the string, skipping backslashed characters.
1391 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
1392 {
1393 if (*p == '\\' && p[1] != NUL)
1394 {
1395 ++p;
1396 // A "\<x>" form occupies at least 4 characters, and produces up
1397 // to 21 characters (3 * 6 for the char and 3 for a modifier):
1398 // reserve space for 18 extra.
1399 // Each byte in the char could be encoded as K_SPECIAL K_EXTRA x.
1400 if (*p == '<')
1401 extra += 18;
1402 }
1403 }
1404
1405 if (*p != '"')
1406 {
1407 semsg(_("E114: Missing quote: %s"), *arg);
1408 return FAIL;
1409 }
1410
1411 // If only parsing, set *arg and return here
1412 if (!evaluate)
1413 {
1414 *arg = p + 1;
1415 return OK;
1416 }
1417
1418 // Copy the string into allocated memory, handling backslashed
1419 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001420 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001421 len = (int)(p - *arg + extra);
1422 rettv->vval.v_string = alloc(len);
1423 if (rettv->vval.v_string == NULL)
1424 return FAIL;
1425 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001426
1427 for (p = *arg + 1; *p != NUL && *p != '"'; )
1428 {
1429 if (*p == '\\')
1430 {
1431 switch (*++p)
1432 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001433 case 'b': *end++ = BS; ++p; break;
1434 case 'e': *end++ = ESC; ++p; break;
1435 case 'f': *end++ = FF; ++p; break;
1436 case 'n': *end++ = NL; ++p; break;
1437 case 'r': *end++ = CAR; ++p; break;
1438 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001439
1440 case 'X': // hex: "\x1", "\x12"
1441 case 'x':
1442 case 'u': // Unicode: "\u0023"
1443 case 'U':
1444 if (vim_isxdigit(p[1]))
1445 {
1446 int n, nr;
1447 int c = toupper(*p);
1448
1449 if (c == 'X')
1450 n = 2;
1451 else if (*p == 'u')
1452 n = 4;
1453 else
1454 n = 8;
1455 nr = 0;
1456 while (--n >= 0 && vim_isxdigit(p[1]))
1457 {
1458 ++p;
1459 nr = (nr << 4) + hex2nr(*p);
1460 }
1461 ++p;
1462 // For "\u" store the number according to
1463 // 'encoding'.
1464 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001465 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001466 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001467 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001468 }
1469 break;
1470
1471 // octal: "\1", "\12", "\123"
1472 case '0':
1473 case '1':
1474 case '2':
1475 case '3':
1476 case '4':
1477 case '5':
1478 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001479 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001480 if (*p >= '0' && *p <= '7')
1481 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001482 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001483 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001484 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001485 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001486 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001487 break;
1488
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001489 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001490 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001491 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001492 int flags = FSK_KEYCODE | FSK_IN_STRING;
1493
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001494 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001495 flags |= FSK_SIMPLIFY;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001496 extra = trans_special(&p, end, flags, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001497 if (extra != 0)
1498 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001499 end += extra;
1500 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001501 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001502 break;
1503 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001504 }
1505 // FALLTHROUGH
1506
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001507 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001508 break;
1509 }
1510 }
1511 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001512 MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001513 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001514 *end = NUL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001515 if (*p != NUL) // just in case
1516 ++p;
1517 *arg = p;
1518
1519 return OK;
1520}
1521
1522/*
1523 * Allocate a variable for a 'str''ing' constant.
1524 * Return OK or FAIL.
1525 */
1526 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001527eval_lit_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001528{
1529 char_u *p;
1530 char_u *str;
1531 int reduce = 0;
1532
1533 // Find the end of the string, skipping ''.
1534 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
1535 {
1536 if (*p == '\'')
1537 {
1538 if (p[1] != '\'')
1539 break;
1540 ++reduce;
1541 ++p;
1542 }
1543 }
1544
1545 if (*p != '\'')
1546 {
1547 semsg(_("E115: Missing quote: %s"), *arg);
1548 return FAIL;
1549 }
1550
1551 // If only parsing return after setting "*arg"
1552 if (!evaluate)
1553 {
1554 *arg = p + 1;
1555 return OK;
1556 }
1557
1558 // Copy the string into allocated memory, handling '' to ' reduction.
1559 str = alloc((p - *arg) - reduce);
1560 if (str == NULL)
1561 return FAIL;
1562 rettv->v_type = VAR_STRING;
1563 rettv->vval.v_string = str;
1564
1565 for (p = *arg + 1; *p != NUL; )
1566 {
1567 if (*p == '\'')
1568 {
1569 if (p[1] != '\'')
1570 break;
1571 ++p;
1572 }
1573 MB_COPY_CHAR(p, str);
1574 }
1575 *str = NUL;
1576 *arg = p + 1;
1577
1578 return OK;
1579}
1580
1581/*
1582 * Return a string with the string representation of a variable.
1583 * If the memory is allocated "tofree" is set to it, otherwise NULL.
1584 * "numbuf" is used for a number.
1585 * Puts quotes around strings, so that they can be parsed back by eval().
1586 * May return NULL.
1587 */
1588 char_u *
1589tv2string(
1590 typval_T *tv,
1591 char_u **tofree,
1592 char_u *numbuf,
1593 int copyID)
1594{
1595 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
1596}
1597
1598/*
1599 * Get the value of an environment variable.
1600 * "arg" is pointing to the '$'. It is advanced to after the name.
1601 * If the environment variable was not set, silently assume it is empty.
1602 * Return FAIL if the name is invalid.
1603 */
1604 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001605eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001606{
1607 char_u *string = NULL;
1608 int len;
1609 int cc;
1610 char_u *name;
1611 int mustfree = FALSE;
1612
1613 ++*arg;
1614 name = *arg;
1615 len = get_env_len(arg);
1616 if (evaluate)
1617 {
1618 if (len == 0)
1619 return FAIL; // invalid empty name
1620
1621 cc = name[len];
1622 name[len] = NUL;
1623 // first try vim_getenv(), fast for normal environment vars
1624 string = vim_getenv(name, &mustfree);
1625 if (string != NULL && *string != NUL)
1626 {
1627 if (!mustfree)
1628 string = vim_strsave(string);
1629 }
1630 else
1631 {
1632 if (mustfree)
1633 vim_free(string);
1634
1635 // next try expanding things like $VIM and ${HOME}
1636 string = expand_env_save(name - 1);
1637 if (string != NULL && *string == '$')
1638 VIM_CLEAR(string);
1639 }
1640 name[len] = cc;
1641
1642 rettv->v_type = VAR_STRING;
1643 rettv->vval.v_string = string;
1644 }
1645
1646 return OK;
1647}
1648
1649/*
1650 * Get the lnum from the first argument.
1651 * Also accepts ".", "$", etc., but that only works for the current buffer.
1652 * Returns -1 on error.
1653 */
1654 linenr_T
1655tv_get_lnum(typval_T *argvars)
1656{
Bram Moolenaar9a963372020-12-21 21:58:46 +01001657 linenr_T lnum = -1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001658
Bram Moolenaar56acb092020-08-16 14:48:19 +02001659 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
1660 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02001661 if (lnum <= 0 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001662 {
1663 int fnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001664 pos_T *fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001665
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02001666 // no valid number, try using arg like line()
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001667 if (fp != NULL)
1668 lnum = fp->lnum;
1669 }
1670 return lnum;
1671}
1672
1673/*
1674 * Get the lnum from the first argument.
1675 * Also accepts "$", then "buf" is used.
1676 * Returns 0 on error.
1677 */
1678 linenr_T
1679tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1680{
1681 if (argvars[0].v_type == VAR_STRING
1682 && argvars[0].vval.v_string != NULL
1683 && argvars[0].vval.v_string[0] == '$'
1684 && buf != NULL)
1685 return buf->b_ml.ml_line_count;
1686 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1687}
1688
1689/*
1690 * Get buffer by number or pattern.
1691 */
1692 buf_T *
1693tv_get_buf(typval_T *tv, int curtab_only)
1694{
1695 char_u *name = tv->vval.v_string;
1696 buf_T *buf;
1697
1698 if (tv->v_type == VAR_NUMBER)
1699 return buflist_findnr((int)tv->vval.v_number);
1700 if (tv->v_type != VAR_STRING)
1701 return NULL;
1702 if (name == NULL || *name == NUL)
1703 return curbuf;
1704 if (name[0] == '$' && name[1] == NUL)
1705 return lastbuf;
1706
1707 buf = buflist_find_by_name(name, curtab_only);
1708
1709 // If not found, try expanding the name, like done for bufexists().
1710 if (buf == NULL)
1711 buf = find_buffer(tv);
1712
1713 return buf;
1714}
1715
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02001716/*
1717 * Like tv_get_buf() but give an error message is the type is wrong.
1718 */
1719 buf_T *
1720tv_get_buf_from_arg(typval_T *tv)
1721{
1722 buf_T *buf;
1723
1724 ++emsg_off;
1725 buf = tv_get_buf(tv, FALSE);
1726 --emsg_off;
1727 if (buf == NULL
1728 && tv->v_type != VAR_NUMBER
1729 && tv->v_type != VAR_STRING)
1730 // issue errmsg for type error
1731 (void)tv_get_number(tv);
1732 return buf;
1733}
1734
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001735#endif // FEAT_EVAL