blob: b131ba2c1516dbeecd62470b4cdb6b5678ee2807 [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 Lakshmanan83494b42021-07-20 17:51:51 +0200536 * Check for an optional dict argument at 'idx'
537 */
538 int
539check_for_opt_dict_arg(typval_T *args, int idx)
540{
541 return (args[idx].v_type == VAR_UNKNOWN
542 || check_for_dict_arg(args, idx) != FAIL);
543}
544
Dominique Pelle748b3082022-01-08 12:41:16 +0000545#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200546/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200547 * Give an error and return FAIL unless "args[idx]" is a channel or a job.
548 */
549 int
550check_for_chan_or_job_arg(typval_T *args, int idx)
551{
552 if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
553 {
rbtnnddc80af2021-12-17 18:01:31 +0000554 semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200555 return FAIL;
556 }
557 return OK;
558}
559
560/*
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200561 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
562 * job.
563 */
564 int
565check_for_opt_chan_or_job_arg(typval_T *args, int idx)
566{
567 return (args[idx].v_type == VAR_UNKNOWN
568 || check_for_chan_or_job_arg(args, idx) != FAIL);
569}
570
571/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200572 * Give an error and return FAIL unless "args[idx]" is a job.
573 */
574 int
575check_for_job_arg(typval_T *args, int idx)
576{
577 if (args[idx].v_type != VAR_JOB)
578 {
rbtnnddc80af2021-12-17 18:01:31 +0000579 semsg(_(e_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200580 return FAIL;
581 }
582 return OK;
583}
584
585/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200586 * Check for an optional job argument at 'idx'.
587 */
588 int
589check_for_opt_job_arg(typval_T *args, int idx)
590{
591 return (args[idx].v_type == VAR_UNKNOWN
592 || check_for_job_arg(args, idx) != FAIL);
593}
Dominique Pelle748b3082022-01-08 12:41:16 +0000594#endif
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200595
596/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200597 * Give an error and return FAIL unless "args[idx]" is a string or
598 * a number.
599 */
600 int
601check_for_string_or_number_arg(typval_T *args, int idx)
602{
603 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
604 {
rbtnnddc80af2021-12-17 18:01:31 +0000605 semsg(_(e_string_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200606 return FAIL;
607 }
608 return OK;
609}
610
611/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200612 * Check for an optional string or number argument at 'idx'.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200613 */
614 int
615check_for_opt_string_or_number_arg(typval_T *args, int idx)
616{
617 return (args[idx].v_type == VAR_UNKNOWN
618 || check_for_string_or_number_arg(args, idx) != FAIL);
619}
620
621/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200622 * Give an error and return FAIL unless "args[idx]" is a buffer number.
623 * Buffer number can be a number or a string.
624 */
625 int
626check_for_buffer_arg(typval_T *args, int idx)
627{
628 return check_for_string_or_number_arg(args, idx);
629}
630
631/*
632 * Check for an optional buffer argument at 'idx'
633 */
634 int
635check_for_opt_buffer_arg(typval_T *args, int idx)
636{
637 return (args[idx].v_type == VAR_UNKNOWN
638 || check_for_buffer_arg(args, idx));
639}
640
641/*
642 * Give an error and return FAIL unless "args[idx]" is a line number.
643 * Line number can be a number or a string.
644 */
645 int
646check_for_lnum_arg(typval_T *args, int idx)
647{
648 return check_for_string_or_number_arg(args, idx);
649}
650
651/*
652 * Check for an optional line number argument at 'idx'
653 */
654 int
655check_for_opt_lnum_arg(typval_T *args, int idx)
656{
657 return (args[idx].v_type == VAR_UNKNOWN
658 || check_for_lnum_arg(args, idx));
659}
660
Dominique Pelle748b3082022-01-08 12:41:16 +0000661#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200662/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200663 * Give an error and return FAIL unless "args[idx]" is a string or a blob.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200664 */
665 int
666check_for_string_or_blob_arg(typval_T *args, int idx)
667{
668 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
669 {
rbtnnddc80af2021-12-17 18:01:31 +0000670 semsg(_(e_string_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200671 return FAIL;
672 }
673 return OK;
674}
Dominique Pelle748b3082022-01-08 12:41:16 +0000675#endif
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200676
677/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200678 * Give an error and return FAIL unless "args[idx]" is a string or a list.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200679 */
680 int
681check_for_string_or_list_arg(typval_T *args, int idx)
682{
683 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
684 {
rbtnnddc80af2021-12-17 18:01:31 +0000685 semsg(_(e_string_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200686 return FAIL;
687 }
688 return OK;
689}
690
691/*
rbtnn0ccb5842021-12-18 18:33:46 +0000692 * Give an error and return FAIL unless "args[idx]" is a string, a list or a
693 * blob.
694 */
695 int
696check_for_string_or_list_or_blob_arg(typval_T *args, int idx)
697{
698 if (args[idx].v_type != VAR_STRING
699 && args[idx].v_type != VAR_LIST
700 && args[idx].v_type != VAR_BLOB)
701 {
702 semsg(_(e_string_list_or_blob_required_for_argument_nr), idx + 1);
703 return FAIL;
704 }
705 return OK;
706}
707
708/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200709 * Check for an optional string or list argument at 'idx'
710 */
711 int
712check_for_opt_string_or_list_arg(typval_T *args, int idx)
713{
714 return (args[idx].v_type == VAR_UNKNOWN
715 || check_for_string_or_list_arg(args, idx));
716}
717
718/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200719 * Give an error and return FAIL unless "args[idx]" is a string or a dict.
720 */
721 int
722check_for_string_or_dict_arg(typval_T *args, int idx)
723{
724 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_DICT)
725 {
rbtnnddc80af2021-12-17 18:01:31 +0000726 semsg(_(e_string_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200727 return FAIL;
728 }
729 return OK;
730}
731
732/*
733 * Give an error and return FAIL unless "args[idx]" is a string or a number
734 * or a list.
735 */
736 int
737check_for_string_or_number_or_list_arg(typval_T *args, int idx)
738{
739 if (args[idx].v_type != VAR_STRING
740 && args[idx].v_type != VAR_NUMBER
741 && args[idx].v_type != VAR_LIST)
742 {
rbtnn0ccb5842021-12-18 18:33:46 +0000743 semsg(_(e_string_number_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200744 return FAIL;
745 }
746 return OK;
747}
748
749/*
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200750 * Give an error and return FAIL unless "args[idx]" is an optional string
751 * or number or a list
752 */
753 int
754check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx)
755{
756 return (args[idx].v_type == VAR_UNKNOWN
757 || check_for_string_or_number_or_list_arg(args, idx) != FAIL);
758}
759
760/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200761 * Give an error and return FAIL unless "args[idx]" is a string or a list
762 * or a dict.
763 */
764 int
765check_for_string_or_list_or_dict_arg(typval_T *args, int idx)
766{
767 if (args[idx].v_type != VAR_STRING
768 && args[idx].v_type != VAR_LIST
769 && args[idx].v_type != VAR_DICT)
770 {
rbtnnddc80af2021-12-17 18:01:31 +0000771 semsg(_(e_string_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200772 return FAIL;
773 }
774 return OK;
775}
776
777/*
Bram Moolenaar223d0a62021-12-25 19:29:21 +0000778 * Give an error and return FAIL unless "args[idx]" is a string
779 * or a function reference.
780 */
781 int
782check_for_string_or_func_arg(typval_T *args, int idx)
783{
784 if (args[idx].v_type != VAR_PARTIAL
785 && args[idx].v_type != VAR_FUNC
786 && args[idx].v_type != VAR_STRING)
787 {
788 semsg(_(e_string_or_function_required_for_argument_nr), idx + 1);
789 return FAIL;
790 }
791 return OK;
792}
793
794/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200795 * Give an error and return FAIL unless "args[idx]" is a list or a blob.
796 */
797 int
798check_for_list_or_blob_arg(typval_T *args, int idx)
799{
800 if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
801 {
rbtnn0ccb5842021-12-18 18:33:46 +0000802 semsg(_(e_list_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200803 return FAIL;
804 }
805 return OK;
806}
807
808/*
809 * Give an error and return FAIL unless "args[idx]" is a list or dict
810 */
811 int
812check_for_list_or_dict_arg(typval_T *args, int idx)
813{
814 if (args[idx].v_type != VAR_LIST
815 && args[idx].v_type != VAR_DICT)
816 {
rbtnnddc80af2021-12-17 18:01:31 +0000817 semsg(_(e_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200818 return FAIL;
819 }
820 return OK;
821}
822
823/*
824 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
825 * blob.
826 */
827 int
828check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
829{
830 if (args[idx].v_type != VAR_LIST
831 && args[idx].v_type != VAR_DICT
832 && args[idx].v_type != VAR_BLOB)
833 {
rbtnnddc80af2021-12-17 18:01:31 +0000834 semsg(_(e_list_dict_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200835 return FAIL;
836 }
837 return OK;
838}
839
840/*
Bram Moolenaar2d877592021-12-16 08:21:09 +0000841 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
842 * blob or a string.
843 */
844 int
845check_for_list_or_dict_or_blob_or_string_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 && args[idx].v_type != VAR_STRING)
851 {
rbtnnddc80af2021-12-17 18:01:31 +0000852 semsg(_(e_list_dict_blob_or_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2d877592021-12-16 08:21:09 +0000853 return FAIL;
854 }
855 return OK;
856}
857
858/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200859 * Give an error and return FAIL unless "args[idx]" is an optional buffer
860 * number or a dict.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200861 */
862 int
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200863check_for_opt_buffer_or_dict_arg(typval_T *args, int idx)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200864{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200865 if (args[idx].v_type != VAR_UNKNOWN
866 && args[idx].v_type != VAR_STRING
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200867 && args[idx].v_type != VAR_NUMBER
868 && args[idx].v_type != VAR_DICT)
869 {
rbtnnddc80af2021-12-17 18:01:31 +0000870 semsg(_(e_string_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200871 return FAIL;
872 }
873 return OK;
874}
875
876/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200877 * Get the string value of a variable.
878 * If it is a Number variable, the number is converted into a string.
879 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
880 * tv_get_string_buf() uses a given buffer.
881 * If the String variable has never been set, return an empty string.
882 * Never returns NULL;
883 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
884 * NULL on error.
885 */
886 char_u *
887tv_get_string(typval_T *varp)
888{
889 static char_u mybuf[NUMBUFLEN];
890
891 return tv_get_string_buf(varp, mybuf);
892}
893
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100894/*
895 * Like tv_get_string() but don't allow number to string conversion for Vim9.
896 */
897 char_u *
898tv_get_string_strict(typval_T *varp)
899{
900 static char_u mybuf[NUMBUFLEN];
901 char_u *res = tv_get_string_buf_chk_strict(
902 varp, mybuf, in_vim9script());
903
904 return res != NULL ? res : (char_u *)"";
905}
906
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200907 char_u *
908tv_get_string_buf(typval_T *varp, char_u *buf)
909{
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200910 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200911
912 return res != NULL ? res : (char_u *)"";
913}
914
915/*
916 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
917 */
918 char_u *
919tv_get_string_chk(typval_T *varp)
920{
921 static char_u mybuf[NUMBUFLEN];
922
923 return tv_get_string_buf_chk(varp, mybuf);
924}
925
926 char_u *
927tv_get_string_buf_chk(typval_T *varp, char_u *buf)
928{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100929 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
930}
931
932 char_u *
933tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
934{
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200935 switch (varp->v_type)
936 {
937 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100938 if (strict)
939 {
940 emsg(_(e_using_number_as_string));
941 break;
942 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200943 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
944 (varnumber_T)varp->vval.v_number);
945 return buf;
946 case VAR_FUNC:
947 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000948 emsg(_(e_using_funcref_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200949 break;
950 case VAR_LIST:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000951 emsg(_(e_using_list_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200952 break;
953 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000954 emsg(_(e_using_dictionary_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200955 break;
956 case VAR_FLOAT:
957#ifdef FEAT_FLOAT
Bram Moolenaar7a2217b2021-06-06 12:33:49 +0200958 if (strict)
959 {
Bram Moolenaar0699b042022-01-01 16:01:23 +0000960 emsg(_(e_using_float_as_string));
Bram Moolenaar7a2217b2021-06-06 12:33:49 +0200961 break;
962 }
963 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
964 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200965#endif
966 case VAR_STRING:
967 if (varp->vval.v_string != NULL)
968 return varp->vval.v_string;
969 return (char_u *)"";
970 case VAR_BOOL:
971 case VAR_SPECIAL:
972 STRCPY(buf, get_var_special_name(varp->vval.v_number));
973 return buf;
974 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000975 emsg(_(e_using_blob_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200976 break;
977 case VAR_JOB:
978#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200979 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200980 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200981 semsg(_(e_using_invalid_value_as_string_str), "job");
982 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200983 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200984 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200985#endif
986 break;
987 case VAR_CHANNEL:
988#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200989 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200990 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200991 semsg(_(e_using_invalid_value_as_string_str), "channel");
992 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200993 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200994 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200995#endif
996 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200997 case VAR_VOID:
998 emsg(_(e_cannot_use_void_value));
999 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001000 case VAR_UNKNOWN:
1001 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001002 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +02001003 semsg(_(e_using_invalid_value_as_string_str),
1004 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001005 break;
1006 }
1007 return NULL;
1008}
1009
1010/*
1011 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
1012 * string() on Dict, List, etc.
1013 */
1014 char_u *
1015tv_stringify(typval_T *varp, char_u *buf)
1016{
1017 if (varp->v_type == VAR_LIST
1018 || varp->v_type == VAR_DICT
1019 || varp->v_type == VAR_BLOB
1020 || varp->v_type == VAR_FUNC
1021 || varp->v_type == VAR_PARTIAL
1022 || varp->v_type == VAR_FLOAT)
1023 {
1024 typval_T tmp;
1025
1026 f_string(varp, &tmp);
1027 tv_get_string_buf(&tmp, buf);
1028 clear_tv(varp);
1029 *varp = tmp;
1030 return tmp.vval.v_string;
1031 }
1032 return tv_get_string_buf(varp, buf);
1033}
1034
1035/*
1036 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
1037 * Also give an error message, using "name" or _("name") when use_gettext is
1038 * TRUE.
1039 */
1040 int
1041tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
1042{
1043 int lock = 0;
1044
1045 switch (tv->v_type)
1046 {
1047 case VAR_BLOB:
1048 if (tv->vval.v_blob != NULL)
1049 lock = tv->vval.v_blob->bv_lock;
1050 break;
1051 case VAR_LIST:
1052 if (tv->vval.v_list != NULL)
1053 lock = tv->vval.v_list->lv_lock;
1054 break;
1055 case VAR_DICT:
1056 if (tv->vval.v_dict != NULL)
1057 lock = tv->vval.v_dict->dv_lock;
1058 break;
1059 default:
1060 break;
1061 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001062 return value_check_lock(tv->v_lock, name, use_gettext)
1063 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001064}
1065
1066/*
1067 * Copy the values from typval_T "from" to typval_T "to".
1068 * When needed allocates string or increases reference count.
1069 * Does not make a copy of a list, blob or dict but copies the reference!
1070 * It is OK for "from" and "to" to point to the same item. This is used to
1071 * make a copy later.
1072 */
1073 void
1074copy_tv(typval_T *from, typval_T *to)
1075{
1076 to->v_type = from->v_type;
1077 to->v_lock = 0;
1078 switch (from->v_type)
1079 {
1080 case VAR_NUMBER:
1081 case VAR_BOOL:
1082 case VAR_SPECIAL:
1083 to->vval.v_number = from->vval.v_number;
1084 break;
1085 case VAR_FLOAT:
1086#ifdef FEAT_FLOAT
1087 to->vval.v_float = from->vval.v_float;
1088 break;
1089#endif
1090 case VAR_JOB:
1091#ifdef FEAT_JOB_CHANNEL
1092 to->vval.v_job = from->vval.v_job;
1093 if (to->vval.v_job != NULL)
1094 ++to->vval.v_job->jv_refcount;
1095 break;
1096#endif
1097 case VAR_CHANNEL:
1098#ifdef FEAT_JOB_CHANNEL
1099 to->vval.v_channel = from->vval.v_channel;
1100 if (to->vval.v_channel != NULL)
1101 ++to->vval.v_channel->ch_refcount;
1102 break;
1103#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001104 case VAR_INSTR:
1105 to->vval.v_instr = from->vval.v_instr;
1106 break;
1107
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001108 case VAR_STRING:
1109 case VAR_FUNC:
1110 if (from->vval.v_string == NULL)
1111 to->vval.v_string = NULL;
1112 else
1113 {
1114 to->vval.v_string = vim_strsave(from->vval.v_string);
1115 if (from->v_type == VAR_FUNC)
1116 func_ref(to->vval.v_string);
1117 }
1118 break;
1119 case VAR_PARTIAL:
1120 if (from->vval.v_partial == NULL)
1121 to->vval.v_partial = NULL;
1122 else
1123 {
1124 to->vval.v_partial = from->vval.v_partial;
1125 ++to->vval.v_partial->pt_refcount;
1126 }
1127 break;
1128 case VAR_BLOB:
1129 if (from->vval.v_blob == NULL)
1130 to->vval.v_blob = NULL;
1131 else
1132 {
1133 to->vval.v_blob = from->vval.v_blob;
1134 ++to->vval.v_blob->bv_refcount;
1135 }
1136 break;
1137 case VAR_LIST:
1138 if (from->vval.v_list == NULL)
1139 to->vval.v_list = NULL;
1140 else
1141 {
1142 to->vval.v_list = from->vval.v_list;
1143 ++to->vval.v_list->lv_refcount;
1144 }
1145 break;
1146 case VAR_DICT:
1147 if (from->vval.v_dict == NULL)
1148 to->vval.v_dict = NULL;
1149 else
1150 {
1151 to->vval.v_dict = from->vval.v_dict;
1152 ++to->vval.v_dict->dv_refcount;
1153 }
1154 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +02001155 case VAR_VOID:
1156 emsg(_(e_cannot_use_void_value));
1157 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001158 case VAR_UNKNOWN:
1159 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001160 internal_error_no_abort("copy_tv(UNKNOWN)");
1161 break;
1162 }
1163}
1164
1165/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001166 * Compare "tv1" and "tv2".
1167 * Put the result in "tv1". Caller should clear "tv2".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001168 */
1169 int
1170typval_compare(
Bram Moolenaar265f8112021-12-19 12:33:05 +00001171 typval_T *tv1, // first operand
1172 typval_T *tv2, // second operand
1173 exprtype_T type, // operator
1174 int ic) // ignore case
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001175{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001176 varnumber_T n1, n2;
Bram Moolenaar265f8112021-12-19 12:33:05 +00001177 int res = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001178 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1179
Bram Moolenaar265f8112021-12-19 12:33:05 +00001180 if (type_is && tv1->v_type != tv2->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001181 {
1182 // For "is" a different type always means FALSE, for "notis"
1183 // it means TRUE.
1184 n1 = (type == EXPR_ISNOT);
1185 }
Bram Moolenaar7a222242022-03-01 19:23:24 +00001186 else if (((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1187 || (tv2->v_type == VAR_SPECIAL
1188 && tv2->vval.v_number == VVAL_NULL))
1189 && tv1->v_type != tv2->v_type
1190 && (type == EXPR_EQUAL || type == EXPR_NEQUAL))
1191 {
1192 n1 = typval_compare_null(tv1, tv2);
1193 if (n1 == MAYBE)
1194 {
1195 clear_tv(tv1);
1196 return FAIL;
1197 }
1198 if (type == EXPR_NEQUAL)
1199 n1 = !n1;
1200 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001201 else if (tv1->v_type == VAR_BLOB || tv2->v_type == VAR_BLOB)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001202 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001203 if (typval_compare_blob(tv1, tv2, type, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001204 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001205 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001206 return FAIL;
1207 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001208 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001209 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001210 else if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001211 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001212 if (typval_compare_list(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001213 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001214 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001215 return FAIL;
1216 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001217 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001218 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001219 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001220 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001221 if (typval_compare_dict(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001222 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001223 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001224 return FAIL;
1225 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001226 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001227 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001228 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC
1229 || tv1->v_type == VAR_PARTIAL || tv2->v_type == VAR_PARTIAL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001230 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001231 if (typval_compare_func(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001232 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001233 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001234 return FAIL;
1235 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001236 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001237 }
1238
1239#ifdef FEAT_FLOAT
1240 // If one of the two variables is a float, compare as a float.
1241 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001242 else if ((tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001243 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1244 {
1245 float_T f1, f2;
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001246 int error = FALSE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001247
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001248 f1 = tv_get_float_chk(tv1, &error);
1249 if (!error)
1250 f2 = tv_get_float_chk(tv2, &error);
1251 if (error)
1252 {
1253 clear_tv(tv1);
1254 return FAIL;
1255 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001256 n1 = FALSE;
1257 switch (type)
1258 {
1259 case EXPR_IS:
1260 case EXPR_EQUAL: n1 = (f1 == f2); break;
1261 case EXPR_ISNOT:
1262 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1263 case EXPR_GREATER: n1 = (f1 > f2); break;
1264 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1265 case EXPR_SMALLER: n1 = (f1 < f2); break;
1266 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1267 case EXPR_UNKNOWN:
1268 case EXPR_MATCH:
1269 default: break; // avoid gcc warning
1270 }
1271 }
1272#endif
1273
1274 // If one of the two variables is a number, compare as a number.
1275 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001276 else if ((tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001277 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1278 {
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001279 int error = FALSE;
1280
1281 n1 = tv_get_number_chk(tv1, &error);
1282 if (!error)
1283 n2 = tv_get_number_chk(tv2, &error);
1284 if (error)
1285 {
1286 clear_tv(tv1);
1287 return FAIL;
1288 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001289 switch (type)
1290 {
1291 case EXPR_IS:
1292 case EXPR_EQUAL: n1 = (n1 == n2); break;
1293 case EXPR_ISNOT:
1294 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1295 case EXPR_GREATER: n1 = (n1 > n2); break;
1296 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1297 case EXPR_SMALLER: n1 = (n1 < n2); break;
1298 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1299 case EXPR_UNKNOWN:
1300 case EXPR_MATCH:
1301 default: break; // avoid gcc warning
1302 }
1303 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001304 else if (in_vim9script() && (tv1->v_type == VAR_BOOL
1305 || tv2->v_type == VAR_BOOL
1306 || (tv1->v_type == VAR_SPECIAL
1307 && tv2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001308 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001309 if (tv1->v_type != tv2->v_type)
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001310 {
1311 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001312 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1313 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001314 return FAIL;
1315 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001316 n1 = tv1->vval.v_number;
1317 n2 = tv2->vval.v_number;
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001318 switch (type)
1319 {
1320 case EXPR_IS:
1321 case EXPR_EQUAL: n1 = (n1 == n2); break;
1322 case EXPR_ISNOT:
1323 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1324 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001325 semsg(_(e_invalid_operation_for_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001326 vartype_name(tv1->v_type));
1327 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001328 return FAIL;
1329 }
1330 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001331#ifdef FEAT_JOB_CHANNEL
1332 else if (tv1->v_type == tv2->v_type
1333 && (tv1->v_type == VAR_CHANNEL || tv1->v_type == VAR_JOB)
1334 && (type == EXPR_NEQUAL || type == EXPR_EQUAL))
1335 {
1336 if (tv1->v_type == VAR_CHANNEL)
1337 n1 = tv1->vval.v_channel == tv2->vval.v_channel;
1338 else
1339 n1 = tv1->vval.v_job == tv2->vval.v_job;
1340 if (type == EXPR_NEQUAL)
1341 n1 = !n1;
1342 }
1343#endif
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001344 else
1345 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001346 if (typval_compare_string(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar0c357522021-07-18 14:43:43 +02001347 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001348 clear_tv(tv1);
Bram Moolenaar0c357522021-07-18 14:43:43 +02001349 return FAIL;
1350 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001351 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001352 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001353 clear_tv(tv1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001354 if (in_vim9script())
1355 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001356 tv1->v_type = VAR_BOOL;
1357 tv1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001358 }
1359 else
1360 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001361 tv1->v_type = VAR_NUMBER;
1362 tv1->vval.v_number = n1;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001363 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001364
1365 return OK;
1366}
1367
Bram Moolenaar34453202021-01-31 13:08:38 +01001368/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001369 * Compare "tv1" to "tv2" as lists acording to "type" and "ic".
1370 * Put the result, false or true, in "res".
1371 * Return FAIL and give an error message when the comparison can't be done.
1372 */
1373 int
1374typval_compare_list(
1375 typval_T *tv1,
1376 typval_T *tv2,
1377 exprtype_T type,
1378 int ic,
1379 int *res)
1380{
1381 int val = 0;
1382
1383 if (type == EXPR_IS || type == EXPR_ISNOT)
1384 {
1385 val = (tv1->v_type == tv2->v_type
1386 && tv1->vval.v_list == tv2->vval.v_list);
1387 if (type == EXPR_ISNOT)
1388 val = !val;
1389 }
1390 else if (tv1->v_type != tv2->v_type
1391 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1392 {
1393 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001394 emsg(_(e_can_only_compare_list_with_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001395 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001396 emsg(_(e_invalid_operation_for_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001397 return FAIL;
1398 }
1399 else
1400 {
1401 val = list_equal(tv1->vval.v_list, tv2->vval.v_list,
1402 ic, FALSE);
1403 if (type == EXPR_NEQUAL)
1404 val = !val;
1405 }
1406 *res = val;
1407 return OK;
1408}
1409
1410/*
Bram Moolenaarf3507a52022-03-09 11:56:21 +00001411 * Compare v:null with another type. Return TRUE if the value is NULL.
Bram Moolenaar7a222242022-03-01 19:23:24 +00001412 */
1413 int
1414typval_compare_null(typval_T *tv1, typval_T *tv2)
1415{
1416 if ((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1417 || (tv2->v_type == VAR_SPECIAL && tv2->vval.v_number == VVAL_NULL))
1418 {
1419 typval_T *tv = tv1->v_type == VAR_SPECIAL ? tv2 : tv1;
1420
1421 switch (tv->v_type)
1422 {
1423 case VAR_BLOB: return tv->vval.v_blob == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001424#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001425 case VAR_CHANNEL: return tv->vval.v_channel == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001426#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001427 case VAR_DICT: return tv->vval.v_dict == NULL;
1428 case VAR_FUNC: return tv->vval.v_string == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001429#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001430 case VAR_JOB: return tv->vval.v_job == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001431#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001432 case VAR_LIST: return tv->vval.v_list == NULL;
1433 case VAR_PARTIAL: return tv->vval.v_partial == NULL;
1434 case VAR_STRING: return tv->vval.v_string == NULL;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001435
1436 case VAR_NUMBER: if (!in_vim9script())
1437 return tv->vval.v_number == 0;
1438 break;
1439#ifdef FEAT_FLOAT
1440 case VAR_FLOAT: if (!in_vim9script())
1441 return tv->vval.v_float == 0.0;
1442 break;
1443#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001444 default: break;
1445 }
1446 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001447 // although comparing null with number, float or bool is not very useful
Bram Moolenaar05667812022-03-15 20:21:33 +00001448 // we won't give an error
1449 return FALSE;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001450}
1451
1452/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001453 * Compare "tv1" to "tv2" as blobs acording to "type".
1454 * Put the result, false or true, in "res".
1455 * Return FAIL and give an error message when the comparison can't be done.
1456 */
1457 int
1458typval_compare_blob(
1459 typval_T *tv1,
1460 typval_T *tv2,
1461 exprtype_T type,
1462 int *res)
1463{
1464 int val = 0;
1465
1466 if (type == EXPR_IS || type == EXPR_ISNOT)
1467 {
1468 val = (tv1->v_type == tv2->v_type
1469 && tv1->vval.v_blob == tv2->vval.v_blob);
1470 if (type == EXPR_ISNOT)
1471 val = !val;
1472 }
1473 else if (tv1->v_type != tv2->v_type
1474 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1475 {
1476 if (tv1->v_type != tv2->v_type)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001477 emsg(_(e_can_only_compare_blob_with_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001478 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001479 emsg(_(e_invalid_operation_for_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001480 return FAIL;
1481 }
1482 else
1483 {
1484 val = blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1485 if (type == EXPR_NEQUAL)
1486 val = !val;
1487 }
1488 *res = val;
1489 return OK;
1490}
1491
1492/*
1493 * Compare "tv1" to "tv2" as dictionaries acording to "type" and "ic".
1494 * Put the result, false or true, in "res".
1495 * Return FAIL and give an error message when the comparison can't be done.
1496 */
1497 int
1498typval_compare_dict(
1499 typval_T *tv1,
1500 typval_T *tv2,
1501 exprtype_T type,
1502 int ic,
1503 int *res)
1504{
1505 int val;
1506
1507 if (type == EXPR_IS || type == EXPR_ISNOT)
1508 {
1509 val = (tv1->v_type == tv2->v_type
1510 && tv1->vval.v_dict == tv2->vval.v_dict);
1511 if (type == EXPR_ISNOT)
1512 val = !val;
1513 }
1514 else if (tv1->v_type != tv2->v_type
1515 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1516 {
1517 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001518 emsg(_(e_can_only_compare_dictionary_with_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001519 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001520 emsg(_(e_invalid_operation_for_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001521 return FAIL;
1522 }
1523 else
1524 {
1525 val = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, FALSE);
1526 if (type == EXPR_NEQUAL)
1527 val = !val;
1528 }
1529 *res = val;
1530 return OK;
1531}
1532
1533/*
1534 * Compare "tv1" to "tv2" as funcrefs acording to "type" and "ic".
1535 * Put the result, false or true, in "res".
1536 * Return FAIL and give an error message when the comparison can't be done.
1537 */
1538 int
1539typval_compare_func(
1540 typval_T *tv1,
1541 typval_T *tv2,
1542 exprtype_T type,
1543 int ic,
1544 int *res)
1545{
1546 int val = 0;
1547
1548 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1549 && type != EXPR_IS && type != EXPR_ISNOT)
1550 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001551 emsg(_(e_invalid_operation_for_funcrefs));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001552 return FAIL;
1553 }
1554 if ((tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial == NULL)
1555 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial == NULL))
1556 // When both partials are NULL, then they are equal.
1557 // Otherwise they are not equal.
1558 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1559 else if (type == EXPR_IS || type == EXPR_ISNOT)
1560 {
1561 if (tv1->v_type == VAR_FUNC && tv2->v_type == VAR_FUNC)
1562 // strings are considered the same if their value is
1563 // the same
1564 val = tv_equal(tv1, tv2, ic, FALSE);
1565 else if (tv1->v_type == VAR_PARTIAL && tv2->v_type == VAR_PARTIAL)
1566 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1567 else
1568 val = FALSE;
1569 }
1570 else
1571 val = tv_equal(tv1, tv2, ic, FALSE);
1572 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1573 val = !val;
1574 *res = val;
1575 return OK;
1576}
1577
1578/*
1579 * Compare "tv1" to "tv2" as strings according to "type" and "ic".
1580 * Put the result, false or true, in "res".
1581 * Return FAIL and give an error message when the comparison can't be done.
1582 */
1583 int
1584typval_compare_string(
1585 typval_T *tv1,
1586 typval_T *tv2,
1587 exprtype_T type,
1588 int ic,
1589 int *res)
1590{
1591 int i = 0;
1592 int val = FALSE;
1593 char_u *s1, *s2;
1594 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1595
1596 if (in_vim9script()
1597 && ((tv1->v_type != VAR_STRING && tv1->v_type != VAR_SPECIAL)
1598 || (tv2->v_type != VAR_STRING && tv2->v_type != VAR_SPECIAL)))
1599 {
1600 semsg(_(e_cannot_compare_str_with_str),
1601 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1602 return FAIL;
1603 }
1604 s1 = tv_get_string_buf(tv1, buf1);
1605 s2 = tv_get_string_buf(tv2, buf2);
1606 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1607 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1608 switch (type)
1609 {
Bram Moolenaarf8691002022-03-10 12:20:53 +00001610 case EXPR_IS: if (in_vim9script())
1611 {
1612 // Really check it is the same string, not just
1613 // the same value.
1614 val = tv1->vval.v_string == tv2->vval.v_string;
1615 break;
1616 }
1617 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001618 case EXPR_EQUAL: val = (i == 0); break;
Bram Moolenaarf8691002022-03-10 12:20:53 +00001619 case EXPR_ISNOT: if (in_vim9script())
1620 {
1621 // Really check it is not the same string, not
1622 // just a different value.
1623 val = tv1->vval.v_string != tv2->vval.v_string;
1624 break;
1625 }
1626 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001627 case EXPR_NEQUAL: val = (i != 0); break;
1628 case EXPR_GREATER: val = (i > 0); break;
1629 case EXPR_GEQUAL: val = (i >= 0); break;
1630 case EXPR_SMALLER: val = (i < 0); break;
1631 case EXPR_SEQUAL: val = (i <= 0); break;
1632
1633 case EXPR_MATCH:
1634 case EXPR_NOMATCH:
1635 val = pattern_match(s2, s1, ic);
1636 if (type == EXPR_NOMATCH)
1637 val = !val;
1638 break;
1639
1640 default: break; // avoid gcc warning
1641 }
1642 *res = val;
1643 return OK;
1644}
1645/*
Bram Moolenaar34453202021-01-31 13:08:38 +01001646 * Convert any type to a string, never give an error.
1647 * When "quotes" is TRUE add quotes to a string.
1648 * Returns an allocated string.
1649 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001650 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001651typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001652{
1653 char_u *tofree;
1654 char_u numbuf[NUMBUFLEN];
1655 char_u *ret = NULL;
1656
1657 if (arg == NULL)
1658 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001659 if (!quotes && arg->v_type == VAR_STRING)
1660 {
1661 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1662 : arg->vval.v_string);
1663 }
1664 else
1665 {
1666 ret = tv2string(arg, &tofree, numbuf, 0);
1667 // Make a copy if we have a value but it's not in allocated memory.
1668 if (ret != NULL && tofree == NULL)
1669 ret = vim_strsave(ret);
1670 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001671 return ret;
1672}
1673
1674/*
1675 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1676 * or it refers to a List or Dictionary that is locked.
1677 */
1678 int
1679tv_islocked(typval_T *tv)
1680{
1681 return (tv->v_lock & VAR_LOCKED)
1682 || (tv->v_type == VAR_LIST
1683 && tv->vval.v_list != NULL
1684 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1685 || (tv->v_type == VAR_DICT
1686 && tv->vval.v_dict != NULL
1687 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1688}
1689
1690 static int
1691func_equal(
1692 typval_T *tv1,
1693 typval_T *tv2,
1694 int ic) // ignore case
1695{
1696 char_u *s1, *s2;
1697 dict_T *d1, *d2;
1698 int a1, a2;
1699 int i;
1700
1701 // empty and NULL function name considered the same
1702 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1703 : partial_name(tv1->vval.v_partial);
1704 if (s1 != NULL && *s1 == NUL)
1705 s1 = NULL;
1706 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1707 : partial_name(tv2->vval.v_partial);
1708 if (s2 != NULL && *s2 == NUL)
1709 s2 = NULL;
1710 if (s1 == NULL || s2 == NULL)
1711 {
1712 if (s1 != s2)
1713 return FALSE;
1714 }
1715 else if (STRCMP(s1, s2) != 0)
1716 return FALSE;
1717
1718 // empty dict and NULL dict is different
1719 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1720 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1721 if (d1 == NULL || d2 == NULL)
1722 {
1723 if (d1 != d2)
1724 return FALSE;
1725 }
1726 else if (!dict_equal(d1, d2, ic, TRUE))
1727 return FALSE;
1728
1729 // empty list and no list considered the same
1730 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1731 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1732 if (a1 != a2)
1733 return FALSE;
1734 for (i = 0; i < a1; ++i)
1735 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1736 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1737 return FALSE;
1738
1739 return TRUE;
1740}
1741
1742/*
1743 * Return TRUE if "tv1" and "tv2" have the same value.
1744 * Compares the items just like "==" would compare them, but strings and
1745 * numbers are different. Floats and numbers are also different.
1746 */
1747 int
1748tv_equal(
1749 typval_T *tv1,
1750 typval_T *tv2,
1751 int ic, // ignore case
1752 int recursive) // TRUE when used recursively
1753{
1754 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1755 char_u *s1, *s2;
1756 static int recursive_cnt = 0; // catch recursive loops
1757 int r;
1758 static int tv_equal_recurse_limit;
1759
1760 // Catch lists and dicts that have an endless loop by limiting
1761 // recursiveness to a limit. We guess they are equal then.
1762 // A fixed limit has the problem of still taking an awful long time.
1763 // Reduce the limit every time running into it. That should work fine for
1764 // deeply linked structures that are not recursively linked and catch
1765 // recursiveness quickly.
1766 if (!recursive)
1767 tv_equal_recurse_limit = 1000;
1768 if (recursive_cnt >= tv_equal_recurse_limit)
1769 {
1770 --tv_equal_recurse_limit;
1771 return TRUE;
1772 }
1773
1774 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1775 // arguments.
1776 if ((tv1->v_type == VAR_FUNC
1777 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1778 && (tv2->v_type == VAR_FUNC
1779 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1780 {
1781 ++recursive_cnt;
1782 r = func_equal(tv1, tv2, ic);
1783 --recursive_cnt;
1784 return r;
1785 }
1786
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001787 if (tv1->v_type != tv2->v_type
1788 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1789 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001790 return FALSE;
1791
1792 switch (tv1->v_type)
1793 {
1794 case VAR_LIST:
1795 ++recursive_cnt;
1796 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1797 --recursive_cnt;
1798 return r;
1799
1800 case VAR_DICT:
1801 ++recursive_cnt;
1802 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1803 --recursive_cnt;
1804 return r;
1805
1806 case VAR_BLOB:
1807 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1808
1809 case VAR_NUMBER:
1810 case VAR_BOOL:
1811 case VAR_SPECIAL:
1812 return tv1->vval.v_number == tv2->vval.v_number;
1813
1814 case VAR_STRING:
1815 s1 = tv_get_string_buf(tv1, buf1);
1816 s2 = tv_get_string_buf(tv2, buf2);
1817 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1818
1819 case VAR_FLOAT:
1820#ifdef FEAT_FLOAT
1821 return tv1->vval.v_float == tv2->vval.v_float;
1822#endif
1823 case VAR_JOB:
1824#ifdef FEAT_JOB_CHANNEL
1825 return tv1->vval.v_job == tv2->vval.v_job;
1826#endif
1827 case VAR_CHANNEL:
1828#ifdef FEAT_JOB_CHANNEL
1829 return tv1->vval.v_channel == tv2->vval.v_channel;
1830#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001831 case VAR_INSTR:
1832 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001833
1834 case VAR_PARTIAL:
1835 return tv1->vval.v_partial == tv2->vval.v_partial;
1836
1837 case VAR_FUNC:
1838 return tv1->vval.v_string == tv2->vval.v_string;
1839
1840 case VAR_UNKNOWN:
1841 case VAR_ANY:
1842 case VAR_VOID:
1843 break;
1844 }
1845
1846 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1847 // does not equal anything, not even itself.
1848 return FALSE;
1849}
1850
1851/*
1852 * Get an option value.
1853 * "arg" points to the '&' or '+' before the option name.
1854 * "arg" is advanced to character after the option name.
1855 * Return OK or FAIL.
1856 */
1857 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001858eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001859 char_u **arg,
1860 typval_T *rettv, // when NULL, only check if option exists
1861 int evaluate)
1862{
1863 char_u *option_end;
1864 long numval;
1865 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001866 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001867 int c;
1868 int working = (**arg == '+'); // has("+option")
1869 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001870 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001871
1872 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001873 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001874 if (option_end == NULL)
1875 {
1876 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001877 semsg(_(e_option_name_missing_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001878 return FAIL;
1879 }
1880
1881 if (!evaluate)
1882 {
1883 *arg = option_end;
1884 return OK;
1885 }
1886
1887 c = *option_end;
1888 *option_end = NUL;
1889 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001890 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001891
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001892 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001893 {
1894 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001895 semsg(_(e_unknown_option_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001896 ret = FAIL;
1897 }
1898 else if (rettv != NULL)
1899 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001900 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001901 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001902 {
1903 rettv->v_type = VAR_STRING;
1904 rettv->vval.v_string = NULL;
1905 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001906 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001907 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001908 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1909 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001910 rettv->vval.v_number = 0;
1911 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001912 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001913 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001914 if (in_vim9script() && opt_type == gov_bool)
1915 {
1916 rettv->v_type = VAR_BOOL;
1917 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1918 }
1919 else
1920 {
1921 rettv->v_type = VAR_NUMBER;
1922 rettv->vval.v_number = numval;
1923 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001924 }
1925 else // string option
1926 {
1927 rettv->v_type = VAR_STRING;
1928 rettv->vval.v_string = stringval;
1929 }
1930 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001931 else if (working && (opt_type == gov_hidden_bool
1932 || opt_type == gov_hidden_number
1933 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001934 ret = FAIL;
1935
1936 *option_end = c; // put back for error messages
1937 *arg = option_end;
1938
1939 return ret;
1940}
1941
1942/*
1943 * Allocate a variable for a number constant. Also deals with "0z" for blob.
1944 * Return OK or FAIL.
1945 */
1946 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001947eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001948 char_u **arg,
1949 typval_T *rettv,
1950 int evaluate,
1951 int want_string UNUSED)
1952{
1953 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02001954 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001955#ifdef FEAT_FLOAT
1956 char_u *p;
1957 int get_float = FALSE;
1958
1959 // We accept a float when the format matches
1960 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
1961 // strict to avoid backwards compatibility problems.
1962 // With script version 2 and later the leading digit can be
1963 // omitted.
1964 // Don't look for a float after the "." operator, so that
1965 // ":let vers = 1.2.3" doesn't fail.
1966 if (**arg == '.')
1967 p = *arg;
1968 else
Bram Moolenaar29500652021-08-08 15:43:34 +02001969 {
1970 p = *arg + 1;
1971 if (skip_quotes)
1972 for (;;)
1973 {
1974 if (*p == '\'')
1975 ++p;
1976 if (!vim_isdigit(*p))
1977 break;
1978 p = skipdigits(p);
1979 }
1980 else
1981 p = skipdigits(p);
1982 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001983 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
1984 {
1985 get_float = TRUE;
1986 p = skipdigits(p + 2);
1987 if (*p == 'e' || *p == 'E')
1988 {
1989 ++p;
1990 if (*p == '-' || *p == '+')
1991 ++p;
1992 if (!vim_isdigit(*p))
1993 get_float = FALSE;
1994 else
1995 p = skipdigits(p + 1);
1996 }
1997 if (ASCII_ISALPHA(*p) || *p == '.')
1998 get_float = FALSE;
1999 }
2000 if (get_float)
2001 {
2002 float_T f;
2003
Bram Moolenaar29500652021-08-08 15:43:34 +02002004 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002005 if (evaluate)
2006 {
2007 rettv->v_type = VAR_FLOAT;
2008 rettv->vval.v_float = f;
2009 }
2010 }
2011 else
2012#endif
2013 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
2014 {
2015 char_u *bp;
2016 blob_T *blob = NULL; // init for gcc
2017
2018 // Blob constant: 0z0123456789abcdef
2019 if (evaluate)
2020 blob = blob_alloc();
2021 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2022 {
2023 if (!vim_isxdigit(bp[1]))
2024 {
2025 if (blob != NULL)
2026 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002027 emsg(_(e_blob_literal_should_have_an_even_number_of_hex_characters));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002028 ga_clear(&blob->bv_ga);
2029 VIM_CLEAR(blob);
2030 }
2031 return FAIL;
2032 }
2033 if (blob != NULL)
2034 ga_append(&blob->bv_ga,
2035 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
2036 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2037 ++bp;
2038 }
2039 if (blob != NULL)
2040 rettv_blob_set(rettv, blob);
2041 *arg = bp;
2042 }
2043 else
2044 {
2045 varnumber_T n;
2046
2047 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02002048 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002049 ? STR2NR_NO_OCT + STR2NR_QUOTE
2050 : STR2NR_ALL, &n, NULL, 0, TRUE);
2051 if (len == 0)
2052 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00002053 if (evaluate)
2054 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002055 return FAIL;
2056 }
2057 *arg += len;
2058 if (evaluate)
2059 {
2060 rettv->v_type = VAR_NUMBER;
2061 rettv->vval.v_number = n;
2062 }
2063 }
2064 return OK;
2065}
2066
2067/*
2068 * Allocate a variable for a string constant.
2069 * Return OK or FAIL.
2070 */
2071 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002072eval_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002073{
2074 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002075 char_u *end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002076 int extra = 0;
2077 int len;
2078
2079 // Find the end of the string, skipping backslashed characters.
2080 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
2081 {
2082 if (*p == '\\' && p[1] != NUL)
2083 {
2084 ++p;
2085 // A "\<x>" form occupies at least 4 characters, and produces up
zeertzjqdb088872022-05-02 22:53:45 +01002086 // to 9 characters (6 for the char and 3 for a modifier):
2087 // reserve space for 5 extra.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002088 if (*p == '<')
zeertzjqdb088872022-05-02 22:53:45 +01002089 extra += 5;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002090 }
2091 }
2092
2093 if (*p != '"')
2094 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002095 semsg(_(e_missing_double_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002096 return FAIL;
2097 }
2098
2099 // If only parsing, set *arg and return here
2100 if (!evaluate)
2101 {
2102 *arg = p + 1;
2103 return OK;
2104 }
2105
2106 // Copy the string into allocated memory, handling backslashed
2107 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002108 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002109 len = (int)(p - *arg + extra);
2110 rettv->vval.v_string = alloc(len);
2111 if (rettv->vval.v_string == NULL)
2112 return FAIL;
2113 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002114
2115 for (p = *arg + 1; *p != NUL && *p != '"'; )
2116 {
2117 if (*p == '\\')
2118 {
2119 switch (*++p)
2120 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002121 case 'b': *end++ = BS; ++p; break;
2122 case 'e': *end++ = ESC; ++p; break;
2123 case 'f': *end++ = FF; ++p; break;
2124 case 'n': *end++ = NL; ++p; break;
2125 case 'r': *end++ = CAR; ++p; break;
2126 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002127
2128 case 'X': // hex: "\x1", "\x12"
2129 case 'x':
2130 case 'u': // Unicode: "\u0023"
2131 case 'U':
2132 if (vim_isxdigit(p[1]))
2133 {
2134 int n, nr;
2135 int c = toupper(*p);
2136
2137 if (c == 'X')
2138 n = 2;
2139 else if (*p == 'u')
2140 n = 4;
2141 else
2142 n = 8;
2143 nr = 0;
2144 while (--n >= 0 && vim_isxdigit(p[1]))
2145 {
2146 ++p;
2147 nr = (nr << 4) + hex2nr(*p);
2148 }
2149 ++p;
2150 // For "\u" store the number according to
2151 // 'encoding'.
2152 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002153 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002154 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002155 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002156 }
2157 break;
2158
2159 // octal: "\1", "\12", "\123"
2160 case '0':
2161 case '1':
2162 case '2':
2163 case '3':
2164 case '4':
2165 case '5':
2166 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002167 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002168 if (*p >= '0' && *p <= '7')
2169 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002170 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002171 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002172 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002173 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002174 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002175 break;
2176
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002177 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002178 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002179 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002180 int flags = FSK_KEYCODE | FSK_IN_STRING;
2181
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002182 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002183 flags |= FSK_SIMPLIFY;
zeertzjqdb088872022-05-02 22:53:45 +01002184 extra = trans_special(&p, end, flags, FALSE, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002185 if (extra != 0)
2186 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002187 end += extra;
2188 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002189 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002190 break;
2191 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002192 }
2193 // FALLTHROUGH
2194
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002195 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002196 break;
2197 }
2198 }
2199 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002200 MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002201 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002202 *end = NUL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002203 if (*p != NUL) // just in case
2204 ++p;
2205 *arg = p;
2206
2207 return OK;
2208}
2209
2210/*
2211 * Allocate a variable for a 'str''ing' constant.
2212 * Return OK or FAIL.
2213 */
2214 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002215eval_lit_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002216{
2217 char_u *p;
2218 char_u *str;
2219 int reduce = 0;
2220
2221 // Find the end of the string, skipping ''.
2222 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
2223 {
2224 if (*p == '\'')
2225 {
2226 if (p[1] != '\'')
2227 break;
2228 ++reduce;
2229 ++p;
2230 }
2231 }
2232
2233 if (*p != '\'')
2234 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002235 semsg(_(e_missing_single_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002236 return FAIL;
2237 }
2238
2239 // If only parsing return after setting "*arg"
2240 if (!evaluate)
2241 {
2242 *arg = p + 1;
2243 return OK;
2244 }
2245
2246 // Copy the string into allocated memory, handling '' to ' reduction.
2247 str = alloc((p - *arg) - reduce);
2248 if (str == NULL)
2249 return FAIL;
2250 rettv->v_type = VAR_STRING;
2251 rettv->vval.v_string = str;
2252
2253 for (p = *arg + 1; *p != NUL; )
2254 {
2255 if (*p == '\'')
2256 {
2257 if (p[1] != '\'')
2258 break;
2259 ++p;
2260 }
2261 MB_COPY_CHAR(p, str);
2262 }
2263 *str = NUL;
2264 *arg = p + 1;
2265
2266 return OK;
2267}
2268
2269/*
2270 * Return a string with the string representation of a variable.
2271 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2272 * "numbuf" is used for a number.
2273 * Puts quotes around strings, so that they can be parsed back by eval().
2274 * May return NULL.
2275 */
2276 char_u *
2277tv2string(
2278 typval_T *tv,
2279 char_u **tofree,
2280 char_u *numbuf,
2281 int copyID)
2282{
2283 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2284}
2285
2286/*
2287 * Get the value of an environment variable.
2288 * "arg" is pointing to the '$'. It is advanced to after the name.
2289 * If the environment variable was not set, silently assume it is empty.
2290 * Return FAIL if the name is invalid.
2291 */
2292 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002293eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002294{
2295 char_u *string = NULL;
2296 int len;
2297 int cc;
2298 char_u *name;
2299 int mustfree = FALSE;
2300
2301 ++*arg;
2302 name = *arg;
2303 len = get_env_len(arg);
2304 if (evaluate)
2305 {
2306 if (len == 0)
2307 return FAIL; // invalid empty name
2308
2309 cc = name[len];
2310 name[len] = NUL;
2311 // first try vim_getenv(), fast for normal environment vars
2312 string = vim_getenv(name, &mustfree);
2313 if (string != NULL && *string != NUL)
2314 {
2315 if (!mustfree)
2316 string = vim_strsave(string);
2317 }
2318 else
2319 {
2320 if (mustfree)
2321 vim_free(string);
2322
2323 // next try expanding things like $VIM and ${HOME}
2324 string = expand_env_save(name - 1);
2325 if (string != NULL && *string == '$')
2326 VIM_CLEAR(string);
2327 }
2328 name[len] = cc;
2329
2330 rettv->v_type = VAR_STRING;
2331 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002332 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002333 }
2334
2335 return OK;
2336}
2337
2338/*
2339 * Get the lnum from the first argument.
2340 * Also accepts ".", "$", etc., but that only works for the current buffer.
2341 * Returns -1 on error.
2342 */
2343 linenr_T
2344tv_get_lnum(typval_T *argvars)
2345{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002346 linenr_T lnum = -1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002347
Bram Moolenaar56acb092020-08-16 14:48:19 +02002348 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2349 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002350 if (lnum <= 0 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002351 {
2352 int fnum;
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002353 pos_T *fp;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002354
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002355 // no valid number, try using arg like line()
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002356 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002357 if (fp != NULL)
2358 lnum = fp->lnum;
2359 }
2360 return lnum;
2361}
2362
2363/*
2364 * Get the lnum from the first argument.
2365 * Also accepts "$", then "buf" is used.
2366 * Returns 0 on error.
2367 */
2368 linenr_T
2369tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2370{
2371 if (argvars[0].v_type == VAR_STRING
2372 && argvars[0].vval.v_string != NULL
2373 && argvars[0].vval.v_string[0] == '$'
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002374 && argvars[0].vval.v_string[1] == NUL
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002375 && buf != NULL)
2376 return buf->b_ml.ml_line_count;
2377 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2378}
2379
2380/*
2381 * Get buffer by number or pattern.
2382 */
2383 buf_T *
2384tv_get_buf(typval_T *tv, int curtab_only)
2385{
2386 char_u *name = tv->vval.v_string;
2387 buf_T *buf;
2388
2389 if (tv->v_type == VAR_NUMBER)
2390 return buflist_findnr((int)tv->vval.v_number);
2391 if (tv->v_type != VAR_STRING)
2392 return NULL;
2393 if (name == NULL || *name == NUL)
2394 return curbuf;
2395 if (name[0] == '$' && name[1] == NUL)
2396 return lastbuf;
2397
2398 buf = buflist_find_by_name(name, curtab_only);
2399
2400 // If not found, try expanding the name, like done for bufexists().
2401 if (buf == NULL)
2402 buf = find_buffer(tv);
2403
2404 return buf;
2405}
2406
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002407/*
2408 * Like tv_get_buf() but give an error message is the type is wrong.
2409 */
2410 buf_T *
2411tv_get_buf_from_arg(typval_T *tv)
2412{
2413 buf_T *buf;
2414
2415 ++emsg_off;
2416 buf = tv_get_buf(tv, FALSE);
2417 --emsg_off;
2418 if (buf == NULL
2419 && tv->v_type != VAR_NUMBER
2420 && tv->v_type != VAR_STRING)
2421 // issue errmsg for type error
2422 (void)tv_get_number(tv);
2423 return buf;
2424}
2425
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002426#endif // FEAT_EVAL