blob: b73c5002bb3b69e00ed690b2f270ce0a643036e6 [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/*
509 * Give an error and return FAIL unless "args[idx]" is a blob.
510 */
511 int
512check_for_blob_arg(typval_T *args, int idx)
513{
514 if (args[idx].v_type != VAR_BLOB)
515 {
516 if (idx >= 0)
517 semsg(_(e_blob_required_for_argument_nr), idx + 1);
518 else
519 emsg(_(e_blobreq));
520 return FAIL;
521 }
522 return OK;
523}
524
525/*
526 * Give an error and return FAIL unless "args[idx]" is a channel or a job.
527 */
528 int
529check_for_chan_or_job_arg(typval_T *args, int idx)
530{
531 if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
532 {
533 if (idx >= 0)
534 semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
535 else
536 emsg(_(e_chan_or_job_req));
537 return FAIL;
538 }
539 return OK;
540}
541
542/*
543 * Give an error and return FAIL unless "args[idx]" is a job.
544 */
545 int
546check_for_job_arg(typval_T *args, int idx)
547{
548 if (args[idx].v_type != VAR_JOB)
549 {
550 if (idx >= 0)
551 semsg(_(e_job_required_for_argument_nr), idx + 1);
552 else
553 emsg(_(e_jobreq));
554 return FAIL;
555 }
556 return OK;
557}
558
559/*
560 * Give an error and return FAIL unless "args[idx]" is a string or
561 * a number.
562 */
563 int
564check_for_string_or_number_arg(typval_T *args, int idx)
565{
566 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
567 {
568 if (idx >= 0)
569 semsg(_(e_string_required_for_argument_nr), idx + 1);
570 else
571 emsg(_(e_stringreq));
572 return FAIL;
573 }
574 return OK;
575}
576
577/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200578 * Check for an optional string or number argument at 'idx'.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200579 */
580 int
581check_for_opt_string_or_number_arg(typval_T *args, int idx)
582{
583 return (args[idx].v_type == VAR_UNKNOWN
584 || check_for_string_or_number_arg(args, idx) != FAIL);
585}
586
587/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200588 * Give an error and return FAIL unless "args[idx]" is a buffer number.
589 * Buffer number can be a number or a string.
590 */
591 int
592check_for_buffer_arg(typval_T *args, int idx)
593{
594 return check_for_string_or_number_arg(args, idx);
595}
596
597/*
598 * Check for an optional buffer argument at 'idx'
599 */
600 int
601check_for_opt_buffer_arg(typval_T *args, int idx)
602{
603 return (args[idx].v_type == VAR_UNKNOWN
604 || check_for_buffer_arg(args, idx));
605}
606
607/*
608 * Give an error and return FAIL unless "args[idx]" is a line number.
609 * Line number can be a number or a string.
610 */
611 int
612check_for_lnum_arg(typval_T *args, int idx)
613{
614 return check_for_string_or_number_arg(args, idx);
615}
616
617/*
618 * Check for an optional line number argument at 'idx'
619 */
620 int
621check_for_opt_lnum_arg(typval_T *args, int idx)
622{
623 return (args[idx].v_type == VAR_UNKNOWN
624 || check_for_lnum_arg(args, idx));
625}
626
627/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200628 * Give an error and return FAIL unless "args[idx]" is a string or
629 * a blob.
630 */
631 int
632check_for_string_or_blob_arg(typval_T *args, int idx)
633{
634 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
635 {
636 if (idx >= 0)
637 semsg(_(e_string_required_for_argument_nr), idx + 1);
638 else
639 emsg(_(e_stringreq));
640 return FAIL;
641 }
642 return OK;
643}
644
645/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200646 * Give an error and return FAIL unless "args[idx]" is a string or
647 * a list.
648 */
649 int
650check_for_string_or_list_arg(typval_T *args, int idx)
651{
652 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
653 {
654 if (idx >= 0)
655 semsg(_(e_string_required_for_argument_nr), idx + 1);
656 else
657 emsg(_(e_stringreq));
658 return FAIL;
659 }
660 return OK;
661}
662
663/*
664 * Give an error and return FAIL unless "args[idx]" is a buffer
665 * number or a dict.
666 */
667 int
668check_for_buffer_or_dict_arg(typval_T *args, int idx)
669{
670 if (args[idx].v_type != VAR_STRING
671 && args[idx].v_type != VAR_NUMBER
672 && args[idx].v_type != VAR_DICT)
673 {
674 if (idx >= 0)
675 semsg(_(e_string_required_for_argument_nr), idx + 1);
676 else
677 emsg(_(e_stringreq));
678 return FAIL;
679 }
680 return OK;
681}
682
683/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200684 * Get the string value of a variable.
685 * If it is a Number variable, the number is converted into a string.
686 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
687 * tv_get_string_buf() uses a given buffer.
688 * If the String variable has never been set, return an empty string.
689 * Never returns NULL;
690 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
691 * NULL on error.
692 */
693 char_u *
694tv_get_string(typval_T *varp)
695{
696 static char_u mybuf[NUMBUFLEN];
697
698 return tv_get_string_buf(varp, mybuf);
699}
700
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100701/*
702 * Like tv_get_string() but don't allow number to string conversion for Vim9.
703 */
704 char_u *
705tv_get_string_strict(typval_T *varp)
706{
707 static char_u mybuf[NUMBUFLEN];
708 char_u *res = tv_get_string_buf_chk_strict(
709 varp, mybuf, in_vim9script());
710
711 return res != NULL ? res : (char_u *)"";
712}
713
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200714 char_u *
715tv_get_string_buf(typval_T *varp, char_u *buf)
716{
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200717 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200718
719 return res != NULL ? res : (char_u *)"";
720}
721
722/*
723 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
724 */
725 char_u *
726tv_get_string_chk(typval_T *varp)
727{
728 static char_u mybuf[NUMBUFLEN];
729
730 return tv_get_string_buf_chk(varp, mybuf);
731}
732
733 char_u *
734tv_get_string_buf_chk(typval_T *varp, char_u *buf)
735{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100736 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
737}
738
739 char_u *
740tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
741{
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200742 switch (varp->v_type)
743 {
744 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100745 if (strict)
746 {
747 emsg(_(e_using_number_as_string));
748 break;
749 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200750 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
751 (varnumber_T)varp->vval.v_number);
752 return buf;
753 case VAR_FUNC:
754 case VAR_PARTIAL:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200755 emsg(_("E729: Using a Funcref as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200756 break;
757 case VAR_LIST:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200758 emsg(_("E730: Using a List as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200759 break;
760 case VAR_DICT:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200761 emsg(_("E731: Using a Dictionary as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200762 break;
763 case VAR_FLOAT:
764#ifdef FEAT_FLOAT
Bram Moolenaar7a2217b2021-06-06 12:33:49 +0200765 if (strict)
766 {
767 emsg(_(e_float_as_string));
768 break;
769 }
770 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
771 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200772#endif
773 case VAR_STRING:
774 if (varp->vval.v_string != NULL)
775 return varp->vval.v_string;
776 return (char_u *)"";
777 case VAR_BOOL:
778 case VAR_SPECIAL:
779 STRCPY(buf, get_var_special_name(varp->vval.v_number));
780 return buf;
781 case VAR_BLOB:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200782 emsg(_("E976: Using a Blob as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200783 break;
784 case VAR_JOB:
785#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200786 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200787 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200788 semsg(_(e_using_invalid_value_as_string_str), "job");
789 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200790 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200791 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200792#endif
793 break;
794 case VAR_CHANNEL:
795#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200796 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200797 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200798 semsg(_(e_using_invalid_value_as_string_str), "channel");
799 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200800 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200801 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200802#endif
803 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200804 case VAR_VOID:
805 emsg(_(e_cannot_use_void_value));
806 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200807 case VAR_UNKNOWN:
808 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200809 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +0200810 semsg(_(e_using_invalid_value_as_string_str),
811 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200812 break;
813 }
814 return NULL;
815}
816
817/*
818 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
819 * string() on Dict, List, etc.
820 */
821 char_u *
822tv_stringify(typval_T *varp, char_u *buf)
823{
824 if (varp->v_type == VAR_LIST
825 || varp->v_type == VAR_DICT
826 || varp->v_type == VAR_BLOB
827 || varp->v_type == VAR_FUNC
828 || varp->v_type == VAR_PARTIAL
829 || varp->v_type == VAR_FLOAT)
830 {
831 typval_T tmp;
832
833 f_string(varp, &tmp);
834 tv_get_string_buf(&tmp, buf);
835 clear_tv(varp);
836 *varp = tmp;
837 return tmp.vval.v_string;
838 }
839 return tv_get_string_buf(varp, buf);
840}
841
842/*
843 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
844 * Also give an error message, using "name" or _("name") when use_gettext is
845 * TRUE.
846 */
847 int
848tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
849{
850 int lock = 0;
851
852 switch (tv->v_type)
853 {
854 case VAR_BLOB:
855 if (tv->vval.v_blob != NULL)
856 lock = tv->vval.v_blob->bv_lock;
857 break;
858 case VAR_LIST:
859 if (tv->vval.v_list != NULL)
860 lock = tv->vval.v_list->lv_lock;
861 break;
862 case VAR_DICT:
863 if (tv->vval.v_dict != NULL)
864 lock = tv->vval.v_dict->dv_lock;
865 break;
866 default:
867 break;
868 }
Bram Moolenaara187c432020-09-16 21:08:28 +0200869 return value_check_lock(tv->v_lock, name, use_gettext)
870 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200871}
872
873/*
874 * Copy the values from typval_T "from" to typval_T "to".
875 * When needed allocates string or increases reference count.
876 * Does not make a copy of a list, blob or dict but copies the reference!
877 * It is OK for "from" and "to" to point to the same item. This is used to
878 * make a copy later.
879 */
880 void
881copy_tv(typval_T *from, typval_T *to)
882{
883 to->v_type = from->v_type;
884 to->v_lock = 0;
885 switch (from->v_type)
886 {
887 case VAR_NUMBER:
888 case VAR_BOOL:
889 case VAR_SPECIAL:
890 to->vval.v_number = from->vval.v_number;
891 break;
892 case VAR_FLOAT:
893#ifdef FEAT_FLOAT
894 to->vval.v_float = from->vval.v_float;
895 break;
896#endif
897 case VAR_JOB:
898#ifdef FEAT_JOB_CHANNEL
899 to->vval.v_job = from->vval.v_job;
900 if (to->vval.v_job != NULL)
901 ++to->vval.v_job->jv_refcount;
902 break;
903#endif
904 case VAR_CHANNEL:
905#ifdef FEAT_JOB_CHANNEL
906 to->vval.v_channel = from->vval.v_channel;
907 if (to->vval.v_channel != NULL)
908 ++to->vval.v_channel->ch_refcount;
909 break;
910#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200911 case VAR_INSTR:
912 to->vval.v_instr = from->vval.v_instr;
913 break;
914
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200915 case VAR_STRING:
916 case VAR_FUNC:
917 if (from->vval.v_string == NULL)
918 to->vval.v_string = NULL;
919 else
920 {
921 to->vval.v_string = vim_strsave(from->vval.v_string);
922 if (from->v_type == VAR_FUNC)
923 func_ref(to->vval.v_string);
924 }
925 break;
926 case VAR_PARTIAL:
927 if (from->vval.v_partial == NULL)
928 to->vval.v_partial = NULL;
929 else
930 {
931 to->vval.v_partial = from->vval.v_partial;
932 ++to->vval.v_partial->pt_refcount;
933 }
934 break;
935 case VAR_BLOB:
936 if (from->vval.v_blob == NULL)
937 to->vval.v_blob = NULL;
938 else
939 {
940 to->vval.v_blob = from->vval.v_blob;
941 ++to->vval.v_blob->bv_refcount;
942 }
943 break;
944 case VAR_LIST:
945 if (from->vval.v_list == NULL)
946 to->vval.v_list = NULL;
947 else
948 {
949 to->vval.v_list = from->vval.v_list;
950 ++to->vval.v_list->lv_refcount;
951 }
952 break;
953 case VAR_DICT:
954 if (from->vval.v_dict == NULL)
955 to->vval.v_dict = NULL;
956 else
957 {
958 to->vval.v_dict = from->vval.v_dict;
959 ++to->vval.v_dict->dv_refcount;
960 }
961 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +0200962 case VAR_VOID:
963 emsg(_(e_cannot_use_void_value));
964 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200965 case VAR_UNKNOWN:
966 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200967 internal_error_no_abort("copy_tv(UNKNOWN)");
968 break;
969 }
970}
971
972/*
973 * Compare "typ1" and "typ2". Put the result in "typ1".
974 */
975 int
976typval_compare(
977 typval_T *typ1, // first operand
978 typval_T *typ2, // second operand
Bram Moolenaar657137c2021-01-09 15:45:23 +0100979 exprtype_T type, // operator
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200980 int ic) // ignore case
981{
982 int i;
983 varnumber_T n1, n2;
984 char_u *s1, *s2;
985 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
986 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
987
988 if (type_is && typ1->v_type != typ2->v_type)
989 {
990 // For "is" a different type always means FALSE, for "notis"
991 // it means TRUE.
992 n1 = (type == EXPR_ISNOT);
993 }
994 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
995 {
996 if (type_is)
997 {
998 n1 = (typ1->v_type == typ2->v_type
999 && typ1->vval.v_blob == typ2->vval.v_blob);
1000 if (type == EXPR_ISNOT)
1001 n1 = !n1;
1002 }
1003 else if (typ1->v_type != typ2->v_type
1004 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1005 {
1006 if (typ1->v_type != typ2->v_type)
1007 emsg(_("E977: Can only compare Blob with Blob"));
1008 else
1009 emsg(_(e_invalblob));
1010 clear_tv(typ1);
1011 return FAIL;
1012 }
1013 else
1014 {
1015 // Compare two Blobs for being equal or unequal.
1016 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
1017 if (type == EXPR_NEQUAL)
1018 n1 = !n1;
1019 }
1020 }
1021 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
1022 {
1023 if (type_is)
1024 {
1025 n1 = (typ1->v_type == typ2->v_type
1026 && typ1->vval.v_list == typ2->vval.v_list);
1027 if (type == EXPR_ISNOT)
1028 n1 = !n1;
1029 }
1030 else if (typ1->v_type != typ2->v_type
1031 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1032 {
1033 if (typ1->v_type != typ2->v_type)
1034 emsg(_("E691: Can only compare List with List"));
1035 else
1036 emsg(_("E692: Invalid operation for List"));
1037 clear_tv(typ1);
1038 return FAIL;
1039 }
1040 else
1041 {
1042 // Compare two Lists for being equal or unequal.
1043 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
1044 ic, FALSE);
1045 if (type == EXPR_NEQUAL)
1046 n1 = !n1;
1047 }
1048 }
1049
1050 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
1051 {
1052 if (type_is)
1053 {
1054 n1 = (typ1->v_type == typ2->v_type
1055 && typ1->vval.v_dict == typ2->vval.v_dict);
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(_("E735: Can only compare Dictionary with Dictionary"));
1064 else
1065 emsg(_("E736: Invalid operation for Dictionary"));
1066 clear_tv(typ1);
1067 return FAIL;
1068 }
1069 else
1070 {
1071 // Compare two Dictionaries for being equal or unequal.
1072 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
1073 ic, FALSE);
1074 if (type == EXPR_NEQUAL)
1075 n1 = !n1;
1076 }
1077 }
1078
1079 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
1080 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
1081 {
1082 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1083 && type != EXPR_IS && type != EXPR_ISNOT)
1084 {
1085 emsg(_("E694: Invalid operation for Funcrefs"));
1086 clear_tv(typ1);
1087 return FAIL;
1088 }
1089 if ((typ1->v_type == VAR_PARTIAL
1090 && typ1->vval.v_partial == NULL)
1091 || (typ2->v_type == VAR_PARTIAL
1092 && typ2->vval.v_partial == NULL))
1093 // When both partials are NULL, then they are equal.
1094 // Otherwise they are not equal.
1095 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
1096 else if (type_is)
1097 {
1098 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
1099 // strings are considered the same if their value is
1100 // the same
1101 n1 = tv_equal(typ1, typ2, ic, FALSE);
1102 else if (typ1->v_type == VAR_PARTIAL
1103 && typ2->v_type == VAR_PARTIAL)
1104 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
1105 else
1106 n1 = FALSE;
1107 }
1108 else
1109 n1 = tv_equal(typ1, typ2, ic, FALSE);
1110 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1111 n1 = !n1;
1112 }
1113
1114#ifdef FEAT_FLOAT
1115 // If one of the two variables is a float, compare as a float.
1116 // When using "=~" or "!~", always compare as string.
1117 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
1118 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1119 {
1120 float_T f1, f2;
1121
1122 f1 = tv_get_float(typ1);
1123 f2 = tv_get_float(typ2);
1124 n1 = FALSE;
1125 switch (type)
1126 {
1127 case EXPR_IS:
1128 case EXPR_EQUAL: n1 = (f1 == f2); break;
1129 case EXPR_ISNOT:
1130 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1131 case EXPR_GREATER: n1 = (f1 > f2); break;
1132 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1133 case EXPR_SMALLER: n1 = (f1 < f2); break;
1134 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1135 case EXPR_UNKNOWN:
1136 case EXPR_MATCH:
1137 default: break; // avoid gcc warning
1138 }
1139 }
1140#endif
1141
1142 // If one of the two variables is a number, compare as a number.
1143 // When using "=~" or "!~", always compare as string.
1144 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
1145 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1146 {
1147 n1 = tv_get_number(typ1);
1148 n2 = tv_get_number(typ2);
1149 switch (type)
1150 {
1151 case EXPR_IS:
1152 case EXPR_EQUAL: n1 = (n1 == n2); break;
1153 case EXPR_ISNOT:
1154 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1155 case EXPR_GREATER: n1 = (n1 > n2); break;
1156 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1157 case EXPR_SMALLER: n1 = (n1 < n2); break;
1158 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1159 case EXPR_UNKNOWN:
1160 case EXPR_MATCH:
1161 default: break; // avoid gcc warning
1162 }
1163 }
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001164 else if (in_vim9script() && (typ1->v_type == VAR_BOOL
Bram Moolenaar0c357522021-07-18 14:43:43 +02001165 || typ2->v_type == VAR_BOOL
1166 || (typ1->v_type == VAR_SPECIAL
1167 && typ2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001168 {
1169 if (typ1->v_type != typ2->v_type)
1170 {
1171 semsg(_(e_cannot_compare_str_with_str),
1172 vartype_name(typ1->v_type), vartype_name(typ2->v_type));
1173 clear_tv(typ1);
1174 return FAIL;
1175 }
1176 n1 = typ1->vval.v_number;
1177 n2 = typ2->vval.v_number;
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 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001185 semsg(_(e_invalid_operation_for_str),
1186 vartype_name(typ1->v_type));
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001187 clear_tv(typ1);
1188 return FAIL;
1189 }
1190 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001191 else
1192 {
Bram Moolenaar0c357522021-07-18 14:43:43 +02001193 if (in_vim9script()
1194 && ((typ1->v_type != VAR_STRING && typ1->v_type != VAR_SPECIAL)
1195 || (typ2->v_type != VAR_STRING && typ2->v_type != VAR_SPECIAL)))
1196 {
1197 semsg(_(e_cannot_compare_str_with_str),
1198 vartype_name(typ1->v_type), vartype_name(typ2->v_type));
1199 clear_tv(typ1);
1200 return FAIL;
1201 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001202 s1 = tv_get_string_buf(typ1, buf1);
1203 s2 = tv_get_string_buf(typ2, buf2);
1204 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1205 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1206 else
1207 i = 0;
1208 n1 = FALSE;
1209 switch (type)
1210 {
1211 case EXPR_IS:
1212 case EXPR_EQUAL: n1 = (i == 0); break;
1213 case EXPR_ISNOT:
1214 case EXPR_NEQUAL: n1 = (i != 0); break;
1215 case EXPR_GREATER: n1 = (i > 0); break;
1216 case EXPR_GEQUAL: n1 = (i >= 0); break;
1217 case EXPR_SMALLER: n1 = (i < 0); break;
1218 case EXPR_SEQUAL: n1 = (i <= 0); break;
1219
1220 case EXPR_MATCH:
1221 case EXPR_NOMATCH:
1222 n1 = pattern_match(s2, s1, ic);
1223 if (type == EXPR_NOMATCH)
1224 n1 = !n1;
1225 break;
1226
1227 default: break; // avoid gcc warning
1228 }
1229 }
1230 clear_tv(typ1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001231 if (in_vim9script())
1232 {
1233 typ1->v_type = VAR_BOOL;
1234 typ1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
1235 }
1236 else
1237 {
1238 typ1->v_type = VAR_NUMBER;
1239 typ1->vval.v_number = n1;
1240 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001241
1242 return OK;
1243}
1244
Bram Moolenaar34453202021-01-31 13:08:38 +01001245/*
1246 * Convert any type to a string, never give an error.
1247 * When "quotes" is TRUE add quotes to a string.
1248 * Returns an allocated string.
1249 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001250 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001251typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001252{
1253 char_u *tofree;
1254 char_u numbuf[NUMBUFLEN];
1255 char_u *ret = NULL;
1256
1257 if (arg == NULL)
1258 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001259 if (!quotes && arg->v_type == VAR_STRING)
1260 {
1261 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1262 : arg->vval.v_string);
1263 }
1264 else
1265 {
1266 ret = tv2string(arg, &tofree, numbuf, 0);
1267 // Make a copy if we have a value but it's not in allocated memory.
1268 if (ret != NULL && tofree == NULL)
1269 ret = vim_strsave(ret);
1270 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001271 return ret;
1272}
1273
1274/*
1275 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1276 * or it refers to a List or Dictionary that is locked.
1277 */
1278 int
1279tv_islocked(typval_T *tv)
1280{
1281 return (tv->v_lock & VAR_LOCKED)
1282 || (tv->v_type == VAR_LIST
1283 && tv->vval.v_list != NULL
1284 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1285 || (tv->v_type == VAR_DICT
1286 && tv->vval.v_dict != NULL
1287 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1288}
1289
1290 static int
1291func_equal(
1292 typval_T *tv1,
1293 typval_T *tv2,
1294 int ic) // ignore case
1295{
1296 char_u *s1, *s2;
1297 dict_T *d1, *d2;
1298 int a1, a2;
1299 int i;
1300
1301 // empty and NULL function name considered the same
1302 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1303 : partial_name(tv1->vval.v_partial);
1304 if (s1 != NULL && *s1 == NUL)
1305 s1 = NULL;
1306 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1307 : partial_name(tv2->vval.v_partial);
1308 if (s2 != NULL && *s2 == NUL)
1309 s2 = NULL;
1310 if (s1 == NULL || s2 == NULL)
1311 {
1312 if (s1 != s2)
1313 return FALSE;
1314 }
1315 else if (STRCMP(s1, s2) != 0)
1316 return FALSE;
1317
1318 // empty dict and NULL dict is different
1319 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1320 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1321 if (d1 == NULL || d2 == NULL)
1322 {
1323 if (d1 != d2)
1324 return FALSE;
1325 }
1326 else if (!dict_equal(d1, d2, ic, TRUE))
1327 return FALSE;
1328
1329 // empty list and no list considered the same
1330 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1331 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1332 if (a1 != a2)
1333 return FALSE;
1334 for (i = 0; i < a1; ++i)
1335 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1336 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1337 return FALSE;
1338
1339 return TRUE;
1340}
1341
1342/*
1343 * Return TRUE if "tv1" and "tv2" have the same value.
1344 * Compares the items just like "==" would compare them, but strings and
1345 * numbers are different. Floats and numbers are also different.
1346 */
1347 int
1348tv_equal(
1349 typval_T *tv1,
1350 typval_T *tv2,
1351 int ic, // ignore case
1352 int recursive) // TRUE when used recursively
1353{
1354 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1355 char_u *s1, *s2;
1356 static int recursive_cnt = 0; // catch recursive loops
1357 int r;
1358 static int tv_equal_recurse_limit;
1359
1360 // Catch lists and dicts that have an endless loop by limiting
1361 // recursiveness to a limit. We guess they are equal then.
1362 // A fixed limit has the problem of still taking an awful long time.
1363 // Reduce the limit every time running into it. That should work fine for
1364 // deeply linked structures that are not recursively linked and catch
1365 // recursiveness quickly.
1366 if (!recursive)
1367 tv_equal_recurse_limit = 1000;
1368 if (recursive_cnt >= tv_equal_recurse_limit)
1369 {
1370 --tv_equal_recurse_limit;
1371 return TRUE;
1372 }
1373
1374 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1375 // arguments.
1376 if ((tv1->v_type == VAR_FUNC
1377 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1378 && (tv2->v_type == VAR_FUNC
1379 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1380 {
1381 ++recursive_cnt;
1382 r = func_equal(tv1, tv2, ic);
1383 --recursive_cnt;
1384 return r;
1385 }
1386
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001387 if (tv1->v_type != tv2->v_type
1388 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1389 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001390 return FALSE;
1391
1392 switch (tv1->v_type)
1393 {
1394 case VAR_LIST:
1395 ++recursive_cnt;
1396 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1397 --recursive_cnt;
1398 return r;
1399
1400 case VAR_DICT:
1401 ++recursive_cnt;
1402 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1403 --recursive_cnt;
1404 return r;
1405
1406 case VAR_BLOB:
1407 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1408
1409 case VAR_NUMBER:
1410 case VAR_BOOL:
1411 case VAR_SPECIAL:
1412 return tv1->vval.v_number == tv2->vval.v_number;
1413
1414 case VAR_STRING:
1415 s1 = tv_get_string_buf(tv1, buf1);
1416 s2 = tv_get_string_buf(tv2, buf2);
1417 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1418
1419 case VAR_FLOAT:
1420#ifdef FEAT_FLOAT
1421 return tv1->vval.v_float == tv2->vval.v_float;
1422#endif
1423 case VAR_JOB:
1424#ifdef FEAT_JOB_CHANNEL
1425 return tv1->vval.v_job == tv2->vval.v_job;
1426#endif
1427 case VAR_CHANNEL:
1428#ifdef FEAT_JOB_CHANNEL
1429 return tv1->vval.v_channel == tv2->vval.v_channel;
1430#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001431 case VAR_INSTR:
1432 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001433
1434 case VAR_PARTIAL:
1435 return tv1->vval.v_partial == tv2->vval.v_partial;
1436
1437 case VAR_FUNC:
1438 return tv1->vval.v_string == tv2->vval.v_string;
1439
1440 case VAR_UNKNOWN:
1441 case VAR_ANY:
1442 case VAR_VOID:
1443 break;
1444 }
1445
1446 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1447 // does not equal anything, not even itself.
1448 return FALSE;
1449}
1450
1451/*
1452 * Get an option value.
1453 * "arg" points to the '&' or '+' before the option name.
1454 * "arg" is advanced to character after the option name.
1455 * Return OK or FAIL.
1456 */
1457 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001458eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001459 char_u **arg,
1460 typval_T *rettv, // when NULL, only check if option exists
1461 int evaluate)
1462{
1463 char_u *option_end;
1464 long numval;
1465 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001466 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001467 int c;
1468 int working = (**arg == '+'); // has("+option")
1469 int ret = OK;
1470 int opt_flags;
1471
1472 // Isolate the option name and find its value.
1473 option_end = find_option_end(arg, &opt_flags);
1474 if (option_end == NULL)
1475 {
1476 if (rettv != NULL)
1477 semsg(_("E112: Option name missing: %s"), *arg);
1478 return FAIL;
1479 }
1480
1481 if (!evaluate)
1482 {
1483 *arg = option_end;
1484 return OK;
1485 }
1486
1487 c = *option_end;
1488 *option_end = NUL;
1489 opt_type = get_option_value(*arg, &numval,
1490 rettv == NULL ? NULL : &stringval, opt_flags);
1491
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001492 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001493 {
1494 if (rettv != NULL)
1495 semsg(_(e_unknown_option), *arg);
1496 ret = FAIL;
1497 }
1498 else if (rettv != NULL)
1499 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001500 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001501 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001502 {
1503 rettv->v_type = VAR_STRING;
1504 rettv->vval.v_string = NULL;
1505 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001506 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001507 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001508 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1509 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001510 rettv->vval.v_number = 0;
1511 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001512 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001513 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001514 if (in_vim9script() && opt_type == gov_bool)
1515 {
1516 rettv->v_type = VAR_BOOL;
1517 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1518 }
1519 else
1520 {
1521 rettv->v_type = VAR_NUMBER;
1522 rettv->vval.v_number = numval;
1523 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001524 }
1525 else // string option
1526 {
1527 rettv->v_type = VAR_STRING;
1528 rettv->vval.v_string = stringval;
1529 }
1530 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001531 else if (working && (opt_type == gov_hidden_bool
1532 || opt_type == gov_hidden_number
1533 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001534 ret = FAIL;
1535
1536 *option_end = c; // put back for error messages
1537 *arg = option_end;
1538
1539 return ret;
1540}
1541
1542/*
1543 * Allocate a variable for a number constant. Also deals with "0z" for blob.
1544 * Return OK or FAIL.
1545 */
1546 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001547eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001548 char_u **arg,
1549 typval_T *rettv,
1550 int evaluate,
1551 int want_string UNUSED)
1552{
1553 int len;
1554#ifdef FEAT_FLOAT
1555 char_u *p;
1556 int get_float = FALSE;
1557
1558 // We accept a float when the format matches
1559 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
1560 // strict to avoid backwards compatibility problems.
1561 // With script version 2 and later the leading digit can be
1562 // omitted.
1563 // Don't look for a float after the "." operator, so that
1564 // ":let vers = 1.2.3" doesn't fail.
1565 if (**arg == '.')
1566 p = *arg;
1567 else
1568 p = skipdigits(*arg + 1);
1569 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
1570 {
1571 get_float = TRUE;
1572 p = skipdigits(p + 2);
1573 if (*p == 'e' || *p == 'E')
1574 {
1575 ++p;
1576 if (*p == '-' || *p == '+')
1577 ++p;
1578 if (!vim_isdigit(*p))
1579 get_float = FALSE;
1580 else
1581 p = skipdigits(p + 1);
1582 }
1583 if (ASCII_ISALPHA(*p) || *p == '.')
1584 get_float = FALSE;
1585 }
1586 if (get_float)
1587 {
1588 float_T f;
1589
1590 *arg += string2float(*arg, &f);
1591 if (evaluate)
1592 {
1593 rettv->v_type = VAR_FLOAT;
1594 rettv->vval.v_float = f;
1595 }
1596 }
1597 else
1598#endif
1599 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
1600 {
1601 char_u *bp;
1602 blob_T *blob = NULL; // init for gcc
1603
1604 // Blob constant: 0z0123456789abcdef
1605 if (evaluate)
1606 blob = blob_alloc();
1607 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
1608 {
1609 if (!vim_isxdigit(bp[1]))
1610 {
1611 if (blob != NULL)
1612 {
1613 emsg(_("E973: Blob literal should have an even number of hex characters"));
1614 ga_clear(&blob->bv_ga);
1615 VIM_CLEAR(blob);
1616 }
1617 return FAIL;
1618 }
1619 if (blob != NULL)
1620 ga_append(&blob->bv_ga,
1621 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
1622 if (bp[2] == '.' && vim_isxdigit(bp[3]))
1623 ++bp;
1624 }
1625 if (blob != NULL)
1626 rettv_blob_set(rettv, blob);
1627 *arg = bp;
1628 }
1629 else
1630 {
1631 varnumber_T n;
1632
1633 // decimal, hex or octal number
1634 vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4
1635 ? STR2NR_NO_OCT + STR2NR_QUOTE
1636 : STR2NR_ALL, &n, NULL, 0, TRUE);
1637 if (len == 0)
1638 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001639 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001640 return FAIL;
1641 }
1642 *arg += len;
1643 if (evaluate)
1644 {
1645 rettv->v_type = VAR_NUMBER;
1646 rettv->vval.v_number = n;
1647 }
1648 }
1649 return OK;
1650}
1651
1652/*
1653 * Allocate a variable for a string constant.
1654 * Return OK or FAIL.
1655 */
1656 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001657eval_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001658{
1659 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001660 char_u *end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001661 int extra = 0;
1662 int len;
1663
1664 // Find the end of the string, skipping backslashed characters.
1665 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
1666 {
1667 if (*p == '\\' && p[1] != NUL)
1668 {
1669 ++p;
1670 // A "\<x>" form occupies at least 4 characters, and produces up
1671 // to 21 characters (3 * 6 for the char and 3 for a modifier):
1672 // reserve space for 18 extra.
1673 // Each byte in the char could be encoded as K_SPECIAL K_EXTRA x.
1674 if (*p == '<')
1675 extra += 18;
1676 }
1677 }
1678
1679 if (*p != '"')
1680 {
1681 semsg(_("E114: Missing quote: %s"), *arg);
1682 return FAIL;
1683 }
1684
1685 // If only parsing, set *arg and return here
1686 if (!evaluate)
1687 {
1688 *arg = p + 1;
1689 return OK;
1690 }
1691
1692 // Copy the string into allocated memory, handling backslashed
1693 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001694 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001695 len = (int)(p - *arg + extra);
1696 rettv->vval.v_string = alloc(len);
1697 if (rettv->vval.v_string == NULL)
1698 return FAIL;
1699 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001700
1701 for (p = *arg + 1; *p != NUL && *p != '"'; )
1702 {
1703 if (*p == '\\')
1704 {
1705 switch (*++p)
1706 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001707 case 'b': *end++ = BS; ++p; break;
1708 case 'e': *end++ = ESC; ++p; break;
1709 case 'f': *end++ = FF; ++p; break;
1710 case 'n': *end++ = NL; ++p; break;
1711 case 'r': *end++ = CAR; ++p; break;
1712 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001713
1714 case 'X': // hex: "\x1", "\x12"
1715 case 'x':
1716 case 'u': // Unicode: "\u0023"
1717 case 'U':
1718 if (vim_isxdigit(p[1]))
1719 {
1720 int n, nr;
1721 int c = toupper(*p);
1722
1723 if (c == 'X')
1724 n = 2;
1725 else if (*p == 'u')
1726 n = 4;
1727 else
1728 n = 8;
1729 nr = 0;
1730 while (--n >= 0 && vim_isxdigit(p[1]))
1731 {
1732 ++p;
1733 nr = (nr << 4) + hex2nr(*p);
1734 }
1735 ++p;
1736 // For "\u" store the number according to
1737 // 'encoding'.
1738 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001739 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001740 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001741 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001742 }
1743 break;
1744
1745 // octal: "\1", "\12", "\123"
1746 case '0':
1747 case '1':
1748 case '2':
1749 case '3':
1750 case '4':
1751 case '5':
1752 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001753 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001754 if (*p >= '0' && *p <= '7')
1755 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001756 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001757 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001758 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001759 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001760 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001761 break;
1762
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001763 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001764 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001765 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001766 int flags = FSK_KEYCODE | FSK_IN_STRING;
1767
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001768 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001769 flags |= FSK_SIMPLIFY;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001770 extra = trans_special(&p, end, flags, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001771 if (extra != 0)
1772 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001773 end += extra;
1774 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001775 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001776 break;
1777 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001778 }
1779 // FALLTHROUGH
1780
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001781 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001782 break;
1783 }
1784 }
1785 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001786 MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001787 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001788 *end = NUL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001789 if (*p != NUL) // just in case
1790 ++p;
1791 *arg = p;
1792
1793 return OK;
1794}
1795
1796/*
1797 * Allocate a variable for a 'str''ing' constant.
1798 * Return OK or FAIL.
1799 */
1800 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001801eval_lit_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001802{
1803 char_u *p;
1804 char_u *str;
1805 int reduce = 0;
1806
1807 // Find the end of the string, skipping ''.
1808 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
1809 {
1810 if (*p == '\'')
1811 {
1812 if (p[1] != '\'')
1813 break;
1814 ++reduce;
1815 ++p;
1816 }
1817 }
1818
1819 if (*p != '\'')
1820 {
1821 semsg(_("E115: Missing quote: %s"), *arg);
1822 return FAIL;
1823 }
1824
1825 // If only parsing return after setting "*arg"
1826 if (!evaluate)
1827 {
1828 *arg = p + 1;
1829 return OK;
1830 }
1831
1832 // Copy the string into allocated memory, handling '' to ' reduction.
1833 str = alloc((p - *arg) - reduce);
1834 if (str == NULL)
1835 return FAIL;
1836 rettv->v_type = VAR_STRING;
1837 rettv->vval.v_string = str;
1838
1839 for (p = *arg + 1; *p != NUL; )
1840 {
1841 if (*p == '\'')
1842 {
1843 if (p[1] != '\'')
1844 break;
1845 ++p;
1846 }
1847 MB_COPY_CHAR(p, str);
1848 }
1849 *str = NUL;
1850 *arg = p + 1;
1851
1852 return OK;
1853}
1854
1855/*
1856 * Return a string with the string representation of a variable.
1857 * If the memory is allocated "tofree" is set to it, otherwise NULL.
1858 * "numbuf" is used for a number.
1859 * Puts quotes around strings, so that they can be parsed back by eval().
1860 * May return NULL.
1861 */
1862 char_u *
1863tv2string(
1864 typval_T *tv,
1865 char_u **tofree,
1866 char_u *numbuf,
1867 int copyID)
1868{
1869 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
1870}
1871
1872/*
1873 * Get the value of an environment variable.
1874 * "arg" is pointing to the '$'. It is advanced to after the name.
1875 * If the environment variable was not set, silently assume it is empty.
1876 * Return FAIL if the name is invalid.
1877 */
1878 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001879eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001880{
1881 char_u *string = NULL;
1882 int len;
1883 int cc;
1884 char_u *name;
1885 int mustfree = FALSE;
1886
1887 ++*arg;
1888 name = *arg;
1889 len = get_env_len(arg);
1890 if (evaluate)
1891 {
1892 if (len == 0)
1893 return FAIL; // invalid empty name
1894
1895 cc = name[len];
1896 name[len] = NUL;
1897 // first try vim_getenv(), fast for normal environment vars
1898 string = vim_getenv(name, &mustfree);
1899 if (string != NULL && *string != NUL)
1900 {
1901 if (!mustfree)
1902 string = vim_strsave(string);
1903 }
1904 else
1905 {
1906 if (mustfree)
1907 vim_free(string);
1908
1909 // next try expanding things like $VIM and ${HOME}
1910 string = expand_env_save(name - 1);
1911 if (string != NULL && *string == '$')
1912 VIM_CLEAR(string);
1913 }
1914 name[len] = cc;
1915
1916 rettv->v_type = VAR_STRING;
1917 rettv->vval.v_string = string;
1918 }
1919
1920 return OK;
1921}
1922
1923/*
1924 * Get the lnum from the first argument.
1925 * Also accepts ".", "$", etc., but that only works for the current buffer.
1926 * Returns -1 on error.
1927 */
1928 linenr_T
1929tv_get_lnum(typval_T *argvars)
1930{
Bram Moolenaar9a963372020-12-21 21:58:46 +01001931 linenr_T lnum = -1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001932
Bram Moolenaar56acb092020-08-16 14:48:19 +02001933 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
1934 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02001935 if (lnum <= 0 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001936 {
1937 int fnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001938 pos_T *fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001939
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02001940 // no valid number, try using arg like line()
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001941 if (fp != NULL)
1942 lnum = fp->lnum;
1943 }
1944 return lnum;
1945}
1946
1947/*
1948 * Get the lnum from the first argument.
1949 * Also accepts "$", then "buf" is used.
1950 * Returns 0 on error.
1951 */
1952 linenr_T
1953tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1954{
1955 if (argvars[0].v_type == VAR_STRING
1956 && argvars[0].vval.v_string != NULL
1957 && argvars[0].vval.v_string[0] == '$'
1958 && buf != NULL)
1959 return buf->b_ml.ml_line_count;
1960 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1961}
1962
1963/*
1964 * Get buffer by number or pattern.
1965 */
1966 buf_T *
1967tv_get_buf(typval_T *tv, int curtab_only)
1968{
1969 char_u *name = tv->vval.v_string;
1970 buf_T *buf;
1971
1972 if (tv->v_type == VAR_NUMBER)
1973 return buflist_findnr((int)tv->vval.v_number);
1974 if (tv->v_type != VAR_STRING)
1975 return NULL;
1976 if (name == NULL || *name == NUL)
1977 return curbuf;
1978 if (name[0] == '$' && name[1] == NUL)
1979 return lastbuf;
1980
1981 buf = buflist_find_by_name(name, curtab_only);
1982
1983 // If not found, try expanding the name, like done for bufexists().
1984 if (buf == NULL)
1985 buf = find_buffer(tv);
1986
1987 return buf;
1988}
1989
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02001990/*
1991 * Like tv_get_buf() but give an error message is the type is wrong.
1992 */
1993 buf_T *
1994tv_get_buf_from_arg(typval_T *tv)
1995{
1996 buf_T *buf;
1997
1998 ++emsg_off;
1999 buf = tv_get_buf(tv, FALSE);
2000 --emsg_off;
2001 if (buf == NULL
2002 && tv->v_type != VAR_NUMBER
2003 && tv->v_type != VAR_STRING)
2004 // issue errmsg for type error
2005 (void)tv_get_number(tv);
2006 return buf;
2007}
2008
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002009#endif // FEAT_EVAL