1 /* 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 *
alloc_tv(void)23 alloc_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 *
alloc_string_tv(char_u * s)34 alloc_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
free_tv(typval_T * varp)53 free_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:
94 	    case VAR_INSTR:
95 		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
clear_tv(typval_T * varp)105 clear_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
154 		break;
155 	    case VAR_INSTR:
156 		VIM_CLEAR(varp->vval.v_instr);
157 		break;
158 	    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
init_tv(typval_T * varp)171 init_tv(typval_T *varp)
172 {
173     if (varp != NULL)
174 	CLEAR_POINTER(varp);
175 }
176 
177     static varnumber_T
tv_get_bool_or_number_chk(typval_T * varp,int * denote,int want_bool)178 tv_get_bool_or_number_chk(typval_T *varp, int *denote, int want_bool)
179 {
180     varnumber_T	n = 0L;
181 
182     switch (varp->v_type)
183     {
184 	case VAR_NUMBER:
185 	    if (in_vim9script() && want_bool && varp->vval.v_number != 0
186 						   && varp->vval.v_number != 1)
187 	    {
188 		semsg(_(e_using_number_as_bool_nr), varp->vval.v_number);
189 		break;
190 	    }
191 	    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:
202 	    if (in_vim9script())
203 	    {
204 		emsg_using_string_as(varp, !want_bool);
205 		break;
206 	    }
207 	    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:
219 	    if (!want_bool && in_vim9script())
220 	    {
221 		if (varp->v_type == VAR_BOOL)
222 		    emsg(_(e_using_bool_as_number));
223 		else
224 		    emsg(_("E611: Using a Special as a Number"));
225 		break;
226 	    }
227 	    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;
241 	case VAR_VOID:
242 	    emsg(_(e_cannot_use_void_value));
243 	    break;
244 	case VAR_UNKNOWN:
245 	case VAR_ANY:
246 	case VAR_INSTR:
247 	    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 
257 /*
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
tv_get_number(typval_T * varp)266 tv_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
tv_get_number_chk(typval_T * varp,int * denote)274 tv_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(),
281  * but in Vim9 script accepts Number (0 and 1) and Bool/Special.
282  */
283     varnumber_T
tv_get_bool(typval_T * varp)284 tv_get_bool(typval_T *varp)
285 {
286     return tv_get_bool_or_number_chk(varp, NULL, TRUE);
287 }
288 
289 /*
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
tv_get_bool_chk(typval_T * varp,int * denote)294 tv_get_bool_chk(typval_T *varp, int *denote)
295 {
296     return tv_get_bool_or_number_chk(varp, denote, TRUE);
297 }
298 
299 #if defined(FEAT_FLOAT) || defined(PROTO)
300     float_T
tv_get_float(typval_T * varp)301 tv_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;
341 	case VAR_VOID:
342 	    emsg(_(e_cannot_use_void_value));
343 	    break;
344 	case VAR_UNKNOWN:
345 	case VAR_ANY:
346 	case VAR_INSTR:
347 	    internal_error_no_abort("tv_get_float(UNKNOWN)");
348 	    break;
349     }
350     return 0;
351 }
352 #endif
353 
354 /*
355  * Give an error and return FAIL unless "args[idx]" is a string.
356  */
357     int
check_for_string_arg(typval_T * args,int idx)358 check_for_string_arg(typval_T *args, int idx)
359 {
360     if (args[idx].v_type != VAR_STRING)
361     {
362 	if (idx >= 0)
363 	    semsg(_(e_string_required_for_argument_nr), idx + 1);
364 	else
365 	    emsg(_(e_stringreq));
366 	return FAIL;
367     }
368     return OK;
369 }
370 
371 /*
372  * Give an error and return FAIL unless "args[idx]" is a non-empty string.
373  */
374     int
check_for_nonempty_string_arg(typval_T * args,int idx)375 check_for_nonempty_string_arg(typval_T *args, int idx)
376 {
377     if (check_for_string_arg(args, idx) == FAIL)
378 	return FAIL;
379     if (args[idx].vval.v_string == NULL || *args[idx].vval.v_string == NUL)
380     {
381 	semsg(_(e_non_empty_string_required_for_argument_nr), idx + 1);
382 	return FAIL;
383     }
384     return OK;
385 }
386 
387 /*
388  * Check for an optional string argument at 'idx'
389  */
390     int
check_for_opt_string_arg(typval_T * args,int idx)391 check_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 /*
398  * Give an error and return FAIL unless "args[idx]" is a number.
399  */
400     int
check_for_number_arg(typval_T * args,int idx)401 check_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 /*
415  * Check for an optional number argument at 'idx'
416  */
417     int
check_for_opt_number_arg(typval_T * args,int idx)418 check_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 /*
425  * Give an error and return FAIL unless "args[idx]" is a float or a number.
426  */
427     int
check_for_float_or_nr_arg(typval_T * args,int idx)428 check_for_float_or_nr_arg(typval_T *args, int idx)
429 {
430     if (args[idx].v_type != VAR_FLOAT && args[idx].v_type != VAR_NUMBER)
431     {
432 	if (idx >= 0)
433 	    semsg(_(e_float_or_number_required_for_argument_nr), idx + 1);
434 	else
435 	    emsg(_(e_numberreq));
436 	return FAIL;
437     }
438     return OK;
439 }
440 
441 /*
442  * Give an error and return FAIL unless "args[idx]" is a bool.
443  */
444     int
check_for_bool_arg(typval_T * args,int idx)445 check_for_bool_arg(typval_T *args, int idx)
446 {
447     if (args[idx].v_type != VAR_BOOL
448 	    && !(args[idx].v_type == VAR_NUMBER
449 		&& (args[idx].vval.v_number == 0
450 		    || args[idx].vval.v_number == 1)))
451     {
452 	if (idx >= 0)
453 	    semsg(_(e_bool_required_for_argument_nr), idx + 1);
454 	else
455 	    emsg(_(e_boolreq));
456 	return FAIL;
457     }
458     return OK;
459 }
460 
461 /*
462  * Check for an optional bool argument at 'idx'.
463  * Return FAIL if the type is wrong.
464  */
465     int
check_for_opt_bool_arg(typval_T * args,int idx)466 check_for_opt_bool_arg(typval_T *args, int idx)
467 {
468     if (args[idx].v_type == VAR_UNKNOWN)
469 	return OK;
470     return check_for_bool_arg(args, idx);
471 }
472 
473 /*
474  * Give an error and return FAIL unless "args[idx]" is a blob.
475  */
476     int
check_for_blob_arg(typval_T * args,int idx)477 check_for_blob_arg(typval_T *args, int idx)
478 {
479     if (args[idx].v_type != VAR_BLOB)
480     {
481 	if (idx >= 0)
482 	    semsg(_(e_blob_required_for_argument_nr), idx + 1);
483 	else
484 	    emsg(_(e_blob_required));
485 	return FAIL;
486     }
487     return OK;
488 }
489 
490 /*
491  * Give an error and return FAIL unless "args[idx]" is a list.
492  */
493     int
check_for_list_arg(typval_T * args,int idx)494 check_for_list_arg(typval_T *args, int idx)
495 {
496     if (args[idx].v_type != VAR_LIST)
497     {
498 	if (idx >= 0)
499 	    semsg(_(e_list_required_for_argument_nr), idx + 1);
500 	else
501 	    emsg(_(e_listreq));
502 	return FAIL;
503     }
504     return OK;
505 }
506 
507 /*
508  * Check for an optional list argument at 'idx'
509  */
510     int
check_for_opt_list_arg(typval_T * args,int idx)511 check_for_opt_list_arg(typval_T *args, int idx)
512 {
513     return (args[idx].v_type == VAR_UNKNOWN
514 	    || check_for_list_arg(args, idx) != FAIL);
515 }
516 
517 /*
518  * Give an error and return FAIL unless "args[idx]" is a dict.
519  */
520     int
check_for_dict_arg(typval_T * args,int idx)521 check_for_dict_arg(typval_T *args, int idx)
522 {
523     if (args[idx].v_type != VAR_DICT)
524     {
525 	if (idx >= 0)
526 	    semsg(_(e_dict_required_for_argument_nr), idx + 1);
527 	else
528 	    emsg(_(e_dictreq));
529 	return FAIL;
530     }
531     return OK;
532 }
533 
534 /*
535  * Check for an optional dict argument at 'idx'
536  */
537     int
check_for_opt_dict_arg(typval_T * args,int idx)538 check_for_opt_dict_arg(typval_T *args, int idx)
539 {
540     return (args[idx].v_type == VAR_UNKNOWN
541 	    || check_for_dict_arg(args, idx) != FAIL);
542 }
543 
544 /*
545  * Give an error and return FAIL unless "args[idx]" is a channel or a job.
546  */
547     int
check_for_chan_or_job_arg(typval_T * args,int idx)548 check_for_chan_or_job_arg(typval_T *args, int idx)
549 {
550     if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
551     {
552 	if (idx >= 0)
553 	    semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
554 	else
555 	    emsg(_(e_chan_or_job_req));
556 	return FAIL;
557     }
558     return OK;
559 }
560 
561 /*
562  * Give an error and return FAIL unless "args[idx]" is an optional channel or a
563  * job.
564  */
565     int
check_for_opt_chan_or_job_arg(typval_T * args,int idx)566 check_for_opt_chan_or_job_arg(typval_T *args, int idx)
567 {
568     return (args[idx].v_type == VAR_UNKNOWN
569 	    || check_for_chan_or_job_arg(args, idx) != FAIL);
570 }
571 
572 /*
573  * Give an error and return FAIL unless "args[idx]" is a job.
574  */
575     int
check_for_job_arg(typval_T * args,int idx)576 check_for_job_arg(typval_T *args, int idx)
577 {
578     if (args[idx].v_type != VAR_JOB)
579     {
580 	if (idx >= 0)
581 	    semsg(_(e_job_required_for_argument_nr), idx + 1);
582 	else
583 	    emsg(_(e_jobreq));
584 	return FAIL;
585     }
586     return OK;
587 }
588 
589 /*
590  * Check for an optional job argument at 'idx'.
591  */
592     int
check_for_opt_job_arg(typval_T * args,int idx)593 check_for_opt_job_arg(typval_T *args, int idx)
594 {
595     return (args[idx].v_type == VAR_UNKNOWN
596 	    || check_for_job_arg(args, idx) != FAIL);
597 }
598 
599 /*
600  * Give an error and return FAIL unless "args[idx]" is a string or
601  * a number.
602  */
603     int
check_for_string_or_number_arg(typval_T * args,int idx)604 check_for_string_or_number_arg(typval_T *args, int idx)
605 {
606     if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
607     {
608 	if (idx >= 0)
609 	    semsg(_(e_string_or_number_required_for_argument_nr), idx + 1);
610 	else
611 	    emsg(_(e_stringreq));
612 	return FAIL;
613     }
614     return OK;
615 }
616 
617 /*
618  * Check for an optional string or number argument at 'idx'.
619  */
620     int
check_for_opt_string_or_number_arg(typval_T * args,int idx)621 check_for_opt_string_or_number_arg(typval_T *args, int idx)
622 {
623     return (args[idx].v_type == VAR_UNKNOWN
624 	    || check_for_string_or_number_arg(args, idx) != FAIL);
625 }
626 
627 /*
628  * Give an error and return FAIL unless "args[idx]" is a buffer number.
629  * Buffer number can be a number or a string.
630  */
631     int
check_for_buffer_arg(typval_T * args,int idx)632 check_for_buffer_arg(typval_T *args, int idx)
633 {
634     return check_for_string_or_number_arg(args, idx);
635 }
636 
637 /*
638  * Check for an optional buffer argument at 'idx'
639  */
640     int
check_for_opt_buffer_arg(typval_T * args,int idx)641 check_for_opt_buffer_arg(typval_T *args, int idx)
642 {
643     return (args[idx].v_type == VAR_UNKNOWN
644 	    || check_for_buffer_arg(args, idx));
645 }
646 
647 /*
648  * Give an error and return FAIL unless "args[idx]" is a line number.
649  * Line number can be a number or a string.
650  */
651     int
check_for_lnum_arg(typval_T * args,int idx)652 check_for_lnum_arg(typval_T *args, int idx)
653 {
654     return check_for_string_or_number_arg(args, idx);
655 }
656 
657 /*
658  * Check for an optional line number argument at 'idx'
659  */
660     int
check_for_opt_lnum_arg(typval_T * args,int idx)661 check_for_opt_lnum_arg(typval_T *args, int idx)
662 {
663     return (args[idx].v_type == VAR_UNKNOWN
664 	    || check_for_lnum_arg(args, idx));
665 }
666 
667 /*
668  * Give an error and return FAIL unless "args[idx]" is a string or a blob.
669  */
670     int
check_for_string_or_blob_arg(typval_T * args,int idx)671 check_for_string_or_blob_arg(typval_T *args, int idx)
672 {
673     if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
674     {
675 	if (idx >= 0)
676 	    semsg(_(e_string_or_blob_required_for_argument_nr), idx + 1);
677 	else
678 	    emsg(_(e_stringreq));
679 	return FAIL;
680     }
681     return OK;
682 }
683 
684 /*
685  * Give an error and return FAIL unless "args[idx]" is a string or a list.
686  */
687     int
check_for_string_or_list_arg(typval_T * args,int idx)688 check_for_string_or_list_arg(typval_T *args, int idx)
689 {
690     if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
691     {
692 	if (idx >= 0)
693 	    semsg(_(e_string_or_list_required_for_argument_nr), idx + 1);
694 	else
695 	    emsg(_(e_stringreq));
696 	return FAIL;
697     }
698     return OK;
699 }
700 
701 /*
702  * Check for an optional string or list argument at 'idx'
703  */
704     int
check_for_opt_string_or_list_arg(typval_T * args,int idx)705 check_for_opt_string_or_list_arg(typval_T *args, int idx)
706 {
707     return (args[idx].v_type == VAR_UNKNOWN
708 	    || check_for_string_or_list_arg(args, idx));
709 }
710 
711 /*
712  * Give an error and return FAIL unless "args[idx]" is a string or a dict.
713  */
714     int
check_for_string_or_dict_arg(typval_T * args,int idx)715 check_for_string_or_dict_arg(typval_T *args, int idx)
716 {
717     if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_DICT)
718     {
719 	if (idx >= 0)
720 	    semsg(_(e_string_or_dict_required_for_argument_nr), idx + 1);
721 	else
722 	    emsg(_(e_stringreq));
723 	return FAIL;
724     }
725     return OK;
726 }
727 
728 /*
729  * Give an error and return FAIL unless "args[idx]" is a string or a number
730  * or a list.
731  */
732     int
check_for_string_or_number_or_list_arg(typval_T * args,int idx)733 check_for_string_or_number_or_list_arg(typval_T *args, int idx)
734 {
735     if (args[idx].v_type != VAR_STRING
736 	    && args[idx].v_type != VAR_NUMBER
737 	    && args[idx].v_type != VAR_LIST)
738     {
739 	if (idx >= 0)
740 	    semsg(_(e_string_number_or_list_required_for_argument_nr), idx + 1);
741 	else
742 	    emsg(_(e_stringreq));
743 	return FAIL;
744     }
745     return OK;
746 }
747 
748 /*
749  * Give an error and return FAIL unless "args[idx]" is an optional string
750  * or number or a list
751  */
752     int
check_for_opt_string_or_number_or_list_arg(typval_T * args,int idx)753 check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx)
754 {
755     return (args[idx].v_type == VAR_UNKNOWN
756 	    || check_for_string_or_number_or_list_arg(args, idx) != FAIL);
757 }
758 
759 /*
760  * Give an error and return FAIL unless "args[idx]" is a string or a list
761  * or a dict.
762  */
763     int
check_for_string_or_list_or_dict_arg(typval_T * args,int idx)764 check_for_string_or_list_or_dict_arg(typval_T *args, int idx)
765 {
766     if (args[idx].v_type != VAR_STRING
767 	    && args[idx].v_type != VAR_LIST
768 	    && args[idx].v_type != VAR_DICT)
769     {
770 	if (idx >= 0)
771 	    semsg(_(e_string_list_or_dict_required_for_argument_nr), idx + 1);
772 	else
773 	    emsg(_(e_stringreq));
774 	return FAIL;
775     }
776     return OK;
777 }
778 
779 /*
780  * Give an error and return FAIL unless "args[idx]" is a list or a blob.
781  */
782     int
check_for_list_or_blob_arg(typval_T * args,int idx)783 check_for_list_or_blob_arg(typval_T *args, int idx)
784 {
785     if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
786     {
787 	if (idx >= 0)
788 	    semsg(_(e_list_or_blob_required_for_argument_nr), idx + 1);
789 	else
790 	    emsg(_(e_listreq));
791 	return FAIL;
792     }
793     return OK;
794 }
795 
796 /*
797  * Give an error and return FAIL unless "args[idx]" is a list or dict
798  */
799     int
check_for_list_or_dict_arg(typval_T * args,int idx)800 check_for_list_or_dict_arg(typval_T *args, int idx)
801 {
802     if (args[idx].v_type != VAR_LIST
803 	    && args[idx].v_type != VAR_DICT)
804     {
805 	if (idx >= 0)
806 	    semsg(_(e_list_or_dict_required_for_argument_nr), idx + 1);
807 	else
808 	    emsg(_(e_listreq));
809 	return FAIL;
810     }
811     return OK;
812 }
813 
814 /*
815  * Give an error and return FAIL unless "args[idx]" is a list or dict or a
816  * blob.
817  */
818     int
check_for_list_or_dict_or_blob_arg(typval_T * args,int idx)819 check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
820 {
821     if (args[idx].v_type != VAR_LIST
822 	    && args[idx].v_type != VAR_DICT
823 	    && args[idx].v_type != VAR_BLOB)
824     {
825 	if (idx >= 0)
826 	    semsg(_(e_list_dict_or_blob_required_for_argument_nr), idx + 1);
827 	else
828 	    emsg(_(e_listreq));
829 	return FAIL;
830     }
831     return OK;
832 }
833 
834 /*
835  * Give an error and return FAIL unless "args[idx]" is an optional buffer
836  * number or a dict.
837  */
838     int
check_for_opt_buffer_or_dict_arg(typval_T * args,int idx)839 check_for_opt_buffer_or_dict_arg(typval_T *args, int idx)
840 {
841     if (args[idx].v_type != VAR_UNKNOWN
842 	    && args[idx].v_type != VAR_STRING
843 	    && args[idx].v_type != VAR_NUMBER
844 	    && args[idx].v_type != VAR_DICT)
845     {
846 	if (idx >= 0)
847 	    semsg(_(e_string_required_for_argument_nr), idx + 1);
848 	else
849 	    emsg(_(e_stringreq));
850 	return FAIL;
851     }
852     return OK;
853 }
854 
855 /*
856  * Get the string value of a variable.
857  * If it is a Number variable, the number is converted into a string.
858  * tv_get_string() uses a single, static buffer.  YOU CAN ONLY USE IT ONCE!
859  * tv_get_string_buf() uses a given buffer.
860  * If the String variable has never been set, return an empty string.
861  * Never returns NULL;
862  * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
863  * NULL on error.
864  */
865     char_u *
tv_get_string(typval_T * varp)866 tv_get_string(typval_T *varp)
867 {
868     static char_u   mybuf[NUMBUFLEN];
869 
870     return tv_get_string_buf(varp, mybuf);
871 }
872 
873 /*
874  * Like tv_get_string() but don't allow number to string conversion for Vim9.
875  */
876     char_u *
tv_get_string_strict(typval_T * varp)877 tv_get_string_strict(typval_T *varp)
878 {
879     static char_u   mybuf[NUMBUFLEN];
880     char_u	    *res =  tv_get_string_buf_chk_strict(
881 						 varp, mybuf, in_vim9script());
882 
883     return res != NULL ? res : (char_u *)"";
884 }
885 
886     char_u *
tv_get_string_buf(typval_T * varp,char_u * buf)887 tv_get_string_buf(typval_T *varp, char_u *buf)
888 {
889     char_u	*res = tv_get_string_buf_chk(varp, buf);
890 
891     return res != NULL ? res : (char_u *)"";
892 }
893 
894 /*
895  * Careful: This uses a single, static buffer.  YOU CAN ONLY USE IT ONCE!
896  */
897     char_u *
tv_get_string_chk(typval_T * varp)898 tv_get_string_chk(typval_T *varp)
899 {
900     static char_u   mybuf[NUMBUFLEN];
901 
902     return tv_get_string_buf_chk(varp, mybuf);
903 }
904 
905     char_u *
tv_get_string_buf_chk(typval_T * varp,char_u * buf)906 tv_get_string_buf_chk(typval_T *varp, char_u *buf)
907 {
908     return tv_get_string_buf_chk_strict(varp, buf, FALSE);
909 }
910 
911     char_u *
tv_get_string_buf_chk_strict(typval_T * varp,char_u * buf,int strict)912 tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
913 {
914     switch (varp->v_type)
915     {
916 	case VAR_NUMBER:
917 	    if (strict)
918 	    {
919 		emsg(_(e_using_number_as_string));
920 		break;
921 	    }
922 	    vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
923 					    (varnumber_T)varp->vval.v_number);
924 	    return buf;
925 	case VAR_FUNC:
926 	case VAR_PARTIAL:
927 	    emsg(_("E729: Using a Funcref as a String"));
928 	    break;
929 	case VAR_LIST:
930 	    emsg(_("E730: Using a List as a String"));
931 	    break;
932 	case VAR_DICT:
933 	    emsg(_("E731: Using a Dictionary as a String"));
934 	    break;
935 	case VAR_FLOAT:
936 #ifdef FEAT_FLOAT
937 	    if (strict)
938 	    {
939 		emsg(_(e_float_as_string));
940 		break;
941 	    }
942 	    vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
943 	    return buf;
944 #endif
945 	case VAR_STRING:
946 	    if (varp->vval.v_string != NULL)
947 		return varp->vval.v_string;
948 	    return (char_u *)"";
949 	case VAR_BOOL:
950 	case VAR_SPECIAL:
951 	    STRCPY(buf, get_var_special_name(varp->vval.v_number));
952 	    return buf;
953         case VAR_BLOB:
954 	    emsg(_("E976: Using a Blob as a String"));
955 	    break;
956 	case VAR_JOB:
957 #ifdef FEAT_JOB_CHANNEL
958 	    if (in_vim9script())
959 	    {
960 		semsg(_(e_using_invalid_value_as_string_str), "job");
961 		break;
962 	    }
963 	    return job_to_string_buf(varp, buf);
964 #endif
965 	    break;
966 	case VAR_CHANNEL:
967 #ifdef FEAT_JOB_CHANNEL
968 	    if (in_vim9script())
969 	    {
970 		semsg(_(e_using_invalid_value_as_string_str), "channel");
971 		break;
972 	    }
973 	    return channel_to_string_buf(varp, buf);
974 #endif
975 	    break;
976 	case VAR_VOID:
977 	    emsg(_(e_cannot_use_void_value));
978 	    break;
979 	case VAR_UNKNOWN:
980 	case VAR_ANY:
981 	case VAR_INSTR:
982 	    semsg(_(e_using_invalid_value_as_string_str),
983 						  vartype_name(varp->v_type));
984 	    break;
985     }
986     return NULL;
987 }
988 
989 /*
990  * Turn a typeval into a string.  Similar to tv_get_string_buf() but uses
991  * string() on Dict, List, etc.
992  */
993     char_u *
tv_stringify(typval_T * varp,char_u * buf)994 tv_stringify(typval_T *varp, char_u *buf)
995 {
996     if (varp->v_type == VAR_LIST
997 	    || varp->v_type == VAR_DICT
998 	    || varp->v_type == VAR_BLOB
999 	    || varp->v_type == VAR_FUNC
1000 	    || varp->v_type == VAR_PARTIAL
1001 	    || varp->v_type == VAR_FLOAT)
1002     {
1003 	typval_T tmp;
1004 
1005 	f_string(varp, &tmp);
1006 	tv_get_string_buf(&tmp, buf);
1007 	clear_tv(varp);
1008 	*varp = tmp;
1009 	return tmp.vval.v_string;
1010     }
1011     return tv_get_string_buf(varp, buf);
1012 }
1013 
1014 /*
1015  * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
1016  * Also give an error message, using "name" or _("name") when use_gettext is
1017  * TRUE.
1018  */
1019     int
tv_check_lock(typval_T * tv,char_u * name,int use_gettext)1020 tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
1021 {
1022     int	lock = 0;
1023 
1024     switch (tv->v_type)
1025     {
1026 	case VAR_BLOB:
1027 	    if (tv->vval.v_blob != NULL)
1028 		lock = tv->vval.v_blob->bv_lock;
1029 	    break;
1030 	case VAR_LIST:
1031 	    if (tv->vval.v_list != NULL)
1032 		lock = tv->vval.v_list->lv_lock;
1033 	    break;
1034 	case VAR_DICT:
1035 	    if (tv->vval.v_dict != NULL)
1036 		lock = tv->vval.v_dict->dv_lock;
1037 	    break;
1038 	default:
1039 	    break;
1040     }
1041     return value_check_lock(tv->v_lock, name, use_gettext)
1042 		   || (lock != 0 && value_check_lock(lock, name, use_gettext));
1043 }
1044 
1045 /*
1046  * Copy the values from typval_T "from" to typval_T "to".
1047  * When needed allocates string or increases reference count.
1048  * Does not make a copy of a list, blob or dict but copies the reference!
1049  * It is OK for "from" and "to" to point to the same item.  This is used to
1050  * make a copy later.
1051  */
1052     void
copy_tv(typval_T * from,typval_T * to)1053 copy_tv(typval_T *from, typval_T *to)
1054 {
1055     to->v_type = from->v_type;
1056     to->v_lock = 0;
1057     switch (from->v_type)
1058     {
1059 	case VAR_NUMBER:
1060 	case VAR_BOOL:
1061 	case VAR_SPECIAL:
1062 	    to->vval.v_number = from->vval.v_number;
1063 	    break;
1064 	case VAR_FLOAT:
1065 #ifdef FEAT_FLOAT
1066 	    to->vval.v_float = from->vval.v_float;
1067 	    break;
1068 #endif
1069 	case VAR_JOB:
1070 #ifdef FEAT_JOB_CHANNEL
1071 	    to->vval.v_job = from->vval.v_job;
1072 	    if (to->vval.v_job != NULL)
1073 		++to->vval.v_job->jv_refcount;
1074 	    break;
1075 #endif
1076 	case VAR_CHANNEL:
1077 #ifdef FEAT_JOB_CHANNEL
1078 	    to->vval.v_channel = from->vval.v_channel;
1079 	    if (to->vval.v_channel != NULL)
1080 		++to->vval.v_channel->ch_refcount;
1081 	    break;
1082 #endif
1083 	case VAR_INSTR:
1084 	    to->vval.v_instr = from->vval.v_instr;
1085 	    break;
1086 
1087 	case VAR_STRING:
1088 	case VAR_FUNC:
1089 	    if (from->vval.v_string == NULL)
1090 		to->vval.v_string = NULL;
1091 	    else
1092 	    {
1093 		to->vval.v_string = vim_strsave(from->vval.v_string);
1094 		if (from->v_type == VAR_FUNC)
1095 		    func_ref(to->vval.v_string);
1096 	    }
1097 	    break;
1098 	case VAR_PARTIAL:
1099 	    if (from->vval.v_partial == NULL)
1100 		to->vval.v_partial = NULL;
1101 	    else
1102 	    {
1103 		to->vval.v_partial = from->vval.v_partial;
1104 		++to->vval.v_partial->pt_refcount;
1105 	    }
1106 	    break;
1107 	case VAR_BLOB:
1108 	    if (from->vval.v_blob == NULL)
1109 		to->vval.v_blob = NULL;
1110 	    else
1111 	    {
1112 		to->vval.v_blob = from->vval.v_blob;
1113 		++to->vval.v_blob->bv_refcount;
1114 	    }
1115 	    break;
1116 	case VAR_LIST:
1117 	    if (from->vval.v_list == NULL)
1118 		to->vval.v_list = NULL;
1119 	    else
1120 	    {
1121 		to->vval.v_list = from->vval.v_list;
1122 		++to->vval.v_list->lv_refcount;
1123 	    }
1124 	    break;
1125 	case VAR_DICT:
1126 	    if (from->vval.v_dict == NULL)
1127 		to->vval.v_dict = NULL;
1128 	    else
1129 	    {
1130 		to->vval.v_dict = from->vval.v_dict;
1131 		++to->vval.v_dict->dv_refcount;
1132 	    }
1133 	    break;
1134 	case VAR_VOID:
1135 	    emsg(_(e_cannot_use_void_value));
1136 	    break;
1137 	case VAR_UNKNOWN:
1138 	case VAR_ANY:
1139 	    internal_error_no_abort("copy_tv(UNKNOWN)");
1140 	    break;
1141     }
1142 }
1143 
1144 /*
1145  * Compare "typ1" and "typ2".  Put the result in "typ1".
1146  */
1147     int
typval_compare(typval_T * typ1,typval_T * typ2,exprtype_T type,int ic)1148 typval_compare(
1149     typval_T	*typ1,   // first operand
1150     typval_T	*typ2,   // second operand
1151     exprtype_T	type,    // operator
1152     int		ic)      // ignore case
1153 {
1154     int		i;
1155     varnumber_T	n1, n2;
1156     char_u	*s1, *s2;
1157     char_u	buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1158     int		type_is = type == EXPR_IS || type == EXPR_ISNOT;
1159 
1160     if (type_is && typ1->v_type != typ2->v_type)
1161     {
1162 	// For "is" a different type always means FALSE, for "notis"
1163 	// it means TRUE.
1164 	n1 = (type == EXPR_ISNOT);
1165     }
1166     else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
1167     {
1168 	if (type_is)
1169 	{
1170 	    n1 = (typ1->v_type == typ2->v_type
1171 			    && typ1->vval.v_blob == typ2->vval.v_blob);
1172 	    if (type == EXPR_ISNOT)
1173 		n1 = !n1;
1174 	}
1175 	else if (typ1->v_type != typ2->v_type
1176 		|| (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1177 	{
1178 	    if (typ1->v_type != typ2->v_type)
1179 		emsg(_("E977: Can only compare Blob with Blob"));
1180 	    else
1181 		emsg(_(e_invalblob));
1182 	    clear_tv(typ1);
1183 	    return FAIL;
1184 	}
1185 	else
1186 	{
1187 	    // Compare two Blobs for being equal or unequal.
1188 	    n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
1189 	    if (type == EXPR_NEQUAL)
1190 		n1 = !n1;
1191 	}
1192     }
1193     else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
1194     {
1195 	if (type_is)
1196 	{
1197 	    n1 = (typ1->v_type == typ2->v_type
1198 			    && typ1->vval.v_list == typ2->vval.v_list);
1199 	    if (type == EXPR_ISNOT)
1200 		n1 = !n1;
1201 	}
1202 	else if (typ1->v_type != typ2->v_type
1203 		|| (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1204 	{
1205 	    if (typ1->v_type != typ2->v_type)
1206 		emsg(_("E691: Can only compare List with List"));
1207 	    else
1208 		emsg(_("E692: Invalid operation for List"));
1209 	    clear_tv(typ1);
1210 	    return FAIL;
1211 	}
1212 	else
1213 	{
1214 	    // Compare two Lists for being equal or unequal.
1215 	    n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
1216 							    ic, FALSE);
1217 	    if (type == EXPR_NEQUAL)
1218 		n1 = !n1;
1219 	}
1220     }
1221 
1222     else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
1223     {
1224 	if (type_is)
1225 	{
1226 	    n1 = (typ1->v_type == typ2->v_type
1227 			    && typ1->vval.v_dict == typ2->vval.v_dict);
1228 	    if (type == EXPR_ISNOT)
1229 		n1 = !n1;
1230 	}
1231 	else if (typ1->v_type != typ2->v_type
1232 		|| (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1233 	{
1234 	    if (typ1->v_type != typ2->v_type)
1235 		emsg(_("E735: Can only compare Dictionary with Dictionary"));
1236 	    else
1237 		emsg(_("E736: Invalid operation for Dictionary"));
1238 	    clear_tv(typ1);
1239 	    return FAIL;
1240 	}
1241 	else
1242 	{
1243 	    // Compare two Dictionaries for being equal or unequal.
1244 	    n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
1245 							    ic, FALSE);
1246 	    if (type == EXPR_NEQUAL)
1247 		n1 = !n1;
1248 	}
1249     }
1250 
1251     else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
1252 	|| typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
1253     {
1254 	if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1255 		&& type != EXPR_IS && type != EXPR_ISNOT)
1256 	{
1257 	    emsg(_("E694: Invalid operation for Funcrefs"));
1258 	    clear_tv(typ1);
1259 	    return FAIL;
1260 	}
1261 	if ((typ1->v_type == VAR_PARTIAL
1262 					&& typ1->vval.v_partial == NULL)
1263 		|| (typ2->v_type == VAR_PARTIAL
1264 					&& typ2->vval.v_partial == NULL))
1265 	    // When both partials are NULL, then they are equal.
1266 	    // Otherwise they are not equal.
1267 	    n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
1268 	else if (type_is)
1269 	{
1270 	    if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
1271 		// strings are considered the same if their value is
1272 		// the same
1273 		n1 = tv_equal(typ1, typ2, ic, FALSE);
1274 	    else if (typ1->v_type == VAR_PARTIAL
1275 					&& typ2->v_type == VAR_PARTIAL)
1276 		n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
1277 	    else
1278 		n1 = FALSE;
1279 	}
1280 	else
1281 	    n1 = tv_equal(typ1, typ2, ic, FALSE);
1282 	if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1283 	    n1 = !n1;
1284     }
1285 
1286 #ifdef FEAT_FLOAT
1287     // If one of the two variables is a float, compare as a float.
1288     // When using "=~" or "!~", always compare as string.
1289     else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
1290 	    && type != EXPR_MATCH && type != EXPR_NOMATCH)
1291     {
1292 	float_T f1, f2;
1293 
1294 	f1 = tv_get_float(typ1);
1295 	f2 = tv_get_float(typ2);
1296 	n1 = FALSE;
1297 	switch (type)
1298 	{
1299 	    case EXPR_IS:
1300 	    case EXPR_EQUAL:    n1 = (f1 == f2); break;
1301 	    case EXPR_ISNOT:
1302 	    case EXPR_NEQUAL:   n1 = (f1 != f2); break;
1303 	    case EXPR_GREATER:  n1 = (f1 > f2); break;
1304 	    case EXPR_GEQUAL:   n1 = (f1 >= f2); break;
1305 	    case EXPR_SMALLER:  n1 = (f1 < f2); break;
1306 	    case EXPR_SEQUAL:   n1 = (f1 <= f2); break;
1307 	    case EXPR_UNKNOWN:
1308 	    case EXPR_MATCH:
1309 	    default:  break;  // avoid gcc warning
1310 	}
1311     }
1312 #endif
1313 
1314     // If one of the two variables is a number, compare as a number.
1315     // When using "=~" or "!~", always compare as string.
1316     else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
1317 	    && type != EXPR_MATCH && type != EXPR_NOMATCH)
1318     {
1319 	n1 = tv_get_number(typ1);
1320 	n2 = tv_get_number(typ2);
1321 	switch (type)
1322 	{
1323 	    case EXPR_IS:
1324 	    case EXPR_EQUAL:    n1 = (n1 == n2); break;
1325 	    case EXPR_ISNOT:
1326 	    case EXPR_NEQUAL:   n1 = (n1 != n2); break;
1327 	    case EXPR_GREATER:  n1 = (n1 > n2); break;
1328 	    case EXPR_GEQUAL:   n1 = (n1 >= n2); break;
1329 	    case EXPR_SMALLER:  n1 = (n1 < n2); break;
1330 	    case EXPR_SEQUAL:   n1 = (n1 <= n2); break;
1331 	    case EXPR_UNKNOWN:
1332 	    case EXPR_MATCH:
1333 	    default:  break;  // avoid gcc warning
1334 	}
1335     }
1336     else if (in_vim9script() && (typ1->v_type == VAR_BOOL
1337 				    || typ2->v_type == VAR_BOOL
1338 				    || (typ1->v_type == VAR_SPECIAL
1339 					      && typ2->v_type == VAR_SPECIAL)))
1340     {
1341 	if (typ1->v_type != typ2->v_type)
1342 	{
1343 	    semsg(_(e_cannot_compare_str_with_str),
1344 		       vartype_name(typ1->v_type), vartype_name(typ2->v_type));
1345 	    clear_tv(typ1);
1346 	    return FAIL;
1347 	}
1348 	n1 = typ1->vval.v_number;
1349 	n2 = typ2->vval.v_number;
1350 	switch (type)
1351 	{
1352 	    case EXPR_IS:
1353 	    case EXPR_EQUAL:    n1 = (n1 == n2); break;
1354 	    case EXPR_ISNOT:
1355 	    case EXPR_NEQUAL:   n1 = (n1 != n2); break;
1356 	    default:
1357 		semsg(_(e_invalid_operation_for_str),
1358 						   vartype_name(typ1->v_type));
1359 		clear_tv(typ1);
1360 		return FAIL;
1361 	}
1362     }
1363     else
1364     {
1365 	if (in_vim9script()
1366 	      && ((typ1->v_type != VAR_STRING && typ1->v_type != VAR_SPECIAL)
1367 	       || (typ2->v_type != VAR_STRING && typ2->v_type != VAR_SPECIAL)))
1368 	{
1369 	    semsg(_(e_cannot_compare_str_with_str),
1370 		       vartype_name(typ1->v_type), vartype_name(typ2->v_type));
1371 	    clear_tv(typ1);
1372 	    return FAIL;
1373 	}
1374 	s1 = tv_get_string_buf(typ1, buf1);
1375 	s2 = tv_get_string_buf(typ2, buf2);
1376 	if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1377 	    i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1378 	else
1379 	    i = 0;
1380 	n1 = FALSE;
1381 	switch (type)
1382 	{
1383 	    case EXPR_IS:
1384 	    case EXPR_EQUAL:    n1 = (i == 0); break;
1385 	    case EXPR_ISNOT:
1386 	    case EXPR_NEQUAL:   n1 = (i != 0); break;
1387 	    case EXPR_GREATER:  n1 = (i > 0); break;
1388 	    case EXPR_GEQUAL:   n1 = (i >= 0); break;
1389 	    case EXPR_SMALLER:  n1 = (i < 0); break;
1390 	    case EXPR_SEQUAL:   n1 = (i <= 0); break;
1391 
1392 	    case EXPR_MATCH:
1393 	    case EXPR_NOMATCH:
1394 		    n1 = pattern_match(s2, s1, ic);
1395 		    if (type == EXPR_NOMATCH)
1396 			n1 = !n1;
1397 		    break;
1398 
1399 	    default:  break;  // avoid gcc warning
1400 	}
1401     }
1402     clear_tv(typ1);
1403     if (in_vim9script())
1404     {
1405 	typ1->v_type = VAR_BOOL;
1406 	typ1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
1407     }
1408     else
1409     {
1410 	typ1->v_type = VAR_NUMBER;
1411 	typ1->vval.v_number = n1;
1412     }
1413 
1414     return OK;
1415 }
1416 
1417 /*
1418  * Convert any type to a string, never give an error.
1419  * When "quotes" is TRUE add quotes to a string.
1420  * Returns an allocated string.
1421  */
1422     char_u *
typval_tostring(typval_T * arg,int quotes)1423 typval_tostring(typval_T *arg, int quotes)
1424 {
1425     char_u	*tofree;
1426     char_u	numbuf[NUMBUFLEN];
1427     char_u	*ret = NULL;
1428 
1429     if (arg == NULL)
1430 	return vim_strsave((char_u *)"(does not exist)");
1431     if (!quotes && arg->v_type == VAR_STRING)
1432     {
1433 	ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1434 							 : arg->vval.v_string);
1435     }
1436     else
1437     {
1438 	ret = tv2string(arg, &tofree, numbuf, 0);
1439 	// Make a copy if we have a value but it's not in allocated memory.
1440 	if (ret != NULL && tofree == NULL)
1441 	    ret = vim_strsave(ret);
1442     }
1443     return ret;
1444 }
1445 
1446 /*
1447  * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1448  * or it refers to a List or Dictionary that is locked.
1449  */
1450     int
tv_islocked(typval_T * tv)1451 tv_islocked(typval_T *tv)
1452 {
1453     return (tv->v_lock & VAR_LOCKED)
1454 	|| (tv->v_type == VAR_LIST
1455 		&& tv->vval.v_list != NULL
1456 		&& (tv->vval.v_list->lv_lock & VAR_LOCKED))
1457 	|| (tv->v_type == VAR_DICT
1458 		&& tv->vval.v_dict != NULL
1459 		&& (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1460 }
1461 
1462     static int
func_equal(typval_T * tv1,typval_T * tv2,int ic)1463 func_equal(
1464     typval_T *tv1,
1465     typval_T *tv2,
1466     int	     ic)	    // ignore case
1467 {
1468     char_u	*s1, *s2;
1469     dict_T	*d1, *d2;
1470     int		a1, a2;
1471     int		i;
1472 
1473     // empty and NULL function name considered the same
1474     s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1475 					   : partial_name(tv1->vval.v_partial);
1476     if (s1 != NULL && *s1 == NUL)
1477 	s1 = NULL;
1478     s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1479 					   : partial_name(tv2->vval.v_partial);
1480     if (s2 != NULL && *s2 == NUL)
1481 	s2 = NULL;
1482     if (s1 == NULL || s2 == NULL)
1483     {
1484 	if (s1 != s2)
1485 	    return FALSE;
1486     }
1487     else if (STRCMP(s1, s2) != 0)
1488 	return FALSE;
1489 
1490     // empty dict and NULL dict is different
1491     d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1492     d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1493     if (d1 == NULL || d2 == NULL)
1494     {
1495 	if (d1 != d2)
1496 	    return FALSE;
1497     }
1498     else if (!dict_equal(d1, d2, ic, TRUE))
1499 	return FALSE;
1500 
1501     // empty list and no list considered the same
1502     a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1503     a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1504     if (a1 != a2)
1505 	return FALSE;
1506     for (i = 0; i < a1; ++i)
1507 	if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1508 		      tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1509 	    return FALSE;
1510 
1511     return TRUE;
1512 }
1513 
1514 /*
1515  * Return TRUE if "tv1" and "tv2" have the same value.
1516  * Compares the items just like "==" would compare them, but strings and
1517  * numbers are different.  Floats and numbers are also different.
1518  */
1519     int
tv_equal(typval_T * tv1,typval_T * tv2,int ic,int recursive)1520 tv_equal(
1521     typval_T *tv1,
1522     typval_T *tv2,
1523     int	     ic,	    // ignore case
1524     int	     recursive)	    // TRUE when used recursively
1525 {
1526     char_u	buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1527     char_u	*s1, *s2;
1528     static int  recursive_cnt = 0;	    // catch recursive loops
1529     int		r;
1530     static int	tv_equal_recurse_limit;
1531 
1532     // Catch lists and dicts that have an endless loop by limiting
1533     // recursiveness to a limit.  We guess they are equal then.
1534     // A fixed limit has the problem of still taking an awful long time.
1535     // Reduce the limit every time running into it. That should work fine for
1536     // deeply linked structures that are not recursively linked and catch
1537     // recursiveness quickly.
1538     if (!recursive)
1539 	tv_equal_recurse_limit = 1000;
1540     if (recursive_cnt >= tv_equal_recurse_limit)
1541     {
1542 	--tv_equal_recurse_limit;
1543 	return TRUE;
1544     }
1545 
1546     // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1547     // arguments.
1548     if ((tv1->v_type == VAR_FUNC
1549 		|| (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1550 	    && (tv2->v_type == VAR_FUNC
1551 		|| (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1552     {
1553 	++recursive_cnt;
1554 	r = func_equal(tv1, tv2, ic);
1555 	--recursive_cnt;
1556 	return r;
1557     }
1558 
1559     if (tv1->v_type != tv2->v_type
1560 	    && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1561 		|| (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
1562 	return FALSE;
1563 
1564     switch (tv1->v_type)
1565     {
1566 	case VAR_LIST:
1567 	    ++recursive_cnt;
1568 	    r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1569 	    --recursive_cnt;
1570 	    return r;
1571 
1572 	case VAR_DICT:
1573 	    ++recursive_cnt;
1574 	    r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1575 	    --recursive_cnt;
1576 	    return r;
1577 
1578 	case VAR_BLOB:
1579 	    return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1580 
1581 	case VAR_NUMBER:
1582 	case VAR_BOOL:
1583 	case VAR_SPECIAL:
1584 	    return tv1->vval.v_number == tv2->vval.v_number;
1585 
1586 	case VAR_STRING:
1587 	    s1 = tv_get_string_buf(tv1, buf1);
1588 	    s2 = tv_get_string_buf(tv2, buf2);
1589 	    return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1590 
1591 	case VAR_FLOAT:
1592 #ifdef FEAT_FLOAT
1593 	    return tv1->vval.v_float == tv2->vval.v_float;
1594 #endif
1595 	case VAR_JOB:
1596 #ifdef FEAT_JOB_CHANNEL
1597 	    return tv1->vval.v_job == tv2->vval.v_job;
1598 #endif
1599 	case VAR_CHANNEL:
1600 #ifdef FEAT_JOB_CHANNEL
1601 	    return tv1->vval.v_channel == tv2->vval.v_channel;
1602 #endif
1603 	case VAR_INSTR:
1604 	    return tv1->vval.v_instr == tv2->vval.v_instr;
1605 
1606 	case VAR_PARTIAL:
1607 	    return tv1->vval.v_partial == tv2->vval.v_partial;
1608 
1609 	case VAR_FUNC:
1610 	    return tv1->vval.v_string == tv2->vval.v_string;
1611 
1612 	case VAR_UNKNOWN:
1613 	case VAR_ANY:
1614 	case VAR_VOID:
1615 	    break;
1616     }
1617 
1618     // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1619     // does not equal anything, not even itself.
1620     return FALSE;
1621 }
1622 
1623 /*
1624  * Get an option value.
1625  * "arg" points to the '&' or '+' before the option name.
1626  * "arg" is advanced to character after the option name.
1627  * Return OK or FAIL.
1628  */
1629     int
eval_option(char_u ** arg,typval_T * rettv,int evaluate)1630 eval_option(
1631     char_u	**arg,
1632     typval_T	*rettv,	// when NULL, only check if option exists
1633     int		evaluate)
1634 {
1635     char_u	*option_end;
1636     long	numval;
1637     char_u	*stringval;
1638     getoption_T	opt_type;
1639     int		c;
1640     int		working = (**arg == '+');    // has("+option")
1641     int		ret = OK;
1642     int		opt_flags;
1643 
1644     // Isolate the option name and find its value.
1645     option_end = find_option_end(arg, &opt_flags);
1646     if (option_end == NULL)
1647     {
1648 	if (rettv != NULL)
1649 	    semsg(_("E112: Option name missing: %s"), *arg);
1650 	return FAIL;
1651     }
1652 
1653     if (!evaluate)
1654     {
1655 	*arg = option_end;
1656 	return OK;
1657     }
1658 
1659     c = *option_end;
1660     *option_end = NUL;
1661     opt_type = get_option_value(*arg, &numval,
1662 			       rettv == NULL ? NULL : &stringval, opt_flags);
1663 
1664     if (opt_type == gov_unknown)
1665     {
1666 	if (rettv != NULL)
1667 	    semsg(_(e_unknown_option), *arg);
1668 	ret = FAIL;
1669     }
1670     else if (rettv != NULL)
1671     {
1672 	rettv->v_lock = 0;
1673 	if (opt_type == gov_hidden_string)
1674 	{
1675 	    rettv->v_type = VAR_STRING;
1676 	    rettv->vval.v_string = NULL;
1677 	}
1678 	else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
1679 	{
1680 	    rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1681 						       ? VAR_BOOL : VAR_NUMBER;
1682 	    rettv->vval.v_number = 0;
1683 	}
1684 	else if (opt_type == gov_bool || opt_type == gov_number)
1685 	{
1686 	    if (in_vim9script() && opt_type == gov_bool)
1687 	    {
1688 		rettv->v_type = VAR_BOOL;
1689 		rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1690 	    }
1691 	    else
1692 	    {
1693 		rettv->v_type = VAR_NUMBER;
1694 		rettv->vval.v_number = numval;
1695 	    }
1696 	}
1697 	else				// string option
1698 	{
1699 	    rettv->v_type = VAR_STRING;
1700 	    rettv->vval.v_string = stringval;
1701 	}
1702     }
1703     else if (working && (opt_type == gov_hidden_bool
1704 			|| opt_type == gov_hidden_number
1705 			|| opt_type == gov_hidden_string))
1706 	ret = FAIL;
1707 
1708     *option_end = c;		    // put back for error messages
1709     *arg = option_end;
1710 
1711     return ret;
1712 }
1713 
1714 /*
1715  * Allocate a variable for a number constant.  Also deals with "0z" for blob.
1716  * Return OK or FAIL.
1717  */
1718     int
eval_number(char_u ** arg,typval_T * rettv,int evaluate,int want_string UNUSED)1719 eval_number(
1720 	char_u	    **arg,
1721 	typval_T    *rettv,
1722 	int	    evaluate,
1723 	int	    want_string UNUSED)
1724 {
1725     int		len;
1726     int		skip_quotes = !in_old_script(4);
1727 #ifdef FEAT_FLOAT
1728     char_u	*p;
1729     int		get_float = FALSE;
1730 
1731     // We accept a float when the format matches
1732     // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?".  This is very
1733     // strict to avoid backwards compatibility problems.
1734     // With script version 2 and later the leading digit can be
1735     // omitted.
1736     // Don't look for a float after the "." operator, so that
1737     // ":let vers = 1.2.3" doesn't fail.
1738     if (**arg == '.')
1739 	p = *arg;
1740     else
1741     {
1742 	p = *arg + 1;
1743 	if (skip_quotes)
1744 	    for (;;)
1745 	    {
1746 		if (*p == '\'')
1747 		    ++p;
1748 		if (!vim_isdigit(*p))
1749 		    break;
1750 		p = skipdigits(p);
1751 	    }
1752 	else
1753 	    p = skipdigits(p);
1754     }
1755     if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
1756     {
1757 	get_float = TRUE;
1758 	p = skipdigits(p + 2);
1759 	if (*p == 'e' || *p == 'E')
1760 	{
1761 	    ++p;
1762 	    if (*p == '-' || *p == '+')
1763 		++p;
1764 	    if (!vim_isdigit(*p))
1765 		get_float = FALSE;
1766 	    else
1767 		p = skipdigits(p + 1);
1768 	}
1769 	if (ASCII_ISALPHA(*p) || *p == '.')
1770 	    get_float = FALSE;
1771     }
1772     if (get_float)
1773     {
1774 	float_T	f;
1775 
1776 	*arg += string2float(*arg, &f, skip_quotes);
1777 	if (evaluate)
1778 	{
1779 	    rettv->v_type = VAR_FLOAT;
1780 	    rettv->vval.v_float = f;
1781 	}
1782     }
1783     else
1784 #endif
1785     if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
1786     {
1787 	char_u  *bp;
1788 	blob_T  *blob = NULL;  // init for gcc
1789 
1790 	// Blob constant: 0z0123456789abcdef
1791 	if (evaluate)
1792 	    blob = blob_alloc();
1793 	for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
1794 	{
1795 	    if (!vim_isxdigit(bp[1]))
1796 	    {
1797 		if (blob != NULL)
1798 		{
1799 		    emsg(_("E973: Blob literal should have an even number of hex characters"));
1800 		    ga_clear(&blob->bv_ga);
1801 		    VIM_CLEAR(blob);
1802 		}
1803 		return FAIL;
1804 	    }
1805 	    if (blob != NULL)
1806 		ga_append(&blob->bv_ga,
1807 			     (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
1808 	    if (bp[2] == '.' && vim_isxdigit(bp[3]))
1809 		++bp;
1810 	}
1811 	if (blob != NULL)
1812 	    rettv_blob_set(rettv, blob);
1813 	*arg = bp;
1814     }
1815     else
1816     {
1817 	varnumber_T	n;
1818 
1819 	// decimal, hex or octal number
1820 	vim_str2nr(*arg, NULL, &len, skip_quotes
1821 		      ? STR2NR_NO_OCT + STR2NR_QUOTE
1822 		      : STR2NR_ALL, &n, NULL, 0, TRUE);
1823 	if (len == 0)
1824 	{
1825 	    if (evaluate)
1826 		semsg(_(e_invalid_expression_str), *arg);
1827 	    return FAIL;
1828 	}
1829 	*arg += len;
1830 	if (evaluate)
1831 	{
1832 	    rettv->v_type = VAR_NUMBER;
1833 	    rettv->vval.v_number = n;
1834 	}
1835     }
1836     return OK;
1837 }
1838 
1839 /*
1840  * Allocate a variable for a string constant.
1841  * Return OK or FAIL.
1842  */
1843     int
eval_string(char_u ** arg,typval_T * rettv,int evaluate)1844 eval_string(char_u **arg, typval_T *rettv, int evaluate)
1845 {
1846     char_u	*p;
1847     char_u	*end;
1848     int		extra = 0;
1849     int		len;
1850 
1851     // Find the end of the string, skipping backslashed characters.
1852     for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
1853     {
1854 	if (*p == '\\' && p[1] != NUL)
1855 	{
1856 	    ++p;
1857 	    // A "\<x>" form occupies at least 4 characters, and produces up
1858 	    // to 21 characters (3 * 6 for the char and 3 for a modifier):
1859 	    // reserve space for 18 extra.
1860 	    // Each byte in the char could be encoded as K_SPECIAL K_EXTRA x.
1861 	    if (*p == '<')
1862 		extra += 18;
1863 	}
1864     }
1865 
1866     if (*p != '"')
1867     {
1868 	semsg(_("E114: Missing quote: %s"), *arg);
1869 	return FAIL;
1870     }
1871 
1872     // If only parsing, set *arg and return here
1873     if (!evaluate)
1874     {
1875 	*arg = p + 1;
1876 	return OK;
1877     }
1878 
1879     // Copy the string into allocated memory, handling backslashed
1880     // characters.
1881     rettv->v_type = VAR_STRING;
1882     len = (int)(p - *arg + extra);
1883     rettv->vval.v_string = alloc(len);
1884     if (rettv->vval.v_string == NULL)
1885 	return FAIL;
1886     end = rettv->vval.v_string;
1887 
1888     for (p = *arg + 1; *p != NUL && *p != '"'; )
1889     {
1890 	if (*p == '\\')
1891 	{
1892 	    switch (*++p)
1893 	    {
1894 		case 'b': *end++ = BS; ++p; break;
1895 		case 'e': *end++ = ESC; ++p; break;
1896 		case 'f': *end++ = FF; ++p; break;
1897 		case 'n': *end++ = NL; ++p; break;
1898 		case 'r': *end++ = CAR; ++p; break;
1899 		case 't': *end++ = TAB; ++p; break;
1900 
1901 		case 'X': // hex: "\x1", "\x12"
1902 		case 'x':
1903 		case 'u': // Unicode: "\u0023"
1904 		case 'U':
1905 			  if (vim_isxdigit(p[1]))
1906 			  {
1907 			      int	n, nr;
1908 			      int	c = toupper(*p);
1909 
1910 			      if (c == 'X')
1911 				  n = 2;
1912 			      else if (*p == 'u')
1913 				  n = 4;
1914 			      else
1915 				  n = 8;
1916 			      nr = 0;
1917 			      while (--n >= 0 && vim_isxdigit(p[1]))
1918 			      {
1919 				  ++p;
1920 				  nr = (nr << 4) + hex2nr(*p);
1921 			      }
1922 			      ++p;
1923 			      // For "\u" store the number according to
1924 			      // 'encoding'.
1925 			      if (c != 'X')
1926 				  end += (*mb_char2bytes)(nr, end);
1927 			      else
1928 				  *end++ = nr;
1929 			  }
1930 			  break;
1931 
1932 			  // octal: "\1", "\12", "\123"
1933 		case '0':
1934 		case '1':
1935 		case '2':
1936 		case '3':
1937 		case '4':
1938 		case '5':
1939 		case '6':
1940 		case '7': *end = *p++ - '0';
1941 			  if (*p >= '0' && *p <= '7')
1942 			  {
1943 			      *end = (*end << 3) + *p++ - '0';
1944 			      if (*p >= '0' && *p <= '7')
1945 				  *end = (*end << 3) + *p++ - '0';
1946 			  }
1947 			  ++end;
1948 			  break;
1949 
1950 			  // Special key, e.g.: "\<C-W>"
1951 		case '<':
1952 			  {
1953 			      int flags = FSK_KEYCODE | FSK_IN_STRING;
1954 
1955 			      if (p[1] != '*')
1956 				  flags |= FSK_SIMPLIFY;
1957 			      extra = trans_special(&p, end, flags, NULL);
1958 			      if (extra != 0)
1959 			      {
1960 				  end += extra;
1961 				  if (end >= rettv->vval.v_string + len)
1962 				      iemsg("eval_string() used more space than allocated");
1963 				  break;
1964 			      }
1965 			  }
1966 			  // FALLTHROUGH
1967 
1968 		default:  MB_COPY_CHAR(p, end);
1969 			  break;
1970 	    }
1971 	}
1972 	else
1973 	    MB_COPY_CHAR(p, end);
1974     }
1975     *end = NUL;
1976     if (*p != NUL) // just in case
1977 	++p;
1978     *arg = p;
1979 
1980     return OK;
1981 }
1982 
1983 /*
1984  * Allocate a variable for a 'str''ing' constant.
1985  * Return OK or FAIL.
1986  */
1987     int
eval_lit_string(char_u ** arg,typval_T * rettv,int evaluate)1988 eval_lit_string(char_u **arg, typval_T *rettv, int evaluate)
1989 {
1990     char_u	*p;
1991     char_u	*str;
1992     int		reduce = 0;
1993 
1994     // Find the end of the string, skipping ''.
1995     for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
1996     {
1997 	if (*p == '\'')
1998 	{
1999 	    if (p[1] != '\'')
2000 		break;
2001 	    ++reduce;
2002 	    ++p;
2003 	}
2004     }
2005 
2006     if (*p != '\'')
2007     {
2008 	semsg(_("E115: Missing quote: %s"), *arg);
2009 	return FAIL;
2010     }
2011 
2012     // If only parsing return after setting "*arg"
2013     if (!evaluate)
2014     {
2015 	*arg = p + 1;
2016 	return OK;
2017     }
2018 
2019     // Copy the string into allocated memory, handling '' to ' reduction.
2020     str = alloc((p - *arg) - reduce);
2021     if (str == NULL)
2022 	return FAIL;
2023     rettv->v_type = VAR_STRING;
2024     rettv->vval.v_string = str;
2025 
2026     for (p = *arg + 1; *p != NUL; )
2027     {
2028 	if (*p == '\'')
2029 	{
2030 	    if (p[1] != '\'')
2031 		break;
2032 	    ++p;
2033 	}
2034 	MB_COPY_CHAR(p, str);
2035     }
2036     *str = NUL;
2037     *arg = p + 1;
2038 
2039     return OK;
2040 }
2041 
2042 /*
2043  * Return a string with the string representation of a variable.
2044  * If the memory is allocated "tofree" is set to it, otherwise NULL.
2045  * "numbuf" is used for a number.
2046  * Puts quotes around strings, so that they can be parsed back by eval().
2047  * May return NULL.
2048  */
2049     char_u *
tv2string(typval_T * tv,char_u ** tofree,char_u * numbuf,int copyID)2050 tv2string(
2051     typval_T	*tv,
2052     char_u	**tofree,
2053     char_u	*numbuf,
2054     int		copyID)
2055 {
2056     return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2057 }
2058 
2059 /*
2060  * Get the value of an environment variable.
2061  * "arg" is pointing to the '$'.  It is advanced to after the name.
2062  * If the environment variable was not set, silently assume it is empty.
2063  * Return FAIL if the name is invalid.
2064  */
2065     int
eval_env_var(char_u ** arg,typval_T * rettv,int evaluate)2066 eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
2067 {
2068     char_u	*string = NULL;
2069     int		len;
2070     int		cc;
2071     char_u	*name;
2072     int		mustfree = FALSE;
2073 
2074     ++*arg;
2075     name = *arg;
2076     len = get_env_len(arg);
2077     if (evaluate)
2078     {
2079 	if (len == 0)
2080 	    return FAIL; // invalid empty name
2081 
2082 	cc = name[len];
2083 	name[len] = NUL;
2084 	// first try vim_getenv(), fast for normal environment vars
2085 	string = vim_getenv(name, &mustfree);
2086 	if (string != NULL && *string != NUL)
2087 	{
2088 	    if (!mustfree)
2089 		string = vim_strsave(string);
2090 	}
2091 	else
2092 	{
2093 	    if (mustfree)
2094 		vim_free(string);
2095 
2096 	    // next try expanding things like $VIM and ${HOME}
2097 	    string = expand_env_save(name - 1);
2098 	    if (string != NULL && *string == '$')
2099 		VIM_CLEAR(string);
2100 	}
2101 	name[len] = cc;
2102 
2103 	rettv->v_type = VAR_STRING;
2104 	rettv->vval.v_string = string;
2105 	rettv->v_lock = 0;
2106     }
2107 
2108     return OK;
2109 }
2110 
2111 /*
2112  * Get the lnum from the first argument.
2113  * Also accepts ".", "$", etc., but that only works for the current buffer.
2114  * Returns -1 on error.
2115  */
2116     linenr_T
tv_get_lnum(typval_T * argvars)2117 tv_get_lnum(typval_T *argvars)
2118 {
2119     linenr_T	lnum = -1;
2120 
2121     if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2122 	lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2123     if (lnum <= 0 && argvars[0].v_type != VAR_NUMBER)
2124     {
2125 	int	fnum;
2126 	pos_T	*fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
2127 
2128 	// no valid number, try using arg like line()
2129 	if (fp != NULL)
2130 	    lnum = fp->lnum;
2131     }
2132     return lnum;
2133 }
2134 
2135 /*
2136  * Get the lnum from the first argument.
2137  * Also accepts "$", then "buf" is used.
2138  * Returns 0 on error.
2139  */
2140     linenr_T
tv_get_lnum_buf(typval_T * argvars,buf_T * buf)2141 tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2142 {
2143     if (argvars[0].v_type == VAR_STRING
2144 	    && argvars[0].vval.v_string != NULL
2145 	    && argvars[0].vval.v_string[0] == '$'
2146 	    && buf != NULL)
2147 	return buf->b_ml.ml_line_count;
2148     return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2149 }
2150 
2151 /*
2152  * Get buffer by number or pattern.
2153  */
2154     buf_T *
tv_get_buf(typval_T * tv,int curtab_only)2155 tv_get_buf(typval_T *tv, int curtab_only)
2156 {
2157     char_u	*name = tv->vval.v_string;
2158     buf_T	*buf;
2159 
2160     if (tv->v_type == VAR_NUMBER)
2161 	return buflist_findnr((int)tv->vval.v_number);
2162     if (tv->v_type != VAR_STRING)
2163 	return NULL;
2164     if (name == NULL || *name == NUL)
2165 	return curbuf;
2166     if (name[0] == '$' && name[1] == NUL)
2167 	return lastbuf;
2168 
2169     buf = buflist_find_by_name(name, curtab_only);
2170 
2171     // If not found, try expanding the name, like done for bufexists().
2172     if (buf == NULL)
2173 	buf = find_buffer(tv);
2174 
2175     return buf;
2176 }
2177 
2178 /*
2179  * Like tv_get_buf() but give an error message is the type is wrong.
2180  */
2181     buf_T *
tv_get_buf_from_arg(typval_T * tv)2182 tv_get_buf_from_arg(typval_T *tv)
2183 {
2184     buf_T *buf;
2185 
2186     ++emsg_off;
2187     buf = tv_get_buf(tv, FALSE);
2188     --emsg_off;
2189     if (buf == NULL
2190 	    && tv->v_type != VAR_NUMBER
2191 	    && tv->v_type != VAR_STRING)
2192 	// issue errmsg for type error
2193 	(void)tv_get_number(tv);
2194     return buf;
2195 }
2196 
2197 #endif // FEAT_EVAL
2198