blob: e22dd3bbe20f697c4e8233b6317f062c47808ff2 [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/*
526 * Give an error and return FAIL unless "args[idx]" is a job.
527 */
528 int
529check_for_job_arg(typval_T *args, int idx)
530{
531 if (args[idx].v_type != VAR_JOB)
532 {
533 if (idx >= 0)
534 semsg(_(e_job_required_for_argument_nr), idx + 1);
535 else
536 emsg(_(e_jobreq));
537 return FAIL;
538 }
539 return OK;
540}
541
542/*
543 * Give an error and return FAIL unless "args[idx]" is a string or
544 * a number.
545 */
546 int
547check_for_string_or_number_arg(typval_T *args, int idx)
548{
549 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
550 {
551 if (idx >= 0)
552 semsg(_(e_string_required_for_argument_nr), idx + 1);
553 else
554 emsg(_(e_stringreq));
555 return FAIL;
556 }
557 return OK;
558}
559
560/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200561 * Check for an optional string or number argument at 'idx'.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200562 */
563 int
564check_for_opt_string_or_number_arg(typval_T *args, int idx)
565{
566 return (args[idx].v_type == VAR_UNKNOWN
567 || check_for_string_or_number_arg(args, idx) != FAIL);
568}
569
570/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200571 * Give an error and return FAIL unless "args[idx]" is a buffer number.
572 * Buffer number can be a number or a string.
573 */
574 int
575check_for_buffer_arg(typval_T *args, int idx)
576{
577 return check_for_string_or_number_arg(args, idx);
578}
579
580/*
581 * Check for an optional buffer argument at 'idx'
582 */
583 int
584check_for_opt_buffer_arg(typval_T *args, int idx)
585{
586 return (args[idx].v_type == VAR_UNKNOWN
587 || check_for_buffer_arg(args, idx));
588}
589
590/*
591 * Give an error and return FAIL unless "args[idx]" is a line number.
592 * Line number can be a number or a string.
593 */
594 int
595check_for_lnum_arg(typval_T *args, int idx)
596{
597 return check_for_string_or_number_arg(args, idx);
598}
599
600/*
601 * Check for an optional line number argument at 'idx'
602 */
603 int
604check_for_opt_lnum_arg(typval_T *args, int idx)
605{
606 return (args[idx].v_type == VAR_UNKNOWN
607 || check_for_lnum_arg(args, idx));
608}
609
610/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200611 * Give an error and return FAIL unless "args[idx]" is a string or a blob.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200612 */
613 int
614check_for_string_or_blob_arg(typval_T *args, int idx)
615{
616 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
617 {
618 if (idx >= 0)
619 semsg(_(e_string_required_for_argument_nr), idx + 1);
620 else
621 emsg(_(e_stringreq));
622 return FAIL;
623 }
624 return OK;
625}
626
627/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200628 * Give an error and return FAIL unless "args[idx]" is a string or a list.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200629 */
630 int
631check_for_string_or_list_arg(typval_T *args, int idx)
632{
633 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
634 {
635 if (idx >= 0)
636 semsg(_(e_string_required_for_argument_nr), idx + 1);
637 else
638 emsg(_(e_stringreq));
639 return FAIL;
640 }
641 return OK;
642}
643
644/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200645 * Give an error and return FAIL unless "args[idx]" is a list or a blob.
646 */
647 int
648check_for_list_or_blob_arg(typval_T *args, int idx)
649{
650 if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
651 {
652 if (idx >= 0)
653 semsg(_(e_list_required_for_argument_nr), idx + 1);
654 else
655 emsg(_(e_listreq));
656 return FAIL;
657 }
658 return OK;
659}
660
661/*
662 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
663 * blob.
664 */
665 int
666check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
667{
668 if (args[idx].v_type != VAR_LIST
669 && args[idx].v_type != VAR_DICT
670 && args[idx].v_type != VAR_BLOB)
671 {
672 if (idx >= 0)
673 semsg(_(e_list_required_for_argument_nr), idx + 1);
674 else
675 emsg(_(e_listreq));
676 return FAIL;
677 }
678 return OK;
679}
680
681/*
682 * Give an error and return FAIL unless "args[idx]" is a buffer number or a
683 * dict.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200684 */
685 int
686check_for_buffer_or_dict_arg(typval_T *args, int idx)
687{
688 if (args[idx].v_type != VAR_STRING
689 && args[idx].v_type != VAR_NUMBER
690 && args[idx].v_type != VAR_DICT)
691 {
692 if (idx >= 0)
693 semsg(_(e_string_required_for_argument_nr), idx + 1);
694 else
695 emsg(_(e_stringreq));
696 return FAIL;
697 }
698 return OK;
699}
700
701/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200702 * Get the string value of a variable.
703 * If it is a Number variable, the number is converted into a string.
704 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
705 * tv_get_string_buf() uses a given buffer.
706 * If the String variable has never been set, return an empty string.
707 * Never returns NULL;
708 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
709 * NULL on error.
710 */
711 char_u *
712tv_get_string(typval_T *varp)
713{
714 static char_u mybuf[NUMBUFLEN];
715
716 return tv_get_string_buf(varp, mybuf);
717}
718
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100719/*
720 * Like tv_get_string() but don't allow number to string conversion for Vim9.
721 */
722 char_u *
723tv_get_string_strict(typval_T *varp)
724{
725 static char_u mybuf[NUMBUFLEN];
726 char_u *res = tv_get_string_buf_chk_strict(
727 varp, mybuf, in_vim9script());
728
729 return res != NULL ? res : (char_u *)"";
730}
731
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200732 char_u *
733tv_get_string_buf(typval_T *varp, char_u *buf)
734{
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200735 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200736
737 return res != NULL ? res : (char_u *)"";
738}
739
740/*
741 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
742 */
743 char_u *
744tv_get_string_chk(typval_T *varp)
745{
746 static char_u mybuf[NUMBUFLEN];
747
748 return tv_get_string_buf_chk(varp, mybuf);
749}
750
751 char_u *
752tv_get_string_buf_chk(typval_T *varp, char_u *buf)
753{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100754 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
755}
756
757 char_u *
758tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
759{
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200760 switch (varp->v_type)
761 {
762 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100763 if (strict)
764 {
765 emsg(_(e_using_number_as_string));
766 break;
767 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200768 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
769 (varnumber_T)varp->vval.v_number);
770 return buf;
771 case VAR_FUNC:
772 case VAR_PARTIAL:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200773 emsg(_("E729: Using a Funcref as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200774 break;
775 case VAR_LIST:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200776 emsg(_("E730: Using a List as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200777 break;
778 case VAR_DICT:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200779 emsg(_("E731: Using a Dictionary as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200780 break;
781 case VAR_FLOAT:
782#ifdef FEAT_FLOAT
Bram Moolenaar7a2217b2021-06-06 12:33:49 +0200783 if (strict)
784 {
785 emsg(_(e_float_as_string));
786 break;
787 }
788 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
789 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200790#endif
791 case VAR_STRING:
792 if (varp->vval.v_string != NULL)
793 return varp->vval.v_string;
794 return (char_u *)"";
795 case VAR_BOOL:
796 case VAR_SPECIAL:
797 STRCPY(buf, get_var_special_name(varp->vval.v_number));
798 return buf;
799 case VAR_BLOB:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200800 emsg(_("E976: Using a Blob as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200801 break;
802 case VAR_JOB:
803#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200804 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200805 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200806 semsg(_(e_using_invalid_value_as_string_str), "job");
807 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200808 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200809 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200810#endif
811 break;
812 case VAR_CHANNEL:
813#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200814 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200815 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200816 semsg(_(e_using_invalid_value_as_string_str), "channel");
817 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200818 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200819 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200820#endif
821 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200822 case VAR_VOID:
823 emsg(_(e_cannot_use_void_value));
824 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200825 case VAR_UNKNOWN:
826 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200827 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +0200828 semsg(_(e_using_invalid_value_as_string_str),
829 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200830 break;
831 }
832 return NULL;
833}
834
835/*
836 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
837 * string() on Dict, List, etc.
838 */
839 char_u *
840tv_stringify(typval_T *varp, char_u *buf)
841{
842 if (varp->v_type == VAR_LIST
843 || varp->v_type == VAR_DICT
844 || varp->v_type == VAR_BLOB
845 || varp->v_type == VAR_FUNC
846 || varp->v_type == VAR_PARTIAL
847 || varp->v_type == VAR_FLOAT)
848 {
849 typval_T tmp;
850
851 f_string(varp, &tmp);
852 tv_get_string_buf(&tmp, buf);
853 clear_tv(varp);
854 *varp = tmp;
855 return tmp.vval.v_string;
856 }
857 return tv_get_string_buf(varp, buf);
858}
859
860/*
861 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
862 * Also give an error message, using "name" or _("name") when use_gettext is
863 * TRUE.
864 */
865 int
866tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
867{
868 int lock = 0;
869
870 switch (tv->v_type)
871 {
872 case VAR_BLOB:
873 if (tv->vval.v_blob != NULL)
874 lock = tv->vval.v_blob->bv_lock;
875 break;
876 case VAR_LIST:
877 if (tv->vval.v_list != NULL)
878 lock = tv->vval.v_list->lv_lock;
879 break;
880 case VAR_DICT:
881 if (tv->vval.v_dict != NULL)
882 lock = tv->vval.v_dict->dv_lock;
883 break;
884 default:
885 break;
886 }
Bram Moolenaara187c432020-09-16 21:08:28 +0200887 return value_check_lock(tv->v_lock, name, use_gettext)
888 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200889}
890
891/*
892 * Copy the values from typval_T "from" to typval_T "to".
893 * When needed allocates string or increases reference count.
894 * Does not make a copy of a list, blob or dict but copies the reference!
895 * It is OK for "from" and "to" to point to the same item. This is used to
896 * make a copy later.
897 */
898 void
899copy_tv(typval_T *from, typval_T *to)
900{
901 to->v_type = from->v_type;
902 to->v_lock = 0;
903 switch (from->v_type)
904 {
905 case VAR_NUMBER:
906 case VAR_BOOL:
907 case VAR_SPECIAL:
908 to->vval.v_number = from->vval.v_number;
909 break;
910 case VAR_FLOAT:
911#ifdef FEAT_FLOAT
912 to->vval.v_float = from->vval.v_float;
913 break;
914#endif
915 case VAR_JOB:
916#ifdef FEAT_JOB_CHANNEL
917 to->vval.v_job = from->vval.v_job;
918 if (to->vval.v_job != NULL)
919 ++to->vval.v_job->jv_refcount;
920 break;
921#endif
922 case VAR_CHANNEL:
923#ifdef FEAT_JOB_CHANNEL
924 to->vval.v_channel = from->vval.v_channel;
925 if (to->vval.v_channel != NULL)
926 ++to->vval.v_channel->ch_refcount;
927 break;
928#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200929 case VAR_INSTR:
930 to->vval.v_instr = from->vval.v_instr;
931 break;
932
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200933 case VAR_STRING:
934 case VAR_FUNC:
935 if (from->vval.v_string == NULL)
936 to->vval.v_string = NULL;
937 else
938 {
939 to->vval.v_string = vim_strsave(from->vval.v_string);
940 if (from->v_type == VAR_FUNC)
941 func_ref(to->vval.v_string);
942 }
943 break;
944 case VAR_PARTIAL:
945 if (from->vval.v_partial == NULL)
946 to->vval.v_partial = NULL;
947 else
948 {
949 to->vval.v_partial = from->vval.v_partial;
950 ++to->vval.v_partial->pt_refcount;
951 }
952 break;
953 case VAR_BLOB:
954 if (from->vval.v_blob == NULL)
955 to->vval.v_blob = NULL;
956 else
957 {
958 to->vval.v_blob = from->vval.v_blob;
959 ++to->vval.v_blob->bv_refcount;
960 }
961 break;
962 case VAR_LIST:
963 if (from->vval.v_list == NULL)
964 to->vval.v_list = NULL;
965 else
966 {
967 to->vval.v_list = from->vval.v_list;
968 ++to->vval.v_list->lv_refcount;
969 }
970 break;
971 case VAR_DICT:
972 if (from->vval.v_dict == NULL)
973 to->vval.v_dict = NULL;
974 else
975 {
976 to->vval.v_dict = from->vval.v_dict;
977 ++to->vval.v_dict->dv_refcount;
978 }
979 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +0200980 case VAR_VOID:
981 emsg(_(e_cannot_use_void_value));
982 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200983 case VAR_UNKNOWN:
984 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200985 internal_error_no_abort("copy_tv(UNKNOWN)");
986 break;
987 }
988}
989
990/*
991 * Compare "typ1" and "typ2". Put the result in "typ1".
992 */
993 int
994typval_compare(
995 typval_T *typ1, // first operand
996 typval_T *typ2, // second operand
Bram Moolenaar657137c2021-01-09 15:45:23 +0100997 exprtype_T type, // operator
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200998 int ic) // ignore case
999{
1000 int i;
1001 varnumber_T n1, n2;
1002 char_u *s1, *s2;
1003 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1004 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1005
1006 if (type_is && typ1->v_type != typ2->v_type)
1007 {
1008 // For "is" a different type always means FALSE, for "notis"
1009 // it means TRUE.
1010 n1 = (type == EXPR_ISNOT);
1011 }
1012 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
1013 {
1014 if (type_is)
1015 {
1016 n1 = (typ1->v_type == typ2->v_type
1017 && typ1->vval.v_blob == typ2->vval.v_blob);
1018 if (type == EXPR_ISNOT)
1019 n1 = !n1;
1020 }
1021 else if (typ1->v_type != typ2->v_type
1022 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1023 {
1024 if (typ1->v_type != typ2->v_type)
1025 emsg(_("E977: Can only compare Blob with Blob"));
1026 else
1027 emsg(_(e_invalblob));
1028 clear_tv(typ1);
1029 return FAIL;
1030 }
1031 else
1032 {
1033 // Compare two Blobs for being equal or unequal.
1034 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
1035 if (type == EXPR_NEQUAL)
1036 n1 = !n1;
1037 }
1038 }
1039 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
1040 {
1041 if (type_is)
1042 {
1043 n1 = (typ1->v_type == typ2->v_type
1044 && typ1->vval.v_list == typ2->vval.v_list);
1045 if (type == EXPR_ISNOT)
1046 n1 = !n1;
1047 }
1048 else if (typ1->v_type != typ2->v_type
1049 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1050 {
1051 if (typ1->v_type != typ2->v_type)
1052 emsg(_("E691: Can only compare List with List"));
1053 else
1054 emsg(_("E692: Invalid operation for List"));
1055 clear_tv(typ1);
1056 return FAIL;
1057 }
1058 else
1059 {
1060 // Compare two Lists for being equal or unequal.
1061 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
1062 ic, FALSE);
1063 if (type == EXPR_NEQUAL)
1064 n1 = !n1;
1065 }
1066 }
1067
1068 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
1069 {
1070 if (type_is)
1071 {
1072 n1 = (typ1->v_type == typ2->v_type
1073 && typ1->vval.v_dict == typ2->vval.v_dict);
1074 if (type == EXPR_ISNOT)
1075 n1 = !n1;
1076 }
1077 else if (typ1->v_type != typ2->v_type
1078 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1079 {
1080 if (typ1->v_type != typ2->v_type)
1081 emsg(_("E735: Can only compare Dictionary with Dictionary"));
1082 else
1083 emsg(_("E736: Invalid operation for Dictionary"));
1084 clear_tv(typ1);
1085 return FAIL;
1086 }
1087 else
1088 {
1089 // Compare two Dictionaries for being equal or unequal.
1090 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
1091 ic, FALSE);
1092 if (type == EXPR_NEQUAL)
1093 n1 = !n1;
1094 }
1095 }
1096
1097 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
1098 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
1099 {
1100 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1101 && type != EXPR_IS && type != EXPR_ISNOT)
1102 {
1103 emsg(_("E694: Invalid operation for Funcrefs"));
1104 clear_tv(typ1);
1105 return FAIL;
1106 }
1107 if ((typ1->v_type == VAR_PARTIAL
1108 && typ1->vval.v_partial == NULL)
1109 || (typ2->v_type == VAR_PARTIAL
1110 && typ2->vval.v_partial == NULL))
1111 // When both partials are NULL, then they are equal.
1112 // Otherwise they are not equal.
1113 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
1114 else if (type_is)
1115 {
1116 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
1117 // strings are considered the same if their value is
1118 // the same
1119 n1 = tv_equal(typ1, typ2, ic, FALSE);
1120 else if (typ1->v_type == VAR_PARTIAL
1121 && typ2->v_type == VAR_PARTIAL)
1122 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
1123 else
1124 n1 = FALSE;
1125 }
1126 else
1127 n1 = tv_equal(typ1, typ2, ic, FALSE);
1128 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1129 n1 = !n1;
1130 }
1131
1132#ifdef FEAT_FLOAT
1133 // If one of the two variables is a float, compare as a float.
1134 // When using "=~" or "!~", always compare as string.
1135 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
1136 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1137 {
1138 float_T f1, f2;
1139
1140 f1 = tv_get_float(typ1);
1141 f2 = tv_get_float(typ2);
1142 n1 = FALSE;
1143 switch (type)
1144 {
1145 case EXPR_IS:
1146 case EXPR_EQUAL: n1 = (f1 == f2); break;
1147 case EXPR_ISNOT:
1148 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1149 case EXPR_GREATER: n1 = (f1 > f2); break;
1150 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1151 case EXPR_SMALLER: n1 = (f1 < f2); break;
1152 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1153 case EXPR_UNKNOWN:
1154 case EXPR_MATCH:
1155 default: break; // avoid gcc warning
1156 }
1157 }
1158#endif
1159
1160 // If one of the two variables is a number, compare as a number.
1161 // When using "=~" or "!~", always compare as string.
1162 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
1163 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1164 {
1165 n1 = tv_get_number(typ1);
1166 n2 = tv_get_number(typ2);
1167 switch (type)
1168 {
1169 case EXPR_IS:
1170 case EXPR_EQUAL: n1 = (n1 == n2); break;
1171 case EXPR_ISNOT:
1172 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1173 case EXPR_GREATER: n1 = (n1 > n2); break;
1174 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1175 case EXPR_SMALLER: n1 = (n1 < n2); break;
1176 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1177 case EXPR_UNKNOWN:
1178 case EXPR_MATCH:
1179 default: break; // avoid gcc warning
1180 }
1181 }
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001182 else if (in_vim9script() && (typ1->v_type == VAR_BOOL
Bram Moolenaar0c357522021-07-18 14:43:43 +02001183 || typ2->v_type == VAR_BOOL
1184 || (typ1->v_type == VAR_SPECIAL
1185 && typ2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001186 {
1187 if (typ1->v_type != typ2->v_type)
1188 {
1189 semsg(_(e_cannot_compare_str_with_str),
1190 vartype_name(typ1->v_type), vartype_name(typ2->v_type));
1191 clear_tv(typ1);
1192 return FAIL;
1193 }
1194 n1 = typ1->vval.v_number;
1195 n2 = typ2->vval.v_number;
1196 switch (type)
1197 {
1198 case EXPR_IS:
1199 case EXPR_EQUAL: n1 = (n1 == n2); break;
1200 case EXPR_ISNOT:
1201 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1202 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001203 semsg(_(e_invalid_operation_for_str),
1204 vartype_name(typ1->v_type));
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001205 clear_tv(typ1);
1206 return FAIL;
1207 }
1208 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001209 else
1210 {
Bram Moolenaar0c357522021-07-18 14:43:43 +02001211 if (in_vim9script()
1212 && ((typ1->v_type != VAR_STRING && typ1->v_type != VAR_SPECIAL)
1213 || (typ2->v_type != VAR_STRING && typ2->v_type != VAR_SPECIAL)))
1214 {
1215 semsg(_(e_cannot_compare_str_with_str),
1216 vartype_name(typ1->v_type), vartype_name(typ2->v_type));
1217 clear_tv(typ1);
1218 return FAIL;
1219 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001220 s1 = tv_get_string_buf(typ1, buf1);
1221 s2 = tv_get_string_buf(typ2, buf2);
1222 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1223 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1224 else
1225 i = 0;
1226 n1 = FALSE;
1227 switch (type)
1228 {
1229 case EXPR_IS:
1230 case EXPR_EQUAL: n1 = (i == 0); break;
1231 case EXPR_ISNOT:
1232 case EXPR_NEQUAL: n1 = (i != 0); break;
1233 case EXPR_GREATER: n1 = (i > 0); break;
1234 case EXPR_GEQUAL: n1 = (i >= 0); break;
1235 case EXPR_SMALLER: n1 = (i < 0); break;
1236 case EXPR_SEQUAL: n1 = (i <= 0); break;
1237
1238 case EXPR_MATCH:
1239 case EXPR_NOMATCH:
1240 n1 = pattern_match(s2, s1, ic);
1241 if (type == EXPR_NOMATCH)
1242 n1 = !n1;
1243 break;
1244
1245 default: break; // avoid gcc warning
1246 }
1247 }
1248 clear_tv(typ1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001249 if (in_vim9script())
1250 {
1251 typ1->v_type = VAR_BOOL;
1252 typ1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
1253 }
1254 else
1255 {
1256 typ1->v_type = VAR_NUMBER;
1257 typ1->vval.v_number = n1;
1258 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001259
1260 return OK;
1261}
1262
Bram Moolenaar34453202021-01-31 13:08:38 +01001263/*
1264 * Convert any type to a string, never give an error.
1265 * When "quotes" is TRUE add quotes to a string.
1266 * Returns an allocated string.
1267 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001268 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001269typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001270{
1271 char_u *tofree;
1272 char_u numbuf[NUMBUFLEN];
1273 char_u *ret = NULL;
1274
1275 if (arg == NULL)
1276 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001277 if (!quotes && arg->v_type == VAR_STRING)
1278 {
1279 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1280 : arg->vval.v_string);
1281 }
1282 else
1283 {
1284 ret = tv2string(arg, &tofree, numbuf, 0);
1285 // Make a copy if we have a value but it's not in allocated memory.
1286 if (ret != NULL && tofree == NULL)
1287 ret = vim_strsave(ret);
1288 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001289 return ret;
1290}
1291
1292/*
1293 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1294 * or it refers to a List or Dictionary that is locked.
1295 */
1296 int
1297tv_islocked(typval_T *tv)
1298{
1299 return (tv->v_lock & VAR_LOCKED)
1300 || (tv->v_type == VAR_LIST
1301 && tv->vval.v_list != NULL
1302 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1303 || (tv->v_type == VAR_DICT
1304 && tv->vval.v_dict != NULL
1305 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1306}
1307
1308 static int
1309func_equal(
1310 typval_T *tv1,
1311 typval_T *tv2,
1312 int ic) // ignore case
1313{
1314 char_u *s1, *s2;
1315 dict_T *d1, *d2;
1316 int a1, a2;
1317 int i;
1318
1319 // empty and NULL function name considered the same
1320 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1321 : partial_name(tv1->vval.v_partial);
1322 if (s1 != NULL && *s1 == NUL)
1323 s1 = NULL;
1324 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1325 : partial_name(tv2->vval.v_partial);
1326 if (s2 != NULL && *s2 == NUL)
1327 s2 = NULL;
1328 if (s1 == NULL || s2 == NULL)
1329 {
1330 if (s1 != s2)
1331 return FALSE;
1332 }
1333 else if (STRCMP(s1, s2) != 0)
1334 return FALSE;
1335
1336 // empty dict and NULL dict is different
1337 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1338 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1339 if (d1 == NULL || d2 == NULL)
1340 {
1341 if (d1 != d2)
1342 return FALSE;
1343 }
1344 else if (!dict_equal(d1, d2, ic, TRUE))
1345 return FALSE;
1346
1347 // empty list and no list considered the same
1348 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1349 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1350 if (a1 != a2)
1351 return FALSE;
1352 for (i = 0; i < a1; ++i)
1353 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1354 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1355 return FALSE;
1356
1357 return TRUE;
1358}
1359
1360/*
1361 * Return TRUE if "tv1" and "tv2" have the same value.
1362 * Compares the items just like "==" would compare them, but strings and
1363 * numbers are different. Floats and numbers are also different.
1364 */
1365 int
1366tv_equal(
1367 typval_T *tv1,
1368 typval_T *tv2,
1369 int ic, // ignore case
1370 int recursive) // TRUE when used recursively
1371{
1372 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1373 char_u *s1, *s2;
1374 static int recursive_cnt = 0; // catch recursive loops
1375 int r;
1376 static int tv_equal_recurse_limit;
1377
1378 // Catch lists and dicts that have an endless loop by limiting
1379 // recursiveness to a limit. We guess they are equal then.
1380 // A fixed limit has the problem of still taking an awful long time.
1381 // Reduce the limit every time running into it. That should work fine for
1382 // deeply linked structures that are not recursively linked and catch
1383 // recursiveness quickly.
1384 if (!recursive)
1385 tv_equal_recurse_limit = 1000;
1386 if (recursive_cnt >= tv_equal_recurse_limit)
1387 {
1388 --tv_equal_recurse_limit;
1389 return TRUE;
1390 }
1391
1392 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1393 // arguments.
1394 if ((tv1->v_type == VAR_FUNC
1395 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1396 && (tv2->v_type == VAR_FUNC
1397 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1398 {
1399 ++recursive_cnt;
1400 r = func_equal(tv1, tv2, ic);
1401 --recursive_cnt;
1402 return r;
1403 }
1404
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001405 if (tv1->v_type != tv2->v_type
1406 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1407 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001408 return FALSE;
1409
1410 switch (tv1->v_type)
1411 {
1412 case VAR_LIST:
1413 ++recursive_cnt;
1414 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1415 --recursive_cnt;
1416 return r;
1417
1418 case VAR_DICT:
1419 ++recursive_cnt;
1420 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1421 --recursive_cnt;
1422 return r;
1423
1424 case VAR_BLOB:
1425 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1426
1427 case VAR_NUMBER:
1428 case VAR_BOOL:
1429 case VAR_SPECIAL:
1430 return tv1->vval.v_number == tv2->vval.v_number;
1431
1432 case VAR_STRING:
1433 s1 = tv_get_string_buf(tv1, buf1);
1434 s2 = tv_get_string_buf(tv2, buf2);
1435 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1436
1437 case VAR_FLOAT:
1438#ifdef FEAT_FLOAT
1439 return tv1->vval.v_float == tv2->vval.v_float;
1440#endif
1441 case VAR_JOB:
1442#ifdef FEAT_JOB_CHANNEL
1443 return tv1->vval.v_job == tv2->vval.v_job;
1444#endif
1445 case VAR_CHANNEL:
1446#ifdef FEAT_JOB_CHANNEL
1447 return tv1->vval.v_channel == tv2->vval.v_channel;
1448#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001449 case VAR_INSTR:
1450 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001451
1452 case VAR_PARTIAL:
1453 return tv1->vval.v_partial == tv2->vval.v_partial;
1454
1455 case VAR_FUNC:
1456 return tv1->vval.v_string == tv2->vval.v_string;
1457
1458 case VAR_UNKNOWN:
1459 case VAR_ANY:
1460 case VAR_VOID:
1461 break;
1462 }
1463
1464 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1465 // does not equal anything, not even itself.
1466 return FALSE;
1467}
1468
1469/*
1470 * Get an option value.
1471 * "arg" points to the '&' or '+' before the option name.
1472 * "arg" is advanced to character after the option name.
1473 * Return OK or FAIL.
1474 */
1475 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001476eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001477 char_u **arg,
1478 typval_T *rettv, // when NULL, only check if option exists
1479 int evaluate)
1480{
1481 char_u *option_end;
1482 long numval;
1483 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001484 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001485 int c;
1486 int working = (**arg == '+'); // has("+option")
1487 int ret = OK;
1488 int opt_flags;
1489
1490 // Isolate the option name and find its value.
1491 option_end = find_option_end(arg, &opt_flags);
1492 if (option_end == NULL)
1493 {
1494 if (rettv != NULL)
1495 semsg(_("E112: Option name missing: %s"), *arg);
1496 return FAIL;
1497 }
1498
1499 if (!evaluate)
1500 {
1501 *arg = option_end;
1502 return OK;
1503 }
1504
1505 c = *option_end;
1506 *option_end = NUL;
1507 opt_type = get_option_value(*arg, &numval,
1508 rettv == NULL ? NULL : &stringval, opt_flags);
1509
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001510 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001511 {
1512 if (rettv != NULL)
1513 semsg(_(e_unknown_option), *arg);
1514 ret = FAIL;
1515 }
1516 else if (rettv != NULL)
1517 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001518 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001519 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001520 {
1521 rettv->v_type = VAR_STRING;
1522 rettv->vval.v_string = NULL;
1523 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001524 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001525 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001526 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1527 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001528 rettv->vval.v_number = 0;
1529 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001530 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001531 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001532 if (in_vim9script() && opt_type == gov_bool)
1533 {
1534 rettv->v_type = VAR_BOOL;
1535 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1536 }
1537 else
1538 {
1539 rettv->v_type = VAR_NUMBER;
1540 rettv->vval.v_number = numval;
1541 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001542 }
1543 else // string option
1544 {
1545 rettv->v_type = VAR_STRING;
1546 rettv->vval.v_string = stringval;
1547 }
1548 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001549 else if (working && (opt_type == gov_hidden_bool
1550 || opt_type == gov_hidden_number
1551 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001552 ret = FAIL;
1553
1554 *option_end = c; // put back for error messages
1555 *arg = option_end;
1556
1557 return ret;
1558}
1559
1560/*
1561 * Allocate a variable for a number constant. Also deals with "0z" for blob.
1562 * Return OK or FAIL.
1563 */
1564 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001565eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001566 char_u **arg,
1567 typval_T *rettv,
1568 int evaluate,
1569 int want_string UNUSED)
1570{
1571 int len;
1572#ifdef FEAT_FLOAT
1573 char_u *p;
1574 int get_float = FALSE;
1575
1576 // We accept a float when the format matches
1577 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
1578 // strict to avoid backwards compatibility problems.
1579 // With script version 2 and later the leading digit can be
1580 // omitted.
1581 // Don't look for a float after the "." operator, so that
1582 // ":let vers = 1.2.3" doesn't fail.
1583 if (**arg == '.')
1584 p = *arg;
1585 else
1586 p = skipdigits(*arg + 1);
1587 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
1588 {
1589 get_float = TRUE;
1590 p = skipdigits(p + 2);
1591 if (*p == 'e' || *p == 'E')
1592 {
1593 ++p;
1594 if (*p == '-' || *p == '+')
1595 ++p;
1596 if (!vim_isdigit(*p))
1597 get_float = FALSE;
1598 else
1599 p = skipdigits(p + 1);
1600 }
1601 if (ASCII_ISALPHA(*p) || *p == '.')
1602 get_float = FALSE;
1603 }
1604 if (get_float)
1605 {
1606 float_T f;
1607
1608 *arg += string2float(*arg, &f);
1609 if (evaluate)
1610 {
1611 rettv->v_type = VAR_FLOAT;
1612 rettv->vval.v_float = f;
1613 }
1614 }
1615 else
1616#endif
1617 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
1618 {
1619 char_u *bp;
1620 blob_T *blob = NULL; // init for gcc
1621
1622 // Blob constant: 0z0123456789abcdef
1623 if (evaluate)
1624 blob = blob_alloc();
1625 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
1626 {
1627 if (!vim_isxdigit(bp[1]))
1628 {
1629 if (blob != NULL)
1630 {
1631 emsg(_("E973: Blob literal should have an even number of hex characters"));
1632 ga_clear(&blob->bv_ga);
1633 VIM_CLEAR(blob);
1634 }
1635 return FAIL;
1636 }
1637 if (blob != NULL)
1638 ga_append(&blob->bv_ga,
1639 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
1640 if (bp[2] == '.' && vim_isxdigit(bp[3]))
1641 ++bp;
1642 }
1643 if (blob != NULL)
1644 rettv_blob_set(rettv, blob);
1645 *arg = bp;
1646 }
1647 else
1648 {
1649 varnumber_T n;
1650
1651 // decimal, hex or octal number
1652 vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4
1653 ? STR2NR_NO_OCT + STR2NR_QUOTE
1654 : STR2NR_ALL, &n, NULL, 0, TRUE);
1655 if (len == 0)
1656 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001657 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001658 return FAIL;
1659 }
1660 *arg += len;
1661 if (evaluate)
1662 {
1663 rettv->v_type = VAR_NUMBER;
1664 rettv->vval.v_number = n;
1665 }
1666 }
1667 return OK;
1668}
1669
1670/*
1671 * Allocate a variable for a string constant.
1672 * Return OK or FAIL.
1673 */
1674 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001675eval_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001676{
1677 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001678 char_u *end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001679 int extra = 0;
1680 int len;
1681
1682 // Find the end of the string, skipping backslashed characters.
1683 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
1684 {
1685 if (*p == '\\' && p[1] != NUL)
1686 {
1687 ++p;
1688 // A "\<x>" form occupies at least 4 characters, and produces up
1689 // to 21 characters (3 * 6 for the char and 3 for a modifier):
1690 // reserve space for 18 extra.
1691 // Each byte in the char could be encoded as K_SPECIAL K_EXTRA x.
1692 if (*p == '<')
1693 extra += 18;
1694 }
1695 }
1696
1697 if (*p != '"')
1698 {
1699 semsg(_("E114: Missing quote: %s"), *arg);
1700 return FAIL;
1701 }
1702
1703 // If only parsing, set *arg and return here
1704 if (!evaluate)
1705 {
1706 *arg = p + 1;
1707 return OK;
1708 }
1709
1710 // Copy the string into allocated memory, handling backslashed
1711 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001712 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001713 len = (int)(p - *arg + extra);
1714 rettv->vval.v_string = alloc(len);
1715 if (rettv->vval.v_string == NULL)
1716 return FAIL;
1717 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001718
1719 for (p = *arg + 1; *p != NUL && *p != '"'; )
1720 {
1721 if (*p == '\\')
1722 {
1723 switch (*++p)
1724 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001725 case 'b': *end++ = BS; ++p; break;
1726 case 'e': *end++ = ESC; ++p; break;
1727 case 'f': *end++ = FF; ++p; break;
1728 case 'n': *end++ = NL; ++p; break;
1729 case 'r': *end++ = CAR; ++p; break;
1730 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001731
1732 case 'X': // hex: "\x1", "\x12"
1733 case 'x':
1734 case 'u': // Unicode: "\u0023"
1735 case 'U':
1736 if (vim_isxdigit(p[1]))
1737 {
1738 int n, nr;
1739 int c = toupper(*p);
1740
1741 if (c == 'X')
1742 n = 2;
1743 else if (*p == 'u')
1744 n = 4;
1745 else
1746 n = 8;
1747 nr = 0;
1748 while (--n >= 0 && vim_isxdigit(p[1]))
1749 {
1750 ++p;
1751 nr = (nr << 4) + hex2nr(*p);
1752 }
1753 ++p;
1754 // For "\u" store the number according to
1755 // 'encoding'.
1756 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001757 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001758 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001759 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001760 }
1761 break;
1762
1763 // octal: "\1", "\12", "\123"
1764 case '0':
1765 case '1':
1766 case '2':
1767 case '3':
1768 case '4':
1769 case '5':
1770 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001771 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001772 if (*p >= '0' && *p <= '7')
1773 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001774 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001775 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001776 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001777 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001778 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001779 break;
1780
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001781 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001782 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001783 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001784 int flags = FSK_KEYCODE | FSK_IN_STRING;
1785
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001786 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001787 flags |= FSK_SIMPLIFY;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001788 extra = trans_special(&p, end, flags, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001789 if (extra != 0)
1790 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001791 end += extra;
1792 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001793 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001794 break;
1795 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001796 }
1797 // FALLTHROUGH
1798
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001799 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001800 break;
1801 }
1802 }
1803 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001804 MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001805 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001806 *end = NUL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001807 if (*p != NUL) // just in case
1808 ++p;
1809 *arg = p;
1810
1811 return OK;
1812}
1813
1814/*
1815 * Allocate a variable for a 'str''ing' constant.
1816 * Return OK or FAIL.
1817 */
1818 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001819eval_lit_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001820{
1821 char_u *p;
1822 char_u *str;
1823 int reduce = 0;
1824
1825 // Find the end of the string, skipping ''.
1826 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
1827 {
1828 if (*p == '\'')
1829 {
1830 if (p[1] != '\'')
1831 break;
1832 ++reduce;
1833 ++p;
1834 }
1835 }
1836
1837 if (*p != '\'')
1838 {
1839 semsg(_("E115: Missing quote: %s"), *arg);
1840 return FAIL;
1841 }
1842
1843 // If only parsing return after setting "*arg"
1844 if (!evaluate)
1845 {
1846 *arg = p + 1;
1847 return OK;
1848 }
1849
1850 // Copy the string into allocated memory, handling '' to ' reduction.
1851 str = alloc((p - *arg) - reduce);
1852 if (str == NULL)
1853 return FAIL;
1854 rettv->v_type = VAR_STRING;
1855 rettv->vval.v_string = str;
1856
1857 for (p = *arg + 1; *p != NUL; )
1858 {
1859 if (*p == '\'')
1860 {
1861 if (p[1] != '\'')
1862 break;
1863 ++p;
1864 }
1865 MB_COPY_CHAR(p, str);
1866 }
1867 *str = NUL;
1868 *arg = p + 1;
1869
1870 return OK;
1871}
1872
1873/*
1874 * Return a string with the string representation of a variable.
1875 * If the memory is allocated "tofree" is set to it, otherwise NULL.
1876 * "numbuf" is used for a number.
1877 * Puts quotes around strings, so that they can be parsed back by eval().
1878 * May return NULL.
1879 */
1880 char_u *
1881tv2string(
1882 typval_T *tv,
1883 char_u **tofree,
1884 char_u *numbuf,
1885 int copyID)
1886{
1887 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
1888}
1889
1890/*
1891 * Get the value of an environment variable.
1892 * "arg" is pointing to the '$'. It is advanced to after the name.
1893 * If the environment variable was not set, silently assume it is empty.
1894 * Return FAIL if the name is invalid.
1895 */
1896 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001897eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001898{
1899 char_u *string = NULL;
1900 int len;
1901 int cc;
1902 char_u *name;
1903 int mustfree = FALSE;
1904
1905 ++*arg;
1906 name = *arg;
1907 len = get_env_len(arg);
1908 if (evaluate)
1909 {
1910 if (len == 0)
1911 return FAIL; // invalid empty name
1912
1913 cc = name[len];
1914 name[len] = NUL;
1915 // first try vim_getenv(), fast for normal environment vars
1916 string = vim_getenv(name, &mustfree);
1917 if (string != NULL && *string != NUL)
1918 {
1919 if (!mustfree)
1920 string = vim_strsave(string);
1921 }
1922 else
1923 {
1924 if (mustfree)
1925 vim_free(string);
1926
1927 // next try expanding things like $VIM and ${HOME}
1928 string = expand_env_save(name - 1);
1929 if (string != NULL && *string == '$')
1930 VIM_CLEAR(string);
1931 }
1932 name[len] = cc;
1933
1934 rettv->v_type = VAR_STRING;
1935 rettv->vval.v_string = string;
1936 }
1937
1938 return OK;
1939}
1940
1941/*
1942 * Get the lnum from the first argument.
1943 * Also accepts ".", "$", etc., but that only works for the current buffer.
1944 * Returns -1 on error.
1945 */
1946 linenr_T
1947tv_get_lnum(typval_T *argvars)
1948{
Bram Moolenaar9a963372020-12-21 21:58:46 +01001949 linenr_T lnum = -1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001950
Bram Moolenaar56acb092020-08-16 14:48:19 +02001951 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
1952 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02001953 if (lnum <= 0 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001954 {
1955 int fnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001956 pos_T *fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001957
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02001958 // no valid number, try using arg like line()
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001959 if (fp != NULL)
1960 lnum = fp->lnum;
1961 }
1962 return lnum;
1963}
1964
1965/*
1966 * Get the lnum from the first argument.
1967 * Also accepts "$", then "buf" is used.
1968 * Returns 0 on error.
1969 */
1970 linenr_T
1971tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1972{
1973 if (argvars[0].v_type == VAR_STRING
1974 && argvars[0].vval.v_string != NULL
1975 && argvars[0].vval.v_string[0] == '$'
1976 && buf != NULL)
1977 return buf->b_ml.ml_line_count;
1978 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1979}
1980
1981/*
1982 * Get buffer by number or pattern.
1983 */
1984 buf_T *
1985tv_get_buf(typval_T *tv, int curtab_only)
1986{
1987 char_u *name = tv->vval.v_string;
1988 buf_T *buf;
1989
1990 if (tv->v_type == VAR_NUMBER)
1991 return buflist_findnr((int)tv->vval.v_number);
1992 if (tv->v_type != VAR_STRING)
1993 return NULL;
1994 if (name == NULL || *name == NUL)
1995 return curbuf;
1996 if (name[0] == '$' && name[1] == NUL)
1997 return lastbuf;
1998
1999 buf = buflist_find_by_name(name, curtab_only);
2000
2001 // If not found, try expanding the name, like done for bufexists().
2002 if (buf == NULL)
2003 buf = find_buffer(tv);
2004
2005 return buf;
2006}
2007
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002008/*
2009 * Like tv_get_buf() but give an error message is the type is wrong.
2010 */
2011 buf_T *
2012tv_get_buf_from_arg(typval_T *tv)
2013{
2014 buf_T *buf;
2015
2016 ++emsg_off;
2017 buf = tv_get_buf(tv, FALSE);
2018 --emsg_off;
2019 if (buf == NULL
2020 && tv->v_type != VAR_NUMBER
2021 && tv->v_type != VAR_STRING)
2022 // issue errmsg for type error
2023 (void)tv_get_number(tv);
2024 return buf;
2025}
2026
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002027#endif // FEAT_EVAL