blob: 9bcc7818848292a5810242d320e2809f43cf4f1e [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
zeertzjqcd2d5c12022-09-12 14:09:30 +0100413 || check_for_string_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200414}
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
zeertzjqcd2d5c12022-09-12 14:09:30 +0100437 || check_for_number_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200438}
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
zeertzjqcd2d5c12022-09-12 14:09:30 +0100535 || check_for_list_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200536}
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
zeertzjqcd2d5c12022-09-12 14:09:30 +0100576 || check_for_dict_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200577}
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
zeertzjqcd2d5c12022-09-12 14:09:30 +0100602 || check_for_chan_or_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200603}
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
zeertzjqcd2d5c12022-09-12 14:09:30 +0100626 || check_for_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200627}
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
zeertzjqcd2d5c12022-09-12 14:09:30 +0100652 || check_for_string_or_number_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200653}
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
zeertzjqcd2d5c12022-09-12 14:09:30 +0100672 || check_for_buffer_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200673}
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
zeertzjqcd2d5c12022-09-12 14:09:30 +0100692 || check_for_lnum_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200693}
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
zeertzjqcd2d5c12022-09-12 14:09:30 +0100749 || check_for_string_or_list_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200750}
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
zeertzjqcd2d5c12022-09-12 14:09:30 +0100791 || check_for_string_or_number_or_list_arg(args, idx)
792 != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200793}
794
795/*
Bakudankun375141e2022-09-09 18:46:47 +0100796 * Give an error and return FAIL unless "args[idx]" is a string or a number
797 * or a list or a blob.
798 */
799 int
800check_for_string_or_number_or_list_or_blob_arg(typval_T *args, int idx)
801{
802 if (args[idx].v_type != VAR_STRING
803 && args[idx].v_type != VAR_NUMBER
804 && args[idx].v_type != VAR_LIST
805 && args[idx].v_type != VAR_BLOB)
806 {
807 semsg(_(e_string_number_list_or_blob_required_for_argument_nr), idx + 1);
808 return FAIL;
809 }
810 return OK;
811}
812
813/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200814 * Give an error and return FAIL unless "args[idx]" is a string or a list
815 * or a dict.
816 */
817 int
818check_for_string_or_list_or_dict_arg(typval_T *args, int idx)
819{
820 if (args[idx].v_type != VAR_STRING
821 && args[idx].v_type != VAR_LIST
822 && args[idx].v_type != VAR_DICT)
823 {
rbtnnddc80af2021-12-17 18:01:31 +0000824 semsg(_(e_string_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200825 return FAIL;
826 }
827 return OK;
828}
829
830/*
Bram Moolenaar223d0a62021-12-25 19:29:21 +0000831 * Give an error and return FAIL unless "args[idx]" is a string
832 * or a function reference.
833 */
834 int
835check_for_string_or_func_arg(typval_T *args, int idx)
836{
837 if (args[idx].v_type != VAR_PARTIAL
838 && args[idx].v_type != VAR_FUNC
839 && args[idx].v_type != VAR_STRING)
840 {
841 semsg(_(e_string_or_function_required_for_argument_nr), idx + 1);
842 return FAIL;
843 }
844 return OK;
845}
846
847/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200848 * Give an error and return FAIL unless "args[idx]" is a list or a blob.
849 */
850 int
851check_for_list_or_blob_arg(typval_T *args, int idx)
852{
853 if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
854 {
rbtnn0ccb5842021-12-18 18:33:46 +0000855 semsg(_(e_list_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200856 return FAIL;
857 }
858 return OK;
859}
860
861/*
862 * Give an error and return FAIL unless "args[idx]" is a list or dict
863 */
864 int
865check_for_list_or_dict_arg(typval_T *args, int idx)
866{
867 if (args[idx].v_type != VAR_LIST
868 && args[idx].v_type != VAR_DICT)
869 {
rbtnnddc80af2021-12-17 18:01:31 +0000870 semsg(_(e_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200871 return FAIL;
872 }
873 return OK;
874}
875
876/*
877 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
878 * blob.
879 */
880 int
881check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
882{
883 if (args[idx].v_type != VAR_LIST
884 && args[idx].v_type != VAR_DICT
885 && args[idx].v_type != VAR_BLOB)
886 {
rbtnnddc80af2021-12-17 18:01:31 +0000887 semsg(_(e_list_dict_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200888 return FAIL;
889 }
890 return OK;
891}
892
893/*
Bram Moolenaar2d877592021-12-16 08:21:09 +0000894 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
895 * blob or a string.
896 */
897 int
898check_for_list_or_dict_or_blob_or_string_arg(typval_T *args, int idx)
899{
900 if (args[idx].v_type != VAR_LIST
901 && args[idx].v_type != VAR_DICT
902 && args[idx].v_type != VAR_BLOB
903 && args[idx].v_type != VAR_STRING)
904 {
rbtnnddc80af2021-12-17 18:01:31 +0000905 semsg(_(e_list_dict_blob_or_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2d877592021-12-16 08:21:09 +0000906 return FAIL;
907 }
908 return OK;
909}
910
911/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200912 * Give an error and return FAIL unless "args[idx]" is an optional buffer
913 * number or a dict.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200914 */
915 int
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200916check_for_opt_buffer_or_dict_arg(typval_T *args, int idx)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200917{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200918 if (args[idx].v_type != VAR_UNKNOWN
919 && args[idx].v_type != VAR_STRING
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200920 && args[idx].v_type != VAR_NUMBER
921 && args[idx].v_type != VAR_DICT)
922 {
rbtnnddc80af2021-12-17 18:01:31 +0000923 semsg(_(e_string_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200924 return FAIL;
925 }
926 return OK;
927}
928
929/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200930 * Get the string value of a variable.
931 * If it is a Number variable, the number is converted into a string.
932 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
933 * tv_get_string_buf() uses a given buffer.
934 * If the String variable has never been set, return an empty string.
935 * Never returns NULL;
936 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
937 * NULL on error.
938 */
939 char_u *
940tv_get_string(typval_T *varp)
941{
942 static char_u mybuf[NUMBUFLEN];
943
944 return tv_get_string_buf(varp, mybuf);
945}
946
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100947/*
948 * Like tv_get_string() but don't allow number to string conversion for Vim9.
949 */
950 char_u *
951tv_get_string_strict(typval_T *varp)
952{
953 static char_u mybuf[NUMBUFLEN];
954 char_u *res = tv_get_string_buf_chk_strict(
955 varp, mybuf, in_vim9script());
956
957 return res != NULL ? res : (char_u *)"";
958}
959
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200960 char_u *
961tv_get_string_buf(typval_T *varp, char_u *buf)
962{
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200963 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200964
965 return res != NULL ? res : (char_u *)"";
966}
967
968/*
969 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
970 */
971 char_u *
972tv_get_string_chk(typval_T *varp)
973{
974 static char_u mybuf[NUMBUFLEN];
975
976 return tv_get_string_buf_chk(varp, mybuf);
977}
978
979 char_u *
980tv_get_string_buf_chk(typval_T *varp, char_u *buf)
981{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100982 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
983}
984
985 char_u *
986tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
987{
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200988 switch (varp->v_type)
989 {
990 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100991 if (strict)
992 {
993 emsg(_(e_using_number_as_string));
994 break;
995 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200996 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
997 (varnumber_T)varp->vval.v_number);
998 return buf;
999 case VAR_FUNC:
1000 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001001 emsg(_(e_using_funcref_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001002 break;
1003 case VAR_LIST:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001004 emsg(_(e_using_list_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001005 break;
1006 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001007 emsg(_(e_using_dictionary_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001008 break;
1009 case VAR_FLOAT:
1010#ifdef FEAT_FLOAT
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001011 if (strict)
1012 {
Bram Moolenaar0699b042022-01-01 16:01:23 +00001013 emsg(_(e_using_float_as_string));
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001014 break;
1015 }
1016 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
1017 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001018#endif
1019 case VAR_STRING:
1020 if (varp->vval.v_string != NULL)
1021 return varp->vval.v_string;
1022 return (char_u *)"";
1023 case VAR_BOOL:
1024 case VAR_SPECIAL:
1025 STRCPY(buf, get_var_special_name(varp->vval.v_number));
1026 return buf;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001027 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001028 emsg(_(e_using_blob_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001029 break;
1030 case VAR_JOB:
1031#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001032 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001033 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001034 semsg(_(e_using_invalid_value_as_string_str), "job");
1035 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001036 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001037 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001038#endif
1039 break;
1040 case VAR_CHANNEL:
1041#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001042 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001043 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001044 semsg(_(e_using_invalid_value_as_string_str), "channel");
1045 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001046 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001047 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001048#endif
1049 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02001050 case VAR_VOID:
1051 emsg(_(e_cannot_use_void_value));
1052 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001053 case VAR_UNKNOWN:
1054 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001055 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +02001056 semsg(_(e_using_invalid_value_as_string_str),
1057 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001058 break;
1059 }
1060 return NULL;
1061}
1062
1063/*
1064 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
1065 * string() on Dict, List, etc.
1066 */
1067 char_u *
1068tv_stringify(typval_T *varp, char_u *buf)
1069{
1070 if (varp->v_type == VAR_LIST
1071 || varp->v_type == VAR_DICT
1072 || varp->v_type == VAR_BLOB
1073 || varp->v_type == VAR_FUNC
1074 || varp->v_type == VAR_PARTIAL
1075 || varp->v_type == VAR_FLOAT)
1076 {
1077 typval_T tmp;
1078
1079 f_string(varp, &tmp);
1080 tv_get_string_buf(&tmp, buf);
1081 clear_tv(varp);
1082 *varp = tmp;
1083 return tmp.vval.v_string;
1084 }
1085 return tv_get_string_buf(varp, buf);
1086}
1087
1088/*
1089 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
1090 * Also give an error message, using "name" or _("name") when use_gettext is
1091 * TRUE.
1092 */
1093 int
1094tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
1095{
1096 int lock = 0;
1097
1098 switch (tv->v_type)
1099 {
1100 case VAR_BLOB:
1101 if (tv->vval.v_blob != NULL)
1102 lock = tv->vval.v_blob->bv_lock;
1103 break;
1104 case VAR_LIST:
1105 if (tv->vval.v_list != NULL)
1106 lock = tv->vval.v_list->lv_lock;
1107 break;
1108 case VAR_DICT:
1109 if (tv->vval.v_dict != NULL)
1110 lock = tv->vval.v_dict->dv_lock;
1111 break;
1112 default:
1113 break;
1114 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001115 return value_check_lock(tv->v_lock, name, use_gettext)
1116 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001117}
1118
1119/*
1120 * Copy the values from typval_T "from" to typval_T "to".
1121 * When needed allocates string or increases reference count.
1122 * Does not make a copy of a list, blob or dict but copies the reference!
1123 * It is OK for "from" and "to" to point to the same item. This is used to
1124 * make a copy later.
1125 */
1126 void
1127copy_tv(typval_T *from, typval_T *to)
1128{
1129 to->v_type = from->v_type;
1130 to->v_lock = 0;
1131 switch (from->v_type)
1132 {
1133 case VAR_NUMBER:
1134 case VAR_BOOL:
1135 case VAR_SPECIAL:
1136 to->vval.v_number = from->vval.v_number;
1137 break;
1138 case VAR_FLOAT:
1139#ifdef FEAT_FLOAT
1140 to->vval.v_float = from->vval.v_float;
1141 break;
1142#endif
1143 case VAR_JOB:
1144#ifdef FEAT_JOB_CHANNEL
1145 to->vval.v_job = from->vval.v_job;
1146 if (to->vval.v_job != NULL)
1147 ++to->vval.v_job->jv_refcount;
1148 break;
1149#endif
1150 case VAR_CHANNEL:
1151#ifdef FEAT_JOB_CHANNEL
1152 to->vval.v_channel = from->vval.v_channel;
1153 if (to->vval.v_channel != NULL)
1154 ++to->vval.v_channel->ch_refcount;
1155 break;
1156#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001157 case VAR_INSTR:
1158 to->vval.v_instr = from->vval.v_instr;
1159 break;
1160
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001161 case VAR_STRING:
1162 case VAR_FUNC:
1163 if (from->vval.v_string == NULL)
1164 to->vval.v_string = NULL;
1165 else
1166 {
1167 to->vval.v_string = vim_strsave(from->vval.v_string);
1168 if (from->v_type == VAR_FUNC)
1169 func_ref(to->vval.v_string);
1170 }
1171 break;
1172 case VAR_PARTIAL:
1173 if (from->vval.v_partial == NULL)
1174 to->vval.v_partial = NULL;
1175 else
1176 {
1177 to->vval.v_partial = from->vval.v_partial;
1178 ++to->vval.v_partial->pt_refcount;
1179 }
1180 break;
1181 case VAR_BLOB:
1182 if (from->vval.v_blob == NULL)
1183 to->vval.v_blob = NULL;
1184 else
1185 {
1186 to->vval.v_blob = from->vval.v_blob;
1187 ++to->vval.v_blob->bv_refcount;
1188 }
1189 break;
1190 case VAR_LIST:
1191 if (from->vval.v_list == NULL)
1192 to->vval.v_list = NULL;
1193 else
1194 {
1195 to->vval.v_list = from->vval.v_list;
1196 ++to->vval.v_list->lv_refcount;
1197 }
1198 break;
1199 case VAR_DICT:
1200 if (from->vval.v_dict == NULL)
1201 to->vval.v_dict = NULL;
1202 else
1203 {
1204 to->vval.v_dict = from->vval.v_dict;
1205 ++to->vval.v_dict->dv_refcount;
1206 }
1207 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +02001208 case VAR_VOID:
1209 emsg(_(e_cannot_use_void_value));
1210 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001211 case VAR_UNKNOWN:
1212 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001213 internal_error_no_abort("copy_tv(UNKNOWN)");
1214 break;
1215 }
1216}
1217
1218/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001219 * Compare "tv1" and "tv2".
1220 * Put the result in "tv1". Caller should clear "tv2".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001221 */
1222 int
1223typval_compare(
Bram Moolenaar265f8112021-12-19 12:33:05 +00001224 typval_T *tv1, // first operand
1225 typval_T *tv2, // second operand
1226 exprtype_T type, // operator
1227 int ic) // ignore case
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001228{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001229 varnumber_T n1, n2;
Bram Moolenaar265f8112021-12-19 12:33:05 +00001230 int res = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001231 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1232
Bram Moolenaar265f8112021-12-19 12:33:05 +00001233 if (type_is && tv1->v_type != tv2->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001234 {
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001235 // For "is" a different type always means FALSE, for "isnot"
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001236 // it means TRUE.
1237 n1 = (type == EXPR_ISNOT);
1238 }
Bram Moolenaar7a222242022-03-01 19:23:24 +00001239 else if (((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1240 || (tv2->v_type == VAR_SPECIAL
1241 && tv2->vval.v_number == VVAL_NULL))
1242 && tv1->v_type != tv2->v_type
1243 && (type == EXPR_EQUAL || type == EXPR_NEQUAL))
1244 {
1245 n1 = typval_compare_null(tv1, tv2);
1246 if (n1 == MAYBE)
1247 {
1248 clear_tv(tv1);
1249 return FAIL;
1250 }
1251 if (type == EXPR_NEQUAL)
1252 n1 = !n1;
1253 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001254 else if (tv1->v_type == VAR_BLOB || tv2->v_type == VAR_BLOB)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001255 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001256 if (typval_compare_blob(tv1, tv2, type, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001257 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001258 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001259 return FAIL;
1260 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001261 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001262 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001263 else if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001264 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001265 if (typval_compare_list(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 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001272 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001273 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001274 if (typval_compare_dict(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001275 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001276 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001277 return FAIL;
1278 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001279 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001280 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001281 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC
1282 || tv1->v_type == VAR_PARTIAL || tv2->v_type == VAR_PARTIAL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001283 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001284 if (typval_compare_func(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001285 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001286 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001287 return FAIL;
1288 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001289 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001290 }
1291
1292#ifdef FEAT_FLOAT
1293 // If one of the two variables is a float, compare as a float.
1294 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001295 else if ((tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001296 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1297 {
1298 float_T f1, f2;
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001299 int error = FALSE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001300
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001301 f1 = tv_get_float_chk(tv1, &error);
1302 if (!error)
1303 f2 = tv_get_float_chk(tv2, &error);
1304 if (error)
1305 {
1306 clear_tv(tv1);
1307 return FAIL;
1308 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001309 n1 = FALSE;
1310 switch (type)
1311 {
1312 case EXPR_IS:
1313 case EXPR_EQUAL: n1 = (f1 == f2); break;
1314 case EXPR_ISNOT:
1315 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1316 case EXPR_GREATER: n1 = (f1 > f2); break;
1317 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1318 case EXPR_SMALLER: n1 = (f1 < f2); break;
1319 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1320 case EXPR_UNKNOWN:
1321 case EXPR_MATCH:
1322 default: break; // avoid gcc warning
1323 }
1324 }
1325#endif
1326
1327 // If one of the two variables is a number, compare as a number.
1328 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001329 else if ((tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001330 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1331 {
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001332 int error = FALSE;
1333
1334 n1 = tv_get_number_chk(tv1, &error);
1335 if (!error)
1336 n2 = tv_get_number_chk(tv2, &error);
1337 if (error)
1338 {
1339 clear_tv(tv1);
1340 return FAIL;
1341 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001342 switch (type)
1343 {
1344 case EXPR_IS:
1345 case EXPR_EQUAL: n1 = (n1 == n2); break;
1346 case EXPR_ISNOT:
1347 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1348 case EXPR_GREATER: n1 = (n1 > n2); break;
1349 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1350 case EXPR_SMALLER: n1 = (n1 < n2); break;
1351 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1352 case EXPR_UNKNOWN:
1353 case EXPR_MATCH:
1354 default: break; // avoid gcc warning
1355 }
1356 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001357 else if (in_vim9script() && (tv1->v_type == VAR_BOOL
1358 || tv2->v_type == VAR_BOOL
1359 || (tv1->v_type == VAR_SPECIAL
1360 && tv2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001361 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001362 if (tv1->v_type != tv2->v_type)
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001363 {
1364 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001365 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1366 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001367 return FAIL;
1368 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001369 n1 = tv1->vval.v_number;
1370 n2 = tv2->vval.v_number;
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001371 switch (type)
1372 {
1373 case EXPR_IS:
1374 case EXPR_EQUAL: n1 = (n1 == n2); break;
1375 case EXPR_ISNOT:
1376 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1377 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001378 semsg(_(e_invalid_operation_for_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001379 vartype_name(tv1->v_type));
1380 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001381 return FAIL;
1382 }
1383 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001384#ifdef FEAT_JOB_CHANNEL
1385 else if (tv1->v_type == tv2->v_type
1386 && (tv1->v_type == VAR_CHANNEL || tv1->v_type == VAR_JOB)
1387 && (type == EXPR_NEQUAL || type == EXPR_EQUAL))
1388 {
1389 if (tv1->v_type == VAR_CHANNEL)
1390 n1 = tv1->vval.v_channel == tv2->vval.v_channel;
1391 else
1392 n1 = tv1->vval.v_job == tv2->vval.v_job;
1393 if (type == EXPR_NEQUAL)
1394 n1 = !n1;
1395 }
1396#endif
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001397 else
1398 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001399 if (typval_compare_string(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar0c357522021-07-18 14:43:43 +02001400 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001401 clear_tv(tv1);
Bram Moolenaar0c357522021-07-18 14:43:43 +02001402 return FAIL;
1403 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001404 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001405 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001406 clear_tv(tv1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001407 if (in_vim9script())
1408 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001409 tv1->v_type = VAR_BOOL;
1410 tv1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001411 }
1412 else
1413 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001414 tv1->v_type = VAR_NUMBER;
1415 tv1->vval.v_number = n1;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001416 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001417
1418 return OK;
1419}
1420
Bram Moolenaar34453202021-01-31 13:08:38 +01001421/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001422 * Compare "tv1" to "tv2" as lists acording to "type" and "ic".
1423 * Put the result, false or true, in "res".
1424 * Return FAIL and give an error message when the comparison can't be done.
1425 */
1426 int
1427typval_compare_list(
1428 typval_T *tv1,
1429 typval_T *tv2,
1430 exprtype_T type,
1431 int ic,
1432 int *res)
1433{
1434 int val = 0;
1435
1436 if (type == EXPR_IS || type == EXPR_ISNOT)
1437 {
1438 val = (tv1->v_type == tv2->v_type
1439 && tv1->vval.v_list == tv2->vval.v_list);
1440 if (type == EXPR_ISNOT)
1441 val = !val;
1442 }
1443 else if (tv1->v_type != tv2->v_type
1444 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1445 {
1446 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001447 emsg(_(e_can_only_compare_list_with_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001448 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001449 emsg(_(e_invalid_operation_for_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001450 return FAIL;
1451 }
1452 else
1453 {
1454 val = list_equal(tv1->vval.v_list, tv2->vval.v_list,
1455 ic, FALSE);
1456 if (type == EXPR_NEQUAL)
1457 val = !val;
1458 }
1459 *res = val;
1460 return OK;
1461}
1462
1463/*
Bram Moolenaarf3507a52022-03-09 11:56:21 +00001464 * Compare v:null with another type. Return TRUE if the value is NULL.
Bram Moolenaar7a222242022-03-01 19:23:24 +00001465 */
1466 int
1467typval_compare_null(typval_T *tv1, typval_T *tv2)
1468{
1469 if ((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1470 || (tv2->v_type == VAR_SPECIAL && tv2->vval.v_number == VVAL_NULL))
1471 {
1472 typval_T *tv = tv1->v_type == VAR_SPECIAL ? tv2 : tv1;
1473
1474 switch (tv->v_type)
1475 {
1476 case VAR_BLOB: return tv->vval.v_blob == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001477#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001478 case VAR_CHANNEL: return tv->vval.v_channel == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001479#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001480 case VAR_DICT: return tv->vval.v_dict == NULL;
1481 case VAR_FUNC: return tv->vval.v_string == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001482#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001483 case VAR_JOB: return tv->vval.v_job == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001484#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001485 case VAR_LIST: return tv->vval.v_list == NULL;
1486 case VAR_PARTIAL: return tv->vval.v_partial == NULL;
1487 case VAR_STRING: return tv->vval.v_string == NULL;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001488
1489 case VAR_NUMBER: if (!in_vim9script())
1490 return tv->vval.v_number == 0;
1491 break;
1492#ifdef FEAT_FLOAT
1493 case VAR_FLOAT: if (!in_vim9script())
1494 return tv->vval.v_float == 0.0;
1495 break;
1496#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001497 default: break;
1498 }
1499 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001500 // although comparing null with number, float or bool is not very useful
Bram Moolenaar05667812022-03-15 20:21:33 +00001501 // we won't give an error
1502 return FALSE;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001503}
1504
1505/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001506 * Compare "tv1" to "tv2" as blobs acording to "type".
1507 * Put the result, false or true, in "res".
1508 * Return FAIL and give an error message when the comparison can't be done.
1509 */
1510 int
1511typval_compare_blob(
1512 typval_T *tv1,
1513 typval_T *tv2,
1514 exprtype_T type,
1515 int *res)
1516{
1517 int val = 0;
1518
1519 if (type == EXPR_IS || type == EXPR_ISNOT)
1520 {
1521 val = (tv1->v_type == tv2->v_type
1522 && tv1->vval.v_blob == tv2->vval.v_blob);
1523 if (type == EXPR_ISNOT)
1524 val = !val;
1525 }
1526 else if (tv1->v_type != tv2->v_type
1527 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1528 {
1529 if (tv1->v_type != tv2->v_type)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001530 emsg(_(e_can_only_compare_blob_with_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001531 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001532 emsg(_(e_invalid_operation_for_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001533 return FAIL;
1534 }
1535 else
1536 {
1537 val = blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1538 if (type == EXPR_NEQUAL)
1539 val = !val;
1540 }
1541 *res = val;
1542 return OK;
1543}
1544
1545/*
1546 * Compare "tv1" to "tv2" as dictionaries acording to "type" and "ic".
1547 * Put the result, false or true, in "res".
1548 * Return FAIL and give an error message when the comparison can't be done.
1549 */
1550 int
1551typval_compare_dict(
1552 typval_T *tv1,
1553 typval_T *tv2,
1554 exprtype_T type,
1555 int ic,
1556 int *res)
1557{
1558 int val;
1559
1560 if (type == EXPR_IS || type == EXPR_ISNOT)
1561 {
1562 val = (tv1->v_type == tv2->v_type
1563 && tv1->vval.v_dict == tv2->vval.v_dict);
1564 if (type == EXPR_ISNOT)
1565 val = !val;
1566 }
1567 else if (tv1->v_type != tv2->v_type
1568 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1569 {
1570 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001571 emsg(_(e_can_only_compare_dictionary_with_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001572 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001573 emsg(_(e_invalid_operation_for_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001574 return FAIL;
1575 }
1576 else
1577 {
1578 val = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, FALSE);
1579 if (type == EXPR_NEQUAL)
1580 val = !val;
1581 }
1582 *res = val;
1583 return OK;
1584}
1585
1586/*
1587 * Compare "tv1" to "tv2" as funcrefs acording to "type" and "ic".
1588 * Put the result, false or true, in "res".
1589 * Return FAIL and give an error message when the comparison can't be done.
1590 */
1591 int
1592typval_compare_func(
1593 typval_T *tv1,
1594 typval_T *tv2,
1595 exprtype_T type,
1596 int ic,
1597 int *res)
1598{
1599 int val = 0;
1600
1601 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1602 && type != EXPR_IS && type != EXPR_ISNOT)
1603 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001604 emsg(_(e_invalid_operation_for_funcrefs));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001605 return FAIL;
1606 }
1607 if ((tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial == NULL)
1608 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial == NULL))
1609 // When both partials are NULL, then they are equal.
1610 // Otherwise they are not equal.
1611 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1612 else if (type == EXPR_IS || type == EXPR_ISNOT)
1613 {
1614 if (tv1->v_type == VAR_FUNC && tv2->v_type == VAR_FUNC)
1615 // strings are considered the same if their value is
1616 // the same
1617 val = tv_equal(tv1, tv2, ic, FALSE);
1618 else if (tv1->v_type == VAR_PARTIAL && tv2->v_type == VAR_PARTIAL)
1619 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1620 else
1621 val = FALSE;
1622 }
1623 else
1624 val = tv_equal(tv1, tv2, ic, FALSE);
1625 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1626 val = !val;
1627 *res = val;
1628 return OK;
1629}
1630
1631/*
1632 * Compare "tv1" to "tv2" as strings according to "type" and "ic".
1633 * Put the result, false or true, in "res".
1634 * Return FAIL and give an error message when the comparison can't be done.
1635 */
1636 int
1637typval_compare_string(
1638 typval_T *tv1,
1639 typval_T *tv2,
1640 exprtype_T type,
1641 int ic,
1642 int *res)
1643{
1644 int i = 0;
1645 int val = FALSE;
1646 char_u *s1, *s2;
1647 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1648
1649 if (in_vim9script()
1650 && ((tv1->v_type != VAR_STRING && tv1->v_type != VAR_SPECIAL)
1651 || (tv2->v_type != VAR_STRING && tv2->v_type != VAR_SPECIAL)))
1652 {
1653 semsg(_(e_cannot_compare_str_with_str),
1654 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1655 return FAIL;
1656 }
1657 s1 = tv_get_string_buf(tv1, buf1);
1658 s2 = tv_get_string_buf(tv2, buf2);
1659 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1660 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1661 switch (type)
1662 {
Bram Moolenaarf8691002022-03-10 12:20:53 +00001663 case EXPR_IS: if (in_vim9script())
1664 {
1665 // Really check it is the same string, not just
1666 // the same value.
1667 val = tv1->vval.v_string == tv2->vval.v_string;
1668 break;
1669 }
1670 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001671 case EXPR_EQUAL: val = (i == 0); break;
Bram Moolenaarf8691002022-03-10 12:20:53 +00001672 case EXPR_ISNOT: if (in_vim9script())
1673 {
1674 // Really check it is not the same string, not
1675 // just a different value.
1676 val = tv1->vval.v_string != tv2->vval.v_string;
1677 break;
1678 }
1679 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001680 case EXPR_NEQUAL: val = (i != 0); break;
1681 case EXPR_GREATER: val = (i > 0); break;
1682 case EXPR_GEQUAL: val = (i >= 0); break;
1683 case EXPR_SMALLER: val = (i < 0); break;
1684 case EXPR_SEQUAL: val = (i <= 0); break;
1685
1686 case EXPR_MATCH:
1687 case EXPR_NOMATCH:
1688 val = pattern_match(s2, s1, ic);
1689 if (type == EXPR_NOMATCH)
1690 val = !val;
1691 break;
1692
1693 default: break; // avoid gcc warning
1694 }
1695 *res = val;
1696 return OK;
1697}
1698/*
Bram Moolenaar34453202021-01-31 13:08:38 +01001699 * Convert any type to a string, never give an error.
1700 * When "quotes" is TRUE add quotes to a string.
1701 * Returns an allocated string.
1702 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001703 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001704typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001705{
1706 char_u *tofree;
1707 char_u numbuf[NUMBUFLEN];
1708 char_u *ret = NULL;
1709
1710 if (arg == NULL)
1711 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001712 if (!quotes && arg->v_type == VAR_STRING)
1713 {
1714 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1715 : arg->vval.v_string);
1716 }
1717 else
1718 {
1719 ret = tv2string(arg, &tofree, numbuf, 0);
1720 // Make a copy if we have a value but it's not in allocated memory.
1721 if (ret != NULL && tofree == NULL)
1722 ret = vim_strsave(ret);
1723 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001724 return ret;
1725}
1726
1727/*
1728 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1729 * or it refers to a List or Dictionary that is locked.
1730 */
1731 int
1732tv_islocked(typval_T *tv)
1733{
1734 return (tv->v_lock & VAR_LOCKED)
1735 || (tv->v_type == VAR_LIST
1736 && tv->vval.v_list != NULL
1737 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1738 || (tv->v_type == VAR_DICT
1739 && tv->vval.v_dict != NULL
1740 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1741}
1742
1743 static int
1744func_equal(
1745 typval_T *tv1,
1746 typval_T *tv2,
1747 int ic) // ignore case
1748{
1749 char_u *s1, *s2;
1750 dict_T *d1, *d2;
1751 int a1, a2;
1752 int i;
1753
1754 // empty and NULL function name considered the same
1755 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1756 : partial_name(tv1->vval.v_partial);
1757 if (s1 != NULL && *s1 == NUL)
1758 s1 = NULL;
1759 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1760 : partial_name(tv2->vval.v_partial);
1761 if (s2 != NULL && *s2 == NUL)
1762 s2 = NULL;
1763 if (s1 == NULL || s2 == NULL)
1764 {
1765 if (s1 != s2)
1766 return FALSE;
1767 }
1768 else if (STRCMP(s1, s2) != 0)
1769 return FALSE;
1770
1771 // empty dict and NULL dict is different
1772 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1773 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1774 if (d1 == NULL || d2 == NULL)
1775 {
1776 if (d1 != d2)
1777 return FALSE;
1778 }
1779 else if (!dict_equal(d1, d2, ic, TRUE))
1780 return FALSE;
1781
1782 // empty list and no list considered the same
1783 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1784 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1785 if (a1 != a2)
1786 return FALSE;
1787 for (i = 0; i < a1; ++i)
1788 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1789 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1790 return FALSE;
1791
1792 return TRUE;
1793}
1794
1795/*
1796 * Return TRUE if "tv1" and "tv2" have the same value.
1797 * Compares the items just like "==" would compare them, but strings and
1798 * numbers are different. Floats and numbers are also different.
1799 */
1800 int
1801tv_equal(
1802 typval_T *tv1,
1803 typval_T *tv2,
1804 int ic, // ignore case
1805 int recursive) // TRUE when used recursively
1806{
1807 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1808 char_u *s1, *s2;
1809 static int recursive_cnt = 0; // catch recursive loops
1810 int r;
1811 static int tv_equal_recurse_limit;
1812
1813 // Catch lists and dicts that have an endless loop by limiting
1814 // recursiveness to a limit. We guess they are equal then.
1815 // A fixed limit has the problem of still taking an awful long time.
1816 // Reduce the limit every time running into it. That should work fine for
1817 // deeply linked structures that are not recursively linked and catch
1818 // recursiveness quickly.
1819 if (!recursive)
1820 tv_equal_recurse_limit = 1000;
1821 if (recursive_cnt >= tv_equal_recurse_limit)
1822 {
1823 --tv_equal_recurse_limit;
1824 return TRUE;
1825 }
1826
1827 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1828 // arguments.
1829 if ((tv1->v_type == VAR_FUNC
1830 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1831 && (tv2->v_type == VAR_FUNC
1832 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1833 {
1834 ++recursive_cnt;
1835 r = func_equal(tv1, tv2, ic);
1836 --recursive_cnt;
1837 return r;
1838 }
1839
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001840 if (tv1->v_type != tv2->v_type
1841 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1842 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001843 return FALSE;
1844
1845 switch (tv1->v_type)
1846 {
1847 case VAR_LIST:
1848 ++recursive_cnt;
1849 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1850 --recursive_cnt;
1851 return r;
1852
1853 case VAR_DICT:
1854 ++recursive_cnt;
1855 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1856 --recursive_cnt;
1857 return r;
1858
1859 case VAR_BLOB:
1860 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1861
1862 case VAR_NUMBER:
1863 case VAR_BOOL:
1864 case VAR_SPECIAL:
1865 return tv1->vval.v_number == tv2->vval.v_number;
1866
1867 case VAR_STRING:
1868 s1 = tv_get_string_buf(tv1, buf1);
1869 s2 = tv_get_string_buf(tv2, buf2);
1870 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1871
1872 case VAR_FLOAT:
1873#ifdef FEAT_FLOAT
1874 return tv1->vval.v_float == tv2->vval.v_float;
1875#endif
1876 case VAR_JOB:
1877#ifdef FEAT_JOB_CHANNEL
1878 return tv1->vval.v_job == tv2->vval.v_job;
1879#endif
1880 case VAR_CHANNEL:
1881#ifdef FEAT_JOB_CHANNEL
1882 return tv1->vval.v_channel == tv2->vval.v_channel;
1883#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001884 case VAR_INSTR:
1885 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001886
1887 case VAR_PARTIAL:
1888 return tv1->vval.v_partial == tv2->vval.v_partial;
1889
1890 case VAR_FUNC:
1891 return tv1->vval.v_string == tv2->vval.v_string;
1892
1893 case VAR_UNKNOWN:
1894 case VAR_ANY:
1895 case VAR_VOID:
1896 break;
1897 }
1898
1899 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1900 // does not equal anything, not even itself.
1901 return FALSE;
1902}
1903
1904/*
1905 * Get an option value.
1906 * "arg" points to the '&' or '+' before the option name.
1907 * "arg" is advanced to character after the option name.
1908 * Return OK or FAIL.
1909 */
1910 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001911eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001912 char_u **arg,
1913 typval_T *rettv, // when NULL, only check if option exists
1914 int evaluate)
1915{
1916 char_u *option_end;
1917 long numval;
1918 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001919 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001920 int c;
1921 int working = (**arg == '+'); // has("+option")
1922 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001923 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001924
1925 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001926 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001927 if (option_end == NULL)
1928 {
1929 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001930 semsg(_(e_option_name_missing_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001931 return FAIL;
1932 }
1933
1934 if (!evaluate)
1935 {
1936 *arg = option_end;
1937 return OK;
1938 }
1939
1940 c = *option_end;
1941 *option_end = NUL;
1942 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001943 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001944
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001945 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001946 {
1947 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001948 semsg(_(e_unknown_option_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001949 ret = FAIL;
1950 }
1951 else if (rettv != NULL)
1952 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001953 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001954 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001955 {
1956 rettv->v_type = VAR_STRING;
1957 rettv->vval.v_string = NULL;
1958 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001959 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001960 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001961 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1962 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001963 rettv->vval.v_number = 0;
1964 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001965 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001966 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001967 if (in_vim9script() && opt_type == gov_bool)
1968 {
1969 rettv->v_type = VAR_BOOL;
1970 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1971 }
1972 else
1973 {
1974 rettv->v_type = VAR_NUMBER;
1975 rettv->vval.v_number = numval;
1976 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001977 }
1978 else // string option
1979 {
1980 rettv->v_type = VAR_STRING;
1981 rettv->vval.v_string = stringval;
1982 }
1983 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001984 else if (working && (opt_type == gov_hidden_bool
1985 || opt_type == gov_hidden_number
1986 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001987 ret = FAIL;
1988
1989 *option_end = c; // put back for error messages
1990 *arg = option_end;
1991
1992 return ret;
1993}
1994
1995/*
1996 * Allocate a variable for a number constant. Also deals with "0z" for blob.
1997 * Return OK or FAIL.
1998 */
1999 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002000eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002001 char_u **arg,
2002 typval_T *rettv,
2003 int evaluate,
2004 int want_string UNUSED)
2005{
2006 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002007 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002008#ifdef FEAT_FLOAT
2009 char_u *p;
2010 int get_float = FALSE;
2011
2012 // We accept a float when the format matches
2013 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
2014 // strict to avoid backwards compatibility problems.
2015 // With script version 2 and later the leading digit can be
2016 // omitted.
2017 // Don't look for a float after the "." operator, so that
2018 // ":let vers = 1.2.3" doesn't fail.
2019 if (**arg == '.')
2020 p = *arg;
2021 else
Bram Moolenaar29500652021-08-08 15:43:34 +02002022 {
2023 p = *arg + 1;
2024 if (skip_quotes)
2025 for (;;)
2026 {
2027 if (*p == '\'')
2028 ++p;
2029 if (!vim_isdigit(*p))
2030 break;
2031 p = skipdigits(p);
2032 }
2033 else
2034 p = skipdigits(p);
2035 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002036 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
2037 {
2038 get_float = TRUE;
2039 p = skipdigits(p + 2);
2040 if (*p == 'e' || *p == 'E')
2041 {
2042 ++p;
2043 if (*p == '-' || *p == '+')
2044 ++p;
2045 if (!vim_isdigit(*p))
2046 get_float = FALSE;
2047 else
2048 p = skipdigits(p + 1);
2049 }
2050 if (ASCII_ISALPHA(*p) || *p == '.')
2051 get_float = FALSE;
2052 }
2053 if (get_float)
2054 {
2055 float_T f;
2056
Bram Moolenaar29500652021-08-08 15:43:34 +02002057 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002058 if (evaluate)
2059 {
2060 rettv->v_type = VAR_FLOAT;
2061 rettv->vval.v_float = f;
2062 }
2063 }
2064 else
2065#endif
2066 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
2067 {
2068 char_u *bp;
2069 blob_T *blob = NULL; // init for gcc
2070
2071 // Blob constant: 0z0123456789abcdef
2072 if (evaluate)
2073 blob = blob_alloc();
2074 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2075 {
2076 if (!vim_isxdigit(bp[1]))
2077 {
2078 if (blob != NULL)
2079 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002080 emsg(_(e_blob_literal_should_have_an_even_number_of_hex_characters));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002081 ga_clear(&blob->bv_ga);
2082 VIM_CLEAR(blob);
2083 }
2084 return FAIL;
2085 }
2086 if (blob != NULL)
2087 ga_append(&blob->bv_ga,
2088 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
2089 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2090 ++bp;
2091 }
2092 if (blob != NULL)
2093 rettv_blob_set(rettv, blob);
2094 *arg = bp;
2095 }
2096 else
2097 {
2098 varnumber_T n;
2099
2100 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02002101 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002102 ? STR2NR_NO_OCT + STR2NR_QUOTE
2103 : STR2NR_ALL, &n, NULL, 0, TRUE);
2104 if (len == 0)
2105 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00002106 if (evaluate)
2107 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002108 return FAIL;
2109 }
2110 *arg += len;
2111 if (evaluate)
2112 {
2113 rettv->v_type = VAR_NUMBER;
2114 rettv->vval.v_number = n;
2115 }
2116 }
2117 return OK;
2118}
2119
2120/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002121 * Evaluate a string constant and put the result in "rettv".
2122 * "*arg" points to the double quote or to after it when "interpolate" is TRUE.
2123 * When "interpolate" is TRUE reduce "{{" to "{", reduce "}}" to "}" and stop
2124 * at a single "{".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002125 * Return OK or FAIL.
2126 */
2127 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002128eval_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002129{
2130 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002131 char_u *end;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002132 int extra = interpolate ? 1 : 0;
2133 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002134 int len;
2135
2136 // Find the end of the string, skipping backslashed characters.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002137 for (p = *arg + off; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002138 {
2139 if (*p == '\\' && p[1] != NUL)
2140 {
2141 ++p;
2142 // A "\<x>" form occupies at least 4 characters, and produces up
zeertzjqdb088872022-05-02 22:53:45 +01002143 // to 9 characters (6 for the char and 3 for a modifier):
2144 // reserve space for 5 extra.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002145 if (*p == '<')
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002146 {
2147 int modifiers = 0;
2148 int flags = FSK_KEYCODE | FSK_IN_STRING;
2149
zeertzjqdb088872022-05-02 22:53:45 +01002150 extra += 5;
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002151
2152 // Skip to the '>' to avoid using '{' inside for string
2153 // interpolation.
2154 if (p[1] != '*')
2155 flags |= FSK_SIMPLIFY;
2156 if (find_special_key(&p, &modifiers, flags, NULL) != 0)
2157 --p; // leave "p" on the ">"
2158 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002159 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002160 else if (interpolate && (*p == '{' || *p == '}'))
2161 {
2162 if (*p == '{' && p[1] != '{') // start of expression
2163 break;
2164 ++p;
2165 if (p[-1] == '}' && *p != '}') // single '}' is an error
2166 {
2167 semsg(_(e_stray_closing_curly_str), *arg);
2168 return FAIL;
2169 }
2170 --extra; // "{{" becomes "{", "}}" becomes "}"
2171 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002172 }
2173
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002174 if (*p != '"' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002175 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002176 semsg(_(e_missing_double_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002177 return FAIL;
2178 }
2179
2180 // If only parsing, set *arg and return here
2181 if (!evaluate)
2182 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002183 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002184 return OK;
2185 }
2186
2187 // Copy the string into allocated memory, handling backslashed
2188 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002189 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002190 len = (int)(p - *arg + extra);
2191 rettv->vval.v_string = alloc(len);
2192 if (rettv->vval.v_string == NULL)
2193 return FAIL;
2194 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002195
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002196 for (p = *arg + off; *p != NUL && *p != '"'; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002197 {
2198 if (*p == '\\')
2199 {
2200 switch (*++p)
2201 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002202 case 'b': *end++ = BS; ++p; break;
2203 case 'e': *end++ = ESC; ++p; break;
2204 case 'f': *end++ = FF; ++p; break;
2205 case 'n': *end++ = NL; ++p; break;
2206 case 'r': *end++ = CAR; ++p; break;
2207 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002208
2209 case 'X': // hex: "\x1", "\x12"
2210 case 'x':
2211 case 'u': // Unicode: "\u0023"
2212 case 'U':
2213 if (vim_isxdigit(p[1]))
2214 {
2215 int n, nr;
2216 int c = toupper(*p);
2217
2218 if (c == 'X')
2219 n = 2;
2220 else if (*p == 'u')
2221 n = 4;
2222 else
2223 n = 8;
2224 nr = 0;
2225 while (--n >= 0 && vim_isxdigit(p[1]))
2226 {
2227 ++p;
2228 nr = (nr << 4) + hex2nr(*p);
2229 }
2230 ++p;
2231 // For "\u" store the number according to
2232 // 'encoding'.
2233 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002234 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002235 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002236 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002237 }
2238 break;
2239
2240 // octal: "\1", "\12", "\123"
2241 case '0':
2242 case '1':
2243 case '2':
2244 case '3':
2245 case '4':
2246 case '5':
2247 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002248 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002249 if (*p >= '0' && *p <= '7')
2250 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002251 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002252 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002253 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002254 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002255 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002256 break;
2257
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002258 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002259 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002260 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002261 int flags = FSK_KEYCODE | FSK_IN_STRING;
2262
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002263 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002264 flags |= FSK_SIMPLIFY;
zeertzjqdb088872022-05-02 22:53:45 +01002265 extra = trans_special(&p, end, flags, FALSE, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002266 if (extra != 0)
2267 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002268 end += extra;
2269 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002270 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002271 break;
2272 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002273 }
2274 // FALLTHROUGH
2275
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002276 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002277 break;
2278 }
2279 }
2280 else
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002281 {
2282 if (interpolate && (*p == '{' || *p == '}'))
2283 {
2284 if (*p == '{' && p[1] != '{') // start of expression
2285 break;
2286 ++p; // reduce "{{" to "{" and "}}" to "}"
2287 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002288 MB_COPY_CHAR(p, end);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002289 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002290 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002291 *end = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002292 if (*p == '"' && !interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002293 ++p;
2294 *arg = p;
2295
2296 return OK;
2297}
2298
2299/*
2300 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002301 * When "interpolate" is TRUE reduce "{{" to "{" and stop at a single "{".
2302 * Return OK when a "rettv" was set to the string.
2303 * Return FAIL on error, "rettv" is not set.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002304 */
2305 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002306eval_lit_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002307{
2308 char_u *p;
2309 char_u *str;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002310 int reduce = interpolate ? -1 : 0;
2311 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002312
2313 // Find the end of the string, skipping ''.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002314 for (p = *arg + off; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002315 {
2316 if (*p == '\'')
2317 {
2318 if (p[1] != '\'')
2319 break;
2320 ++reduce;
2321 ++p;
2322 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002323 else if (interpolate)
2324 {
2325 if (*p == '{')
2326 {
2327 if (p[1] != '{')
2328 break;
2329 ++p;
2330 ++reduce;
2331 }
2332 else if (*p == '}')
2333 {
2334 ++p;
2335 if (*p != '}')
2336 {
2337 semsg(_(e_stray_closing_curly_str), *arg);
2338 return FAIL;
2339 }
2340 ++reduce;
2341 }
2342 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002343 }
2344
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002345 if (*p != '\'' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002346 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002347 semsg(_(e_missing_single_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002348 return FAIL;
2349 }
2350
2351 // If only parsing return after setting "*arg"
2352 if (!evaluate)
2353 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002354 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002355 return OK;
2356 }
2357
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002358 // Copy the string into allocated memory, handling '' to ' reduction and
2359 // any expressions.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002360 str = alloc((p - *arg) - reduce);
2361 if (str == NULL)
2362 return FAIL;
2363 rettv->v_type = VAR_STRING;
2364 rettv->vval.v_string = str;
2365
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002366 for (p = *arg + off; *p != NUL; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002367 {
2368 if (*p == '\'')
2369 {
2370 if (p[1] != '\'')
2371 break;
2372 ++p;
2373 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002374 else if (interpolate && (*p == '{' || *p == '}'))
2375 {
2376 if (*p == '{' && p[1] != '{')
2377 break;
2378 ++p;
2379 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002380 MB_COPY_CHAR(p, str);
2381 }
2382 *str = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002383 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002384
2385 return OK;
2386}
2387
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002388/*
2389 * Evaluate a single or double quoted string possibly containing expressions.
2390 * "arg" points to the '$'. The result is put in "rettv".
2391 * Returns OK or FAIL.
2392 */
LemonBoy2eaef102022-05-06 13:14:50 +01002393 int
2394eval_interp_string(char_u **arg, typval_T *rettv, int evaluate)
2395{
2396 typval_T tv;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002397 int ret = OK;
2398 int quote;
2399 garray_T ga;
2400 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01002401
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002402 ga_init2(&ga, 1, 80);
2403
2404 // *arg is on the '$' character, move it to the first string character.
2405 ++*arg;
2406 quote = **arg;
2407 ++*arg;
2408
2409 for (;;)
2410 {
2411 // Get the string up to the matching quote or to a single '{'.
2412 // "arg" is advanced to either the quote or the '{'.
2413 if (quote == '"')
2414 ret = eval_string(arg, &tv, evaluate, TRUE);
2415 else
2416 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
2417 if (ret == FAIL)
2418 break;
2419 if (evaluate)
2420 {
2421 ga_concat(&ga, tv.vval.v_string);
2422 clear_tv(&tv);
2423 }
2424
2425 if (**arg != '{')
2426 {
2427 // found terminating quote
2428 ++*arg;
2429 break;
2430 }
Bram Moolenaar70c41242022-05-10 18:11:43 +01002431 p = eval_one_expr_in_str(*arg, &ga, evaluate);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002432 if (p == NULL)
2433 {
2434 ret = FAIL;
2435 break;
2436 }
2437 *arg = p;
2438 }
LemonBoy2eaef102022-05-06 13:14:50 +01002439
2440 rettv->v_type = VAR_STRING;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002441 if (ret == FAIL || !evaluate || ga_append(&ga, NUL) == FAIL)
2442 {
2443 ga_clear(&ga);
2444 rettv->vval.v_string = NULL;
LemonBoy2eaef102022-05-06 13:14:50 +01002445 return ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002446 }
LemonBoy2eaef102022-05-06 13:14:50 +01002447
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002448 rettv->vval.v_string = ga.ga_data;
2449 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01002450}
2451
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002452/*
2453 * Return a string with the string representation of a variable.
2454 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2455 * "numbuf" is used for a number.
2456 * Puts quotes around strings, so that they can be parsed back by eval().
2457 * May return NULL.
2458 */
2459 char_u *
2460tv2string(
2461 typval_T *tv,
2462 char_u **tofree,
2463 char_u *numbuf,
2464 int copyID)
2465{
2466 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2467}
2468
2469/*
2470 * Get the value of an environment variable.
2471 * "arg" is pointing to the '$'. It is advanced to after the name.
2472 * If the environment variable was not set, silently assume it is empty.
2473 * Return FAIL if the name is invalid.
2474 */
2475 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002476eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002477{
2478 char_u *string = NULL;
2479 int len;
2480 int cc;
2481 char_u *name;
2482 int mustfree = FALSE;
2483
2484 ++*arg;
2485 name = *arg;
2486 len = get_env_len(arg);
2487 if (evaluate)
2488 {
2489 if (len == 0)
2490 return FAIL; // invalid empty name
2491
2492 cc = name[len];
2493 name[len] = NUL;
2494 // first try vim_getenv(), fast for normal environment vars
2495 string = vim_getenv(name, &mustfree);
2496 if (string != NULL && *string != NUL)
2497 {
2498 if (!mustfree)
2499 string = vim_strsave(string);
2500 }
2501 else
2502 {
2503 if (mustfree)
2504 vim_free(string);
2505
2506 // next try expanding things like $VIM and ${HOME}
2507 string = expand_env_save(name - 1);
2508 if (string != NULL && *string == '$')
2509 VIM_CLEAR(string);
2510 }
2511 name[len] = cc;
2512
2513 rettv->v_type = VAR_STRING;
2514 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002515 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002516 }
2517
2518 return OK;
2519}
2520
2521/*
2522 * Get the lnum from the first argument.
2523 * Also accepts ".", "$", etc., but that only works for the current buffer.
2524 * Returns -1 on error.
2525 */
2526 linenr_T
2527tv_get_lnum(typval_T *argvars)
2528{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002529 linenr_T lnum = -1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002530
Bram Moolenaar56acb092020-08-16 14:48:19 +02002531 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2532 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002533 if (lnum <= 0 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002534 {
2535 int fnum;
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002536 pos_T *fp;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002537
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002538 // no valid number, try using arg like line()
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002539 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002540 if (fp != NULL)
2541 lnum = fp->lnum;
2542 }
2543 return lnum;
2544}
2545
2546/*
2547 * Get the lnum from the first argument.
2548 * Also accepts "$", then "buf" is used.
2549 * Returns 0 on error.
2550 */
2551 linenr_T
2552tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2553{
2554 if (argvars[0].v_type == VAR_STRING
2555 && argvars[0].vval.v_string != NULL
2556 && argvars[0].vval.v_string[0] == '$'
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002557 && argvars[0].vval.v_string[1] == NUL
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002558 && buf != NULL)
2559 return buf->b_ml.ml_line_count;
2560 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2561}
2562
2563/*
2564 * Get buffer by number or pattern.
2565 */
2566 buf_T *
2567tv_get_buf(typval_T *tv, int curtab_only)
2568{
2569 char_u *name = tv->vval.v_string;
2570 buf_T *buf;
2571
2572 if (tv->v_type == VAR_NUMBER)
2573 return buflist_findnr((int)tv->vval.v_number);
2574 if (tv->v_type != VAR_STRING)
2575 return NULL;
2576 if (name == NULL || *name == NUL)
2577 return curbuf;
2578 if (name[0] == '$' && name[1] == NUL)
2579 return lastbuf;
2580
2581 buf = buflist_find_by_name(name, curtab_only);
2582
2583 // If not found, try expanding the name, like done for bufexists().
2584 if (buf == NULL)
2585 buf = find_buffer(tv);
2586
2587 return buf;
2588}
2589
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002590/*
2591 * Like tv_get_buf() but give an error message is the type is wrong.
2592 */
2593 buf_T *
2594tv_get_buf_from_arg(typval_T *tv)
2595{
2596 buf_T *buf;
2597
2598 ++emsg_off;
2599 buf = tv_get_buf(tv, FALSE);
2600 --emsg_off;
2601 if (buf == NULL
2602 && tv->v_type != VAR_NUMBER
2603 && tv->v_type != VAR_STRING)
2604 // issue errmsg for type error
2605 (void)tv_get_number(tv);
2606 return buf;
2607}
2608
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002609#endif // FEAT_EVAL