blob: 3a0e2e5d99ee10f3d1c8d7fabc8ff238bf83fc7a [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 Lakshmanana9a7c0c2021-07-17 19:11:07 +0200474 * Give an error and return FAIL unless "args[idx]" is a list.
475 */
476 int
477check_for_list_arg(typval_T *args, int idx)
478{
479 if (args[idx].v_type != VAR_LIST)
480 {
481 if (idx >= 0)
482 semsg(_(e_list_required_for_argument_nr), idx + 1);
483 else
484 emsg(_(e_listreq));
485 return FAIL;
486 }
487 return OK;
488}
489
490/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200491 * Check for an optional list argument at 'idx'
492 */
493 int
494check_for_opt_list_arg(typval_T *args, int idx)
495{
496 return (args[idx].v_type == VAR_UNKNOWN
497 || check_for_list_arg(args, idx) != FAIL);
498}
499
500/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200501 * Give an error and return FAIL unless "args[idx]" is a dict.
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200502 */
503 int
504check_for_dict_arg(typval_T *args, int idx)
505{
506 if (args[idx].v_type != VAR_DICT)
507 {
508 if (idx >= 0)
509 semsg(_(e_dict_required_for_argument_nr), idx + 1);
510 else
511 emsg(_(e_dictreq));
512 return FAIL;
513 }
514 return OK;
515}
516
517/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200518 * Check for an optional dict argument at 'idx'
519 */
520 int
521check_for_opt_dict_arg(typval_T *args, int idx)
522{
523 return (args[idx].v_type == VAR_UNKNOWN
524 || check_for_dict_arg(args, idx) != FAIL);
525}
526
527/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200528 * Give an error and return FAIL unless "args[idx]" is a channel or a job.
529 */
530 int
531check_for_chan_or_job_arg(typval_T *args, int idx)
532{
533 if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
534 {
535 if (idx >= 0)
536 semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
537 else
538 emsg(_(e_chan_or_job_req));
539 return FAIL;
540 }
541 return OK;
542}
543
544/*
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200545 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
546 * job.
547 */
548 int
549check_for_opt_chan_or_job_arg(typval_T *args, int idx)
550{
551 return (args[idx].v_type == VAR_UNKNOWN
552 || check_for_chan_or_job_arg(args, idx) != FAIL);
553}
554
555/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200556 * Give an error and return FAIL unless "args[idx]" is a job.
557 */
558 int
559check_for_job_arg(typval_T *args, int idx)
560{
561 if (args[idx].v_type != VAR_JOB)
562 {
563 if (idx >= 0)
564 semsg(_(e_job_required_for_argument_nr), idx + 1);
565 else
566 emsg(_(e_jobreq));
567 return FAIL;
568 }
569 return OK;
570}
571
572/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200573 * Check for an optional job argument at 'idx'.
574 */
575 int
576check_for_opt_job_arg(typval_T *args, int idx)
577{
578 return (args[idx].v_type == VAR_UNKNOWN
579 || check_for_job_arg(args, idx) != FAIL);
580}
581
582/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200583 * Give an error and return FAIL unless "args[idx]" is a string or
584 * a number.
585 */
586 int
587check_for_string_or_number_arg(typval_T *args, int idx)
588{
589 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
590 {
591 if (idx >= 0)
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200592 semsg(_(e_string_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200593 else
594 emsg(_(e_stringreq));
595 return FAIL;
596 }
597 return OK;
598}
599
600/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200601 * Check for an optional string or number argument at 'idx'.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200602 */
603 int
604check_for_opt_string_or_number_arg(typval_T *args, int idx)
605{
606 return (args[idx].v_type == VAR_UNKNOWN
607 || check_for_string_or_number_arg(args, idx) != FAIL);
608}
609
610/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200611 * Give an error and return FAIL unless "args[idx]" is a buffer number.
612 * Buffer number can be a number or a string.
613 */
614 int
615check_for_buffer_arg(typval_T *args, int idx)
616{
617 return check_for_string_or_number_arg(args, idx);
618}
619
620/*
621 * Check for an optional buffer argument at 'idx'
622 */
623 int
624check_for_opt_buffer_arg(typval_T *args, int idx)
625{
626 return (args[idx].v_type == VAR_UNKNOWN
627 || check_for_buffer_arg(args, idx));
628}
629
630/*
631 * Give an error and return FAIL unless "args[idx]" is a line number.
632 * Line number can be a number or a string.
633 */
634 int
635check_for_lnum_arg(typval_T *args, int idx)
636{
637 return check_for_string_or_number_arg(args, idx);
638}
639
640/*
641 * Check for an optional line number argument at 'idx'
642 */
643 int
644check_for_opt_lnum_arg(typval_T *args, int idx)
645{
646 return (args[idx].v_type == VAR_UNKNOWN
647 || check_for_lnum_arg(args, idx));
648}
649
650/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200651 * Give an error and return FAIL unless "args[idx]" is a string or a blob.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200652 */
653 int
654check_for_string_or_blob_arg(typval_T *args, int idx)
655{
656 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
657 {
658 if (idx >= 0)
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200659 semsg(_(e_string_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200660 else
661 emsg(_(e_stringreq));
662 return FAIL;
663 }
664 return OK;
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 list.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200669 */
670 int
671check_for_string_or_list_arg(typval_T *args, int idx)
672{
673 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
674 {
675 if (idx >= 0)
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200676 semsg(_(e_string_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200677 else
678 emsg(_(e_stringreq));
679 return FAIL;
680 }
681 return OK;
682}
683
684/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200685 * Check for an optional string or list argument at 'idx'
686 */
687 int
688check_for_opt_string_or_list_arg(typval_T *args, int idx)
689{
690 return (args[idx].v_type == VAR_UNKNOWN
691 || check_for_string_or_list_arg(args, idx));
692}
693
694/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200695 * Give an error and return FAIL unless "args[idx]" is a string or a dict.
696 */
697 int
698check_for_string_or_dict_arg(typval_T *args, int idx)
699{
700 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_DICT)
701 {
702 if (idx >= 0)
703 semsg(_(e_string_or_dict_required_for_argument_nr), idx + 1);
704 else
705 emsg(_(e_stringreq));
706 return FAIL;
707 }
708 return OK;
709}
710
711/*
712 * Give an error and return FAIL unless "args[idx]" is a string or a number
713 * or a list.
714 */
715 int
716check_for_string_or_number_or_list_arg(typval_T *args, int idx)
717{
718 if (args[idx].v_type != VAR_STRING
719 && args[idx].v_type != VAR_NUMBER
720 && args[idx].v_type != VAR_LIST)
721 {
722 if (idx >= 0)
Bram Moolenaar78db17c2021-07-31 19:12:58 +0200723 semsg(_(e_string_number_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200724 else
725 emsg(_(e_stringreq));
726 return FAIL;
727 }
728 return OK;
729}
730
731/*
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200732 * Give an error and return FAIL unless "args[idx]" is an optional string
733 * or number or a list
734 */
735 int
736check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx)
737{
738 return (args[idx].v_type == VAR_UNKNOWN
739 || check_for_string_or_number_or_list_arg(args, idx) != FAIL);
740}
741
742/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200743 * Give an error and return FAIL unless "args[idx]" is a string or a list
744 * or a dict.
745 */
746 int
747check_for_string_or_list_or_dict_arg(typval_T *args, int idx)
748{
749 if (args[idx].v_type != VAR_STRING
750 && args[idx].v_type != VAR_LIST
751 && args[idx].v_type != VAR_DICT)
752 {
753 if (idx >= 0)
Bram Moolenaar78db17c2021-07-31 19:12:58 +0200754 semsg(_(e_string_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200755 else
756 emsg(_(e_stringreq));
757 return FAIL;
758 }
759 return OK;
760}
761
762/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200763 * Give an error and return FAIL unless "args[idx]" is a list or a blob.
764 */
765 int
766check_for_list_or_blob_arg(typval_T *args, int idx)
767{
768 if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
769 {
770 if (idx >= 0)
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200771 semsg(_(e_list_or_blob_required_for_argument_nr), idx + 1);
772 else
773 emsg(_(e_listreq));
774 return FAIL;
775 }
776 return OK;
777}
778
779/*
780 * Give an error and return FAIL unless "args[idx]" is a list or dict
781 */
782 int
783check_for_list_or_dict_arg(typval_T *args, int idx)
784{
785 if (args[idx].v_type != VAR_LIST
786 && args[idx].v_type != VAR_DICT)
787 {
788 if (idx >= 0)
789 semsg(_(e_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200790 else
791 emsg(_(e_listreq));
792 return FAIL;
793 }
794 return OK;
795}
796
797/*
798 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
799 * blob.
800 */
801 int
802check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
803{
804 if (args[idx].v_type != VAR_LIST
805 && args[idx].v_type != VAR_DICT
806 && args[idx].v_type != VAR_BLOB)
807 {
808 if (idx >= 0)
Bram Moolenaar78db17c2021-07-31 19:12:58 +0200809 semsg(_(e_list_dict_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200810 else
811 emsg(_(e_listreq));
812 return FAIL;
813 }
814 return OK;
815}
816
817/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200818 * Give an error and return FAIL unless "args[idx]" is an optional buffer
819 * number or a dict.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200820 */
821 int
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200822check_for_opt_buffer_or_dict_arg(typval_T *args, int idx)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200823{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200824 if (args[idx].v_type != VAR_UNKNOWN
825 && args[idx].v_type != VAR_STRING
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200826 && args[idx].v_type != VAR_NUMBER
827 && args[idx].v_type != VAR_DICT)
828 {
829 if (idx >= 0)
830 semsg(_(e_string_required_for_argument_nr), idx + 1);
831 else
832 emsg(_(e_stringreq));
833 return FAIL;
834 }
835 return OK;
836}
837
838/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200839 * Get the string value of a variable.
840 * If it is a Number variable, the number is converted into a string.
841 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
842 * tv_get_string_buf() uses a given buffer.
843 * If the String variable has never been set, return an empty string.
844 * Never returns NULL;
845 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
846 * NULL on error.
847 */
848 char_u *
849tv_get_string(typval_T *varp)
850{
851 static char_u mybuf[NUMBUFLEN];
852
853 return tv_get_string_buf(varp, mybuf);
854}
855
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100856/*
857 * Like tv_get_string() but don't allow number to string conversion for Vim9.
858 */
859 char_u *
860tv_get_string_strict(typval_T *varp)
861{
862 static char_u mybuf[NUMBUFLEN];
863 char_u *res = tv_get_string_buf_chk_strict(
864 varp, mybuf, in_vim9script());
865
866 return res != NULL ? res : (char_u *)"";
867}
868
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200869 char_u *
870tv_get_string_buf(typval_T *varp, char_u *buf)
871{
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200872 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200873
874 return res != NULL ? res : (char_u *)"";
875}
876
877/*
878 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
879 */
880 char_u *
881tv_get_string_chk(typval_T *varp)
882{
883 static char_u mybuf[NUMBUFLEN];
884
885 return tv_get_string_buf_chk(varp, mybuf);
886}
887
888 char_u *
889tv_get_string_buf_chk(typval_T *varp, char_u *buf)
890{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100891 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
892}
893
894 char_u *
895tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
896{
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200897 switch (varp->v_type)
898 {
899 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100900 if (strict)
901 {
902 emsg(_(e_using_number_as_string));
903 break;
904 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200905 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
906 (varnumber_T)varp->vval.v_number);
907 return buf;
908 case VAR_FUNC:
909 case VAR_PARTIAL:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200910 emsg(_("E729: Using a Funcref as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200911 break;
912 case VAR_LIST:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200913 emsg(_("E730: Using a List as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200914 break;
915 case VAR_DICT:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200916 emsg(_("E731: Using a Dictionary as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200917 break;
918 case VAR_FLOAT:
919#ifdef FEAT_FLOAT
Bram Moolenaar7a2217b2021-06-06 12:33:49 +0200920 if (strict)
921 {
922 emsg(_(e_float_as_string));
923 break;
924 }
925 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
926 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200927#endif
928 case VAR_STRING:
929 if (varp->vval.v_string != NULL)
930 return varp->vval.v_string;
931 return (char_u *)"";
932 case VAR_BOOL:
933 case VAR_SPECIAL:
934 STRCPY(buf, get_var_special_name(varp->vval.v_number));
935 return buf;
936 case VAR_BLOB:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200937 emsg(_("E976: Using a Blob as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200938 break;
939 case VAR_JOB:
940#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200941 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200942 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200943 semsg(_(e_using_invalid_value_as_string_str), "job");
944 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200945 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200946 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200947#endif
948 break;
949 case VAR_CHANNEL:
950#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200951 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200952 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200953 semsg(_(e_using_invalid_value_as_string_str), "channel");
954 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200955 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200956 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200957#endif
958 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200959 case VAR_VOID:
960 emsg(_(e_cannot_use_void_value));
961 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200962 case VAR_UNKNOWN:
963 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200964 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +0200965 semsg(_(e_using_invalid_value_as_string_str),
966 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200967 break;
968 }
969 return NULL;
970}
971
972/*
973 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
974 * string() on Dict, List, etc.
975 */
976 char_u *
977tv_stringify(typval_T *varp, char_u *buf)
978{
979 if (varp->v_type == VAR_LIST
980 || varp->v_type == VAR_DICT
981 || varp->v_type == VAR_BLOB
982 || varp->v_type == VAR_FUNC
983 || varp->v_type == VAR_PARTIAL
984 || varp->v_type == VAR_FLOAT)
985 {
986 typval_T tmp;
987
988 f_string(varp, &tmp);
989 tv_get_string_buf(&tmp, buf);
990 clear_tv(varp);
991 *varp = tmp;
992 return tmp.vval.v_string;
993 }
994 return tv_get_string_buf(varp, buf);
995}
996
997/*
998 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
999 * Also give an error message, using "name" or _("name") when use_gettext is
1000 * TRUE.
1001 */
1002 int
1003tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
1004{
1005 int lock = 0;
1006
1007 switch (tv->v_type)
1008 {
1009 case VAR_BLOB:
1010 if (tv->vval.v_blob != NULL)
1011 lock = tv->vval.v_blob->bv_lock;
1012 break;
1013 case VAR_LIST:
1014 if (tv->vval.v_list != NULL)
1015 lock = tv->vval.v_list->lv_lock;
1016 break;
1017 case VAR_DICT:
1018 if (tv->vval.v_dict != NULL)
1019 lock = tv->vval.v_dict->dv_lock;
1020 break;
1021 default:
1022 break;
1023 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001024 return value_check_lock(tv->v_lock, name, use_gettext)
1025 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001026}
1027
1028/*
1029 * Copy the values from typval_T "from" to typval_T "to".
1030 * When needed allocates string or increases reference count.
1031 * Does not make a copy of a list, blob or dict but copies the reference!
1032 * It is OK for "from" and "to" to point to the same item. This is used to
1033 * make a copy later.
1034 */
1035 void
1036copy_tv(typval_T *from, typval_T *to)
1037{
1038 to->v_type = from->v_type;
1039 to->v_lock = 0;
1040 switch (from->v_type)
1041 {
1042 case VAR_NUMBER:
1043 case VAR_BOOL:
1044 case VAR_SPECIAL:
1045 to->vval.v_number = from->vval.v_number;
1046 break;
1047 case VAR_FLOAT:
1048#ifdef FEAT_FLOAT
1049 to->vval.v_float = from->vval.v_float;
1050 break;
1051#endif
1052 case VAR_JOB:
1053#ifdef FEAT_JOB_CHANNEL
1054 to->vval.v_job = from->vval.v_job;
1055 if (to->vval.v_job != NULL)
1056 ++to->vval.v_job->jv_refcount;
1057 break;
1058#endif
1059 case VAR_CHANNEL:
1060#ifdef FEAT_JOB_CHANNEL
1061 to->vval.v_channel = from->vval.v_channel;
1062 if (to->vval.v_channel != NULL)
1063 ++to->vval.v_channel->ch_refcount;
1064 break;
1065#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001066 case VAR_INSTR:
1067 to->vval.v_instr = from->vval.v_instr;
1068 break;
1069
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001070 case VAR_STRING:
1071 case VAR_FUNC:
1072 if (from->vval.v_string == NULL)
1073 to->vval.v_string = NULL;
1074 else
1075 {
1076 to->vval.v_string = vim_strsave(from->vval.v_string);
1077 if (from->v_type == VAR_FUNC)
1078 func_ref(to->vval.v_string);
1079 }
1080 break;
1081 case VAR_PARTIAL:
1082 if (from->vval.v_partial == NULL)
1083 to->vval.v_partial = NULL;
1084 else
1085 {
1086 to->vval.v_partial = from->vval.v_partial;
1087 ++to->vval.v_partial->pt_refcount;
1088 }
1089 break;
1090 case VAR_BLOB:
1091 if (from->vval.v_blob == NULL)
1092 to->vval.v_blob = NULL;
1093 else
1094 {
1095 to->vval.v_blob = from->vval.v_blob;
1096 ++to->vval.v_blob->bv_refcount;
1097 }
1098 break;
1099 case VAR_LIST:
1100 if (from->vval.v_list == NULL)
1101 to->vval.v_list = NULL;
1102 else
1103 {
1104 to->vval.v_list = from->vval.v_list;
1105 ++to->vval.v_list->lv_refcount;
1106 }
1107 break;
1108 case VAR_DICT:
1109 if (from->vval.v_dict == NULL)
1110 to->vval.v_dict = NULL;
1111 else
1112 {
1113 to->vval.v_dict = from->vval.v_dict;
1114 ++to->vval.v_dict->dv_refcount;
1115 }
1116 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +02001117 case VAR_VOID:
1118 emsg(_(e_cannot_use_void_value));
1119 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001120 case VAR_UNKNOWN:
1121 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001122 internal_error_no_abort("copy_tv(UNKNOWN)");
1123 break;
1124 }
1125}
1126
1127/*
1128 * Compare "typ1" and "typ2". Put the result in "typ1".
1129 */
1130 int
1131typval_compare(
1132 typval_T *typ1, // first operand
1133 typval_T *typ2, // second operand
Bram Moolenaar657137c2021-01-09 15:45:23 +01001134 exprtype_T type, // operator
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001135 int ic) // ignore case
1136{
1137 int i;
1138 varnumber_T n1, n2;
1139 char_u *s1, *s2;
1140 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1141 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1142
1143 if (type_is && typ1->v_type != typ2->v_type)
1144 {
1145 // For "is" a different type always means FALSE, for "notis"
1146 // it means TRUE.
1147 n1 = (type == EXPR_ISNOT);
1148 }
1149 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
1150 {
1151 if (type_is)
1152 {
1153 n1 = (typ1->v_type == typ2->v_type
1154 && typ1->vval.v_blob == typ2->vval.v_blob);
1155 if (type == EXPR_ISNOT)
1156 n1 = !n1;
1157 }
1158 else if (typ1->v_type != typ2->v_type
1159 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1160 {
1161 if (typ1->v_type != typ2->v_type)
1162 emsg(_("E977: Can only compare Blob with Blob"));
1163 else
1164 emsg(_(e_invalblob));
1165 clear_tv(typ1);
1166 return FAIL;
1167 }
1168 else
1169 {
1170 // Compare two Blobs for being equal or unequal.
1171 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
1172 if (type == EXPR_NEQUAL)
1173 n1 = !n1;
1174 }
1175 }
1176 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
1177 {
1178 if (type_is)
1179 {
1180 n1 = (typ1->v_type == typ2->v_type
1181 && typ1->vval.v_list == typ2->vval.v_list);
1182 if (type == EXPR_ISNOT)
1183 n1 = !n1;
1184 }
1185 else if (typ1->v_type != typ2->v_type
1186 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1187 {
1188 if (typ1->v_type != typ2->v_type)
1189 emsg(_("E691: Can only compare List with List"));
1190 else
1191 emsg(_("E692: Invalid operation for List"));
1192 clear_tv(typ1);
1193 return FAIL;
1194 }
1195 else
1196 {
1197 // Compare two Lists for being equal or unequal.
1198 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
1199 ic, FALSE);
1200 if (type == EXPR_NEQUAL)
1201 n1 = !n1;
1202 }
1203 }
1204
1205 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
1206 {
1207 if (type_is)
1208 {
1209 n1 = (typ1->v_type == typ2->v_type
1210 && typ1->vval.v_dict == typ2->vval.v_dict);
1211 if (type == EXPR_ISNOT)
1212 n1 = !n1;
1213 }
1214 else if (typ1->v_type != typ2->v_type
1215 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1216 {
1217 if (typ1->v_type != typ2->v_type)
1218 emsg(_("E735: Can only compare Dictionary with Dictionary"));
1219 else
1220 emsg(_("E736: Invalid operation for Dictionary"));
1221 clear_tv(typ1);
1222 return FAIL;
1223 }
1224 else
1225 {
1226 // Compare two Dictionaries for being equal or unequal.
1227 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
1228 ic, FALSE);
1229 if (type == EXPR_NEQUAL)
1230 n1 = !n1;
1231 }
1232 }
1233
1234 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
1235 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
1236 {
1237 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1238 && type != EXPR_IS && type != EXPR_ISNOT)
1239 {
1240 emsg(_("E694: Invalid operation for Funcrefs"));
1241 clear_tv(typ1);
1242 return FAIL;
1243 }
1244 if ((typ1->v_type == VAR_PARTIAL
1245 && typ1->vval.v_partial == NULL)
1246 || (typ2->v_type == VAR_PARTIAL
1247 && typ2->vval.v_partial == NULL))
1248 // When both partials are NULL, then they are equal.
1249 // Otherwise they are not equal.
1250 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
1251 else if (type_is)
1252 {
1253 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
1254 // strings are considered the same if their value is
1255 // the same
1256 n1 = tv_equal(typ1, typ2, ic, FALSE);
1257 else if (typ1->v_type == VAR_PARTIAL
1258 && typ2->v_type == VAR_PARTIAL)
1259 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
1260 else
1261 n1 = FALSE;
1262 }
1263 else
1264 n1 = tv_equal(typ1, typ2, ic, FALSE);
1265 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1266 n1 = !n1;
1267 }
1268
1269#ifdef FEAT_FLOAT
1270 // If one of the two variables is a float, compare as a float.
1271 // When using "=~" or "!~", always compare as string.
1272 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
1273 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1274 {
1275 float_T f1, f2;
1276
1277 f1 = tv_get_float(typ1);
1278 f2 = tv_get_float(typ2);
1279 n1 = FALSE;
1280 switch (type)
1281 {
1282 case EXPR_IS:
1283 case EXPR_EQUAL: n1 = (f1 == f2); break;
1284 case EXPR_ISNOT:
1285 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1286 case EXPR_GREATER: n1 = (f1 > f2); break;
1287 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1288 case EXPR_SMALLER: n1 = (f1 < f2); break;
1289 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1290 case EXPR_UNKNOWN:
1291 case EXPR_MATCH:
1292 default: break; // avoid gcc warning
1293 }
1294 }
1295#endif
1296
1297 // If one of the two variables is a number, compare as a number.
1298 // When using "=~" or "!~", always compare as string.
1299 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
1300 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1301 {
1302 n1 = tv_get_number(typ1);
1303 n2 = tv_get_number(typ2);
1304 switch (type)
1305 {
1306 case EXPR_IS:
1307 case EXPR_EQUAL: n1 = (n1 == n2); break;
1308 case EXPR_ISNOT:
1309 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1310 case EXPR_GREATER: n1 = (n1 > n2); break;
1311 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1312 case EXPR_SMALLER: n1 = (n1 < n2); break;
1313 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1314 case EXPR_UNKNOWN:
1315 case EXPR_MATCH:
1316 default: break; // avoid gcc warning
1317 }
1318 }
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001319 else if (in_vim9script() && (typ1->v_type == VAR_BOOL
Bram Moolenaar0c357522021-07-18 14:43:43 +02001320 || typ2->v_type == VAR_BOOL
1321 || (typ1->v_type == VAR_SPECIAL
1322 && typ2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001323 {
1324 if (typ1->v_type != typ2->v_type)
1325 {
1326 semsg(_(e_cannot_compare_str_with_str),
1327 vartype_name(typ1->v_type), vartype_name(typ2->v_type));
1328 clear_tv(typ1);
1329 return FAIL;
1330 }
1331 n1 = typ1->vval.v_number;
1332 n2 = typ2->vval.v_number;
1333 switch (type)
1334 {
1335 case EXPR_IS:
1336 case EXPR_EQUAL: n1 = (n1 == n2); break;
1337 case EXPR_ISNOT:
1338 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1339 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001340 semsg(_(e_invalid_operation_for_str),
1341 vartype_name(typ1->v_type));
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001342 clear_tv(typ1);
1343 return FAIL;
1344 }
1345 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001346 else
1347 {
Bram Moolenaar0c357522021-07-18 14:43:43 +02001348 if (in_vim9script()
1349 && ((typ1->v_type != VAR_STRING && typ1->v_type != VAR_SPECIAL)
1350 || (typ2->v_type != VAR_STRING && typ2->v_type != VAR_SPECIAL)))
1351 {
1352 semsg(_(e_cannot_compare_str_with_str),
1353 vartype_name(typ1->v_type), vartype_name(typ2->v_type));
1354 clear_tv(typ1);
1355 return FAIL;
1356 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001357 s1 = tv_get_string_buf(typ1, buf1);
1358 s2 = tv_get_string_buf(typ2, buf2);
1359 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1360 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1361 else
1362 i = 0;
1363 n1 = FALSE;
1364 switch (type)
1365 {
1366 case EXPR_IS:
1367 case EXPR_EQUAL: n1 = (i == 0); break;
1368 case EXPR_ISNOT:
1369 case EXPR_NEQUAL: n1 = (i != 0); break;
1370 case EXPR_GREATER: n1 = (i > 0); break;
1371 case EXPR_GEQUAL: n1 = (i >= 0); break;
1372 case EXPR_SMALLER: n1 = (i < 0); break;
1373 case EXPR_SEQUAL: n1 = (i <= 0); break;
1374
1375 case EXPR_MATCH:
1376 case EXPR_NOMATCH:
1377 n1 = pattern_match(s2, s1, ic);
1378 if (type == EXPR_NOMATCH)
1379 n1 = !n1;
1380 break;
1381
1382 default: break; // avoid gcc warning
1383 }
1384 }
1385 clear_tv(typ1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001386 if (in_vim9script())
1387 {
1388 typ1->v_type = VAR_BOOL;
1389 typ1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
1390 }
1391 else
1392 {
1393 typ1->v_type = VAR_NUMBER;
1394 typ1->vval.v_number = n1;
1395 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001396
1397 return OK;
1398}
1399
Bram Moolenaar34453202021-01-31 13:08:38 +01001400/*
1401 * Convert any type to a string, never give an error.
1402 * When "quotes" is TRUE add quotes to a string.
1403 * Returns an allocated string.
1404 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001405 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001406typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001407{
1408 char_u *tofree;
1409 char_u numbuf[NUMBUFLEN];
1410 char_u *ret = NULL;
1411
1412 if (arg == NULL)
1413 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001414 if (!quotes && arg->v_type == VAR_STRING)
1415 {
1416 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1417 : arg->vval.v_string);
1418 }
1419 else
1420 {
1421 ret = tv2string(arg, &tofree, numbuf, 0);
1422 // Make a copy if we have a value but it's not in allocated memory.
1423 if (ret != NULL && tofree == NULL)
1424 ret = vim_strsave(ret);
1425 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001426 return ret;
1427}
1428
1429/*
1430 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1431 * or it refers to a List or Dictionary that is locked.
1432 */
1433 int
1434tv_islocked(typval_T *tv)
1435{
1436 return (tv->v_lock & VAR_LOCKED)
1437 || (tv->v_type == VAR_LIST
1438 && tv->vval.v_list != NULL
1439 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1440 || (tv->v_type == VAR_DICT
1441 && tv->vval.v_dict != NULL
1442 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1443}
1444
1445 static int
1446func_equal(
1447 typval_T *tv1,
1448 typval_T *tv2,
1449 int ic) // ignore case
1450{
1451 char_u *s1, *s2;
1452 dict_T *d1, *d2;
1453 int a1, a2;
1454 int i;
1455
1456 // empty and NULL function name considered the same
1457 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1458 : partial_name(tv1->vval.v_partial);
1459 if (s1 != NULL && *s1 == NUL)
1460 s1 = NULL;
1461 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1462 : partial_name(tv2->vval.v_partial);
1463 if (s2 != NULL && *s2 == NUL)
1464 s2 = NULL;
1465 if (s1 == NULL || s2 == NULL)
1466 {
1467 if (s1 != s2)
1468 return FALSE;
1469 }
1470 else if (STRCMP(s1, s2) != 0)
1471 return FALSE;
1472
1473 // empty dict and NULL dict is different
1474 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1475 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1476 if (d1 == NULL || d2 == NULL)
1477 {
1478 if (d1 != d2)
1479 return FALSE;
1480 }
1481 else if (!dict_equal(d1, d2, ic, TRUE))
1482 return FALSE;
1483
1484 // empty list and no list considered the same
1485 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1486 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1487 if (a1 != a2)
1488 return FALSE;
1489 for (i = 0; i < a1; ++i)
1490 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1491 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1492 return FALSE;
1493
1494 return TRUE;
1495}
1496
1497/*
1498 * Return TRUE if "tv1" and "tv2" have the same value.
1499 * Compares the items just like "==" would compare them, but strings and
1500 * numbers are different. Floats and numbers are also different.
1501 */
1502 int
1503tv_equal(
1504 typval_T *tv1,
1505 typval_T *tv2,
1506 int ic, // ignore case
1507 int recursive) // TRUE when used recursively
1508{
1509 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1510 char_u *s1, *s2;
1511 static int recursive_cnt = 0; // catch recursive loops
1512 int r;
1513 static int tv_equal_recurse_limit;
1514
1515 // Catch lists and dicts that have an endless loop by limiting
1516 // recursiveness to a limit. We guess they are equal then.
1517 // A fixed limit has the problem of still taking an awful long time.
1518 // Reduce the limit every time running into it. That should work fine for
1519 // deeply linked structures that are not recursively linked and catch
1520 // recursiveness quickly.
1521 if (!recursive)
1522 tv_equal_recurse_limit = 1000;
1523 if (recursive_cnt >= tv_equal_recurse_limit)
1524 {
1525 --tv_equal_recurse_limit;
1526 return TRUE;
1527 }
1528
1529 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1530 // arguments.
1531 if ((tv1->v_type == VAR_FUNC
1532 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1533 && (tv2->v_type == VAR_FUNC
1534 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1535 {
1536 ++recursive_cnt;
1537 r = func_equal(tv1, tv2, ic);
1538 --recursive_cnt;
1539 return r;
1540 }
1541
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001542 if (tv1->v_type != tv2->v_type
1543 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1544 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001545 return FALSE;
1546
1547 switch (tv1->v_type)
1548 {
1549 case VAR_LIST:
1550 ++recursive_cnt;
1551 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1552 --recursive_cnt;
1553 return r;
1554
1555 case VAR_DICT:
1556 ++recursive_cnt;
1557 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1558 --recursive_cnt;
1559 return r;
1560
1561 case VAR_BLOB:
1562 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1563
1564 case VAR_NUMBER:
1565 case VAR_BOOL:
1566 case VAR_SPECIAL:
1567 return tv1->vval.v_number == tv2->vval.v_number;
1568
1569 case VAR_STRING:
1570 s1 = tv_get_string_buf(tv1, buf1);
1571 s2 = tv_get_string_buf(tv2, buf2);
1572 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1573
1574 case VAR_FLOAT:
1575#ifdef FEAT_FLOAT
1576 return tv1->vval.v_float == tv2->vval.v_float;
1577#endif
1578 case VAR_JOB:
1579#ifdef FEAT_JOB_CHANNEL
1580 return tv1->vval.v_job == tv2->vval.v_job;
1581#endif
1582 case VAR_CHANNEL:
1583#ifdef FEAT_JOB_CHANNEL
1584 return tv1->vval.v_channel == tv2->vval.v_channel;
1585#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001586 case VAR_INSTR:
1587 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001588
1589 case VAR_PARTIAL:
1590 return tv1->vval.v_partial == tv2->vval.v_partial;
1591
1592 case VAR_FUNC:
1593 return tv1->vval.v_string == tv2->vval.v_string;
1594
1595 case VAR_UNKNOWN:
1596 case VAR_ANY:
1597 case VAR_VOID:
1598 break;
1599 }
1600
1601 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1602 // does not equal anything, not even itself.
1603 return FALSE;
1604}
1605
1606/*
1607 * Get an option value.
1608 * "arg" points to the '&' or '+' before the option name.
1609 * "arg" is advanced to character after the option name.
1610 * Return OK or FAIL.
1611 */
1612 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001613eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001614 char_u **arg,
1615 typval_T *rettv, // when NULL, only check if option exists
1616 int evaluate)
1617{
1618 char_u *option_end;
1619 long numval;
1620 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001621 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001622 int c;
1623 int working = (**arg == '+'); // has("+option")
1624 int ret = OK;
1625 int opt_flags;
1626
1627 // Isolate the option name and find its value.
1628 option_end = find_option_end(arg, &opt_flags);
1629 if (option_end == NULL)
1630 {
1631 if (rettv != NULL)
1632 semsg(_("E112: Option name missing: %s"), *arg);
1633 return FAIL;
1634 }
1635
1636 if (!evaluate)
1637 {
1638 *arg = option_end;
1639 return OK;
1640 }
1641
1642 c = *option_end;
1643 *option_end = NUL;
1644 opt_type = get_option_value(*arg, &numval,
1645 rettv == NULL ? NULL : &stringval, opt_flags);
1646
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001647 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001648 {
1649 if (rettv != NULL)
1650 semsg(_(e_unknown_option), *arg);
1651 ret = FAIL;
1652 }
1653 else if (rettv != NULL)
1654 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001655 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001656 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001657 {
1658 rettv->v_type = VAR_STRING;
1659 rettv->vval.v_string = NULL;
1660 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001661 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001662 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001663 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1664 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001665 rettv->vval.v_number = 0;
1666 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001667 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001668 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001669 if (in_vim9script() && opt_type == gov_bool)
1670 {
1671 rettv->v_type = VAR_BOOL;
1672 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1673 }
1674 else
1675 {
1676 rettv->v_type = VAR_NUMBER;
1677 rettv->vval.v_number = numval;
1678 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001679 }
1680 else // string option
1681 {
1682 rettv->v_type = VAR_STRING;
1683 rettv->vval.v_string = stringval;
1684 }
1685 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001686 else if (working && (opt_type == gov_hidden_bool
1687 || opt_type == gov_hidden_number
1688 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001689 ret = FAIL;
1690
1691 *option_end = c; // put back for error messages
1692 *arg = option_end;
1693
1694 return ret;
1695}
1696
1697/*
1698 * Allocate a variable for a number constant. Also deals with "0z" for blob.
1699 * Return OK or FAIL.
1700 */
1701 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001702eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001703 char_u **arg,
1704 typval_T *rettv,
1705 int evaluate,
1706 int want_string UNUSED)
1707{
1708 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02001709 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001710#ifdef FEAT_FLOAT
1711 char_u *p;
1712 int get_float = FALSE;
1713
1714 // We accept a float when the format matches
1715 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
1716 // strict to avoid backwards compatibility problems.
1717 // With script version 2 and later the leading digit can be
1718 // omitted.
1719 // Don't look for a float after the "." operator, so that
1720 // ":let vers = 1.2.3" doesn't fail.
1721 if (**arg == '.')
1722 p = *arg;
1723 else
Bram Moolenaar29500652021-08-08 15:43:34 +02001724 {
1725 p = *arg + 1;
1726 if (skip_quotes)
1727 for (;;)
1728 {
1729 if (*p == '\'')
1730 ++p;
1731 if (!vim_isdigit(*p))
1732 break;
1733 p = skipdigits(p);
1734 }
1735 else
1736 p = skipdigits(p);
1737 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001738 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
1739 {
1740 get_float = TRUE;
1741 p = skipdigits(p + 2);
1742 if (*p == 'e' || *p == 'E')
1743 {
1744 ++p;
1745 if (*p == '-' || *p == '+')
1746 ++p;
1747 if (!vim_isdigit(*p))
1748 get_float = FALSE;
1749 else
1750 p = skipdigits(p + 1);
1751 }
1752 if (ASCII_ISALPHA(*p) || *p == '.')
1753 get_float = FALSE;
1754 }
1755 if (get_float)
1756 {
1757 float_T f;
1758
Bram Moolenaar29500652021-08-08 15:43:34 +02001759 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001760 if (evaluate)
1761 {
1762 rettv->v_type = VAR_FLOAT;
1763 rettv->vval.v_float = f;
1764 }
1765 }
1766 else
1767#endif
1768 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
1769 {
1770 char_u *bp;
1771 blob_T *blob = NULL; // init for gcc
1772
1773 // Blob constant: 0z0123456789abcdef
1774 if (evaluate)
1775 blob = blob_alloc();
1776 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
1777 {
1778 if (!vim_isxdigit(bp[1]))
1779 {
1780 if (blob != NULL)
1781 {
1782 emsg(_("E973: Blob literal should have an even number of hex characters"));
1783 ga_clear(&blob->bv_ga);
1784 VIM_CLEAR(blob);
1785 }
1786 return FAIL;
1787 }
1788 if (blob != NULL)
1789 ga_append(&blob->bv_ga,
1790 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
1791 if (bp[2] == '.' && vim_isxdigit(bp[3]))
1792 ++bp;
1793 }
1794 if (blob != NULL)
1795 rettv_blob_set(rettv, blob);
1796 *arg = bp;
1797 }
1798 else
1799 {
1800 varnumber_T n;
1801
1802 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02001803 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001804 ? STR2NR_NO_OCT + STR2NR_QUOTE
1805 : STR2NR_ALL, &n, NULL, 0, TRUE);
1806 if (len == 0)
1807 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001808 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001809 return FAIL;
1810 }
1811 *arg += len;
1812 if (evaluate)
1813 {
1814 rettv->v_type = VAR_NUMBER;
1815 rettv->vval.v_number = n;
1816 }
1817 }
1818 return OK;
1819}
1820
1821/*
1822 * Allocate a variable for a string constant.
1823 * Return OK or FAIL.
1824 */
1825 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001826eval_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001827{
1828 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001829 char_u *end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001830 int extra = 0;
1831 int len;
1832
1833 // Find the end of the string, skipping backslashed characters.
1834 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
1835 {
1836 if (*p == '\\' && p[1] != NUL)
1837 {
1838 ++p;
1839 // A "\<x>" form occupies at least 4 characters, and produces up
1840 // to 21 characters (3 * 6 for the char and 3 for a modifier):
1841 // reserve space for 18 extra.
1842 // Each byte in the char could be encoded as K_SPECIAL K_EXTRA x.
1843 if (*p == '<')
1844 extra += 18;
1845 }
1846 }
1847
1848 if (*p != '"')
1849 {
1850 semsg(_("E114: Missing quote: %s"), *arg);
1851 return FAIL;
1852 }
1853
1854 // If only parsing, set *arg and return here
1855 if (!evaluate)
1856 {
1857 *arg = p + 1;
1858 return OK;
1859 }
1860
1861 // Copy the string into allocated memory, handling backslashed
1862 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001863 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001864 len = (int)(p - *arg + extra);
1865 rettv->vval.v_string = alloc(len);
1866 if (rettv->vval.v_string == NULL)
1867 return FAIL;
1868 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001869
1870 for (p = *arg + 1; *p != NUL && *p != '"'; )
1871 {
1872 if (*p == '\\')
1873 {
1874 switch (*++p)
1875 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001876 case 'b': *end++ = BS; ++p; break;
1877 case 'e': *end++ = ESC; ++p; break;
1878 case 'f': *end++ = FF; ++p; break;
1879 case 'n': *end++ = NL; ++p; break;
1880 case 'r': *end++ = CAR; ++p; break;
1881 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001882
1883 case 'X': // hex: "\x1", "\x12"
1884 case 'x':
1885 case 'u': // Unicode: "\u0023"
1886 case 'U':
1887 if (vim_isxdigit(p[1]))
1888 {
1889 int n, nr;
1890 int c = toupper(*p);
1891
1892 if (c == 'X')
1893 n = 2;
1894 else if (*p == 'u')
1895 n = 4;
1896 else
1897 n = 8;
1898 nr = 0;
1899 while (--n >= 0 && vim_isxdigit(p[1]))
1900 {
1901 ++p;
1902 nr = (nr << 4) + hex2nr(*p);
1903 }
1904 ++p;
1905 // For "\u" store the number according to
1906 // 'encoding'.
1907 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001908 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001909 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001910 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001911 }
1912 break;
1913
1914 // octal: "\1", "\12", "\123"
1915 case '0':
1916 case '1':
1917 case '2':
1918 case '3':
1919 case '4':
1920 case '5':
1921 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001922 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001923 if (*p >= '0' && *p <= '7')
1924 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001925 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001926 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001927 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001928 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001929 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001930 break;
1931
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001932 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001933 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001934 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001935 int flags = FSK_KEYCODE | FSK_IN_STRING;
1936
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001937 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001938 flags |= FSK_SIMPLIFY;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001939 extra = trans_special(&p, end, flags, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001940 if (extra != 0)
1941 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001942 end += extra;
1943 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001944 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001945 break;
1946 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001947 }
1948 // FALLTHROUGH
1949
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001950 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001951 break;
1952 }
1953 }
1954 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001955 MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001956 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001957 *end = NUL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001958 if (*p != NUL) // just in case
1959 ++p;
1960 *arg = p;
1961
1962 return OK;
1963}
1964
1965/*
1966 * Allocate a variable for a 'str''ing' constant.
1967 * Return OK or FAIL.
1968 */
1969 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001970eval_lit_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001971{
1972 char_u *p;
1973 char_u *str;
1974 int reduce = 0;
1975
1976 // Find the end of the string, skipping ''.
1977 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
1978 {
1979 if (*p == '\'')
1980 {
1981 if (p[1] != '\'')
1982 break;
1983 ++reduce;
1984 ++p;
1985 }
1986 }
1987
1988 if (*p != '\'')
1989 {
1990 semsg(_("E115: Missing quote: %s"), *arg);
1991 return FAIL;
1992 }
1993
1994 // If only parsing return after setting "*arg"
1995 if (!evaluate)
1996 {
1997 *arg = p + 1;
1998 return OK;
1999 }
2000
2001 // Copy the string into allocated memory, handling '' to ' reduction.
2002 str = alloc((p - *arg) - reduce);
2003 if (str == NULL)
2004 return FAIL;
2005 rettv->v_type = VAR_STRING;
2006 rettv->vval.v_string = str;
2007
2008 for (p = *arg + 1; *p != NUL; )
2009 {
2010 if (*p == '\'')
2011 {
2012 if (p[1] != '\'')
2013 break;
2014 ++p;
2015 }
2016 MB_COPY_CHAR(p, str);
2017 }
2018 *str = NUL;
2019 *arg = p + 1;
2020
2021 return OK;
2022}
2023
2024/*
2025 * Return a string with the string representation of a variable.
2026 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2027 * "numbuf" is used for a number.
2028 * Puts quotes around strings, so that they can be parsed back by eval().
2029 * May return NULL.
2030 */
2031 char_u *
2032tv2string(
2033 typval_T *tv,
2034 char_u **tofree,
2035 char_u *numbuf,
2036 int copyID)
2037{
2038 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2039}
2040
2041/*
2042 * Get the value of an environment variable.
2043 * "arg" is pointing to the '$'. It is advanced to after the name.
2044 * If the environment variable was not set, silently assume it is empty.
2045 * Return FAIL if the name is invalid.
2046 */
2047 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002048eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002049{
2050 char_u *string = NULL;
2051 int len;
2052 int cc;
2053 char_u *name;
2054 int mustfree = FALSE;
2055
2056 ++*arg;
2057 name = *arg;
2058 len = get_env_len(arg);
2059 if (evaluate)
2060 {
2061 if (len == 0)
2062 return FAIL; // invalid empty name
2063
2064 cc = name[len];
2065 name[len] = NUL;
2066 // first try vim_getenv(), fast for normal environment vars
2067 string = vim_getenv(name, &mustfree);
2068 if (string != NULL && *string != NUL)
2069 {
2070 if (!mustfree)
2071 string = vim_strsave(string);
2072 }
2073 else
2074 {
2075 if (mustfree)
2076 vim_free(string);
2077
2078 // next try expanding things like $VIM and ${HOME}
2079 string = expand_env_save(name - 1);
2080 if (string != NULL && *string == '$')
2081 VIM_CLEAR(string);
2082 }
2083 name[len] = cc;
2084
2085 rettv->v_type = VAR_STRING;
2086 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002087 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002088 }
2089
2090 return OK;
2091}
2092
2093/*
2094 * Get the lnum from the first argument.
2095 * Also accepts ".", "$", etc., but that only works for the current buffer.
2096 * Returns -1 on error.
2097 */
2098 linenr_T
2099tv_get_lnum(typval_T *argvars)
2100{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002101 linenr_T lnum = -1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002102
Bram Moolenaar56acb092020-08-16 14:48:19 +02002103 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2104 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002105 if (lnum <= 0 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002106 {
2107 int fnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002108 pos_T *fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002109
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002110 // no valid number, try using arg like line()
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002111 if (fp != NULL)
2112 lnum = fp->lnum;
2113 }
2114 return lnum;
2115}
2116
2117/*
2118 * Get the lnum from the first argument.
2119 * Also accepts "$", then "buf" is used.
2120 * Returns 0 on error.
2121 */
2122 linenr_T
2123tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2124{
2125 if (argvars[0].v_type == VAR_STRING
2126 && argvars[0].vval.v_string != NULL
2127 && argvars[0].vval.v_string[0] == '$'
2128 && buf != NULL)
2129 return buf->b_ml.ml_line_count;
2130 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2131}
2132
2133/*
2134 * Get buffer by number or pattern.
2135 */
2136 buf_T *
2137tv_get_buf(typval_T *tv, int curtab_only)
2138{
2139 char_u *name = tv->vval.v_string;
2140 buf_T *buf;
2141
2142 if (tv->v_type == VAR_NUMBER)
2143 return buflist_findnr((int)tv->vval.v_number);
2144 if (tv->v_type != VAR_STRING)
2145 return NULL;
2146 if (name == NULL || *name == NUL)
2147 return curbuf;
2148 if (name[0] == '$' && name[1] == NUL)
2149 return lastbuf;
2150
2151 buf = buflist_find_by_name(name, curtab_only);
2152
2153 // If not found, try expanding the name, like done for bufexists().
2154 if (buf == NULL)
2155 buf = find_buffer(tv);
2156
2157 return buf;
2158}
2159
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002160/*
2161 * Like tv_get_buf() but give an error message is the type is wrong.
2162 */
2163 buf_T *
2164tv_get_buf_from_arg(typval_T *tv)
2165{
2166 buf_T *buf;
2167
2168 ++emsg_off;
2169 buf = tv_get_buf(tv, FALSE);
2170 --emsg_off;
2171 if (buf == NULL
2172 && tv->v_type != VAR_NUMBER
2173 && tv->v_type != VAR_STRING)
2174 // issue errmsg for type error
2175 (void)tv_get_number(tv);
2176 return buf;
2177}
2178
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002179#endif // FEAT_EVAL