blob: 61f8eb55b77cf03796104062bb02eaeb2d637ab2 [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 Lakshmanana9a7c0c2021-07-17 19:11:07 +0200425 * Give an error and return FAIL unless "args[idx]" is a bool.
426 */
427 int
428check_for_bool_arg(typval_T *args, int idx)
429{
430 if (args[idx].v_type != VAR_BOOL
431 && !(args[idx].v_type == VAR_NUMBER
432 && (args[idx].vval.v_number == 0
433 || args[idx].vval.v_number == 1)))
434 {
435 if (idx >= 0)
436 semsg(_(e_bool_required_for_argument_nr), idx + 1);
437 else
438 emsg(_(e_boolreq));
439 return FAIL;
440 }
441 return OK;
442}
443
444/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200445 * Check for an optional bool argument at 'idx'
446 */
447 int
448check_for_opt_bool_arg(typval_T *args, int idx)
449{
450 return (args[idx].v_type == VAR_UNKNOWN
451 || check_for_bool_arg(args, idx) != FAIL);
452}
453
454/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200455 * Give an error and return FAIL unless "args[idx]" is a list.
456 */
457 int
458check_for_list_arg(typval_T *args, int idx)
459{
460 if (args[idx].v_type != VAR_LIST)
461 {
462 if (idx >= 0)
463 semsg(_(e_list_required_for_argument_nr), idx + 1);
464 else
465 emsg(_(e_listreq));
466 return FAIL;
467 }
468 return OK;
469}
470
471/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200472 * Check for an optional list argument at 'idx'
473 */
474 int
475check_for_opt_list_arg(typval_T *args, int idx)
476{
477 return (args[idx].v_type == VAR_UNKNOWN
478 || check_for_list_arg(args, idx) != FAIL);
479}
480
481/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200482 * Give an error and return FAIL unless "args[idx]" is a dict.
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200483 */
484 int
485check_for_dict_arg(typval_T *args, int idx)
486{
487 if (args[idx].v_type != VAR_DICT)
488 {
489 if (idx >= 0)
490 semsg(_(e_dict_required_for_argument_nr), idx + 1);
491 else
492 emsg(_(e_dictreq));
493 return FAIL;
494 }
495 return OK;
496}
497
498/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200499 * Check for an optional dict argument at 'idx'
500 */
501 int
502check_for_opt_dict_arg(typval_T *args, int idx)
503{
504 return (args[idx].v_type == VAR_UNKNOWN
505 || check_for_dict_arg(args, idx) != FAIL);
506}
507
508/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200509 * Give an error and return FAIL unless "args[idx]" is a channel or a job.
510 */
511 int
512check_for_chan_or_job_arg(typval_T *args, int idx)
513{
514 if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
515 {
516 if (idx >= 0)
517 semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
518 else
519 emsg(_(e_chan_or_job_req));
520 return FAIL;
521 }
522 return OK;
523}
524
525/*
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200526 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
527 * job.
528 */
529 int
530check_for_opt_chan_or_job_arg(typval_T *args, int idx)
531{
532 return (args[idx].v_type == VAR_UNKNOWN
533 || check_for_chan_or_job_arg(args, idx) != FAIL);
534}
535
536/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200537 * Give an error and return FAIL unless "args[idx]" is a job.
538 */
539 int
540check_for_job_arg(typval_T *args, int idx)
541{
542 if (args[idx].v_type != VAR_JOB)
543 {
544 if (idx >= 0)
545 semsg(_(e_job_required_for_argument_nr), idx + 1);
546 else
547 emsg(_(e_jobreq));
548 return FAIL;
549 }
550 return OK;
551}
552
553/*
554 * Give an error and return FAIL unless "args[idx]" is a string or
555 * a number.
556 */
557 int
558check_for_string_or_number_arg(typval_T *args, int idx)
559{
560 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
561 {
562 if (idx >= 0)
563 semsg(_(e_string_required_for_argument_nr), idx + 1);
564 else
565 emsg(_(e_stringreq));
566 return FAIL;
567 }
568 return OK;
569}
570
571/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200572 * Check for an optional string or number argument at 'idx'.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200573 */
574 int
575check_for_opt_string_or_number_arg(typval_T *args, int idx)
576{
577 return (args[idx].v_type == VAR_UNKNOWN
578 || check_for_string_or_number_arg(args, idx) != FAIL);
579}
580
581/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200582 * Give an error and return FAIL unless "args[idx]" is a buffer number.
583 * Buffer number can be a number or a string.
584 */
585 int
586check_for_buffer_arg(typval_T *args, int idx)
587{
588 return check_for_string_or_number_arg(args, idx);
589}
590
591/*
592 * Check for an optional buffer argument at 'idx'
593 */
594 int
595check_for_opt_buffer_arg(typval_T *args, int idx)
596{
597 return (args[idx].v_type == VAR_UNKNOWN
598 || check_for_buffer_arg(args, idx));
599}
600
601/*
602 * Give an error and return FAIL unless "args[idx]" is a line number.
603 * Line number can be a number or a string.
604 */
605 int
606check_for_lnum_arg(typval_T *args, int idx)
607{
608 return check_for_string_or_number_arg(args, idx);
609}
610
611/*
612 * Check for an optional line number argument at 'idx'
613 */
614 int
615check_for_opt_lnum_arg(typval_T *args, int idx)
616{
617 return (args[idx].v_type == VAR_UNKNOWN
618 || check_for_lnum_arg(args, idx));
619}
620
621/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200622 * Give an error and return FAIL unless "args[idx]" is a string or a blob.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200623 */
624 int
625check_for_string_or_blob_arg(typval_T *args, int idx)
626{
627 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
628 {
629 if (idx >= 0)
630 semsg(_(e_string_required_for_argument_nr), idx + 1);
631 else
632 emsg(_(e_stringreq));
633 return FAIL;
634 }
635 return OK;
636}
637
638/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200639 * Give an error and return FAIL unless "args[idx]" is a string or a list.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200640 */
641 int
642check_for_string_or_list_arg(typval_T *args, int idx)
643{
644 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
645 {
646 if (idx >= 0)
647 semsg(_(e_string_required_for_argument_nr), idx + 1);
648 else
649 emsg(_(e_stringreq));
650 return FAIL;
651 }
652 return OK;
653}
654
655/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200656 * Give an error and return FAIL unless "args[idx]" is a list or a blob.
657 */
658 int
659check_for_list_or_blob_arg(typval_T *args, int idx)
660{
661 if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
662 {
663 if (idx >= 0)
664 semsg(_(e_list_required_for_argument_nr), idx + 1);
665 else
666 emsg(_(e_listreq));
667 return FAIL;
668 }
669 return OK;
670}
671
672/*
673 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
674 * blob.
675 */
676 int
677check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
678{
679 if (args[idx].v_type != VAR_LIST
680 && args[idx].v_type != VAR_DICT
681 && args[idx].v_type != VAR_BLOB)
682 {
683 if (idx >= 0)
684 semsg(_(e_list_required_for_argument_nr), idx + 1);
685 else
686 emsg(_(e_listreq));
687 return FAIL;
688 }
689 return OK;
690}
691
692/*
693 * Give an error and return FAIL unless "args[idx]" is a buffer number or a
694 * dict.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200695 */
696 int
697check_for_buffer_or_dict_arg(typval_T *args, int idx)
698{
699 if (args[idx].v_type != VAR_STRING
700 && args[idx].v_type != VAR_NUMBER
701 && args[idx].v_type != VAR_DICT)
702 {
703 if (idx >= 0)
704 semsg(_(e_string_required_for_argument_nr), idx + 1);
705 else
706 emsg(_(e_stringreq));
707 return FAIL;
708 }
709 return OK;
710}
711
712/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200713 * Get the string value of a variable.
714 * If it is a Number variable, the number is converted into a string.
715 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
716 * tv_get_string_buf() uses a given buffer.
717 * If the String variable has never been set, return an empty string.
718 * Never returns NULL;
719 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
720 * NULL on error.
721 */
722 char_u *
723tv_get_string(typval_T *varp)
724{
725 static char_u mybuf[NUMBUFLEN];
726
727 return tv_get_string_buf(varp, mybuf);
728}
729
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100730/*
731 * Like tv_get_string() but don't allow number to string conversion for Vim9.
732 */
733 char_u *
734tv_get_string_strict(typval_T *varp)
735{
736 static char_u mybuf[NUMBUFLEN];
737 char_u *res = tv_get_string_buf_chk_strict(
738 varp, mybuf, in_vim9script());
739
740 return res != NULL ? res : (char_u *)"";
741}
742
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200743 char_u *
744tv_get_string_buf(typval_T *varp, char_u *buf)
745{
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200746 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200747
748 return res != NULL ? res : (char_u *)"";
749}
750
751/*
752 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
753 */
754 char_u *
755tv_get_string_chk(typval_T *varp)
756{
757 static char_u mybuf[NUMBUFLEN];
758
759 return tv_get_string_buf_chk(varp, mybuf);
760}
761
762 char_u *
763tv_get_string_buf_chk(typval_T *varp, char_u *buf)
764{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100765 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
766}
767
768 char_u *
769tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
770{
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200771 switch (varp->v_type)
772 {
773 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100774 if (strict)
775 {
776 emsg(_(e_using_number_as_string));
777 break;
778 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200779 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
780 (varnumber_T)varp->vval.v_number);
781 return buf;
782 case VAR_FUNC:
783 case VAR_PARTIAL:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200784 emsg(_("E729: Using a Funcref as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200785 break;
786 case VAR_LIST:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200787 emsg(_("E730: Using a List as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200788 break;
789 case VAR_DICT:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200790 emsg(_("E731: Using a Dictionary as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200791 break;
792 case VAR_FLOAT:
793#ifdef FEAT_FLOAT
Bram Moolenaar7a2217b2021-06-06 12:33:49 +0200794 if (strict)
795 {
796 emsg(_(e_float_as_string));
797 break;
798 }
799 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
800 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200801#endif
802 case VAR_STRING:
803 if (varp->vval.v_string != NULL)
804 return varp->vval.v_string;
805 return (char_u *)"";
806 case VAR_BOOL:
807 case VAR_SPECIAL:
808 STRCPY(buf, get_var_special_name(varp->vval.v_number));
809 return buf;
810 case VAR_BLOB:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200811 emsg(_("E976: Using a Blob as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200812 break;
813 case VAR_JOB:
814#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200815 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200816 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200817 semsg(_(e_using_invalid_value_as_string_str), "job");
818 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200819 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200820 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200821#endif
822 break;
823 case VAR_CHANNEL:
824#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200825 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200826 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200827 semsg(_(e_using_invalid_value_as_string_str), "channel");
828 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200829 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200830 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200831#endif
832 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200833 case VAR_VOID:
834 emsg(_(e_cannot_use_void_value));
835 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200836 case VAR_UNKNOWN:
837 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200838 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +0200839 semsg(_(e_using_invalid_value_as_string_str),
840 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200841 break;
842 }
843 return NULL;
844}
845
846/*
847 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
848 * string() on Dict, List, etc.
849 */
850 char_u *
851tv_stringify(typval_T *varp, char_u *buf)
852{
853 if (varp->v_type == VAR_LIST
854 || varp->v_type == VAR_DICT
855 || varp->v_type == VAR_BLOB
856 || varp->v_type == VAR_FUNC
857 || varp->v_type == VAR_PARTIAL
858 || varp->v_type == VAR_FLOAT)
859 {
860 typval_T tmp;
861
862 f_string(varp, &tmp);
863 tv_get_string_buf(&tmp, buf);
864 clear_tv(varp);
865 *varp = tmp;
866 return tmp.vval.v_string;
867 }
868 return tv_get_string_buf(varp, buf);
869}
870
871/*
872 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
873 * Also give an error message, using "name" or _("name") when use_gettext is
874 * TRUE.
875 */
876 int
877tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
878{
879 int lock = 0;
880
881 switch (tv->v_type)
882 {
883 case VAR_BLOB:
884 if (tv->vval.v_blob != NULL)
885 lock = tv->vval.v_blob->bv_lock;
886 break;
887 case VAR_LIST:
888 if (tv->vval.v_list != NULL)
889 lock = tv->vval.v_list->lv_lock;
890 break;
891 case VAR_DICT:
892 if (tv->vval.v_dict != NULL)
893 lock = tv->vval.v_dict->dv_lock;
894 break;
895 default:
896 break;
897 }
Bram Moolenaara187c432020-09-16 21:08:28 +0200898 return value_check_lock(tv->v_lock, name, use_gettext)
899 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200900}
901
902/*
903 * Copy the values from typval_T "from" to typval_T "to".
904 * When needed allocates string or increases reference count.
905 * Does not make a copy of a list, blob or dict but copies the reference!
906 * It is OK for "from" and "to" to point to the same item. This is used to
907 * make a copy later.
908 */
909 void
910copy_tv(typval_T *from, typval_T *to)
911{
912 to->v_type = from->v_type;
913 to->v_lock = 0;
914 switch (from->v_type)
915 {
916 case VAR_NUMBER:
917 case VAR_BOOL:
918 case VAR_SPECIAL:
919 to->vval.v_number = from->vval.v_number;
920 break;
921 case VAR_FLOAT:
922#ifdef FEAT_FLOAT
923 to->vval.v_float = from->vval.v_float;
924 break;
925#endif
926 case VAR_JOB:
927#ifdef FEAT_JOB_CHANNEL
928 to->vval.v_job = from->vval.v_job;
929 if (to->vval.v_job != NULL)
930 ++to->vval.v_job->jv_refcount;
931 break;
932#endif
933 case VAR_CHANNEL:
934#ifdef FEAT_JOB_CHANNEL
935 to->vval.v_channel = from->vval.v_channel;
936 if (to->vval.v_channel != NULL)
937 ++to->vval.v_channel->ch_refcount;
938 break;
939#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200940 case VAR_INSTR:
941 to->vval.v_instr = from->vval.v_instr;
942 break;
943
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200944 case VAR_STRING:
945 case VAR_FUNC:
946 if (from->vval.v_string == NULL)
947 to->vval.v_string = NULL;
948 else
949 {
950 to->vval.v_string = vim_strsave(from->vval.v_string);
951 if (from->v_type == VAR_FUNC)
952 func_ref(to->vval.v_string);
953 }
954 break;
955 case VAR_PARTIAL:
956 if (from->vval.v_partial == NULL)
957 to->vval.v_partial = NULL;
958 else
959 {
960 to->vval.v_partial = from->vval.v_partial;
961 ++to->vval.v_partial->pt_refcount;
962 }
963 break;
964 case VAR_BLOB:
965 if (from->vval.v_blob == NULL)
966 to->vval.v_blob = NULL;
967 else
968 {
969 to->vval.v_blob = from->vval.v_blob;
970 ++to->vval.v_blob->bv_refcount;
971 }
972 break;
973 case VAR_LIST:
974 if (from->vval.v_list == NULL)
975 to->vval.v_list = NULL;
976 else
977 {
978 to->vval.v_list = from->vval.v_list;
979 ++to->vval.v_list->lv_refcount;
980 }
981 break;
982 case VAR_DICT:
983 if (from->vval.v_dict == NULL)
984 to->vval.v_dict = NULL;
985 else
986 {
987 to->vval.v_dict = from->vval.v_dict;
988 ++to->vval.v_dict->dv_refcount;
989 }
990 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +0200991 case VAR_VOID:
992 emsg(_(e_cannot_use_void_value));
993 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200994 case VAR_UNKNOWN:
995 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200996 internal_error_no_abort("copy_tv(UNKNOWN)");
997 break;
998 }
999}
1000
1001/*
1002 * Compare "typ1" and "typ2". Put the result in "typ1".
1003 */
1004 int
1005typval_compare(
1006 typval_T *typ1, // first operand
1007 typval_T *typ2, // second operand
Bram Moolenaar657137c2021-01-09 15:45:23 +01001008 exprtype_T type, // operator
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001009 int ic) // ignore case
1010{
1011 int i;
1012 varnumber_T n1, n2;
1013 char_u *s1, *s2;
1014 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1015 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1016
1017 if (type_is && typ1->v_type != typ2->v_type)
1018 {
1019 // For "is" a different type always means FALSE, for "notis"
1020 // it means TRUE.
1021 n1 = (type == EXPR_ISNOT);
1022 }
1023 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
1024 {
1025 if (type_is)
1026 {
1027 n1 = (typ1->v_type == typ2->v_type
1028 && typ1->vval.v_blob == typ2->vval.v_blob);
1029 if (type == EXPR_ISNOT)
1030 n1 = !n1;
1031 }
1032 else if (typ1->v_type != typ2->v_type
1033 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1034 {
1035 if (typ1->v_type != typ2->v_type)
1036 emsg(_("E977: Can only compare Blob with Blob"));
1037 else
1038 emsg(_(e_invalblob));
1039 clear_tv(typ1);
1040 return FAIL;
1041 }
1042 else
1043 {
1044 // Compare two Blobs for being equal or unequal.
1045 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
1046 if (type == EXPR_NEQUAL)
1047 n1 = !n1;
1048 }
1049 }
1050 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
1051 {
1052 if (type_is)
1053 {
1054 n1 = (typ1->v_type == typ2->v_type
1055 && typ1->vval.v_list == typ2->vval.v_list);
1056 if (type == EXPR_ISNOT)
1057 n1 = !n1;
1058 }
1059 else if (typ1->v_type != typ2->v_type
1060 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1061 {
1062 if (typ1->v_type != typ2->v_type)
1063 emsg(_("E691: Can only compare List with List"));
1064 else
1065 emsg(_("E692: Invalid operation for List"));
1066 clear_tv(typ1);
1067 return FAIL;
1068 }
1069 else
1070 {
1071 // Compare two Lists for being equal or unequal.
1072 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
1073 ic, FALSE);
1074 if (type == EXPR_NEQUAL)
1075 n1 = !n1;
1076 }
1077 }
1078
1079 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
1080 {
1081 if (type_is)
1082 {
1083 n1 = (typ1->v_type == typ2->v_type
1084 && typ1->vval.v_dict == typ2->vval.v_dict);
1085 if (type == EXPR_ISNOT)
1086 n1 = !n1;
1087 }
1088 else if (typ1->v_type != typ2->v_type
1089 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1090 {
1091 if (typ1->v_type != typ2->v_type)
1092 emsg(_("E735: Can only compare Dictionary with Dictionary"));
1093 else
1094 emsg(_("E736: Invalid operation for Dictionary"));
1095 clear_tv(typ1);
1096 return FAIL;
1097 }
1098 else
1099 {
1100 // Compare two Dictionaries for being equal or unequal.
1101 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
1102 ic, FALSE);
1103 if (type == EXPR_NEQUAL)
1104 n1 = !n1;
1105 }
1106 }
1107
1108 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
1109 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
1110 {
1111 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1112 && type != EXPR_IS && type != EXPR_ISNOT)
1113 {
1114 emsg(_("E694: Invalid operation for Funcrefs"));
1115 clear_tv(typ1);
1116 return FAIL;
1117 }
1118 if ((typ1->v_type == VAR_PARTIAL
1119 && typ1->vval.v_partial == NULL)
1120 || (typ2->v_type == VAR_PARTIAL
1121 && typ2->vval.v_partial == NULL))
1122 // When both partials are NULL, then they are equal.
1123 // Otherwise they are not equal.
1124 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
1125 else if (type_is)
1126 {
1127 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
1128 // strings are considered the same if their value is
1129 // the same
1130 n1 = tv_equal(typ1, typ2, ic, FALSE);
1131 else if (typ1->v_type == VAR_PARTIAL
1132 && typ2->v_type == VAR_PARTIAL)
1133 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
1134 else
1135 n1 = FALSE;
1136 }
1137 else
1138 n1 = tv_equal(typ1, typ2, ic, FALSE);
1139 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1140 n1 = !n1;
1141 }
1142
1143#ifdef FEAT_FLOAT
1144 // If one of the two variables is a float, compare as a float.
1145 // When using "=~" or "!~", always compare as string.
1146 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
1147 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1148 {
1149 float_T f1, f2;
1150
1151 f1 = tv_get_float(typ1);
1152 f2 = tv_get_float(typ2);
1153 n1 = FALSE;
1154 switch (type)
1155 {
1156 case EXPR_IS:
1157 case EXPR_EQUAL: n1 = (f1 == f2); break;
1158 case EXPR_ISNOT:
1159 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1160 case EXPR_GREATER: n1 = (f1 > f2); break;
1161 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1162 case EXPR_SMALLER: n1 = (f1 < f2); break;
1163 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1164 case EXPR_UNKNOWN:
1165 case EXPR_MATCH:
1166 default: break; // avoid gcc warning
1167 }
1168 }
1169#endif
1170
1171 // If one of the two variables is a number, compare as a number.
1172 // When using "=~" or "!~", always compare as string.
1173 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
1174 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1175 {
1176 n1 = tv_get_number(typ1);
1177 n2 = tv_get_number(typ2);
1178 switch (type)
1179 {
1180 case EXPR_IS:
1181 case EXPR_EQUAL: n1 = (n1 == n2); break;
1182 case EXPR_ISNOT:
1183 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1184 case EXPR_GREATER: n1 = (n1 > n2); break;
1185 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1186 case EXPR_SMALLER: n1 = (n1 < n2); break;
1187 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1188 case EXPR_UNKNOWN:
1189 case EXPR_MATCH:
1190 default: break; // avoid gcc warning
1191 }
1192 }
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001193 else if (in_vim9script() && (typ1->v_type == VAR_BOOL
Bram Moolenaar0c357522021-07-18 14:43:43 +02001194 || typ2->v_type == VAR_BOOL
1195 || (typ1->v_type == VAR_SPECIAL
1196 && typ2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001197 {
1198 if (typ1->v_type != typ2->v_type)
1199 {
1200 semsg(_(e_cannot_compare_str_with_str),
1201 vartype_name(typ1->v_type), vartype_name(typ2->v_type));
1202 clear_tv(typ1);
1203 return FAIL;
1204 }
1205 n1 = typ1->vval.v_number;
1206 n2 = typ2->vval.v_number;
1207 switch (type)
1208 {
1209 case EXPR_IS:
1210 case EXPR_EQUAL: n1 = (n1 == n2); break;
1211 case EXPR_ISNOT:
1212 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1213 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001214 semsg(_(e_invalid_operation_for_str),
1215 vartype_name(typ1->v_type));
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001216 clear_tv(typ1);
1217 return FAIL;
1218 }
1219 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001220 else
1221 {
Bram Moolenaar0c357522021-07-18 14:43:43 +02001222 if (in_vim9script()
1223 && ((typ1->v_type != VAR_STRING && typ1->v_type != VAR_SPECIAL)
1224 || (typ2->v_type != VAR_STRING && typ2->v_type != VAR_SPECIAL)))
1225 {
1226 semsg(_(e_cannot_compare_str_with_str),
1227 vartype_name(typ1->v_type), vartype_name(typ2->v_type));
1228 clear_tv(typ1);
1229 return FAIL;
1230 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001231 s1 = tv_get_string_buf(typ1, buf1);
1232 s2 = tv_get_string_buf(typ2, buf2);
1233 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1234 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1235 else
1236 i = 0;
1237 n1 = FALSE;
1238 switch (type)
1239 {
1240 case EXPR_IS:
1241 case EXPR_EQUAL: n1 = (i == 0); break;
1242 case EXPR_ISNOT:
1243 case EXPR_NEQUAL: n1 = (i != 0); break;
1244 case EXPR_GREATER: n1 = (i > 0); break;
1245 case EXPR_GEQUAL: n1 = (i >= 0); break;
1246 case EXPR_SMALLER: n1 = (i < 0); break;
1247 case EXPR_SEQUAL: n1 = (i <= 0); break;
1248
1249 case EXPR_MATCH:
1250 case EXPR_NOMATCH:
1251 n1 = pattern_match(s2, s1, ic);
1252 if (type == EXPR_NOMATCH)
1253 n1 = !n1;
1254 break;
1255
1256 default: break; // avoid gcc warning
1257 }
1258 }
1259 clear_tv(typ1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001260 if (in_vim9script())
1261 {
1262 typ1->v_type = VAR_BOOL;
1263 typ1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
1264 }
1265 else
1266 {
1267 typ1->v_type = VAR_NUMBER;
1268 typ1->vval.v_number = n1;
1269 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001270
1271 return OK;
1272}
1273
Bram Moolenaar34453202021-01-31 13:08:38 +01001274/*
1275 * Convert any type to a string, never give an error.
1276 * When "quotes" is TRUE add quotes to a string.
1277 * Returns an allocated string.
1278 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001279 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001280typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001281{
1282 char_u *tofree;
1283 char_u numbuf[NUMBUFLEN];
1284 char_u *ret = NULL;
1285
1286 if (arg == NULL)
1287 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001288 if (!quotes && arg->v_type == VAR_STRING)
1289 {
1290 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1291 : arg->vval.v_string);
1292 }
1293 else
1294 {
1295 ret = tv2string(arg, &tofree, numbuf, 0);
1296 // Make a copy if we have a value but it's not in allocated memory.
1297 if (ret != NULL && tofree == NULL)
1298 ret = vim_strsave(ret);
1299 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001300 return ret;
1301}
1302
1303/*
1304 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1305 * or it refers to a List or Dictionary that is locked.
1306 */
1307 int
1308tv_islocked(typval_T *tv)
1309{
1310 return (tv->v_lock & VAR_LOCKED)
1311 || (tv->v_type == VAR_LIST
1312 && tv->vval.v_list != NULL
1313 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1314 || (tv->v_type == VAR_DICT
1315 && tv->vval.v_dict != NULL
1316 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1317}
1318
1319 static int
1320func_equal(
1321 typval_T *tv1,
1322 typval_T *tv2,
1323 int ic) // ignore case
1324{
1325 char_u *s1, *s2;
1326 dict_T *d1, *d2;
1327 int a1, a2;
1328 int i;
1329
1330 // empty and NULL function name considered the same
1331 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1332 : partial_name(tv1->vval.v_partial);
1333 if (s1 != NULL && *s1 == NUL)
1334 s1 = NULL;
1335 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1336 : partial_name(tv2->vval.v_partial);
1337 if (s2 != NULL && *s2 == NUL)
1338 s2 = NULL;
1339 if (s1 == NULL || s2 == NULL)
1340 {
1341 if (s1 != s2)
1342 return FALSE;
1343 }
1344 else if (STRCMP(s1, s2) != 0)
1345 return FALSE;
1346
1347 // empty dict and NULL dict is different
1348 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1349 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1350 if (d1 == NULL || d2 == NULL)
1351 {
1352 if (d1 != d2)
1353 return FALSE;
1354 }
1355 else if (!dict_equal(d1, d2, ic, TRUE))
1356 return FALSE;
1357
1358 // empty list and no list considered the same
1359 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1360 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1361 if (a1 != a2)
1362 return FALSE;
1363 for (i = 0; i < a1; ++i)
1364 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1365 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1366 return FALSE;
1367
1368 return TRUE;
1369}
1370
1371/*
1372 * Return TRUE if "tv1" and "tv2" have the same value.
1373 * Compares the items just like "==" would compare them, but strings and
1374 * numbers are different. Floats and numbers are also different.
1375 */
1376 int
1377tv_equal(
1378 typval_T *tv1,
1379 typval_T *tv2,
1380 int ic, // ignore case
1381 int recursive) // TRUE when used recursively
1382{
1383 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1384 char_u *s1, *s2;
1385 static int recursive_cnt = 0; // catch recursive loops
1386 int r;
1387 static int tv_equal_recurse_limit;
1388
1389 // Catch lists and dicts that have an endless loop by limiting
1390 // recursiveness to a limit. We guess they are equal then.
1391 // A fixed limit has the problem of still taking an awful long time.
1392 // Reduce the limit every time running into it. That should work fine for
1393 // deeply linked structures that are not recursively linked and catch
1394 // recursiveness quickly.
1395 if (!recursive)
1396 tv_equal_recurse_limit = 1000;
1397 if (recursive_cnt >= tv_equal_recurse_limit)
1398 {
1399 --tv_equal_recurse_limit;
1400 return TRUE;
1401 }
1402
1403 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1404 // arguments.
1405 if ((tv1->v_type == VAR_FUNC
1406 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1407 && (tv2->v_type == VAR_FUNC
1408 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1409 {
1410 ++recursive_cnt;
1411 r = func_equal(tv1, tv2, ic);
1412 --recursive_cnt;
1413 return r;
1414 }
1415
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001416 if (tv1->v_type != tv2->v_type
1417 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1418 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001419 return FALSE;
1420
1421 switch (tv1->v_type)
1422 {
1423 case VAR_LIST:
1424 ++recursive_cnt;
1425 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1426 --recursive_cnt;
1427 return r;
1428
1429 case VAR_DICT:
1430 ++recursive_cnt;
1431 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1432 --recursive_cnt;
1433 return r;
1434
1435 case VAR_BLOB:
1436 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1437
1438 case VAR_NUMBER:
1439 case VAR_BOOL:
1440 case VAR_SPECIAL:
1441 return tv1->vval.v_number == tv2->vval.v_number;
1442
1443 case VAR_STRING:
1444 s1 = tv_get_string_buf(tv1, buf1);
1445 s2 = tv_get_string_buf(tv2, buf2);
1446 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1447
1448 case VAR_FLOAT:
1449#ifdef FEAT_FLOAT
1450 return tv1->vval.v_float == tv2->vval.v_float;
1451#endif
1452 case VAR_JOB:
1453#ifdef FEAT_JOB_CHANNEL
1454 return tv1->vval.v_job == tv2->vval.v_job;
1455#endif
1456 case VAR_CHANNEL:
1457#ifdef FEAT_JOB_CHANNEL
1458 return tv1->vval.v_channel == tv2->vval.v_channel;
1459#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001460 case VAR_INSTR:
1461 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001462
1463 case VAR_PARTIAL:
1464 return tv1->vval.v_partial == tv2->vval.v_partial;
1465
1466 case VAR_FUNC:
1467 return tv1->vval.v_string == tv2->vval.v_string;
1468
1469 case VAR_UNKNOWN:
1470 case VAR_ANY:
1471 case VAR_VOID:
1472 break;
1473 }
1474
1475 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1476 // does not equal anything, not even itself.
1477 return FALSE;
1478}
1479
1480/*
1481 * Get an option value.
1482 * "arg" points to the '&' or '+' before the option name.
1483 * "arg" is advanced to character after the option name.
1484 * Return OK or FAIL.
1485 */
1486 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001487eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001488 char_u **arg,
1489 typval_T *rettv, // when NULL, only check if option exists
1490 int evaluate)
1491{
1492 char_u *option_end;
1493 long numval;
1494 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001495 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001496 int c;
1497 int working = (**arg == '+'); // has("+option")
1498 int ret = OK;
1499 int opt_flags;
1500
1501 // Isolate the option name and find its value.
1502 option_end = find_option_end(arg, &opt_flags);
1503 if (option_end == NULL)
1504 {
1505 if (rettv != NULL)
1506 semsg(_("E112: Option name missing: %s"), *arg);
1507 return FAIL;
1508 }
1509
1510 if (!evaluate)
1511 {
1512 *arg = option_end;
1513 return OK;
1514 }
1515
1516 c = *option_end;
1517 *option_end = NUL;
1518 opt_type = get_option_value(*arg, &numval,
1519 rettv == NULL ? NULL : &stringval, opt_flags);
1520
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001521 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001522 {
1523 if (rettv != NULL)
1524 semsg(_(e_unknown_option), *arg);
1525 ret = FAIL;
1526 }
1527 else if (rettv != NULL)
1528 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001529 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001530 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001531 {
1532 rettv->v_type = VAR_STRING;
1533 rettv->vval.v_string = NULL;
1534 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001535 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001536 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001537 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1538 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001539 rettv->vval.v_number = 0;
1540 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001541 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001542 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001543 if (in_vim9script() && opt_type == gov_bool)
1544 {
1545 rettv->v_type = VAR_BOOL;
1546 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1547 }
1548 else
1549 {
1550 rettv->v_type = VAR_NUMBER;
1551 rettv->vval.v_number = numval;
1552 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001553 }
1554 else // string option
1555 {
1556 rettv->v_type = VAR_STRING;
1557 rettv->vval.v_string = stringval;
1558 }
1559 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001560 else if (working && (opt_type == gov_hidden_bool
1561 || opt_type == gov_hidden_number
1562 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001563 ret = FAIL;
1564
1565 *option_end = c; // put back for error messages
1566 *arg = option_end;
1567
1568 return ret;
1569}
1570
1571/*
1572 * Allocate a variable for a number constant. Also deals with "0z" for blob.
1573 * Return OK or FAIL.
1574 */
1575 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001576eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001577 char_u **arg,
1578 typval_T *rettv,
1579 int evaluate,
1580 int want_string UNUSED)
1581{
1582 int len;
1583#ifdef FEAT_FLOAT
1584 char_u *p;
1585 int get_float = FALSE;
1586
1587 // We accept a float when the format matches
1588 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
1589 // strict to avoid backwards compatibility problems.
1590 // With script version 2 and later the leading digit can be
1591 // omitted.
1592 // Don't look for a float after the "." operator, so that
1593 // ":let vers = 1.2.3" doesn't fail.
1594 if (**arg == '.')
1595 p = *arg;
1596 else
1597 p = skipdigits(*arg + 1);
1598 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
1599 {
1600 get_float = TRUE;
1601 p = skipdigits(p + 2);
1602 if (*p == 'e' || *p == 'E')
1603 {
1604 ++p;
1605 if (*p == '-' || *p == '+')
1606 ++p;
1607 if (!vim_isdigit(*p))
1608 get_float = FALSE;
1609 else
1610 p = skipdigits(p + 1);
1611 }
1612 if (ASCII_ISALPHA(*p) || *p == '.')
1613 get_float = FALSE;
1614 }
1615 if (get_float)
1616 {
1617 float_T f;
1618
1619 *arg += string2float(*arg, &f);
1620 if (evaluate)
1621 {
1622 rettv->v_type = VAR_FLOAT;
1623 rettv->vval.v_float = f;
1624 }
1625 }
1626 else
1627#endif
1628 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
1629 {
1630 char_u *bp;
1631 blob_T *blob = NULL; // init for gcc
1632
1633 // Blob constant: 0z0123456789abcdef
1634 if (evaluate)
1635 blob = blob_alloc();
1636 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
1637 {
1638 if (!vim_isxdigit(bp[1]))
1639 {
1640 if (blob != NULL)
1641 {
1642 emsg(_("E973: Blob literal should have an even number of hex characters"));
1643 ga_clear(&blob->bv_ga);
1644 VIM_CLEAR(blob);
1645 }
1646 return FAIL;
1647 }
1648 if (blob != NULL)
1649 ga_append(&blob->bv_ga,
1650 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
1651 if (bp[2] == '.' && vim_isxdigit(bp[3]))
1652 ++bp;
1653 }
1654 if (blob != NULL)
1655 rettv_blob_set(rettv, blob);
1656 *arg = bp;
1657 }
1658 else
1659 {
1660 varnumber_T n;
1661
1662 // decimal, hex or octal number
1663 vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4
1664 ? STR2NR_NO_OCT + STR2NR_QUOTE
1665 : STR2NR_ALL, &n, NULL, 0, TRUE);
1666 if (len == 0)
1667 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001668 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001669 return FAIL;
1670 }
1671 *arg += len;
1672 if (evaluate)
1673 {
1674 rettv->v_type = VAR_NUMBER;
1675 rettv->vval.v_number = n;
1676 }
1677 }
1678 return OK;
1679}
1680
1681/*
1682 * Allocate a variable for a string constant.
1683 * Return OK or FAIL.
1684 */
1685 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001686eval_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001687{
1688 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001689 char_u *end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001690 int extra = 0;
1691 int len;
1692
1693 // Find the end of the string, skipping backslashed characters.
1694 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
1695 {
1696 if (*p == '\\' && p[1] != NUL)
1697 {
1698 ++p;
1699 // A "\<x>" form occupies at least 4 characters, and produces up
1700 // to 21 characters (3 * 6 for the char and 3 for a modifier):
1701 // reserve space for 18 extra.
1702 // Each byte in the char could be encoded as K_SPECIAL K_EXTRA x.
1703 if (*p == '<')
1704 extra += 18;
1705 }
1706 }
1707
1708 if (*p != '"')
1709 {
1710 semsg(_("E114: Missing quote: %s"), *arg);
1711 return FAIL;
1712 }
1713
1714 // If only parsing, set *arg and return here
1715 if (!evaluate)
1716 {
1717 *arg = p + 1;
1718 return OK;
1719 }
1720
1721 // Copy the string into allocated memory, handling backslashed
1722 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001723 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001724 len = (int)(p - *arg + extra);
1725 rettv->vval.v_string = alloc(len);
1726 if (rettv->vval.v_string == NULL)
1727 return FAIL;
1728 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001729
1730 for (p = *arg + 1; *p != NUL && *p != '"'; )
1731 {
1732 if (*p == '\\')
1733 {
1734 switch (*++p)
1735 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001736 case 'b': *end++ = BS; ++p; break;
1737 case 'e': *end++ = ESC; ++p; break;
1738 case 'f': *end++ = FF; ++p; break;
1739 case 'n': *end++ = NL; ++p; break;
1740 case 'r': *end++ = CAR; ++p; break;
1741 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001742
1743 case 'X': // hex: "\x1", "\x12"
1744 case 'x':
1745 case 'u': // Unicode: "\u0023"
1746 case 'U':
1747 if (vim_isxdigit(p[1]))
1748 {
1749 int n, nr;
1750 int c = toupper(*p);
1751
1752 if (c == 'X')
1753 n = 2;
1754 else if (*p == 'u')
1755 n = 4;
1756 else
1757 n = 8;
1758 nr = 0;
1759 while (--n >= 0 && vim_isxdigit(p[1]))
1760 {
1761 ++p;
1762 nr = (nr << 4) + hex2nr(*p);
1763 }
1764 ++p;
1765 // For "\u" store the number according to
1766 // 'encoding'.
1767 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001768 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001769 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001770 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001771 }
1772 break;
1773
1774 // octal: "\1", "\12", "\123"
1775 case '0':
1776 case '1':
1777 case '2':
1778 case '3':
1779 case '4':
1780 case '5':
1781 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001782 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001783 if (*p >= '0' && *p <= '7')
1784 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001785 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001786 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001787 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001788 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001789 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001790 break;
1791
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001792 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001793 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001794 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001795 int flags = FSK_KEYCODE | FSK_IN_STRING;
1796
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001797 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001798 flags |= FSK_SIMPLIFY;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001799 extra = trans_special(&p, end, flags, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001800 if (extra != 0)
1801 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001802 end += extra;
1803 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001804 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001805 break;
1806 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001807 }
1808 // FALLTHROUGH
1809
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001810 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001811 break;
1812 }
1813 }
1814 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001815 MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001816 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001817 *end = NUL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001818 if (*p != NUL) // just in case
1819 ++p;
1820 *arg = p;
1821
1822 return OK;
1823}
1824
1825/*
1826 * Allocate a variable for a 'str''ing' constant.
1827 * Return OK or FAIL.
1828 */
1829 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001830eval_lit_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001831{
1832 char_u *p;
1833 char_u *str;
1834 int reduce = 0;
1835
1836 // Find the end of the string, skipping ''.
1837 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
1838 {
1839 if (*p == '\'')
1840 {
1841 if (p[1] != '\'')
1842 break;
1843 ++reduce;
1844 ++p;
1845 }
1846 }
1847
1848 if (*p != '\'')
1849 {
1850 semsg(_("E115: Missing quote: %s"), *arg);
1851 return FAIL;
1852 }
1853
1854 // If only parsing return after setting "*arg"
1855 if (!evaluate)
1856 {
1857 *arg = p + 1;
1858 return OK;
1859 }
1860
1861 // Copy the string into allocated memory, handling '' to ' reduction.
1862 str = alloc((p - *arg) - reduce);
1863 if (str == NULL)
1864 return FAIL;
1865 rettv->v_type = VAR_STRING;
1866 rettv->vval.v_string = str;
1867
1868 for (p = *arg + 1; *p != NUL; )
1869 {
1870 if (*p == '\'')
1871 {
1872 if (p[1] != '\'')
1873 break;
1874 ++p;
1875 }
1876 MB_COPY_CHAR(p, str);
1877 }
1878 *str = NUL;
1879 *arg = p + 1;
1880
1881 return OK;
1882}
1883
1884/*
1885 * Return a string with the string representation of a variable.
1886 * If the memory is allocated "tofree" is set to it, otherwise NULL.
1887 * "numbuf" is used for a number.
1888 * Puts quotes around strings, so that they can be parsed back by eval().
1889 * May return NULL.
1890 */
1891 char_u *
1892tv2string(
1893 typval_T *tv,
1894 char_u **tofree,
1895 char_u *numbuf,
1896 int copyID)
1897{
1898 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
1899}
1900
1901/*
1902 * Get the value of an environment variable.
1903 * "arg" is pointing to the '$'. It is advanced to after the name.
1904 * If the environment variable was not set, silently assume it is empty.
1905 * Return FAIL if the name is invalid.
1906 */
1907 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001908eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001909{
1910 char_u *string = NULL;
1911 int len;
1912 int cc;
1913 char_u *name;
1914 int mustfree = FALSE;
1915
1916 ++*arg;
1917 name = *arg;
1918 len = get_env_len(arg);
1919 if (evaluate)
1920 {
1921 if (len == 0)
1922 return FAIL; // invalid empty name
1923
1924 cc = name[len];
1925 name[len] = NUL;
1926 // first try vim_getenv(), fast for normal environment vars
1927 string = vim_getenv(name, &mustfree);
1928 if (string != NULL && *string != NUL)
1929 {
1930 if (!mustfree)
1931 string = vim_strsave(string);
1932 }
1933 else
1934 {
1935 if (mustfree)
1936 vim_free(string);
1937
1938 // next try expanding things like $VIM and ${HOME}
1939 string = expand_env_save(name - 1);
1940 if (string != NULL && *string == '$')
1941 VIM_CLEAR(string);
1942 }
1943 name[len] = cc;
1944
1945 rettv->v_type = VAR_STRING;
1946 rettv->vval.v_string = string;
1947 }
1948
1949 return OK;
1950}
1951
1952/*
1953 * Get the lnum from the first argument.
1954 * Also accepts ".", "$", etc., but that only works for the current buffer.
1955 * Returns -1 on error.
1956 */
1957 linenr_T
1958tv_get_lnum(typval_T *argvars)
1959{
Bram Moolenaar9a963372020-12-21 21:58:46 +01001960 linenr_T lnum = -1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001961
Bram Moolenaar56acb092020-08-16 14:48:19 +02001962 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
1963 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02001964 if (lnum <= 0 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001965 {
1966 int fnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001967 pos_T *fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001968
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02001969 // no valid number, try using arg like line()
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001970 if (fp != NULL)
1971 lnum = fp->lnum;
1972 }
1973 return lnum;
1974}
1975
1976/*
1977 * Get the lnum from the first argument.
1978 * Also accepts "$", then "buf" is used.
1979 * Returns 0 on error.
1980 */
1981 linenr_T
1982tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1983{
1984 if (argvars[0].v_type == VAR_STRING
1985 && argvars[0].vval.v_string != NULL
1986 && argvars[0].vval.v_string[0] == '$'
1987 && buf != NULL)
1988 return buf->b_ml.ml_line_count;
1989 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1990}
1991
1992/*
1993 * Get buffer by number or pattern.
1994 */
1995 buf_T *
1996tv_get_buf(typval_T *tv, int curtab_only)
1997{
1998 char_u *name = tv->vval.v_string;
1999 buf_T *buf;
2000
2001 if (tv->v_type == VAR_NUMBER)
2002 return buflist_findnr((int)tv->vval.v_number);
2003 if (tv->v_type != VAR_STRING)
2004 return NULL;
2005 if (name == NULL || *name == NUL)
2006 return curbuf;
2007 if (name[0] == '$' && name[1] == NUL)
2008 return lastbuf;
2009
2010 buf = buflist_find_by_name(name, curtab_only);
2011
2012 // If not found, try expanding the name, like done for bufexists().
2013 if (buf == NULL)
2014 buf = find_buffer(tv);
2015
2016 return buf;
2017}
2018
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002019/*
2020 * Like tv_get_buf() but give an error message is the type is wrong.
2021 */
2022 buf_T *
2023tv_get_buf_from_arg(typval_T *tv)
2024{
2025 buf_T *buf;
2026
2027 ++emsg_off;
2028 buf = tv_get_buf(tv, FALSE);
2029 --emsg_off;
2030 if (buf == NULL
2031 && tv->v_type != VAR_NUMBER
2032 && tv->v_type != VAR_STRING)
2033 // issue errmsg for type error
2034 (void)tv_get_number(tv);
2035 return buf;
2036}
2037
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002038#endif // FEAT_EVAL