blob: 9e9b05f9929430d142f3f04246651131af871287 [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/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001123 * Compare "tv1" and "tv2".
1124 * Put the result in "tv1". Caller should clear "tv2".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001125 */
1126 int
1127typval_compare(
Bram Moolenaar265f8112021-12-19 12:33:05 +00001128 typval_T *tv1, // first operand
1129 typval_T *tv2, // second operand
1130 exprtype_T type, // operator
1131 int ic) // ignore case
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001132{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001133 varnumber_T n1, n2;
Bram Moolenaar265f8112021-12-19 12:33:05 +00001134 int res = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001135 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1136
Bram Moolenaar265f8112021-12-19 12:33:05 +00001137 if (type_is && tv1->v_type != tv2->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001138 {
1139 // For "is" a different type always means FALSE, for "notis"
1140 // it means TRUE.
1141 n1 = (type == EXPR_ISNOT);
1142 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001143 else if (tv1->v_type == VAR_BLOB || tv2->v_type == VAR_BLOB)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001144 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001145 if (typval_compare_blob(tv1, tv2, type, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001146 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001147 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001148 return FAIL;
1149 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001150 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001151 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001152 else if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001153 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001154 if (typval_compare_list(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001155 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001156 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001157 return FAIL;
1158 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001159 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001160 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001161 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001162 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001163 if (typval_compare_dict(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001164 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001165 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001166 return FAIL;
1167 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001168 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001169 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001170 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC
1171 || tv1->v_type == VAR_PARTIAL || tv2->v_type == VAR_PARTIAL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001172 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001173 if (typval_compare_func(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001174 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001175 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001176 return FAIL;
1177 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001178 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001179 }
1180
1181#ifdef FEAT_FLOAT
1182 // If one of the two variables is a float, compare as a float.
1183 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001184 else if ((tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001185 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1186 {
1187 float_T f1, f2;
1188
Bram Moolenaar265f8112021-12-19 12:33:05 +00001189 f1 = tv_get_float(tv1);
1190 f2 = tv_get_float(tv2);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001191 n1 = FALSE;
1192 switch (type)
1193 {
1194 case EXPR_IS:
1195 case EXPR_EQUAL: n1 = (f1 == f2); break;
1196 case EXPR_ISNOT:
1197 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1198 case EXPR_GREATER: n1 = (f1 > f2); break;
1199 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1200 case EXPR_SMALLER: n1 = (f1 < f2); break;
1201 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1202 case EXPR_UNKNOWN:
1203 case EXPR_MATCH:
1204 default: break; // avoid gcc warning
1205 }
1206 }
1207#endif
1208
1209 // If one of the two variables is a number, compare as a number.
1210 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001211 else if ((tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001212 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1213 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001214 n1 = tv_get_number(tv1);
1215 n2 = tv_get_number(tv2);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001216 switch (type)
1217 {
1218 case EXPR_IS:
1219 case EXPR_EQUAL: n1 = (n1 == n2); break;
1220 case EXPR_ISNOT:
1221 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1222 case EXPR_GREATER: n1 = (n1 > n2); break;
1223 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1224 case EXPR_SMALLER: n1 = (n1 < n2); break;
1225 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1226 case EXPR_UNKNOWN:
1227 case EXPR_MATCH:
1228 default: break; // avoid gcc warning
1229 }
1230 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001231 else if (in_vim9script() && (tv1->v_type == VAR_BOOL
1232 || tv2->v_type == VAR_BOOL
1233 || (tv1->v_type == VAR_SPECIAL
1234 && tv2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001235 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001236 if (tv1->v_type != tv2->v_type)
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001237 {
1238 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001239 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1240 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001241 return FAIL;
1242 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001243 n1 = tv1->vval.v_number;
1244 n2 = tv2->vval.v_number;
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001245 switch (type)
1246 {
1247 case EXPR_IS:
1248 case EXPR_EQUAL: n1 = (n1 == n2); break;
1249 case EXPR_ISNOT:
1250 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1251 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001252 semsg(_(e_invalid_operation_for_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001253 vartype_name(tv1->v_type));
1254 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001255 return FAIL;
1256 }
1257 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001258 else
1259 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001260 if (typval_compare_string(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar0c357522021-07-18 14:43:43 +02001261 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001262 clear_tv(tv1);
Bram Moolenaar0c357522021-07-18 14:43:43 +02001263 return FAIL;
1264 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001265 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001266 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001267 clear_tv(tv1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001268 if (in_vim9script())
1269 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001270 tv1->v_type = VAR_BOOL;
1271 tv1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001272 }
1273 else
1274 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001275 tv1->v_type = VAR_NUMBER;
1276 tv1->vval.v_number = n1;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001277 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001278
1279 return OK;
1280}
1281
Bram Moolenaar34453202021-01-31 13:08:38 +01001282/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001283 * Compare "tv1" to "tv2" as lists acording to "type" and "ic".
1284 * Put the result, false or true, in "res".
1285 * Return FAIL and give an error message when the comparison can't be done.
1286 */
1287 int
1288typval_compare_list(
1289 typval_T *tv1,
1290 typval_T *tv2,
1291 exprtype_T type,
1292 int ic,
1293 int *res)
1294{
1295 int val = 0;
1296
1297 if (type == EXPR_IS || type == EXPR_ISNOT)
1298 {
1299 val = (tv1->v_type == tv2->v_type
1300 && tv1->vval.v_list == tv2->vval.v_list);
1301 if (type == EXPR_ISNOT)
1302 val = !val;
1303 }
1304 else if (tv1->v_type != tv2->v_type
1305 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1306 {
1307 if (tv1->v_type != tv2->v_type)
1308 emsg(_("E691: Can only compare List with List"));
1309 else
1310 emsg(_("E692: Invalid operation for List"));
1311 return FAIL;
1312 }
1313 else
1314 {
1315 val = list_equal(tv1->vval.v_list, tv2->vval.v_list,
1316 ic, FALSE);
1317 if (type == EXPR_NEQUAL)
1318 val = !val;
1319 }
1320 *res = val;
1321 return OK;
1322}
1323
1324/*
1325 * Compare "tv1" to "tv2" as blobs acording to "type".
1326 * Put the result, false or true, in "res".
1327 * Return FAIL and give an error message when the comparison can't be done.
1328 */
1329 int
1330typval_compare_blob(
1331 typval_T *tv1,
1332 typval_T *tv2,
1333 exprtype_T type,
1334 int *res)
1335{
1336 int val = 0;
1337
1338 if (type == EXPR_IS || type == EXPR_ISNOT)
1339 {
1340 val = (tv1->v_type == tv2->v_type
1341 && tv1->vval.v_blob == tv2->vval.v_blob);
1342 if (type == EXPR_ISNOT)
1343 val = !val;
1344 }
1345 else if (tv1->v_type != tv2->v_type
1346 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1347 {
1348 if (tv1->v_type != tv2->v_type)
1349 emsg(_("E977: Can only compare Blob with Blob"));
1350 else
1351 emsg(_(e_invalblob));
1352 return FAIL;
1353 }
1354 else
1355 {
1356 val = blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1357 if (type == EXPR_NEQUAL)
1358 val = !val;
1359 }
1360 *res = val;
1361 return OK;
1362}
1363
1364/*
1365 * Compare "tv1" to "tv2" as dictionaries acording to "type" and "ic".
1366 * Put the result, false or true, in "res".
1367 * Return FAIL and give an error message when the comparison can't be done.
1368 */
1369 int
1370typval_compare_dict(
1371 typval_T *tv1,
1372 typval_T *tv2,
1373 exprtype_T type,
1374 int ic,
1375 int *res)
1376{
1377 int val;
1378
1379 if (type == EXPR_IS || type == EXPR_ISNOT)
1380 {
1381 val = (tv1->v_type == tv2->v_type
1382 && tv1->vval.v_dict == tv2->vval.v_dict);
1383 if (type == EXPR_ISNOT)
1384 val = !val;
1385 }
1386 else if (tv1->v_type != tv2->v_type
1387 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1388 {
1389 if (tv1->v_type != tv2->v_type)
1390 emsg(_("E735: Can only compare Dictionary with Dictionary"));
1391 else
1392 emsg(_("E736: Invalid operation for Dictionary"));
1393 return FAIL;
1394 }
1395 else
1396 {
1397 val = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, FALSE);
1398 if (type == EXPR_NEQUAL)
1399 val = !val;
1400 }
1401 *res = val;
1402 return OK;
1403}
1404
1405/*
1406 * Compare "tv1" to "tv2" as funcrefs acording to "type" and "ic".
1407 * Put the result, false or true, in "res".
1408 * Return FAIL and give an error message when the comparison can't be done.
1409 */
1410 int
1411typval_compare_func(
1412 typval_T *tv1,
1413 typval_T *tv2,
1414 exprtype_T type,
1415 int ic,
1416 int *res)
1417{
1418 int val = 0;
1419
1420 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1421 && type != EXPR_IS && type != EXPR_ISNOT)
1422 {
1423 emsg(_("E694: Invalid operation for Funcrefs"));
1424 return FAIL;
1425 }
1426 if ((tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial == NULL)
1427 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial == NULL))
1428 // When both partials are NULL, then they are equal.
1429 // Otherwise they are not equal.
1430 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1431 else if (type == EXPR_IS || type == EXPR_ISNOT)
1432 {
1433 if (tv1->v_type == VAR_FUNC && tv2->v_type == VAR_FUNC)
1434 // strings are considered the same if their value is
1435 // the same
1436 val = tv_equal(tv1, tv2, ic, FALSE);
1437 else if (tv1->v_type == VAR_PARTIAL && tv2->v_type == VAR_PARTIAL)
1438 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1439 else
1440 val = FALSE;
1441 }
1442 else
1443 val = tv_equal(tv1, tv2, ic, FALSE);
1444 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1445 val = !val;
1446 *res = val;
1447 return OK;
1448}
1449
1450/*
1451 * Compare "tv1" to "tv2" as strings according to "type" and "ic".
1452 * Put the result, false or true, in "res".
1453 * Return FAIL and give an error message when the comparison can't be done.
1454 */
1455 int
1456typval_compare_string(
1457 typval_T *tv1,
1458 typval_T *tv2,
1459 exprtype_T type,
1460 int ic,
1461 int *res)
1462{
1463 int i = 0;
1464 int val = FALSE;
1465 char_u *s1, *s2;
1466 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1467
1468 if (in_vim9script()
1469 && ((tv1->v_type != VAR_STRING && tv1->v_type != VAR_SPECIAL)
1470 || (tv2->v_type != VAR_STRING && tv2->v_type != VAR_SPECIAL)))
1471 {
1472 semsg(_(e_cannot_compare_str_with_str),
1473 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1474 return FAIL;
1475 }
1476 s1 = tv_get_string_buf(tv1, buf1);
1477 s2 = tv_get_string_buf(tv2, buf2);
1478 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1479 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1480 switch (type)
1481 {
1482 case EXPR_IS:
1483 case EXPR_EQUAL: val = (i == 0); break;
1484 case EXPR_ISNOT:
1485 case EXPR_NEQUAL: val = (i != 0); break;
1486 case EXPR_GREATER: val = (i > 0); break;
1487 case EXPR_GEQUAL: val = (i >= 0); break;
1488 case EXPR_SMALLER: val = (i < 0); break;
1489 case EXPR_SEQUAL: val = (i <= 0); break;
1490
1491 case EXPR_MATCH:
1492 case EXPR_NOMATCH:
1493 val = pattern_match(s2, s1, ic);
1494 if (type == EXPR_NOMATCH)
1495 val = !val;
1496 break;
1497
1498 default: break; // avoid gcc warning
1499 }
1500 *res = val;
1501 return OK;
1502}
1503/*
Bram Moolenaar34453202021-01-31 13:08:38 +01001504 * Convert any type to a string, never give an error.
1505 * When "quotes" is TRUE add quotes to a string.
1506 * Returns an allocated string.
1507 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001508 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001509typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001510{
1511 char_u *tofree;
1512 char_u numbuf[NUMBUFLEN];
1513 char_u *ret = NULL;
1514
1515 if (arg == NULL)
1516 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001517 if (!quotes && arg->v_type == VAR_STRING)
1518 {
1519 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1520 : arg->vval.v_string);
1521 }
1522 else
1523 {
1524 ret = tv2string(arg, &tofree, numbuf, 0);
1525 // Make a copy if we have a value but it's not in allocated memory.
1526 if (ret != NULL && tofree == NULL)
1527 ret = vim_strsave(ret);
1528 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001529 return ret;
1530}
1531
1532/*
1533 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1534 * or it refers to a List or Dictionary that is locked.
1535 */
1536 int
1537tv_islocked(typval_T *tv)
1538{
1539 return (tv->v_lock & VAR_LOCKED)
1540 || (tv->v_type == VAR_LIST
1541 && tv->vval.v_list != NULL
1542 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1543 || (tv->v_type == VAR_DICT
1544 && tv->vval.v_dict != NULL
1545 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1546}
1547
1548 static int
1549func_equal(
1550 typval_T *tv1,
1551 typval_T *tv2,
1552 int ic) // ignore case
1553{
1554 char_u *s1, *s2;
1555 dict_T *d1, *d2;
1556 int a1, a2;
1557 int i;
1558
1559 // empty and NULL function name considered the same
1560 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1561 : partial_name(tv1->vval.v_partial);
1562 if (s1 != NULL && *s1 == NUL)
1563 s1 = NULL;
1564 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1565 : partial_name(tv2->vval.v_partial);
1566 if (s2 != NULL && *s2 == NUL)
1567 s2 = NULL;
1568 if (s1 == NULL || s2 == NULL)
1569 {
1570 if (s1 != s2)
1571 return FALSE;
1572 }
1573 else if (STRCMP(s1, s2) != 0)
1574 return FALSE;
1575
1576 // empty dict and NULL dict is different
1577 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1578 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1579 if (d1 == NULL || d2 == NULL)
1580 {
1581 if (d1 != d2)
1582 return FALSE;
1583 }
1584 else if (!dict_equal(d1, d2, ic, TRUE))
1585 return FALSE;
1586
1587 // empty list and no list considered the same
1588 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1589 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1590 if (a1 != a2)
1591 return FALSE;
1592 for (i = 0; i < a1; ++i)
1593 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1594 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1595 return FALSE;
1596
1597 return TRUE;
1598}
1599
1600/*
1601 * Return TRUE if "tv1" and "tv2" have the same value.
1602 * Compares the items just like "==" would compare them, but strings and
1603 * numbers are different. Floats and numbers are also different.
1604 */
1605 int
1606tv_equal(
1607 typval_T *tv1,
1608 typval_T *tv2,
1609 int ic, // ignore case
1610 int recursive) // TRUE when used recursively
1611{
1612 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1613 char_u *s1, *s2;
1614 static int recursive_cnt = 0; // catch recursive loops
1615 int r;
1616 static int tv_equal_recurse_limit;
1617
1618 // Catch lists and dicts that have an endless loop by limiting
1619 // recursiveness to a limit. We guess they are equal then.
1620 // A fixed limit has the problem of still taking an awful long time.
1621 // Reduce the limit every time running into it. That should work fine for
1622 // deeply linked structures that are not recursively linked and catch
1623 // recursiveness quickly.
1624 if (!recursive)
1625 tv_equal_recurse_limit = 1000;
1626 if (recursive_cnt >= tv_equal_recurse_limit)
1627 {
1628 --tv_equal_recurse_limit;
1629 return TRUE;
1630 }
1631
1632 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1633 // arguments.
1634 if ((tv1->v_type == VAR_FUNC
1635 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1636 && (tv2->v_type == VAR_FUNC
1637 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1638 {
1639 ++recursive_cnt;
1640 r = func_equal(tv1, tv2, ic);
1641 --recursive_cnt;
1642 return r;
1643 }
1644
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001645 if (tv1->v_type != tv2->v_type
1646 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1647 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001648 return FALSE;
1649
1650 switch (tv1->v_type)
1651 {
1652 case VAR_LIST:
1653 ++recursive_cnt;
1654 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1655 --recursive_cnt;
1656 return r;
1657
1658 case VAR_DICT:
1659 ++recursive_cnt;
1660 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1661 --recursive_cnt;
1662 return r;
1663
1664 case VAR_BLOB:
1665 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1666
1667 case VAR_NUMBER:
1668 case VAR_BOOL:
1669 case VAR_SPECIAL:
1670 return tv1->vval.v_number == tv2->vval.v_number;
1671
1672 case VAR_STRING:
1673 s1 = tv_get_string_buf(tv1, buf1);
1674 s2 = tv_get_string_buf(tv2, buf2);
1675 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1676
1677 case VAR_FLOAT:
1678#ifdef FEAT_FLOAT
1679 return tv1->vval.v_float == tv2->vval.v_float;
1680#endif
1681 case VAR_JOB:
1682#ifdef FEAT_JOB_CHANNEL
1683 return tv1->vval.v_job == tv2->vval.v_job;
1684#endif
1685 case VAR_CHANNEL:
1686#ifdef FEAT_JOB_CHANNEL
1687 return tv1->vval.v_channel == tv2->vval.v_channel;
1688#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001689 case VAR_INSTR:
1690 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001691
1692 case VAR_PARTIAL:
1693 return tv1->vval.v_partial == tv2->vval.v_partial;
1694
1695 case VAR_FUNC:
1696 return tv1->vval.v_string == tv2->vval.v_string;
1697
1698 case VAR_UNKNOWN:
1699 case VAR_ANY:
1700 case VAR_VOID:
1701 break;
1702 }
1703
1704 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1705 // does not equal anything, not even itself.
1706 return FALSE;
1707}
1708
1709/*
1710 * Get an option value.
1711 * "arg" points to the '&' or '+' before the option name.
1712 * "arg" is advanced to character after the option name.
1713 * Return OK or FAIL.
1714 */
1715 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001716eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001717 char_u **arg,
1718 typval_T *rettv, // when NULL, only check if option exists
1719 int evaluate)
1720{
1721 char_u *option_end;
1722 long numval;
1723 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001724 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001725 int c;
1726 int working = (**arg == '+'); // has("+option")
1727 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001728 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001729
1730 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001731 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001732 if (option_end == NULL)
1733 {
1734 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001735 semsg(_(e_option_name_missing_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001736 return FAIL;
1737 }
1738
1739 if (!evaluate)
1740 {
1741 *arg = option_end;
1742 return OK;
1743 }
1744
1745 c = *option_end;
1746 *option_end = NUL;
1747 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001748 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001749
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001750 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001751 {
1752 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001753 semsg(_(e_unknown_option_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001754 ret = FAIL;
1755 }
1756 else if (rettv != NULL)
1757 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001758 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001759 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001760 {
1761 rettv->v_type = VAR_STRING;
1762 rettv->vval.v_string = NULL;
1763 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001764 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001765 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001766 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1767 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001768 rettv->vval.v_number = 0;
1769 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001770 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001771 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001772 if (in_vim9script() && opt_type == gov_bool)
1773 {
1774 rettv->v_type = VAR_BOOL;
1775 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1776 }
1777 else
1778 {
1779 rettv->v_type = VAR_NUMBER;
1780 rettv->vval.v_number = numval;
1781 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001782 }
1783 else // string option
1784 {
1785 rettv->v_type = VAR_STRING;
1786 rettv->vval.v_string = stringval;
1787 }
1788 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001789 else if (working && (opt_type == gov_hidden_bool
1790 || opt_type == gov_hidden_number
1791 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001792 ret = FAIL;
1793
1794 *option_end = c; // put back for error messages
1795 *arg = option_end;
1796
1797 return ret;
1798}
1799
1800/*
1801 * Allocate a variable for a number constant. Also deals with "0z" for blob.
1802 * Return OK or FAIL.
1803 */
1804 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001805eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001806 char_u **arg,
1807 typval_T *rettv,
1808 int evaluate,
1809 int want_string UNUSED)
1810{
1811 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02001812 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001813#ifdef FEAT_FLOAT
1814 char_u *p;
1815 int get_float = FALSE;
1816
1817 // We accept a float when the format matches
1818 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
1819 // strict to avoid backwards compatibility problems.
1820 // With script version 2 and later the leading digit can be
1821 // omitted.
1822 // Don't look for a float after the "." operator, so that
1823 // ":let vers = 1.2.3" doesn't fail.
1824 if (**arg == '.')
1825 p = *arg;
1826 else
Bram Moolenaar29500652021-08-08 15:43:34 +02001827 {
1828 p = *arg + 1;
1829 if (skip_quotes)
1830 for (;;)
1831 {
1832 if (*p == '\'')
1833 ++p;
1834 if (!vim_isdigit(*p))
1835 break;
1836 p = skipdigits(p);
1837 }
1838 else
1839 p = skipdigits(p);
1840 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001841 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
1842 {
1843 get_float = TRUE;
1844 p = skipdigits(p + 2);
1845 if (*p == 'e' || *p == 'E')
1846 {
1847 ++p;
1848 if (*p == '-' || *p == '+')
1849 ++p;
1850 if (!vim_isdigit(*p))
1851 get_float = FALSE;
1852 else
1853 p = skipdigits(p + 1);
1854 }
1855 if (ASCII_ISALPHA(*p) || *p == '.')
1856 get_float = FALSE;
1857 }
1858 if (get_float)
1859 {
1860 float_T f;
1861
Bram Moolenaar29500652021-08-08 15:43:34 +02001862 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001863 if (evaluate)
1864 {
1865 rettv->v_type = VAR_FLOAT;
1866 rettv->vval.v_float = f;
1867 }
1868 }
1869 else
1870#endif
1871 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
1872 {
1873 char_u *bp;
1874 blob_T *blob = NULL; // init for gcc
1875
1876 // Blob constant: 0z0123456789abcdef
1877 if (evaluate)
1878 blob = blob_alloc();
1879 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
1880 {
1881 if (!vim_isxdigit(bp[1]))
1882 {
1883 if (blob != NULL)
1884 {
1885 emsg(_("E973: Blob literal should have an even number of hex characters"));
1886 ga_clear(&blob->bv_ga);
1887 VIM_CLEAR(blob);
1888 }
1889 return FAIL;
1890 }
1891 if (blob != NULL)
1892 ga_append(&blob->bv_ga,
1893 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
1894 if (bp[2] == '.' && vim_isxdigit(bp[3]))
1895 ++bp;
1896 }
1897 if (blob != NULL)
1898 rettv_blob_set(rettv, blob);
1899 *arg = bp;
1900 }
1901 else
1902 {
1903 varnumber_T n;
1904
1905 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02001906 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001907 ? STR2NR_NO_OCT + STR2NR_QUOTE
1908 : STR2NR_ALL, &n, NULL, 0, TRUE);
1909 if (len == 0)
1910 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00001911 if (evaluate)
1912 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001913 return FAIL;
1914 }
1915 *arg += len;
1916 if (evaluate)
1917 {
1918 rettv->v_type = VAR_NUMBER;
1919 rettv->vval.v_number = n;
1920 }
1921 }
1922 return OK;
1923}
1924
1925/*
1926 * Allocate a variable for a string constant.
1927 * Return OK or FAIL.
1928 */
1929 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001930eval_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001931{
1932 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001933 char_u *end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001934 int extra = 0;
1935 int len;
1936
1937 // Find the end of the string, skipping backslashed characters.
1938 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
1939 {
1940 if (*p == '\\' && p[1] != NUL)
1941 {
1942 ++p;
1943 // A "\<x>" form occupies at least 4 characters, and produces up
1944 // to 21 characters (3 * 6 for the char and 3 for a modifier):
1945 // reserve space for 18 extra.
1946 // Each byte in the char could be encoded as K_SPECIAL K_EXTRA x.
1947 if (*p == '<')
1948 extra += 18;
1949 }
1950 }
1951
1952 if (*p != '"')
1953 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001954 semsg(_(e_missing_double_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001955 return FAIL;
1956 }
1957
1958 // If only parsing, set *arg and return here
1959 if (!evaluate)
1960 {
1961 *arg = p + 1;
1962 return OK;
1963 }
1964
1965 // Copy the string into allocated memory, handling backslashed
1966 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001967 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001968 len = (int)(p - *arg + extra);
1969 rettv->vval.v_string = alloc(len);
1970 if (rettv->vval.v_string == NULL)
1971 return FAIL;
1972 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001973
1974 for (p = *arg + 1; *p != NUL && *p != '"'; )
1975 {
1976 if (*p == '\\')
1977 {
1978 switch (*++p)
1979 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001980 case 'b': *end++ = BS; ++p; break;
1981 case 'e': *end++ = ESC; ++p; break;
1982 case 'f': *end++ = FF; ++p; break;
1983 case 'n': *end++ = NL; ++p; break;
1984 case 'r': *end++ = CAR; ++p; break;
1985 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001986
1987 case 'X': // hex: "\x1", "\x12"
1988 case 'x':
1989 case 'u': // Unicode: "\u0023"
1990 case 'U':
1991 if (vim_isxdigit(p[1]))
1992 {
1993 int n, nr;
1994 int c = toupper(*p);
1995
1996 if (c == 'X')
1997 n = 2;
1998 else if (*p == 'u')
1999 n = 4;
2000 else
2001 n = 8;
2002 nr = 0;
2003 while (--n >= 0 && vim_isxdigit(p[1]))
2004 {
2005 ++p;
2006 nr = (nr << 4) + hex2nr(*p);
2007 }
2008 ++p;
2009 // For "\u" store the number according to
2010 // 'encoding'.
2011 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002012 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002013 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002014 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002015 }
2016 break;
2017
2018 // octal: "\1", "\12", "\123"
2019 case '0':
2020 case '1':
2021 case '2':
2022 case '3':
2023 case '4':
2024 case '5':
2025 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002026 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002027 if (*p >= '0' && *p <= '7')
2028 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002029 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002030 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002031 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002032 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002033 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002034 break;
2035
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002036 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002037 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002038 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002039 int flags = FSK_KEYCODE | FSK_IN_STRING;
2040
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002041 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002042 flags |= FSK_SIMPLIFY;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002043 extra = trans_special(&p, end, flags, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002044 if (extra != 0)
2045 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002046 end += extra;
2047 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002048 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002049 break;
2050 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002051 }
2052 // FALLTHROUGH
2053
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002054 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002055 break;
2056 }
2057 }
2058 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002059 MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002060 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002061 *end = NUL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002062 if (*p != NUL) // just in case
2063 ++p;
2064 *arg = p;
2065
2066 return OK;
2067}
2068
2069/*
2070 * Allocate a variable for a 'str''ing' constant.
2071 * Return OK or FAIL.
2072 */
2073 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002074eval_lit_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002075{
2076 char_u *p;
2077 char_u *str;
2078 int reduce = 0;
2079
2080 // Find the end of the string, skipping ''.
2081 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
2082 {
2083 if (*p == '\'')
2084 {
2085 if (p[1] != '\'')
2086 break;
2087 ++reduce;
2088 ++p;
2089 }
2090 }
2091
2092 if (*p != '\'')
2093 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002094 semsg(_(e_missing_single_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002095 return FAIL;
2096 }
2097
2098 // If only parsing return after setting "*arg"
2099 if (!evaluate)
2100 {
2101 *arg = p + 1;
2102 return OK;
2103 }
2104
2105 // Copy the string into allocated memory, handling '' to ' reduction.
2106 str = alloc((p - *arg) - reduce);
2107 if (str == NULL)
2108 return FAIL;
2109 rettv->v_type = VAR_STRING;
2110 rettv->vval.v_string = str;
2111
2112 for (p = *arg + 1; *p != NUL; )
2113 {
2114 if (*p == '\'')
2115 {
2116 if (p[1] != '\'')
2117 break;
2118 ++p;
2119 }
2120 MB_COPY_CHAR(p, str);
2121 }
2122 *str = NUL;
2123 *arg = p + 1;
2124
2125 return OK;
2126}
2127
2128/*
2129 * Return a string with the string representation of a variable.
2130 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2131 * "numbuf" is used for a number.
2132 * Puts quotes around strings, so that they can be parsed back by eval().
2133 * May return NULL.
2134 */
2135 char_u *
2136tv2string(
2137 typval_T *tv,
2138 char_u **tofree,
2139 char_u *numbuf,
2140 int copyID)
2141{
2142 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2143}
2144
2145/*
2146 * Get the value of an environment variable.
2147 * "arg" is pointing to the '$'. It is advanced to after the name.
2148 * If the environment variable was not set, silently assume it is empty.
2149 * Return FAIL if the name is invalid.
2150 */
2151 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002152eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002153{
2154 char_u *string = NULL;
2155 int len;
2156 int cc;
2157 char_u *name;
2158 int mustfree = FALSE;
2159
2160 ++*arg;
2161 name = *arg;
2162 len = get_env_len(arg);
2163 if (evaluate)
2164 {
2165 if (len == 0)
2166 return FAIL; // invalid empty name
2167
2168 cc = name[len];
2169 name[len] = NUL;
2170 // first try vim_getenv(), fast for normal environment vars
2171 string = vim_getenv(name, &mustfree);
2172 if (string != NULL && *string != NUL)
2173 {
2174 if (!mustfree)
2175 string = vim_strsave(string);
2176 }
2177 else
2178 {
2179 if (mustfree)
2180 vim_free(string);
2181
2182 // next try expanding things like $VIM and ${HOME}
2183 string = expand_env_save(name - 1);
2184 if (string != NULL && *string == '$')
2185 VIM_CLEAR(string);
2186 }
2187 name[len] = cc;
2188
2189 rettv->v_type = VAR_STRING;
2190 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002191 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002192 }
2193
2194 return OK;
2195}
2196
2197/*
2198 * Get the lnum from the first argument.
2199 * Also accepts ".", "$", etc., but that only works for the current buffer.
2200 * Returns -1 on error.
2201 */
2202 linenr_T
2203tv_get_lnum(typval_T *argvars)
2204{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002205 linenr_T lnum = -1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002206
Bram Moolenaar56acb092020-08-16 14:48:19 +02002207 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2208 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002209 if (lnum <= 0 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002210 {
2211 int fnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002212 pos_T *fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002213
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002214 // no valid number, try using arg like line()
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002215 if (fp != NULL)
2216 lnum = fp->lnum;
2217 }
2218 return lnum;
2219}
2220
2221/*
2222 * Get the lnum from the first argument.
2223 * Also accepts "$", then "buf" is used.
2224 * Returns 0 on error.
2225 */
2226 linenr_T
2227tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2228{
2229 if (argvars[0].v_type == VAR_STRING
2230 && argvars[0].vval.v_string != NULL
2231 && argvars[0].vval.v_string[0] == '$'
2232 && buf != NULL)
2233 return buf->b_ml.ml_line_count;
2234 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2235}
2236
2237/*
2238 * Get buffer by number or pattern.
2239 */
2240 buf_T *
2241tv_get_buf(typval_T *tv, int curtab_only)
2242{
2243 char_u *name = tv->vval.v_string;
2244 buf_T *buf;
2245
2246 if (tv->v_type == VAR_NUMBER)
2247 return buflist_findnr((int)tv->vval.v_number);
2248 if (tv->v_type != VAR_STRING)
2249 return NULL;
2250 if (name == NULL || *name == NUL)
2251 return curbuf;
2252 if (name[0] == '$' && name[1] == NUL)
2253 return lastbuf;
2254
2255 buf = buflist_find_by_name(name, curtab_only);
2256
2257 // If not found, try expanding the name, like done for bufexists().
2258 if (buf == NULL)
2259 buf = find_buffer(tv);
2260
2261 return buf;
2262}
2263
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002264/*
2265 * Like tv_get_buf() but give an error message is the type is wrong.
2266 */
2267 buf_T *
2268tv_get_buf_from_arg(typval_T *tv)
2269{
2270 buf_T *buf;
2271
2272 ++emsg_off;
2273 buf = tv_get_buf(tv, FALSE);
2274 --emsg_off;
2275 if (buf == NULL
2276 && tv->v_type != VAR_NUMBER
2277 && tv->v_type != VAR_STRING)
2278 // issue errmsg for type error
2279 (void)tv_get_number(tv);
2280 return buf;
2281}
2282
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002283#endif // FEAT_EVAL