blob: 09381f27927f706f03b23c63e67702b8c089c74a [file] [log] [blame]
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * typval.c: functions that deal with a typval
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
18/*
19 * Allocate memory for a variable type-value, and make it empty (0 or NULL
20 * value).
21 */
22 typval_T *
23alloc_tv(void)
24{
25 return ALLOC_CLEAR_ONE(typval_T);
26}
27
28/*
29 * Allocate memory for a variable type-value, and assign a string to it.
30 * The string "s" must have been allocated, it is consumed.
31 * Return NULL for out of memory, the variable otherwise.
32 */
33 typval_T *
34alloc_string_tv(char_u *s)
35{
36 typval_T *rettv;
37
38 rettv = alloc_tv();
39 if (rettv != NULL)
40 {
41 rettv->v_type = VAR_STRING;
42 rettv->vval.v_string = s;
43 }
44 else
45 vim_free(s);
46 return rettv;
47}
48
49/*
50 * Free the memory for a variable type-value.
51 */
52 void
53free_tv(typval_T *varp)
54{
55 if (varp != NULL)
56 {
57 switch (varp->v_type)
58 {
59 case VAR_FUNC:
60 func_unref(varp->vval.v_string);
61 // FALLTHROUGH
62 case VAR_STRING:
63 vim_free(varp->vval.v_string);
64 break;
65 case VAR_PARTIAL:
66 partial_unref(varp->vval.v_partial);
67 break;
68 case VAR_BLOB:
69 blob_unref(varp->vval.v_blob);
70 break;
71 case VAR_LIST:
72 list_unref(varp->vval.v_list);
73 break;
74 case VAR_DICT:
75 dict_unref(varp->vval.v_dict);
76 break;
77 case VAR_JOB:
78#ifdef FEAT_JOB_CHANNEL
79 job_unref(varp->vval.v_job);
80 break;
81#endif
82 case VAR_CHANNEL:
83#ifdef FEAT_JOB_CHANNEL
84 channel_unref(varp->vval.v_channel);
85 break;
86#endif
87 case VAR_NUMBER:
88 case VAR_FLOAT:
89 case VAR_ANY:
90 case VAR_UNKNOWN:
91 case VAR_VOID:
92 case VAR_BOOL:
93 case VAR_SPECIAL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +020094 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +020095 break;
96 }
97 vim_free(varp);
98 }
99}
100
101/*
102 * Free the memory for a variable value and set the value to NULL or 0.
103 */
104 void
105clear_tv(typval_T *varp)
106{
107 if (varp != NULL)
108 {
109 switch (varp->v_type)
110 {
111 case VAR_FUNC:
112 func_unref(varp->vval.v_string);
113 // FALLTHROUGH
114 case VAR_STRING:
115 VIM_CLEAR(varp->vval.v_string);
116 break;
117 case VAR_PARTIAL:
118 partial_unref(varp->vval.v_partial);
119 varp->vval.v_partial = NULL;
120 break;
121 case VAR_BLOB:
122 blob_unref(varp->vval.v_blob);
123 varp->vval.v_blob = NULL;
124 break;
125 case VAR_LIST:
126 list_unref(varp->vval.v_list);
127 varp->vval.v_list = NULL;
128 break;
129 case VAR_DICT:
130 dict_unref(varp->vval.v_dict);
131 varp->vval.v_dict = NULL;
132 break;
133 case VAR_NUMBER:
134 case VAR_BOOL:
135 case VAR_SPECIAL:
136 varp->vval.v_number = 0;
137 break;
138 case VAR_FLOAT:
139#ifdef FEAT_FLOAT
140 varp->vval.v_float = 0.0;
141 break;
142#endif
143 case VAR_JOB:
144#ifdef FEAT_JOB_CHANNEL
145 job_unref(varp->vval.v_job);
146 varp->vval.v_job = NULL;
147#endif
148 break;
149 case VAR_CHANNEL:
150#ifdef FEAT_JOB_CHANNEL
151 channel_unref(varp->vval.v_channel);
152 varp->vval.v_channel = NULL;
153#endif
Bram Moolenaar24f72092021-05-07 20:43:54 +0200154 break;
155 case VAR_INSTR:
156 VIM_CLEAR(varp->vval.v_instr);
157 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200158 case VAR_UNKNOWN:
159 case VAR_ANY:
160 case VAR_VOID:
161 break;
162 }
163 varp->v_lock = 0;
164 }
165}
166
167/*
168 * Set the value of a variable to NULL without freeing items.
169 */
170 void
171init_tv(typval_T *varp)
172{
173 if (varp != NULL)
174 CLEAR_POINTER(varp);
175}
176
Bram Moolenaar36967b32020-08-17 21:41:02 +0200177 static varnumber_T
178tv_get_bool_or_number_chk(typval_T *varp, int *denote, int want_bool)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200179{
180 varnumber_T n = 0L;
181
182 switch (varp->v_type)
183 {
184 case VAR_NUMBER:
Bram Moolenaarbade44e2020-09-26 22:39:24 +0200185 if (in_vim9script() && want_bool && varp->vval.v_number != 0
Bram Moolenaard70840e2020-08-22 15:06:35 +0200186 && varp->vval.v_number != 1)
187 {
188 semsg(_(e_using_number_as_bool_nr), varp->vval.v_number);
189 break;
190 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200191 return varp->vval.v_number;
192 case VAR_FLOAT:
193#ifdef FEAT_FLOAT
194 emsg(_("E805: Using a Float as a Number"));
195 break;
196#endif
197 case VAR_FUNC:
198 case VAR_PARTIAL:
199 emsg(_("E703: Using a Funcref as a Number"));
200 break;
201 case VAR_STRING:
Bram Moolenaar56acb092020-08-16 14:48:19 +0200202 if (in_vim9script())
203 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100204 emsg_using_string_as(varp, !want_bool);
Bram Moolenaar56acb092020-08-16 14:48:19 +0200205 break;
206 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200207 if (varp->vval.v_string != NULL)
208 vim_str2nr(varp->vval.v_string, NULL, NULL,
209 STR2NR_ALL, &n, NULL, 0, FALSE);
210 return n;
211 case VAR_LIST:
212 emsg(_("E745: Using a List as a Number"));
213 break;
214 case VAR_DICT:
215 emsg(_("E728: Using a Dictionary as a Number"));
216 break;
217 case VAR_BOOL:
218 case VAR_SPECIAL:
Bram Moolenaar36967b32020-08-17 21:41:02 +0200219 if (!want_bool && in_vim9script())
Bram Moolenaar56acb092020-08-16 14:48:19 +0200220 {
Bram Moolenaard92cc132020-11-18 17:17:15 +0100221 if (varp->v_type == VAR_BOOL)
222 emsg(_(e_using_bool_as_number));
223 else
224 emsg(_("E611: Using a Special as a Number"));
Bram Moolenaar56acb092020-08-16 14:48:19 +0200225 break;
226 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200227 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
228 case VAR_JOB:
229#ifdef FEAT_JOB_CHANNEL
230 emsg(_("E910: Using a Job as a Number"));
231 break;
232#endif
233 case VAR_CHANNEL:
234#ifdef FEAT_JOB_CHANNEL
235 emsg(_("E913: Using a Channel as a Number"));
236 break;
237#endif
238 case VAR_BLOB:
239 emsg(_("E974: Using a Blob as a Number"));
240 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200241 case VAR_VOID:
242 emsg(_(e_cannot_use_void_value));
243 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200244 case VAR_UNKNOWN:
245 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200246 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200247 internal_error_no_abort("tv_get_number(UNKNOWN)");
248 break;
249 }
250 if (denote == NULL) // useful for values that must be unsigned
251 n = -1;
252 else
253 *denote = TRUE;
254 return n;
255}
256
Bram Moolenaar36967b32020-08-17 21:41:02 +0200257/*
258 * Get the number value of a variable.
259 * If it is a String variable, uses vim_str2nr().
260 * For incompatible types, return 0.
261 * tv_get_number_chk() is similar to tv_get_number(), but informs the
262 * caller of incompatible types: it sets *denote to TRUE if "denote"
263 * is not NULL or returns -1 otherwise.
264 */
265 varnumber_T
266tv_get_number(typval_T *varp)
267{
268 int error = FALSE;
269
270 return tv_get_number_chk(varp, &error); // return 0L on error
271}
272
273 varnumber_T
274tv_get_number_chk(typval_T *varp, int *denote)
275{
276 return tv_get_bool_or_number_chk(varp, denote, FALSE);
277}
278
279/*
280 * Get the boolean value of "varp". This is like tv_get_number_chk(),
Bram Moolenaard70840e2020-08-22 15:06:35 +0200281 * but in Vim9 script accepts Number (0 and 1) and Bool/Special.
Bram Moolenaar36967b32020-08-17 21:41:02 +0200282 */
283 varnumber_T
284tv_get_bool(typval_T *varp)
285{
286 return tv_get_bool_or_number_chk(varp, NULL, TRUE);
Bram Moolenaar36967b32020-08-17 21:41:02 +0200287}
288
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200289/*
290 * Get the boolean value of "varp". This is like tv_get_number_chk(),
291 * but in Vim9 script accepts Number and Bool.
292 */
293 varnumber_T
294tv_get_bool_chk(typval_T *varp, int *denote)
295{
296 return tv_get_bool_or_number_chk(varp, denote, TRUE);
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200297}
298
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200299#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200300 float_T
301tv_get_float(typval_T *varp)
302{
303 switch (varp->v_type)
304 {
305 case VAR_NUMBER:
306 return (float_T)(varp->vval.v_number);
307 case VAR_FLOAT:
308 return varp->vval.v_float;
309 case VAR_FUNC:
310 case VAR_PARTIAL:
311 emsg(_("E891: Using a Funcref as a Float"));
312 break;
313 case VAR_STRING:
314 emsg(_("E892: Using a String as a Float"));
315 break;
316 case VAR_LIST:
317 emsg(_("E893: Using a List as a Float"));
318 break;
319 case VAR_DICT:
320 emsg(_("E894: Using a Dictionary as a Float"));
321 break;
322 case VAR_BOOL:
323 emsg(_("E362: Using a boolean value as a Float"));
324 break;
325 case VAR_SPECIAL:
326 emsg(_("E907: Using a special value as a Float"));
327 break;
328 case VAR_JOB:
329# ifdef FEAT_JOB_CHANNEL
330 emsg(_("E911: Using a Job as a Float"));
331 break;
332# endif
333 case VAR_CHANNEL:
334# ifdef FEAT_JOB_CHANNEL
335 emsg(_("E914: Using a Channel as a Float"));
336 break;
337# endif
338 case VAR_BLOB:
339 emsg(_("E975: Using a Blob as a Float"));
340 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200341 case VAR_VOID:
342 emsg(_(e_cannot_use_void_value));
343 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200344 case VAR_UNKNOWN:
345 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200346 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200347 internal_error_no_abort("tv_get_float(UNKNOWN)");
348 break;
349 }
350 return 0;
351}
352#endif
353
354/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200355 * Give an error and return FAIL unless "args[idx]" is a string.
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100356 */
357 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100358check_for_string_arg(typval_T *args, int idx)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100359{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100360 if (args[idx].v_type != VAR_STRING)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100361 {
rbtnnddc80af2021-12-17 18:01:31 +0000362 semsg(_(e_string_required_for_argument_nr), idx + 1);
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100363 return FAIL;
364 }
365 return OK;
366}
367
368/*
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100369 * Give an error and return FAIL unless "args[idx]" is a non-empty string.
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100370 */
371 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100372check_for_nonempty_string_arg(typval_T *args, int idx)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100373{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100374 if (check_for_string_arg(args, idx) == FAIL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100375 return FAIL;
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100376 if (args[idx].vval.v_string == NULL || *args[idx].vval.v_string == NUL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100377 {
Bram Moolenaare8e30782021-04-10 21:46:05 +0200378 semsg(_(e_non_empty_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100379 return FAIL;
380 }
381 return OK;
382}
383
384/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200385 * Check for an optional string argument at 'idx'
386 */
387 int
388check_for_opt_string_arg(typval_T *args, int idx)
389{
390 return (args[idx].v_type == VAR_UNKNOWN
391 || check_for_string_arg(args, idx) != FAIL);
392}
393
394/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200395 * Give an error and return FAIL unless "args[idx]" is a number.
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200396 */
397 int
398check_for_number_arg(typval_T *args, int idx)
399{
400 if (args[idx].v_type != VAR_NUMBER)
401 {
rbtnnddc80af2021-12-17 18:01:31 +0000402 semsg(_(e_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200403 return FAIL;
404 }
405 return OK;
406}
407
408/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200409 * Check for an optional number argument at 'idx'
410 */
411 int
412check_for_opt_number_arg(typval_T *args, int idx)
413{
414 return (args[idx].v_type == VAR_UNKNOWN
415 || check_for_number_arg(args, idx) != FAIL);
416}
417
418/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200419 * Give an error and return FAIL unless "args[idx]" is a float or a number.
420 */
421 int
422check_for_float_or_nr_arg(typval_T *args, int idx)
423{
424 if (args[idx].v_type != VAR_FLOAT && args[idx].v_type != VAR_NUMBER)
425 {
rbtnnddc80af2021-12-17 18:01:31 +0000426 semsg(_(e_float_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200427 return FAIL;
428 }
429 return OK;
430}
431
432/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200433 * Give an error and return FAIL unless "args[idx]" is a bool.
434 */
435 int
436check_for_bool_arg(typval_T *args, int idx)
437{
438 if (args[idx].v_type != VAR_BOOL
439 && !(args[idx].v_type == VAR_NUMBER
440 && (args[idx].vval.v_number == 0
441 || args[idx].vval.v_number == 1)))
442 {
rbtnnddc80af2021-12-17 18:01:31 +0000443 semsg(_(e_bool_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200444 return FAIL;
445 }
446 return OK;
447}
448
449/*
Bram Moolenaara29856f2021-09-13 21:36:27 +0200450 * Check for an optional bool argument at 'idx'.
451 * Return FAIL if the type is wrong.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200452 */
453 int
454check_for_opt_bool_arg(typval_T *args, int idx)
455{
Bram Moolenaara29856f2021-09-13 21:36:27 +0200456 if (args[idx].v_type == VAR_UNKNOWN)
457 return OK;
458 return check_for_bool_arg(args, idx);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200459}
460
461/*
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200462 * Give an error and return FAIL unless "args[idx]" is a blob.
463 */
464 int
465check_for_blob_arg(typval_T *args, int idx)
466{
467 if (args[idx].v_type != VAR_BLOB)
468 {
rbtnnddc80af2021-12-17 18:01:31 +0000469 semsg(_(e_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200470 return FAIL;
471 }
472 return OK;
473}
474
475/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200476 * Give an error and return FAIL unless "args[idx]" is a list.
477 */
478 int
479check_for_list_arg(typval_T *args, int idx)
480{
481 if (args[idx].v_type != VAR_LIST)
482 {
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200483 semsg(_(e_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200484 return FAIL;
485 }
486 return OK;
487}
488
489/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200490 * Check for an optional list argument at 'idx'
491 */
492 int
493check_for_opt_list_arg(typval_T *args, int idx)
494{
495 return (args[idx].v_type == VAR_UNKNOWN
496 || check_for_list_arg(args, idx) != FAIL);
497}
498
499/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200500 * Give an error and return FAIL unless "args[idx]" is a dict.
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200501 */
502 int
503check_for_dict_arg(typval_T *args, int idx)
504{
505 if (args[idx].v_type != VAR_DICT)
506 {
rbtnnddc80af2021-12-17 18:01:31 +0000507 semsg(_(e_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200508 return FAIL;
509 }
510 return OK;
511}
512
513/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200514 * Check for an optional dict argument at 'idx'
515 */
516 int
517check_for_opt_dict_arg(typval_T *args, int idx)
518{
519 return (args[idx].v_type == VAR_UNKNOWN
520 || check_for_dict_arg(args, idx) != FAIL);
521}
522
523/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200524 * Give an error and return FAIL unless "args[idx]" is a channel or a job.
525 */
526 int
527check_for_chan_or_job_arg(typval_T *args, int idx)
528{
529 if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
530 {
rbtnnddc80af2021-12-17 18:01:31 +0000531 semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200532 return FAIL;
533 }
534 return OK;
535}
536
537/*
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200538 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
539 * job.
540 */
541 int
542check_for_opt_chan_or_job_arg(typval_T *args, int idx)
543{
544 return (args[idx].v_type == VAR_UNKNOWN
545 || check_for_chan_or_job_arg(args, idx) != FAIL);
546}
547
548/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200549 * Give an error and return FAIL unless "args[idx]" is a job.
550 */
551 int
552check_for_job_arg(typval_T *args, int idx)
553{
554 if (args[idx].v_type != VAR_JOB)
555 {
rbtnnddc80af2021-12-17 18:01:31 +0000556 semsg(_(e_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200557 return FAIL;
558 }
559 return OK;
560}
561
562/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200563 * Check for an optional job argument at 'idx'.
564 */
565 int
566check_for_opt_job_arg(typval_T *args, int idx)
567{
568 return (args[idx].v_type == VAR_UNKNOWN
569 || check_for_job_arg(args, idx) != FAIL);
570}
571
572/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200573 * Give an error and return FAIL unless "args[idx]" is a string or
574 * a number.
575 */
576 int
577check_for_string_or_number_arg(typval_T *args, int idx)
578{
579 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
580 {
rbtnnddc80af2021-12-17 18:01:31 +0000581 semsg(_(e_string_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200582 return FAIL;
583 }
584 return OK;
585}
586
587/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200588 * Check for an optional string or number argument at 'idx'.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200589 */
590 int
591check_for_opt_string_or_number_arg(typval_T *args, int idx)
592{
593 return (args[idx].v_type == VAR_UNKNOWN
594 || check_for_string_or_number_arg(args, idx) != FAIL);
595}
596
597/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200598 * Give an error and return FAIL unless "args[idx]" is a buffer number.
599 * Buffer number can be a number or a string.
600 */
601 int
602check_for_buffer_arg(typval_T *args, int idx)
603{
604 return check_for_string_or_number_arg(args, idx);
605}
606
607/*
608 * Check for an optional buffer argument at 'idx'
609 */
610 int
611check_for_opt_buffer_arg(typval_T *args, int idx)
612{
613 return (args[idx].v_type == VAR_UNKNOWN
614 || check_for_buffer_arg(args, idx));
615}
616
617/*
618 * Give an error and return FAIL unless "args[idx]" is a line number.
619 * Line number can be a number or a string.
620 */
621 int
622check_for_lnum_arg(typval_T *args, int idx)
623{
624 return check_for_string_or_number_arg(args, idx);
625}
626
627/*
628 * Check for an optional line number argument at 'idx'
629 */
630 int
631check_for_opt_lnum_arg(typval_T *args, int idx)
632{
633 return (args[idx].v_type == VAR_UNKNOWN
634 || check_for_lnum_arg(args, idx));
635}
636
637/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200638 * Give an error and return FAIL unless "args[idx]" is a string or a blob.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200639 */
640 int
641check_for_string_or_blob_arg(typval_T *args, int idx)
642{
643 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
644 {
rbtnnddc80af2021-12-17 18:01:31 +0000645 semsg(_(e_string_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200646 return FAIL;
647 }
648 return OK;
649}
650
651/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200652 * Give an error and return FAIL unless "args[idx]" is a string or a list.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200653 */
654 int
655check_for_string_or_list_arg(typval_T *args, int idx)
656{
657 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
658 {
rbtnnddc80af2021-12-17 18:01:31 +0000659 semsg(_(e_string_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200660 return FAIL;
661 }
662 return OK;
663}
664
665/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200666 * Check for an optional string or list argument at 'idx'
667 */
668 int
669check_for_opt_string_or_list_arg(typval_T *args, int idx)
670{
671 return (args[idx].v_type == VAR_UNKNOWN
672 || check_for_string_or_list_arg(args, idx));
673}
674
675/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200676 * Give an error and return FAIL unless "args[idx]" is a string or a dict.
677 */
678 int
679check_for_string_or_dict_arg(typval_T *args, int idx)
680{
681 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_DICT)
682 {
rbtnnddc80af2021-12-17 18:01:31 +0000683 semsg(_(e_string_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200684 return FAIL;
685 }
686 return OK;
687}
688
689/*
690 * Give an error and return FAIL unless "args[idx]" is a string or a number
691 * or a list.
692 */
693 int
694check_for_string_or_number_or_list_arg(typval_T *args, int idx)
695{
696 if (args[idx].v_type != VAR_STRING
697 && args[idx].v_type != VAR_NUMBER
698 && args[idx].v_type != VAR_LIST)
699 {
700 if (idx >= 0)
Bram Moolenaar78db17c2021-07-31 19:12:58 +0200701 semsg(_(e_string_number_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200702 else
703 emsg(_(e_stringreq));
704 return FAIL;
705 }
706 return OK;
707}
708
709/*
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200710 * Give an error and return FAIL unless "args[idx]" is an optional string
711 * or number or a list
712 */
713 int
714check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx)
715{
716 return (args[idx].v_type == VAR_UNKNOWN
717 || check_for_string_or_number_or_list_arg(args, idx) != FAIL);
718}
719
720/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200721 * Give an error and return FAIL unless "args[idx]" is a string or a list
722 * or a dict.
723 */
724 int
725check_for_string_or_list_or_dict_arg(typval_T *args, int idx)
726{
727 if (args[idx].v_type != VAR_STRING
728 && args[idx].v_type != VAR_LIST
729 && args[idx].v_type != VAR_DICT)
730 {
rbtnnddc80af2021-12-17 18:01:31 +0000731 semsg(_(e_string_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200732 return FAIL;
733 }
734 return OK;
735}
736
737/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200738 * Give an error and return FAIL unless "args[idx]" is a list or a blob.
739 */
740 int
741check_for_list_or_blob_arg(typval_T *args, int idx)
742{
743 if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
744 {
745 if (idx >= 0)
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200746 semsg(_(e_list_or_blob_required_for_argument_nr), idx + 1);
747 else
748 emsg(_(e_listreq));
749 return FAIL;
750 }
751 return OK;
752}
753
754/*
755 * Give an error and return FAIL unless "args[idx]" is a list or dict
756 */
757 int
758check_for_list_or_dict_arg(typval_T *args, int idx)
759{
760 if (args[idx].v_type != VAR_LIST
761 && args[idx].v_type != VAR_DICT)
762 {
rbtnnddc80af2021-12-17 18:01:31 +0000763 semsg(_(e_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200764 return FAIL;
765 }
766 return OK;
767}
768
769/*
770 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
771 * blob.
772 */
773 int
774check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
775{
776 if (args[idx].v_type != VAR_LIST
777 && args[idx].v_type != VAR_DICT
778 && args[idx].v_type != VAR_BLOB)
779 {
rbtnnddc80af2021-12-17 18:01:31 +0000780 semsg(_(e_list_dict_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200781 return FAIL;
782 }
783 return OK;
784}
785
786/*
Bram Moolenaar2d877592021-12-16 08:21:09 +0000787 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
788 * blob or a string.
789 */
790 int
791check_for_list_or_dict_or_blob_or_string_arg(typval_T *args, int idx)
792{
793 if (args[idx].v_type != VAR_LIST
794 && args[idx].v_type != VAR_DICT
795 && args[idx].v_type != VAR_BLOB
796 && args[idx].v_type != VAR_STRING)
797 {
rbtnnddc80af2021-12-17 18:01:31 +0000798 semsg(_(e_list_dict_blob_or_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2d877592021-12-16 08:21:09 +0000799 return FAIL;
800 }
801 return OK;
802}
803
804/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200805 * Give an error and return FAIL unless "args[idx]" is an optional buffer
806 * number or a dict.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200807 */
808 int
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200809check_for_opt_buffer_or_dict_arg(typval_T *args, int idx)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200810{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200811 if (args[idx].v_type != VAR_UNKNOWN
812 && args[idx].v_type != VAR_STRING
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200813 && args[idx].v_type != VAR_NUMBER
814 && args[idx].v_type != VAR_DICT)
815 {
rbtnnddc80af2021-12-17 18:01:31 +0000816 semsg(_(e_string_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200817 return FAIL;
818 }
819 return OK;
820}
821
822/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200823 * Get the string value of a variable.
824 * If it is a Number variable, the number is converted into a string.
825 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
826 * tv_get_string_buf() uses a given buffer.
827 * If the String variable has never been set, return an empty string.
828 * Never returns NULL;
829 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
830 * NULL on error.
831 */
832 char_u *
833tv_get_string(typval_T *varp)
834{
835 static char_u mybuf[NUMBUFLEN];
836
837 return tv_get_string_buf(varp, mybuf);
838}
839
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100840/*
841 * Like tv_get_string() but don't allow number to string conversion for Vim9.
842 */
843 char_u *
844tv_get_string_strict(typval_T *varp)
845{
846 static char_u mybuf[NUMBUFLEN];
847 char_u *res = tv_get_string_buf_chk_strict(
848 varp, mybuf, in_vim9script());
849
850 return res != NULL ? res : (char_u *)"";
851}
852
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200853 char_u *
854tv_get_string_buf(typval_T *varp, char_u *buf)
855{
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200856 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200857
858 return res != NULL ? res : (char_u *)"";
859}
860
861/*
862 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
863 */
864 char_u *
865tv_get_string_chk(typval_T *varp)
866{
867 static char_u mybuf[NUMBUFLEN];
868
869 return tv_get_string_buf_chk(varp, mybuf);
870}
871
872 char_u *
873tv_get_string_buf_chk(typval_T *varp, char_u *buf)
874{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100875 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
876}
877
878 char_u *
879tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
880{
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200881 switch (varp->v_type)
882 {
883 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100884 if (strict)
885 {
886 emsg(_(e_using_number_as_string));
887 break;
888 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200889 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
890 (varnumber_T)varp->vval.v_number);
891 return buf;
892 case VAR_FUNC:
893 case VAR_PARTIAL:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200894 emsg(_("E729: Using a Funcref as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200895 break;
896 case VAR_LIST:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200897 emsg(_("E730: Using a List as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200898 break;
899 case VAR_DICT:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200900 emsg(_("E731: Using a Dictionary as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200901 break;
902 case VAR_FLOAT:
903#ifdef FEAT_FLOAT
Bram Moolenaar7a2217b2021-06-06 12:33:49 +0200904 if (strict)
905 {
906 emsg(_(e_float_as_string));
907 break;
908 }
909 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
910 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200911#endif
912 case VAR_STRING:
913 if (varp->vval.v_string != NULL)
914 return varp->vval.v_string;
915 return (char_u *)"";
916 case VAR_BOOL:
917 case VAR_SPECIAL:
918 STRCPY(buf, get_var_special_name(varp->vval.v_number));
919 return buf;
920 case VAR_BLOB:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200921 emsg(_("E976: Using a Blob as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200922 break;
923 case VAR_JOB:
924#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200925 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200926 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200927 semsg(_(e_using_invalid_value_as_string_str), "job");
928 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200929 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200930 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200931#endif
932 break;
933 case VAR_CHANNEL:
934#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200935 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200936 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200937 semsg(_(e_using_invalid_value_as_string_str), "channel");
938 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200939 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200940 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200941#endif
942 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200943 case VAR_VOID:
944 emsg(_(e_cannot_use_void_value));
945 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200946 case VAR_UNKNOWN:
947 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200948 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +0200949 semsg(_(e_using_invalid_value_as_string_str),
950 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200951 break;
952 }
953 return NULL;
954}
955
956/*
957 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
958 * string() on Dict, List, etc.
959 */
960 char_u *
961tv_stringify(typval_T *varp, char_u *buf)
962{
963 if (varp->v_type == VAR_LIST
964 || varp->v_type == VAR_DICT
965 || varp->v_type == VAR_BLOB
966 || varp->v_type == VAR_FUNC
967 || varp->v_type == VAR_PARTIAL
968 || varp->v_type == VAR_FLOAT)
969 {
970 typval_T tmp;
971
972 f_string(varp, &tmp);
973 tv_get_string_buf(&tmp, buf);
974 clear_tv(varp);
975 *varp = tmp;
976 return tmp.vval.v_string;
977 }
978 return tv_get_string_buf(varp, buf);
979}
980
981/*
982 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
983 * Also give an error message, using "name" or _("name") when use_gettext is
984 * TRUE.
985 */
986 int
987tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
988{
989 int lock = 0;
990
991 switch (tv->v_type)
992 {
993 case VAR_BLOB:
994 if (tv->vval.v_blob != NULL)
995 lock = tv->vval.v_blob->bv_lock;
996 break;
997 case VAR_LIST:
998 if (tv->vval.v_list != NULL)
999 lock = tv->vval.v_list->lv_lock;
1000 break;
1001 case VAR_DICT:
1002 if (tv->vval.v_dict != NULL)
1003 lock = tv->vval.v_dict->dv_lock;
1004 break;
1005 default:
1006 break;
1007 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001008 return value_check_lock(tv->v_lock, name, use_gettext)
1009 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001010}
1011
1012/*
1013 * Copy the values from typval_T "from" to typval_T "to".
1014 * When needed allocates string or increases reference count.
1015 * Does not make a copy of a list, blob or dict but copies the reference!
1016 * It is OK for "from" and "to" to point to the same item. This is used to
1017 * make a copy later.
1018 */
1019 void
1020copy_tv(typval_T *from, typval_T *to)
1021{
1022 to->v_type = from->v_type;
1023 to->v_lock = 0;
1024 switch (from->v_type)
1025 {
1026 case VAR_NUMBER:
1027 case VAR_BOOL:
1028 case VAR_SPECIAL:
1029 to->vval.v_number = from->vval.v_number;
1030 break;
1031 case VAR_FLOAT:
1032#ifdef FEAT_FLOAT
1033 to->vval.v_float = from->vval.v_float;
1034 break;
1035#endif
1036 case VAR_JOB:
1037#ifdef FEAT_JOB_CHANNEL
1038 to->vval.v_job = from->vval.v_job;
1039 if (to->vval.v_job != NULL)
1040 ++to->vval.v_job->jv_refcount;
1041 break;
1042#endif
1043 case VAR_CHANNEL:
1044#ifdef FEAT_JOB_CHANNEL
1045 to->vval.v_channel = from->vval.v_channel;
1046 if (to->vval.v_channel != NULL)
1047 ++to->vval.v_channel->ch_refcount;
1048 break;
1049#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001050 case VAR_INSTR:
1051 to->vval.v_instr = from->vval.v_instr;
1052 break;
1053
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001054 case VAR_STRING:
1055 case VAR_FUNC:
1056 if (from->vval.v_string == NULL)
1057 to->vval.v_string = NULL;
1058 else
1059 {
1060 to->vval.v_string = vim_strsave(from->vval.v_string);
1061 if (from->v_type == VAR_FUNC)
1062 func_ref(to->vval.v_string);
1063 }
1064 break;
1065 case VAR_PARTIAL:
1066 if (from->vval.v_partial == NULL)
1067 to->vval.v_partial = NULL;
1068 else
1069 {
1070 to->vval.v_partial = from->vval.v_partial;
1071 ++to->vval.v_partial->pt_refcount;
1072 }
1073 break;
1074 case VAR_BLOB:
1075 if (from->vval.v_blob == NULL)
1076 to->vval.v_blob = NULL;
1077 else
1078 {
1079 to->vval.v_blob = from->vval.v_blob;
1080 ++to->vval.v_blob->bv_refcount;
1081 }
1082 break;
1083 case VAR_LIST:
1084 if (from->vval.v_list == NULL)
1085 to->vval.v_list = NULL;
1086 else
1087 {
1088 to->vval.v_list = from->vval.v_list;
1089 ++to->vval.v_list->lv_refcount;
1090 }
1091 break;
1092 case VAR_DICT:
1093 if (from->vval.v_dict == NULL)
1094 to->vval.v_dict = NULL;
1095 else
1096 {
1097 to->vval.v_dict = from->vval.v_dict;
1098 ++to->vval.v_dict->dv_refcount;
1099 }
1100 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +02001101 case VAR_VOID:
1102 emsg(_(e_cannot_use_void_value));
1103 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001104 case VAR_UNKNOWN:
1105 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001106 internal_error_no_abort("copy_tv(UNKNOWN)");
1107 break;
1108 }
1109}
1110
1111/*
1112 * Compare "typ1" and "typ2". Put the result in "typ1".
1113 */
1114 int
1115typval_compare(
1116 typval_T *typ1, // first operand
1117 typval_T *typ2, // second operand
Bram Moolenaar657137c2021-01-09 15:45:23 +01001118 exprtype_T type, // operator
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001119 int ic) // ignore case
1120{
1121 int i;
1122 varnumber_T n1, n2;
1123 char_u *s1, *s2;
1124 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1125 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1126
1127 if (type_is && typ1->v_type != typ2->v_type)
1128 {
1129 // For "is" a different type always means FALSE, for "notis"
1130 // it means TRUE.
1131 n1 = (type == EXPR_ISNOT);
1132 }
1133 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
1134 {
1135 if (type_is)
1136 {
1137 n1 = (typ1->v_type == typ2->v_type
1138 && typ1->vval.v_blob == typ2->vval.v_blob);
1139 if (type == EXPR_ISNOT)
1140 n1 = !n1;
1141 }
1142 else if (typ1->v_type != typ2->v_type
1143 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1144 {
1145 if (typ1->v_type != typ2->v_type)
1146 emsg(_("E977: Can only compare Blob with Blob"));
1147 else
1148 emsg(_(e_invalblob));
1149 clear_tv(typ1);
1150 return FAIL;
1151 }
1152 else
1153 {
1154 // Compare two Blobs for being equal or unequal.
1155 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
1156 if (type == EXPR_NEQUAL)
1157 n1 = !n1;
1158 }
1159 }
1160 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
1161 {
1162 if (type_is)
1163 {
1164 n1 = (typ1->v_type == typ2->v_type
1165 && typ1->vval.v_list == typ2->vval.v_list);
1166 if (type == EXPR_ISNOT)
1167 n1 = !n1;
1168 }
1169 else if (typ1->v_type != typ2->v_type
1170 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1171 {
1172 if (typ1->v_type != typ2->v_type)
1173 emsg(_("E691: Can only compare List with List"));
1174 else
1175 emsg(_("E692: Invalid operation for List"));
1176 clear_tv(typ1);
1177 return FAIL;
1178 }
1179 else
1180 {
1181 // Compare two Lists for being equal or unequal.
1182 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
1183 ic, FALSE);
1184 if (type == EXPR_NEQUAL)
1185 n1 = !n1;
1186 }
1187 }
1188
1189 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
1190 {
1191 if (type_is)
1192 {
1193 n1 = (typ1->v_type == typ2->v_type
1194 && typ1->vval.v_dict == typ2->vval.v_dict);
1195 if (type == EXPR_ISNOT)
1196 n1 = !n1;
1197 }
1198 else if (typ1->v_type != typ2->v_type
1199 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1200 {
1201 if (typ1->v_type != typ2->v_type)
1202 emsg(_("E735: Can only compare Dictionary with Dictionary"));
1203 else
1204 emsg(_("E736: Invalid operation for Dictionary"));
1205 clear_tv(typ1);
1206 return FAIL;
1207 }
1208 else
1209 {
1210 // Compare two Dictionaries for being equal or unequal.
1211 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
1212 ic, FALSE);
1213 if (type == EXPR_NEQUAL)
1214 n1 = !n1;
1215 }
1216 }
1217
1218 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
1219 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
1220 {
1221 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1222 && type != EXPR_IS && type != EXPR_ISNOT)
1223 {
1224 emsg(_("E694: Invalid operation for Funcrefs"));
1225 clear_tv(typ1);
1226 return FAIL;
1227 }
1228 if ((typ1->v_type == VAR_PARTIAL
1229 && typ1->vval.v_partial == NULL)
1230 || (typ2->v_type == VAR_PARTIAL
1231 && typ2->vval.v_partial == NULL))
1232 // When both partials are NULL, then they are equal.
1233 // Otherwise they are not equal.
1234 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
1235 else if (type_is)
1236 {
1237 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
1238 // strings are considered the same if their value is
1239 // the same
1240 n1 = tv_equal(typ1, typ2, ic, FALSE);
1241 else if (typ1->v_type == VAR_PARTIAL
1242 && typ2->v_type == VAR_PARTIAL)
1243 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
1244 else
1245 n1 = FALSE;
1246 }
1247 else
1248 n1 = tv_equal(typ1, typ2, ic, FALSE);
1249 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1250 n1 = !n1;
1251 }
1252
1253#ifdef FEAT_FLOAT
1254 // If one of the two variables is a float, compare as a float.
1255 // When using "=~" or "!~", always compare as string.
1256 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
1257 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1258 {
1259 float_T f1, f2;
1260
1261 f1 = tv_get_float(typ1);
1262 f2 = tv_get_float(typ2);
1263 n1 = FALSE;
1264 switch (type)
1265 {
1266 case EXPR_IS:
1267 case EXPR_EQUAL: n1 = (f1 == f2); break;
1268 case EXPR_ISNOT:
1269 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1270 case EXPR_GREATER: n1 = (f1 > f2); break;
1271 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1272 case EXPR_SMALLER: n1 = (f1 < f2); break;
1273 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1274 case EXPR_UNKNOWN:
1275 case EXPR_MATCH:
1276 default: break; // avoid gcc warning
1277 }
1278 }
1279#endif
1280
1281 // If one of the two variables is a number, compare as a number.
1282 // When using "=~" or "!~", always compare as string.
1283 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
1284 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1285 {
1286 n1 = tv_get_number(typ1);
1287 n2 = tv_get_number(typ2);
1288 switch (type)
1289 {
1290 case EXPR_IS:
1291 case EXPR_EQUAL: n1 = (n1 == n2); break;
1292 case EXPR_ISNOT:
1293 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1294 case EXPR_GREATER: n1 = (n1 > n2); break;
1295 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1296 case EXPR_SMALLER: n1 = (n1 < n2); break;
1297 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1298 case EXPR_UNKNOWN:
1299 case EXPR_MATCH:
1300 default: break; // avoid gcc warning
1301 }
1302 }
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001303 else if (in_vim9script() && (typ1->v_type == VAR_BOOL
Bram Moolenaar0c357522021-07-18 14:43:43 +02001304 || typ2->v_type == VAR_BOOL
1305 || (typ1->v_type == VAR_SPECIAL
1306 && typ2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001307 {
1308 if (typ1->v_type != typ2->v_type)
1309 {
1310 semsg(_(e_cannot_compare_str_with_str),
1311 vartype_name(typ1->v_type), vartype_name(typ2->v_type));
1312 clear_tv(typ1);
1313 return FAIL;
1314 }
1315 n1 = typ1->vval.v_number;
1316 n2 = typ2->vval.v_number;
1317 switch (type)
1318 {
1319 case EXPR_IS:
1320 case EXPR_EQUAL: n1 = (n1 == n2); break;
1321 case EXPR_ISNOT:
1322 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1323 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001324 semsg(_(e_invalid_operation_for_str),
1325 vartype_name(typ1->v_type));
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001326 clear_tv(typ1);
1327 return FAIL;
1328 }
1329 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001330 else
1331 {
Bram Moolenaar0c357522021-07-18 14:43:43 +02001332 if (in_vim9script()
1333 && ((typ1->v_type != VAR_STRING && typ1->v_type != VAR_SPECIAL)
1334 || (typ2->v_type != VAR_STRING && typ2->v_type != VAR_SPECIAL)))
1335 {
1336 semsg(_(e_cannot_compare_str_with_str),
1337 vartype_name(typ1->v_type), vartype_name(typ2->v_type));
1338 clear_tv(typ1);
1339 return FAIL;
1340 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001341 s1 = tv_get_string_buf(typ1, buf1);
1342 s2 = tv_get_string_buf(typ2, buf2);
1343 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1344 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1345 else
1346 i = 0;
1347 n1 = FALSE;
1348 switch (type)
1349 {
1350 case EXPR_IS:
1351 case EXPR_EQUAL: n1 = (i == 0); break;
1352 case EXPR_ISNOT:
1353 case EXPR_NEQUAL: n1 = (i != 0); break;
1354 case EXPR_GREATER: n1 = (i > 0); break;
1355 case EXPR_GEQUAL: n1 = (i >= 0); break;
1356 case EXPR_SMALLER: n1 = (i < 0); break;
1357 case EXPR_SEQUAL: n1 = (i <= 0); break;
1358
1359 case EXPR_MATCH:
1360 case EXPR_NOMATCH:
1361 n1 = pattern_match(s2, s1, ic);
1362 if (type == EXPR_NOMATCH)
1363 n1 = !n1;
1364 break;
1365
1366 default: break; // avoid gcc warning
1367 }
1368 }
1369 clear_tv(typ1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001370 if (in_vim9script())
1371 {
1372 typ1->v_type = VAR_BOOL;
1373 typ1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
1374 }
1375 else
1376 {
1377 typ1->v_type = VAR_NUMBER;
1378 typ1->vval.v_number = n1;
1379 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001380
1381 return OK;
1382}
1383
Bram Moolenaar34453202021-01-31 13:08:38 +01001384/*
1385 * Convert any type to a string, never give an error.
1386 * When "quotes" is TRUE add quotes to a string.
1387 * Returns an allocated string.
1388 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001389 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001390typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001391{
1392 char_u *tofree;
1393 char_u numbuf[NUMBUFLEN];
1394 char_u *ret = NULL;
1395
1396 if (arg == NULL)
1397 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001398 if (!quotes && arg->v_type == VAR_STRING)
1399 {
1400 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1401 : arg->vval.v_string);
1402 }
1403 else
1404 {
1405 ret = tv2string(arg, &tofree, numbuf, 0);
1406 // Make a copy if we have a value but it's not in allocated memory.
1407 if (ret != NULL && tofree == NULL)
1408 ret = vim_strsave(ret);
1409 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001410 return ret;
1411}
1412
1413/*
1414 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1415 * or it refers to a List or Dictionary that is locked.
1416 */
1417 int
1418tv_islocked(typval_T *tv)
1419{
1420 return (tv->v_lock & VAR_LOCKED)
1421 || (tv->v_type == VAR_LIST
1422 && tv->vval.v_list != NULL
1423 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1424 || (tv->v_type == VAR_DICT
1425 && tv->vval.v_dict != NULL
1426 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1427}
1428
1429 static int
1430func_equal(
1431 typval_T *tv1,
1432 typval_T *tv2,
1433 int ic) // ignore case
1434{
1435 char_u *s1, *s2;
1436 dict_T *d1, *d2;
1437 int a1, a2;
1438 int i;
1439
1440 // empty and NULL function name considered the same
1441 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1442 : partial_name(tv1->vval.v_partial);
1443 if (s1 != NULL && *s1 == NUL)
1444 s1 = NULL;
1445 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1446 : partial_name(tv2->vval.v_partial);
1447 if (s2 != NULL && *s2 == NUL)
1448 s2 = NULL;
1449 if (s1 == NULL || s2 == NULL)
1450 {
1451 if (s1 != s2)
1452 return FALSE;
1453 }
1454 else if (STRCMP(s1, s2) != 0)
1455 return FALSE;
1456
1457 // empty dict and NULL dict is different
1458 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1459 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1460 if (d1 == NULL || d2 == NULL)
1461 {
1462 if (d1 != d2)
1463 return FALSE;
1464 }
1465 else if (!dict_equal(d1, d2, ic, TRUE))
1466 return FALSE;
1467
1468 // empty list and no list considered the same
1469 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1470 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1471 if (a1 != a2)
1472 return FALSE;
1473 for (i = 0; i < a1; ++i)
1474 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1475 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1476 return FALSE;
1477
1478 return TRUE;
1479}
1480
1481/*
1482 * Return TRUE if "tv1" and "tv2" have the same value.
1483 * Compares the items just like "==" would compare them, but strings and
1484 * numbers are different. Floats and numbers are also different.
1485 */
1486 int
1487tv_equal(
1488 typval_T *tv1,
1489 typval_T *tv2,
1490 int ic, // ignore case
1491 int recursive) // TRUE when used recursively
1492{
1493 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1494 char_u *s1, *s2;
1495 static int recursive_cnt = 0; // catch recursive loops
1496 int r;
1497 static int tv_equal_recurse_limit;
1498
1499 // Catch lists and dicts that have an endless loop by limiting
1500 // recursiveness to a limit. We guess they are equal then.
1501 // A fixed limit has the problem of still taking an awful long time.
1502 // Reduce the limit every time running into it. That should work fine for
1503 // deeply linked structures that are not recursively linked and catch
1504 // recursiveness quickly.
1505 if (!recursive)
1506 tv_equal_recurse_limit = 1000;
1507 if (recursive_cnt >= tv_equal_recurse_limit)
1508 {
1509 --tv_equal_recurse_limit;
1510 return TRUE;
1511 }
1512
1513 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1514 // arguments.
1515 if ((tv1->v_type == VAR_FUNC
1516 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1517 && (tv2->v_type == VAR_FUNC
1518 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1519 {
1520 ++recursive_cnt;
1521 r = func_equal(tv1, tv2, ic);
1522 --recursive_cnt;
1523 return r;
1524 }
1525
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001526 if (tv1->v_type != tv2->v_type
1527 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1528 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001529 return FALSE;
1530
1531 switch (tv1->v_type)
1532 {
1533 case VAR_LIST:
1534 ++recursive_cnt;
1535 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1536 --recursive_cnt;
1537 return r;
1538
1539 case VAR_DICT:
1540 ++recursive_cnt;
1541 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1542 --recursive_cnt;
1543 return r;
1544
1545 case VAR_BLOB:
1546 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1547
1548 case VAR_NUMBER:
1549 case VAR_BOOL:
1550 case VAR_SPECIAL:
1551 return tv1->vval.v_number == tv2->vval.v_number;
1552
1553 case VAR_STRING:
1554 s1 = tv_get_string_buf(tv1, buf1);
1555 s2 = tv_get_string_buf(tv2, buf2);
1556 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1557
1558 case VAR_FLOAT:
1559#ifdef FEAT_FLOAT
1560 return tv1->vval.v_float == tv2->vval.v_float;
1561#endif
1562 case VAR_JOB:
1563#ifdef FEAT_JOB_CHANNEL
1564 return tv1->vval.v_job == tv2->vval.v_job;
1565#endif
1566 case VAR_CHANNEL:
1567#ifdef FEAT_JOB_CHANNEL
1568 return tv1->vval.v_channel == tv2->vval.v_channel;
1569#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001570 case VAR_INSTR:
1571 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001572
1573 case VAR_PARTIAL:
1574 return tv1->vval.v_partial == tv2->vval.v_partial;
1575
1576 case VAR_FUNC:
1577 return tv1->vval.v_string == tv2->vval.v_string;
1578
1579 case VAR_UNKNOWN:
1580 case VAR_ANY:
1581 case VAR_VOID:
1582 break;
1583 }
1584
1585 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1586 // does not equal anything, not even itself.
1587 return FALSE;
1588}
1589
1590/*
1591 * Get an option value.
1592 * "arg" points to the '&' or '+' before the option name.
1593 * "arg" is advanced to character after the option name.
1594 * Return OK or FAIL.
1595 */
1596 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001597eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001598 char_u **arg,
1599 typval_T *rettv, // when NULL, only check if option exists
1600 int evaluate)
1601{
1602 char_u *option_end;
1603 long numval;
1604 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001605 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001606 int c;
1607 int working = (**arg == '+'); // has("+option")
1608 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001609 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001610
1611 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001612 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001613 if (option_end == NULL)
1614 {
1615 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001616 semsg(_(e_option_name_missing_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001617 return FAIL;
1618 }
1619
1620 if (!evaluate)
1621 {
1622 *arg = option_end;
1623 return OK;
1624 }
1625
1626 c = *option_end;
1627 *option_end = NUL;
1628 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001629 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001630
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001631 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001632 {
1633 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001634 semsg(_(e_unknown_option_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001635 ret = FAIL;
1636 }
1637 else if (rettv != NULL)
1638 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001639 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001640 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001641 {
1642 rettv->v_type = VAR_STRING;
1643 rettv->vval.v_string = NULL;
1644 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001645 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001646 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001647 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1648 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001649 rettv->vval.v_number = 0;
1650 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001651 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001652 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001653 if (in_vim9script() && opt_type == gov_bool)
1654 {
1655 rettv->v_type = VAR_BOOL;
1656 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1657 }
1658 else
1659 {
1660 rettv->v_type = VAR_NUMBER;
1661 rettv->vval.v_number = numval;
1662 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001663 }
1664 else // string option
1665 {
1666 rettv->v_type = VAR_STRING;
1667 rettv->vval.v_string = stringval;
1668 }
1669 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001670 else if (working && (opt_type == gov_hidden_bool
1671 || opt_type == gov_hidden_number
1672 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001673 ret = FAIL;
1674
1675 *option_end = c; // put back for error messages
1676 *arg = option_end;
1677
1678 return ret;
1679}
1680
1681/*
1682 * Allocate a variable for a number constant. Also deals with "0z" for blob.
1683 * Return OK or FAIL.
1684 */
1685 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001686eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001687 char_u **arg,
1688 typval_T *rettv,
1689 int evaluate,
1690 int want_string UNUSED)
1691{
1692 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02001693 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001694#ifdef FEAT_FLOAT
1695 char_u *p;
1696 int get_float = FALSE;
1697
1698 // We accept a float when the format matches
1699 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
1700 // strict to avoid backwards compatibility problems.
1701 // With script version 2 and later the leading digit can be
1702 // omitted.
1703 // Don't look for a float after the "." operator, so that
1704 // ":let vers = 1.2.3" doesn't fail.
1705 if (**arg == '.')
1706 p = *arg;
1707 else
Bram Moolenaar29500652021-08-08 15:43:34 +02001708 {
1709 p = *arg + 1;
1710 if (skip_quotes)
1711 for (;;)
1712 {
1713 if (*p == '\'')
1714 ++p;
1715 if (!vim_isdigit(*p))
1716 break;
1717 p = skipdigits(p);
1718 }
1719 else
1720 p = skipdigits(p);
1721 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001722 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
1723 {
1724 get_float = TRUE;
1725 p = skipdigits(p + 2);
1726 if (*p == 'e' || *p == 'E')
1727 {
1728 ++p;
1729 if (*p == '-' || *p == '+')
1730 ++p;
1731 if (!vim_isdigit(*p))
1732 get_float = FALSE;
1733 else
1734 p = skipdigits(p + 1);
1735 }
1736 if (ASCII_ISALPHA(*p) || *p == '.')
1737 get_float = FALSE;
1738 }
1739 if (get_float)
1740 {
1741 float_T f;
1742
Bram Moolenaar29500652021-08-08 15:43:34 +02001743 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001744 if (evaluate)
1745 {
1746 rettv->v_type = VAR_FLOAT;
1747 rettv->vval.v_float = f;
1748 }
1749 }
1750 else
1751#endif
1752 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
1753 {
1754 char_u *bp;
1755 blob_T *blob = NULL; // init for gcc
1756
1757 // Blob constant: 0z0123456789abcdef
1758 if (evaluate)
1759 blob = blob_alloc();
1760 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
1761 {
1762 if (!vim_isxdigit(bp[1]))
1763 {
1764 if (blob != NULL)
1765 {
1766 emsg(_("E973: Blob literal should have an even number of hex characters"));
1767 ga_clear(&blob->bv_ga);
1768 VIM_CLEAR(blob);
1769 }
1770 return FAIL;
1771 }
1772 if (blob != NULL)
1773 ga_append(&blob->bv_ga,
1774 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
1775 if (bp[2] == '.' && vim_isxdigit(bp[3]))
1776 ++bp;
1777 }
1778 if (blob != NULL)
1779 rettv_blob_set(rettv, blob);
1780 *arg = bp;
1781 }
1782 else
1783 {
1784 varnumber_T n;
1785
1786 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02001787 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001788 ? STR2NR_NO_OCT + STR2NR_QUOTE
1789 : STR2NR_ALL, &n, NULL, 0, TRUE);
1790 if (len == 0)
1791 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00001792 if (evaluate)
1793 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001794 return FAIL;
1795 }
1796 *arg += len;
1797 if (evaluate)
1798 {
1799 rettv->v_type = VAR_NUMBER;
1800 rettv->vval.v_number = n;
1801 }
1802 }
1803 return OK;
1804}
1805
1806/*
1807 * Allocate a variable for a string constant.
1808 * Return OK or FAIL.
1809 */
1810 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001811eval_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001812{
1813 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001814 char_u *end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001815 int extra = 0;
1816 int len;
1817
1818 // Find the end of the string, skipping backslashed characters.
1819 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
1820 {
1821 if (*p == '\\' && p[1] != NUL)
1822 {
1823 ++p;
1824 // A "\<x>" form occupies at least 4 characters, and produces up
1825 // to 21 characters (3 * 6 for the char and 3 for a modifier):
1826 // reserve space for 18 extra.
1827 // Each byte in the char could be encoded as K_SPECIAL K_EXTRA x.
1828 if (*p == '<')
1829 extra += 18;
1830 }
1831 }
1832
1833 if (*p != '"')
1834 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001835 semsg(_(e_missing_double_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001836 return FAIL;
1837 }
1838
1839 // If only parsing, set *arg and return here
1840 if (!evaluate)
1841 {
1842 *arg = p + 1;
1843 return OK;
1844 }
1845
1846 // Copy the string into allocated memory, handling backslashed
1847 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001848 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001849 len = (int)(p - *arg + extra);
1850 rettv->vval.v_string = alloc(len);
1851 if (rettv->vval.v_string == NULL)
1852 return FAIL;
1853 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001854
1855 for (p = *arg + 1; *p != NUL && *p != '"'; )
1856 {
1857 if (*p == '\\')
1858 {
1859 switch (*++p)
1860 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001861 case 'b': *end++ = BS; ++p; break;
1862 case 'e': *end++ = ESC; ++p; break;
1863 case 'f': *end++ = FF; ++p; break;
1864 case 'n': *end++ = NL; ++p; break;
1865 case 'r': *end++ = CAR; ++p; break;
1866 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001867
1868 case 'X': // hex: "\x1", "\x12"
1869 case 'x':
1870 case 'u': // Unicode: "\u0023"
1871 case 'U':
1872 if (vim_isxdigit(p[1]))
1873 {
1874 int n, nr;
1875 int c = toupper(*p);
1876
1877 if (c == 'X')
1878 n = 2;
1879 else if (*p == 'u')
1880 n = 4;
1881 else
1882 n = 8;
1883 nr = 0;
1884 while (--n >= 0 && vim_isxdigit(p[1]))
1885 {
1886 ++p;
1887 nr = (nr << 4) + hex2nr(*p);
1888 }
1889 ++p;
1890 // For "\u" store the number according to
1891 // 'encoding'.
1892 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001893 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001894 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001895 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001896 }
1897 break;
1898
1899 // octal: "\1", "\12", "\123"
1900 case '0':
1901 case '1':
1902 case '2':
1903 case '3':
1904 case '4':
1905 case '5':
1906 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001907 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001908 if (*p >= '0' && *p <= '7')
1909 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001910 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001911 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001912 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001913 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001914 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001915 break;
1916
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001917 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001918 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001919 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001920 int flags = FSK_KEYCODE | FSK_IN_STRING;
1921
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001922 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001923 flags |= FSK_SIMPLIFY;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001924 extra = trans_special(&p, end, flags, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001925 if (extra != 0)
1926 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001927 end += extra;
1928 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001929 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001930 break;
1931 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001932 }
1933 // FALLTHROUGH
1934
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001935 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001936 break;
1937 }
1938 }
1939 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001940 MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001941 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001942 *end = NUL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001943 if (*p != NUL) // just in case
1944 ++p;
1945 *arg = p;
1946
1947 return OK;
1948}
1949
1950/*
1951 * Allocate a variable for a 'str''ing' constant.
1952 * Return OK or FAIL.
1953 */
1954 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001955eval_lit_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001956{
1957 char_u *p;
1958 char_u *str;
1959 int reduce = 0;
1960
1961 // Find the end of the string, skipping ''.
1962 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
1963 {
1964 if (*p == '\'')
1965 {
1966 if (p[1] != '\'')
1967 break;
1968 ++reduce;
1969 ++p;
1970 }
1971 }
1972
1973 if (*p != '\'')
1974 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001975 semsg(_(e_missing_single_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001976 return FAIL;
1977 }
1978
1979 // If only parsing return after setting "*arg"
1980 if (!evaluate)
1981 {
1982 *arg = p + 1;
1983 return OK;
1984 }
1985
1986 // Copy the string into allocated memory, handling '' to ' reduction.
1987 str = alloc((p - *arg) - reduce);
1988 if (str == NULL)
1989 return FAIL;
1990 rettv->v_type = VAR_STRING;
1991 rettv->vval.v_string = str;
1992
1993 for (p = *arg + 1; *p != NUL; )
1994 {
1995 if (*p == '\'')
1996 {
1997 if (p[1] != '\'')
1998 break;
1999 ++p;
2000 }
2001 MB_COPY_CHAR(p, str);
2002 }
2003 *str = NUL;
2004 *arg = p + 1;
2005
2006 return OK;
2007}
2008
2009/*
2010 * Return a string with the string representation of a variable.
2011 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2012 * "numbuf" is used for a number.
2013 * Puts quotes around strings, so that they can be parsed back by eval().
2014 * May return NULL.
2015 */
2016 char_u *
2017tv2string(
2018 typval_T *tv,
2019 char_u **tofree,
2020 char_u *numbuf,
2021 int copyID)
2022{
2023 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2024}
2025
2026/*
2027 * Get the value of an environment variable.
2028 * "arg" is pointing to the '$'. It is advanced to after the name.
2029 * If the environment variable was not set, silently assume it is empty.
2030 * Return FAIL if the name is invalid.
2031 */
2032 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002033eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002034{
2035 char_u *string = NULL;
2036 int len;
2037 int cc;
2038 char_u *name;
2039 int mustfree = FALSE;
2040
2041 ++*arg;
2042 name = *arg;
2043 len = get_env_len(arg);
2044 if (evaluate)
2045 {
2046 if (len == 0)
2047 return FAIL; // invalid empty name
2048
2049 cc = name[len];
2050 name[len] = NUL;
2051 // first try vim_getenv(), fast for normal environment vars
2052 string = vim_getenv(name, &mustfree);
2053 if (string != NULL && *string != NUL)
2054 {
2055 if (!mustfree)
2056 string = vim_strsave(string);
2057 }
2058 else
2059 {
2060 if (mustfree)
2061 vim_free(string);
2062
2063 // next try expanding things like $VIM and ${HOME}
2064 string = expand_env_save(name - 1);
2065 if (string != NULL && *string == '$')
2066 VIM_CLEAR(string);
2067 }
2068 name[len] = cc;
2069
2070 rettv->v_type = VAR_STRING;
2071 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002072 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002073 }
2074
2075 return OK;
2076}
2077
2078/*
2079 * Get the lnum from the first argument.
2080 * Also accepts ".", "$", etc., but that only works for the current buffer.
2081 * Returns -1 on error.
2082 */
2083 linenr_T
2084tv_get_lnum(typval_T *argvars)
2085{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002086 linenr_T lnum = -1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002087
Bram Moolenaar56acb092020-08-16 14:48:19 +02002088 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2089 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002090 if (lnum <= 0 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002091 {
2092 int fnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002093 pos_T *fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002094
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002095 // no valid number, try using arg like line()
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002096 if (fp != NULL)
2097 lnum = fp->lnum;
2098 }
2099 return lnum;
2100}
2101
2102/*
2103 * Get the lnum from the first argument.
2104 * Also accepts "$", then "buf" is used.
2105 * Returns 0 on error.
2106 */
2107 linenr_T
2108tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2109{
2110 if (argvars[0].v_type == VAR_STRING
2111 && argvars[0].vval.v_string != NULL
2112 && argvars[0].vval.v_string[0] == '$'
2113 && buf != NULL)
2114 return buf->b_ml.ml_line_count;
2115 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2116}
2117
2118/*
2119 * Get buffer by number or pattern.
2120 */
2121 buf_T *
2122tv_get_buf(typval_T *tv, int curtab_only)
2123{
2124 char_u *name = tv->vval.v_string;
2125 buf_T *buf;
2126
2127 if (tv->v_type == VAR_NUMBER)
2128 return buflist_findnr((int)tv->vval.v_number);
2129 if (tv->v_type != VAR_STRING)
2130 return NULL;
2131 if (name == NULL || *name == NUL)
2132 return curbuf;
2133 if (name[0] == '$' && name[1] == NUL)
2134 return lastbuf;
2135
2136 buf = buflist_find_by_name(name, curtab_only);
2137
2138 // If not found, try expanding the name, like done for bufexists().
2139 if (buf == NULL)
2140 buf = find_buffer(tv);
2141
2142 return buf;
2143}
2144
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002145/*
2146 * Like tv_get_buf() but give an error message is the type is wrong.
2147 */
2148 buf_T *
2149tv_get_buf_from_arg(typval_T *tv)
2150{
2151 buf_T *buf;
2152
2153 ++emsg_off;
2154 buf = tv_get_buf(tv, FALSE);
2155 --emsg_off;
2156 if (buf == NULL
2157 && tv->v_type != VAR_NUMBER
2158 && tv->v_type != VAR_STRING)
2159 // issue errmsg for type error
2160 (void)tv_get_number(tv);
2161 return buf;
2162}
2163
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002164#endif // FEAT_EVAL