blob: 5fb98eb6733ab618aed7b8cb11dda7b5a075c03a [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/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200462 * Check for an optional bool argument at 'idx'
463 */
464 int
465check_for_opt_bool_arg(typval_T *args, int idx)
466{
467 return (args[idx].v_type == VAR_UNKNOWN
468 || check_for_bool_arg(args, idx) != FAIL);
469}
470
471/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200472 * Give an error and return FAIL unless "args[idx]" is a list.
473 */
474 int
475check_for_list_arg(typval_T *args, int idx)
476{
477 if (args[idx].v_type != VAR_LIST)
478 {
479 if (idx >= 0)
480 semsg(_(e_list_required_for_argument_nr), idx + 1);
481 else
482 emsg(_(e_listreq));
483 return FAIL;
484 }
485 return OK;
486}
487
488/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200489 * Check for an optional list argument at 'idx'
490 */
491 int
492check_for_opt_list_arg(typval_T *args, int idx)
493{
494 return (args[idx].v_type == VAR_UNKNOWN
495 || check_for_list_arg(args, idx) != FAIL);
496}
497
498/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200499 * Give an error and return FAIL unless "args[idx]" is a dict.
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200500 */
501 int
502check_for_dict_arg(typval_T *args, int idx)
503{
504 if (args[idx].v_type != VAR_DICT)
505 {
506 if (idx >= 0)
507 semsg(_(e_dict_required_for_argument_nr), idx + 1);
508 else
509 emsg(_(e_dictreq));
510 return FAIL;
511 }
512 return OK;
513}
514
515/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200516 * Check for an optional dict argument at 'idx'
517 */
518 int
519check_for_opt_dict_arg(typval_T *args, int idx)
520{
521 return (args[idx].v_type == VAR_UNKNOWN
522 || check_for_dict_arg(args, idx) != FAIL);
523}
524
525/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200526 * Give an error and return FAIL unless "args[idx]" is a channel or a job.
527 */
528 int
529check_for_chan_or_job_arg(typval_T *args, int idx)
530{
531 if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
532 {
533 if (idx >= 0)
534 semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
535 else
536 emsg(_(e_chan_or_job_req));
537 return FAIL;
538 }
539 return OK;
540}
541
542/*
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200543 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
544 * job.
545 */
546 int
547check_for_opt_chan_or_job_arg(typval_T *args, int idx)
548{
549 return (args[idx].v_type == VAR_UNKNOWN
550 || check_for_chan_or_job_arg(args, idx) != FAIL);
551}
552
553/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200554 * Give an error and return FAIL unless "args[idx]" is a job.
555 */
556 int
557check_for_job_arg(typval_T *args, int idx)
558{
559 if (args[idx].v_type != VAR_JOB)
560 {
561 if (idx >= 0)
562 semsg(_(e_job_required_for_argument_nr), idx + 1);
563 else
564 emsg(_(e_jobreq));
565 return FAIL;
566 }
567 return OK;
568}
569
570/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200571 * Check for an optional job argument at 'idx'.
572 */
573 int
574check_for_opt_job_arg(typval_T *args, int idx)
575{
576 return (args[idx].v_type == VAR_UNKNOWN
577 || check_for_job_arg(args, idx) != FAIL);
578}
579
580/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200581 * Give an error and return FAIL unless "args[idx]" is a string or
582 * a number.
583 */
584 int
585check_for_string_or_number_arg(typval_T *args, int idx)
586{
587 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
588 {
589 if (idx >= 0)
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200590 semsg(_(e_string_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200591 else
592 emsg(_(e_stringreq));
593 return FAIL;
594 }
595 return OK;
596}
597
598/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200599 * Check for an optional string or number argument at 'idx'.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200600 */
601 int
602check_for_opt_string_or_number_arg(typval_T *args, int idx)
603{
604 return (args[idx].v_type == VAR_UNKNOWN
605 || check_for_string_or_number_arg(args, idx) != FAIL);
606}
607
608/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200609 * Give an error and return FAIL unless "args[idx]" is a buffer number.
610 * Buffer number can be a number or a string.
611 */
612 int
613check_for_buffer_arg(typval_T *args, int idx)
614{
615 return check_for_string_or_number_arg(args, idx);
616}
617
618/*
619 * Check for an optional buffer argument at 'idx'
620 */
621 int
622check_for_opt_buffer_arg(typval_T *args, int idx)
623{
624 return (args[idx].v_type == VAR_UNKNOWN
625 || check_for_buffer_arg(args, idx));
626}
627
628/*
629 * Give an error and return FAIL unless "args[idx]" is a line number.
630 * Line number can be a number or a string.
631 */
632 int
633check_for_lnum_arg(typval_T *args, int idx)
634{
635 return check_for_string_or_number_arg(args, idx);
636}
637
638/*
639 * Check for an optional line number argument at 'idx'
640 */
641 int
642check_for_opt_lnum_arg(typval_T *args, int idx)
643{
644 return (args[idx].v_type == VAR_UNKNOWN
645 || check_for_lnum_arg(args, idx));
646}
647
648/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200649 * Give an error and return FAIL unless "args[idx]" is a string or a blob.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200650 */
651 int
652check_for_string_or_blob_arg(typval_T *args, int idx)
653{
654 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
655 {
656 if (idx >= 0)
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200657 semsg(_(e_string_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200658 else
659 emsg(_(e_stringreq));
660 return FAIL;
661 }
662 return OK;
663}
664
665/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200666 * Give an error and return FAIL unless "args[idx]" is a string or a list.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200667 */
668 int
669check_for_string_or_list_arg(typval_T *args, int idx)
670{
671 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
672 {
673 if (idx >= 0)
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200674 semsg(_(e_string_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200675 else
676 emsg(_(e_stringreq));
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 {
700 if (idx >= 0)
701 semsg(_(e_string_or_dict_required_for_argument_nr), idx + 1);
702 else
703 emsg(_(e_stringreq));
704 return FAIL;
705 }
706 return OK;
707}
708
709/*
710 * Give an error and return FAIL unless "args[idx]" is a string or a number
711 * or a list.
712 */
713 int
714check_for_string_or_number_or_list_arg(typval_T *args, int idx)
715{
716 if (args[idx].v_type != VAR_STRING
717 && args[idx].v_type != VAR_NUMBER
718 && args[idx].v_type != VAR_LIST)
719 {
720 if (idx >= 0)
Bram Moolenaar78db17c2021-07-31 19:12:58 +0200721 semsg(_(e_string_number_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200722 else
723 emsg(_(e_stringreq));
724 return FAIL;
725 }
726 return OK;
727}
728
729/*
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200730 * Give an error and return FAIL unless "args[idx]" is an optional string
731 * or number or a list
732 */
733 int
734check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx)
735{
736 return (args[idx].v_type == VAR_UNKNOWN
737 || check_for_string_or_number_or_list_arg(args, idx) != FAIL);
738}
739
740/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200741 * Give an error and return FAIL unless "args[idx]" is a string or a list
742 * or a dict.
743 */
744 int
745check_for_string_or_list_or_dict_arg(typval_T *args, int idx)
746{
747 if (args[idx].v_type != VAR_STRING
748 && args[idx].v_type != VAR_LIST
749 && args[idx].v_type != VAR_DICT)
750 {
751 if (idx >= 0)
Bram Moolenaar78db17c2021-07-31 19:12:58 +0200752 semsg(_(e_string_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200753 else
754 emsg(_(e_stringreq));
755 return FAIL;
756 }
757 return OK;
758}
759
760/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200761 * Give an error and return FAIL unless "args[idx]" is a list or a blob.
762 */
763 int
764check_for_list_or_blob_arg(typval_T *args, int idx)
765{
766 if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
767 {
768 if (idx >= 0)
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200769 semsg(_(e_list_or_blob_required_for_argument_nr), idx + 1);
770 else
771 emsg(_(e_listreq));
772 return FAIL;
773 }
774 return OK;
775}
776
777/*
778 * Give an error and return FAIL unless "args[idx]" is a list or dict
779 */
780 int
781check_for_list_or_dict_arg(typval_T *args, int idx)
782{
783 if (args[idx].v_type != VAR_LIST
784 && args[idx].v_type != VAR_DICT)
785 {
786 if (idx >= 0)
787 semsg(_(e_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200788 else
789 emsg(_(e_listreq));
790 return FAIL;
791 }
792 return OK;
793}
794
795/*
796 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
797 * blob.
798 */
799 int
800check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
801{
802 if (args[idx].v_type != VAR_LIST
803 && args[idx].v_type != VAR_DICT
804 && args[idx].v_type != VAR_BLOB)
805 {
806 if (idx >= 0)
Bram Moolenaar78db17c2021-07-31 19:12:58 +0200807 semsg(_(e_list_dict_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200808 else
809 emsg(_(e_listreq));
810 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 {
827 if (idx >= 0)
828 semsg(_(e_string_required_for_argument_nr), idx + 1);
829 else
830 emsg(_(e_stringreq));
831 return FAIL;
832 }
833 return OK;
834}
835
836/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200837 * Get the string value of a variable.
838 * If it is a Number variable, the number is converted into a string.
839 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
840 * tv_get_string_buf() uses a given buffer.
841 * If the String variable has never been set, return an empty string.
842 * Never returns NULL;
843 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
844 * NULL on error.
845 */
846 char_u *
847tv_get_string(typval_T *varp)
848{
849 static char_u mybuf[NUMBUFLEN];
850
851 return tv_get_string_buf(varp, mybuf);
852}
853
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100854/*
855 * Like tv_get_string() but don't allow number to string conversion for Vim9.
856 */
857 char_u *
858tv_get_string_strict(typval_T *varp)
859{
860 static char_u mybuf[NUMBUFLEN];
861 char_u *res = tv_get_string_buf_chk_strict(
862 varp, mybuf, in_vim9script());
863
864 return res != NULL ? res : (char_u *)"";
865}
866
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200867 char_u *
868tv_get_string_buf(typval_T *varp, char_u *buf)
869{
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200870 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200871
872 return res != NULL ? res : (char_u *)"";
873}
874
875/*
876 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
877 */
878 char_u *
879tv_get_string_chk(typval_T *varp)
880{
881 static char_u mybuf[NUMBUFLEN];
882
883 return tv_get_string_buf_chk(varp, mybuf);
884}
885
886 char_u *
887tv_get_string_buf_chk(typval_T *varp, char_u *buf)
888{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100889 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
890}
891
892 char_u *
893tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
894{
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200895 switch (varp->v_type)
896 {
897 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100898 if (strict)
899 {
900 emsg(_(e_using_number_as_string));
901 break;
902 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200903 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
904 (varnumber_T)varp->vval.v_number);
905 return buf;
906 case VAR_FUNC:
907 case VAR_PARTIAL:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200908 emsg(_("E729: Using a Funcref as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200909 break;
910 case VAR_LIST:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200911 emsg(_("E730: Using a List as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200912 break;
913 case VAR_DICT:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200914 emsg(_("E731: Using a Dictionary as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200915 break;
916 case VAR_FLOAT:
917#ifdef FEAT_FLOAT
Bram Moolenaar7a2217b2021-06-06 12:33:49 +0200918 if (strict)
919 {
920 emsg(_(e_float_as_string));
921 break;
922 }
923 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
924 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200925#endif
926 case VAR_STRING:
927 if (varp->vval.v_string != NULL)
928 return varp->vval.v_string;
929 return (char_u *)"";
930 case VAR_BOOL:
931 case VAR_SPECIAL:
932 STRCPY(buf, get_var_special_name(varp->vval.v_number));
933 return buf;
934 case VAR_BLOB:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200935 emsg(_("E976: Using a Blob as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200936 break;
937 case VAR_JOB:
938#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200939 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200940 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200941 semsg(_(e_using_invalid_value_as_string_str), "job");
942 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200943 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200944 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200945#endif
946 break;
947 case VAR_CHANNEL:
948#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200949 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200950 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200951 semsg(_(e_using_invalid_value_as_string_str), "channel");
952 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200953 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200954 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200955#endif
956 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200957 case VAR_VOID:
958 emsg(_(e_cannot_use_void_value));
959 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200960 case VAR_UNKNOWN:
961 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200962 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +0200963 semsg(_(e_using_invalid_value_as_string_str),
964 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200965 break;
966 }
967 return NULL;
968}
969
970/*
971 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
972 * string() on Dict, List, etc.
973 */
974 char_u *
975tv_stringify(typval_T *varp, char_u *buf)
976{
977 if (varp->v_type == VAR_LIST
978 || varp->v_type == VAR_DICT
979 || varp->v_type == VAR_BLOB
980 || varp->v_type == VAR_FUNC
981 || varp->v_type == VAR_PARTIAL
982 || varp->v_type == VAR_FLOAT)
983 {
984 typval_T tmp;
985
986 f_string(varp, &tmp);
987 tv_get_string_buf(&tmp, buf);
988 clear_tv(varp);
989 *varp = tmp;
990 return tmp.vval.v_string;
991 }
992 return tv_get_string_buf(varp, buf);
993}
994
995/*
996 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
997 * Also give an error message, using "name" or _("name") when use_gettext is
998 * TRUE.
999 */
1000 int
1001tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
1002{
1003 int lock = 0;
1004
1005 switch (tv->v_type)
1006 {
1007 case VAR_BLOB:
1008 if (tv->vval.v_blob != NULL)
1009 lock = tv->vval.v_blob->bv_lock;
1010 break;
1011 case VAR_LIST:
1012 if (tv->vval.v_list != NULL)
1013 lock = tv->vval.v_list->lv_lock;
1014 break;
1015 case VAR_DICT:
1016 if (tv->vval.v_dict != NULL)
1017 lock = tv->vval.v_dict->dv_lock;
1018 break;
1019 default:
1020 break;
1021 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001022 return value_check_lock(tv->v_lock, name, use_gettext)
1023 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001024}
1025
1026/*
1027 * Copy the values from typval_T "from" to typval_T "to".
1028 * When needed allocates string or increases reference count.
1029 * Does not make a copy of a list, blob or dict but copies the reference!
1030 * It is OK for "from" and "to" to point to the same item. This is used to
1031 * make a copy later.
1032 */
1033 void
1034copy_tv(typval_T *from, typval_T *to)
1035{
1036 to->v_type = from->v_type;
1037 to->v_lock = 0;
1038 switch (from->v_type)
1039 {
1040 case VAR_NUMBER:
1041 case VAR_BOOL:
1042 case VAR_SPECIAL:
1043 to->vval.v_number = from->vval.v_number;
1044 break;
1045 case VAR_FLOAT:
1046#ifdef FEAT_FLOAT
1047 to->vval.v_float = from->vval.v_float;
1048 break;
1049#endif
1050 case VAR_JOB:
1051#ifdef FEAT_JOB_CHANNEL
1052 to->vval.v_job = from->vval.v_job;
1053 if (to->vval.v_job != NULL)
1054 ++to->vval.v_job->jv_refcount;
1055 break;
1056#endif
1057 case VAR_CHANNEL:
1058#ifdef FEAT_JOB_CHANNEL
1059 to->vval.v_channel = from->vval.v_channel;
1060 if (to->vval.v_channel != NULL)
1061 ++to->vval.v_channel->ch_refcount;
1062 break;
1063#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001064 case VAR_INSTR:
1065 to->vval.v_instr = from->vval.v_instr;
1066 break;
1067
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001068 case VAR_STRING:
1069 case VAR_FUNC:
1070 if (from->vval.v_string == NULL)
1071 to->vval.v_string = NULL;
1072 else
1073 {
1074 to->vval.v_string = vim_strsave(from->vval.v_string);
1075 if (from->v_type == VAR_FUNC)
1076 func_ref(to->vval.v_string);
1077 }
1078 break;
1079 case VAR_PARTIAL:
1080 if (from->vval.v_partial == NULL)
1081 to->vval.v_partial = NULL;
1082 else
1083 {
1084 to->vval.v_partial = from->vval.v_partial;
1085 ++to->vval.v_partial->pt_refcount;
1086 }
1087 break;
1088 case VAR_BLOB:
1089 if (from->vval.v_blob == NULL)
1090 to->vval.v_blob = NULL;
1091 else
1092 {
1093 to->vval.v_blob = from->vval.v_blob;
1094 ++to->vval.v_blob->bv_refcount;
1095 }
1096 break;
1097 case VAR_LIST:
1098 if (from->vval.v_list == NULL)
1099 to->vval.v_list = NULL;
1100 else
1101 {
1102 to->vval.v_list = from->vval.v_list;
1103 ++to->vval.v_list->lv_refcount;
1104 }
1105 break;
1106 case VAR_DICT:
1107 if (from->vval.v_dict == NULL)
1108 to->vval.v_dict = NULL;
1109 else
1110 {
1111 to->vval.v_dict = from->vval.v_dict;
1112 ++to->vval.v_dict->dv_refcount;
1113 }
1114 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +02001115 case VAR_VOID:
1116 emsg(_(e_cannot_use_void_value));
1117 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001118 case VAR_UNKNOWN:
1119 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001120 internal_error_no_abort("copy_tv(UNKNOWN)");
1121 break;
1122 }
1123}
1124
1125/*
1126 * Compare "typ1" and "typ2". Put the result in "typ1".
1127 */
1128 int
1129typval_compare(
1130 typval_T *typ1, // first operand
1131 typval_T *typ2, // second operand
Bram Moolenaar657137c2021-01-09 15:45:23 +01001132 exprtype_T type, // operator
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001133 int ic) // ignore case
1134{
1135 int i;
1136 varnumber_T n1, n2;
1137 char_u *s1, *s2;
1138 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1139 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1140
1141 if (type_is && typ1->v_type != typ2->v_type)
1142 {
1143 // For "is" a different type always means FALSE, for "notis"
1144 // it means TRUE.
1145 n1 = (type == EXPR_ISNOT);
1146 }
1147 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
1148 {
1149 if (type_is)
1150 {
1151 n1 = (typ1->v_type == typ2->v_type
1152 && typ1->vval.v_blob == typ2->vval.v_blob);
1153 if (type == EXPR_ISNOT)
1154 n1 = !n1;
1155 }
1156 else if (typ1->v_type != typ2->v_type
1157 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1158 {
1159 if (typ1->v_type != typ2->v_type)
1160 emsg(_("E977: Can only compare Blob with Blob"));
1161 else
1162 emsg(_(e_invalblob));
1163 clear_tv(typ1);
1164 return FAIL;
1165 }
1166 else
1167 {
1168 // Compare two Blobs for being equal or unequal.
1169 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
1170 if (type == EXPR_NEQUAL)
1171 n1 = !n1;
1172 }
1173 }
1174 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
1175 {
1176 if (type_is)
1177 {
1178 n1 = (typ1->v_type == typ2->v_type
1179 && typ1->vval.v_list == typ2->vval.v_list);
1180 if (type == EXPR_ISNOT)
1181 n1 = !n1;
1182 }
1183 else if (typ1->v_type != typ2->v_type
1184 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1185 {
1186 if (typ1->v_type != typ2->v_type)
1187 emsg(_("E691: Can only compare List with List"));
1188 else
1189 emsg(_("E692: Invalid operation for List"));
1190 clear_tv(typ1);
1191 return FAIL;
1192 }
1193 else
1194 {
1195 // Compare two Lists for being equal or unequal.
1196 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
1197 ic, FALSE);
1198 if (type == EXPR_NEQUAL)
1199 n1 = !n1;
1200 }
1201 }
1202
1203 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
1204 {
1205 if (type_is)
1206 {
1207 n1 = (typ1->v_type == typ2->v_type
1208 && typ1->vval.v_dict == typ2->vval.v_dict);
1209 if (type == EXPR_ISNOT)
1210 n1 = !n1;
1211 }
1212 else if (typ1->v_type != typ2->v_type
1213 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1214 {
1215 if (typ1->v_type != typ2->v_type)
1216 emsg(_("E735: Can only compare Dictionary with Dictionary"));
1217 else
1218 emsg(_("E736: Invalid operation for Dictionary"));
1219 clear_tv(typ1);
1220 return FAIL;
1221 }
1222 else
1223 {
1224 // Compare two Dictionaries for being equal or unequal.
1225 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
1226 ic, FALSE);
1227 if (type == EXPR_NEQUAL)
1228 n1 = !n1;
1229 }
1230 }
1231
1232 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
1233 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
1234 {
1235 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1236 && type != EXPR_IS && type != EXPR_ISNOT)
1237 {
1238 emsg(_("E694: Invalid operation for Funcrefs"));
1239 clear_tv(typ1);
1240 return FAIL;
1241 }
1242 if ((typ1->v_type == VAR_PARTIAL
1243 && typ1->vval.v_partial == NULL)
1244 || (typ2->v_type == VAR_PARTIAL
1245 && typ2->vval.v_partial == NULL))
1246 // When both partials are NULL, then they are equal.
1247 // Otherwise they are not equal.
1248 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
1249 else if (type_is)
1250 {
1251 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
1252 // strings are considered the same if their value is
1253 // the same
1254 n1 = tv_equal(typ1, typ2, ic, FALSE);
1255 else if (typ1->v_type == VAR_PARTIAL
1256 && typ2->v_type == VAR_PARTIAL)
1257 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
1258 else
1259 n1 = FALSE;
1260 }
1261 else
1262 n1 = tv_equal(typ1, typ2, ic, FALSE);
1263 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1264 n1 = !n1;
1265 }
1266
1267#ifdef FEAT_FLOAT
1268 // If one of the two variables is a float, compare as a float.
1269 // When using "=~" or "!~", always compare as string.
1270 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
1271 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1272 {
1273 float_T f1, f2;
1274
1275 f1 = tv_get_float(typ1);
1276 f2 = tv_get_float(typ2);
1277 n1 = FALSE;
1278 switch (type)
1279 {
1280 case EXPR_IS:
1281 case EXPR_EQUAL: n1 = (f1 == f2); break;
1282 case EXPR_ISNOT:
1283 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1284 case EXPR_GREATER: n1 = (f1 > f2); break;
1285 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1286 case EXPR_SMALLER: n1 = (f1 < f2); break;
1287 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1288 case EXPR_UNKNOWN:
1289 case EXPR_MATCH:
1290 default: break; // avoid gcc warning
1291 }
1292 }
1293#endif
1294
1295 // If one of the two variables is a number, compare as a number.
1296 // When using "=~" or "!~", always compare as string.
1297 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
1298 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1299 {
1300 n1 = tv_get_number(typ1);
1301 n2 = tv_get_number(typ2);
1302 switch (type)
1303 {
1304 case EXPR_IS:
1305 case EXPR_EQUAL: n1 = (n1 == n2); break;
1306 case EXPR_ISNOT:
1307 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1308 case EXPR_GREATER: n1 = (n1 > n2); break;
1309 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1310 case EXPR_SMALLER: n1 = (n1 < n2); break;
1311 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1312 case EXPR_UNKNOWN:
1313 case EXPR_MATCH:
1314 default: break; // avoid gcc warning
1315 }
1316 }
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001317 else if (in_vim9script() && (typ1->v_type == VAR_BOOL
Bram Moolenaar0c357522021-07-18 14:43:43 +02001318 || typ2->v_type == VAR_BOOL
1319 || (typ1->v_type == VAR_SPECIAL
1320 && typ2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001321 {
1322 if (typ1->v_type != typ2->v_type)
1323 {
1324 semsg(_(e_cannot_compare_str_with_str),
1325 vartype_name(typ1->v_type), vartype_name(typ2->v_type));
1326 clear_tv(typ1);
1327 return FAIL;
1328 }
1329 n1 = typ1->vval.v_number;
1330 n2 = typ2->vval.v_number;
1331 switch (type)
1332 {
1333 case EXPR_IS:
1334 case EXPR_EQUAL: n1 = (n1 == n2); break;
1335 case EXPR_ISNOT:
1336 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1337 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001338 semsg(_(e_invalid_operation_for_str),
1339 vartype_name(typ1->v_type));
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001340 clear_tv(typ1);
1341 return FAIL;
1342 }
1343 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001344 else
1345 {
Bram Moolenaar0c357522021-07-18 14:43:43 +02001346 if (in_vim9script()
1347 && ((typ1->v_type != VAR_STRING && typ1->v_type != VAR_SPECIAL)
1348 || (typ2->v_type != VAR_STRING && typ2->v_type != VAR_SPECIAL)))
1349 {
1350 semsg(_(e_cannot_compare_str_with_str),
1351 vartype_name(typ1->v_type), vartype_name(typ2->v_type));
1352 clear_tv(typ1);
1353 return FAIL;
1354 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001355 s1 = tv_get_string_buf(typ1, buf1);
1356 s2 = tv_get_string_buf(typ2, buf2);
1357 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1358 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1359 else
1360 i = 0;
1361 n1 = FALSE;
1362 switch (type)
1363 {
1364 case EXPR_IS:
1365 case EXPR_EQUAL: n1 = (i == 0); break;
1366 case EXPR_ISNOT:
1367 case EXPR_NEQUAL: n1 = (i != 0); break;
1368 case EXPR_GREATER: n1 = (i > 0); break;
1369 case EXPR_GEQUAL: n1 = (i >= 0); break;
1370 case EXPR_SMALLER: n1 = (i < 0); break;
1371 case EXPR_SEQUAL: n1 = (i <= 0); break;
1372
1373 case EXPR_MATCH:
1374 case EXPR_NOMATCH:
1375 n1 = pattern_match(s2, s1, ic);
1376 if (type == EXPR_NOMATCH)
1377 n1 = !n1;
1378 break;
1379
1380 default: break; // avoid gcc warning
1381 }
1382 }
1383 clear_tv(typ1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001384 if (in_vim9script())
1385 {
1386 typ1->v_type = VAR_BOOL;
1387 typ1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
1388 }
1389 else
1390 {
1391 typ1->v_type = VAR_NUMBER;
1392 typ1->vval.v_number = n1;
1393 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001394
1395 return OK;
1396}
1397
Bram Moolenaar34453202021-01-31 13:08:38 +01001398/*
1399 * Convert any type to a string, never give an error.
1400 * When "quotes" is TRUE add quotes to a string.
1401 * Returns an allocated string.
1402 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001403 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001404typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001405{
1406 char_u *tofree;
1407 char_u numbuf[NUMBUFLEN];
1408 char_u *ret = NULL;
1409
1410 if (arg == NULL)
1411 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001412 if (!quotes && arg->v_type == VAR_STRING)
1413 {
1414 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1415 : arg->vval.v_string);
1416 }
1417 else
1418 {
1419 ret = tv2string(arg, &tofree, numbuf, 0);
1420 // Make a copy if we have a value but it's not in allocated memory.
1421 if (ret != NULL && tofree == NULL)
1422 ret = vim_strsave(ret);
1423 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001424 return ret;
1425}
1426
1427/*
1428 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1429 * or it refers to a List or Dictionary that is locked.
1430 */
1431 int
1432tv_islocked(typval_T *tv)
1433{
1434 return (tv->v_lock & VAR_LOCKED)
1435 || (tv->v_type == VAR_LIST
1436 && tv->vval.v_list != NULL
1437 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1438 || (tv->v_type == VAR_DICT
1439 && tv->vval.v_dict != NULL
1440 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1441}
1442
1443 static int
1444func_equal(
1445 typval_T *tv1,
1446 typval_T *tv2,
1447 int ic) // ignore case
1448{
1449 char_u *s1, *s2;
1450 dict_T *d1, *d2;
1451 int a1, a2;
1452 int i;
1453
1454 // empty and NULL function name considered the same
1455 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1456 : partial_name(tv1->vval.v_partial);
1457 if (s1 != NULL && *s1 == NUL)
1458 s1 = NULL;
1459 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1460 : partial_name(tv2->vval.v_partial);
1461 if (s2 != NULL && *s2 == NUL)
1462 s2 = NULL;
1463 if (s1 == NULL || s2 == NULL)
1464 {
1465 if (s1 != s2)
1466 return FALSE;
1467 }
1468 else if (STRCMP(s1, s2) != 0)
1469 return FALSE;
1470
1471 // empty dict and NULL dict is different
1472 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1473 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1474 if (d1 == NULL || d2 == NULL)
1475 {
1476 if (d1 != d2)
1477 return FALSE;
1478 }
1479 else if (!dict_equal(d1, d2, ic, TRUE))
1480 return FALSE;
1481
1482 // empty list and no list considered the same
1483 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1484 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1485 if (a1 != a2)
1486 return FALSE;
1487 for (i = 0; i < a1; ++i)
1488 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1489 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1490 return FALSE;
1491
1492 return TRUE;
1493}
1494
1495/*
1496 * Return TRUE if "tv1" and "tv2" have the same value.
1497 * Compares the items just like "==" would compare them, but strings and
1498 * numbers are different. Floats and numbers are also different.
1499 */
1500 int
1501tv_equal(
1502 typval_T *tv1,
1503 typval_T *tv2,
1504 int ic, // ignore case
1505 int recursive) // TRUE when used recursively
1506{
1507 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1508 char_u *s1, *s2;
1509 static int recursive_cnt = 0; // catch recursive loops
1510 int r;
1511 static int tv_equal_recurse_limit;
1512
1513 // Catch lists and dicts that have an endless loop by limiting
1514 // recursiveness to a limit. We guess they are equal then.
1515 // A fixed limit has the problem of still taking an awful long time.
1516 // Reduce the limit every time running into it. That should work fine for
1517 // deeply linked structures that are not recursively linked and catch
1518 // recursiveness quickly.
1519 if (!recursive)
1520 tv_equal_recurse_limit = 1000;
1521 if (recursive_cnt >= tv_equal_recurse_limit)
1522 {
1523 --tv_equal_recurse_limit;
1524 return TRUE;
1525 }
1526
1527 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1528 // arguments.
1529 if ((tv1->v_type == VAR_FUNC
1530 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1531 && (tv2->v_type == VAR_FUNC
1532 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1533 {
1534 ++recursive_cnt;
1535 r = func_equal(tv1, tv2, ic);
1536 --recursive_cnt;
1537 return r;
1538 }
1539
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001540 if (tv1->v_type != tv2->v_type
1541 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1542 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001543 return FALSE;
1544
1545 switch (tv1->v_type)
1546 {
1547 case VAR_LIST:
1548 ++recursive_cnt;
1549 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1550 --recursive_cnt;
1551 return r;
1552
1553 case VAR_DICT:
1554 ++recursive_cnt;
1555 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1556 --recursive_cnt;
1557 return r;
1558
1559 case VAR_BLOB:
1560 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1561
1562 case VAR_NUMBER:
1563 case VAR_BOOL:
1564 case VAR_SPECIAL:
1565 return tv1->vval.v_number == tv2->vval.v_number;
1566
1567 case VAR_STRING:
1568 s1 = tv_get_string_buf(tv1, buf1);
1569 s2 = tv_get_string_buf(tv2, buf2);
1570 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1571
1572 case VAR_FLOAT:
1573#ifdef FEAT_FLOAT
1574 return tv1->vval.v_float == tv2->vval.v_float;
1575#endif
1576 case VAR_JOB:
1577#ifdef FEAT_JOB_CHANNEL
1578 return tv1->vval.v_job == tv2->vval.v_job;
1579#endif
1580 case VAR_CHANNEL:
1581#ifdef FEAT_JOB_CHANNEL
1582 return tv1->vval.v_channel == tv2->vval.v_channel;
1583#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001584 case VAR_INSTR:
1585 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001586
1587 case VAR_PARTIAL:
1588 return tv1->vval.v_partial == tv2->vval.v_partial;
1589
1590 case VAR_FUNC:
1591 return tv1->vval.v_string == tv2->vval.v_string;
1592
1593 case VAR_UNKNOWN:
1594 case VAR_ANY:
1595 case VAR_VOID:
1596 break;
1597 }
1598
1599 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1600 // does not equal anything, not even itself.
1601 return FALSE;
1602}
1603
1604/*
1605 * Get an option value.
1606 * "arg" points to the '&' or '+' before the option name.
1607 * "arg" is advanced to character after the option name.
1608 * Return OK or FAIL.
1609 */
1610 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001611eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001612 char_u **arg,
1613 typval_T *rettv, // when NULL, only check if option exists
1614 int evaluate)
1615{
1616 char_u *option_end;
1617 long numval;
1618 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001619 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001620 int c;
1621 int working = (**arg == '+'); // has("+option")
1622 int ret = OK;
1623 int opt_flags;
1624
1625 // Isolate the option name and find its value.
1626 option_end = find_option_end(arg, &opt_flags);
1627 if (option_end == NULL)
1628 {
1629 if (rettv != NULL)
1630 semsg(_("E112: Option name missing: %s"), *arg);
1631 return FAIL;
1632 }
1633
1634 if (!evaluate)
1635 {
1636 *arg = option_end;
1637 return OK;
1638 }
1639
1640 c = *option_end;
1641 *option_end = NUL;
1642 opt_type = get_option_value(*arg, &numval,
1643 rettv == NULL ? NULL : &stringval, opt_flags);
1644
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001645 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001646 {
1647 if (rettv != NULL)
1648 semsg(_(e_unknown_option), *arg);
1649 ret = FAIL;
1650 }
1651 else if (rettv != NULL)
1652 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001653 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001654 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001655 {
1656 rettv->v_type = VAR_STRING;
1657 rettv->vval.v_string = NULL;
1658 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001659 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001660 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001661 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1662 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001663 rettv->vval.v_number = 0;
1664 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001665 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001666 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001667 if (in_vim9script() && opt_type == gov_bool)
1668 {
1669 rettv->v_type = VAR_BOOL;
1670 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1671 }
1672 else
1673 {
1674 rettv->v_type = VAR_NUMBER;
1675 rettv->vval.v_number = numval;
1676 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001677 }
1678 else // string option
1679 {
1680 rettv->v_type = VAR_STRING;
1681 rettv->vval.v_string = stringval;
1682 }
1683 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001684 else if (working && (opt_type == gov_hidden_bool
1685 || opt_type == gov_hidden_number
1686 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001687 ret = FAIL;
1688
1689 *option_end = c; // put back for error messages
1690 *arg = option_end;
1691
1692 return ret;
1693}
1694
1695/*
1696 * Allocate a variable for a number constant. Also deals with "0z" for blob.
1697 * Return OK or FAIL.
1698 */
1699 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001700eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001701 char_u **arg,
1702 typval_T *rettv,
1703 int evaluate,
1704 int want_string UNUSED)
1705{
1706 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02001707 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001708#ifdef FEAT_FLOAT
1709 char_u *p;
1710 int get_float = FALSE;
1711
1712 // We accept a float when the format matches
1713 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
1714 // strict to avoid backwards compatibility problems.
1715 // With script version 2 and later the leading digit can be
1716 // omitted.
1717 // Don't look for a float after the "." operator, so that
1718 // ":let vers = 1.2.3" doesn't fail.
1719 if (**arg == '.')
1720 p = *arg;
1721 else
Bram Moolenaar29500652021-08-08 15:43:34 +02001722 {
1723 p = *arg + 1;
1724 if (skip_quotes)
1725 for (;;)
1726 {
1727 if (*p == '\'')
1728 ++p;
1729 if (!vim_isdigit(*p))
1730 break;
1731 p = skipdigits(p);
1732 }
1733 else
1734 p = skipdigits(p);
1735 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001736 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
1737 {
1738 get_float = TRUE;
1739 p = skipdigits(p + 2);
1740 if (*p == 'e' || *p == 'E')
1741 {
1742 ++p;
1743 if (*p == '-' || *p == '+')
1744 ++p;
1745 if (!vim_isdigit(*p))
1746 get_float = FALSE;
1747 else
1748 p = skipdigits(p + 1);
1749 }
1750 if (ASCII_ISALPHA(*p) || *p == '.')
1751 get_float = FALSE;
1752 }
1753 if (get_float)
1754 {
1755 float_T f;
1756
Bram Moolenaar29500652021-08-08 15:43:34 +02001757 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001758 if (evaluate)
1759 {
1760 rettv->v_type = VAR_FLOAT;
1761 rettv->vval.v_float = f;
1762 }
1763 }
1764 else
1765#endif
1766 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
1767 {
1768 char_u *bp;
1769 blob_T *blob = NULL; // init for gcc
1770
1771 // Blob constant: 0z0123456789abcdef
1772 if (evaluate)
1773 blob = blob_alloc();
1774 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
1775 {
1776 if (!vim_isxdigit(bp[1]))
1777 {
1778 if (blob != NULL)
1779 {
1780 emsg(_("E973: Blob literal should have an even number of hex characters"));
1781 ga_clear(&blob->bv_ga);
1782 VIM_CLEAR(blob);
1783 }
1784 return FAIL;
1785 }
1786 if (blob != NULL)
1787 ga_append(&blob->bv_ga,
1788 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
1789 if (bp[2] == '.' && vim_isxdigit(bp[3]))
1790 ++bp;
1791 }
1792 if (blob != NULL)
1793 rettv_blob_set(rettv, blob);
1794 *arg = bp;
1795 }
1796 else
1797 {
1798 varnumber_T n;
1799
1800 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02001801 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001802 ? STR2NR_NO_OCT + STR2NR_QUOTE
1803 : STR2NR_ALL, &n, NULL, 0, TRUE);
1804 if (len == 0)
1805 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001806 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001807 return FAIL;
1808 }
1809 *arg += len;
1810 if (evaluate)
1811 {
1812 rettv->v_type = VAR_NUMBER;
1813 rettv->vval.v_number = n;
1814 }
1815 }
1816 return OK;
1817}
1818
1819/*
1820 * Allocate a variable for a string constant.
1821 * Return OK or FAIL.
1822 */
1823 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001824eval_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001825{
1826 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001827 char_u *end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001828 int extra = 0;
1829 int len;
1830
1831 // Find the end of the string, skipping backslashed characters.
1832 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
1833 {
1834 if (*p == '\\' && p[1] != NUL)
1835 {
1836 ++p;
1837 // A "\<x>" form occupies at least 4 characters, and produces up
1838 // to 21 characters (3 * 6 for the char and 3 for a modifier):
1839 // reserve space for 18 extra.
1840 // Each byte in the char could be encoded as K_SPECIAL K_EXTRA x.
1841 if (*p == '<')
1842 extra += 18;
1843 }
1844 }
1845
1846 if (*p != '"')
1847 {
1848 semsg(_("E114: Missing quote: %s"), *arg);
1849 return FAIL;
1850 }
1851
1852 // If only parsing, set *arg and return here
1853 if (!evaluate)
1854 {
1855 *arg = p + 1;
1856 return OK;
1857 }
1858
1859 // Copy the string into allocated memory, handling backslashed
1860 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001861 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001862 len = (int)(p - *arg + extra);
1863 rettv->vval.v_string = alloc(len);
1864 if (rettv->vval.v_string == NULL)
1865 return FAIL;
1866 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001867
1868 for (p = *arg + 1; *p != NUL && *p != '"'; )
1869 {
1870 if (*p == '\\')
1871 {
1872 switch (*++p)
1873 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001874 case 'b': *end++ = BS; ++p; break;
1875 case 'e': *end++ = ESC; ++p; break;
1876 case 'f': *end++ = FF; ++p; break;
1877 case 'n': *end++ = NL; ++p; break;
1878 case 'r': *end++ = CAR; ++p; break;
1879 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001880
1881 case 'X': // hex: "\x1", "\x12"
1882 case 'x':
1883 case 'u': // Unicode: "\u0023"
1884 case 'U':
1885 if (vim_isxdigit(p[1]))
1886 {
1887 int n, nr;
1888 int c = toupper(*p);
1889
1890 if (c == 'X')
1891 n = 2;
1892 else if (*p == 'u')
1893 n = 4;
1894 else
1895 n = 8;
1896 nr = 0;
1897 while (--n >= 0 && vim_isxdigit(p[1]))
1898 {
1899 ++p;
1900 nr = (nr << 4) + hex2nr(*p);
1901 }
1902 ++p;
1903 // For "\u" store the number according to
1904 // 'encoding'.
1905 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001906 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001907 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001908 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001909 }
1910 break;
1911
1912 // octal: "\1", "\12", "\123"
1913 case '0':
1914 case '1':
1915 case '2':
1916 case '3':
1917 case '4':
1918 case '5':
1919 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001920 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001921 if (*p >= '0' && *p <= '7')
1922 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001923 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001924 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001925 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001926 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001927 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001928 break;
1929
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001930 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001931 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001932 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001933 int flags = FSK_KEYCODE | FSK_IN_STRING;
1934
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001935 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001936 flags |= FSK_SIMPLIFY;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001937 extra = trans_special(&p, end, flags, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001938 if (extra != 0)
1939 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001940 end += extra;
1941 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001942 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001943 break;
1944 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001945 }
1946 // FALLTHROUGH
1947
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001948 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001949 break;
1950 }
1951 }
1952 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001953 MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001954 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001955 *end = NUL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001956 if (*p != NUL) // just in case
1957 ++p;
1958 *arg = p;
1959
1960 return OK;
1961}
1962
1963/*
1964 * Allocate a variable for a 'str''ing' constant.
1965 * Return OK or FAIL.
1966 */
1967 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001968eval_lit_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001969{
1970 char_u *p;
1971 char_u *str;
1972 int reduce = 0;
1973
1974 // Find the end of the string, skipping ''.
1975 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
1976 {
1977 if (*p == '\'')
1978 {
1979 if (p[1] != '\'')
1980 break;
1981 ++reduce;
1982 ++p;
1983 }
1984 }
1985
1986 if (*p != '\'')
1987 {
1988 semsg(_("E115: Missing quote: %s"), *arg);
1989 return FAIL;
1990 }
1991
1992 // If only parsing return after setting "*arg"
1993 if (!evaluate)
1994 {
1995 *arg = p + 1;
1996 return OK;
1997 }
1998
1999 // Copy the string into allocated memory, handling '' to ' reduction.
2000 str = alloc((p - *arg) - reduce);
2001 if (str == NULL)
2002 return FAIL;
2003 rettv->v_type = VAR_STRING;
2004 rettv->vval.v_string = str;
2005
2006 for (p = *arg + 1; *p != NUL; )
2007 {
2008 if (*p == '\'')
2009 {
2010 if (p[1] != '\'')
2011 break;
2012 ++p;
2013 }
2014 MB_COPY_CHAR(p, str);
2015 }
2016 *str = NUL;
2017 *arg = p + 1;
2018
2019 return OK;
2020}
2021
2022/*
2023 * Return a string with the string representation of a variable.
2024 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2025 * "numbuf" is used for a number.
2026 * Puts quotes around strings, so that they can be parsed back by eval().
2027 * May return NULL.
2028 */
2029 char_u *
2030tv2string(
2031 typval_T *tv,
2032 char_u **tofree,
2033 char_u *numbuf,
2034 int copyID)
2035{
2036 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2037}
2038
2039/*
2040 * Get the value of an environment variable.
2041 * "arg" is pointing to the '$'. It is advanced to after the name.
2042 * If the environment variable was not set, silently assume it is empty.
2043 * Return FAIL if the name is invalid.
2044 */
2045 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002046eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002047{
2048 char_u *string = NULL;
2049 int len;
2050 int cc;
2051 char_u *name;
2052 int mustfree = FALSE;
2053
2054 ++*arg;
2055 name = *arg;
2056 len = get_env_len(arg);
2057 if (evaluate)
2058 {
2059 if (len == 0)
2060 return FAIL; // invalid empty name
2061
2062 cc = name[len];
2063 name[len] = NUL;
2064 // first try vim_getenv(), fast for normal environment vars
2065 string = vim_getenv(name, &mustfree);
2066 if (string != NULL && *string != NUL)
2067 {
2068 if (!mustfree)
2069 string = vim_strsave(string);
2070 }
2071 else
2072 {
2073 if (mustfree)
2074 vim_free(string);
2075
2076 // next try expanding things like $VIM and ${HOME}
2077 string = expand_env_save(name - 1);
2078 if (string != NULL && *string == '$')
2079 VIM_CLEAR(string);
2080 }
2081 name[len] = cc;
2082
2083 rettv->v_type = VAR_STRING;
2084 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002085 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002086 }
2087
2088 return OK;
2089}
2090
2091/*
2092 * Get the lnum from the first argument.
2093 * Also accepts ".", "$", etc., but that only works for the current buffer.
2094 * Returns -1 on error.
2095 */
2096 linenr_T
2097tv_get_lnum(typval_T *argvars)
2098{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002099 linenr_T lnum = -1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002100
Bram Moolenaar56acb092020-08-16 14:48:19 +02002101 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2102 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002103 if (lnum <= 0 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002104 {
2105 int fnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002106 pos_T *fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002107
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002108 // no valid number, try using arg like line()
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002109 if (fp != NULL)
2110 lnum = fp->lnum;
2111 }
2112 return lnum;
2113}
2114
2115/*
2116 * Get the lnum from the first argument.
2117 * Also accepts "$", then "buf" is used.
2118 * Returns 0 on error.
2119 */
2120 linenr_T
2121tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2122{
2123 if (argvars[0].v_type == VAR_STRING
2124 && argvars[0].vval.v_string != NULL
2125 && argvars[0].vval.v_string[0] == '$'
2126 && buf != NULL)
2127 return buf->b_ml.ml_line_count;
2128 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2129}
2130
2131/*
2132 * Get buffer by number or pattern.
2133 */
2134 buf_T *
2135tv_get_buf(typval_T *tv, int curtab_only)
2136{
2137 char_u *name = tv->vval.v_string;
2138 buf_T *buf;
2139
2140 if (tv->v_type == VAR_NUMBER)
2141 return buflist_findnr((int)tv->vval.v_number);
2142 if (tv->v_type != VAR_STRING)
2143 return NULL;
2144 if (name == NULL || *name == NUL)
2145 return curbuf;
2146 if (name[0] == '$' && name[1] == NUL)
2147 return lastbuf;
2148
2149 buf = buflist_find_by_name(name, curtab_only);
2150
2151 // If not found, try expanding the name, like done for bufexists().
2152 if (buf == NULL)
2153 buf = find_buffer(tv);
2154
2155 return buf;
2156}
2157
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002158/*
2159 * Like tv_get_buf() but give an error message is the type is wrong.
2160 */
2161 buf_T *
2162tv_get_buf_from_arg(typval_T *tv)
2163{
2164 buf_T *buf;
2165
2166 ++emsg_off;
2167 buf = tv_get_buf(tv, FALSE);
2168 --emsg_off;
2169 if (buf == NULL
2170 && tv->v_type != VAR_NUMBER
2171 && tv->v_type != VAR_STRING)
2172 // issue errmsg for type error
2173 (void)tv_get_number(tv);
2174 return buf;
2175}
2176
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002177#endif // FEAT_EVAL