blob: 03891c806dec356bd4454bc5b4ceb9a03aef438d [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 Lakshmanana9a7c0c2021-07-17 19:11:07 +0200388 * Give an error and return FAIL unless "args[idx]" is a number.
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200389 */
390 int
391check_for_number_arg(typval_T *args, int idx)
392{
393 if (args[idx].v_type != VAR_NUMBER)
394 {
395 if (idx >= 0)
396 semsg(_(e_number_required_for_argument_nr), idx + 1);
397 else
398 emsg(_(e_numberreq));
399 return FAIL;
400 }
401 return OK;
402}
403
404/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200405 * Give an error and return FAIL unless "args[idx]" is a bool.
406 */
407 int
408check_for_bool_arg(typval_T *args, int idx)
409{
410 if (args[idx].v_type != VAR_BOOL
411 && !(args[idx].v_type == VAR_NUMBER
412 && (args[idx].vval.v_number == 0
413 || args[idx].vval.v_number == 1)))
414 {
415 if (idx >= 0)
416 semsg(_(e_bool_required_for_argument_nr), idx + 1);
417 else
418 emsg(_(e_boolreq));
419 return FAIL;
420 }
421 return OK;
422}
423
424/*
425 * Give an error and return FAIL unless "args[idx]" is a list.
426 */
427 int
428check_for_list_arg(typval_T *args, int idx)
429{
430 if (args[idx].v_type != VAR_LIST)
431 {
432 if (idx >= 0)
433 semsg(_(e_list_required_for_argument_nr), idx + 1);
434 else
435 emsg(_(e_listreq));
436 return FAIL;
437 }
438 return OK;
439}
440
441/*
442 * Give an error and return FAIL unless "args[idx]" is a dict.
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200443 */
444 int
445check_for_dict_arg(typval_T *args, int idx)
446{
447 if (args[idx].v_type != VAR_DICT)
448 {
449 if (idx >= 0)
450 semsg(_(e_dict_required_for_argument_nr), idx + 1);
451 else
452 emsg(_(e_dictreq));
453 return FAIL;
454 }
455 return OK;
456}
457
458/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200459 * Get the string value of a variable.
460 * If it is a Number variable, the number is converted into a string.
461 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
462 * tv_get_string_buf() uses a given buffer.
463 * If the String variable has never been set, return an empty string.
464 * Never returns NULL;
465 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
466 * NULL on error.
467 */
468 char_u *
469tv_get_string(typval_T *varp)
470{
471 static char_u mybuf[NUMBUFLEN];
472
473 return tv_get_string_buf(varp, mybuf);
474}
475
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100476/*
477 * Like tv_get_string() but don't allow number to string conversion for Vim9.
478 */
479 char_u *
480tv_get_string_strict(typval_T *varp)
481{
482 static char_u mybuf[NUMBUFLEN];
483 char_u *res = tv_get_string_buf_chk_strict(
484 varp, mybuf, in_vim9script());
485
486 return res != NULL ? res : (char_u *)"";
487}
488
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200489 char_u *
490tv_get_string_buf(typval_T *varp, char_u *buf)
491{
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200492 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200493
494 return res != NULL ? res : (char_u *)"";
495}
496
497/*
498 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
499 */
500 char_u *
501tv_get_string_chk(typval_T *varp)
502{
503 static char_u mybuf[NUMBUFLEN];
504
505 return tv_get_string_buf_chk(varp, mybuf);
506}
507
508 char_u *
509tv_get_string_buf_chk(typval_T *varp, char_u *buf)
510{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100511 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
512}
513
514 char_u *
515tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
516{
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200517 switch (varp->v_type)
518 {
519 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100520 if (strict)
521 {
522 emsg(_(e_using_number_as_string));
523 break;
524 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200525 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
526 (varnumber_T)varp->vval.v_number);
527 return buf;
528 case VAR_FUNC:
529 case VAR_PARTIAL:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200530 emsg(_("E729: Using a Funcref as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200531 break;
532 case VAR_LIST:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200533 emsg(_("E730: Using a List as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200534 break;
535 case VAR_DICT:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200536 emsg(_("E731: Using a Dictionary as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200537 break;
538 case VAR_FLOAT:
539#ifdef FEAT_FLOAT
Bram Moolenaar7a2217b2021-06-06 12:33:49 +0200540 if (strict)
541 {
542 emsg(_(e_float_as_string));
543 break;
544 }
545 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
546 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200547#endif
548 case VAR_STRING:
549 if (varp->vval.v_string != NULL)
550 return varp->vval.v_string;
551 return (char_u *)"";
552 case VAR_BOOL:
553 case VAR_SPECIAL:
554 STRCPY(buf, get_var_special_name(varp->vval.v_number));
555 return buf;
556 case VAR_BLOB:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200557 emsg(_("E976: Using a Blob as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200558 break;
559 case VAR_JOB:
560#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200561 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200562 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200563 semsg(_(e_using_invalid_value_as_string_str), "job");
564 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200565 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200566 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200567#endif
568 break;
569 case VAR_CHANNEL:
570#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200571 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200572 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200573 semsg(_(e_using_invalid_value_as_string_str), "channel");
574 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200575 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200576 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200577#endif
578 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200579 case VAR_VOID:
580 emsg(_(e_cannot_use_void_value));
581 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200582 case VAR_UNKNOWN:
583 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200584 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +0200585 semsg(_(e_using_invalid_value_as_string_str),
586 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200587 break;
588 }
589 return NULL;
590}
591
592/*
593 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
594 * string() on Dict, List, etc.
595 */
596 char_u *
597tv_stringify(typval_T *varp, char_u *buf)
598{
599 if (varp->v_type == VAR_LIST
600 || varp->v_type == VAR_DICT
601 || varp->v_type == VAR_BLOB
602 || varp->v_type == VAR_FUNC
603 || varp->v_type == VAR_PARTIAL
604 || varp->v_type == VAR_FLOAT)
605 {
606 typval_T tmp;
607
608 f_string(varp, &tmp);
609 tv_get_string_buf(&tmp, buf);
610 clear_tv(varp);
611 *varp = tmp;
612 return tmp.vval.v_string;
613 }
614 return tv_get_string_buf(varp, buf);
615}
616
617/*
618 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
619 * Also give an error message, using "name" or _("name") when use_gettext is
620 * TRUE.
621 */
622 int
623tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
624{
625 int lock = 0;
626
627 switch (tv->v_type)
628 {
629 case VAR_BLOB:
630 if (tv->vval.v_blob != NULL)
631 lock = tv->vval.v_blob->bv_lock;
632 break;
633 case VAR_LIST:
634 if (tv->vval.v_list != NULL)
635 lock = tv->vval.v_list->lv_lock;
636 break;
637 case VAR_DICT:
638 if (tv->vval.v_dict != NULL)
639 lock = tv->vval.v_dict->dv_lock;
640 break;
641 default:
642 break;
643 }
Bram Moolenaara187c432020-09-16 21:08:28 +0200644 return value_check_lock(tv->v_lock, name, use_gettext)
645 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200646}
647
648/*
649 * Copy the values from typval_T "from" to typval_T "to".
650 * When needed allocates string or increases reference count.
651 * Does not make a copy of a list, blob or dict but copies the reference!
652 * It is OK for "from" and "to" to point to the same item. This is used to
653 * make a copy later.
654 */
655 void
656copy_tv(typval_T *from, typval_T *to)
657{
658 to->v_type = from->v_type;
659 to->v_lock = 0;
660 switch (from->v_type)
661 {
662 case VAR_NUMBER:
663 case VAR_BOOL:
664 case VAR_SPECIAL:
665 to->vval.v_number = from->vval.v_number;
666 break;
667 case VAR_FLOAT:
668#ifdef FEAT_FLOAT
669 to->vval.v_float = from->vval.v_float;
670 break;
671#endif
672 case VAR_JOB:
673#ifdef FEAT_JOB_CHANNEL
674 to->vval.v_job = from->vval.v_job;
675 if (to->vval.v_job != NULL)
676 ++to->vval.v_job->jv_refcount;
677 break;
678#endif
679 case VAR_CHANNEL:
680#ifdef FEAT_JOB_CHANNEL
681 to->vval.v_channel = from->vval.v_channel;
682 if (to->vval.v_channel != NULL)
683 ++to->vval.v_channel->ch_refcount;
684 break;
685#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200686 case VAR_INSTR:
687 to->vval.v_instr = from->vval.v_instr;
688 break;
689
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200690 case VAR_STRING:
691 case VAR_FUNC:
692 if (from->vval.v_string == NULL)
693 to->vval.v_string = NULL;
694 else
695 {
696 to->vval.v_string = vim_strsave(from->vval.v_string);
697 if (from->v_type == VAR_FUNC)
698 func_ref(to->vval.v_string);
699 }
700 break;
701 case VAR_PARTIAL:
702 if (from->vval.v_partial == NULL)
703 to->vval.v_partial = NULL;
704 else
705 {
706 to->vval.v_partial = from->vval.v_partial;
707 ++to->vval.v_partial->pt_refcount;
708 }
709 break;
710 case VAR_BLOB:
711 if (from->vval.v_blob == NULL)
712 to->vval.v_blob = NULL;
713 else
714 {
715 to->vval.v_blob = from->vval.v_blob;
716 ++to->vval.v_blob->bv_refcount;
717 }
718 break;
719 case VAR_LIST:
720 if (from->vval.v_list == NULL)
721 to->vval.v_list = NULL;
722 else
723 {
724 to->vval.v_list = from->vval.v_list;
725 ++to->vval.v_list->lv_refcount;
726 }
727 break;
728 case VAR_DICT:
729 if (from->vval.v_dict == NULL)
730 to->vval.v_dict = NULL;
731 else
732 {
733 to->vval.v_dict = from->vval.v_dict;
734 ++to->vval.v_dict->dv_refcount;
735 }
736 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +0200737 case VAR_VOID:
738 emsg(_(e_cannot_use_void_value));
739 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200740 case VAR_UNKNOWN:
741 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200742 internal_error_no_abort("copy_tv(UNKNOWN)");
743 break;
744 }
745}
746
747/*
748 * Compare "typ1" and "typ2". Put the result in "typ1".
749 */
750 int
751typval_compare(
752 typval_T *typ1, // first operand
753 typval_T *typ2, // second operand
Bram Moolenaar657137c2021-01-09 15:45:23 +0100754 exprtype_T type, // operator
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200755 int ic) // ignore case
756{
757 int i;
758 varnumber_T n1, n2;
759 char_u *s1, *s2;
760 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
761 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
762
763 if (type_is && typ1->v_type != typ2->v_type)
764 {
765 // For "is" a different type always means FALSE, for "notis"
766 // it means TRUE.
767 n1 = (type == EXPR_ISNOT);
768 }
769 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
770 {
771 if (type_is)
772 {
773 n1 = (typ1->v_type == typ2->v_type
774 && typ1->vval.v_blob == typ2->vval.v_blob);
775 if (type == EXPR_ISNOT)
776 n1 = !n1;
777 }
778 else if (typ1->v_type != typ2->v_type
779 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
780 {
781 if (typ1->v_type != typ2->v_type)
782 emsg(_("E977: Can only compare Blob with Blob"));
783 else
784 emsg(_(e_invalblob));
785 clear_tv(typ1);
786 return FAIL;
787 }
788 else
789 {
790 // Compare two Blobs for being equal or unequal.
791 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
792 if (type == EXPR_NEQUAL)
793 n1 = !n1;
794 }
795 }
796 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
797 {
798 if (type_is)
799 {
800 n1 = (typ1->v_type == typ2->v_type
801 && typ1->vval.v_list == typ2->vval.v_list);
802 if (type == EXPR_ISNOT)
803 n1 = !n1;
804 }
805 else if (typ1->v_type != typ2->v_type
806 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
807 {
808 if (typ1->v_type != typ2->v_type)
809 emsg(_("E691: Can only compare List with List"));
810 else
811 emsg(_("E692: Invalid operation for List"));
812 clear_tv(typ1);
813 return FAIL;
814 }
815 else
816 {
817 // Compare two Lists for being equal or unequal.
818 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
819 ic, FALSE);
820 if (type == EXPR_NEQUAL)
821 n1 = !n1;
822 }
823 }
824
825 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
826 {
827 if (type_is)
828 {
829 n1 = (typ1->v_type == typ2->v_type
830 && typ1->vval.v_dict == typ2->vval.v_dict);
831 if (type == EXPR_ISNOT)
832 n1 = !n1;
833 }
834 else if (typ1->v_type != typ2->v_type
835 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
836 {
837 if (typ1->v_type != typ2->v_type)
838 emsg(_("E735: Can only compare Dictionary with Dictionary"));
839 else
840 emsg(_("E736: Invalid operation for Dictionary"));
841 clear_tv(typ1);
842 return FAIL;
843 }
844 else
845 {
846 // Compare two Dictionaries for being equal or unequal.
847 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
848 ic, FALSE);
849 if (type == EXPR_NEQUAL)
850 n1 = !n1;
851 }
852 }
853
854 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
855 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
856 {
857 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
858 && type != EXPR_IS && type != EXPR_ISNOT)
859 {
860 emsg(_("E694: Invalid operation for Funcrefs"));
861 clear_tv(typ1);
862 return FAIL;
863 }
864 if ((typ1->v_type == VAR_PARTIAL
865 && typ1->vval.v_partial == NULL)
866 || (typ2->v_type == VAR_PARTIAL
867 && typ2->vval.v_partial == NULL))
868 // When both partials are NULL, then they are equal.
869 // Otherwise they are not equal.
870 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
871 else if (type_is)
872 {
873 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
874 // strings are considered the same if their value is
875 // the same
876 n1 = tv_equal(typ1, typ2, ic, FALSE);
877 else if (typ1->v_type == VAR_PARTIAL
878 && typ2->v_type == VAR_PARTIAL)
879 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
880 else
881 n1 = FALSE;
882 }
883 else
884 n1 = tv_equal(typ1, typ2, ic, FALSE);
885 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
886 n1 = !n1;
887 }
888
889#ifdef FEAT_FLOAT
890 // If one of the two variables is a float, compare as a float.
891 // When using "=~" or "!~", always compare as string.
892 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
893 && type != EXPR_MATCH && type != EXPR_NOMATCH)
894 {
895 float_T f1, f2;
896
897 f1 = tv_get_float(typ1);
898 f2 = tv_get_float(typ2);
899 n1 = FALSE;
900 switch (type)
901 {
902 case EXPR_IS:
903 case EXPR_EQUAL: n1 = (f1 == f2); break;
904 case EXPR_ISNOT:
905 case EXPR_NEQUAL: n1 = (f1 != f2); break;
906 case EXPR_GREATER: n1 = (f1 > f2); break;
907 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
908 case EXPR_SMALLER: n1 = (f1 < f2); break;
909 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
910 case EXPR_UNKNOWN:
911 case EXPR_MATCH:
912 default: break; // avoid gcc warning
913 }
914 }
915#endif
916
917 // If one of the two variables is a number, compare as a number.
918 // When using "=~" or "!~", always compare as string.
919 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
920 && type != EXPR_MATCH && type != EXPR_NOMATCH)
921 {
922 n1 = tv_get_number(typ1);
923 n2 = tv_get_number(typ2);
924 switch (type)
925 {
926 case EXPR_IS:
927 case EXPR_EQUAL: n1 = (n1 == n2); break;
928 case EXPR_ISNOT:
929 case EXPR_NEQUAL: n1 = (n1 != n2); break;
930 case EXPR_GREATER: n1 = (n1 > n2); break;
931 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
932 case EXPR_SMALLER: n1 = (n1 < n2); break;
933 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
934 case EXPR_UNKNOWN:
935 case EXPR_MATCH:
936 default: break; // avoid gcc warning
937 }
938 }
Bram Moolenaarcff40ff2021-01-09 16:21:37 +0100939 else if (in_vim9script() && (typ1->v_type == VAR_BOOL
940 || typ2->v_type == VAR_BOOL))
941 {
942 if (typ1->v_type != typ2->v_type)
943 {
944 semsg(_(e_cannot_compare_str_with_str),
945 vartype_name(typ1->v_type), vartype_name(typ2->v_type));
946 clear_tv(typ1);
947 return FAIL;
948 }
949 n1 = typ1->vval.v_number;
950 n2 = typ2->vval.v_number;
951 switch (type)
952 {
953 case EXPR_IS:
954 case EXPR_EQUAL: n1 = (n1 == n2); break;
955 case EXPR_ISNOT:
956 case EXPR_NEQUAL: n1 = (n1 != n2); break;
957 default:
958 emsg(_(e_invalid_operation_for_bool));
959 clear_tv(typ1);
960 return FAIL;
961 }
962 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200963 else
964 {
965 s1 = tv_get_string_buf(typ1, buf1);
966 s2 = tv_get_string_buf(typ2, buf2);
967 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
968 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
969 else
970 i = 0;
971 n1 = FALSE;
972 switch (type)
973 {
974 case EXPR_IS:
975 case EXPR_EQUAL: n1 = (i == 0); break;
976 case EXPR_ISNOT:
977 case EXPR_NEQUAL: n1 = (i != 0); break;
978 case EXPR_GREATER: n1 = (i > 0); break;
979 case EXPR_GEQUAL: n1 = (i >= 0); break;
980 case EXPR_SMALLER: n1 = (i < 0); break;
981 case EXPR_SEQUAL: n1 = (i <= 0); break;
982
983 case EXPR_MATCH:
984 case EXPR_NOMATCH:
985 n1 = pattern_match(s2, s1, ic);
986 if (type == EXPR_NOMATCH)
987 n1 = !n1;
988 break;
989
990 default: break; // avoid gcc warning
991 }
992 }
993 clear_tv(typ1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +0200994 if (in_vim9script())
995 {
996 typ1->v_type = VAR_BOOL;
997 typ1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
998 }
999 else
1000 {
1001 typ1->v_type = VAR_NUMBER;
1002 typ1->vval.v_number = n1;
1003 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001004
1005 return OK;
1006}
1007
Bram Moolenaar34453202021-01-31 13:08:38 +01001008/*
1009 * Convert any type to a string, never give an error.
1010 * When "quotes" is TRUE add quotes to a string.
1011 * Returns an allocated string.
1012 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001013 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001014typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001015{
1016 char_u *tofree;
1017 char_u numbuf[NUMBUFLEN];
1018 char_u *ret = NULL;
1019
1020 if (arg == NULL)
1021 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001022 if (!quotes && arg->v_type == VAR_STRING)
1023 {
1024 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1025 : arg->vval.v_string);
1026 }
1027 else
1028 {
1029 ret = tv2string(arg, &tofree, numbuf, 0);
1030 // Make a copy if we have a value but it's not in allocated memory.
1031 if (ret != NULL && tofree == NULL)
1032 ret = vim_strsave(ret);
1033 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001034 return ret;
1035}
1036
1037/*
1038 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1039 * or it refers to a List or Dictionary that is locked.
1040 */
1041 int
1042tv_islocked(typval_T *tv)
1043{
1044 return (tv->v_lock & VAR_LOCKED)
1045 || (tv->v_type == VAR_LIST
1046 && tv->vval.v_list != NULL
1047 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1048 || (tv->v_type == VAR_DICT
1049 && tv->vval.v_dict != NULL
1050 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1051}
1052
1053 static int
1054func_equal(
1055 typval_T *tv1,
1056 typval_T *tv2,
1057 int ic) // ignore case
1058{
1059 char_u *s1, *s2;
1060 dict_T *d1, *d2;
1061 int a1, a2;
1062 int i;
1063
1064 // empty and NULL function name considered the same
1065 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1066 : partial_name(tv1->vval.v_partial);
1067 if (s1 != NULL && *s1 == NUL)
1068 s1 = NULL;
1069 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1070 : partial_name(tv2->vval.v_partial);
1071 if (s2 != NULL && *s2 == NUL)
1072 s2 = NULL;
1073 if (s1 == NULL || s2 == NULL)
1074 {
1075 if (s1 != s2)
1076 return FALSE;
1077 }
1078 else if (STRCMP(s1, s2) != 0)
1079 return FALSE;
1080
1081 // empty dict and NULL dict is different
1082 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1083 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1084 if (d1 == NULL || d2 == NULL)
1085 {
1086 if (d1 != d2)
1087 return FALSE;
1088 }
1089 else if (!dict_equal(d1, d2, ic, TRUE))
1090 return FALSE;
1091
1092 // empty list and no list considered the same
1093 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1094 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1095 if (a1 != a2)
1096 return FALSE;
1097 for (i = 0; i < a1; ++i)
1098 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1099 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1100 return FALSE;
1101
1102 return TRUE;
1103}
1104
1105/*
1106 * Return TRUE if "tv1" and "tv2" have the same value.
1107 * Compares the items just like "==" would compare them, but strings and
1108 * numbers are different. Floats and numbers are also different.
1109 */
1110 int
1111tv_equal(
1112 typval_T *tv1,
1113 typval_T *tv2,
1114 int ic, // ignore case
1115 int recursive) // TRUE when used recursively
1116{
1117 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1118 char_u *s1, *s2;
1119 static int recursive_cnt = 0; // catch recursive loops
1120 int r;
1121 static int tv_equal_recurse_limit;
1122
1123 // Catch lists and dicts that have an endless loop by limiting
1124 // recursiveness to a limit. We guess they are equal then.
1125 // A fixed limit has the problem of still taking an awful long time.
1126 // Reduce the limit every time running into it. That should work fine for
1127 // deeply linked structures that are not recursively linked and catch
1128 // recursiveness quickly.
1129 if (!recursive)
1130 tv_equal_recurse_limit = 1000;
1131 if (recursive_cnt >= tv_equal_recurse_limit)
1132 {
1133 --tv_equal_recurse_limit;
1134 return TRUE;
1135 }
1136
1137 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1138 // arguments.
1139 if ((tv1->v_type == VAR_FUNC
1140 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1141 && (tv2->v_type == VAR_FUNC
1142 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1143 {
1144 ++recursive_cnt;
1145 r = func_equal(tv1, tv2, ic);
1146 --recursive_cnt;
1147 return r;
1148 }
1149
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001150 if (tv1->v_type != tv2->v_type
1151 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1152 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001153 return FALSE;
1154
1155 switch (tv1->v_type)
1156 {
1157 case VAR_LIST:
1158 ++recursive_cnt;
1159 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1160 --recursive_cnt;
1161 return r;
1162
1163 case VAR_DICT:
1164 ++recursive_cnt;
1165 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1166 --recursive_cnt;
1167 return r;
1168
1169 case VAR_BLOB:
1170 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1171
1172 case VAR_NUMBER:
1173 case VAR_BOOL:
1174 case VAR_SPECIAL:
1175 return tv1->vval.v_number == tv2->vval.v_number;
1176
1177 case VAR_STRING:
1178 s1 = tv_get_string_buf(tv1, buf1);
1179 s2 = tv_get_string_buf(tv2, buf2);
1180 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1181
1182 case VAR_FLOAT:
1183#ifdef FEAT_FLOAT
1184 return tv1->vval.v_float == tv2->vval.v_float;
1185#endif
1186 case VAR_JOB:
1187#ifdef FEAT_JOB_CHANNEL
1188 return tv1->vval.v_job == tv2->vval.v_job;
1189#endif
1190 case VAR_CHANNEL:
1191#ifdef FEAT_JOB_CHANNEL
1192 return tv1->vval.v_channel == tv2->vval.v_channel;
1193#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001194 case VAR_INSTR:
1195 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001196
1197 case VAR_PARTIAL:
1198 return tv1->vval.v_partial == tv2->vval.v_partial;
1199
1200 case VAR_FUNC:
1201 return tv1->vval.v_string == tv2->vval.v_string;
1202
1203 case VAR_UNKNOWN:
1204 case VAR_ANY:
1205 case VAR_VOID:
1206 break;
1207 }
1208
1209 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1210 // does not equal anything, not even itself.
1211 return FALSE;
1212}
1213
1214/*
1215 * Get an option value.
1216 * "arg" points to the '&' or '+' before the option name.
1217 * "arg" is advanced to character after the option name.
1218 * Return OK or FAIL.
1219 */
1220 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001221eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001222 char_u **arg,
1223 typval_T *rettv, // when NULL, only check if option exists
1224 int evaluate)
1225{
1226 char_u *option_end;
1227 long numval;
1228 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001229 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001230 int c;
1231 int working = (**arg == '+'); // has("+option")
1232 int ret = OK;
1233 int opt_flags;
1234
1235 // Isolate the option name and find its value.
1236 option_end = find_option_end(arg, &opt_flags);
1237 if (option_end == NULL)
1238 {
1239 if (rettv != NULL)
1240 semsg(_("E112: Option name missing: %s"), *arg);
1241 return FAIL;
1242 }
1243
1244 if (!evaluate)
1245 {
1246 *arg = option_end;
1247 return OK;
1248 }
1249
1250 c = *option_end;
1251 *option_end = NUL;
1252 opt_type = get_option_value(*arg, &numval,
1253 rettv == NULL ? NULL : &stringval, opt_flags);
1254
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001255 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001256 {
1257 if (rettv != NULL)
1258 semsg(_(e_unknown_option), *arg);
1259 ret = FAIL;
1260 }
1261 else if (rettv != NULL)
1262 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001263 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001264 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001265 {
1266 rettv->v_type = VAR_STRING;
1267 rettv->vval.v_string = NULL;
1268 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001269 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001270 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001271 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1272 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001273 rettv->vval.v_number = 0;
1274 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001275 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001276 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001277 if (in_vim9script() && opt_type == gov_bool)
1278 {
1279 rettv->v_type = VAR_BOOL;
1280 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1281 }
1282 else
1283 {
1284 rettv->v_type = VAR_NUMBER;
1285 rettv->vval.v_number = numval;
1286 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001287 }
1288 else // string option
1289 {
1290 rettv->v_type = VAR_STRING;
1291 rettv->vval.v_string = stringval;
1292 }
1293 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001294 else if (working && (opt_type == gov_hidden_bool
1295 || opt_type == gov_hidden_number
1296 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001297 ret = FAIL;
1298
1299 *option_end = c; // put back for error messages
1300 *arg = option_end;
1301
1302 return ret;
1303}
1304
1305/*
1306 * Allocate a variable for a number constant. Also deals with "0z" for blob.
1307 * Return OK or FAIL.
1308 */
1309 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001310eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001311 char_u **arg,
1312 typval_T *rettv,
1313 int evaluate,
1314 int want_string UNUSED)
1315{
1316 int len;
1317#ifdef FEAT_FLOAT
1318 char_u *p;
1319 int get_float = FALSE;
1320
1321 // We accept a float when the format matches
1322 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
1323 // strict to avoid backwards compatibility problems.
1324 // With script version 2 and later the leading digit can be
1325 // omitted.
1326 // Don't look for a float after the "." operator, so that
1327 // ":let vers = 1.2.3" doesn't fail.
1328 if (**arg == '.')
1329 p = *arg;
1330 else
1331 p = skipdigits(*arg + 1);
1332 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
1333 {
1334 get_float = TRUE;
1335 p = skipdigits(p + 2);
1336 if (*p == 'e' || *p == 'E')
1337 {
1338 ++p;
1339 if (*p == '-' || *p == '+')
1340 ++p;
1341 if (!vim_isdigit(*p))
1342 get_float = FALSE;
1343 else
1344 p = skipdigits(p + 1);
1345 }
1346 if (ASCII_ISALPHA(*p) || *p == '.')
1347 get_float = FALSE;
1348 }
1349 if (get_float)
1350 {
1351 float_T f;
1352
1353 *arg += string2float(*arg, &f);
1354 if (evaluate)
1355 {
1356 rettv->v_type = VAR_FLOAT;
1357 rettv->vval.v_float = f;
1358 }
1359 }
1360 else
1361#endif
1362 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
1363 {
1364 char_u *bp;
1365 blob_T *blob = NULL; // init for gcc
1366
1367 // Blob constant: 0z0123456789abcdef
1368 if (evaluate)
1369 blob = blob_alloc();
1370 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
1371 {
1372 if (!vim_isxdigit(bp[1]))
1373 {
1374 if (blob != NULL)
1375 {
1376 emsg(_("E973: Blob literal should have an even number of hex characters"));
1377 ga_clear(&blob->bv_ga);
1378 VIM_CLEAR(blob);
1379 }
1380 return FAIL;
1381 }
1382 if (blob != NULL)
1383 ga_append(&blob->bv_ga,
1384 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
1385 if (bp[2] == '.' && vim_isxdigit(bp[3]))
1386 ++bp;
1387 }
1388 if (blob != NULL)
1389 rettv_blob_set(rettv, blob);
1390 *arg = bp;
1391 }
1392 else
1393 {
1394 varnumber_T n;
1395
1396 // decimal, hex or octal number
1397 vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4
1398 ? STR2NR_NO_OCT + STR2NR_QUOTE
1399 : STR2NR_ALL, &n, NULL, 0, TRUE);
1400 if (len == 0)
1401 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001402 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001403 return FAIL;
1404 }
1405 *arg += len;
1406 if (evaluate)
1407 {
1408 rettv->v_type = VAR_NUMBER;
1409 rettv->vval.v_number = n;
1410 }
1411 }
1412 return OK;
1413}
1414
1415/*
1416 * Allocate a variable for a string constant.
1417 * Return OK or FAIL.
1418 */
1419 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001420eval_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001421{
1422 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001423 char_u *end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001424 int extra = 0;
1425 int len;
1426
1427 // Find the end of the string, skipping backslashed characters.
1428 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
1429 {
1430 if (*p == '\\' && p[1] != NUL)
1431 {
1432 ++p;
1433 // A "\<x>" form occupies at least 4 characters, and produces up
1434 // to 21 characters (3 * 6 for the char and 3 for a modifier):
1435 // reserve space for 18 extra.
1436 // Each byte in the char could be encoded as K_SPECIAL K_EXTRA x.
1437 if (*p == '<')
1438 extra += 18;
1439 }
1440 }
1441
1442 if (*p != '"')
1443 {
1444 semsg(_("E114: Missing quote: %s"), *arg);
1445 return FAIL;
1446 }
1447
1448 // If only parsing, set *arg and return here
1449 if (!evaluate)
1450 {
1451 *arg = p + 1;
1452 return OK;
1453 }
1454
1455 // Copy the string into allocated memory, handling backslashed
1456 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001457 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001458 len = (int)(p - *arg + extra);
1459 rettv->vval.v_string = alloc(len);
1460 if (rettv->vval.v_string == NULL)
1461 return FAIL;
1462 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001463
1464 for (p = *arg + 1; *p != NUL && *p != '"'; )
1465 {
1466 if (*p == '\\')
1467 {
1468 switch (*++p)
1469 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001470 case 'b': *end++ = BS; ++p; break;
1471 case 'e': *end++ = ESC; ++p; break;
1472 case 'f': *end++ = FF; ++p; break;
1473 case 'n': *end++ = NL; ++p; break;
1474 case 'r': *end++ = CAR; ++p; break;
1475 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001476
1477 case 'X': // hex: "\x1", "\x12"
1478 case 'x':
1479 case 'u': // Unicode: "\u0023"
1480 case 'U':
1481 if (vim_isxdigit(p[1]))
1482 {
1483 int n, nr;
1484 int c = toupper(*p);
1485
1486 if (c == 'X')
1487 n = 2;
1488 else if (*p == 'u')
1489 n = 4;
1490 else
1491 n = 8;
1492 nr = 0;
1493 while (--n >= 0 && vim_isxdigit(p[1]))
1494 {
1495 ++p;
1496 nr = (nr << 4) + hex2nr(*p);
1497 }
1498 ++p;
1499 // For "\u" store the number according to
1500 // 'encoding'.
1501 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001502 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001503 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001504 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001505 }
1506 break;
1507
1508 // octal: "\1", "\12", "\123"
1509 case '0':
1510 case '1':
1511 case '2':
1512 case '3':
1513 case '4':
1514 case '5':
1515 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001516 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001517 if (*p >= '0' && *p <= '7')
1518 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001519 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001520 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001521 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001522 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001523 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001524 break;
1525
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001526 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001527 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001528 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001529 int flags = FSK_KEYCODE | FSK_IN_STRING;
1530
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001531 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001532 flags |= FSK_SIMPLIFY;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001533 extra = trans_special(&p, end, flags, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001534 if (extra != 0)
1535 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001536 end += extra;
1537 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001538 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001539 break;
1540 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001541 }
1542 // FALLTHROUGH
1543
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001544 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001545 break;
1546 }
1547 }
1548 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001549 MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001550 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001551 *end = NUL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001552 if (*p != NUL) // just in case
1553 ++p;
1554 *arg = p;
1555
1556 return OK;
1557}
1558
1559/*
1560 * Allocate a variable for a 'str''ing' constant.
1561 * Return OK or FAIL.
1562 */
1563 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001564eval_lit_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001565{
1566 char_u *p;
1567 char_u *str;
1568 int reduce = 0;
1569
1570 // Find the end of the string, skipping ''.
1571 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
1572 {
1573 if (*p == '\'')
1574 {
1575 if (p[1] != '\'')
1576 break;
1577 ++reduce;
1578 ++p;
1579 }
1580 }
1581
1582 if (*p != '\'')
1583 {
1584 semsg(_("E115: Missing quote: %s"), *arg);
1585 return FAIL;
1586 }
1587
1588 // If only parsing return after setting "*arg"
1589 if (!evaluate)
1590 {
1591 *arg = p + 1;
1592 return OK;
1593 }
1594
1595 // Copy the string into allocated memory, handling '' to ' reduction.
1596 str = alloc((p - *arg) - reduce);
1597 if (str == NULL)
1598 return FAIL;
1599 rettv->v_type = VAR_STRING;
1600 rettv->vval.v_string = str;
1601
1602 for (p = *arg + 1; *p != NUL; )
1603 {
1604 if (*p == '\'')
1605 {
1606 if (p[1] != '\'')
1607 break;
1608 ++p;
1609 }
1610 MB_COPY_CHAR(p, str);
1611 }
1612 *str = NUL;
1613 *arg = p + 1;
1614
1615 return OK;
1616}
1617
1618/*
1619 * Return a string with the string representation of a variable.
1620 * If the memory is allocated "tofree" is set to it, otherwise NULL.
1621 * "numbuf" is used for a number.
1622 * Puts quotes around strings, so that they can be parsed back by eval().
1623 * May return NULL.
1624 */
1625 char_u *
1626tv2string(
1627 typval_T *tv,
1628 char_u **tofree,
1629 char_u *numbuf,
1630 int copyID)
1631{
1632 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
1633}
1634
1635/*
1636 * Get the value of an environment variable.
1637 * "arg" is pointing to the '$'. It is advanced to after the name.
1638 * If the environment variable was not set, silently assume it is empty.
1639 * Return FAIL if the name is invalid.
1640 */
1641 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001642eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001643{
1644 char_u *string = NULL;
1645 int len;
1646 int cc;
1647 char_u *name;
1648 int mustfree = FALSE;
1649
1650 ++*arg;
1651 name = *arg;
1652 len = get_env_len(arg);
1653 if (evaluate)
1654 {
1655 if (len == 0)
1656 return FAIL; // invalid empty name
1657
1658 cc = name[len];
1659 name[len] = NUL;
1660 // first try vim_getenv(), fast for normal environment vars
1661 string = vim_getenv(name, &mustfree);
1662 if (string != NULL && *string != NUL)
1663 {
1664 if (!mustfree)
1665 string = vim_strsave(string);
1666 }
1667 else
1668 {
1669 if (mustfree)
1670 vim_free(string);
1671
1672 // next try expanding things like $VIM and ${HOME}
1673 string = expand_env_save(name - 1);
1674 if (string != NULL && *string == '$')
1675 VIM_CLEAR(string);
1676 }
1677 name[len] = cc;
1678
1679 rettv->v_type = VAR_STRING;
1680 rettv->vval.v_string = string;
1681 }
1682
1683 return OK;
1684}
1685
1686/*
1687 * Get the lnum from the first argument.
1688 * Also accepts ".", "$", etc., but that only works for the current buffer.
1689 * Returns -1 on error.
1690 */
1691 linenr_T
1692tv_get_lnum(typval_T *argvars)
1693{
Bram Moolenaar9a963372020-12-21 21:58:46 +01001694 linenr_T lnum = -1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001695
Bram Moolenaar56acb092020-08-16 14:48:19 +02001696 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
1697 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02001698 if (lnum <= 0 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001699 {
1700 int fnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001701 pos_T *fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001702
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02001703 // no valid number, try using arg like line()
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001704 if (fp != NULL)
1705 lnum = fp->lnum;
1706 }
1707 return lnum;
1708}
1709
1710/*
1711 * Get the lnum from the first argument.
1712 * Also accepts "$", then "buf" is used.
1713 * Returns 0 on error.
1714 */
1715 linenr_T
1716tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1717{
1718 if (argvars[0].v_type == VAR_STRING
1719 && argvars[0].vval.v_string != NULL
1720 && argvars[0].vval.v_string[0] == '$'
1721 && buf != NULL)
1722 return buf->b_ml.ml_line_count;
1723 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1724}
1725
1726/*
1727 * Get buffer by number or pattern.
1728 */
1729 buf_T *
1730tv_get_buf(typval_T *tv, int curtab_only)
1731{
1732 char_u *name = tv->vval.v_string;
1733 buf_T *buf;
1734
1735 if (tv->v_type == VAR_NUMBER)
1736 return buflist_findnr((int)tv->vval.v_number);
1737 if (tv->v_type != VAR_STRING)
1738 return NULL;
1739 if (name == NULL || *name == NUL)
1740 return curbuf;
1741 if (name[0] == '$' && name[1] == NUL)
1742 return lastbuf;
1743
1744 buf = buflist_find_by_name(name, curtab_only);
1745
1746 // If not found, try expanding the name, like done for bufexists().
1747 if (buf == NULL)
1748 buf = find_buffer(tv);
1749
1750 return buf;
1751}
1752
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02001753/*
1754 * Like tv_get_buf() but give an error message is the type is wrong.
1755 */
1756 buf_T *
1757tv_get_buf_from_arg(typval_T *tv)
1758{
1759 buf_T *buf;
1760
1761 ++emsg_off;
1762 buf = tv_get_buf(tv, FALSE);
1763 --emsg_off;
1764 if (buf == NULL
1765 && tv->v_type != VAR_NUMBER
1766 && tv->v_type != VAR_STRING)
1767 // issue errmsg for type error
1768 (void)tv_get_number(tv);
1769 return buf;
1770}
1771
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001772#endif // FEAT_EVAL