blob: 101f48f34a71e9b1884a1c507899bb658fc6e974 [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/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200512 * Check for an optional list argument at 'idx'
513 */
514 int
515check_for_opt_list_arg(typval_T *args, int idx)
516{
517 return (args[idx].v_type == VAR_UNKNOWN
518 || check_for_list_arg(args, idx) != FAIL);
519}
520
521/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200522 * Give an error and return FAIL unless "args[idx]" is a dict.
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200523 */
524 int
525check_for_dict_arg(typval_T *args, int idx)
526{
527 if (args[idx].v_type != VAR_DICT)
528 {
rbtnnddc80af2021-12-17 18:01:31 +0000529 semsg(_(e_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200530 return FAIL;
531 }
532 return OK;
533}
534
535/*
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100536 * Give an error and return FAIL unless "args[idx]" is a non-NULL dict.
537 */
538 int
539check_for_nonnull_dict_arg(typval_T *args, int idx)
540{
541 if (check_for_dict_arg(args, idx) == FAIL)
542 return FAIL;
543
544 if (args[idx].vval.v_dict == NULL)
545 {
546 semsg(_(e_non_null_dict_required_for_argument_nr), idx + 1);
547 return FAIL;
548 }
549 return OK;
550}
551
552/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200553 * Check for an optional dict argument at 'idx'
554 */
555 int
556check_for_opt_dict_arg(typval_T *args, int idx)
557{
558 return (args[idx].v_type == VAR_UNKNOWN
559 || check_for_dict_arg(args, idx) != FAIL);
560}
561
Dominique Pelle748b3082022-01-08 12:41:16 +0000562#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200563/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200564 * Give an error and return FAIL unless "args[idx]" is a channel or a job.
565 */
566 int
567check_for_chan_or_job_arg(typval_T *args, int idx)
568{
569 if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
570 {
rbtnnddc80af2021-12-17 18:01:31 +0000571 semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200572 return FAIL;
573 }
574 return OK;
575}
576
577/*
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200578 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
579 * job.
580 */
581 int
582check_for_opt_chan_or_job_arg(typval_T *args, int idx)
583{
584 return (args[idx].v_type == VAR_UNKNOWN
585 || check_for_chan_or_job_arg(args, idx) != FAIL);
586}
587
588/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200589 * Give an error and return FAIL unless "args[idx]" is a job.
590 */
591 int
592check_for_job_arg(typval_T *args, int idx)
593{
594 if (args[idx].v_type != VAR_JOB)
595 {
rbtnnddc80af2021-12-17 18:01:31 +0000596 semsg(_(e_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200597 return FAIL;
598 }
599 return OK;
600}
601
602/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200603 * Check for an optional job argument at 'idx'.
604 */
605 int
606check_for_opt_job_arg(typval_T *args, int idx)
607{
608 return (args[idx].v_type == VAR_UNKNOWN
609 || check_for_job_arg(args, idx) != FAIL);
610}
Dominique Pelle748b3082022-01-08 12:41:16 +0000611#endif
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200612
613/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200614 * Give an error and return FAIL unless "args[idx]" is a string or
615 * a number.
616 */
617 int
618check_for_string_or_number_arg(typval_T *args, int idx)
619{
620 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
621 {
rbtnnddc80af2021-12-17 18:01:31 +0000622 semsg(_(e_string_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200623 return FAIL;
624 }
625 return OK;
626}
627
628/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200629 * Check for an optional string or number argument at 'idx'.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200630 */
631 int
632check_for_opt_string_or_number_arg(typval_T *args, int idx)
633{
634 return (args[idx].v_type == VAR_UNKNOWN
635 || check_for_string_or_number_arg(args, idx) != FAIL);
636}
637
638/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200639 * Give an error and return FAIL unless "args[idx]" is a buffer number.
640 * Buffer number can be a number or a string.
641 */
642 int
643check_for_buffer_arg(typval_T *args, int idx)
644{
645 return check_for_string_or_number_arg(args, idx);
646}
647
648/*
649 * Check for an optional buffer argument at 'idx'
650 */
651 int
652check_for_opt_buffer_arg(typval_T *args, int idx)
653{
654 return (args[idx].v_type == VAR_UNKNOWN
655 || check_for_buffer_arg(args, idx));
656}
657
658/*
659 * Give an error and return FAIL unless "args[idx]" is a line number.
660 * Line number can be a number or a string.
661 */
662 int
663check_for_lnum_arg(typval_T *args, int idx)
664{
665 return check_for_string_or_number_arg(args, idx);
666}
667
668/*
669 * Check for an optional line number argument at 'idx'
670 */
671 int
672check_for_opt_lnum_arg(typval_T *args, int idx)
673{
674 return (args[idx].v_type == VAR_UNKNOWN
675 || check_for_lnum_arg(args, idx));
676}
677
Dominique Pelle748b3082022-01-08 12:41:16 +0000678#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200679/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200680 * Give an error and return FAIL unless "args[idx]" is a string or a blob.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200681 */
682 int
683check_for_string_or_blob_arg(typval_T *args, int idx)
684{
685 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
686 {
rbtnnddc80af2021-12-17 18:01:31 +0000687 semsg(_(e_string_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200688 return FAIL;
689 }
690 return OK;
691}
Dominique Pelle748b3082022-01-08 12:41:16 +0000692#endif
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200693
694/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200695 * Give an error and return FAIL unless "args[idx]" is a string or a list.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200696 */
697 int
698check_for_string_or_list_arg(typval_T *args, int idx)
699{
700 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
701 {
rbtnnddc80af2021-12-17 18:01:31 +0000702 semsg(_(e_string_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200703 return FAIL;
704 }
705 return OK;
706}
707
708/*
rbtnn0ccb5842021-12-18 18:33:46 +0000709 * Give an error and return FAIL unless "args[idx]" is a string, a list or a
710 * blob.
711 */
712 int
713check_for_string_or_list_or_blob_arg(typval_T *args, int idx)
714{
715 if (args[idx].v_type != VAR_STRING
716 && args[idx].v_type != VAR_LIST
717 && args[idx].v_type != VAR_BLOB)
718 {
719 semsg(_(e_string_list_or_blob_required_for_argument_nr), idx + 1);
720 return FAIL;
721 }
722 return OK;
723}
724
725/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200726 * Check for an optional string or list argument at 'idx'
727 */
728 int
729check_for_opt_string_or_list_arg(typval_T *args, int idx)
730{
731 return (args[idx].v_type == VAR_UNKNOWN
732 || check_for_string_or_list_arg(args, idx));
733}
734
735/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200736 * Give an error and return FAIL unless "args[idx]" is a string or a dict.
737 */
738 int
739check_for_string_or_dict_arg(typval_T *args, int idx)
740{
741 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_DICT)
742 {
rbtnnddc80af2021-12-17 18:01:31 +0000743 semsg(_(e_string_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200744 return FAIL;
745 }
746 return OK;
747}
748
749/*
750 * Give an error and return FAIL unless "args[idx]" is a string or a number
751 * or a list.
752 */
753 int
754check_for_string_or_number_or_list_arg(typval_T *args, int idx)
755{
756 if (args[idx].v_type != VAR_STRING
757 && args[idx].v_type != VAR_NUMBER
758 && args[idx].v_type != VAR_LIST)
759 {
rbtnn0ccb5842021-12-18 18:33:46 +0000760 semsg(_(e_string_number_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200761 return FAIL;
762 }
763 return OK;
764}
765
766/*
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200767 * Give an error and return FAIL unless "args[idx]" is an optional string
768 * or number or a list
769 */
770 int
771check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx)
772{
773 return (args[idx].v_type == VAR_UNKNOWN
774 || check_for_string_or_number_or_list_arg(args, idx) != FAIL);
775}
776
777/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200778 * Give an error and return FAIL unless "args[idx]" is a string or a list
779 * or a dict.
780 */
781 int
782check_for_string_or_list_or_dict_arg(typval_T *args, int idx)
783{
784 if (args[idx].v_type != VAR_STRING
785 && args[idx].v_type != VAR_LIST
786 && args[idx].v_type != VAR_DICT)
787 {
rbtnnddc80af2021-12-17 18:01:31 +0000788 semsg(_(e_string_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200789 return FAIL;
790 }
791 return OK;
792}
793
794/*
Bram Moolenaar223d0a62021-12-25 19:29:21 +0000795 * Give an error and return FAIL unless "args[idx]" is a string
796 * or a function reference.
797 */
798 int
799check_for_string_or_func_arg(typval_T *args, int idx)
800{
801 if (args[idx].v_type != VAR_PARTIAL
802 && args[idx].v_type != VAR_FUNC
803 && args[idx].v_type != VAR_STRING)
804 {
805 semsg(_(e_string_or_function_required_for_argument_nr), idx + 1);
806 return FAIL;
807 }
808 return OK;
809}
810
811/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200812 * Give an error and return FAIL unless "args[idx]" is a list or a blob.
813 */
814 int
815check_for_list_or_blob_arg(typval_T *args, int idx)
816{
817 if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
818 {
rbtnn0ccb5842021-12-18 18:33:46 +0000819 semsg(_(e_list_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200820 return FAIL;
821 }
822 return OK;
823}
824
825/*
826 * Give an error and return FAIL unless "args[idx]" is a list or dict
827 */
828 int
829check_for_list_or_dict_arg(typval_T *args, int idx)
830{
831 if (args[idx].v_type != VAR_LIST
832 && args[idx].v_type != VAR_DICT)
833 {
rbtnnddc80af2021-12-17 18:01:31 +0000834 semsg(_(e_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200835 return FAIL;
836 }
837 return OK;
838}
839
840/*
841 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
842 * blob.
843 */
844 int
845check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
846{
847 if (args[idx].v_type != VAR_LIST
848 && args[idx].v_type != VAR_DICT
849 && args[idx].v_type != VAR_BLOB)
850 {
rbtnnddc80af2021-12-17 18:01:31 +0000851 semsg(_(e_list_dict_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200852 return FAIL;
853 }
854 return OK;
855}
856
857/*
Bram Moolenaar2d877592021-12-16 08:21:09 +0000858 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
859 * blob or a string.
860 */
861 int
862check_for_list_or_dict_or_blob_or_string_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 && args[idx].v_type != VAR_STRING)
868 {
rbtnnddc80af2021-12-17 18:01:31 +0000869 semsg(_(e_list_dict_blob_or_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2d877592021-12-16 08:21:09 +0000870 return FAIL;
871 }
872 return OK;
873}
874
875/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200876 * Give an error and return FAIL unless "args[idx]" is an optional buffer
877 * number or a dict.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200878 */
879 int
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200880check_for_opt_buffer_or_dict_arg(typval_T *args, int idx)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200881{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200882 if (args[idx].v_type != VAR_UNKNOWN
883 && args[idx].v_type != VAR_STRING
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200884 && args[idx].v_type != VAR_NUMBER
885 && args[idx].v_type != VAR_DICT)
886 {
rbtnnddc80af2021-12-17 18:01:31 +0000887 semsg(_(e_string_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200888 return FAIL;
889 }
890 return OK;
891}
892
893/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200894 * Get the string value of a variable.
895 * If it is a Number variable, the number is converted into a string.
896 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
897 * tv_get_string_buf() uses a given buffer.
898 * If the String variable has never been set, return an empty string.
899 * Never returns NULL;
900 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
901 * NULL on error.
902 */
903 char_u *
904tv_get_string(typval_T *varp)
905{
906 static char_u mybuf[NUMBUFLEN];
907
908 return tv_get_string_buf(varp, mybuf);
909}
910
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100911/*
912 * Like tv_get_string() but don't allow number to string conversion for Vim9.
913 */
914 char_u *
915tv_get_string_strict(typval_T *varp)
916{
917 static char_u mybuf[NUMBUFLEN];
918 char_u *res = tv_get_string_buf_chk_strict(
919 varp, mybuf, in_vim9script());
920
921 return res != NULL ? res : (char_u *)"";
922}
923
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200924 char_u *
925tv_get_string_buf(typval_T *varp, char_u *buf)
926{
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200927 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200928
929 return res != NULL ? res : (char_u *)"";
930}
931
932/*
933 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
934 */
935 char_u *
936tv_get_string_chk(typval_T *varp)
937{
938 static char_u mybuf[NUMBUFLEN];
939
940 return tv_get_string_buf_chk(varp, mybuf);
941}
942
943 char_u *
944tv_get_string_buf_chk(typval_T *varp, char_u *buf)
945{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100946 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
947}
948
949 char_u *
950tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
951{
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200952 switch (varp->v_type)
953 {
954 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100955 if (strict)
956 {
957 emsg(_(e_using_number_as_string));
958 break;
959 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200960 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
961 (varnumber_T)varp->vval.v_number);
962 return buf;
963 case VAR_FUNC:
964 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000965 emsg(_(e_using_funcref_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200966 break;
967 case VAR_LIST:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000968 emsg(_(e_using_list_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200969 break;
970 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000971 emsg(_(e_using_dictionary_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200972 break;
973 case VAR_FLOAT:
974#ifdef FEAT_FLOAT
Bram Moolenaar7a2217b2021-06-06 12:33:49 +0200975 if (strict)
976 {
Bram Moolenaar0699b042022-01-01 16:01:23 +0000977 emsg(_(e_using_float_as_string));
Bram Moolenaar7a2217b2021-06-06 12:33:49 +0200978 break;
979 }
980 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
981 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200982#endif
983 case VAR_STRING:
984 if (varp->vval.v_string != NULL)
985 return varp->vval.v_string;
986 return (char_u *)"";
987 case VAR_BOOL:
988 case VAR_SPECIAL:
989 STRCPY(buf, get_var_special_name(varp->vval.v_number));
990 return buf;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100991 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000992 emsg(_(e_using_blob_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200993 break;
994 case VAR_JOB:
995#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200996 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200997 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200998 semsg(_(e_using_invalid_value_as_string_str), "job");
999 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001000 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001001 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001002#endif
1003 break;
1004 case VAR_CHANNEL:
1005#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001006 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001007 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001008 semsg(_(e_using_invalid_value_as_string_str), "channel");
1009 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001010 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001011 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001012#endif
1013 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02001014 case VAR_VOID:
1015 emsg(_(e_cannot_use_void_value));
1016 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001017 case VAR_UNKNOWN:
1018 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001019 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +02001020 semsg(_(e_using_invalid_value_as_string_str),
1021 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001022 break;
1023 }
1024 return NULL;
1025}
1026
1027/*
1028 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
1029 * string() on Dict, List, etc.
1030 */
1031 char_u *
1032tv_stringify(typval_T *varp, char_u *buf)
1033{
1034 if (varp->v_type == VAR_LIST
1035 || varp->v_type == VAR_DICT
1036 || varp->v_type == VAR_BLOB
1037 || varp->v_type == VAR_FUNC
1038 || varp->v_type == VAR_PARTIAL
1039 || varp->v_type == VAR_FLOAT)
1040 {
1041 typval_T tmp;
1042
1043 f_string(varp, &tmp);
1044 tv_get_string_buf(&tmp, buf);
1045 clear_tv(varp);
1046 *varp = tmp;
1047 return tmp.vval.v_string;
1048 }
1049 return tv_get_string_buf(varp, buf);
1050}
1051
1052/*
1053 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
1054 * Also give an error message, using "name" or _("name") when use_gettext is
1055 * TRUE.
1056 */
1057 int
1058tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
1059{
1060 int lock = 0;
1061
1062 switch (tv->v_type)
1063 {
1064 case VAR_BLOB:
1065 if (tv->vval.v_blob != NULL)
1066 lock = tv->vval.v_blob->bv_lock;
1067 break;
1068 case VAR_LIST:
1069 if (tv->vval.v_list != NULL)
1070 lock = tv->vval.v_list->lv_lock;
1071 break;
1072 case VAR_DICT:
1073 if (tv->vval.v_dict != NULL)
1074 lock = tv->vval.v_dict->dv_lock;
1075 break;
1076 default:
1077 break;
1078 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001079 return value_check_lock(tv->v_lock, name, use_gettext)
1080 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001081}
1082
1083/*
1084 * Copy the values from typval_T "from" to typval_T "to".
1085 * When needed allocates string or increases reference count.
1086 * Does not make a copy of a list, blob or dict but copies the reference!
1087 * It is OK for "from" and "to" to point to the same item. This is used to
1088 * make a copy later.
1089 */
1090 void
1091copy_tv(typval_T *from, typval_T *to)
1092{
1093 to->v_type = from->v_type;
1094 to->v_lock = 0;
1095 switch (from->v_type)
1096 {
1097 case VAR_NUMBER:
1098 case VAR_BOOL:
1099 case VAR_SPECIAL:
1100 to->vval.v_number = from->vval.v_number;
1101 break;
1102 case VAR_FLOAT:
1103#ifdef FEAT_FLOAT
1104 to->vval.v_float = from->vval.v_float;
1105 break;
1106#endif
1107 case VAR_JOB:
1108#ifdef FEAT_JOB_CHANNEL
1109 to->vval.v_job = from->vval.v_job;
1110 if (to->vval.v_job != NULL)
1111 ++to->vval.v_job->jv_refcount;
1112 break;
1113#endif
1114 case VAR_CHANNEL:
1115#ifdef FEAT_JOB_CHANNEL
1116 to->vval.v_channel = from->vval.v_channel;
1117 if (to->vval.v_channel != NULL)
1118 ++to->vval.v_channel->ch_refcount;
1119 break;
1120#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001121 case VAR_INSTR:
1122 to->vval.v_instr = from->vval.v_instr;
1123 break;
1124
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001125 case VAR_STRING:
1126 case VAR_FUNC:
1127 if (from->vval.v_string == NULL)
1128 to->vval.v_string = NULL;
1129 else
1130 {
1131 to->vval.v_string = vim_strsave(from->vval.v_string);
1132 if (from->v_type == VAR_FUNC)
1133 func_ref(to->vval.v_string);
1134 }
1135 break;
1136 case VAR_PARTIAL:
1137 if (from->vval.v_partial == NULL)
1138 to->vval.v_partial = NULL;
1139 else
1140 {
1141 to->vval.v_partial = from->vval.v_partial;
1142 ++to->vval.v_partial->pt_refcount;
1143 }
1144 break;
1145 case VAR_BLOB:
1146 if (from->vval.v_blob == NULL)
1147 to->vval.v_blob = NULL;
1148 else
1149 {
1150 to->vval.v_blob = from->vval.v_blob;
1151 ++to->vval.v_blob->bv_refcount;
1152 }
1153 break;
1154 case VAR_LIST:
1155 if (from->vval.v_list == NULL)
1156 to->vval.v_list = NULL;
1157 else
1158 {
1159 to->vval.v_list = from->vval.v_list;
1160 ++to->vval.v_list->lv_refcount;
1161 }
1162 break;
1163 case VAR_DICT:
1164 if (from->vval.v_dict == NULL)
1165 to->vval.v_dict = NULL;
1166 else
1167 {
1168 to->vval.v_dict = from->vval.v_dict;
1169 ++to->vval.v_dict->dv_refcount;
1170 }
1171 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +02001172 case VAR_VOID:
1173 emsg(_(e_cannot_use_void_value));
1174 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001175 case VAR_UNKNOWN:
1176 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001177 internal_error_no_abort("copy_tv(UNKNOWN)");
1178 break;
1179 }
1180}
1181
1182/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001183 * Compare "tv1" and "tv2".
1184 * Put the result in "tv1". Caller should clear "tv2".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001185 */
1186 int
1187typval_compare(
Bram Moolenaar265f8112021-12-19 12:33:05 +00001188 typval_T *tv1, // first operand
1189 typval_T *tv2, // second operand
1190 exprtype_T type, // operator
1191 int ic) // ignore case
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001192{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001193 varnumber_T n1, n2;
Bram Moolenaar265f8112021-12-19 12:33:05 +00001194 int res = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001195 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1196
Bram Moolenaar265f8112021-12-19 12:33:05 +00001197 if (type_is && tv1->v_type != tv2->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001198 {
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001199 // For "is" a different type always means FALSE, for "isnot"
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001200 // it means TRUE.
1201 n1 = (type == EXPR_ISNOT);
1202 }
Bram Moolenaar7a222242022-03-01 19:23:24 +00001203 else if (((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1204 || (tv2->v_type == VAR_SPECIAL
1205 && tv2->vval.v_number == VVAL_NULL))
1206 && tv1->v_type != tv2->v_type
1207 && (type == EXPR_EQUAL || type == EXPR_NEQUAL))
1208 {
1209 n1 = typval_compare_null(tv1, tv2);
1210 if (n1 == MAYBE)
1211 {
1212 clear_tv(tv1);
1213 return FAIL;
1214 }
1215 if (type == EXPR_NEQUAL)
1216 n1 = !n1;
1217 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001218 else if (tv1->v_type == VAR_BLOB || tv2->v_type == VAR_BLOB)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001219 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001220 if (typval_compare_blob(tv1, tv2, type, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001221 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001222 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001223 return FAIL;
1224 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001225 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001226 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001227 else if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001228 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001229 if (typval_compare_list(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001230 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001231 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001232 return FAIL;
1233 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001234 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001235 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001236 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001237 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001238 if (typval_compare_dict(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001239 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001240 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001241 return FAIL;
1242 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001243 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001244 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001245 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC
1246 || tv1->v_type == VAR_PARTIAL || tv2->v_type == VAR_PARTIAL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001247 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001248 if (typval_compare_func(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001249 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001250 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001251 return FAIL;
1252 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001253 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001254 }
1255
1256#ifdef FEAT_FLOAT
1257 // If one of the two variables is a float, compare as a float.
1258 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001259 else if ((tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001260 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1261 {
1262 float_T f1, f2;
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001263 int error = FALSE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001264
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001265 f1 = tv_get_float_chk(tv1, &error);
1266 if (!error)
1267 f2 = tv_get_float_chk(tv2, &error);
1268 if (error)
1269 {
1270 clear_tv(tv1);
1271 return FAIL;
1272 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001273 n1 = FALSE;
1274 switch (type)
1275 {
1276 case EXPR_IS:
1277 case EXPR_EQUAL: n1 = (f1 == f2); break;
1278 case EXPR_ISNOT:
1279 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1280 case EXPR_GREATER: n1 = (f1 > f2); break;
1281 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1282 case EXPR_SMALLER: n1 = (f1 < f2); break;
1283 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1284 case EXPR_UNKNOWN:
1285 case EXPR_MATCH:
1286 default: break; // avoid gcc warning
1287 }
1288 }
1289#endif
1290
1291 // If one of the two variables is a number, compare as a number.
1292 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001293 else if ((tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001294 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1295 {
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001296 int error = FALSE;
1297
1298 n1 = tv_get_number_chk(tv1, &error);
1299 if (!error)
1300 n2 = tv_get_number_chk(tv2, &error);
1301 if (error)
1302 {
1303 clear_tv(tv1);
1304 return FAIL;
1305 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001306 switch (type)
1307 {
1308 case EXPR_IS:
1309 case EXPR_EQUAL: n1 = (n1 == n2); break;
1310 case EXPR_ISNOT:
1311 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1312 case EXPR_GREATER: n1 = (n1 > n2); break;
1313 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1314 case EXPR_SMALLER: n1 = (n1 < n2); break;
1315 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1316 case EXPR_UNKNOWN:
1317 case EXPR_MATCH:
1318 default: break; // avoid gcc warning
1319 }
1320 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001321 else if (in_vim9script() && (tv1->v_type == VAR_BOOL
1322 || tv2->v_type == VAR_BOOL
1323 || (tv1->v_type == VAR_SPECIAL
1324 && tv2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001325 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001326 if (tv1->v_type != tv2->v_type)
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001327 {
1328 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001329 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1330 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001331 return FAIL;
1332 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001333 n1 = tv1->vval.v_number;
1334 n2 = tv2->vval.v_number;
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001335 switch (type)
1336 {
1337 case EXPR_IS:
1338 case EXPR_EQUAL: n1 = (n1 == n2); break;
1339 case EXPR_ISNOT:
1340 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1341 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001342 semsg(_(e_invalid_operation_for_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001343 vartype_name(tv1->v_type));
1344 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001345 return FAIL;
1346 }
1347 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001348#ifdef FEAT_JOB_CHANNEL
1349 else if (tv1->v_type == tv2->v_type
1350 && (tv1->v_type == VAR_CHANNEL || tv1->v_type == VAR_JOB)
1351 && (type == EXPR_NEQUAL || type == EXPR_EQUAL))
1352 {
1353 if (tv1->v_type == VAR_CHANNEL)
1354 n1 = tv1->vval.v_channel == tv2->vval.v_channel;
1355 else
1356 n1 = tv1->vval.v_job == tv2->vval.v_job;
1357 if (type == EXPR_NEQUAL)
1358 n1 = !n1;
1359 }
1360#endif
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001361 else
1362 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001363 if (typval_compare_string(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar0c357522021-07-18 14:43:43 +02001364 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001365 clear_tv(tv1);
Bram Moolenaar0c357522021-07-18 14:43:43 +02001366 return FAIL;
1367 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001368 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001369 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001370 clear_tv(tv1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001371 if (in_vim9script())
1372 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001373 tv1->v_type = VAR_BOOL;
1374 tv1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001375 }
1376 else
1377 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001378 tv1->v_type = VAR_NUMBER;
1379 tv1->vval.v_number = n1;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001380 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001381
1382 return OK;
1383}
1384
Bram Moolenaar34453202021-01-31 13:08:38 +01001385/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001386 * Compare "tv1" to "tv2" as lists acording to "type" and "ic".
1387 * Put the result, false or true, in "res".
1388 * Return FAIL and give an error message when the comparison can't be done.
1389 */
1390 int
1391typval_compare_list(
1392 typval_T *tv1,
1393 typval_T *tv2,
1394 exprtype_T type,
1395 int ic,
1396 int *res)
1397{
1398 int val = 0;
1399
1400 if (type == EXPR_IS || type == EXPR_ISNOT)
1401 {
1402 val = (tv1->v_type == tv2->v_type
1403 && tv1->vval.v_list == tv2->vval.v_list);
1404 if (type == EXPR_ISNOT)
1405 val = !val;
1406 }
1407 else if (tv1->v_type != tv2->v_type
1408 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1409 {
1410 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001411 emsg(_(e_can_only_compare_list_with_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001412 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001413 emsg(_(e_invalid_operation_for_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001414 return FAIL;
1415 }
1416 else
1417 {
1418 val = list_equal(tv1->vval.v_list, tv2->vval.v_list,
1419 ic, FALSE);
1420 if (type == EXPR_NEQUAL)
1421 val = !val;
1422 }
1423 *res = val;
1424 return OK;
1425}
1426
1427/*
Bram Moolenaarf3507a52022-03-09 11:56:21 +00001428 * Compare v:null with another type. Return TRUE if the value is NULL.
Bram Moolenaar7a222242022-03-01 19:23:24 +00001429 */
1430 int
1431typval_compare_null(typval_T *tv1, typval_T *tv2)
1432{
1433 if ((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1434 || (tv2->v_type == VAR_SPECIAL && tv2->vval.v_number == VVAL_NULL))
1435 {
1436 typval_T *tv = tv1->v_type == VAR_SPECIAL ? tv2 : tv1;
1437
1438 switch (tv->v_type)
1439 {
1440 case VAR_BLOB: return tv->vval.v_blob == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001441#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001442 case VAR_CHANNEL: return tv->vval.v_channel == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001443#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001444 case VAR_DICT: return tv->vval.v_dict == NULL;
1445 case VAR_FUNC: return tv->vval.v_string == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001446#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001447 case VAR_JOB: return tv->vval.v_job == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001448#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001449 case VAR_LIST: return tv->vval.v_list == NULL;
1450 case VAR_PARTIAL: return tv->vval.v_partial == NULL;
1451 case VAR_STRING: return tv->vval.v_string == NULL;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001452
1453 case VAR_NUMBER: if (!in_vim9script())
1454 return tv->vval.v_number == 0;
1455 break;
1456#ifdef FEAT_FLOAT
1457 case VAR_FLOAT: if (!in_vim9script())
1458 return tv->vval.v_float == 0.0;
1459 break;
1460#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001461 default: break;
1462 }
1463 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001464 // although comparing null with number, float or bool is not very useful
Bram Moolenaar05667812022-03-15 20:21:33 +00001465 // we won't give an error
1466 return FALSE;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001467}
1468
1469/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001470 * Compare "tv1" to "tv2" as blobs acording to "type".
1471 * Put the result, false or true, in "res".
1472 * Return FAIL and give an error message when the comparison can't be done.
1473 */
1474 int
1475typval_compare_blob(
1476 typval_T *tv1,
1477 typval_T *tv2,
1478 exprtype_T type,
1479 int *res)
1480{
1481 int val = 0;
1482
1483 if (type == EXPR_IS || type == EXPR_ISNOT)
1484 {
1485 val = (tv1->v_type == tv2->v_type
1486 && tv1->vval.v_blob == tv2->vval.v_blob);
1487 if (type == EXPR_ISNOT)
1488 val = !val;
1489 }
1490 else if (tv1->v_type != tv2->v_type
1491 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1492 {
1493 if (tv1->v_type != tv2->v_type)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001494 emsg(_(e_can_only_compare_blob_with_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001495 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001496 emsg(_(e_invalid_operation_for_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001497 return FAIL;
1498 }
1499 else
1500 {
1501 val = blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1502 if (type == EXPR_NEQUAL)
1503 val = !val;
1504 }
1505 *res = val;
1506 return OK;
1507}
1508
1509/*
1510 * Compare "tv1" to "tv2" as dictionaries acording to "type" and "ic".
1511 * Put the result, false or true, in "res".
1512 * Return FAIL and give an error message when the comparison can't be done.
1513 */
1514 int
1515typval_compare_dict(
1516 typval_T *tv1,
1517 typval_T *tv2,
1518 exprtype_T type,
1519 int ic,
1520 int *res)
1521{
1522 int val;
1523
1524 if (type == EXPR_IS || type == EXPR_ISNOT)
1525 {
1526 val = (tv1->v_type == tv2->v_type
1527 && tv1->vval.v_dict == tv2->vval.v_dict);
1528 if (type == EXPR_ISNOT)
1529 val = !val;
1530 }
1531 else if (tv1->v_type != tv2->v_type
1532 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1533 {
1534 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001535 emsg(_(e_can_only_compare_dictionary_with_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001536 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001537 emsg(_(e_invalid_operation_for_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001538 return FAIL;
1539 }
1540 else
1541 {
1542 val = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, FALSE);
1543 if (type == EXPR_NEQUAL)
1544 val = !val;
1545 }
1546 *res = val;
1547 return OK;
1548}
1549
1550/*
1551 * Compare "tv1" to "tv2" as funcrefs acording to "type" and "ic".
1552 * Put the result, false or true, in "res".
1553 * Return FAIL and give an error message when the comparison can't be done.
1554 */
1555 int
1556typval_compare_func(
1557 typval_T *tv1,
1558 typval_T *tv2,
1559 exprtype_T type,
1560 int ic,
1561 int *res)
1562{
1563 int val = 0;
1564
1565 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1566 && type != EXPR_IS && type != EXPR_ISNOT)
1567 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001568 emsg(_(e_invalid_operation_for_funcrefs));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001569 return FAIL;
1570 }
1571 if ((tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial == NULL)
1572 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial == NULL))
1573 // When both partials are NULL, then they are equal.
1574 // Otherwise they are not equal.
1575 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1576 else if (type == EXPR_IS || type == EXPR_ISNOT)
1577 {
1578 if (tv1->v_type == VAR_FUNC && tv2->v_type == VAR_FUNC)
1579 // strings are considered the same if their value is
1580 // the same
1581 val = tv_equal(tv1, tv2, ic, FALSE);
1582 else if (tv1->v_type == VAR_PARTIAL && tv2->v_type == VAR_PARTIAL)
1583 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1584 else
1585 val = FALSE;
1586 }
1587 else
1588 val = tv_equal(tv1, tv2, ic, FALSE);
1589 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1590 val = !val;
1591 *res = val;
1592 return OK;
1593}
1594
1595/*
1596 * Compare "tv1" to "tv2" as strings according to "type" and "ic".
1597 * Put the result, false or true, in "res".
1598 * Return FAIL and give an error message when the comparison can't be done.
1599 */
1600 int
1601typval_compare_string(
1602 typval_T *tv1,
1603 typval_T *tv2,
1604 exprtype_T type,
1605 int ic,
1606 int *res)
1607{
1608 int i = 0;
1609 int val = FALSE;
1610 char_u *s1, *s2;
1611 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1612
1613 if (in_vim9script()
1614 && ((tv1->v_type != VAR_STRING && tv1->v_type != VAR_SPECIAL)
1615 || (tv2->v_type != VAR_STRING && tv2->v_type != VAR_SPECIAL)))
1616 {
1617 semsg(_(e_cannot_compare_str_with_str),
1618 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1619 return FAIL;
1620 }
1621 s1 = tv_get_string_buf(tv1, buf1);
1622 s2 = tv_get_string_buf(tv2, buf2);
1623 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1624 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1625 switch (type)
1626 {
Bram Moolenaarf8691002022-03-10 12:20:53 +00001627 case EXPR_IS: if (in_vim9script())
1628 {
1629 // Really check it is the same string, not just
1630 // the same value.
1631 val = tv1->vval.v_string == tv2->vval.v_string;
1632 break;
1633 }
1634 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001635 case EXPR_EQUAL: val = (i == 0); break;
Bram Moolenaarf8691002022-03-10 12:20:53 +00001636 case EXPR_ISNOT: if (in_vim9script())
1637 {
1638 // Really check it is not the same string, not
1639 // just a different value.
1640 val = tv1->vval.v_string != tv2->vval.v_string;
1641 break;
1642 }
1643 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001644 case EXPR_NEQUAL: val = (i != 0); break;
1645 case EXPR_GREATER: val = (i > 0); break;
1646 case EXPR_GEQUAL: val = (i >= 0); break;
1647 case EXPR_SMALLER: val = (i < 0); break;
1648 case EXPR_SEQUAL: val = (i <= 0); break;
1649
1650 case EXPR_MATCH:
1651 case EXPR_NOMATCH:
1652 val = pattern_match(s2, s1, ic);
1653 if (type == EXPR_NOMATCH)
1654 val = !val;
1655 break;
1656
1657 default: break; // avoid gcc warning
1658 }
1659 *res = val;
1660 return OK;
1661}
1662/*
Bram Moolenaar34453202021-01-31 13:08:38 +01001663 * Convert any type to a string, never give an error.
1664 * When "quotes" is TRUE add quotes to a string.
1665 * Returns an allocated string.
1666 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001667 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001668typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001669{
1670 char_u *tofree;
1671 char_u numbuf[NUMBUFLEN];
1672 char_u *ret = NULL;
1673
1674 if (arg == NULL)
1675 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001676 if (!quotes && arg->v_type == VAR_STRING)
1677 {
1678 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1679 : arg->vval.v_string);
1680 }
1681 else
1682 {
1683 ret = tv2string(arg, &tofree, numbuf, 0);
1684 // Make a copy if we have a value but it's not in allocated memory.
1685 if (ret != NULL && tofree == NULL)
1686 ret = vim_strsave(ret);
1687 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001688 return ret;
1689}
1690
1691/*
1692 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1693 * or it refers to a List or Dictionary that is locked.
1694 */
1695 int
1696tv_islocked(typval_T *tv)
1697{
1698 return (tv->v_lock & VAR_LOCKED)
1699 || (tv->v_type == VAR_LIST
1700 && tv->vval.v_list != NULL
1701 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1702 || (tv->v_type == VAR_DICT
1703 && tv->vval.v_dict != NULL
1704 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1705}
1706
1707 static int
1708func_equal(
1709 typval_T *tv1,
1710 typval_T *tv2,
1711 int ic) // ignore case
1712{
1713 char_u *s1, *s2;
1714 dict_T *d1, *d2;
1715 int a1, a2;
1716 int i;
1717
1718 // empty and NULL function name considered the same
1719 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1720 : partial_name(tv1->vval.v_partial);
1721 if (s1 != NULL && *s1 == NUL)
1722 s1 = NULL;
1723 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1724 : partial_name(tv2->vval.v_partial);
1725 if (s2 != NULL && *s2 == NUL)
1726 s2 = NULL;
1727 if (s1 == NULL || s2 == NULL)
1728 {
1729 if (s1 != s2)
1730 return FALSE;
1731 }
1732 else if (STRCMP(s1, s2) != 0)
1733 return FALSE;
1734
1735 // empty dict and NULL dict is different
1736 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1737 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1738 if (d1 == NULL || d2 == NULL)
1739 {
1740 if (d1 != d2)
1741 return FALSE;
1742 }
1743 else if (!dict_equal(d1, d2, ic, TRUE))
1744 return FALSE;
1745
1746 // empty list and no list considered the same
1747 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1748 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1749 if (a1 != a2)
1750 return FALSE;
1751 for (i = 0; i < a1; ++i)
1752 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1753 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1754 return FALSE;
1755
1756 return TRUE;
1757}
1758
1759/*
1760 * Return TRUE if "tv1" and "tv2" have the same value.
1761 * Compares the items just like "==" would compare them, but strings and
1762 * numbers are different. Floats and numbers are also different.
1763 */
1764 int
1765tv_equal(
1766 typval_T *tv1,
1767 typval_T *tv2,
1768 int ic, // ignore case
1769 int recursive) // TRUE when used recursively
1770{
1771 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1772 char_u *s1, *s2;
1773 static int recursive_cnt = 0; // catch recursive loops
1774 int r;
1775 static int tv_equal_recurse_limit;
1776
1777 // Catch lists and dicts that have an endless loop by limiting
1778 // recursiveness to a limit. We guess they are equal then.
1779 // A fixed limit has the problem of still taking an awful long time.
1780 // Reduce the limit every time running into it. That should work fine for
1781 // deeply linked structures that are not recursively linked and catch
1782 // recursiveness quickly.
1783 if (!recursive)
1784 tv_equal_recurse_limit = 1000;
1785 if (recursive_cnt >= tv_equal_recurse_limit)
1786 {
1787 --tv_equal_recurse_limit;
1788 return TRUE;
1789 }
1790
1791 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1792 // arguments.
1793 if ((tv1->v_type == VAR_FUNC
1794 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1795 && (tv2->v_type == VAR_FUNC
1796 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1797 {
1798 ++recursive_cnt;
1799 r = func_equal(tv1, tv2, ic);
1800 --recursive_cnt;
1801 return r;
1802 }
1803
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001804 if (tv1->v_type != tv2->v_type
1805 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1806 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001807 return FALSE;
1808
1809 switch (tv1->v_type)
1810 {
1811 case VAR_LIST:
1812 ++recursive_cnt;
1813 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1814 --recursive_cnt;
1815 return r;
1816
1817 case VAR_DICT:
1818 ++recursive_cnt;
1819 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1820 --recursive_cnt;
1821 return r;
1822
1823 case VAR_BLOB:
1824 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1825
1826 case VAR_NUMBER:
1827 case VAR_BOOL:
1828 case VAR_SPECIAL:
1829 return tv1->vval.v_number == tv2->vval.v_number;
1830
1831 case VAR_STRING:
1832 s1 = tv_get_string_buf(tv1, buf1);
1833 s2 = tv_get_string_buf(tv2, buf2);
1834 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1835
1836 case VAR_FLOAT:
1837#ifdef FEAT_FLOAT
1838 return tv1->vval.v_float == tv2->vval.v_float;
1839#endif
1840 case VAR_JOB:
1841#ifdef FEAT_JOB_CHANNEL
1842 return tv1->vval.v_job == tv2->vval.v_job;
1843#endif
1844 case VAR_CHANNEL:
1845#ifdef FEAT_JOB_CHANNEL
1846 return tv1->vval.v_channel == tv2->vval.v_channel;
1847#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001848 case VAR_INSTR:
1849 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001850
1851 case VAR_PARTIAL:
1852 return tv1->vval.v_partial == tv2->vval.v_partial;
1853
1854 case VAR_FUNC:
1855 return tv1->vval.v_string == tv2->vval.v_string;
1856
1857 case VAR_UNKNOWN:
1858 case VAR_ANY:
1859 case VAR_VOID:
1860 break;
1861 }
1862
1863 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1864 // does not equal anything, not even itself.
1865 return FALSE;
1866}
1867
1868/*
1869 * Get an option value.
1870 * "arg" points to the '&' or '+' before the option name.
1871 * "arg" is advanced to character after the option name.
1872 * Return OK or FAIL.
1873 */
1874 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001875eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001876 char_u **arg,
1877 typval_T *rettv, // when NULL, only check if option exists
1878 int evaluate)
1879{
1880 char_u *option_end;
1881 long numval;
1882 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001883 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001884 int c;
1885 int working = (**arg == '+'); // has("+option")
1886 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001887 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001888
1889 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001890 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001891 if (option_end == NULL)
1892 {
1893 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001894 semsg(_(e_option_name_missing_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001895 return FAIL;
1896 }
1897
1898 if (!evaluate)
1899 {
1900 *arg = option_end;
1901 return OK;
1902 }
1903
1904 c = *option_end;
1905 *option_end = NUL;
1906 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001907 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001908
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001909 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001910 {
1911 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001912 semsg(_(e_unknown_option_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001913 ret = FAIL;
1914 }
1915 else if (rettv != NULL)
1916 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001917 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001918 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001919 {
1920 rettv->v_type = VAR_STRING;
1921 rettv->vval.v_string = NULL;
1922 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001923 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001924 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001925 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1926 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001927 rettv->vval.v_number = 0;
1928 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001929 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001930 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001931 if (in_vim9script() && opt_type == gov_bool)
1932 {
1933 rettv->v_type = VAR_BOOL;
1934 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1935 }
1936 else
1937 {
1938 rettv->v_type = VAR_NUMBER;
1939 rettv->vval.v_number = numval;
1940 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001941 }
1942 else // string option
1943 {
1944 rettv->v_type = VAR_STRING;
1945 rettv->vval.v_string = stringval;
1946 }
1947 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001948 else if (working && (opt_type == gov_hidden_bool
1949 || opt_type == gov_hidden_number
1950 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001951 ret = FAIL;
1952
1953 *option_end = c; // put back for error messages
1954 *arg = option_end;
1955
1956 return ret;
1957}
1958
1959/*
1960 * Allocate a variable for a number constant. Also deals with "0z" for blob.
1961 * Return OK or FAIL.
1962 */
1963 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001964eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001965 char_u **arg,
1966 typval_T *rettv,
1967 int evaluate,
1968 int want_string UNUSED)
1969{
1970 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02001971 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001972#ifdef FEAT_FLOAT
1973 char_u *p;
1974 int get_float = FALSE;
1975
1976 // We accept a float when the format matches
1977 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
1978 // strict to avoid backwards compatibility problems.
1979 // With script version 2 and later the leading digit can be
1980 // omitted.
1981 // Don't look for a float after the "." operator, so that
1982 // ":let vers = 1.2.3" doesn't fail.
1983 if (**arg == '.')
1984 p = *arg;
1985 else
Bram Moolenaar29500652021-08-08 15:43:34 +02001986 {
1987 p = *arg + 1;
1988 if (skip_quotes)
1989 for (;;)
1990 {
1991 if (*p == '\'')
1992 ++p;
1993 if (!vim_isdigit(*p))
1994 break;
1995 p = skipdigits(p);
1996 }
1997 else
1998 p = skipdigits(p);
1999 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002000 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
2001 {
2002 get_float = TRUE;
2003 p = skipdigits(p + 2);
2004 if (*p == 'e' || *p == 'E')
2005 {
2006 ++p;
2007 if (*p == '-' || *p == '+')
2008 ++p;
2009 if (!vim_isdigit(*p))
2010 get_float = FALSE;
2011 else
2012 p = skipdigits(p + 1);
2013 }
2014 if (ASCII_ISALPHA(*p) || *p == '.')
2015 get_float = FALSE;
2016 }
2017 if (get_float)
2018 {
2019 float_T f;
2020
Bram Moolenaar29500652021-08-08 15:43:34 +02002021 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002022 if (evaluate)
2023 {
2024 rettv->v_type = VAR_FLOAT;
2025 rettv->vval.v_float = f;
2026 }
2027 }
2028 else
2029#endif
2030 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
2031 {
2032 char_u *bp;
2033 blob_T *blob = NULL; // init for gcc
2034
2035 // Blob constant: 0z0123456789abcdef
2036 if (evaluate)
2037 blob = blob_alloc();
2038 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2039 {
2040 if (!vim_isxdigit(bp[1]))
2041 {
2042 if (blob != NULL)
2043 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002044 emsg(_(e_blob_literal_should_have_an_even_number_of_hex_characters));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002045 ga_clear(&blob->bv_ga);
2046 VIM_CLEAR(blob);
2047 }
2048 return FAIL;
2049 }
2050 if (blob != NULL)
2051 ga_append(&blob->bv_ga,
2052 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
2053 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2054 ++bp;
2055 }
2056 if (blob != NULL)
2057 rettv_blob_set(rettv, blob);
2058 *arg = bp;
2059 }
2060 else
2061 {
2062 varnumber_T n;
2063
2064 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02002065 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002066 ? STR2NR_NO_OCT + STR2NR_QUOTE
2067 : STR2NR_ALL, &n, NULL, 0, TRUE);
2068 if (len == 0)
2069 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00002070 if (evaluate)
2071 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002072 return FAIL;
2073 }
2074 *arg += len;
2075 if (evaluate)
2076 {
2077 rettv->v_type = VAR_NUMBER;
2078 rettv->vval.v_number = n;
2079 }
2080 }
2081 return OK;
2082}
2083
2084/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002085 * Evaluate a string constant and put the result in "rettv".
2086 * "*arg" points to the double quote or to after it when "interpolate" is TRUE.
2087 * When "interpolate" is TRUE reduce "{{" to "{", reduce "}}" to "}" and stop
2088 * at a single "{".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002089 * Return OK or FAIL.
2090 */
2091 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002092eval_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002093{
2094 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002095 char_u *end;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002096 int extra = interpolate ? 1 : 0;
2097 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002098 int len;
2099
2100 // Find the end of the string, skipping backslashed characters.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002101 for (p = *arg + off; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002102 {
2103 if (*p == '\\' && p[1] != NUL)
2104 {
2105 ++p;
2106 // A "\<x>" form occupies at least 4 characters, and produces up
zeertzjqdb088872022-05-02 22:53:45 +01002107 // to 9 characters (6 for the char and 3 for a modifier):
2108 // reserve space for 5 extra.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002109 if (*p == '<')
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002110 {
2111 int modifiers = 0;
2112 int flags = FSK_KEYCODE | FSK_IN_STRING;
2113
zeertzjqdb088872022-05-02 22:53:45 +01002114 extra += 5;
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002115
2116 // Skip to the '>' to avoid using '{' inside for string
2117 // interpolation.
2118 if (p[1] != '*')
2119 flags |= FSK_SIMPLIFY;
2120 if (find_special_key(&p, &modifiers, flags, NULL) != 0)
2121 --p; // leave "p" on the ">"
2122 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002123 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002124 else if (interpolate && (*p == '{' || *p == '}'))
2125 {
2126 if (*p == '{' && p[1] != '{') // start of expression
2127 break;
2128 ++p;
2129 if (p[-1] == '}' && *p != '}') // single '}' is an error
2130 {
2131 semsg(_(e_stray_closing_curly_str), *arg);
2132 return FAIL;
2133 }
2134 --extra; // "{{" becomes "{", "}}" becomes "}"
2135 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002136 }
2137
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002138 if (*p != '"' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002139 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002140 semsg(_(e_missing_double_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002141 return FAIL;
2142 }
2143
2144 // If only parsing, set *arg and return here
2145 if (!evaluate)
2146 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002147 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002148 return OK;
2149 }
2150
2151 // Copy the string into allocated memory, handling backslashed
2152 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002153 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002154 len = (int)(p - *arg + extra);
2155 rettv->vval.v_string = alloc(len);
2156 if (rettv->vval.v_string == NULL)
2157 return FAIL;
2158 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002159
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002160 for (p = *arg + off; *p != NUL && *p != '"'; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002161 {
2162 if (*p == '\\')
2163 {
2164 switch (*++p)
2165 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002166 case 'b': *end++ = BS; ++p; break;
2167 case 'e': *end++ = ESC; ++p; break;
2168 case 'f': *end++ = FF; ++p; break;
2169 case 'n': *end++ = NL; ++p; break;
2170 case 'r': *end++ = CAR; ++p; break;
2171 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002172
2173 case 'X': // hex: "\x1", "\x12"
2174 case 'x':
2175 case 'u': // Unicode: "\u0023"
2176 case 'U':
2177 if (vim_isxdigit(p[1]))
2178 {
2179 int n, nr;
2180 int c = toupper(*p);
2181
2182 if (c == 'X')
2183 n = 2;
2184 else if (*p == 'u')
2185 n = 4;
2186 else
2187 n = 8;
2188 nr = 0;
2189 while (--n >= 0 && vim_isxdigit(p[1]))
2190 {
2191 ++p;
2192 nr = (nr << 4) + hex2nr(*p);
2193 }
2194 ++p;
2195 // For "\u" store the number according to
2196 // 'encoding'.
2197 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002198 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002199 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002200 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002201 }
2202 break;
2203
2204 // octal: "\1", "\12", "\123"
2205 case '0':
2206 case '1':
2207 case '2':
2208 case '3':
2209 case '4':
2210 case '5':
2211 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002212 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002213 if (*p >= '0' && *p <= '7')
2214 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002215 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002216 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002217 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002218 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002219 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002220 break;
2221
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002222 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002223 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002224 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002225 int flags = FSK_KEYCODE | FSK_IN_STRING;
2226
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002227 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002228 flags |= FSK_SIMPLIFY;
zeertzjqdb088872022-05-02 22:53:45 +01002229 extra = trans_special(&p, end, flags, FALSE, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002230 if (extra != 0)
2231 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002232 end += extra;
2233 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002234 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002235 break;
2236 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002237 }
2238 // FALLTHROUGH
2239
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002240 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002241 break;
2242 }
2243 }
2244 else
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002245 {
2246 if (interpolate && (*p == '{' || *p == '}'))
2247 {
2248 if (*p == '{' && p[1] != '{') // start of expression
2249 break;
2250 ++p; // reduce "{{" to "{" and "}}" to "}"
2251 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002252 MB_COPY_CHAR(p, end);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002253 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002254 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002255 *end = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002256 if (*p == '"' && !interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002257 ++p;
2258 *arg = p;
2259
2260 return OK;
2261}
2262
2263/*
2264 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002265 * When "interpolate" is TRUE reduce "{{" to "{" and stop at a single "{".
2266 * Return OK when a "rettv" was set to the string.
2267 * Return FAIL on error, "rettv" is not set.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002268 */
2269 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002270eval_lit_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002271{
2272 char_u *p;
2273 char_u *str;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002274 int reduce = interpolate ? -1 : 0;
2275 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002276
2277 // Find the end of the string, skipping ''.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002278 for (p = *arg + off; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002279 {
2280 if (*p == '\'')
2281 {
2282 if (p[1] != '\'')
2283 break;
2284 ++reduce;
2285 ++p;
2286 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002287 else if (interpolate)
2288 {
2289 if (*p == '{')
2290 {
2291 if (p[1] != '{')
2292 break;
2293 ++p;
2294 ++reduce;
2295 }
2296 else if (*p == '}')
2297 {
2298 ++p;
2299 if (*p != '}')
2300 {
2301 semsg(_(e_stray_closing_curly_str), *arg);
2302 return FAIL;
2303 }
2304 ++reduce;
2305 }
2306 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002307 }
2308
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002309 if (*p != '\'' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002310 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002311 semsg(_(e_missing_single_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002312 return FAIL;
2313 }
2314
2315 // If only parsing return after setting "*arg"
2316 if (!evaluate)
2317 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002318 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002319 return OK;
2320 }
2321
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002322 // Copy the string into allocated memory, handling '' to ' reduction and
2323 // any expressions.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002324 str = alloc((p - *arg) - reduce);
2325 if (str == NULL)
2326 return FAIL;
2327 rettv->v_type = VAR_STRING;
2328 rettv->vval.v_string = str;
2329
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002330 for (p = *arg + off; *p != NUL; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002331 {
2332 if (*p == '\'')
2333 {
2334 if (p[1] != '\'')
2335 break;
2336 ++p;
2337 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002338 else if (interpolate && (*p == '{' || *p == '}'))
2339 {
2340 if (*p == '{' && p[1] != '{')
2341 break;
2342 ++p;
2343 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002344 MB_COPY_CHAR(p, str);
2345 }
2346 *str = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002347 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002348
2349 return OK;
2350}
2351
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002352/*
2353 * Evaluate a single or double quoted string possibly containing expressions.
2354 * "arg" points to the '$'. The result is put in "rettv".
2355 * Returns OK or FAIL.
2356 */
LemonBoy2eaef102022-05-06 13:14:50 +01002357 int
2358eval_interp_string(char_u **arg, typval_T *rettv, int evaluate)
2359{
2360 typval_T tv;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002361 int ret = OK;
2362 int quote;
2363 garray_T ga;
2364 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01002365
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002366 ga_init2(&ga, 1, 80);
2367
2368 // *arg is on the '$' character, move it to the first string character.
2369 ++*arg;
2370 quote = **arg;
2371 ++*arg;
2372
2373 for (;;)
2374 {
2375 // Get the string up to the matching quote or to a single '{'.
2376 // "arg" is advanced to either the quote or the '{'.
2377 if (quote == '"')
2378 ret = eval_string(arg, &tv, evaluate, TRUE);
2379 else
2380 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
2381 if (ret == FAIL)
2382 break;
2383 if (evaluate)
2384 {
2385 ga_concat(&ga, tv.vval.v_string);
2386 clear_tv(&tv);
2387 }
2388
2389 if (**arg != '{')
2390 {
2391 // found terminating quote
2392 ++*arg;
2393 break;
2394 }
Bram Moolenaar70c41242022-05-10 18:11:43 +01002395 p = eval_one_expr_in_str(*arg, &ga, evaluate);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002396 if (p == NULL)
2397 {
2398 ret = FAIL;
2399 break;
2400 }
2401 *arg = p;
2402 }
LemonBoy2eaef102022-05-06 13:14:50 +01002403
2404 rettv->v_type = VAR_STRING;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002405 if (ret == FAIL || !evaluate || ga_append(&ga, NUL) == FAIL)
2406 {
2407 ga_clear(&ga);
2408 rettv->vval.v_string = NULL;
LemonBoy2eaef102022-05-06 13:14:50 +01002409 return ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002410 }
LemonBoy2eaef102022-05-06 13:14:50 +01002411
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002412 rettv->vval.v_string = ga.ga_data;
2413 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01002414}
2415
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002416/*
2417 * Return a string with the string representation of a variable.
2418 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2419 * "numbuf" is used for a number.
2420 * Puts quotes around strings, so that they can be parsed back by eval().
2421 * May return NULL.
2422 */
2423 char_u *
2424tv2string(
2425 typval_T *tv,
2426 char_u **tofree,
2427 char_u *numbuf,
2428 int copyID)
2429{
2430 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2431}
2432
2433/*
2434 * Get the value of an environment variable.
2435 * "arg" is pointing to the '$'. It is advanced to after the name.
2436 * If the environment variable was not set, silently assume it is empty.
2437 * Return FAIL if the name is invalid.
2438 */
2439 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002440eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002441{
2442 char_u *string = NULL;
2443 int len;
2444 int cc;
2445 char_u *name;
2446 int mustfree = FALSE;
2447
2448 ++*arg;
2449 name = *arg;
2450 len = get_env_len(arg);
2451 if (evaluate)
2452 {
2453 if (len == 0)
2454 return FAIL; // invalid empty name
2455
2456 cc = name[len];
2457 name[len] = NUL;
2458 // first try vim_getenv(), fast for normal environment vars
2459 string = vim_getenv(name, &mustfree);
2460 if (string != NULL && *string != NUL)
2461 {
2462 if (!mustfree)
2463 string = vim_strsave(string);
2464 }
2465 else
2466 {
2467 if (mustfree)
2468 vim_free(string);
2469
2470 // next try expanding things like $VIM and ${HOME}
2471 string = expand_env_save(name - 1);
2472 if (string != NULL && *string == '$')
2473 VIM_CLEAR(string);
2474 }
2475 name[len] = cc;
2476
2477 rettv->v_type = VAR_STRING;
2478 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002479 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002480 }
2481
2482 return OK;
2483}
2484
2485/*
2486 * Get the lnum from the first argument.
2487 * Also accepts ".", "$", etc., but that only works for the current buffer.
2488 * Returns -1 on error.
2489 */
2490 linenr_T
2491tv_get_lnum(typval_T *argvars)
2492{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002493 linenr_T lnum = -1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002494
Bram Moolenaar56acb092020-08-16 14:48:19 +02002495 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2496 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002497 if (lnum <= 0 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002498 {
2499 int fnum;
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002500 pos_T *fp;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002501
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002502 // no valid number, try using arg like line()
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002503 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002504 if (fp != NULL)
2505 lnum = fp->lnum;
2506 }
2507 return lnum;
2508}
2509
2510/*
2511 * Get the lnum from the first argument.
2512 * Also accepts "$", then "buf" is used.
2513 * Returns 0 on error.
2514 */
2515 linenr_T
2516tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2517{
2518 if (argvars[0].v_type == VAR_STRING
2519 && argvars[0].vval.v_string != NULL
2520 && argvars[0].vval.v_string[0] == '$'
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002521 && argvars[0].vval.v_string[1] == NUL
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002522 && buf != NULL)
2523 return buf->b_ml.ml_line_count;
2524 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2525}
2526
2527/*
2528 * Get buffer by number or pattern.
2529 */
2530 buf_T *
2531tv_get_buf(typval_T *tv, int curtab_only)
2532{
2533 char_u *name = tv->vval.v_string;
2534 buf_T *buf;
2535
2536 if (tv->v_type == VAR_NUMBER)
2537 return buflist_findnr((int)tv->vval.v_number);
2538 if (tv->v_type != VAR_STRING)
2539 return NULL;
2540 if (name == NULL || *name == NUL)
2541 return curbuf;
2542 if (name[0] == '$' && name[1] == NUL)
2543 return lastbuf;
2544
2545 buf = buflist_find_by_name(name, curtab_only);
2546
2547 // If not found, try expanding the name, like done for bufexists().
2548 if (buf == NULL)
2549 buf = find_buffer(tv);
2550
2551 return buf;
2552}
2553
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002554/*
2555 * Like tv_get_buf() but give an error message is the type is wrong.
2556 */
2557 buf_T *
2558tv_get_buf_from_arg(typval_T *tv)
2559{
2560 buf_T *buf;
2561
2562 ++emsg_off;
2563 buf = tv_get_buf(tv, FALSE);
2564 --emsg_off;
2565 if (buf == NULL
2566 && tv->v_type != VAR_NUMBER
2567 && tv->v_type != VAR_STRING)
2568 // issue errmsg for type error
2569 (void)tv_get_number(tv);
2570 return buf;
2571}
2572
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002573#endif // FEAT_EVAL