blob: 12a741ec220c45ba2c05996c287e40cc8fa7a418 [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
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000194 emsg(_(e_using_float_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200195 break;
196#endif
197 case VAR_FUNC:
198 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000199 emsg(_(e_using_funcref_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200200 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:
Bram Moolenaar677658a2022-01-05 16:09:06 +0000212 emsg(_(e_using_list_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200213 break;
214 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000215 emsg(_(e_using_dictionary_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200216 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
Bram Moolenaard88be5b2022-01-04 19:57:55 +0000224 emsg(_(e_using_special_as_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
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000230 emsg(_(e_using_job_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200231 break;
232#endif
233 case VAR_CHANNEL:
234#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000235 emsg(_(e_using_channel_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200236 break;
237#endif
238 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000239 emsg(_(e_using_blob_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200240 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 Moolenaar28fbbea2021-12-22 21:40:33 +0000300 static float_T
301tv_get_float_chk(typval_T *varp, int *error)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200302{
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:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000311 emsg(_(e_using_funcref_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200312 break;
313 case VAR_STRING:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000314 emsg(_(e_using_string_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200315 break;
316 case VAR_LIST:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000317 emsg(_(e_using_list_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200318 break;
319 case VAR_DICT:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000320 emsg(_(e_using_dictionary_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200321 break;
322 case VAR_BOOL:
Bram Moolenaarb2810f12022-01-08 21:38:52 +0000323 emsg(_(e_using_boolean_value_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200324 break;
325 case VAR_SPECIAL:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000326 emsg(_(e_using_special_value_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200327 break;
328 case VAR_JOB:
329# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000330 emsg(_(e_using_job_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200331 break;
332# endif
333 case VAR_CHANNEL:
334# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000335 emsg(_(e_using_channel_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200336 break;
337# endif
338 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000339 emsg(_(e_using_blob_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200340 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 }
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000350 if (error != NULL)
351 *error = TRUE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200352 return 0;
353}
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000354
355 float_T
356tv_get_float(typval_T *varp)
357{
358 return tv_get_float_chk(varp, NULL);
359}
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200360#endif
361
362/*
Ernie Rael51d04d12022-05-04 15:40:22 +0100363 * Give an error and return FAIL unless "args[idx]" is unknown
364 */
365 int
366check_for_unknown_arg(typval_T *args, int idx)
367{
368 if (args[idx].v_type != VAR_UNKNOWN)
369 {
370 semsg(_(e_too_many_arguments), idx + 1);
371 return FAIL;
372 }
373 return OK;
374}
375
376/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200377 * Give an error and return FAIL unless "args[idx]" is a string.
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100378 */
379 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100380check_for_string_arg(typval_T *args, int idx)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100381{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100382 if (args[idx].v_type != VAR_STRING)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100383 {
rbtnnddc80af2021-12-17 18:01:31 +0000384 semsg(_(e_string_required_for_argument_nr), idx + 1);
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100385 return FAIL;
386 }
387 return OK;
388}
389
390/*
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100391 * Give an error and return FAIL unless "args[idx]" is a non-empty string.
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100392 */
393 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100394check_for_nonempty_string_arg(typval_T *args, int idx)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100395{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100396 if (check_for_string_arg(args, idx) == FAIL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100397 return FAIL;
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100398 if (args[idx].vval.v_string == NULL || *args[idx].vval.v_string == NUL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100399 {
Bram Moolenaare8e30782021-04-10 21:46:05 +0200400 semsg(_(e_non_empty_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100401 return FAIL;
402 }
403 return OK;
404}
405
406/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200407 * Check for an optional string argument at 'idx'
408 */
409 int
410check_for_opt_string_arg(typval_T *args, int idx)
411{
412 return (args[idx].v_type == VAR_UNKNOWN
413 || check_for_string_arg(args, idx) != FAIL);
414}
415
416/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200417 * Give an error and return FAIL unless "args[idx]" is a number.
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200418 */
419 int
420check_for_number_arg(typval_T *args, int idx)
421{
422 if (args[idx].v_type != VAR_NUMBER)
423 {
rbtnnddc80af2021-12-17 18:01:31 +0000424 semsg(_(e_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200425 return FAIL;
426 }
427 return OK;
428}
429
430/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200431 * Check for an optional number argument at 'idx'
432 */
433 int
434check_for_opt_number_arg(typval_T *args, int idx)
435{
436 return (args[idx].v_type == VAR_UNKNOWN
437 || check_for_number_arg(args, idx) != FAIL);
438}
439
440/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200441 * Give an error and return FAIL unless "args[idx]" is a float or a number.
442 */
443 int
444check_for_float_or_nr_arg(typval_T *args, int idx)
445{
446 if (args[idx].v_type != VAR_FLOAT && args[idx].v_type != VAR_NUMBER)
447 {
rbtnnddc80af2021-12-17 18:01:31 +0000448 semsg(_(e_float_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200449 return FAIL;
450 }
451 return OK;
452}
453
454/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200455 * Give an error and return FAIL unless "args[idx]" is a bool.
456 */
457 int
458check_for_bool_arg(typval_T *args, int idx)
459{
460 if (args[idx].v_type != VAR_BOOL
461 && !(args[idx].v_type == VAR_NUMBER
462 && (args[idx].vval.v_number == 0
463 || args[idx].vval.v_number == 1)))
464 {
rbtnnddc80af2021-12-17 18:01:31 +0000465 semsg(_(e_bool_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200466 return FAIL;
467 }
468 return OK;
469}
470
471/*
Bram Moolenaara29856f2021-09-13 21:36:27 +0200472 * Check for an optional bool argument at 'idx'.
473 * Return FAIL if the type is wrong.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200474 */
475 int
476check_for_opt_bool_arg(typval_T *args, int idx)
477{
Bram Moolenaara29856f2021-09-13 21:36:27 +0200478 if (args[idx].v_type == VAR_UNKNOWN)
479 return OK;
480 return check_for_bool_arg(args, idx);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200481}
482
483/*
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200484 * Give an error and return FAIL unless "args[idx]" is a blob.
485 */
486 int
487check_for_blob_arg(typval_T *args, int idx)
488{
489 if (args[idx].v_type != VAR_BLOB)
490 {
rbtnnddc80af2021-12-17 18:01:31 +0000491 semsg(_(e_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200492 return FAIL;
493 }
494 return OK;
495}
496
497/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200498 * Give an error and return FAIL unless "args[idx]" is a list.
499 */
500 int
501check_for_list_arg(typval_T *args, int idx)
502{
503 if (args[idx].v_type != VAR_LIST)
504 {
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200505 semsg(_(e_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200506 return FAIL;
507 }
508 return OK;
509}
510
511/*
Bram Moolenaard83392a2022-09-01 12:22:46 +0100512 * Give an error and return FAIL unless "args[idx]" is a non-NULL list.
513 */
514 int
515check_for_nonnull_list_arg(typval_T *args, int idx)
516{
517 if (check_for_list_arg(args, idx) == FAIL)
518 return FAIL;
519
520 if (args[idx].vval.v_list == NULL)
521 {
522 semsg(_(e_non_null_list_required_for_argument_nr), idx + 1);
523 return FAIL;
524 }
525 return OK;
526}
527
528/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200529 * Check for an optional list argument at 'idx'
530 */
531 int
532check_for_opt_list_arg(typval_T *args, int idx)
533{
534 return (args[idx].v_type == VAR_UNKNOWN
535 || check_for_list_arg(args, idx) != FAIL);
536}
537
538/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200539 * Give an error and return FAIL unless "args[idx]" is a dict.
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200540 */
541 int
542check_for_dict_arg(typval_T *args, int idx)
543{
544 if (args[idx].v_type != VAR_DICT)
545 {
rbtnnddc80af2021-12-17 18:01:31 +0000546 semsg(_(e_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200547 return FAIL;
548 }
549 return OK;
550}
551
552/*
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100553 * Give an error and return FAIL unless "args[idx]" is a non-NULL dict.
554 */
555 int
556check_for_nonnull_dict_arg(typval_T *args, int idx)
557{
558 if (check_for_dict_arg(args, idx) == FAIL)
559 return FAIL;
560
561 if (args[idx].vval.v_dict == NULL)
562 {
563 semsg(_(e_non_null_dict_required_for_argument_nr), idx + 1);
564 return FAIL;
565 }
566 return OK;
567}
568
569/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200570 * Check for an optional dict argument at 'idx'
571 */
572 int
573check_for_opt_dict_arg(typval_T *args, int idx)
574{
575 return (args[idx].v_type == VAR_UNKNOWN
576 || check_for_dict_arg(args, idx) != FAIL);
577}
578
Dominique Pelle748b3082022-01-08 12:41:16 +0000579#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200580/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200581 * Give an error and return FAIL unless "args[idx]" is a channel or a job.
582 */
583 int
584check_for_chan_or_job_arg(typval_T *args, int idx)
585{
586 if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
587 {
rbtnnddc80af2021-12-17 18:01:31 +0000588 semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200589 return FAIL;
590 }
591 return OK;
592}
593
594/*
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200595 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
596 * job.
597 */
598 int
599check_for_opt_chan_or_job_arg(typval_T *args, int idx)
600{
601 return (args[idx].v_type == VAR_UNKNOWN
602 || check_for_chan_or_job_arg(args, idx) != FAIL);
603}
604
605/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200606 * Give an error and return FAIL unless "args[idx]" is a job.
607 */
608 int
609check_for_job_arg(typval_T *args, int idx)
610{
611 if (args[idx].v_type != VAR_JOB)
612 {
rbtnnddc80af2021-12-17 18:01:31 +0000613 semsg(_(e_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200614 return FAIL;
615 }
616 return OK;
617}
618
619/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200620 * Check for an optional job argument at 'idx'.
621 */
622 int
623check_for_opt_job_arg(typval_T *args, int idx)
624{
625 return (args[idx].v_type == VAR_UNKNOWN
626 || check_for_job_arg(args, idx) != FAIL);
627}
Dominique Pelle748b3082022-01-08 12:41:16 +0000628#endif
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200629
630/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200631 * Give an error and return FAIL unless "args[idx]" is a string or
632 * a number.
633 */
634 int
635check_for_string_or_number_arg(typval_T *args, int idx)
636{
637 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
638 {
rbtnnddc80af2021-12-17 18:01:31 +0000639 semsg(_(e_string_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200640 return FAIL;
641 }
642 return OK;
643}
644
645/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200646 * Check for an optional string or number argument at 'idx'.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200647 */
648 int
649check_for_opt_string_or_number_arg(typval_T *args, int idx)
650{
651 return (args[idx].v_type == VAR_UNKNOWN
652 || check_for_string_or_number_arg(args, idx) != FAIL);
653}
654
655/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200656 * Give an error and return FAIL unless "args[idx]" is a buffer number.
657 * Buffer number can be a number or a string.
658 */
659 int
660check_for_buffer_arg(typval_T *args, int idx)
661{
662 return check_for_string_or_number_arg(args, idx);
663}
664
665/*
666 * Check for an optional buffer argument at 'idx'
667 */
668 int
669check_for_opt_buffer_arg(typval_T *args, int idx)
670{
671 return (args[idx].v_type == VAR_UNKNOWN
672 || check_for_buffer_arg(args, idx));
673}
674
675/*
676 * Give an error and return FAIL unless "args[idx]" is a line number.
677 * Line number can be a number or a string.
678 */
679 int
680check_for_lnum_arg(typval_T *args, int idx)
681{
682 return check_for_string_or_number_arg(args, idx);
683}
684
685/*
686 * Check for an optional line number argument at 'idx'
687 */
688 int
689check_for_opt_lnum_arg(typval_T *args, int idx)
690{
691 return (args[idx].v_type == VAR_UNKNOWN
692 || check_for_lnum_arg(args, idx));
693}
694
Dominique Pelle748b3082022-01-08 12:41:16 +0000695#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200696/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200697 * Give an error and return FAIL unless "args[idx]" is a string or a blob.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200698 */
699 int
700check_for_string_or_blob_arg(typval_T *args, int idx)
701{
702 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
703 {
rbtnnddc80af2021-12-17 18:01:31 +0000704 semsg(_(e_string_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200705 return FAIL;
706 }
707 return OK;
708}
Dominique Pelle748b3082022-01-08 12:41:16 +0000709#endif
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200710
711/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200712 * Give an error and return FAIL unless "args[idx]" is a string or a list.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200713 */
714 int
715check_for_string_or_list_arg(typval_T *args, int idx)
716{
717 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
718 {
rbtnnddc80af2021-12-17 18:01:31 +0000719 semsg(_(e_string_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200720 return FAIL;
721 }
722 return OK;
723}
724
725/*
rbtnn0ccb5842021-12-18 18:33:46 +0000726 * Give an error and return FAIL unless "args[idx]" is a string, a list or a
727 * blob.
728 */
729 int
730check_for_string_or_list_or_blob_arg(typval_T *args, int idx)
731{
732 if (args[idx].v_type != VAR_STRING
733 && args[idx].v_type != VAR_LIST
734 && args[idx].v_type != VAR_BLOB)
735 {
736 semsg(_(e_string_list_or_blob_required_for_argument_nr), idx + 1);
737 return FAIL;
738 }
739 return OK;
740}
741
742/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200743 * Check for an optional string or list argument at 'idx'
744 */
745 int
746check_for_opt_string_or_list_arg(typval_T *args, int idx)
747{
748 return (args[idx].v_type == VAR_UNKNOWN
749 || check_for_string_or_list_arg(args, idx));
750}
751
752/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200753 * Give an error and return FAIL unless "args[idx]" is a string or a dict.
754 */
755 int
756check_for_string_or_dict_arg(typval_T *args, int idx)
757{
758 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_DICT)
759 {
rbtnnddc80af2021-12-17 18:01:31 +0000760 semsg(_(e_string_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200761 return FAIL;
762 }
763 return OK;
764}
765
766/*
767 * Give an error and return FAIL unless "args[idx]" is a string or a number
768 * or a list.
769 */
770 int
771check_for_string_or_number_or_list_arg(typval_T *args, int idx)
772{
773 if (args[idx].v_type != VAR_STRING
774 && args[idx].v_type != VAR_NUMBER
775 && args[idx].v_type != VAR_LIST)
776 {
rbtnn0ccb5842021-12-18 18:33:46 +0000777 semsg(_(e_string_number_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200778 return FAIL;
779 }
780 return OK;
781}
782
783/*
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200784 * Give an error and return FAIL unless "args[idx]" is an optional string
785 * or number or a list
786 */
787 int
788check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx)
789{
790 return (args[idx].v_type == VAR_UNKNOWN
791 || check_for_string_or_number_or_list_arg(args, idx) != FAIL);
792}
793
794/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200795 * Give an error and return FAIL unless "args[idx]" is a string or a list
796 * or a dict.
797 */
798 int
799check_for_string_or_list_or_dict_arg(typval_T *args, int idx)
800{
801 if (args[idx].v_type != VAR_STRING
802 && args[idx].v_type != VAR_LIST
803 && args[idx].v_type != VAR_DICT)
804 {
rbtnnddc80af2021-12-17 18:01:31 +0000805 semsg(_(e_string_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200806 return FAIL;
807 }
808 return OK;
809}
810
811/*
Bram Moolenaar223d0a62021-12-25 19:29:21 +0000812 * Give an error and return FAIL unless "args[idx]" is a string
813 * or a function reference.
814 */
815 int
816check_for_string_or_func_arg(typval_T *args, int idx)
817{
818 if (args[idx].v_type != VAR_PARTIAL
819 && args[idx].v_type != VAR_FUNC
820 && args[idx].v_type != VAR_STRING)
821 {
822 semsg(_(e_string_or_function_required_for_argument_nr), idx + 1);
823 return FAIL;
824 }
825 return OK;
826}
827
828/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200829 * Give an error and return FAIL unless "args[idx]" is a list or a blob.
830 */
831 int
832check_for_list_or_blob_arg(typval_T *args, int idx)
833{
834 if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
835 {
rbtnn0ccb5842021-12-18 18:33:46 +0000836 semsg(_(e_list_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200837 return FAIL;
838 }
839 return OK;
840}
841
842/*
843 * Give an error and return FAIL unless "args[idx]" is a list or dict
844 */
845 int
846check_for_list_or_dict_arg(typval_T *args, int idx)
847{
848 if (args[idx].v_type != VAR_LIST
849 && args[idx].v_type != VAR_DICT)
850 {
rbtnnddc80af2021-12-17 18:01:31 +0000851 semsg(_(e_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200852 return FAIL;
853 }
854 return OK;
855}
856
857/*
858 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
859 * blob.
860 */
861 int
862check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
863{
864 if (args[idx].v_type != VAR_LIST
865 && args[idx].v_type != VAR_DICT
866 && args[idx].v_type != VAR_BLOB)
867 {
rbtnnddc80af2021-12-17 18:01:31 +0000868 semsg(_(e_list_dict_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200869 return FAIL;
870 }
871 return OK;
872}
873
874/*
Bram Moolenaar2d877592021-12-16 08:21:09 +0000875 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
876 * blob or a string.
877 */
878 int
879check_for_list_or_dict_or_blob_or_string_arg(typval_T *args, int idx)
880{
881 if (args[idx].v_type != VAR_LIST
882 && args[idx].v_type != VAR_DICT
883 && args[idx].v_type != VAR_BLOB
884 && args[idx].v_type != VAR_STRING)
885 {
rbtnnddc80af2021-12-17 18:01:31 +0000886 semsg(_(e_list_dict_blob_or_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2d877592021-12-16 08:21:09 +0000887 return FAIL;
888 }
889 return OK;
890}
891
892/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200893 * Give an error and return FAIL unless "args[idx]" is an optional buffer
894 * number or a dict.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200895 */
896 int
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200897check_for_opt_buffer_or_dict_arg(typval_T *args, int idx)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200898{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200899 if (args[idx].v_type != VAR_UNKNOWN
900 && args[idx].v_type != VAR_STRING
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200901 && args[idx].v_type != VAR_NUMBER
902 && args[idx].v_type != VAR_DICT)
903 {
rbtnnddc80af2021-12-17 18:01:31 +0000904 semsg(_(e_string_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200905 return FAIL;
906 }
907 return OK;
908}
909
910/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200911 * Get the string value of a variable.
912 * If it is a Number variable, the number is converted into a string.
913 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
914 * tv_get_string_buf() uses a given buffer.
915 * If the String variable has never been set, return an empty string.
916 * Never returns NULL;
917 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
918 * NULL on error.
919 */
920 char_u *
921tv_get_string(typval_T *varp)
922{
923 static char_u mybuf[NUMBUFLEN];
924
925 return tv_get_string_buf(varp, mybuf);
926}
927
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100928/*
929 * Like tv_get_string() but don't allow number to string conversion for Vim9.
930 */
931 char_u *
932tv_get_string_strict(typval_T *varp)
933{
934 static char_u mybuf[NUMBUFLEN];
935 char_u *res = tv_get_string_buf_chk_strict(
936 varp, mybuf, in_vim9script());
937
938 return res != NULL ? res : (char_u *)"";
939}
940
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200941 char_u *
942tv_get_string_buf(typval_T *varp, char_u *buf)
943{
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200944 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200945
946 return res != NULL ? res : (char_u *)"";
947}
948
949/*
950 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
951 */
952 char_u *
953tv_get_string_chk(typval_T *varp)
954{
955 static char_u mybuf[NUMBUFLEN];
956
957 return tv_get_string_buf_chk(varp, mybuf);
958}
959
960 char_u *
961tv_get_string_buf_chk(typval_T *varp, char_u *buf)
962{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100963 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
964}
965
966 char_u *
967tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
968{
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200969 switch (varp->v_type)
970 {
971 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100972 if (strict)
973 {
974 emsg(_(e_using_number_as_string));
975 break;
976 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200977 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
978 (varnumber_T)varp->vval.v_number);
979 return buf;
980 case VAR_FUNC:
981 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000982 emsg(_(e_using_funcref_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200983 break;
984 case VAR_LIST:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000985 emsg(_(e_using_list_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200986 break;
987 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000988 emsg(_(e_using_dictionary_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200989 break;
990 case VAR_FLOAT:
991#ifdef FEAT_FLOAT
Bram Moolenaar7a2217b2021-06-06 12:33:49 +0200992 if (strict)
993 {
Bram Moolenaar0699b042022-01-01 16:01:23 +0000994 emsg(_(e_using_float_as_string));
Bram Moolenaar7a2217b2021-06-06 12:33:49 +0200995 break;
996 }
997 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
998 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200999#endif
1000 case VAR_STRING:
1001 if (varp->vval.v_string != NULL)
1002 return varp->vval.v_string;
1003 return (char_u *)"";
1004 case VAR_BOOL:
1005 case VAR_SPECIAL:
1006 STRCPY(buf, get_var_special_name(varp->vval.v_number));
1007 return buf;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001008 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001009 emsg(_(e_using_blob_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001010 break;
1011 case VAR_JOB:
1012#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001013 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001014 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001015 semsg(_(e_using_invalid_value_as_string_str), "job");
1016 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001017 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001018 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001019#endif
1020 break;
1021 case VAR_CHANNEL:
1022#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001023 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001024 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001025 semsg(_(e_using_invalid_value_as_string_str), "channel");
1026 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001027 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001028 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001029#endif
1030 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02001031 case VAR_VOID:
1032 emsg(_(e_cannot_use_void_value));
1033 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001034 case VAR_UNKNOWN:
1035 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001036 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +02001037 semsg(_(e_using_invalid_value_as_string_str),
1038 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001039 break;
1040 }
1041 return NULL;
1042}
1043
1044/*
1045 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
1046 * string() on Dict, List, etc.
1047 */
1048 char_u *
1049tv_stringify(typval_T *varp, char_u *buf)
1050{
1051 if (varp->v_type == VAR_LIST
1052 || varp->v_type == VAR_DICT
1053 || varp->v_type == VAR_BLOB
1054 || varp->v_type == VAR_FUNC
1055 || varp->v_type == VAR_PARTIAL
1056 || varp->v_type == VAR_FLOAT)
1057 {
1058 typval_T tmp;
1059
1060 f_string(varp, &tmp);
1061 tv_get_string_buf(&tmp, buf);
1062 clear_tv(varp);
1063 *varp = tmp;
1064 return tmp.vval.v_string;
1065 }
1066 return tv_get_string_buf(varp, buf);
1067}
1068
1069/*
1070 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
1071 * Also give an error message, using "name" or _("name") when use_gettext is
1072 * TRUE.
1073 */
1074 int
1075tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
1076{
1077 int lock = 0;
1078
1079 switch (tv->v_type)
1080 {
1081 case VAR_BLOB:
1082 if (tv->vval.v_blob != NULL)
1083 lock = tv->vval.v_blob->bv_lock;
1084 break;
1085 case VAR_LIST:
1086 if (tv->vval.v_list != NULL)
1087 lock = tv->vval.v_list->lv_lock;
1088 break;
1089 case VAR_DICT:
1090 if (tv->vval.v_dict != NULL)
1091 lock = tv->vval.v_dict->dv_lock;
1092 break;
1093 default:
1094 break;
1095 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001096 return value_check_lock(tv->v_lock, name, use_gettext)
1097 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001098}
1099
1100/*
1101 * Copy the values from typval_T "from" to typval_T "to".
1102 * When needed allocates string or increases reference count.
1103 * Does not make a copy of a list, blob or dict but copies the reference!
1104 * It is OK for "from" and "to" to point to the same item. This is used to
1105 * make a copy later.
1106 */
1107 void
1108copy_tv(typval_T *from, typval_T *to)
1109{
1110 to->v_type = from->v_type;
1111 to->v_lock = 0;
1112 switch (from->v_type)
1113 {
1114 case VAR_NUMBER:
1115 case VAR_BOOL:
1116 case VAR_SPECIAL:
1117 to->vval.v_number = from->vval.v_number;
1118 break;
1119 case VAR_FLOAT:
1120#ifdef FEAT_FLOAT
1121 to->vval.v_float = from->vval.v_float;
1122 break;
1123#endif
1124 case VAR_JOB:
1125#ifdef FEAT_JOB_CHANNEL
1126 to->vval.v_job = from->vval.v_job;
1127 if (to->vval.v_job != NULL)
1128 ++to->vval.v_job->jv_refcount;
1129 break;
1130#endif
1131 case VAR_CHANNEL:
1132#ifdef FEAT_JOB_CHANNEL
1133 to->vval.v_channel = from->vval.v_channel;
1134 if (to->vval.v_channel != NULL)
1135 ++to->vval.v_channel->ch_refcount;
1136 break;
1137#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001138 case VAR_INSTR:
1139 to->vval.v_instr = from->vval.v_instr;
1140 break;
1141
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001142 case VAR_STRING:
1143 case VAR_FUNC:
1144 if (from->vval.v_string == NULL)
1145 to->vval.v_string = NULL;
1146 else
1147 {
1148 to->vval.v_string = vim_strsave(from->vval.v_string);
1149 if (from->v_type == VAR_FUNC)
1150 func_ref(to->vval.v_string);
1151 }
1152 break;
1153 case VAR_PARTIAL:
1154 if (from->vval.v_partial == NULL)
1155 to->vval.v_partial = NULL;
1156 else
1157 {
1158 to->vval.v_partial = from->vval.v_partial;
1159 ++to->vval.v_partial->pt_refcount;
1160 }
1161 break;
1162 case VAR_BLOB:
1163 if (from->vval.v_blob == NULL)
1164 to->vval.v_blob = NULL;
1165 else
1166 {
1167 to->vval.v_blob = from->vval.v_blob;
1168 ++to->vval.v_blob->bv_refcount;
1169 }
1170 break;
1171 case VAR_LIST:
1172 if (from->vval.v_list == NULL)
1173 to->vval.v_list = NULL;
1174 else
1175 {
1176 to->vval.v_list = from->vval.v_list;
1177 ++to->vval.v_list->lv_refcount;
1178 }
1179 break;
1180 case VAR_DICT:
1181 if (from->vval.v_dict == NULL)
1182 to->vval.v_dict = NULL;
1183 else
1184 {
1185 to->vval.v_dict = from->vval.v_dict;
1186 ++to->vval.v_dict->dv_refcount;
1187 }
1188 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +02001189 case VAR_VOID:
1190 emsg(_(e_cannot_use_void_value));
1191 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001192 case VAR_UNKNOWN:
1193 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001194 internal_error_no_abort("copy_tv(UNKNOWN)");
1195 break;
1196 }
1197}
1198
1199/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001200 * Compare "tv1" and "tv2".
1201 * Put the result in "tv1". Caller should clear "tv2".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001202 */
1203 int
1204typval_compare(
Bram Moolenaar265f8112021-12-19 12:33:05 +00001205 typval_T *tv1, // first operand
1206 typval_T *tv2, // second operand
1207 exprtype_T type, // operator
1208 int ic) // ignore case
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001209{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001210 varnumber_T n1, n2;
Bram Moolenaar265f8112021-12-19 12:33:05 +00001211 int res = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001212 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1213
Bram Moolenaar265f8112021-12-19 12:33:05 +00001214 if (type_is && tv1->v_type != tv2->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001215 {
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001216 // For "is" a different type always means FALSE, for "isnot"
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001217 // it means TRUE.
1218 n1 = (type == EXPR_ISNOT);
1219 }
Bram Moolenaar7a222242022-03-01 19:23:24 +00001220 else if (((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1221 || (tv2->v_type == VAR_SPECIAL
1222 && tv2->vval.v_number == VVAL_NULL))
1223 && tv1->v_type != tv2->v_type
1224 && (type == EXPR_EQUAL || type == EXPR_NEQUAL))
1225 {
1226 n1 = typval_compare_null(tv1, tv2);
1227 if (n1 == MAYBE)
1228 {
1229 clear_tv(tv1);
1230 return FAIL;
1231 }
1232 if (type == EXPR_NEQUAL)
1233 n1 = !n1;
1234 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001235 else if (tv1->v_type == VAR_BLOB || tv2->v_type == VAR_BLOB)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001236 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001237 if (typval_compare_blob(tv1, tv2, type, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001238 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001239 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001240 return FAIL;
1241 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001242 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001243 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001244 else if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001245 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001246 if (typval_compare_list(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001247 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001248 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001249 return FAIL;
1250 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001251 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001252 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001253 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001254 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001255 if (typval_compare_dict(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001256 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001257 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001258 return FAIL;
1259 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001260 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001261 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001262 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC
1263 || tv1->v_type == VAR_PARTIAL || tv2->v_type == VAR_PARTIAL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001264 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001265 if (typval_compare_func(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001266 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001267 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001268 return FAIL;
1269 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001270 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001271 }
1272
1273#ifdef FEAT_FLOAT
1274 // If one of the two variables is a float, compare as a float.
1275 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001276 else if ((tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001277 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1278 {
1279 float_T f1, f2;
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001280 int error = FALSE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001281
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001282 f1 = tv_get_float_chk(tv1, &error);
1283 if (!error)
1284 f2 = tv_get_float_chk(tv2, &error);
1285 if (error)
1286 {
1287 clear_tv(tv1);
1288 return FAIL;
1289 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001290 n1 = FALSE;
1291 switch (type)
1292 {
1293 case EXPR_IS:
1294 case EXPR_EQUAL: n1 = (f1 == f2); break;
1295 case EXPR_ISNOT:
1296 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1297 case EXPR_GREATER: n1 = (f1 > f2); break;
1298 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1299 case EXPR_SMALLER: n1 = (f1 < f2); break;
1300 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1301 case EXPR_UNKNOWN:
1302 case EXPR_MATCH:
1303 default: break; // avoid gcc warning
1304 }
1305 }
1306#endif
1307
1308 // If one of the two variables is a number, compare as a number.
1309 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001310 else if ((tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001311 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1312 {
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001313 int error = FALSE;
1314
1315 n1 = tv_get_number_chk(tv1, &error);
1316 if (!error)
1317 n2 = tv_get_number_chk(tv2, &error);
1318 if (error)
1319 {
1320 clear_tv(tv1);
1321 return FAIL;
1322 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001323 switch (type)
1324 {
1325 case EXPR_IS:
1326 case EXPR_EQUAL: n1 = (n1 == n2); break;
1327 case EXPR_ISNOT:
1328 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1329 case EXPR_GREATER: n1 = (n1 > n2); break;
1330 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1331 case EXPR_SMALLER: n1 = (n1 < n2); break;
1332 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1333 case EXPR_UNKNOWN:
1334 case EXPR_MATCH:
1335 default: break; // avoid gcc warning
1336 }
1337 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001338 else if (in_vim9script() && (tv1->v_type == VAR_BOOL
1339 || tv2->v_type == VAR_BOOL
1340 || (tv1->v_type == VAR_SPECIAL
1341 && tv2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001342 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001343 if (tv1->v_type != tv2->v_type)
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001344 {
1345 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001346 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1347 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001348 return FAIL;
1349 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001350 n1 = tv1->vval.v_number;
1351 n2 = tv2->vval.v_number;
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001352 switch (type)
1353 {
1354 case EXPR_IS:
1355 case EXPR_EQUAL: n1 = (n1 == n2); break;
1356 case EXPR_ISNOT:
1357 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1358 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001359 semsg(_(e_invalid_operation_for_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001360 vartype_name(tv1->v_type));
1361 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001362 return FAIL;
1363 }
1364 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001365#ifdef FEAT_JOB_CHANNEL
1366 else if (tv1->v_type == tv2->v_type
1367 && (tv1->v_type == VAR_CHANNEL || tv1->v_type == VAR_JOB)
1368 && (type == EXPR_NEQUAL || type == EXPR_EQUAL))
1369 {
1370 if (tv1->v_type == VAR_CHANNEL)
1371 n1 = tv1->vval.v_channel == tv2->vval.v_channel;
1372 else
1373 n1 = tv1->vval.v_job == tv2->vval.v_job;
1374 if (type == EXPR_NEQUAL)
1375 n1 = !n1;
1376 }
1377#endif
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001378 else
1379 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001380 if (typval_compare_string(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar0c357522021-07-18 14:43:43 +02001381 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001382 clear_tv(tv1);
Bram Moolenaar0c357522021-07-18 14:43:43 +02001383 return FAIL;
1384 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001385 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001386 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001387 clear_tv(tv1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001388 if (in_vim9script())
1389 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001390 tv1->v_type = VAR_BOOL;
1391 tv1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001392 }
1393 else
1394 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001395 tv1->v_type = VAR_NUMBER;
1396 tv1->vval.v_number = n1;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001397 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001398
1399 return OK;
1400}
1401
Bram Moolenaar34453202021-01-31 13:08:38 +01001402/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001403 * Compare "tv1" to "tv2" as lists acording to "type" and "ic".
1404 * Put the result, false or true, in "res".
1405 * Return FAIL and give an error message when the comparison can't be done.
1406 */
1407 int
1408typval_compare_list(
1409 typval_T *tv1,
1410 typval_T *tv2,
1411 exprtype_T type,
1412 int ic,
1413 int *res)
1414{
1415 int val = 0;
1416
1417 if (type == EXPR_IS || type == EXPR_ISNOT)
1418 {
1419 val = (tv1->v_type == tv2->v_type
1420 && tv1->vval.v_list == tv2->vval.v_list);
1421 if (type == EXPR_ISNOT)
1422 val = !val;
1423 }
1424 else if (tv1->v_type != tv2->v_type
1425 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1426 {
1427 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001428 emsg(_(e_can_only_compare_list_with_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001429 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001430 emsg(_(e_invalid_operation_for_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001431 return FAIL;
1432 }
1433 else
1434 {
1435 val = list_equal(tv1->vval.v_list, tv2->vval.v_list,
1436 ic, FALSE);
1437 if (type == EXPR_NEQUAL)
1438 val = !val;
1439 }
1440 *res = val;
1441 return OK;
1442}
1443
1444/*
Bram Moolenaarf3507a52022-03-09 11:56:21 +00001445 * Compare v:null with another type. Return TRUE if the value is NULL.
Bram Moolenaar7a222242022-03-01 19:23:24 +00001446 */
1447 int
1448typval_compare_null(typval_T *tv1, typval_T *tv2)
1449{
1450 if ((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1451 || (tv2->v_type == VAR_SPECIAL && tv2->vval.v_number == VVAL_NULL))
1452 {
1453 typval_T *tv = tv1->v_type == VAR_SPECIAL ? tv2 : tv1;
1454
1455 switch (tv->v_type)
1456 {
1457 case VAR_BLOB: return tv->vval.v_blob == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001458#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001459 case VAR_CHANNEL: return tv->vval.v_channel == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001460#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001461 case VAR_DICT: return tv->vval.v_dict == NULL;
1462 case VAR_FUNC: return tv->vval.v_string == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001463#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001464 case VAR_JOB: return tv->vval.v_job == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001465#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001466 case VAR_LIST: return tv->vval.v_list == NULL;
1467 case VAR_PARTIAL: return tv->vval.v_partial == NULL;
1468 case VAR_STRING: return tv->vval.v_string == NULL;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001469
1470 case VAR_NUMBER: if (!in_vim9script())
1471 return tv->vval.v_number == 0;
1472 break;
1473#ifdef FEAT_FLOAT
1474 case VAR_FLOAT: if (!in_vim9script())
1475 return tv->vval.v_float == 0.0;
1476 break;
1477#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001478 default: break;
1479 }
1480 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001481 // although comparing null with number, float or bool is not very useful
Bram Moolenaar05667812022-03-15 20:21:33 +00001482 // we won't give an error
1483 return FALSE;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001484}
1485
1486/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001487 * Compare "tv1" to "tv2" as blobs acording to "type".
1488 * Put the result, false or true, in "res".
1489 * Return FAIL and give an error message when the comparison can't be done.
1490 */
1491 int
1492typval_compare_blob(
1493 typval_T *tv1,
1494 typval_T *tv2,
1495 exprtype_T type,
1496 int *res)
1497{
1498 int val = 0;
1499
1500 if (type == EXPR_IS || type == EXPR_ISNOT)
1501 {
1502 val = (tv1->v_type == tv2->v_type
1503 && tv1->vval.v_blob == tv2->vval.v_blob);
1504 if (type == EXPR_ISNOT)
1505 val = !val;
1506 }
1507 else if (tv1->v_type != tv2->v_type
1508 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1509 {
1510 if (tv1->v_type != tv2->v_type)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001511 emsg(_(e_can_only_compare_blob_with_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001512 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001513 emsg(_(e_invalid_operation_for_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001514 return FAIL;
1515 }
1516 else
1517 {
1518 val = blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1519 if (type == EXPR_NEQUAL)
1520 val = !val;
1521 }
1522 *res = val;
1523 return OK;
1524}
1525
1526/*
1527 * Compare "tv1" to "tv2" as dictionaries acording to "type" and "ic".
1528 * Put the result, false or true, in "res".
1529 * Return FAIL and give an error message when the comparison can't be done.
1530 */
1531 int
1532typval_compare_dict(
1533 typval_T *tv1,
1534 typval_T *tv2,
1535 exprtype_T type,
1536 int ic,
1537 int *res)
1538{
1539 int val;
1540
1541 if (type == EXPR_IS || type == EXPR_ISNOT)
1542 {
1543 val = (tv1->v_type == tv2->v_type
1544 && tv1->vval.v_dict == tv2->vval.v_dict);
1545 if (type == EXPR_ISNOT)
1546 val = !val;
1547 }
1548 else if (tv1->v_type != tv2->v_type
1549 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1550 {
1551 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001552 emsg(_(e_can_only_compare_dictionary_with_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001553 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001554 emsg(_(e_invalid_operation_for_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001555 return FAIL;
1556 }
1557 else
1558 {
1559 val = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, FALSE);
1560 if (type == EXPR_NEQUAL)
1561 val = !val;
1562 }
1563 *res = val;
1564 return OK;
1565}
1566
1567/*
1568 * Compare "tv1" to "tv2" as funcrefs acording to "type" and "ic".
1569 * Put the result, false or true, in "res".
1570 * Return FAIL and give an error message when the comparison can't be done.
1571 */
1572 int
1573typval_compare_func(
1574 typval_T *tv1,
1575 typval_T *tv2,
1576 exprtype_T type,
1577 int ic,
1578 int *res)
1579{
1580 int val = 0;
1581
1582 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1583 && type != EXPR_IS && type != EXPR_ISNOT)
1584 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001585 emsg(_(e_invalid_operation_for_funcrefs));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001586 return FAIL;
1587 }
1588 if ((tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial == NULL)
1589 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial == NULL))
1590 // When both partials are NULL, then they are equal.
1591 // Otherwise they are not equal.
1592 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1593 else if (type == EXPR_IS || type == EXPR_ISNOT)
1594 {
1595 if (tv1->v_type == VAR_FUNC && tv2->v_type == VAR_FUNC)
1596 // strings are considered the same if their value is
1597 // the same
1598 val = tv_equal(tv1, tv2, ic, FALSE);
1599 else if (tv1->v_type == VAR_PARTIAL && tv2->v_type == VAR_PARTIAL)
1600 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1601 else
1602 val = FALSE;
1603 }
1604 else
1605 val = tv_equal(tv1, tv2, ic, FALSE);
1606 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1607 val = !val;
1608 *res = val;
1609 return OK;
1610}
1611
1612/*
1613 * Compare "tv1" to "tv2" as strings according to "type" and "ic".
1614 * Put the result, false or true, in "res".
1615 * Return FAIL and give an error message when the comparison can't be done.
1616 */
1617 int
1618typval_compare_string(
1619 typval_T *tv1,
1620 typval_T *tv2,
1621 exprtype_T type,
1622 int ic,
1623 int *res)
1624{
1625 int i = 0;
1626 int val = FALSE;
1627 char_u *s1, *s2;
1628 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1629
1630 if (in_vim9script()
1631 && ((tv1->v_type != VAR_STRING && tv1->v_type != VAR_SPECIAL)
1632 || (tv2->v_type != VAR_STRING && tv2->v_type != VAR_SPECIAL)))
1633 {
1634 semsg(_(e_cannot_compare_str_with_str),
1635 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1636 return FAIL;
1637 }
1638 s1 = tv_get_string_buf(tv1, buf1);
1639 s2 = tv_get_string_buf(tv2, buf2);
1640 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1641 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1642 switch (type)
1643 {
Bram Moolenaarf8691002022-03-10 12:20:53 +00001644 case EXPR_IS: if (in_vim9script())
1645 {
1646 // Really check it is the same string, not just
1647 // the same value.
1648 val = tv1->vval.v_string == tv2->vval.v_string;
1649 break;
1650 }
1651 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001652 case EXPR_EQUAL: val = (i == 0); break;
Bram Moolenaarf8691002022-03-10 12:20:53 +00001653 case EXPR_ISNOT: if (in_vim9script())
1654 {
1655 // Really check it is not the same string, not
1656 // just a different value.
1657 val = tv1->vval.v_string != tv2->vval.v_string;
1658 break;
1659 }
1660 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001661 case EXPR_NEQUAL: val = (i != 0); break;
1662 case EXPR_GREATER: val = (i > 0); break;
1663 case EXPR_GEQUAL: val = (i >= 0); break;
1664 case EXPR_SMALLER: val = (i < 0); break;
1665 case EXPR_SEQUAL: val = (i <= 0); break;
1666
1667 case EXPR_MATCH:
1668 case EXPR_NOMATCH:
1669 val = pattern_match(s2, s1, ic);
1670 if (type == EXPR_NOMATCH)
1671 val = !val;
1672 break;
1673
1674 default: break; // avoid gcc warning
1675 }
1676 *res = val;
1677 return OK;
1678}
1679/*
Bram Moolenaar34453202021-01-31 13:08:38 +01001680 * Convert any type to a string, never give an error.
1681 * When "quotes" is TRUE add quotes to a string.
1682 * Returns an allocated string.
1683 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001684 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001685typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001686{
1687 char_u *tofree;
1688 char_u numbuf[NUMBUFLEN];
1689 char_u *ret = NULL;
1690
1691 if (arg == NULL)
1692 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001693 if (!quotes && arg->v_type == VAR_STRING)
1694 {
1695 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1696 : arg->vval.v_string);
1697 }
1698 else
1699 {
1700 ret = tv2string(arg, &tofree, numbuf, 0);
1701 // Make a copy if we have a value but it's not in allocated memory.
1702 if (ret != NULL && tofree == NULL)
1703 ret = vim_strsave(ret);
1704 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001705 return ret;
1706}
1707
1708/*
1709 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1710 * or it refers to a List or Dictionary that is locked.
1711 */
1712 int
1713tv_islocked(typval_T *tv)
1714{
1715 return (tv->v_lock & VAR_LOCKED)
1716 || (tv->v_type == VAR_LIST
1717 && tv->vval.v_list != NULL
1718 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1719 || (tv->v_type == VAR_DICT
1720 && tv->vval.v_dict != NULL
1721 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1722}
1723
1724 static int
1725func_equal(
1726 typval_T *tv1,
1727 typval_T *tv2,
1728 int ic) // ignore case
1729{
1730 char_u *s1, *s2;
1731 dict_T *d1, *d2;
1732 int a1, a2;
1733 int i;
1734
1735 // empty and NULL function name considered the same
1736 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1737 : partial_name(tv1->vval.v_partial);
1738 if (s1 != NULL && *s1 == NUL)
1739 s1 = NULL;
1740 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1741 : partial_name(tv2->vval.v_partial);
1742 if (s2 != NULL && *s2 == NUL)
1743 s2 = NULL;
1744 if (s1 == NULL || s2 == NULL)
1745 {
1746 if (s1 != s2)
1747 return FALSE;
1748 }
1749 else if (STRCMP(s1, s2) != 0)
1750 return FALSE;
1751
1752 // empty dict and NULL dict is different
1753 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1754 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1755 if (d1 == NULL || d2 == NULL)
1756 {
1757 if (d1 != d2)
1758 return FALSE;
1759 }
1760 else if (!dict_equal(d1, d2, ic, TRUE))
1761 return FALSE;
1762
1763 // empty list and no list considered the same
1764 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1765 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1766 if (a1 != a2)
1767 return FALSE;
1768 for (i = 0; i < a1; ++i)
1769 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1770 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1771 return FALSE;
1772
1773 return TRUE;
1774}
1775
1776/*
1777 * Return TRUE if "tv1" and "tv2" have the same value.
1778 * Compares the items just like "==" would compare them, but strings and
1779 * numbers are different. Floats and numbers are also different.
1780 */
1781 int
1782tv_equal(
1783 typval_T *tv1,
1784 typval_T *tv2,
1785 int ic, // ignore case
1786 int recursive) // TRUE when used recursively
1787{
1788 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1789 char_u *s1, *s2;
1790 static int recursive_cnt = 0; // catch recursive loops
1791 int r;
1792 static int tv_equal_recurse_limit;
1793
1794 // Catch lists and dicts that have an endless loop by limiting
1795 // recursiveness to a limit. We guess they are equal then.
1796 // A fixed limit has the problem of still taking an awful long time.
1797 // Reduce the limit every time running into it. That should work fine for
1798 // deeply linked structures that are not recursively linked and catch
1799 // recursiveness quickly.
1800 if (!recursive)
1801 tv_equal_recurse_limit = 1000;
1802 if (recursive_cnt >= tv_equal_recurse_limit)
1803 {
1804 --tv_equal_recurse_limit;
1805 return TRUE;
1806 }
1807
1808 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1809 // arguments.
1810 if ((tv1->v_type == VAR_FUNC
1811 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1812 && (tv2->v_type == VAR_FUNC
1813 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1814 {
1815 ++recursive_cnt;
1816 r = func_equal(tv1, tv2, ic);
1817 --recursive_cnt;
1818 return r;
1819 }
1820
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001821 if (tv1->v_type != tv2->v_type
1822 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1823 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001824 return FALSE;
1825
1826 switch (tv1->v_type)
1827 {
1828 case VAR_LIST:
1829 ++recursive_cnt;
1830 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1831 --recursive_cnt;
1832 return r;
1833
1834 case VAR_DICT:
1835 ++recursive_cnt;
1836 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1837 --recursive_cnt;
1838 return r;
1839
1840 case VAR_BLOB:
1841 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1842
1843 case VAR_NUMBER:
1844 case VAR_BOOL:
1845 case VAR_SPECIAL:
1846 return tv1->vval.v_number == tv2->vval.v_number;
1847
1848 case VAR_STRING:
1849 s1 = tv_get_string_buf(tv1, buf1);
1850 s2 = tv_get_string_buf(tv2, buf2);
1851 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1852
1853 case VAR_FLOAT:
1854#ifdef FEAT_FLOAT
1855 return tv1->vval.v_float == tv2->vval.v_float;
1856#endif
1857 case VAR_JOB:
1858#ifdef FEAT_JOB_CHANNEL
1859 return tv1->vval.v_job == tv2->vval.v_job;
1860#endif
1861 case VAR_CHANNEL:
1862#ifdef FEAT_JOB_CHANNEL
1863 return tv1->vval.v_channel == tv2->vval.v_channel;
1864#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001865 case VAR_INSTR:
1866 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001867
1868 case VAR_PARTIAL:
1869 return tv1->vval.v_partial == tv2->vval.v_partial;
1870
1871 case VAR_FUNC:
1872 return tv1->vval.v_string == tv2->vval.v_string;
1873
1874 case VAR_UNKNOWN:
1875 case VAR_ANY:
1876 case VAR_VOID:
1877 break;
1878 }
1879
1880 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1881 // does not equal anything, not even itself.
1882 return FALSE;
1883}
1884
1885/*
1886 * Get an option value.
1887 * "arg" points to the '&' or '+' before the option name.
1888 * "arg" is advanced to character after the option name.
1889 * Return OK or FAIL.
1890 */
1891 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001892eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001893 char_u **arg,
1894 typval_T *rettv, // when NULL, only check if option exists
1895 int evaluate)
1896{
1897 char_u *option_end;
1898 long numval;
1899 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001900 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001901 int c;
1902 int working = (**arg == '+'); // has("+option")
1903 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001904 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001905
1906 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001907 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001908 if (option_end == NULL)
1909 {
1910 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001911 semsg(_(e_option_name_missing_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001912 return FAIL;
1913 }
1914
1915 if (!evaluate)
1916 {
1917 *arg = option_end;
1918 return OK;
1919 }
1920
1921 c = *option_end;
1922 *option_end = NUL;
1923 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001924 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001925
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001926 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001927 {
1928 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001929 semsg(_(e_unknown_option_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001930 ret = FAIL;
1931 }
1932 else if (rettv != NULL)
1933 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001934 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001935 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001936 {
1937 rettv->v_type = VAR_STRING;
1938 rettv->vval.v_string = NULL;
1939 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001940 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001941 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001942 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1943 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001944 rettv->vval.v_number = 0;
1945 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001946 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001947 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001948 if (in_vim9script() && opt_type == gov_bool)
1949 {
1950 rettv->v_type = VAR_BOOL;
1951 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1952 }
1953 else
1954 {
1955 rettv->v_type = VAR_NUMBER;
1956 rettv->vval.v_number = numval;
1957 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001958 }
1959 else // string option
1960 {
1961 rettv->v_type = VAR_STRING;
1962 rettv->vval.v_string = stringval;
1963 }
1964 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001965 else if (working && (opt_type == gov_hidden_bool
1966 || opt_type == gov_hidden_number
1967 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001968 ret = FAIL;
1969
1970 *option_end = c; // put back for error messages
1971 *arg = option_end;
1972
1973 return ret;
1974}
1975
1976/*
1977 * Allocate a variable for a number constant. Also deals with "0z" for blob.
1978 * Return OK or FAIL.
1979 */
1980 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001981eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001982 char_u **arg,
1983 typval_T *rettv,
1984 int evaluate,
1985 int want_string UNUSED)
1986{
1987 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02001988 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001989#ifdef FEAT_FLOAT
1990 char_u *p;
1991 int get_float = FALSE;
1992
1993 // We accept a float when the format matches
1994 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
1995 // strict to avoid backwards compatibility problems.
1996 // With script version 2 and later the leading digit can be
1997 // omitted.
1998 // Don't look for a float after the "." operator, so that
1999 // ":let vers = 1.2.3" doesn't fail.
2000 if (**arg == '.')
2001 p = *arg;
2002 else
Bram Moolenaar29500652021-08-08 15:43:34 +02002003 {
2004 p = *arg + 1;
2005 if (skip_quotes)
2006 for (;;)
2007 {
2008 if (*p == '\'')
2009 ++p;
2010 if (!vim_isdigit(*p))
2011 break;
2012 p = skipdigits(p);
2013 }
2014 else
2015 p = skipdigits(p);
2016 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002017 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
2018 {
2019 get_float = TRUE;
2020 p = skipdigits(p + 2);
2021 if (*p == 'e' || *p == 'E')
2022 {
2023 ++p;
2024 if (*p == '-' || *p == '+')
2025 ++p;
2026 if (!vim_isdigit(*p))
2027 get_float = FALSE;
2028 else
2029 p = skipdigits(p + 1);
2030 }
2031 if (ASCII_ISALPHA(*p) || *p == '.')
2032 get_float = FALSE;
2033 }
2034 if (get_float)
2035 {
2036 float_T f;
2037
Bram Moolenaar29500652021-08-08 15:43:34 +02002038 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002039 if (evaluate)
2040 {
2041 rettv->v_type = VAR_FLOAT;
2042 rettv->vval.v_float = f;
2043 }
2044 }
2045 else
2046#endif
2047 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
2048 {
2049 char_u *bp;
2050 blob_T *blob = NULL; // init for gcc
2051
2052 // Blob constant: 0z0123456789abcdef
2053 if (evaluate)
2054 blob = blob_alloc();
2055 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2056 {
2057 if (!vim_isxdigit(bp[1]))
2058 {
2059 if (blob != NULL)
2060 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002061 emsg(_(e_blob_literal_should_have_an_even_number_of_hex_characters));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002062 ga_clear(&blob->bv_ga);
2063 VIM_CLEAR(blob);
2064 }
2065 return FAIL;
2066 }
2067 if (blob != NULL)
2068 ga_append(&blob->bv_ga,
2069 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
2070 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2071 ++bp;
2072 }
2073 if (blob != NULL)
2074 rettv_blob_set(rettv, blob);
2075 *arg = bp;
2076 }
2077 else
2078 {
2079 varnumber_T n;
2080
2081 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02002082 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002083 ? STR2NR_NO_OCT + STR2NR_QUOTE
2084 : STR2NR_ALL, &n, NULL, 0, TRUE);
2085 if (len == 0)
2086 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00002087 if (evaluate)
2088 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002089 return FAIL;
2090 }
2091 *arg += len;
2092 if (evaluate)
2093 {
2094 rettv->v_type = VAR_NUMBER;
2095 rettv->vval.v_number = n;
2096 }
2097 }
2098 return OK;
2099}
2100
2101/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002102 * Evaluate a string constant and put the result in "rettv".
2103 * "*arg" points to the double quote or to after it when "interpolate" is TRUE.
2104 * When "interpolate" is TRUE reduce "{{" to "{", reduce "}}" to "}" and stop
2105 * at a single "{".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002106 * Return OK or FAIL.
2107 */
2108 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002109eval_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002110{
2111 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002112 char_u *end;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002113 int extra = interpolate ? 1 : 0;
2114 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002115 int len;
2116
2117 // Find the end of the string, skipping backslashed characters.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002118 for (p = *arg + off; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002119 {
2120 if (*p == '\\' && p[1] != NUL)
2121 {
2122 ++p;
2123 // A "\<x>" form occupies at least 4 characters, and produces up
zeertzjqdb088872022-05-02 22:53:45 +01002124 // to 9 characters (6 for the char and 3 for a modifier):
2125 // reserve space for 5 extra.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002126 if (*p == '<')
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002127 {
2128 int modifiers = 0;
2129 int flags = FSK_KEYCODE | FSK_IN_STRING;
2130
zeertzjqdb088872022-05-02 22:53:45 +01002131 extra += 5;
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002132
2133 // Skip to the '>' to avoid using '{' inside for string
2134 // interpolation.
2135 if (p[1] != '*')
2136 flags |= FSK_SIMPLIFY;
2137 if (find_special_key(&p, &modifiers, flags, NULL) != 0)
2138 --p; // leave "p" on the ">"
2139 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002140 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002141 else if (interpolate && (*p == '{' || *p == '}'))
2142 {
2143 if (*p == '{' && p[1] != '{') // start of expression
2144 break;
2145 ++p;
2146 if (p[-1] == '}' && *p != '}') // single '}' is an error
2147 {
2148 semsg(_(e_stray_closing_curly_str), *arg);
2149 return FAIL;
2150 }
2151 --extra; // "{{" becomes "{", "}}" becomes "}"
2152 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002153 }
2154
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002155 if (*p != '"' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002156 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002157 semsg(_(e_missing_double_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002158 return FAIL;
2159 }
2160
2161 // If only parsing, set *arg and return here
2162 if (!evaluate)
2163 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002164 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002165 return OK;
2166 }
2167
2168 // Copy the string into allocated memory, handling backslashed
2169 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002170 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002171 len = (int)(p - *arg + extra);
2172 rettv->vval.v_string = alloc(len);
2173 if (rettv->vval.v_string == NULL)
2174 return FAIL;
2175 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002176
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002177 for (p = *arg + off; *p != NUL && *p != '"'; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002178 {
2179 if (*p == '\\')
2180 {
2181 switch (*++p)
2182 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002183 case 'b': *end++ = BS; ++p; break;
2184 case 'e': *end++ = ESC; ++p; break;
2185 case 'f': *end++ = FF; ++p; break;
2186 case 'n': *end++ = NL; ++p; break;
2187 case 'r': *end++ = CAR; ++p; break;
2188 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002189
2190 case 'X': // hex: "\x1", "\x12"
2191 case 'x':
2192 case 'u': // Unicode: "\u0023"
2193 case 'U':
2194 if (vim_isxdigit(p[1]))
2195 {
2196 int n, nr;
2197 int c = toupper(*p);
2198
2199 if (c == 'X')
2200 n = 2;
2201 else if (*p == 'u')
2202 n = 4;
2203 else
2204 n = 8;
2205 nr = 0;
2206 while (--n >= 0 && vim_isxdigit(p[1]))
2207 {
2208 ++p;
2209 nr = (nr << 4) + hex2nr(*p);
2210 }
2211 ++p;
2212 // For "\u" store the number according to
2213 // 'encoding'.
2214 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002215 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002216 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002217 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002218 }
2219 break;
2220
2221 // octal: "\1", "\12", "\123"
2222 case '0':
2223 case '1':
2224 case '2':
2225 case '3':
2226 case '4':
2227 case '5':
2228 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002229 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002230 if (*p >= '0' && *p <= '7')
2231 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002232 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002233 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002234 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002235 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002236 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002237 break;
2238
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002239 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002240 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002241 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002242 int flags = FSK_KEYCODE | FSK_IN_STRING;
2243
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002244 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002245 flags |= FSK_SIMPLIFY;
zeertzjqdb088872022-05-02 22:53:45 +01002246 extra = trans_special(&p, end, flags, FALSE, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002247 if (extra != 0)
2248 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002249 end += extra;
2250 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002251 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002252 break;
2253 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002254 }
2255 // FALLTHROUGH
2256
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002257 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002258 break;
2259 }
2260 }
2261 else
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002262 {
2263 if (interpolate && (*p == '{' || *p == '}'))
2264 {
2265 if (*p == '{' && p[1] != '{') // start of expression
2266 break;
2267 ++p; // reduce "{{" to "{" and "}}" to "}"
2268 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002269 MB_COPY_CHAR(p, end);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002270 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002271 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002272 *end = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002273 if (*p == '"' && !interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002274 ++p;
2275 *arg = p;
2276
2277 return OK;
2278}
2279
2280/*
2281 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002282 * When "interpolate" is TRUE reduce "{{" to "{" and stop at a single "{".
2283 * Return OK when a "rettv" was set to the string.
2284 * Return FAIL on error, "rettv" is not set.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002285 */
2286 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002287eval_lit_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002288{
2289 char_u *p;
2290 char_u *str;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002291 int reduce = interpolate ? -1 : 0;
2292 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002293
2294 // Find the end of the string, skipping ''.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002295 for (p = *arg + off; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002296 {
2297 if (*p == '\'')
2298 {
2299 if (p[1] != '\'')
2300 break;
2301 ++reduce;
2302 ++p;
2303 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002304 else if (interpolate)
2305 {
2306 if (*p == '{')
2307 {
2308 if (p[1] != '{')
2309 break;
2310 ++p;
2311 ++reduce;
2312 }
2313 else if (*p == '}')
2314 {
2315 ++p;
2316 if (*p != '}')
2317 {
2318 semsg(_(e_stray_closing_curly_str), *arg);
2319 return FAIL;
2320 }
2321 ++reduce;
2322 }
2323 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002324 }
2325
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002326 if (*p != '\'' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002327 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002328 semsg(_(e_missing_single_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002329 return FAIL;
2330 }
2331
2332 // If only parsing return after setting "*arg"
2333 if (!evaluate)
2334 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002335 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002336 return OK;
2337 }
2338
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002339 // Copy the string into allocated memory, handling '' to ' reduction and
2340 // any expressions.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002341 str = alloc((p - *arg) - reduce);
2342 if (str == NULL)
2343 return FAIL;
2344 rettv->v_type = VAR_STRING;
2345 rettv->vval.v_string = str;
2346
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002347 for (p = *arg + off; *p != NUL; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002348 {
2349 if (*p == '\'')
2350 {
2351 if (p[1] != '\'')
2352 break;
2353 ++p;
2354 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002355 else if (interpolate && (*p == '{' || *p == '}'))
2356 {
2357 if (*p == '{' && p[1] != '{')
2358 break;
2359 ++p;
2360 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002361 MB_COPY_CHAR(p, str);
2362 }
2363 *str = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002364 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002365
2366 return OK;
2367}
2368
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002369/*
2370 * Evaluate a single or double quoted string possibly containing expressions.
2371 * "arg" points to the '$'. The result is put in "rettv".
2372 * Returns OK or FAIL.
2373 */
LemonBoy2eaef102022-05-06 13:14:50 +01002374 int
2375eval_interp_string(char_u **arg, typval_T *rettv, int evaluate)
2376{
2377 typval_T tv;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002378 int ret = OK;
2379 int quote;
2380 garray_T ga;
2381 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01002382
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002383 ga_init2(&ga, 1, 80);
2384
2385 // *arg is on the '$' character, move it to the first string character.
2386 ++*arg;
2387 quote = **arg;
2388 ++*arg;
2389
2390 for (;;)
2391 {
2392 // Get the string up to the matching quote or to a single '{'.
2393 // "arg" is advanced to either the quote or the '{'.
2394 if (quote == '"')
2395 ret = eval_string(arg, &tv, evaluate, TRUE);
2396 else
2397 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
2398 if (ret == FAIL)
2399 break;
2400 if (evaluate)
2401 {
2402 ga_concat(&ga, tv.vval.v_string);
2403 clear_tv(&tv);
2404 }
2405
2406 if (**arg != '{')
2407 {
2408 // found terminating quote
2409 ++*arg;
2410 break;
2411 }
Bram Moolenaar70c41242022-05-10 18:11:43 +01002412 p = eval_one_expr_in_str(*arg, &ga, evaluate);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002413 if (p == NULL)
2414 {
2415 ret = FAIL;
2416 break;
2417 }
2418 *arg = p;
2419 }
LemonBoy2eaef102022-05-06 13:14:50 +01002420
2421 rettv->v_type = VAR_STRING;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002422 if (ret == FAIL || !evaluate || ga_append(&ga, NUL) == FAIL)
2423 {
2424 ga_clear(&ga);
2425 rettv->vval.v_string = NULL;
LemonBoy2eaef102022-05-06 13:14:50 +01002426 return ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002427 }
LemonBoy2eaef102022-05-06 13:14:50 +01002428
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002429 rettv->vval.v_string = ga.ga_data;
2430 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01002431}
2432
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002433/*
2434 * Return a string with the string representation of a variable.
2435 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2436 * "numbuf" is used for a number.
2437 * Puts quotes around strings, so that they can be parsed back by eval().
2438 * May return NULL.
2439 */
2440 char_u *
2441tv2string(
2442 typval_T *tv,
2443 char_u **tofree,
2444 char_u *numbuf,
2445 int copyID)
2446{
2447 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2448}
2449
2450/*
2451 * Get the value of an environment variable.
2452 * "arg" is pointing to the '$'. It is advanced to after the name.
2453 * If the environment variable was not set, silently assume it is empty.
2454 * Return FAIL if the name is invalid.
2455 */
2456 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002457eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002458{
2459 char_u *string = NULL;
2460 int len;
2461 int cc;
2462 char_u *name;
2463 int mustfree = FALSE;
2464
2465 ++*arg;
2466 name = *arg;
2467 len = get_env_len(arg);
2468 if (evaluate)
2469 {
2470 if (len == 0)
2471 return FAIL; // invalid empty name
2472
2473 cc = name[len];
2474 name[len] = NUL;
2475 // first try vim_getenv(), fast for normal environment vars
2476 string = vim_getenv(name, &mustfree);
2477 if (string != NULL && *string != NUL)
2478 {
2479 if (!mustfree)
2480 string = vim_strsave(string);
2481 }
2482 else
2483 {
2484 if (mustfree)
2485 vim_free(string);
2486
2487 // next try expanding things like $VIM and ${HOME}
2488 string = expand_env_save(name - 1);
2489 if (string != NULL && *string == '$')
2490 VIM_CLEAR(string);
2491 }
2492 name[len] = cc;
2493
2494 rettv->v_type = VAR_STRING;
2495 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002496 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002497 }
2498
2499 return OK;
2500}
2501
2502/*
2503 * Get the lnum from the first argument.
2504 * Also accepts ".", "$", etc., but that only works for the current buffer.
2505 * Returns -1 on error.
2506 */
2507 linenr_T
2508tv_get_lnum(typval_T *argvars)
2509{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002510 linenr_T lnum = -1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002511
Bram Moolenaar56acb092020-08-16 14:48:19 +02002512 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2513 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002514 if (lnum <= 0 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002515 {
2516 int fnum;
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002517 pos_T *fp;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002518
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002519 // no valid number, try using arg like line()
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002520 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002521 if (fp != NULL)
2522 lnum = fp->lnum;
2523 }
2524 return lnum;
2525}
2526
2527/*
2528 * Get the lnum from the first argument.
2529 * Also accepts "$", then "buf" is used.
2530 * Returns 0 on error.
2531 */
2532 linenr_T
2533tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2534{
2535 if (argvars[0].v_type == VAR_STRING
2536 && argvars[0].vval.v_string != NULL
2537 && argvars[0].vval.v_string[0] == '$'
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002538 && argvars[0].vval.v_string[1] == NUL
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002539 && buf != NULL)
2540 return buf->b_ml.ml_line_count;
2541 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2542}
2543
2544/*
2545 * Get buffer by number or pattern.
2546 */
2547 buf_T *
2548tv_get_buf(typval_T *tv, int curtab_only)
2549{
2550 char_u *name = tv->vval.v_string;
2551 buf_T *buf;
2552
2553 if (tv->v_type == VAR_NUMBER)
2554 return buflist_findnr((int)tv->vval.v_number);
2555 if (tv->v_type != VAR_STRING)
2556 return NULL;
2557 if (name == NULL || *name == NUL)
2558 return curbuf;
2559 if (name[0] == '$' && name[1] == NUL)
2560 return lastbuf;
2561
2562 buf = buflist_find_by_name(name, curtab_only);
2563
2564 // If not found, try expanding the name, like done for bufexists().
2565 if (buf == NULL)
2566 buf = find_buffer(tv);
2567
2568 return buf;
2569}
2570
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002571/*
2572 * Like tv_get_buf() but give an error message is the type is wrong.
2573 */
2574 buf_T *
2575tv_get_buf_from_arg(typval_T *tv)
2576{
2577 buf_T *buf;
2578
2579 ++emsg_off;
2580 buf = tv_get_buf(tv, FALSE);
2581 --emsg_off;
2582 if (buf == NULL
2583 && tv->v_type != VAR_NUMBER
2584 && tv->v_type != VAR_STRING)
2585 // issue errmsg for type error
2586 (void)tv_get_number(tv);
2587 return buf;
2588}
2589
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002590#endif // FEAT_EVAL