blob: 9d5903f649063c8cad559fc43751e4a0e79b62f7 [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/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200355 * Give an error and return FAIL unless "args[idx]" 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 Lakshmanana9a7c0c2021-07-17 19:11:07 +0200388 * Give an error and return FAIL unless "args[idx]" is a number.
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200389 */
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 Lakshmanana9a7c0c2021-07-17 19:11:07 +0200405 * Give an error and return FAIL unless "args[idx]" is a bool.
406 */
407 int
408check_for_bool_arg(typval_T *args, int idx)
409{
410 if (args[idx].v_type != VAR_BOOL
411 && !(args[idx].v_type == VAR_NUMBER
412 && (args[idx].vval.v_number == 0
413 || args[idx].vval.v_number == 1)))
414 {
415 if (idx >= 0)
416 semsg(_(e_bool_required_for_argument_nr), idx + 1);
417 else
418 emsg(_(e_boolreq));
419 return FAIL;
420 }
421 return OK;
422}
423
424/*
425 * Give an error and return FAIL unless "args[idx]" is a list.
426 */
427 int
428check_for_list_arg(typval_T *args, int idx)
429{
430 if (args[idx].v_type != VAR_LIST)
431 {
432 if (idx >= 0)
433 semsg(_(e_list_required_for_argument_nr), idx + 1);
434 else
435 emsg(_(e_listreq));
436 return FAIL;
437 }
438 return OK;
439}
440
441/*
442 * Give an error and return FAIL unless "args[idx]" is a dict.
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200443 */
444 int
445check_for_dict_arg(typval_T *args, int idx)
446{
447 if (args[idx].v_type != VAR_DICT)
448 {
449 if (idx >= 0)
450 semsg(_(e_dict_required_for_argument_nr), idx + 1);
451 else
452 emsg(_(e_dictreq));
453 return FAIL;
454 }
455 return OK;
456}
457
458/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200459 * Get the string value of a variable.
460 * If it is a Number variable, the number is converted into a string.
461 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
462 * tv_get_string_buf() uses a given buffer.
463 * If the String variable has never been set, return an empty string.
464 * Never returns NULL;
465 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
466 * NULL on error.
467 */
468 char_u *
469tv_get_string(typval_T *varp)
470{
471 static char_u mybuf[NUMBUFLEN];
472
473 return tv_get_string_buf(varp, mybuf);
474}
475
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100476/*
477 * Like tv_get_string() but don't allow number to string conversion for Vim9.
478 */
479 char_u *
480tv_get_string_strict(typval_T *varp)
481{
482 static char_u mybuf[NUMBUFLEN];
483 char_u *res = tv_get_string_buf_chk_strict(
484 varp, mybuf, in_vim9script());
485
486 return res != NULL ? res : (char_u *)"";
487}
488
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200489 char_u *
490tv_get_string_buf(typval_T *varp, char_u *buf)
491{
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200492 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200493
494 return res != NULL ? res : (char_u *)"";
495}
496
497/*
498 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
499 */
500 char_u *
501tv_get_string_chk(typval_T *varp)
502{
503 static char_u mybuf[NUMBUFLEN];
504
505 return tv_get_string_buf_chk(varp, mybuf);
506}
507
508 char_u *
509tv_get_string_buf_chk(typval_T *varp, char_u *buf)
510{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100511 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
512}
513
514 char_u *
515tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
516{
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200517 switch (varp->v_type)
518 {
519 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100520 if (strict)
521 {
522 emsg(_(e_using_number_as_string));
523 break;
524 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200525 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
526 (varnumber_T)varp->vval.v_number);
527 return buf;
528 case VAR_FUNC:
529 case VAR_PARTIAL:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200530 emsg(_("E729: Using a Funcref as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200531 break;
532 case VAR_LIST:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200533 emsg(_("E730: Using a List as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200534 break;
535 case VAR_DICT:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200536 emsg(_("E731: Using a Dictionary as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200537 break;
538 case VAR_FLOAT:
539#ifdef FEAT_FLOAT
Bram Moolenaar7a2217b2021-06-06 12:33:49 +0200540 if (strict)
541 {
542 emsg(_(e_float_as_string));
543 break;
544 }
545 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
546 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200547#endif
548 case VAR_STRING:
549 if (varp->vval.v_string != NULL)
550 return varp->vval.v_string;
551 return (char_u *)"";
552 case VAR_BOOL:
553 case VAR_SPECIAL:
554 STRCPY(buf, get_var_special_name(varp->vval.v_number));
555 return buf;
556 case VAR_BLOB:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200557 emsg(_("E976: Using a Blob as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200558 break;
559 case VAR_JOB:
560#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200561 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200562 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200563 semsg(_(e_using_invalid_value_as_string_str), "job");
564 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200565 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200566 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200567#endif
568 break;
569 case VAR_CHANNEL:
570#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200571 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200572 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200573 semsg(_(e_using_invalid_value_as_string_str), "channel");
574 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200575 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200576 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200577#endif
578 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200579 case VAR_VOID:
580 emsg(_(e_cannot_use_void_value));
581 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200582 case VAR_UNKNOWN:
583 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200584 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +0200585 semsg(_(e_using_invalid_value_as_string_str),
586 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200587 break;
588 }
589 return NULL;
590}
591
592/*
593 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
594 * string() on Dict, List, etc.
595 */
596 char_u *
597tv_stringify(typval_T *varp, char_u *buf)
598{
599 if (varp->v_type == VAR_LIST
600 || varp->v_type == VAR_DICT
601 || varp->v_type == VAR_BLOB
602 || varp->v_type == VAR_FUNC
603 || varp->v_type == VAR_PARTIAL
604 || varp->v_type == VAR_FLOAT)
605 {
606 typval_T tmp;
607
608 f_string(varp, &tmp);
609 tv_get_string_buf(&tmp, buf);
610 clear_tv(varp);
611 *varp = tmp;
612 return tmp.vval.v_string;
613 }
614 return tv_get_string_buf(varp, buf);
615}
616
617/*
618 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
619 * Also give an error message, using "name" or _("name") when use_gettext is
620 * TRUE.
621 */
622 int
623tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
624{
625 int lock = 0;
626
627 switch (tv->v_type)
628 {
629 case VAR_BLOB:
630 if (tv->vval.v_blob != NULL)
631 lock = tv->vval.v_blob->bv_lock;
632 break;
633 case VAR_LIST:
634 if (tv->vval.v_list != NULL)
635 lock = tv->vval.v_list->lv_lock;
636 break;
637 case VAR_DICT:
638 if (tv->vval.v_dict != NULL)
639 lock = tv->vval.v_dict->dv_lock;
640 break;
641 default:
642 break;
643 }
Bram Moolenaara187c432020-09-16 21:08:28 +0200644 return value_check_lock(tv->v_lock, name, use_gettext)
645 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200646}
647
648/*
649 * Copy the values from typval_T "from" to typval_T "to".
650 * When needed allocates string or increases reference count.
651 * Does not make a copy of a list, blob or dict but copies the reference!
652 * It is OK for "from" and "to" to point to the same item. This is used to
653 * make a copy later.
654 */
655 void
656copy_tv(typval_T *from, typval_T *to)
657{
658 to->v_type = from->v_type;
659 to->v_lock = 0;
660 switch (from->v_type)
661 {
662 case VAR_NUMBER:
663 case VAR_BOOL:
664 case VAR_SPECIAL:
665 to->vval.v_number = from->vval.v_number;
666 break;
667 case VAR_FLOAT:
668#ifdef FEAT_FLOAT
669 to->vval.v_float = from->vval.v_float;
670 break;
671#endif
672 case VAR_JOB:
673#ifdef FEAT_JOB_CHANNEL
674 to->vval.v_job = from->vval.v_job;
675 if (to->vval.v_job != NULL)
676 ++to->vval.v_job->jv_refcount;
677 break;
678#endif
679 case VAR_CHANNEL:
680#ifdef FEAT_JOB_CHANNEL
681 to->vval.v_channel = from->vval.v_channel;
682 if (to->vval.v_channel != NULL)
683 ++to->vval.v_channel->ch_refcount;
684 break;
685#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200686 case VAR_INSTR:
687 to->vval.v_instr = from->vval.v_instr;
688 break;
689
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200690 case VAR_STRING:
691 case VAR_FUNC:
692 if (from->vval.v_string == NULL)
693 to->vval.v_string = NULL;
694 else
695 {
696 to->vval.v_string = vim_strsave(from->vval.v_string);
697 if (from->v_type == VAR_FUNC)
698 func_ref(to->vval.v_string);
699 }
700 break;
701 case VAR_PARTIAL:
702 if (from->vval.v_partial == NULL)
703 to->vval.v_partial = NULL;
704 else
705 {
706 to->vval.v_partial = from->vval.v_partial;
707 ++to->vval.v_partial->pt_refcount;
708 }
709 break;
710 case VAR_BLOB:
711 if (from->vval.v_blob == NULL)
712 to->vval.v_blob = NULL;
713 else
714 {
715 to->vval.v_blob = from->vval.v_blob;
716 ++to->vval.v_blob->bv_refcount;
717 }
718 break;
719 case VAR_LIST:
720 if (from->vval.v_list == NULL)
721 to->vval.v_list = NULL;
722 else
723 {
724 to->vval.v_list = from->vval.v_list;
725 ++to->vval.v_list->lv_refcount;
726 }
727 break;
728 case VAR_DICT:
729 if (from->vval.v_dict == NULL)
730 to->vval.v_dict = NULL;
731 else
732 {
733 to->vval.v_dict = from->vval.v_dict;
734 ++to->vval.v_dict->dv_refcount;
735 }
736 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +0200737 case VAR_VOID:
738 emsg(_(e_cannot_use_void_value));
739 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200740 case VAR_UNKNOWN:
741 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200742 internal_error_no_abort("copy_tv(UNKNOWN)");
743 break;
744 }
745}
746
747/*
748 * Compare "typ1" and "typ2". Put the result in "typ1".
749 */
750 int
751typval_compare(
752 typval_T *typ1, // first operand
753 typval_T *typ2, // second operand
Bram Moolenaar657137c2021-01-09 15:45:23 +0100754 exprtype_T type, // operator
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200755 int ic) // ignore case
756{
757 int i;
758 varnumber_T n1, n2;
759 char_u *s1, *s2;
760 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
761 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
762
763 if (type_is && typ1->v_type != typ2->v_type)
764 {
765 // For "is" a different type always means FALSE, for "notis"
766 // it means TRUE.
767 n1 = (type == EXPR_ISNOT);
768 }
769 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
770 {
771 if (type_is)
772 {
773 n1 = (typ1->v_type == typ2->v_type
774 && typ1->vval.v_blob == typ2->vval.v_blob);
775 if (type == EXPR_ISNOT)
776 n1 = !n1;
777 }
778 else if (typ1->v_type != typ2->v_type
779 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
780 {
781 if (typ1->v_type != typ2->v_type)
782 emsg(_("E977: Can only compare Blob with Blob"));
783 else
784 emsg(_(e_invalblob));
785 clear_tv(typ1);
786 return FAIL;
787 }
788 else
789 {
790 // Compare two Blobs for being equal or unequal.
791 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
792 if (type == EXPR_NEQUAL)
793 n1 = !n1;
794 }
795 }
796 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
797 {
798 if (type_is)
799 {
800 n1 = (typ1->v_type == typ2->v_type
801 && typ1->vval.v_list == typ2->vval.v_list);
802 if (type == EXPR_ISNOT)
803 n1 = !n1;
804 }
805 else if (typ1->v_type != typ2->v_type
806 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
807 {
808 if (typ1->v_type != typ2->v_type)
809 emsg(_("E691: Can only compare List with List"));
810 else
811 emsg(_("E692: Invalid operation for List"));
812 clear_tv(typ1);
813 return FAIL;
814 }
815 else
816 {
817 // Compare two Lists for being equal or unequal.
818 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
819 ic, FALSE);
820 if (type == EXPR_NEQUAL)
821 n1 = !n1;
822 }
823 }
824
825 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
826 {
827 if (type_is)
828 {
829 n1 = (typ1->v_type == typ2->v_type
830 && typ1->vval.v_dict == typ2->vval.v_dict);
831 if (type == EXPR_ISNOT)
832 n1 = !n1;
833 }
834 else if (typ1->v_type != typ2->v_type
835 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
836 {
837 if (typ1->v_type != typ2->v_type)
838 emsg(_("E735: Can only compare Dictionary with Dictionary"));
839 else
840 emsg(_("E736: Invalid operation for Dictionary"));
841 clear_tv(typ1);
842 return FAIL;
843 }
844 else
845 {
846 // Compare two Dictionaries for being equal or unequal.
847 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
848 ic, FALSE);
849 if (type == EXPR_NEQUAL)
850 n1 = !n1;
851 }
852 }
853
854 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
855 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
856 {
857 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
858 && type != EXPR_IS && type != EXPR_ISNOT)
859 {
860 emsg(_("E694: Invalid operation for Funcrefs"));
861 clear_tv(typ1);
862 return FAIL;
863 }
864 if ((typ1->v_type == VAR_PARTIAL
865 && typ1->vval.v_partial == NULL)
866 || (typ2->v_type == VAR_PARTIAL
867 && typ2->vval.v_partial == NULL))
868 // When both partials are NULL, then they are equal.
869 // Otherwise they are not equal.
870 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
871 else if (type_is)
872 {
873 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
874 // strings are considered the same if their value is
875 // the same
876 n1 = tv_equal(typ1, typ2, ic, FALSE);
877 else if (typ1->v_type == VAR_PARTIAL
878 && typ2->v_type == VAR_PARTIAL)
879 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
880 else
881 n1 = FALSE;
882 }
883 else
884 n1 = tv_equal(typ1, typ2, ic, FALSE);
885 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
886 n1 = !n1;
887 }
888
889#ifdef FEAT_FLOAT
890 // If one of the two variables is a float, compare as a float.
891 // When using "=~" or "!~", always compare as string.
892 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
893 && type != EXPR_MATCH && type != EXPR_NOMATCH)
894 {
895 float_T f1, f2;
896
897 f1 = tv_get_float(typ1);
898 f2 = tv_get_float(typ2);
899 n1 = FALSE;
900 switch (type)
901 {
902 case EXPR_IS:
903 case EXPR_EQUAL: n1 = (f1 == f2); break;
904 case EXPR_ISNOT:
905 case EXPR_NEQUAL: n1 = (f1 != f2); break;
906 case EXPR_GREATER: n1 = (f1 > f2); break;
907 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
908 case EXPR_SMALLER: n1 = (f1 < f2); break;
909 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
910 case EXPR_UNKNOWN:
911 case EXPR_MATCH:
912 default: break; // avoid gcc warning
913 }
914 }
915#endif
916
917 // If one of the two variables is a number, compare as a number.
918 // When using "=~" or "!~", always compare as string.
919 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
920 && type != EXPR_MATCH && type != EXPR_NOMATCH)
921 {
922 n1 = tv_get_number(typ1);
923 n2 = tv_get_number(typ2);
924 switch (type)
925 {
926 case EXPR_IS:
927 case EXPR_EQUAL: n1 = (n1 == n2); break;
928 case EXPR_ISNOT:
929 case EXPR_NEQUAL: n1 = (n1 != n2); break;
930 case EXPR_GREATER: n1 = (n1 > n2); break;
931 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
932 case EXPR_SMALLER: n1 = (n1 < n2); break;
933 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
934 case EXPR_UNKNOWN:
935 case EXPR_MATCH:
936 default: break; // avoid gcc warning
937 }
938 }
Bram Moolenaarcff40ff2021-01-09 16:21:37 +0100939 else if (in_vim9script() && (typ1->v_type == VAR_BOOL
Bram Moolenaar0c357522021-07-18 14:43:43 +0200940 || typ2->v_type == VAR_BOOL
941 || (typ1->v_type == VAR_SPECIAL
942 && typ2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +0100943 {
944 if (typ1->v_type != typ2->v_type)
945 {
946 semsg(_(e_cannot_compare_str_with_str),
947 vartype_name(typ1->v_type), vartype_name(typ2->v_type));
948 clear_tv(typ1);
949 return FAIL;
950 }
951 n1 = typ1->vval.v_number;
952 n2 = typ2->vval.v_number;
953 switch (type)
954 {
955 case EXPR_IS:
956 case EXPR_EQUAL: n1 = (n1 == n2); break;
957 case EXPR_ISNOT:
958 case EXPR_NEQUAL: n1 = (n1 != n2); break;
959 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +0200960 semsg(_(e_invalid_operation_for_str),
961 vartype_name(typ1->v_type));
Bram Moolenaarcff40ff2021-01-09 16:21:37 +0100962 clear_tv(typ1);
963 return FAIL;
964 }
965 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200966 else
967 {
Bram Moolenaar0c357522021-07-18 14:43:43 +0200968 if (in_vim9script()
969 && ((typ1->v_type != VAR_STRING && typ1->v_type != VAR_SPECIAL)
970 || (typ2->v_type != VAR_STRING && typ2->v_type != VAR_SPECIAL)))
971 {
972 semsg(_(e_cannot_compare_str_with_str),
973 vartype_name(typ1->v_type), vartype_name(typ2->v_type));
974 clear_tv(typ1);
975 return FAIL;
976 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200977 s1 = tv_get_string_buf(typ1, buf1);
978 s2 = tv_get_string_buf(typ2, buf2);
979 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
980 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
981 else
982 i = 0;
983 n1 = FALSE;
984 switch (type)
985 {
986 case EXPR_IS:
987 case EXPR_EQUAL: n1 = (i == 0); break;
988 case EXPR_ISNOT:
989 case EXPR_NEQUAL: n1 = (i != 0); break;
990 case EXPR_GREATER: n1 = (i > 0); break;
991 case EXPR_GEQUAL: n1 = (i >= 0); break;
992 case EXPR_SMALLER: n1 = (i < 0); break;
993 case EXPR_SEQUAL: n1 = (i <= 0); break;
994
995 case EXPR_MATCH:
996 case EXPR_NOMATCH:
997 n1 = pattern_match(s2, s1, ic);
998 if (type == EXPR_NOMATCH)
999 n1 = !n1;
1000 break;
1001
1002 default: break; // avoid gcc warning
1003 }
1004 }
1005 clear_tv(typ1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001006 if (in_vim9script())
1007 {
1008 typ1->v_type = VAR_BOOL;
1009 typ1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
1010 }
1011 else
1012 {
1013 typ1->v_type = VAR_NUMBER;
1014 typ1->vval.v_number = n1;
1015 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001016
1017 return OK;
1018}
1019
Bram Moolenaar34453202021-01-31 13:08:38 +01001020/*
1021 * Convert any type to a string, never give an error.
1022 * When "quotes" is TRUE add quotes to a string.
1023 * Returns an allocated string.
1024 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001025 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001026typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001027{
1028 char_u *tofree;
1029 char_u numbuf[NUMBUFLEN];
1030 char_u *ret = NULL;
1031
1032 if (arg == NULL)
1033 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001034 if (!quotes && arg->v_type == VAR_STRING)
1035 {
1036 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1037 : arg->vval.v_string);
1038 }
1039 else
1040 {
1041 ret = tv2string(arg, &tofree, numbuf, 0);
1042 // Make a copy if we have a value but it's not in allocated memory.
1043 if (ret != NULL && tofree == NULL)
1044 ret = vim_strsave(ret);
1045 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001046 return ret;
1047}
1048
1049/*
1050 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1051 * or it refers to a List or Dictionary that is locked.
1052 */
1053 int
1054tv_islocked(typval_T *tv)
1055{
1056 return (tv->v_lock & VAR_LOCKED)
1057 || (tv->v_type == VAR_LIST
1058 && tv->vval.v_list != NULL
1059 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1060 || (tv->v_type == VAR_DICT
1061 && tv->vval.v_dict != NULL
1062 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1063}
1064
1065 static int
1066func_equal(
1067 typval_T *tv1,
1068 typval_T *tv2,
1069 int ic) // ignore case
1070{
1071 char_u *s1, *s2;
1072 dict_T *d1, *d2;
1073 int a1, a2;
1074 int i;
1075
1076 // empty and NULL function name considered the same
1077 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1078 : partial_name(tv1->vval.v_partial);
1079 if (s1 != NULL && *s1 == NUL)
1080 s1 = NULL;
1081 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1082 : partial_name(tv2->vval.v_partial);
1083 if (s2 != NULL && *s2 == NUL)
1084 s2 = NULL;
1085 if (s1 == NULL || s2 == NULL)
1086 {
1087 if (s1 != s2)
1088 return FALSE;
1089 }
1090 else if (STRCMP(s1, s2) != 0)
1091 return FALSE;
1092
1093 // empty dict and NULL dict is different
1094 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1095 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1096 if (d1 == NULL || d2 == NULL)
1097 {
1098 if (d1 != d2)
1099 return FALSE;
1100 }
1101 else if (!dict_equal(d1, d2, ic, TRUE))
1102 return FALSE;
1103
1104 // empty list and no list considered the same
1105 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1106 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1107 if (a1 != a2)
1108 return FALSE;
1109 for (i = 0; i < a1; ++i)
1110 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1111 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1112 return FALSE;
1113
1114 return TRUE;
1115}
1116
1117/*
1118 * Return TRUE if "tv1" and "tv2" have the same value.
1119 * Compares the items just like "==" would compare them, but strings and
1120 * numbers are different. Floats and numbers are also different.
1121 */
1122 int
1123tv_equal(
1124 typval_T *tv1,
1125 typval_T *tv2,
1126 int ic, // ignore case
1127 int recursive) // TRUE when used recursively
1128{
1129 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1130 char_u *s1, *s2;
1131 static int recursive_cnt = 0; // catch recursive loops
1132 int r;
1133 static int tv_equal_recurse_limit;
1134
1135 // Catch lists and dicts that have an endless loop by limiting
1136 // recursiveness to a limit. We guess they are equal then.
1137 // A fixed limit has the problem of still taking an awful long time.
1138 // Reduce the limit every time running into it. That should work fine for
1139 // deeply linked structures that are not recursively linked and catch
1140 // recursiveness quickly.
1141 if (!recursive)
1142 tv_equal_recurse_limit = 1000;
1143 if (recursive_cnt >= tv_equal_recurse_limit)
1144 {
1145 --tv_equal_recurse_limit;
1146 return TRUE;
1147 }
1148
1149 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1150 // arguments.
1151 if ((tv1->v_type == VAR_FUNC
1152 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1153 && (tv2->v_type == VAR_FUNC
1154 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1155 {
1156 ++recursive_cnt;
1157 r = func_equal(tv1, tv2, ic);
1158 --recursive_cnt;
1159 return r;
1160 }
1161
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001162 if (tv1->v_type != tv2->v_type
1163 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1164 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001165 return FALSE;
1166
1167 switch (tv1->v_type)
1168 {
1169 case VAR_LIST:
1170 ++recursive_cnt;
1171 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1172 --recursive_cnt;
1173 return r;
1174
1175 case VAR_DICT:
1176 ++recursive_cnt;
1177 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1178 --recursive_cnt;
1179 return r;
1180
1181 case VAR_BLOB:
1182 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1183
1184 case VAR_NUMBER:
1185 case VAR_BOOL:
1186 case VAR_SPECIAL:
1187 return tv1->vval.v_number == tv2->vval.v_number;
1188
1189 case VAR_STRING:
1190 s1 = tv_get_string_buf(tv1, buf1);
1191 s2 = tv_get_string_buf(tv2, buf2);
1192 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1193
1194 case VAR_FLOAT:
1195#ifdef FEAT_FLOAT
1196 return tv1->vval.v_float == tv2->vval.v_float;
1197#endif
1198 case VAR_JOB:
1199#ifdef FEAT_JOB_CHANNEL
1200 return tv1->vval.v_job == tv2->vval.v_job;
1201#endif
1202 case VAR_CHANNEL:
1203#ifdef FEAT_JOB_CHANNEL
1204 return tv1->vval.v_channel == tv2->vval.v_channel;
1205#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001206 case VAR_INSTR:
1207 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001208
1209 case VAR_PARTIAL:
1210 return tv1->vval.v_partial == tv2->vval.v_partial;
1211
1212 case VAR_FUNC:
1213 return tv1->vval.v_string == tv2->vval.v_string;
1214
1215 case VAR_UNKNOWN:
1216 case VAR_ANY:
1217 case VAR_VOID:
1218 break;
1219 }
1220
1221 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1222 // does not equal anything, not even itself.
1223 return FALSE;
1224}
1225
1226/*
1227 * Get an option value.
1228 * "arg" points to the '&' or '+' before the option name.
1229 * "arg" is advanced to character after the option name.
1230 * Return OK or FAIL.
1231 */
1232 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001233eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001234 char_u **arg,
1235 typval_T *rettv, // when NULL, only check if option exists
1236 int evaluate)
1237{
1238 char_u *option_end;
1239 long numval;
1240 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001241 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001242 int c;
1243 int working = (**arg == '+'); // has("+option")
1244 int ret = OK;
1245 int opt_flags;
1246
1247 // Isolate the option name and find its value.
1248 option_end = find_option_end(arg, &opt_flags);
1249 if (option_end == NULL)
1250 {
1251 if (rettv != NULL)
1252 semsg(_("E112: Option name missing: %s"), *arg);
1253 return FAIL;
1254 }
1255
1256 if (!evaluate)
1257 {
1258 *arg = option_end;
1259 return OK;
1260 }
1261
1262 c = *option_end;
1263 *option_end = NUL;
1264 opt_type = get_option_value(*arg, &numval,
1265 rettv == NULL ? NULL : &stringval, opt_flags);
1266
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001267 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001268 {
1269 if (rettv != NULL)
1270 semsg(_(e_unknown_option), *arg);
1271 ret = FAIL;
1272 }
1273 else if (rettv != NULL)
1274 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001275 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001276 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001277 {
1278 rettv->v_type = VAR_STRING;
1279 rettv->vval.v_string = NULL;
1280 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001281 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001282 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001283 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1284 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001285 rettv->vval.v_number = 0;
1286 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001287 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001288 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001289 if (in_vim9script() && opt_type == gov_bool)
1290 {
1291 rettv->v_type = VAR_BOOL;
1292 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1293 }
1294 else
1295 {
1296 rettv->v_type = VAR_NUMBER;
1297 rettv->vval.v_number = numval;
1298 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001299 }
1300 else // string option
1301 {
1302 rettv->v_type = VAR_STRING;
1303 rettv->vval.v_string = stringval;
1304 }
1305 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001306 else if (working && (opt_type == gov_hidden_bool
1307 || opt_type == gov_hidden_number
1308 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001309 ret = FAIL;
1310
1311 *option_end = c; // put back for error messages
1312 *arg = option_end;
1313
1314 return ret;
1315}
1316
1317/*
1318 * Allocate a variable for a number constant. Also deals with "0z" for blob.
1319 * Return OK or FAIL.
1320 */
1321 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001322eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001323 char_u **arg,
1324 typval_T *rettv,
1325 int evaluate,
1326 int want_string UNUSED)
1327{
1328 int len;
1329#ifdef FEAT_FLOAT
1330 char_u *p;
1331 int get_float = FALSE;
1332
1333 // We accept a float when the format matches
1334 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
1335 // strict to avoid backwards compatibility problems.
1336 // With script version 2 and later the leading digit can be
1337 // omitted.
1338 // Don't look for a float after the "." operator, so that
1339 // ":let vers = 1.2.3" doesn't fail.
1340 if (**arg == '.')
1341 p = *arg;
1342 else
1343 p = skipdigits(*arg + 1);
1344 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
1345 {
1346 get_float = TRUE;
1347 p = skipdigits(p + 2);
1348 if (*p == 'e' || *p == 'E')
1349 {
1350 ++p;
1351 if (*p == '-' || *p == '+')
1352 ++p;
1353 if (!vim_isdigit(*p))
1354 get_float = FALSE;
1355 else
1356 p = skipdigits(p + 1);
1357 }
1358 if (ASCII_ISALPHA(*p) || *p == '.')
1359 get_float = FALSE;
1360 }
1361 if (get_float)
1362 {
1363 float_T f;
1364
1365 *arg += string2float(*arg, &f);
1366 if (evaluate)
1367 {
1368 rettv->v_type = VAR_FLOAT;
1369 rettv->vval.v_float = f;
1370 }
1371 }
1372 else
1373#endif
1374 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
1375 {
1376 char_u *bp;
1377 blob_T *blob = NULL; // init for gcc
1378
1379 // Blob constant: 0z0123456789abcdef
1380 if (evaluate)
1381 blob = blob_alloc();
1382 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
1383 {
1384 if (!vim_isxdigit(bp[1]))
1385 {
1386 if (blob != NULL)
1387 {
1388 emsg(_("E973: Blob literal should have an even number of hex characters"));
1389 ga_clear(&blob->bv_ga);
1390 VIM_CLEAR(blob);
1391 }
1392 return FAIL;
1393 }
1394 if (blob != NULL)
1395 ga_append(&blob->bv_ga,
1396 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
1397 if (bp[2] == '.' && vim_isxdigit(bp[3]))
1398 ++bp;
1399 }
1400 if (blob != NULL)
1401 rettv_blob_set(rettv, blob);
1402 *arg = bp;
1403 }
1404 else
1405 {
1406 varnumber_T n;
1407
1408 // decimal, hex or octal number
1409 vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4
1410 ? STR2NR_NO_OCT + STR2NR_QUOTE
1411 : STR2NR_ALL, &n, NULL, 0, TRUE);
1412 if (len == 0)
1413 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001414 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001415 return FAIL;
1416 }
1417 *arg += len;
1418 if (evaluate)
1419 {
1420 rettv->v_type = VAR_NUMBER;
1421 rettv->vval.v_number = n;
1422 }
1423 }
1424 return OK;
1425}
1426
1427/*
1428 * Allocate a variable for a string constant.
1429 * Return OK or FAIL.
1430 */
1431 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001432eval_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001433{
1434 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001435 char_u *end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001436 int extra = 0;
1437 int len;
1438
1439 // Find the end of the string, skipping backslashed characters.
1440 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
1441 {
1442 if (*p == '\\' && p[1] != NUL)
1443 {
1444 ++p;
1445 // A "\<x>" form occupies at least 4 characters, and produces up
1446 // to 21 characters (3 * 6 for the char and 3 for a modifier):
1447 // reserve space for 18 extra.
1448 // Each byte in the char could be encoded as K_SPECIAL K_EXTRA x.
1449 if (*p == '<')
1450 extra += 18;
1451 }
1452 }
1453
1454 if (*p != '"')
1455 {
1456 semsg(_("E114: Missing quote: %s"), *arg);
1457 return FAIL;
1458 }
1459
1460 // If only parsing, set *arg and return here
1461 if (!evaluate)
1462 {
1463 *arg = p + 1;
1464 return OK;
1465 }
1466
1467 // Copy the string into allocated memory, handling backslashed
1468 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001469 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001470 len = (int)(p - *arg + extra);
1471 rettv->vval.v_string = alloc(len);
1472 if (rettv->vval.v_string == NULL)
1473 return FAIL;
1474 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001475
1476 for (p = *arg + 1; *p != NUL && *p != '"'; )
1477 {
1478 if (*p == '\\')
1479 {
1480 switch (*++p)
1481 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001482 case 'b': *end++ = BS; ++p; break;
1483 case 'e': *end++ = ESC; ++p; break;
1484 case 'f': *end++ = FF; ++p; break;
1485 case 'n': *end++ = NL; ++p; break;
1486 case 'r': *end++ = CAR; ++p; break;
1487 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001488
1489 case 'X': // hex: "\x1", "\x12"
1490 case 'x':
1491 case 'u': // Unicode: "\u0023"
1492 case 'U':
1493 if (vim_isxdigit(p[1]))
1494 {
1495 int n, nr;
1496 int c = toupper(*p);
1497
1498 if (c == 'X')
1499 n = 2;
1500 else if (*p == 'u')
1501 n = 4;
1502 else
1503 n = 8;
1504 nr = 0;
1505 while (--n >= 0 && vim_isxdigit(p[1]))
1506 {
1507 ++p;
1508 nr = (nr << 4) + hex2nr(*p);
1509 }
1510 ++p;
1511 // For "\u" store the number according to
1512 // 'encoding'.
1513 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001514 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001515 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001516 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001517 }
1518 break;
1519
1520 // octal: "\1", "\12", "\123"
1521 case '0':
1522 case '1':
1523 case '2':
1524 case '3':
1525 case '4':
1526 case '5':
1527 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001528 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001529 if (*p >= '0' && *p <= '7')
1530 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001531 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001532 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001533 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001534 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001535 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001536 break;
1537
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001538 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001539 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001540 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001541 int flags = FSK_KEYCODE | FSK_IN_STRING;
1542
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001543 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001544 flags |= FSK_SIMPLIFY;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001545 extra = trans_special(&p, end, flags, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001546 if (extra != 0)
1547 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001548 end += extra;
1549 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001550 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001551 break;
1552 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001553 }
1554 // FALLTHROUGH
1555
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001556 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001557 break;
1558 }
1559 }
1560 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001561 MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001562 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001563 *end = NUL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001564 if (*p != NUL) // just in case
1565 ++p;
1566 *arg = p;
1567
1568 return OK;
1569}
1570
1571/*
1572 * Allocate a variable for a 'str''ing' constant.
1573 * Return OK or FAIL.
1574 */
1575 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001576eval_lit_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001577{
1578 char_u *p;
1579 char_u *str;
1580 int reduce = 0;
1581
1582 // Find the end of the string, skipping ''.
1583 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
1584 {
1585 if (*p == '\'')
1586 {
1587 if (p[1] != '\'')
1588 break;
1589 ++reduce;
1590 ++p;
1591 }
1592 }
1593
1594 if (*p != '\'')
1595 {
1596 semsg(_("E115: Missing quote: %s"), *arg);
1597 return FAIL;
1598 }
1599
1600 // If only parsing return after setting "*arg"
1601 if (!evaluate)
1602 {
1603 *arg = p + 1;
1604 return OK;
1605 }
1606
1607 // Copy the string into allocated memory, handling '' to ' reduction.
1608 str = alloc((p - *arg) - reduce);
1609 if (str == NULL)
1610 return FAIL;
1611 rettv->v_type = VAR_STRING;
1612 rettv->vval.v_string = str;
1613
1614 for (p = *arg + 1; *p != NUL; )
1615 {
1616 if (*p == '\'')
1617 {
1618 if (p[1] != '\'')
1619 break;
1620 ++p;
1621 }
1622 MB_COPY_CHAR(p, str);
1623 }
1624 *str = NUL;
1625 *arg = p + 1;
1626
1627 return OK;
1628}
1629
1630/*
1631 * Return a string with the string representation of a variable.
1632 * If the memory is allocated "tofree" is set to it, otherwise NULL.
1633 * "numbuf" is used for a number.
1634 * Puts quotes around strings, so that they can be parsed back by eval().
1635 * May return NULL.
1636 */
1637 char_u *
1638tv2string(
1639 typval_T *tv,
1640 char_u **tofree,
1641 char_u *numbuf,
1642 int copyID)
1643{
1644 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
1645}
1646
1647/*
1648 * Get the value of an environment variable.
1649 * "arg" is pointing to the '$'. It is advanced to after the name.
1650 * If the environment variable was not set, silently assume it is empty.
1651 * Return FAIL if the name is invalid.
1652 */
1653 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001654eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001655{
1656 char_u *string = NULL;
1657 int len;
1658 int cc;
1659 char_u *name;
1660 int mustfree = FALSE;
1661
1662 ++*arg;
1663 name = *arg;
1664 len = get_env_len(arg);
1665 if (evaluate)
1666 {
1667 if (len == 0)
1668 return FAIL; // invalid empty name
1669
1670 cc = name[len];
1671 name[len] = NUL;
1672 // first try vim_getenv(), fast for normal environment vars
1673 string = vim_getenv(name, &mustfree);
1674 if (string != NULL && *string != NUL)
1675 {
1676 if (!mustfree)
1677 string = vim_strsave(string);
1678 }
1679 else
1680 {
1681 if (mustfree)
1682 vim_free(string);
1683
1684 // next try expanding things like $VIM and ${HOME}
1685 string = expand_env_save(name - 1);
1686 if (string != NULL && *string == '$')
1687 VIM_CLEAR(string);
1688 }
1689 name[len] = cc;
1690
1691 rettv->v_type = VAR_STRING;
1692 rettv->vval.v_string = string;
1693 }
1694
1695 return OK;
1696}
1697
1698/*
1699 * Get the lnum from the first argument.
1700 * Also accepts ".", "$", etc., but that only works for the current buffer.
1701 * Returns -1 on error.
1702 */
1703 linenr_T
1704tv_get_lnum(typval_T *argvars)
1705{
Bram Moolenaar9a963372020-12-21 21:58:46 +01001706 linenr_T lnum = -1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001707
Bram Moolenaar56acb092020-08-16 14:48:19 +02001708 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
1709 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02001710 if (lnum <= 0 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001711 {
1712 int fnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001713 pos_T *fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001714
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02001715 // no valid number, try using arg like line()
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001716 if (fp != NULL)
1717 lnum = fp->lnum;
1718 }
1719 return lnum;
1720}
1721
1722/*
1723 * Get the lnum from the first argument.
1724 * Also accepts "$", then "buf" is used.
1725 * Returns 0 on error.
1726 */
1727 linenr_T
1728tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1729{
1730 if (argvars[0].v_type == VAR_STRING
1731 && argvars[0].vval.v_string != NULL
1732 && argvars[0].vval.v_string[0] == '$'
1733 && buf != NULL)
1734 return buf->b_ml.ml_line_count;
1735 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1736}
1737
1738/*
1739 * Get buffer by number or pattern.
1740 */
1741 buf_T *
1742tv_get_buf(typval_T *tv, int curtab_only)
1743{
1744 char_u *name = tv->vval.v_string;
1745 buf_T *buf;
1746
1747 if (tv->v_type == VAR_NUMBER)
1748 return buflist_findnr((int)tv->vval.v_number);
1749 if (tv->v_type != VAR_STRING)
1750 return NULL;
1751 if (name == NULL || *name == NUL)
1752 return curbuf;
1753 if (name[0] == '$' && name[1] == NUL)
1754 return lastbuf;
1755
1756 buf = buflist_find_by_name(name, curtab_only);
1757
1758 // If not found, try expanding the name, like done for bufexists().
1759 if (buf == NULL)
1760 buf = find_buffer(tv);
1761
1762 return buf;
1763}
1764
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02001765/*
1766 * Like tv_get_buf() but give an error message is the type is wrong.
1767 */
1768 buf_T *
1769tv_get_buf_from_arg(typval_T *tv)
1770{
1771 buf_T *buf;
1772
1773 ++emsg_off;
1774 buf = tv_get_buf(tv, FALSE);
1775 --emsg_off;
1776 if (buf == NULL
1777 && tv->v_type != VAR_NUMBER
1778 && tv->v_type != VAR_STRING)
1779 // issue errmsg for type error
1780 (void)tv_get_number(tv);
1781 return buf;
1782}
1783
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001784#endif // FEAT_EVAL