blob: 7716873bbb9b00444676d37ce9171f147b588734 [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/*
rbtnn0ccb5842021-12-18 18:33:46 +0000666 * Give an error and return FAIL unless "args[idx]" is a string, a list or a
667 * blob.
668 */
669 int
670check_for_string_or_list_or_blob_arg(typval_T *args, int idx)
671{
672 if (args[idx].v_type != VAR_STRING
673 && args[idx].v_type != VAR_LIST
674 && args[idx].v_type != VAR_BLOB)
675 {
676 semsg(_(e_string_list_or_blob_required_for_argument_nr), idx + 1);
677 return FAIL;
678 }
679 return OK;
680}
681
682/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200683 * Check for an optional string or list argument at 'idx'
684 */
685 int
686check_for_opt_string_or_list_arg(typval_T *args, int idx)
687{
688 return (args[idx].v_type == VAR_UNKNOWN
689 || check_for_string_or_list_arg(args, idx));
690}
691
692/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200693 * Give an error and return FAIL unless "args[idx]" is a string or a dict.
694 */
695 int
696check_for_string_or_dict_arg(typval_T *args, int idx)
697{
698 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_DICT)
699 {
rbtnnddc80af2021-12-17 18:01:31 +0000700 semsg(_(e_string_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200701 return FAIL;
702 }
703 return OK;
704}
705
706/*
707 * Give an error and return FAIL unless "args[idx]" is a string or a number
708 * or a list.
709 */
710 int
711check_for_string_or_number_or_list_arg(typval_T *args, int idx)
712{
713 if (args[idx].v_type != VAR_STRING
714 && args[idx].v_type != VAR_NUMBER
715 && args[idx].v_type != VAR_LIST)
716 {
rbtnn0ccb5842021-12-18 18:33:46 +0000717 semsg(_(e_string_number_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200718 return FAIL;
719 }
720 return OK;
721}
722
723/*
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200724 * Give an error and return FAIL unless "args[idx]" is an optional string
725 * or number or a list
726 */
727 int
728check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx)
729{
730 return (args[idx].v_type == VAR_UNKNOWN
731 || check_for_string_or_number_or_list_arg(args, idx) != FAIL);
732}
733
734/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200735 * Give an error and return FAIL unless "args[idx]" is a string or a list
736 * or a dict.
737 */
738 int
739check_for_string_or_list_or_dict_arg(typval_T *args, int idx)
740{
741 if (args[idx].v_type != VAR_STRING
742 && args[idx].v_type != VAR_LIST
743 && args[idx].v_type != VAR_DICT)
744 {
rbtnnddc80af2021-12-17 18:01:31 +0000745 semsg(_(e_string_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200746 return FAIL;
747 }
748 return OK;
749}
750
751/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200752 * Give an error and return FAIL unless "args[idx]" is a list or a blob.
753 */
754 int
755check_for_list_or_blob_arg(typval_T *args, int idx)
756{
757 if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
758 {
rbtnn0ccb5842021-12-18 18:33:46 +0000759 semsg(_(e_list_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200760 return FAIL;
761 }
762 return OK;
763}
764
765/*
766 * Give an error and return FAIL unless "args[idx]" is a list or dict
767 */
768 int
769check_for_list_or_dict_arg(typval_T *args, int idx)
770{
771 if (args[idx].v_type != VAR_LIST
772 && args[idx].v_type != VAR_DICT)
773 {
rbtnnddc80af2021-12-17 18:01:31 +0000774 semsg(_(e_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200775 return FAIL;
776 }
777 return OK;
778}
779
780/*
781 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
782 * blob.
783 */
784 int
785check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
786{
787 if (args[idx].v_type != VAR_LIST
788 && args[idx].v_type != VAR_DICT
789 && args[idx].v_type != VAR_BLOB)
790 {
rbtnnddc80af2021-12-17 18:01:31 +0000791 semsg(_(e_list_dict_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200792 return FAIL;
793 }
794 return OK;
795}
796
797/*
Bram Moolenaar2d877592021-12-16 08:21:09 +0000798 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
799 * blob or a string.
800 */
801 int
802check_for_list_or_dict_or_blob_or_string_arg(typval_T *args, int idx)
803{
804 if (args[idx].v_type != VAR_LIST
805 && args[idx].v_type != VAR_DICT
806 && args[idx].v_type != VAR_BLOB
807 && args[idx].v_type != VAR_STRING)
808 {
rbtnnddc80af2021-12-17 18:01:31 +0000809 semsg(_(e_list_dict_blob_or_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2d877592021-12-16 08:21:09 +0000810 return FAIL;
811 }
812 return OK;
813}
814
815/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200816 * Give an error and return FAIL unless "args[idx]" is an optional buffer
817 * number or a dict.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200818 */
819 int
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200820check_for_opt_buffer_or_dict_arg(typval_T *args, int idx)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200821{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200822 if (args[idx].v_type != VAR_UNKNOWN
823 && args[idx].v_type != VAR_STRING
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200824 && args[idx].v_type != VAR_NUMBER
825 && args[idx].v_type != VAR_DICT)
826 {
rbtnnddc80af2021-12-17 18:01:31 +0000827 semsg(_(e_string_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200828 return FAIL;
829 }
830 return OK;
831}
832
833/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200834 * Get the string value of a variable.
835 * If it is a Number variable, the number is converted into a string.
836 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
837 * tv_get_string_buf() uses a given buffer.
838 * If the String variable has never been set, return an empty string.
839 * Never returns NULL;
840 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
841 * NULL on error.
842 */
843 char_u *
844tv_get_string(typval_T *varp)
845{
846 static char_u mybuf[NUMBUFLEN];
847
848 return tv_get_string_buf(varp, mybuf);
849}
850
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100851/*
852 * Like tv_get_string() but don't allow number to string conversion for Vim9.
853 */
854 char_u *
855tv_get_string_strict(typval_T *varp)
856{
857 static char_u mybuf[NUMBUFLEN];
858 char_u *res = tv_get_string_buf_chk_strict(
859 varp, mybuf, in_vim9script());
860
861 return res != NULL ? res : (char_u *)"";
862}
863
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200864 char_u *
865tv_get_string_buf(typval_T *varp, char_u *buf)
866{
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200867 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200868
869 return res != NULL ? res : (char_u *)"";
870}
871
872/*
873 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
874 */
875 char_u *
876tv_get_string_chk(typval_T *varp)
877{
878 static char_u mybuf[NUMBUFLEN];
879
880 return tv_get_string_buf_chk(varp, mybuf);
881}
882
883 char_u *
884tv_get_string_buf_chk(typval_T *varp, char_u *buf)
885{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100886 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
887}
888
889 char_u *
890tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
891{
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200892 switch (varp->v_type)
893 {
894 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100895 if (strict)
896 {
897 emsg(_(e_using_number_as_string));
898 break;
899 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200900 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
901 (varnumber_T)varp->vval.v_number);
902 return buf;
903 case VAR_FUNC:
904 case VAR_PARTIAL:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200905 emsg(_("E729: Using a Funcref as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200906 break;
907 case VAR_LIST:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200908 emsg(_("E730: Using a List as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200909 break;
910 case VAR_DICT:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200911 emsg(_("E731: Using a Dictionary as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200912 break;
913 case VAR_FLOAT:
914#ifdef FEAT_FLOAT
Bram Moolenaar7a2217b2021-06-06 12:33:49 +0200915 if (strict)
916 {
917 emsg(_(e_float_as_string));
918 break;
919 }
920 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
921 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200922#endif
923 case VAR_STRING:
924 if (varp->vval.v_string != NULL)
925 return varp->vval.v_string;
926 return (char_u *)"";
927 case VAR_BOOL:
928 case VAR_SPECIAL:
929 STRCPY(buf, get_var_special_name(varp->vval.v_number));
930 return buf;
931 case VAR_BLOB:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200932 emsg(_("E976: Using a Blob as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200933 break;
934 case VAR_JOB:
935#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200936 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200937 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200938 semsg(_(e_using_invalid_value_as_string_str), "job");
939 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200940 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200941 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200942#endif
943 break;
944 case VAR_CHANNEL:
945#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200946 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200947 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200948 semsg(_(e_using_invalid_value_as_string_str), "channel");
949 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200950 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200951 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200952#endif
953 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200954 case VAR_VOID:
955 emsg(_(e_cannot_use_void_value));
956 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200957 case VAR_UNKNOWN:
958 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200959 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +0200960 semsg(_(e_using_invalid_value_as_string_str),
961 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200962 break;
963 }
964 return NULL;
965}
966
967/*
968 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
969 * string() on Dict, List, etc.
970 */
971 char_u *
972tv_stringify(typval_T *varp, char_u *buf)
973{
974 if (varp->v_type == VAR_LIST
975 || varp->v_type == VAR_DICT
976 || varp->v_type == VAR_BLOB
977 || varp->v_type == VAR_FUNC
978 || varp->v_type == VAR_PARTIAL
979 || varp->v_type == VAR_FLOAT)
980 {
981 typval_T tmp;
982
983 f_string(varp, &tmp);
984 tv_get_string_buf(&tmp, buf);
985 clear_tv(varp);
986 *varp = tmp;
987 return tmp.vval.v_string;
988 }
989 return tv_get_string_buf(varp, buf);
990}
991
992/*
993 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
994 * Also give an error message, using "name" or _("name") when use_gettext is
995 * TRUE.
996 */
997 int
998tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
999{
1000 int lock = 0;
1001
1002 switch (tv->v_type)
1003 {
1004 case VAR_BLOB:
1005 if (tv->vval.v_blob != NULL)
1006 lock = tv->vval.v_blob->bv_lock;
1007 break;
1008 case VAR_LIST:
1009 if (tv->vval.v_list != NULL)
1010 lock = tv->vval.v_list->lv_lock;
1011 break;
1012 case VAR_DICT:
1013 if (tv->vval.v_dict != NULL)
1014 lock = tv->vval.v_dict->dv_lock;
1015 break;
1016 default:
1017 break;
1018 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001019 return value_check_lock(tv->v_lock, name, use_gettext)
1020 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001021}
1022
1023/*
1024 * Copy the values from typval_T "from" to typval_T "to".
1025 * When needed allocates string or increases reference count.
1026 * Does not make a copy of a list, blob or dict but copies the reference!
1027 * It is OK for "from" and "to" to point to the same item. This is used to
1028 * make a copy later.
1029 */
1030 void
1031copy_tv(typval_T *from, typval_T *to)
1032{
1033 to->v_type = from->v_type;
1034 to->v_lock = 0;
1035 switch (from->v_type)
1036 {
1037 case VAR_NUMBER:
1038 case VAR_BOOL:
1039 case VAR_SPECIAL:
1040 to->vval.v_number = from->vval.v_number;
1041 break;
1042 case VAR_FLOAT:
1043#ifdef FEAT_FLOAT
1044 to->vval.v_float = from->vval.v_float;
1045 break;
1046#endif
1047 case VAR_JOB:
1048#ifdef FEAT_JOB_CHANNEL
1049 to->vval.v_job = from->vval.v_job;
1050 if (to->vval.v_job != NULL)
1051 ++to->vval.v_job->jv_refcount;
1052 break;
1053#endif
1054 case VAR_CHANNEL:
1055#ifdef FEAT_JOB_CHANNEL
1056 to->vval.v_channel = from->vval.v_channel;
1057 if (to->vval.v_channel != NULL)
1058 ++to->vval.v_channel->ch_refcount;
1059 break;
1060#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001061 case VAR_INSTR:
1062 to->vval.v_instr = from->vval.v_instr;
1063 break;
1064
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001065 case VAR_STRING:
1066 case VAR_FUNC:
1067 if (from->vval.v_string == NULL)
1068 to->vval.v_string = NULL;
1069 else
1070 {
1071 to->vval.v_string = vim_strsave(from->vval.v_string);
1072 if (from->v_type == VAR_FUNC)
1073 func_ref(to->vval.v_string);
1074 }
1075 break;
1076 case VAR_PARTIAL:
1077 if (from->vval.v_partial == NULL)
1078 to->vval.v_partial = NULL;
1079 else
1080 {
1081 to->vval.v_partial = from->vval.v_partial;
1082 ++to->vval.v_partial->pt_refcount;
1083 }
1084 break;
1085 case VAR_BLOB:
1086 if (from->vval.v_blob == NULL)
1087 to->vval.v_blob = NULL;
1088 else
1089 {
1090 to->vval.v_blob = from->vval.v_blob;
1091 ++to->vval.v_blob->bv_refcount;
1092 }
1093 break;
1094 case VAR_LIST:
1095 if (from->vval.v_list == NULL)
1096 to->vval.v_list = NULL;
1097 else
1098 {
1099 to->vval.v_list = from->vval.v_list;
1100 ++to->vval.v_list->lv_refcount;
1101 }
1102 break;
1103 case VAR_DICT:
1104 if (from->vval.v_dict == NULL)
1105 to->vval.v_dict = NULL;
1106 else
1107 {
1108 to->vval.v_dict = from->vval.v_dict;
1109 ++to->vval.v_dict->dv_refcount;
1110 }
1111 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +02001112 case VAR_VOID:
1113 emsg(_(e_cannot_use_void_value));
1114 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001115 case VAR_UNKNOWN:
1116 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001117 internal_error_no_abort("copy_tv(UNKNOWN)");
1118 break;
1119 }
1120}
1121
1122/*
1123 * Compare "typ1" and "typ2". Put the result in "typ1".
1124 */
1125 int
1126typval_compare(
1127 typval_T *typ1, // first operand
1128 typval_T *typ2, // second operand
Bram Moolenaar657137c2021-01-09 15:45:23 +01001129 exprtype_T type, // operator
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001130 int ic) // ignore case
1131{
1132 int i;
1133 varnumber_T n1, n2;
1134 char_u *s1, *s2;
1135 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1136 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1137
1138 if (type_is && typ1->v_type != typ2->v_type)
1139 {
1140 // For "is" a different type always means FALSE, for "notis"
1141 // it means TRUE.
1142 n1 = (type == EXPR_ISNOT);
1143 }
1144 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
1145 {
1146 if (type_is)
1147 {
1148 n1 = (typ1->v_type == typ2->v_type
1149 && typ1->vval.v_blob == typ2->vval.v_blob);
1150 if (type == EXPR_ISNOT)
1151 n1 = !n1;
1152 }
1153 else if (typ1->v_type != typ2->v_type
1154 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1155 {
1156 if (typ1->v_type != typ2->v_type)
1157 emsg(_("E977: Can only compare Blob with Blob"));
1158 else
1159 emsg(_(e_invalblob));
1160 clear_tv(typ1);
1161 return FAIL;
1162 }
1163 else
1164 {
1165 // Compare two Blobs for being equal or unequal.
1166 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
1167 if (type == EXPR_NEQUAL)
1168 n1 = !n1;
1169 }
1170 }
1171 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
1172 {
1173 if (type_is)
1174 {
1175 n1 = (typ1->v_type == typ2->v_type
1176 && typ1->vval.v_list == typ2->vval.v_list);
1177 if (type == EXPR_ISNOT)
1178 n1 = !n1;
1179 }
1180 else if (typ1->v_type != typ2->v_type
1181 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1182 {
1183 if (typ1->v_type != typ2->v_type)
1184 emsg(_("E691: Can only compare List with List"));
1185 else
1186 emsg(_("E692: Invalid operation for List"));
1187 clear_tv(typ1);
1188 return FAIL;
1189 }
1190 else
1191 {
1192 // Compare two Lists for being equal or unequal.
1193 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
1194 ic, FALSE);
1195 if (type == EXPR_NEQUAL)
1196 n1 = !n1;
1197 }
1198 }
1199
1200 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
1201 {
1202 if (type_is)
1203 {
1204 n1 = (typ1->v_type == typ2->v_type
1205 && typ1->vval.v_dict == typ2->vval.v_dict);
1206 if (type == EXPR_ISNOT)
1207 n1 = !n1;
1208 }
1209 else if (typ1->v_type != typ2->v_type
1210 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1211 {
1212 if (typ1->v_type != typ2->v_type)
1213 emsg(_("E735: Can only compare Dictionary with Dictionary"));
1214 else
1215 emsg(_("E736: Invalid operation for Dictionary"));
1216 clear_tv(typ1);
1217 return FAIL;
1218 }
1219 else
1220 {
1221 // Compare two Dictionaries for being equal or unequal.
1222 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
1223 ic, FALSE);
1224 if (type == EXPR_NEQUAL)
1225 n1 = !n1;
1226 }
1227 }
1228
1229 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
1230 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
1231 {
1232 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1233 && type != EXPR_IS && type != EXPR_ISNOT)
1234 {
1235 emsg(_("E694: Invalid operation for Funcrefs"));
1236 clear_tv(typ1);
1237 return FAIL;
1238 }
1239 if ((typ1->v_type == VAR_PARTIAL
1240 && typ1->vval.v_partial == NULL)
1241 || (typ2->v_type == VAR_PARTIAL
1242 && typ2->vval.v_partial == NULL))
1243 // When both partials are NULL, then they are equal.
1244 // Otherwise they are not equal.
1245 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
1246 else if (type_is)
1247 {
1248 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
1249 // strings are considered the same if their value is
1250 // the same
1251 n1 = tv_equal(typ1, typ2, ic, FALSE);
1252 else if (typ1->v_type == VAR_PARTIAL
1253 && typ2->v_type == VAR_PARTIAL)
1254 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
1255 else
1256 n1 = FALSE;
1257 }
1258 else
1259 n1 = tv_equal(typ1, typ2, ic, FALSE);
1260 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1261 n1 = !n1;
1262 }
1263
1264#ifdef FEAT_FLOAT
1265 // If one of the two variables is a float, compare as a float.
1266 // When using "=~" or "!~", always compare as string.
1267 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
1268 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1269 {
1270 float_T f1, f2;
1271
1272 f1 = tv_get_float(typ1);
1273 f2 = tv_get_float(typ2);
1274 n1 = FALSE;
1275 switch (type)
1276 {
1277 case EXPR_IS:
1278 case EXPR_EQUAL: n1 = (f1 == f2); break;
1279 case EXPR_ISNOT:
1280 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1281 case EXPR_GREATER: n1 = (f1 > f2); break;
1282 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1283 case EXPR_SMALLER: n1 = (f1 < f2); break;
1284 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1285 case EXPR_UNKNOWN:
1286 case EXPR_MATCH:
1287 default: break; // avoid gcc warning
1288 }
1289 }
1290#endif
1291
1292 // If one of the two variables is a number, compare as a number.
1293 // When using "=~" or "!~", always compare as string.
1294 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
1295 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1296 {
1297 n1 = tv_get_number(typ1);
1298 n2 = tv_get_number(typ2);
1299 switch (type)
1300 {
1301 case EXPR_IS:
1302 case EXPR_EQUAL: n1 = (n1 == n2); break;
1303 case EXPR_ISNOT:
1304 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1305 case EXPR_GREATER: n1 = (n1 > n2); break;
1306 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1307 case EXPR_SMALLER: n1 = (n1 < n2); break;
1308 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1309 case EXPR_UNKNOWN:
1310 case EXPR_MATCH:
1311 default: break; // avoid gcc warning
1312 }
1313 }
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001314 else if (in_vim9script() && (typ1->v_type == VAR_BOOL
Bram Moolenaar0c357522021-07-18 14:43:43 +02001315 || typ2->v_type == VAR_BOOL
1316 || (typ1->v_type == VAR_SPECIAL
1317 && typ2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001318 {
1319 if (typ1->v_type != typ2->v_type)
1320 {
1321 semsg(_(e_cannot_compare_str_with_str),
1322 vartype_name(typ1->v_type), vartype_name(typ2->v_type));
1323 clear_tv(typ1);
1324 return FAIL;
1325 }
1326 n1 = typ1->vval.v_number;
1327 n2 = typ2->vval.v_number;
1328 switch (type)
1329 {
1330 case EXPR_IS:
1331 case EXPR_EQUAL: n1 = (n1 == n2); break;
1332 case EXPR_ISNOT:
1333 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1334 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001335 semsg(_(e_invalid_operation_for_str),
1336 vartype_name(typ1->v_type));
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001337 clear_tv(typ1);
1338 return FAIL;
1339 }
1340 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001341 else
1342 {
Bram Moolenaar0c357522021-07-18 14:43:43 +02001343 if (in_vim9script()
1344 && ((typ1->v_type != VAR_STRING && typ1->v_type != VAR_SPECIAL)
1345 || (typ2->v_type != VAR_STRING && typ2->v_type != VAR_SPECIAL)))
1346 {
1347 semsg(_(e_cannot_compare_str_with_str),
1348 vartype_name(typ1->v_type), vartype_name(typ2->v_type));
1349 clear_tv(typ1);
1350 return FAIL;
1351 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001352 s1 = tv_get_string_buf(typ1, buf1);
1353 s2 = tv_get_string_buf(typ2, buf2);
1354 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1355 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1356 else
1357 i = 0;
1358 n1 = FALSE;
1359 switch (type)
1360 {
1361 case EXPR_IS:
1362 case EXPR_EQUAL: n1 = (i == 0); break;
1363 case EXPR_ISNOT:
1364 case EXPR_NEQUAL: n1 = (i != 0); break;
1365 case EXPR_GREATER: n1 = (i > 0); break;
1366 case EXPR_GEQUAL: n1 = (i >= 0); break;
1367 case EXPR_SMALLER: n1 = (i < 0); break;
1368 case EXPR_SEQUAL: n1 = (i <= 0); break;
1369
1370 case EXPR_MATCH:
1371 case EXPR_NOMATCH:
1372 n1 = pattern_match(s2, s1, ic);
1373 if (type == EXPR_NOMATCH)
1374 n1 = !n1;
1375 break;
1376
1377 default: break; // avoid gcc warning
1378 }
1379 }
1380 clear_tv(typ1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001381 if (in_vim9script())
1382 {
1383 typ1->v_type = VAR_BOOL;
1384 typ1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
1385 }
1386 else
1387 {
1388 typ1->v_type = VAR_NUMBER;
1389 typ1->vval.v_number = n1;
1390 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001391
1392 return OK;
1393}
1394
Bram Moolenaar34453202021-01-31 13:08:38 +01001395/*
1396 * Convert any type to a string, never give an error.
1397 * When "quotes" is TRUE add quotes to a string.
1398 * Returns an allocated string.
1399 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001400 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001401typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001402{
1403 char_u *tofree;
1404 char_u numbuf[NUMBUFLEN];
1405 char_u *ret = NULL;
1406
1407 if (arg == NULL)
1408 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001409 if (!quotes && arg->v_type == VAR_STRING)
1410 {
1411 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1412 : arg->vval.v_string);
1413 }
1414 else
1415 {
1416 ret = tv2string(arg, &tofree, numbuf, 0);
1417 // Make a copy if we have a value but it's not in allocated memory.
1418 if (ret != NULL && tofree == NULL)
1419 ret = vim_strsave(ret);
1420 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001421 return ret;
1422}
1423
1424/*
1425 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1426 * or it refers to a List or Dictionary that is locked.
1427 */
1428 int
1429tv_islocked(typval_T *tv)
1430{
1431 return (tv->v_lock & VAR_LOCKED)
1432 || (tv->v_type == VAR_LIST
1433 && tv->vval.v_list != NULL
1434 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1435 || (tv->v_type == VAR_DICT
1436 && tv->vval.v_dict != NULL
1437 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1438}
1439
1440 static int
1441func_equal(
1442 typval_T *tv1,
1443 typval_T *tv2,
1444 int ic) // ignore case
1445{
1446 char_u *s1, *s2;
1447 dict_T *d1, *d2;
1448 int a1, a2;
1449 int i;
1450
1451 // empty and NULL function name considered the same
1452 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1453 : partial_name(tv1->vval.v_partial);
1454 if (s1 != NULL && *s1 == NUL)
1455 s1 = NULL;
1456 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1457 : partial_name(tv2->vval.v_partial);
1458 if (s2 != NULL && *s2 == NUL)
1459 s2 = NULL;
1460 if (s1 == NULL || s2 == NULL)
1461 {
1462 if (s1 != s2)
1463 return FALSE;
1464 }
1465 else if (STRCMP(s1, s2) != 0)
1466 return FALSE;
1467
1468 // empty dict and NULL dict is different
1469 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1470 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1471 if (d1 == NULL || d2 == NULL)
1472 {
1473 if (d1 != d2)
1474 return FALSE;
1475 }
1476 else if (!dict_equal(d1, d2, ic, TRUE))
1477 return FALSE;
1478
1479 // empty list and no list considered the same
1480 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1481 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1482 if (a1 != a2)
1483 return FALSE;
1484 for (i = 0; i < a1; ++i)
1485 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1486 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1487 return FALSE;
1488
1489 return TRUE;
1490}
1491
1492/*
1493 * Return TRUE if "tv1" and "tv2" have the same value.
1494 * Compares the items just like "==" would compare them, but strings and
1495 * numbers are different. Floats and numbers are also different.
1496 */
1497 int
1498tv_equal(
1499 typval_T *tv1,
1500 typval_T *tv2,
1501 int ic, // ignore case
1502 int recursive) // TRUE when used recursively
1503{
1504 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1505 char_u *s1, *s2;
1506 static int recursive_cnt = 0; // catch recursive loops
1507 int r;
1508 static int tv_equal_recurse_limit;
1509
1510 // Catch lists and dicts that have an endless loop by limiting
1511 // recursiveness to a limit. We guess they are equal then.
1512 // A fixed limit has the problem of still taking an awful long time.
1513 // Reduce the limit every time running into it. That should work fine for
1514 // deeply linked structures that are not recursively linked and catch
1515 // recursiveness quickly.
1516 if (!recursive)
1517 tv_equal_recurse_limit = 1000;
1518 if (recursive_cnt >= tv_equal_recurse_limit)
1519 {
1520 --tv_equal_recurse_limit;
1521 return TRUE;
1522 }
1523
1524 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1525 // arguments.
1526 if ((tv1->v_type == VAR_FUNC
1527 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1528 && (tv2->v_type == VAR_FUNC
1529 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1530 {
1531 ++recursive_cnt;
1532 r = func_equal(tv1, tv2, ic);
1533 --recursive_cnt;
1534 return r;
1535 }
1536
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001537 if (tv1->v_type != tv2->v_type
1538 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1539 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001540 return FALSE;
1541
1542 switch (tv1->v_type)
1543 {
1544 case VAR_LIST:
1545 ++recursive_cnt;
1546 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1547 --recursive_cnt;
1548 return r;
1549
1550 case VAR_DICT:
1551 ++recursive_cnt;
1552 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1553 --recursive_cnt;
1554 return r;
1555
1556 case VAR_BLOB:
1557 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1558
1559 case VAR_NUMBER:
1560 case VAR_BOOL:
1561 case VAR_SPECIAL:
1562 return tv1->vval.v_number == tv2->vval.v_number;
1563
1564 case VAR_STRING:
1565 s1 = tv_get_string_buf(tv1, buf1);
1566 s2 = tv_get_string_buf(tv2, buf2);
1567 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1568
1569 case VAR_FLOAT:
1570#ifdef FEAT_FLOAT
1571 return tv1->vval.v_float == tv2->vval.v_float;
1572#endif
1573 case VAR_JOB:
1574#ifdef FEAT_JOB_CHANNEL
1575 return tv1->vval.v_job == tv2->vval.v_job;
1576#endif
1577 case VAR_CHANNEL:
1578#ifdef FEAT_JOB_CHANNEL
1579 return tv1->vval.v_channel == tv2->vval.v_channel;
1580#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001581 case VAR_INSTR:
1582 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001583
1584 case VAR_PARTIAL:
1585 return tv1->vval.v_partial == tv2->vval.v_partial;
1586
1587 case VAR_FUNC:
1588 return tv1->vval.v_string == tv2->vval.v_string;
1589
1590 case VAR_UNKNOWN:
1591 case VAR_ANY:
1592 case VAR_VOID:
1593 break;
1594 }
1595
1596 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1597 // does not equal anything, not even itself.
1598 return FALSE;
1599}
1600
1601/*
1602 * Get an option value.
1603 * "arg" points to the '&' or '+' before the option name.
1604 * "arg" is advanced to character after the option name.
1605 * Return OK or FAIL.
1606 */
1607 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001608eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001609 char_u **arg,
1610 typval_T *rettv, // when NULL, only check if option exists
1611 int evaluate)
1612{
1613 char_u *option_end;
1614 long numval;
1615 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001616 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001617 int c;
1618 int working = (**arg == '+'); // has("+option")
1619 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001620 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001621
1622 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001623 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001624 if (option_end == NULL)
1625 {
1626 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001627 semsg(_(e_option_name_missing_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001628 return FAIL;
1629 }
1630
1631 if (!evaluate)
1632 {
1633 *arg = option_end;
1634 return OK;
1635 }
1636
1637 c = *option_end;
1638 *option_end = NUL;
1639 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001640 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001641
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001642 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001643 {
1644 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001645 semsg(_(e_unknown_option_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001646 ret = FAIL;
1647 }
1648 else if (rettv != NULL)
1649 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001650 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001651 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001652 {
1653 rettv->v_type = VAR_STRING;
1654 rettv->vval.v_string = NULL;
1655 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001656 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001657 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001658 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1659 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001660 rettv->vval.v_number = 0;
1661 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001662 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001663 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001664 if (in_vim9script() && opt_type == gov_bool)
1665 {
1666 rettv->v_type = VAR_BOOL;
1667 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1668 }
1669 else
1670 {
1671 rettv->v_type = VAR_NUMBER;
1672 rettv->vval.v_number = numval;
1673 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001674 }
1675 else // string option
1676 {
1677 rettv->v_type = VAR_STRING;
1678 rettv->vval.v_string = stringval;
1679 }
1680 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001681 else if (working && (opt_type == gov_hidden_bool
1682 || opt_type == gov_hidden_number
1683 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001684 ret = FAIL;
1685
1686 *option_end = c; // put back for error messages
1687 *arg = option_end;
1688
1689 return ret;
1690}
1691
1692/*
1693 * Allocate a variable for a number constant. Also deals with "0z" for blob.
1694 * Return OK or FAIL.
1695 */
1696 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001697eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001698 char_u **arg,
1699 typval_T *rettv,
1700 int evaluate,
1701 int want_string UNUSED)
1702{
1703 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02001704 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001705#ifdef FEAT_FLOAT
1706 char_u *p;
1707 int get_float = FALSE;
1708
1709 // We accept a float when the format matches
1710 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
1711 // strict to avoid backwards compatibility problems.
1712 // With script version 2 and later the leading digit can be
1713 // omitted.
1714 // Don't look for a float after the "." operator, so that
1715 // ":let vers = 1.2.3" doesn't fail.
1716 if (**arg == '.')
1717 p = *arg;
1718 else
Bram Moolenaar29500652021-08-08 15:43:34 +02001719 {
1720 p = *arg + 1;
1721 if (skip_quotes)
1722 for (;;)
1723 {
1724 if (*p == '\'')
1725 ++p;
1726 if (!vim_isdigit(*p))
1727 break;
1728 p = skipdigits(p);
1729 }
1730 else
1731 p = skipdigits(p);
1732 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001733 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
1734 {
1735 get_float = TRUE;
1736 p = skipdigits(p + 2);
1737 if (*p == 'e' || *p == 'E')
1738 {
1739 ++p;
1740 if (*p == '-' || *p == '+')
1741 ++p;
1742 if (!vim_isdigit(*p))
1743 get_float = FALSE;
1744 else
1745 p = skipdigits(p + 1);
1746 }
1747 if (ASCII_ISALPHA(*p) || *p == '.')
1748 get_float = FALSE;
1749 }
1750 if (get_float)
1751 {
1752 float_T f;
1753
Bram Moolenaar29500652021-08-08 15:43:34 +02001754 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001755 if (evaluate)
1756 {
1757 rettv->v_type = VAR_FLOAT;
1758 rettv->vval.v_float = f;
1759 }
1760 }
1761 else
1762#endif
1763 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
1764 {
1765 char_u *bp;
1766 blob_T *blob = NULL; // init for gcc
1767
1768 // Blob constant: 0z0123456789abcdef
1769 if (evaluate)
1770 blob = blob_alloc();
1771 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
1772 {
1773 if (!vim_isxdigit(bp[1]))
1774 {
1775 if (blob != NULL)
1776 {
1777 emsg(_("E973: Blob literal should have an even number of hex characters"));
1778 ga_clear(&blob->bv_ga);
1779 VIM_CLEAR(blob);
1780 }
1781 return FAIL;
1782 }
1783 if (blob != NULL)
1784 ga_append(&blob->bv_ga,
1785 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
1786 if (bp[2] == '.' && vim_isxdigit(bp[3]))
1787 ++bp;
1788 }
1789 if (blob != NULL)
1790 rettv_blob_set(rettv, blob);
1791 *arg = bp;
1792 }
1793 else
1794 {
1795 varnumber_T n;
1796
1797 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02001798 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001799 ? STR2NR_NO_OCT + STR2NR_QUOTE
1800 : STR2NR_ALL, &n, NULL, 0, TRUE);
1801 if (len == 0)
1802 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00001803 if (evaluate)
1804 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001805 return FAIL;
1806 }
1807 *arg += len;
1808 if (evaluate)
1809 {
1810 rettv->v_type = VAR_NUMBER;
1811 rettv->vval.v_number = n;
1812 }
1813 }
1814 return OK;
1815}
1816
1817/*
1818 * Allocate a variable for a string constant.
1819 * Return OK or FAIL.
1820 */
1821 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001822eval_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001823{
1824 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001825 char_u *end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001826 int extra = 0;
1827 int len;
1828
1829 // Find the end of the string, skipping backslashed characters.
1830 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
1831 {
1832 if (*p == '\\' && p[1] != NUL)
1833 {
1834 ++p;
1835 // A "\<x>" form occupies at least 4 characters, and produces up
1836 // to 21 characters (3 * 6 for the char and 3 for a modifier):
1837 // reserve space for 18 extra.
1838 // Each byte in the char could be encoded as K_SPECIAL K_EXTRA x.
1839 if (*p == '<')
1840 extra += 18;
1841 }
1842 }
1843
1844 if (*p != '"')
1845 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001846 semsg(_(e_missing_double_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001847 return FAIL;
1848 }
1849
1850 // If only parsing, set *arg and return here
1851 if (!evaluate)
1852 {
1853 *arg = p + 1;
1854 return OK;
1855 }
1856
1857 // Copy the string into allocated memory, handling backslashed
1858 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001859 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001860 len = (int)(p - *arg + extra);
1861 rettv->vval.v_string = alloc(len);
1862 if (rettv->vval.v_string == NULL)
1863 return FAIL;
1864 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001865
1866 for (p = *arg + 1; *p != NUL && *p != '"'; )
1867 {
1868 if (*p == '\\')
1869 {
1870 switch (*++p)
1871 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001872 case 'b': *end++ = BS; ++p; break;
1873 case 'e': *end++ = ESC; ++p; break;
1874 case 'f': *end++ = FF; ++p; break;
1875 case 'n': *end++ = NL; ++p; break;
1876 case 'r': *end++ = CAR; ++p; break;
1877 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001878
1879 case 'X': // hex: "\x1", "\x12"
1880 case 'x':
1881 case 'u': // Unicode: "\u0023"
1882 case 'U':
1883 if (vim_isxdigit(p[1]))
1884 {
1885 int n, nr;
1886 int c = toupper(*p);
1887
1888 if (c == 'X')
1889 n = 2;
1890 else if (*p == 'u')
1891 n = 4;
1892 else
1893 n = 8;
1894 nr = 0;
1895 while (--n >= 0 && vim_isxdigit(p[1]))
1896 {
1897 ++p;
1898 nr = (nr << 4) + hex2nr(*p);
1899 }
1900 ++p;
1901 // For "\u" store the number according to
1902 // 'encoding'.
1903 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001904 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001905 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001906 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001907 }
1908 break;
1909
1910 // octal: "\1", "\12", "\123"
1911 case '0':
1912 case '1':
1913 case '2':
1914 case '3':
1915 case '4':
1916 case '5':
1917 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001918 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001919 if (*p >= '0' && *p <= '7')
1920 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001921 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001922 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001923 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001924 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001925 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001926 break;
1927
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001928 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001929 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001930 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001931 int flags = FSK_KEYCODE | FSK_IN_STRING;
1932
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001933 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001934 flags |= FSK_SIMPLIFY;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001935 extra = trans_special(&p, end, flags, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001936 if (extra != 0)
1937 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001938 end += extra;
1939 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001940 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001941 break;
1942 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001943 }
1944 // FALLTHROUGH
1945
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001946 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001947 break;
1948 }
1949 }
1950 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001951 MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001952 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001953 *end = NUL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001954 if (*p != NUL) // just in case
1955 ++p;
1956 *arg = p;
1957
1958 return OK;
1959}
1960
1961/*
1962 * Allocate a variable for a 'str''ing' constant.
1963 * Return OK or FAIL.
1964 */
1965 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001966eval_lit_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001967{
1968 char_u *p;
1969 char_u *str;
1970 int reduce = 0;
1971
1972 // Find the end of the string, skipping ''.
1973 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
1974 {
1975 if (*p == '\'')
1976 {
1977 if (p[1] != '\'')
1978 break;
1979 ++reduce;
1980 ++p;
1981 }
1982 }
1983
1984 if (*p != '\'')
1985 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001986 semsg(_(e_missing_single_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001987 return FAIL;
1988 }
1989
1990 // If only parsing return after setting "*arg"
1991 if (!evaluate)
1992 {
1993 *arg = p + 1;
1994 return OK;
1995 }
1996
1997 // Copy the string into allocated memory, handling '' to ' reduction.
1998 str = alloc((p - *arg) - reduce);
1999 if (str == NULL)
2000 return FAIL;
2001 rettv->v_type = VAR_STRING;
2002 rettv->vval.v_string = str;
2003
2004 for (p = *arg + 1; *p != NUL; )
2005 {
2006 if (*p == '\'')
2007 {
2008 if (p[1] != '\'')
2009 break;
2010 ++p;
2011 }
2012 MB_COPY_CHAR(p, str);
2013 }
2014 *str = NUL;
2015 *arg = p + 1;
2016
2017 return OK;
2018}
2019
2020/*
2021 * Return a string with the string representation of a variable.
2022 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2023 * "numbuf" is used for a number.
2024 * Puts quotes around strings, so that they can be parsed back by eval().
2025 * May return NULL.
2026 */
2027 char_u *
2028tv2string(
2029 typval_T *tv,
2030 char_u **tofree,
2031 char_u *numbuf,
2032 int copyID)
2033{
2034 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2035}
2036
2037/*
2038 * Get the value of an environment variable.
2039 * "arg" is pointing to the '$'. It is advanced to after the name.
2040 * If the environment variable was not set, silently assume it is empty.
2041 * Return FAIL if the name is invalid.
2042 */
2043 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002044eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002045{
2046 char_u *string = NULL;
2047 int len;
2048 int cc;
2049 char_u *name;
2050 int mustfree = FALSE;
2051
2052 ++*arg;
2053 name = *arg;
2054 len = get_env_len(arg);
2055 if (evaluate)
2056 {
2057 if (len == 0)
2058 return FAIL; // invalid empty name
2059
2060 cc = name[len];
2061 name[len] = NUL;
2062 // first try vim_getenv(), fast for normal environment vars
2063 string = vim_getenv(name, &mustfree);
2064 if (string != NULL && *string != NUL)
2065 {
2066 if (!mustfree)
2067 string = vim_strsave(string);
2068 }
2069 else
2070 {
2071 if (mustfree)
2072 vim_free(string);
2073
2074 // next try expanding things like $VIM and ${HOME}
2075 string = expand_env_save(name - 1);
2076 if (string != NULL && *string == '$')
2077 VIM_CLEAR(string);
2078 }
2079 name[len] = cc;
2080
2081 rettv->v_type = VAR_STRING;
2082 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002083 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002084 }
2085
2086 return OK;
2087}
2088
2089/*
2090 * Get the lnum from the first argument.
2091 * Also accepts ".", "$", etc., but that only works for the current buffer.
2092 * Returns -1 on error.
2093 */
2094 linenr_T
2095tv_get_lnum(typval_T *argvars)
2096{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002097 linenr_T lnum = -1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002098
Bram Moolenaar56acb092020-08-16 14:48:19 +02002099 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2100 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002101 if (lnum <= 0 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002102 {
2103 int fnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002104 pos_T *fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002105
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002106 // no valid number, try using arg like line()
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002107 if (fp != NULL)
2108 lnum = fp->lnum;
2109 }
2110 return lnum;
2111}
2112
2113/*
2114 * Get the lnum from the first argument.
2115 * Also accepts "$", then "buf" is used.
2116 * Returns 0 on error.
2117 */
2118 linenr_T
2119tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2120{
2121 if (argvars[0].v_type == VAR_STRING
2122 && argvars[0].vval.v_string != NULL
2123 && argvars[0].vval.v_string[0] == '$'
2124 && buf != NULL)
2125 return buf->b_ml.ml_line_count;
2126 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2127}
2128
2129/*
2130 * Get buffer by number or pattern.
2131 */
2132 buf_T *
2133tv_get_buf(typval_T *tv, int curtab_only)
2134{
2135 char_u *name = tv->vval.v_string;
2136 buf_T *buf;
2137
2138 if (tv->v_type == VAR_NUMBER)
2139 return buflist_findnr((int)tv->vval.v_number);
2140 if (tv->v_type != VAR_STRING)
2141 return NULL;
2142 if (name == NULL || *name == NUL)
2143 return curbuf;
2144 if (name[0] == '$' && name[1] == NUL)
2145 return lastbuf;
2146
2147 buf = buflist_find_by_name(name, curtab_only);
2148
2149 // If not found, try expanding the name, like done for bufexists().
2150 if (buf == NULL)
2151 buf = find_buffer(tv);
2152
2153 return buf;
2154}
2155
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002156/*
2157 * Like tv_get_buf() but give an error message is the type is wrong.
2158 */
2159 buf_T *
2160tv_get_buf_from_arg(typval_T *tv)
2161{
2162 buf_T *buf;
2163
2164 ++emsg_off;
2165 buf = tv_get_buf(tv, FALSE);
2166 --emsg_off;
2167 if (buf == NULL
2168 && tv->v_type != VAR_NUMBER
2169 && tv->v_type != VAR_STRING)
2170 // issue errmsg for type error
2171 (void)tv_get_number(tv);
2172 return buf;
2173}
2174
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002175#endif // FEAT_EVAL