blob: 5e1c78c41cdd1f579a9c3cf6e1bc58b5b1fc2249 [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 {
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100362 if (idx >= 0)
363 semsg(_(e_string_required_for_argument_nr), idx + 1);
Bram Moolenaarf28f2ac2021-03-22 22:21:26 +0100364 else
365 emsg(_(e_stringreq));
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100366 return FAIL;
367 }
368 return OK;
369}
370
371/*
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100372 * Give an error and return FAIL unless "args[idx]" is a non-empty string.
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100373 */
374 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100375check_for_nonempty_string_arg(typval_T *args, int idx)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100376{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100377 if (check_for_string_arg(args, idx) == FAIL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100378 return FAIL;
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100379 if (args[idx].vval.v_string == NULL || *args[idx].vval.v_string == NUL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100380 {
Bram Moolenaare8e30782021-04-10 21:46:05 +0200381 semsg(_(e_non_empty_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100382 return FAIL;
383 }
384 return OK;
385}
386
387/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200388 * Check for an optional string argument at 'idx'
389 */
390 int
391check_for_opt_string_arg(typval_T *args, int idx)
392{
393 return (args[idx].v_type == VAR_UNKNOWN
394 || check_for_string_arg(args, idx) != FAIL);
395}
396
397/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200398 * Give an error and return FAIL unless "args[idx]" is a number.
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200399 */
400 int
401check_for_number_arg(typval_T *args, int idx)
402{
403 if (args[idx].v_type != VAR_NUMBER)
404 {
405 if (idx >= 0)
406 semsg(_(e_number_required_for_argument_nr), idx + 1);
407 else
408 emsg(_(e_numberreq));
409 return FAIL;
410 }
411 return OK;
412}
413
414/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200415 * Check for an optional number argument at 'idx'
416 */
417 int
418check_for_opt_number_arg(typval_T *args, int idx)
419{
420 return (args[idx].v_type == VAR_UNKNOWN
421 || check_for_number_arg(args, idx) != FAIL);
422}
423
424/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200425 * Give an error and return FAIL unless "args[idx]" is a float or a number.
426 */
427 int
428check_for_float_or_nr_arg(typval_T *args, int idx)
429{
430 if (args[idx].v_type != VAR_FLOAT && args[idx].v_type != VAR_NUMBER)
431 {
432 if (idx >= 0)
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200433 semsg(_(e_float_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200434 else
435 emsg(_(e_numberreq));
436 return FAIL;
437 }
438 return OK;
439}
440
441/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200442 * Give an error and return FAIL unless "args[idx]" is a bool.
443 */
444 int
445check_for_bool_arg(typval_T *args, int idx)
446{
447 if (args[idx].v_type != VAR_BOOL
448 && !(args[idx].v_type == VAR_NUMBER
449 && (args[idx].vval.v_number == 0
450 || args[idx].vval.v_number == 1)))
451 {
452 if (idx >= 0)
453 semsg(_(e_bool_required_for_argument_nr), idx + 1);
454 else
455 emsg(_(e_boolreq));
456 return FAIL;
457 }
458 return OK;
459}
460
461/*
Bram Moolenaara29856f2021-09-13 21:36:27 +0200462 * Check for an optional bool argument at 'idx'.
463 * Return FAIL if the type is wrong.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200464 */
465 int
466check_for_opt_bool_arg(typval_T *args, int idx)
467{
Bram Moolenaara29856f2021-09-13 21:36:27 +0200468 if (args[idx].v_type == VAR_UNKNOWN)
469 return OK;
470 return check_for_bool_arg(args, idx);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200471}
472
473/*
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200474 * Give an error and return FAIL unless "args[idx]" is a blob.
475 */
476 int
477check_for_blob_arg(typval_T *args, int idx)
478{
479 if (args[idx].v_type != VAR_BLOB)
480 {
481 if (idx >= 0)
482 semsg(_(e_blob_required_for_argument_nr), idx + 1);
483 else
484 emsg(_(e_blob_required));
485 return FAIL;
486 }
487 return OK;
488}
489
490/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200491 * Give an error and return FAIL unless "args[idx]" is a list.
492 */
493 int
494check_for_list_arg(typval_T *args, int idx)
495{
496 if (args[idx].v_type != VAR_LIST)
497 {
498 if (idx >= 0)
499 semsg(_(e_list_required_for_argument_nr), idx + 1);
500 else
501 emsg(_(e_listreq));
502 return FAIL;
503 }
504 return OK;
505}
506
507/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200508 * Check for an optional list argument at 'idx'
509 */
510 int
511check_for_opt_list_arg(typval_T *args, int idx)
512{
513 return (args[idx].v_type == VAR_UNKNOWN
514 || check_for_list_arg(args, idx) != FAIL);
515}
516
517/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200518 * Give an error and return FAIL unless "args[idx]" is a dict.
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200519 */
520 int
521check_for_dict_arg(typval_T *args, int idx)
522{
523 if (args[idx].v_type != VAR_DICT)
524 {
525 if (idx >= 0)
526 semsg(_(e_dict_required_for_argument_nr), idx + 1);
527 else
528 emsg(_(e_dictreq));
529 return FAIL;
530 }
531 return OK;
532}
533
534/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200535 * Check for an optional dict argument at 'idx'
536 */
537 int
538check_for_opt_dict_arg(typval_T *args, int idx)
539{
540 return (args[idx].v_type == VAR_UNKNOWN
541 || check_for_dict_arg(args, idx) != FAIL);
542}
543
544/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200545 * Give an error and return FAIL unless "args[idx]" is a channel or a job.
546 */
547 int
548check_for_chan_or_job_arg(typval_T *args, int idx)
549{
550 if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
551 {
552 if (idx >= 0)
553 semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
554 else
555 emsg(_(e_chan_or_job_req));
556 return FAIL;
557 }
558 return OK;
559}
560
561/*
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200562 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
563 * job.
564 */
565 int
566check_for_opt_chan_or_job_arg(typval_T *args, int idx)
567{
568 return (args[idx].v_type == VAR_UNKNOWN
569 || check_for_chan_or_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 job.
574 */
575 int
576check_for_job_arg(typval_T *args, int idx)
577{
578 if (args[idx].v_type != VAR_JOB)
579 {
580 if (idx >= 0)
581 semsg(_(e_job_required_for_argument_nr), idx + 1);
582 else
583 emsg(_(e_jobreq));
584 return FAIL;
585 }
586 return OK;
587}
588
589/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200590 * Check for an optional job argument at 'idx'.
591 */
592 int
593check_for_opt_job_arg(typval_T *args, int idx)
594{
595 return (args[idx].v_type == VAR_UNKNOWN
596 || check_for_job_arg(args, idx) != FAIL);
597}
598
599/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200600 * Give an error and return FAIL unless "args[idx]" is a string or
601 * a number.
602 */
603 int
604check_for_string_or_number_arg(typval_T *args, int idx)
605{
606 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
607 {
608 if (idx >= 0)
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200609 semsg(_(e_string_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200610 else
611 emsg(_(e_stringreq));
612 return FAIL;
613 }
614 return OK;
615}
616
617/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200618 * Check for an optional string or number argument at 'idx'.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200619 */
620 int
621check_for_opt_string_or_number_arg(typval_T *args, int idx)
622{
623 return (args[idx].v_type == VAR_UNKNOWN
624 || check_for_string_or_number_arg(args, idx) != FAIL);
625}
626
627/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200628 * Give an error and return FAIL unless "args[idx]" is a buffer number.
629 * Buffer number can be a number or a string.
630 */
631 int
632check_for_buffer_arg(typval_T *args, int idx)
633{
634 return check_for_string_or_number_arg(args, idx);
635}
636
637/*
638 * Check for an optional buffer argument at 'idx'
639 */
640 int
641check_for_opt_buffer_arg(typval_T *args, int idx)
642{
643 return (args[idx].v_type == VAR_UNKNOWN
644 || check_for_buffer_arg(args, idx));
645}
646
647/*
648 * Give an error and return FAIL unless "args[idx]" is a line number.
649 * Line number can be a number or a string.
650 */
651 int
652check_for_lnum_arg(typval_T *args, int idx)
653{
654 return check_for_string_or_number_arg(args, idx);
655}
656
657/*
658 * Check for an optional line number argument at 'idx'
659 */
660 int
661check_for_opt_lnum_arg(typval_T *args, int idx)
662{
663 return (args[idx].v_type == VAR_UNKNOWN
664 || check_for_lnum_arg(args, idx));
665}
666
667/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200668 * Give an error and return FAIL unless "args[idx]" is a string or a blob.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200669 */
670 int
671check_for_string_or_blob_arg(typval_T *args, int idx)
672{
673 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
674 {
675 if (idx >= 0)
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200676 semsg(_(e_string_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200677 else
678 emsg(_(e_stringreq));
679 return FAIL;
680 }
681 return OK;
682}
683
684/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200685 * Give an error and return FAIL unless "args[idx]" is a string or a list.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200686 */
687 int
688check_for_string_or_list_arg(typval_T *args, int idx)
689{
690 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
691 {
692 if (idx >= 0)
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200693 semsg(_(e_string_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200694 else
695 emsg(_(e_stringreq));
696 return FAIL;
697 }
698 return OK;
699}
700
701/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200702 * Check for an optional string or list argument at 'idx'
703 */
704 int
705check_for_opt_string_or_list_arg(typval_T *args, int idx)
706{
707 return (args[idx].v_type == VAR_UNKNOWN
708 || check_for_string_or_list_arg(args, idx));
709}
710
711/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200712 * Give an error and return FAIL unless "args[idx]" is a string or a dict.
713 */
714 int
715check_for_string_or_dict_arg(typval_T *args, int idx)
716{
717 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_DICT)
718 {
719 if (idx >= 0)
720 semsg(_(e_string_or_dict_required_for_argument_nr), idx + 1);
721 else
722 emsg(_(e_stringreq));
723 return FAIL;
724 }
725 return OK;
726}
727
728/*
729 * Give an error and return FAIL unless "args[idx]" is a string or a number
730 * or a list.
731 */
732 int
733check_for_string_or_number_or_list_arg(typval_T *args, int idx)
734{
735 if (args[idx].v_type != VAR_STRING
736 && args[idx].v_type != VAR_NUMBER
737 && args[idx].v_type != VAR_LIST)
738 {
739 if (idx >= 0)
Bram Moolenaar78db17c2021-07-31 19:12:58 +0200740 semsg(_(e_string_number_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200741 else
742 emsg(_(e_stringreq));
743 return FAIL;
744 }
745 return OK;
746}
747
748/*
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200749 * Give an error and return FAIL unless "args[idx]" is an optional string
750 * or number or a list
751 */
752 int
753check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx)
754{
755 return (args[idx].v_type == VAR_UNKNOWN
756 || check_for_string_or_number_or_list_arg(args, idx) != FAIL);
757}
758
759/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200760 * Give an error and return FAIL unless "args[idx]" is a string or a list
761 * or a dict.
762 */
763 int
764check_for_string_or_list_or_dict_arg(typval_T *args, int idx)
765{
766 if (args[idx].v_type != VAR_STRING
767 && args[idx].v_type != VAR_LIST
768 && args[idx].v_type != VAR_DICT)
769 {
770 if (idx >= 0)
Bram Moolenaar78db17c2021-07-31 19:12:58 +0200771 semsg(_(e_string_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200772 else
773 emsg(_(e_stringreq));
774 return FAIL;
775 }
776 return OK;
777}
778
779/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200780 * Give an error and return FAIL unless "args[idx]" is a list or a blob.
781 */
782 int
783check_for_list_or_blob_arg(typval_T *args, int idx)
784{
785 if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
786 {
787 if (idx >= 0)
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200788 semsg(_(e_list_or_blob_required_for_argument_nr), idx + 1);
789 else
790 emsg(_(e_listreq));
791 return FAIL;
792 }
793 return OK;
794}
795
796/*
797 * Give an error and return FAIL unless "args[idx]" is a list or dict
798 */
799 int
800check_for_list_or_dict_arg(typval_T *args, int idx)
801{
802 if (args[idx].v_type != VAR_LIST
803 && args[idx].v_type != VAR_DICT)
804 {
805 if (idx >= 0)
806 semsg(_(e_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200807 else
808 emsg(_(e_listreq));
809 return FAIL;
810 }
811 return OK;
812}
813
814/*
815 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
816 * blob.
817 */
818 int
819check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
820{
821 if (args[idx].v_type != VAR_LIST
822 && args[idx].v_type != VAR_DICT
823 && args[idx].v_type != VAR_BLOB)
824 {
825 if (idx >= 0)
Bram Moolenaar78db17c2021-07-31 19:12:58 +0200826 semsg(_(e_list_dict_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200827 else
828 emsg(_(e_listreq));
829 return FAIL;
830 }
831 return OK;
832}
833
834/*
Bram Moolenaar2d877592021-12-16 08:21:09 +0000835 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
836 * blob or a string.
837 */
838 int
839check_for_list_or_dict_or_blob_or_string_arg(typval_T *args, int idx)
840{
841 if (args[idx].v_type != VAR_LIST
842 && args[idx].v_type != VAR_DICT
843 && args[idx].v_type != VAR_BLOB
844 && args[idx].v_type != VAR_STRING)
845 {
846 if (idx >= 0)
847 semsg(_(e_list_dict_blob_or_string_required_for_argument_nr),
848 idx + 1);
849 else
850 emsg(_(e_listreq));
851 return FAIL;
852 }
853 return OK;
854}
855
856/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200857 * Give an error and return FAIL unless "args[idx]" is an optional buffer
858 * number or a dict.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200859 */
860 int
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200861check_for_opt_buffer_or_dict_arg(typval_T *args, int idx)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200862{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200863 if (args[idx].v_type != VAR_UNKNOWN
864 && args[idx].v_type != VAR_STRING
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200865 && args[idx].v_type != VAR_NUMBER
866 && args[idx].v_type != VAR_DICT)
867 {
868 if (idx >= 0)
869 semsg(_(e_string_required_for_argument_nr), idx + 1);
870 else
871 emsg(_(e_stringreq));
872 return FAIL;
873 }
874 return OK;
875}
876
877/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200878 * Get the string value of a variable.
879 * If it is a Number variable, the number is converted into a string.
880 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
881 * tv_get_string_buf() uses a given buffer.
882 * If the String variable has never been set, return an empty string.
883 * Never returns NULL;
884 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
885 * NULL on error.
886 */
887 char_u *
888tv_get_string(typval_T *varp)
889{
890 static char_u mybuf[NUMBUFLEN];
891
892 return tv_get_string_buf(varp, mybuf);
893}
894
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100895/*
896 * Like tv_get_string() but don't allow number to string conversion for Vim9.
897 */
898 char_u *
899tv_get_string_strict(typval_T *varp)
900{
901 static char_u mybuf[NUMBUFLEN];
902 char_u *res = tv_get_string_buf_chk_strict(
903 varp, mybuf, in_vim9script());
904
905 return res != NULL ? res : (char_u *)"";
906}
907
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200908 char_u *
909tv_get_string_buf(typval_T *varp, char_u *buf)
910{
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200911 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200912
913 return res != NULL ? res : (char_u *)"";
914}
915
916/*
917 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
918 */
919 char_u *
920tv_get_string_chk(typval_T *varp)
921{
922 static char_u mybuf[NUMBUFLEN];
923
924 return tv_get_string_buf_chk(varp, mybuf);
925}
926
927 char_u *
928tv_get_string_buf_chk(typval_T *varp, char_u *buf)
929{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100930 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
931}
932
933 char_u *
934tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
935{
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200936 switch (varp->v_type)
937 {
938 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100939 if (strict)
940 {
941 emsg(_(e_using_number_as_string));
942 break;
943 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200944 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
945 (varnumber_T)varp->vval.v_number);
946 return buf;
947 case VAR_FUNC:
948 case VAR_PARTIAL:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200949 emsg(_("E729: Using a Funcref as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200950 break;
951 case VAR_LIST:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200952 emsg(_("E730: Using a List as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200953 break;
954 case VAR_DICT:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200955 emsg(_("E731: Using a Dictionary as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200956 break;
957 case VAR_FLOAT:
958#ifdef FEAT_FLOAT
Bram Moolenaar7a2217b2021-06-06 12:33:49 +0200959 if (strict)
960 {
961 emsg(_(e_float_as_string));
962 break;
963 }
964 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
965 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200966#endif
967 case VAR_STRING:
968 if (varp->vval.v_string != NULL)
969 return varp->vval.v_string;
970 return (char_u *)"";
971 case VAR_BOOL:
972 case VAR_SPECIAL:
973 STRCPY(buf, get_var_special_name(varp->vval.v_number));
974 return buf;
975 case VAR_BLOB:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200976 emsg(_("E976: Using a Blob as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200977 break;
978 case VAR_JOB:
979#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200980 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200981 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200982 semsg(_(e_using_invalid_value_as_string_str), "job");
983 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200984 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200985 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200986#endif
987 break;
988 case VAR_CHANNEL:
989#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200990 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200991 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200992 semsg(_(e_using_invalid_value_as_string_str), "channel");
993 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200994 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200995 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200996#endif
997 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200998 case VAR_VOID:
999 emsg(_(e_cannot_use_void_value));
1000 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001001 case VAR_UNKNOWN:
1002 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001003 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +02001004 semsg(_(e_using_invalid_value_as_string_str),
1005 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001006 break;
1007 }
1008 return NULL;
1009}
1010
1011/*
1012 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
1013 * string() on Dict, List, etc.
1014 */
1015 char_u *
1016tv_stringify(typval_T *varp, char_u *buf)
1017{
1018 if (varp->v_type == VAR_LIST
1019 || varp->v_type == VAR_DICT
1020 || varp->v_type == VAR_BLOB
1021 || varp->v_type == VAR_FUNC
1022 || varp->v_type == VAR_PARTIAL
1023 || varp->v_type == VAR_FLOAT)
1024 {
1025 typval_T tmp;
1026
1027 f_string(varp, &tmp);
1028 tv_get_string_buf(&tmp, buf);
1029 clear_tv(varp);
1030 *varp = tmp;
1031 return tmp.vval.v_string;
1032 }
1033 return tv_get_string_buf(varp, buf);
1034}
1035
1036/*
1037 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
1038 * Also give an error message, using "name" or _("name") when use_gettext is
1039 * TRUE.
1040 */
1041 int
1042tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
1043{
1044 int lock = 0;
1045
1046 switch (tv->v_type)
1047 {
1048 case VAR_BLOB:
1049 if (tv->vval.v_blob != NULL)
1050 lock = tv->vval.v_blob->bv_lock;
1051 break;
1052 case VAR_LIST:
1053 if (tv->vval.v_list != NULL)
1054 lock = tv->vval.v_list->lv_lock;
1055 break;
1056 case VAR_DICT:
1057 if (tv->vval.v_dict != NULL)
1058 lock = tv->vval.v_dict->dv_lock;
1059 break;
1060 default:
1061 break;
1062 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001063 return value_check_lock(tv->v_lock, name, use_gettext)
1064 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001065}
1066
1067/*
1068 * Copy the values from typval_T "from" to typval_T "to".
1069 * When needed allocates string or increases reference count.
1070 * Does not make a copy of a list, blob or dict but copies the reference!
1071 * It is OK for "from" and "to" to point to the same item. This is used to
1072 * make a copy later.
1073 */
1074 void
1075copy_tv(typval_T *from, typval_T *to)
1076{
1077 to->v_type = from->v_type;
1078 to->v_lock = 0;
1079 switch (from->v_type)
1080 {
1081 case VAR_NUMBER:
1082 case VAR_BOOL:
1083 case VAR_SPECIAL:
1084 to->vval.v_number = from->vval.v_number;
1085 break;
1086 case VAR_FLOAT:
1087#ifdef FEAT_FLOAT
1088 to->vval.v_float = from->vval.v_float;
1089 break;
1090#endif
1091 case VAR_JOB:
1092#ifdef FEAT_JOB_CHANNEL
1093 to->vval.v_job = from->vval.v_job;
1094 if (to->vval.v_job != NULL)
1095 ++to->vval.v_job->jv_refcount;
1096 break;
1097#endif
1098 case VAR_CHANNEL:
1099#ifdef FEAT_JOB_CHANNEL
1100 to->vval.v_channel = from->vval.v_channel;
1101 if (to->vval.v_channel != NULL)
1102 ++to->vval.v_channel->ch_refcount;
1103 break;
1104#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001105 case VAR_INSTR:
1106 to->vval.v_instr = from->vval.v_instr;
1107 break;
1108
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001109 case VAR_STRING:
1110 case VAR_FUNC:
1111 if (from->vval.v_string == NULL)
1112 to->vval.v_string = NULL;
1113 else
1114 {
1115 to->vval.v_string = vim_strsave(from->vval.v_string);
1116 if (from->v_type == VAR_FUNC)
1117 func_ref(to->vval.v_string);
1118 }
1119 break;
1120 case VAR_PARTIAL:
1121 if (from->vval.v_partial == NULL)
1122 to->vval.v_partial = NULL;
1123 else
1124 {
1125 to->vval.v_partial = from->vval.v_partial;
1126 ++to->vval.v_partial->pt_refcount;
1127 }
1128 break;
1129 case VAR_BLOB:
1130 if (from->vval.v_blob == NULL)
1131 to->vval.v_blob = NULL;
1132 else
1133 {
1134 to->vval.v_blob = from->vval.v_blob;
1135 ++to->vval.v_blob->bv_refcount;
1136 }
1137 break;
1138 case VAR_LIST:
1139 if (from->vval.v_list == NULL)
1140 to->vval.v_list = NULL;
1141 else
1142 {
1143 to->vval.v_list = from->vval.v_list;
1144 ++to->vval.v_list->lv_refcount;
1145 }
1146 break;
1147 case VAR_DICT:
1148 if (from->vval.v_dict == NULL)
1149 to->vval.v_dict = NULL;
1150 else
1151 {
1152 to->vval.v_dict = from->vval.v_dict;
1153 ++to->vval.v_dict->dv_refcount;
1154 }
1155 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +02001156 case VAR_VOID:
1157 emsg(_(e_cannot_use_void_value));
1158 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001159 case VAR_UNKNOWN:
1160 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001161 internal_error_no_abort("copy_tv(UNKNOWN)");
1162 break;
1163 }
1164}
1165
1166/*
1167 * Compare "typ1" and "typ2". Put the result in "typ1".
1168 */
1169 int
1170typval_compare(
1171 typval_T *typ1, // first operand
1172 typval_T *typ2, // second operand
Bram Moolenaar657137c2021-01-09 15:45:23 +01001173 exprtype_T type, // operator
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001174 int ic) // ignore case
1175{
1176 int i;
1177 varnumber_T n1, n2;
1178 char_u *s1, *s2;
1179 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1180 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1181
1182 if (type_is && typ1->v_type != typ2->v_type)
1183 {
1184 // For "is" a different type always means FALSE, for "notis"
1185 // it means TRUE.
1186 n1 = (type == EXPR_ISNOT);
1187 }
1188 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
1189 {
1190 if (type_is)
1191 {
1192 n1 = (typ1->v_type == typ2->v_type
1193 && typ1->vval.v_blob == typ2->vval.v_blob);
1194 if (type == EXPR_ISNOT)
1195 n1 = !n1;
1196 }
1197 else if (typ1->v_type != typ2->v_type
1198 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1199 {
1200 if (typ1->v_type != typ2->v_type)
1201 emsg(_("E977: Can only compare Blob with Blob"));
1202 else
1203 emsg(_(e_invalblob));
1204 clear_tv(typ1);
1205 return FAIL;
1206 }
1207 else
1208 {
1209 // Compare two Blobs for being equal or unequal.
1210 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
1211 if (type == EXPR_NEQUAL)
1212 n1 = !n1;
1213 }
1214 }
1215 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
1216 {
1217 if (type_is)
1218 {
1219 n1 = (typ1->v_type == typ2->v_type
1220 && typ1->vval.v_list == typ2->vval.v_list);
1221 if (type == EXPR_ISNOT)
1222 n1 = !n1;
1223 }
1224 else if (typ1->v_type != typ2->v_type
1225 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1226 {
1227 if (typ1->v_type != typ2->v_type)
1228 emsg(_("E691: Can only compare List with List"));
1229 else
1230 emsg(_("E692: Invalid operation for List"));
1231 clear_tv(typ1);
1232 return FAIL;
1233 }
1234 else
1235 {
1236 // Compare two Lists for being equal or unequal.
1237 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
1238 ic, FALSE);
1239 if (type == EXPR_NEQUAL)
1240 n1 = !n1;
1241 }
1242 }
1243
1244 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
1245 {
1246 if (type_is)
1247 {
1248 n1 = (typ1->v_type == typ2->v_type
1249 && typ1->vval.v_dict == typ2->vval.v_dict);
1250 if (type == EXPR_ISNOT)
1251 n1 = !n1;
1252 }
1253 else if (typ1->v_type != typ2->v_type
1254 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1255 {
1256 if (typ1->v_type != typ2->v_type)
1257 emsg(_("E735: Can only compare Dictionary with Dictionary"));
1258 else
1259 emsg(_("E736: Invalid operation for Dictionary"));
1260 clear_tv(typ1);
1261 return FAIL;
1262 }
1263 else
1264 {
1265 // Compare two Dictionaries for being equal or unequal.
1266 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
1267 ic, FALSE);
1268 if (type == EXPR_NEQUAL)
1269 n1 = !n1;
1270 }
1271 }
1272
1273 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
1274 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
1275 {
1276 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1277 && type != EXPR_IS && type != EXPR_ISNOT)
1278 {
1279 emsg(_("E694: Invalid operation for Funcrefs"));
1280 clear_tv(typ1);
1281 return FAIL;
1282 }
1283 if ((typ1->v_type == VAR_PARTIAL
1284 && typ1->vval.v_partial == NULL)
1285 || (typ2->v_type == VAR_PARTIAL
1286 && typ2->vval.v_partial == NULL))
1287 // When both partials are NULL, then they are equal.
1288 // Otherwise they are not equal.
1289 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
1290 else if (type_is)
1291 {
1292 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
1293 // strings are considered the same if their value is
1294 // the same
1295 n1 = tv_equal(typ1, typ2, ic, FALSE);
1296 else if (typ1->v_type == VAR_PARTIAL
1297 && typ2->v_type == VAR_PARTIAL)
1298 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
1299 else
1300 n1 = FALSE;
1301 }
1302 else
1303 n1 = tv_equal(typ1, typ2, ic, FALSE);
1304 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1305 n1 = !n1;
1306 }
1307
1308#ifdef FEAT_FLOAT
1309 // If one of the two variables is a float, compare as a float.
1310 // When using "=~" or "!~", always compare as string.
1311 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
1312 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1313 {
1314 float_T f1, f2;
1315
1316 f1 = tv_get_float(typ1);
1317 f2 = tv_get_float(typ2);
1318 n1 = FALSE;
1319 switch (type)
1320 {
1321 case EXPR_IS:
1322 case EXPR_EQUAL: n1 = (f1 == f2); break;
1323 case EXPR_ISNOT:
1324 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1325 case EXPR_GREATER: n1 = (f1 > f2); break;
1326 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1327 case EXPR_SMALLER: n1 = (f1 < f2); break;
1328 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1329 case EXPR_UNKNOWN:
1330 case EXPR_MATCH:
1331 default: break; // avoid gcc warning
1332 }
1333 }
1334#endif
1335
1336 // If one of the two variables is a number, compare as a number.
1337 // When using "=~" or "!~", always compare as string.
1338 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
1339 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1340 {
1341 n1 = tv_get_number(typ1);
1342 n2 = tv_get_number(typ2);
1343 switch (type)
1344 {
1345 case EXPR_IS:
1346 case EXPR_EQUAL: n1 = (n1 == n2); break;
1347 case EXPR_ISNOT:
1348 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1349 case EXPR_GREATER: n1 = (n1 > n2); break;
1350 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1351 case EXPR_SMALLER: n1 = (n1 < n2); break;
1352 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1353 case EXPR_UNKNOWN:
1354 case EXPR_MATCH:
1355 default: break; // avoid gcc warning
1356 }
1357 }
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001358 else if (in_vim9script() && (typ1->v_type == VAR_BOOL
Bram Moolenaar0c357522021-07-18 14:43:43 +02001359 || typ2->v_type == VAR_BOOL
1360 || (typ1->v_type == VAR_SPECIAL
1361 && typ2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001362 {
1363 if (typ1->v_type != typ2->v_type)
1364 {
1365 semsg(_(e_cannot_compare_str_with_str),
1366 vartype_name(typ1->v_type), vartype_name(typ2->v_type));
1367 clear_tv(typ1);
1368 return FAIL;
1369 }
1370 n1 = typ1->vval.v_number;
1371 n2 = typ2->vval.v_number;
1372 switch (type)
1373 {
1374 case EXPR_IS:
1375 case EXPR_EQUAL: n1 = (n1 == n2); break;
1376 case EXPR_ISNOT:
1377 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1378 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001379 semsg(_(e_invalid_operation_for_str),
1380 vartype_name(typ1->v_type));
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001381 clear_tv(typ1);
1382 return FAIL;
1383 }
1384 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001385 else
1386 {
Bram Moolenaar0c357522021-07-18 14:43:43 +02001387 if (in_vim9script()
1388 && ((typ1->v_type != VAR_STRING && typ1->v_type != VAR_SPECIAL)
1389 || (typ2->v_type != VAR_STRING && typ2->v_type != VAR_SPECIAL)))
1390 {
1391 semsg(_(e_cannot_compare_str_with_str),
1392 vartype_name(typ1->v_type), vartype_name(typ2->v_type));
1393 clear_tv(typ1);
1394 return FAIL;
1395 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001396 s1 = tv_get_string_buf(typ1, buf1);
1397 s2 = tv_get_string_buf(typ2, buf2);
1398 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1399 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1400 else
1401 i = 0;
1402 n1 = FALSE;
1403 switch (type)
1404 {
1405 case EXPR_IS:
1406 case EXPR_EQUAL: n1 = (i == 0); break;
1407 case EXPR_ISNOT:
1408 case EXPR_NEQUAL: n1 = (i != 0); break;
1409 case EXPR_GREATER: n1 = (i > 0); break;
1410 case EXPR_GEQUAL: n1 = (i >= 0); break;
1411 case EXPR_SMALLER: n1 = (i < 0); break;
1412 case EXPR_SEQUAL: n1 = (i <= 0); break;
1413
1414 case EXPR_MATCH:
1415 case EXPR_NOMATCH:
1416 n1 = pattern_match(s2, s1, ic);
1417 if (type == EXPR_NOMATCH)
1418 n1 = !n1;
1419 break;
1420
1421 default: break; // avoid gcc warning
1422 }
1423 }
1424 clear_tv(typ1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001425 if (in_vim9script())
1426 {
1427 typ1->v_type = VAR_BOOL;
1428 typ1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
1429 }
1430 else
1431 {
1432 typ1->v_type = VAR_NUMBER;
1433 typ1->vval.v_number = n1;
1434 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001435
1436 return OK;
1437}
1438
Bram Moolenaar34453202021-01-31 13:08:38 +01001439/*
1440 * Convert any type to a string, never give an error.
1441 * When "quotes" is TRUE add quotes to a string.
1442 * Returns an allocated string.
1443 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001444 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001445typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001446{
1447 char_u *tofree;
1448 char_u numbuf[NUMBUFLEN];
1449 char_u *ret = NULL;
1450
1451 if (arg == NULL)
1452 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001453 if (!quotes && arg->v_type == VAR_STRING)
1454 {
1455 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1456 : arg->vval.v_string);
1457 }
1458 else
1459 {
1460 ret = tv2string(arg, &tofree, numbuf, 0);
1461 // Make a copy if we have a value but it's not in allocated memory.
1462 if (ret != NULL && tofree == NULL)
1463 ret = vim_strsave(ret);
1464 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001465 return ret;
1466}
1467
1468/*
1469 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1470 * or it refers to a List or Dictionary that is locked.
1471 */
1472 int
1473tv_islocked(typval_T *tv)
1474{
1475 return (tv->v_lock & VAR_LOCKED)
1476 || (tv->v_type == VAR_LIST
1477 && tv->vval.v_list != NULL
1478 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1479 || (tv->v_type == VAR_DICT
1480 && tv->vval.v_dict != NULL
1481 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1482}
1483
1484 static int
1485func_equal(
1486 typval_T *tv1,
1487 typval_T *tv2,
1488 int ic) // ignore case
1489{
1490 char_u *s1, *s2;
1491 dict_T *d1, *d2;
1492 int a1, a2;
1493 int i;
1494
1495 // empty and NULL function name considered the same
1496 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1497 : partial_name(tv1->vval.v_partial);
1498 if (s1 != NULL && *s1 == NUL)
1499 s1 = NULL;
1500 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1501 : partial_name(tv2->vval.v_partial);
1502 if (s2 != NULL && *s2 == NUL)
1503 s2 = NULL;
1504 if (s1 == NULL || s2 == NULL)
1505 {
1506 if (s1 != s2)
1507 return FALSE;
1508 }
1509 else if (STRCMP(s1, s2) != 0)
1510 return FALSE;
1511
1512 // empty dict and NULL dict is different
1513 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1514 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1515 if (d1 == NULL || d2 == NULL)
1516 {
1517 if (d1 != d2)
1518 return FALSE;
1519 }
1520 else if (!dict_equal(d1, d2, ic, TRUE))
1521 return FALSE;
1522
1523 // empty list and no list considered the same
1524 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1525 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1526 if (a1 != a2)
1527 return FALSE;
1528 for (i = 0; i < a1; ++i)
1529 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1530 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1531 return FALSE;
1532
1533 return TRUE;
1534}
1535
1536/*
1537 * Return TRUE if "tv1" and "tv2" have the same value.
1538 * Compares the items just like "==" would compare them, but strings and
1539 * numbers are different. Floats and numbers are also different.
1540 */
1541 int
1542tv_equal(
1543 typval_T *tv1,
1544 typval_T *tv2,
1545 int ic, // ignore case
1546 int recursive) // TRUE when used recursively
1547{
1548 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1549 char_u *s1, *s2;
1550 static int recursive_cnt = 0; // catch recursive loops
1551 int r;
1552 static int tv_equal_recurse_limit;
1553
1554 // Catch lists and dicts that have an endless loop by limiting
1555 // recursiveness to a limit. We guess they are equal then.
1556 // A fixed limit has the problem of still taking an awful long time.
1557 // Reduce the limit every time running into it. That should work fine for
1558 // deeply linked structures that are not recursively linked and catch
1559 // recursiveness quickly.
1560 if (!recursive)
1561 tv_equal_recurse_limit = 1000;
1562 if (recursive_cnt >= tv_equal_recurse_limit)
1563 {
1564 --tv_equal_recurse_limit;
1565 return TRUE;
1566 }
1567
1568 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1569 // arguments.
1570 if ((tv1->v_type == VAR_FUNC
1571 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1572 && (tv2->v_type == VAR_FUNC
1573 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1574 {
1575 ++recursive_cnt;
1576 r = func_equal(tv1, tv2, ic);
1577 --recursive_cnt;
1578 return r;
1579 }
1580
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001581 if (tv1->v_type != tv2->v_type
1582 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1583 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001584 return FALSE;
1585
1586 switch (tv1->v_type)
1587 {
1588 case VAR_LIST:
1589 ++recursive_cnt;
1590 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1591 --recursive_cnt;
1592 return r;
1593
1594 case VAR_DICT:
1595 ++recursive_cnt;
1596 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1597 --recursive_cnt;
1598 return r;
1599
1600 case VAR_BLOB:
1601 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1602
1603 case VAR_NUMBER:
1604 case VAR_BOOL:
1605 case VAR_SPECIAL:
1606 return tv1->vval.v_number == tv2->vval.v_number;
1607
1608 case VAR_STRING:
1609 s1 = tv_get_string_buf(tv1, buf1);
1610 s2 = tv_get_string_buf(tv2, buf2);
1611 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1612
1613 case VAR_FLOAT:
1614#ifdef FEAT_FLOAT
1615 return tv1->vval.v_float == tv2->vval.v_float;
1616#endif
1617 case VAR_JOB:
1618#ifdef FEAT_JOB_CHANNEL
1619 return tv1->vval.v_job == tv2->vval.v_job;
1620#endif
1621 case VAR_CHANNEL:
1622#ifdef FEAT_JOB_CHANNEL
1623 return tv1->vval.v_channel == tv2->vval.v_channel;
1624#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001625 case VAR_INSTR:
1626 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001627
1628 case VAR_PARTIAL:
1629 return tv1->vval.v_partial == tv2->vval.v_partial;
1630
1631 case VAR_FUNC:
1632 return tv1->vval.v_string == tv2->vval.v_string;
1633
1634 case VAR_UNKNOWN:
1635 case VAR_ANY:
1636 case VAR_VOID:
1637 break;
1638 }
1639
1640 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1641 // does not equal anything, not even itself.
1642 return FALSE;
1643}
1644
1645/*
1646 * Get an option value.
1647 * "arg" points to the '&' or '+' before the option name.
1648 * "arg" is advanced to character after the option name.
1649 * Return OK or FAIL.
1650 */
1651 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001652eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001653 char_u **arg,
1654 typval_T *rettv, // when NULL, only check if option exists
1655 int evaluate)
1656{
1657 char_u *option_end;
1658 long numval;
1659 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001660 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001661 int c;
1662 int working = (**arg == '+'); // has("+option")
1663 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001664 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001665
1666 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001667 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001668 if (option_end == NULL)
1669 {
1670 if (rettv != NULL)
1671 semsg(_("E112: Option name missing: %s"), *arg);
1672 return FAIL;
1673 }
1674
1675 if (!evaluate)
1676 {
1677 *arg = option_end;
1678 return OK;
1679 }
1680
1681 c = *option_end;
1682 *option_end = NUL;
1683 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001684 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001685
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001686 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001687 {
1688 if (rettv != NULL)
1689 semsg(_(e_unknown_option), *arg);
1690 ret = FAIL;
1691 }
1692 else if (rettv != NULL)
1693 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001694 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001695 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001696 {
1697 rettv->v_type = VAR_STRING;
1698 rettv->vval.v_string = NULL;
1699 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001700 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001701 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001702 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1703 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001704 rettv->vval.v_number = 0;
1705 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001706 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001707 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001708 if (in_vim9script() && opt_type == gov_bool)
1709 {
1710 rettv->v_type = VAR_BOOL;
1711 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1712 }
1713 else
1714 {
1715 rettv->v_type = VAR_NUMBER;
1716 rettv->vval.v_number = numval;
1717 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001718 }
1719 else // string option
1720 {
1721 rettv->v_type = VAR_STRING;
1722 rettv->vval.v_string = stringval;
1723 }
1724 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001725 else if (working && (opt_type == gov_hidden_bool
1726 || opt_type == gov_hidden_number
1727 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001728 ret = FAIL;
1729
1730 *option_end = c; // put back for error messages
1731 *arg = option_end;
1732
1733 return ret;
1734}
1735
1736/*
1737 * Allocate a variable for a number constant. Also deals with "0z" for blob.
1738 * Return OK or FAIL.
1739 */
1740 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001741eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001742 char_u **arg,
1743 typval_T *rettv,
1744 int evaluate,
1745 int want_string UNUSED)
1746{
1747 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02001748 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001749#ifdef FEAT_FLOAT
1750 char_u *p;
1751 int get_float = FALSE;
1752
1753 // We accept a float when the format matches
1754 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
1755 // strict to avoid backwards compatibility problems.
1756 // With script version 2 and later the leading digit can be
1757 // omitted.
1758 // Don't look for a float after the "." operator, so that
1759 // ":let vers = 1.2.3" doesn't fail.
1760 if (**arg == '.')
1761 p = *arg;
1762 else
Bram Moolenaar29500652021-08-08 15:43:34 +02001763 {
1764 p = *arg + 1;
1765 if (skip_quotes)
1766 for (;;)
1767 {
1768 if (*p == '\'')
1769 ++p;
1770 if (!vim_isdigit(*p))
1771 break;
1772 p = skipdigits(p);
1773 }
1774 else
1775 p = skipdigits(p);
1776 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001777 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
1778 {
1779 get_float = TRUE;
1780 p = skipdigits(p + 2);
1781 if (*p == 'e' || *p == 'E')
1782 {
1783 ++p;
1784 if (*p == '-' || *p == '+')
1785 ++p;
1786 if (!vim_isdigit(*p))
1787 get_float = FALSE;
1788 else
1789 p = skipdigits(p + 1);
1790 }
1791 if (ASCII_ISALPHA(*p) || *p == '.')
1792 get_float = FALSE;
1793 }
1794 if (get_float)
1795 {
1796 float_T f;
1797
Bram Moolenaar29500652021-08-08 15:43:34 +02001798 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001799 if (evaluate)
1800 {
1801 rettv->v_type = VAR_FLOAT;
1802 rettv->vval.v_float = f;
1803 }
1804 }
1805 else
1806#endif
1807 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
1808 {
1809 char_u *bp;
1810 blob_T *blob = NULL; // init for gcc
1811
1812 // Blob constant: 0z0123456789abcdef
1813 if (evaluate)
1814 blob = blob_alloc();
1815 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
1816 {
1817 if (!vim_isxdigit(bp[1]))
1818 {
1819 if (blob != NULL)
1820 {
1821 emsg(_("E973: Blob literal should have an even number of hex characters"));
1822 ga_clear(&blob->bv_ga);
1823 VIM_CLEAR(blob);
1824 }
1825 return FAIL;
1826 }
1827 if (blob != NULL)
1828 ga_append(&blob->bv_ga,
1829 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
1830 if (bp[2] == '.' && vim_isxdigit(bp[3]))
1831 ++bp;
1832 }
1833 if (blob != NULL)
1834 rettv_blob_set(rettv, blob);
1835 *arg = bp;
1836 }
1837 else
1838 {
1839 varnumber_T n;
1840
1841 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02001842 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001843 ? STR2NR_NO_OCT + STR2NR_QUOTE
1844 : STR2NR_ALL, &n, NULL, 0, TRUE);
1845 if (len == 0)
1846 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00001847 if (evaluate)
1848 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001849 return FAIL;
1850 }
1851 *arg += len;
1852 if (evaluate)
1853 {
1854 rettv->v_type = VAR_NUMBER;
1855 rettv->vval.v_number = n;
1856 }
1857 }
1858 return OK;
1859}
1860
1861/*
1862 * Allocate a variable for a string constant.
1863 * Return OK or FAIL.
1864 */
1865 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001866eval_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001867{
1868 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001869 char_u *end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001870 int extra = 0;
1871 int len;
1872
1873 // Find the end of the string, skipping backslashed characters.
1874 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
1875 {
1876 if (*p == '\\' && p[1] != NUL)
1877 {
1878 ++p;
1879 // A "\<x>" form occupies at least 4 characters, and produces up
1880 // to 21 characters (3 * 6 for the char and 3 for a modifier):
1881 // reserve space for 18 extra.
1882 // Each byte in the char could be encoded as K_SPECIAL K_EXTRA x.
1883 if (*p == '<')
1884 extra += 18;
1885 }
1886 }
1887
1888 if (*p != '"')
1889 {
1890 semsg(_("E114: Missing quote: %s"), *arg);
1891 return FAIL;
1892 }
1893
1894 // If only parsing, set *arg and return here
1895 if (!evaluate)
1896 {
1897 *arg = p + 1;
1898 return OK;
1899 }
1900
1901 // Copy the string into allocated memory, handling backslashed
1902 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001903 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001904 len = (int)(p - *arg + extra);
1905 rettv->vval.v_string = alloc(len);
1906 if (rettv->vval.v_string == NULL)
1907 return FAIL;
1908 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001909
1910 for (p = *arg + 1; *p != NUL && *p != '"'; )
1911 {
1912 if (*p == '\\')
1913 {
1914 switch (*++p)
1915 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001916 case 'b': *end++ = BS; ++p; break;
1917 case 'e': *end++ = ESC; ++p; break;
1918 case 'f': *end++ = FF; ++p; break;
1919 case 'n': *end++ = NL; ++p; break;
1920 case 'r': *end++ = CAR; ++p; break;
1921 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001922
1923 case 'X': // hex: "\x1", "\x12"
1924 case 'x':
1925 case 'u': // Unicode: "\u0023"
1926 case 'U':
1927 if (vim_isxdigit(p[1]))
1928 {
1929 int n, nr;
1930 int c = toupper(*p);
1931
1932 if (c == 'X')
1933 n = 2;
1934 else if (*p == 'u')
1935 n = 4;
1936 else
1937 n = 8;
1938 nr = 0;
1939 while (--n >= 0 && vim_isxdigit(p[1]))
1940 {
1941 ++p;
1942 nr = (nr << 4) + hex2nr(*p);
1943 }
1944 ++p;
1945 // For "\u" store the number according to
1946 // 'encoding'.
1947 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001948 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001949 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001950 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001951 }
1952 break;
1953
1954 // octal: "\1", "\12", "\123"
1955 case '0':
1956 case '1':
1957 case '2':
1958 case '3':
1959 case '4':
1960 case '5':
1961 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001962 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001963 if (*p >= '0' && *p <= '7')
1964 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001965 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001966 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001967 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001968 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001969 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001970 break;
1971
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001972 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001973 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001974 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001975 int flags = FSK_KEYCODE | FSK_IN_STRING;
1976
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001977 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001978 flags |= FSK_SIMPLIFY;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001979 extra = trans_special(&p, end, flags, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001980 if (extra != 0)
1981 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001982 end += extra;
1983 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001984 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001985 break;
1986 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001987 }
1988 // FALLTHROUGH
1989
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001990 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001991 break;
1992 }
1993 }
1994 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001995 MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001996 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001997 *end = NUL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001998 if (*p != NUL) // just in case
1999 ++p;
2000 *arg = p;
2001
2002 return OK;
2003}
2004
2005/*
2006 * Allocate a variable for a 'str''ing' constant.
2007 * Return OK or FAIL.
2008 */
2009 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002010eval_lit_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002011{
2012 char_u *p;
2013 char_u *str;
2014 int reduce = 0;
2015
2016 // Find the end of the string, skipping ''.
2017 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
2018 {
2019 if (*p == '\'')
2020 {
2021 if (p[1] != '\'')
2022 break;
2023 ++reduce;
2024 ++p;
2025 }
2026 }
2027
2028 if (*p != '\'')
2029 {
2030 semsg(_("E115: Missing quote: %s"), *arg);
2031 return FAIL;
2032 }
2033
2034 // If only parsing return after setting "*arg"
2035 if (!evaluate)
2036 {
2037 *arg = p + 1;
2038 return OK;
2039 }
2040
2041 // Copy the string into allocated memory, handling '' to ' reduction.
2042 str = alloc((p - *arg) - reduce);
2043 if (str == NULL)
2044 return FAIL;
2045 rettv->v_type = VAR_STRING;
2046 rettv->vval.v_string = str;
2047
2048 for (p = *arg + 1; *p != NUL; )
2049 {
2050 if (*p == '\'')
2051 {
2052 if (p[1] != '\'')
2053 break;
2054 ++p;
2055 }
2056 MB_COPY_CHAR(p, str);
2057 }
2058 *str = NUL;
2059 *arg = p + 1;
2060
2061 return OK;
2062}
2063
2064/*
2065 * Return a string with the string representation of a variable.
2066 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2067 * "numbuf" is used for a number.
2068 * Puts quotes around strings, so that they can be parsed back by eval().
2069 * May return NULL.
2070 */
2071 char_u *
2072tv2string(
2073 typval_T *tv,
2074 char_u **tofree,
2075 char_u *numbuf,
2076 int copyID)
2077{
2078 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2079}
2080
2081/*
2082 * Get the value of an environment variable.
2083 * "arg" is pointing to the '$'. It is advanced to after the name.
2084 * If the environment variable was not set, silently assume it is empty.
2085 * Return FAIL if the name is invalid.
2086 */
2087 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002088eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002089{
2090 char_u *string = NULL;
2091 int len;
2092 int cc;
2093 char_u *name;
2094 int mustfree = FALSE;
2095
2096 ++*arg;
2097 name = *arg;
2098 len = get_env_len(arg);
2099 if (evaluate)
2100 {
2101 if (len == 0)
2102 return FAIL; // invalid empty name
2103
2104 cc = name[len];
2105 name[len] = NUL;
2106 // first try vim_getenv(), fast for normal environment vars
2107 string = vim_getenv(name, &mustfree);
2108 if (string != NULL && *string != NUL)
2109 {
2110 if (!mustfree)
2111 string = vim_strsave(string);
2112 }
2113 else
2114 {
2115 if (mustfree)
2116 vim_free(string);
2117
2118 // next try expanding things like $VIM and ${HOME}
2119 string = expand_env_save(name - 1);
2120 if (string != NULL && *string == '$')
2121 VIM_CLEAR(string);
2122 }
2123 name[len] = cc;
2124
2125 rettv->v_type = VAR_STRING;
2126 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002127 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002128 }
2129
2130 return OK;
2131}
2132
2133/*
2134 * Get the lnum from the first argument.
2135 * Also accepts ".", "$", etc., but that only works for the current buffer.
2136 * Returns -1 on error.
2137 */
2138 linenr_T
2139tv_get_lnum(typval_T *argvars)
2140{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002141 linenr_T lnum = -1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002142
Bram Moolenaar56acb092020-08-16 14:48:19 +02002143 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2144 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002145 if (lnum <= 0 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002146 {
2147 int fnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002148 pos_T *fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002149
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002150 // no valid number, try using arg like line()
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002151 if (fp != NULL)
2152 lnum = fp->lnum;
2153 }
2154 return lnum;
2155}
2156
2157/*
2158 * Get the lnum from the first argument.
2159 * Also accepts "$", then "buf" is used.
2160 * Returns 0 on error.
2161 */
2162 linenr_T
2163tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2164{
2165 if (argvars[0].v_type == VAR_STRING
2166 && argvars[0].vval.v_string != NULL
2167 && argvars[0].vval.v_string[0] == '$'
2168 && buf != NULL)
2169 return buf->b_ml.ml_line_count;
2170 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2171}
2172
2173/*
2174 * Get buffer by number or pattern.
2175 */
2176 buf_T *
2177tv_get_buf(typval_T *tv, int curtab_only)
2178{
2179 char_u *name = tv->vval.v_string;
2180 buf_T *buf;
2181
2182 if (tv->v_type == VAR_NUMBER)
2183 return buflist_findnr((int)tv->vval.v_number);
2184 if (tv->v_type != VAR_STRING)
2185 return NULL;
2186 if (name == NULL || *name == NUL)
2187 return curbuf;
2188 if (name[0] == '$' && name[1] == NUL)
2189 return lastbuf;
2190
2191 buf = buflist_find_by_name(name, curtab_only);
2192
2193 // If not found, try expanding the name, like done for bufexists().
2194 if (buf == NULL)
2195 buf = find_buffer(tv);
2196
2197 return buf;
2198}
2199
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002200/*
2201 * Like tv_get_buf() but give an error message is the type is wrong.
2202 */
2203 buf_T *
2204tv_get_buf_from_arg(typval_T *tv)
2205{
2206 buf_T *buf;
2207
2208 ++emsg_off;
2209 buf = tv_get_buf(tv, FALSE);
2210 --emsg_off;
2211 if (buf == NULL
2212 && tv->v_type != VAR_NUMBER
2213 && tv->v_type != VAR_STRING)
2214 // issue errmsg for type error
2215 (void)tv_get_number(tv);
2216 return buf;
2217}
2218
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002219#endif // FEAT_EVAL