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