Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 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 * |
| 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 * |
| 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 |
| 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: |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 94 | case VAR_INSTR: |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 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 |
| 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 |
Bram Moolenaar | 24f7209 | 2021-05-07 20:43:54 +0200 | [diff] [blame] | 154 | break; |
| 155 | case VAR_INSTR: |
| 156 | VIM_CLEAR(varp->vval.v_instr); |
| 157 | break; |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 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 |
| 171 | init_tv(typval_T *varp) |
| 172 | { |
| 173 | if (varp != NULL) |
| 174 | CLEAR_POINTER(varp); |
| 175 | } |
| 176 | |
Bram Moolenaar | 36967b3 | 2020-08-17 21:41:02 +0200 | [diff] [blame] | 177 | static varnumber_T |
| 178 | tv_get_bool_or_number_chk(typval_T *varp, int *denote, int want_bool) |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 179 | { |
| 180 | varnumber_T n = 0L; |
| 181 | |
| 182 | switch (varp->v_type) |
| 183 | { |
| 184 | case VAR_NUMBER: |
Bram Moolenaar | bade44e | 2020-09-26 22:39:24 +0200 | [diff] [blame] | 185 | if (in_vim9script() && want_bool && varp->vval.v_number != 0 |
Bram Moolenaar | d70840e | 2020-08-22 15:06:35 +0200 | [diff] [blame] | 186 | && varp->vval.v_number != 1) |
| 187 | { |
| 188 | semsg(_(e_using_number_as_bool_nr), varp->vval.v_number); |
| 189 | break; |
| 190 | } |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 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: |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 202 | if (in_vim9script()) |
| 203 | { |
Bram Moolenaar | ea2d407 | 2020-11-12 12:08:51 +0100 | [diff] [blame] | 204 | emsg_using_string_as(varp, !want_bool); |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 205 | break; |
| 206 | } |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 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: |
Bram Moolenaar | 36967b3 | 2020-08-17 21:41:02 +0200 | [diff] [blame] | 219 | if (!want_bool && in_vim9script()) |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 220 | { |
Bram Moolenaar | d92cc13 | 2020-11-18 17:17:15 +0100 | [diff] [blame] | 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")); |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 225 | break; |
| 226 | } |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 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_UNKNOWN: |
| 242 | case VAR_ANY: |
| 243 | case VAR_VOID: |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 244 | case VAR_INSTR: |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 245 | internal_error_no_abort("tv_get_number(UNKNOWN)"); |
| 246 | break; |
| 247 | } |
| 248 | if (denote == NULL) // useful for values that must be unsigned |
| 249 | n = -1; |
| 250 | else |
| 251 | *denote = TRUE; |
| 252 | return n; |
| 253 | } |
| 254 | |
Bram Moolenaar | 36967b3 | 2020-08-17 21:41:02 +0200 | [diff] [blame] | 255 | /* |
| 256 | * Get the number value of a variable. |
| 257 | * If it is a String variable, uses vim_str2nr(). |
| 258 | * For incompatible types, return 0. |
| 259 | * tv_get_number_chk() is similar to tv_get_number(), but informs the |
| 260 | * caller of incompatible types: it sets *denote to TRUE if "denote" |
| 261 | * is not NULL or returns -1 otherwise. |
| 262 | */ |
| 263 | varnumber_T |
| 264 | tv_get_number(typval_T *varp) |
| 265 | { |
| 266 | int error = FALSE; |
| 267 | |
| 268 | return tv_get_number_chk(varp, &error); // return 0L on error |
| 269 | } |
| 270 | |
| 271 | varnumber_T |
| 272 | tv_get_number_chk(typval_T *varp, int *denote) |
| 273 | { |
| 274 | return tv_get_bool_or_number_chk(varp, denote, FALSE); |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * Get the boolean value of "varp". This is like tv_get_number_chk(), |
Bram Moolenaar | d70840e | 2020-08-22 15:06:35 +0200 | [diff] [blame] | 279 | * but in Vim9 script accepts Number (0 and 1) and Bool/Special. |
Bram Moolenaar | 36967b3 | 2020-08-17 21:41:02 +0200 | [diff] [blame] | 280 | */ |
| 281 | varnumber_T |
| 282 | tv_get_bool(typval_T *varp) |
| 283 | { |
| 284 | return tv_get_bool_or_number_chk(varp, NULL, TRUE); |
Bram Moolenaar | 36967b3 | 2020-08-17 21:41:02 +0200 | [diff] [blame] | 285 | } |
| 286 | |
Bram Moolenaar | e15eebd | 2020-08-18 19:11:38 +0200 | [diff] [blame] | 287 | /* |
| 288 | * Get the boolean value of "varp". This is like tv_get_number_chk(), |
| 289 | * but in Vim9 script accepts Number and Bool. |
| 290 | */ |
| 291 | varnumber_T |
| 292 | tv_get_bool_chk(typval_T *varp, int *denote) |
| 293 | { |
| 294 | return tv_get_bool_or_number_chk(varp, denote, TRUE); |
Bram Moolenaar | e15eebd | 2020-08-18 19:11:38 +0200 | [diff] [blame] | 295 | } |
| 296 | |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 297 | #ifdef FEAT_FLOAT |
| 298 | float_T |
| 299 | tv_get_float(typval_T *varp) |
| 300 | { |
| 301 | switch (varp->v_type) |
| 302 | { |
| 303 | case VAR_NUMBER: |
| 304 | return (float_T)(varp->vval.v_number); |
| 305 | case VAR_FLOAT: |
| 306 | return varp->vval.v_float; |
| 307 | case VAR_FUNC: |
| 308 | case VAR_PARTIAL: |
| 309 | emsg(_("E891: Using a Funcref as a Float")); |
| 310 | break; |
| 311 | case VAR_STRING: |
| 312 | emsg(_("E892: Using a String as a Float")); |
| 313 | break; |
| 314 | case VAR_LIST: |
| 315 | emsg(_("E893: Using a List as a Float")); |
| 316 | break; |
| 317 | case VAR_DICT: |
| 318 | emsg(_("E894: Using a Dictionary as a Float")); |
| 319 | break; |
| 320 | case VAR_BOOL: |
| 321 | emsg(_("E362: Using a boolean value as a Float")); |
| 322 | break; |
| 323 | case VAR_SPECIAL: |
| 324 | emsg(_("E907: Using a special value as a Float")); |
| 325 | break; |
| 326 | case VAR_JOB: |
| 327 | # ifdef FEAT_JOB_CHANNEL |
| 328 | emsg(_("E911: Using a Job as a Float")); |
| 329 | break; |
| 330 | # endif |
| 331 | case VAR_CHANNEL: |
| 332 | # ifdef FEAT_JOB_CHANNEL |
| 333 | emsg(_("E914: Using a Channel as a Float")); |
| 334 | break; |
| 335 | # endif |
| 336 | case VAR_BLOB: |
| 337 | emsg(_("E975: Using a Blob as a Float")); |
| 338 | break; |
| 339 | case VAR_UNKNOWN: |
| 340 | case VAR_ANY: |
| 341 | case VAR_VOID: |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 342 | case VAR_INSTR: |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 343 | internal_error_no_abort("tv_get_float(UNKNOWN)"); |
| 344 | break; |
| 345 | } |
| 346 | return 0; |
| 347 | } |
| 348 | #endif |
| 349 | |
| 350 | /* |
Bram Moolenaar | 2a9d5d3 | 2020-12-12 18:58:40 +0100 | [diff] [blame] | 351 | * Give an error and return FAIL unless "tv" is a string. |
Bram Moolenaar | 7bb4e74 | 2020-12-09 12:41:50 +0100 | [diff] [blame] | 352 | */ |
| 353 | int |
Bram Moolenaar | 32105ae | 2021-03-27 18:59:25 +0100 | [diff] [blame] | 354 | check_for_string_arg(typval_T *args, int idx) |
Bram Moolenaar | 7bb4e74 | 2020-12-09 12:41:50 +0100 | [diff] [blame] | 355 | { |
Bram Moolenaar | 32105ae | 2021-03-27 18:59:25 +0100 | [diff] [blame] | 356 | if (args[idx].v_type != VAR_STRING) |
Bram Moolenaar | 7bb4e74 | 2020-12-09 12:41:50 +0100 | [diff] [blame] | 357 | { |
Bram Moolenaar | 32105ae | 2021-03-27 18:59:25 +0100 | [diff] [blame] | 358 | if (idx >= 0) |
| 359 | semsg(_(e_string_required_for_argument_nr), idx + 1); |
Bram Moolenaar | f28f2ac | 2021-03-22 22:21:26 +0100 | [diff] [blame] | 360 | else |
| 361 | emsg(_(e_stringreq)); |
Bram Moolenaar | 7bb4e74 | 2020-12-09 12:41:50 +0100 | [diff] [blame] | 362 | return FAIL; |
| 363 | } |
| 364 | return OK; |
| 365 | } |
| 366 | |
| 367 | /* |
Bram Moolenaar | 32105ae | 2021-03-27 18:59:25 +0100 | [diff] [blame] | 368 | * Give an error and return FAIL unless "args[idx]" is a non-empty string. |
Bram Moolenaar | 2a9d5d3 | 2020-12-12 18:58:40 +0100 | [diff] [blame] | 369 | */ |
| 370 | int |
Bram Moolenaar | 32105ae | 2021-03-27 18:59:25 +0100 | [diff] [blame] | 371 | check_for_nonempty_string_arg(typval_T *args, int idx) |
Bram Moolenaar | 2a9d5d3 | 2020-12-12 18:58:40 +0100 | [diff] [blame] | 372 | { |
Bram Moolenaar | 32105ae | 2021-03-27 18:59:25 +0100 | [diff] [blame] | 373 | if (check_for_string_arg(args, idx) == FAIL) |
Bram Moolenaar | 2a9d5d3 | 2020-12-12 18:58:40 +0100 | [diff] [blame] | 374 | return FAIL; |
Bram Moolenaar | 32105ae | 2021-03-27 18:59:25 +0100 | [diff] [blame] | 375 | if (args[idx].vval.v_string == NULL || *args[idx].vval.v_string == NUL) |
Bram Moolenaar | 2a9d5d3 | 2020-12-12 18:58:40 +0100 | [diff] [blame] | 376 | { |
Bram Moolenaar | e8e3078 | 2021-04-10 21:46:05 +0200 | [diff] [blame] | 377 | semsg(_(e_non_empty_string_required_for_argument_nr), idx + 1); |
Bram Moolenaar | 2a9d5d3 | 2020-12-12 18:58:40 +0100 | [diff] [blame] | 378 | return FAIL; |
| 379 | } |
| 380 | return OK; |
| 381 | } |
| 382 | |
| 383 | /* |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 384 | * Get the string value of a variable. |
| 385 | * If it is a Number variable, the number is converted into a string. |
| 386 | * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE! |
| 387 | * tv_get_string_buf() uses a given buffer. |
| 388 | * If the String variable has never been set, return an empty string. |
| 389 | * Never returns NULL; |
| 390 | * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return |
| 391 | * NULL on error. |
| 392 | */ |
| 393 | char_u * |
| 394 | tv_get_string(typval_T *varp) |
| 395 | { |
| 396 | static char_u mybuf[NUMBUFLEN]; |
| 397 | |
| 398 | return tv_get_string_buf(varp, mybuf); |
| 399 | } |
| 400 | |
Bram Moolenaar | f2b26bc | 2021-01-30 23:05:11 +0100 | [diff] [blame] | 401 | /* |
| 402 | * Like tv_get_string() but don't allow number to string conversion for Vim9. |
| 403 | */ |
| 404 | char_u * |
| 405 | tv_get_string_strict(typval_T *varp) |
| 406 | { |
| 407 | static char_u mybuf[NUMBUFLEN]; |
| 408 | char_u *res = tv_get_string_buf_chk_strict( |
| 409 | varp, mybuf, in_vim9script()); |
| 410 | |
| 411 | return res != NULL ? res : (char_u *)""; |
| 412 | } |
| 413 | |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 414 | char_u * |
| 415 | tv_get_string_buf(typval_T *varp, char_u *buf) |
| 416 | { |
| 417 | char_u *res = tv_get_string_buf_chk(varp, buf); |
| 418 | |
| 419 | return res != NULL ? res : (char_u *)""; |
| 420 | } |
| 421 | |
| 422 | /* |
| 423 | * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE! |
| 424 | */ |
| 425 | char_u * |
| 426 | tv_get_string_chk(typval_T *varp) |
| 427 | { |
| 428 | static char_u mybuf[NUMBUFLEN]; |
| 429 | |
| 430 | return tv_get_string_buf_chk(varp, mybuf); |
| 431 | } |
| 432 | |
| 433 | char_u * |
| 434 | tv_get_string_buf_chk(typval_T *varp, char_u *buf) |
| 435 | { |
Bram Moolenaar | f2b26bc | 2021-01-30 23:05:11 +0100 | [diff] [blame] | 436 | return tv_get_string_buf_chk_strict(varp, buf, FALSE); |
| 437 | } |
| 438 | |
| 439 | char_u * |
| 440 | tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict) |
| 441 | { |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 442 | switch (varp->v_type) |
| 443 | { |
| 444 | case VAR_NUMBER: |
Bram Moolenaar | f2b26bc | 2021-01-30 23:05:11 +0100 | [diff] [blame] | 445 | if (strict) |
| 446 | { |
| 447 | emsg(_(e_using_number_as_string)); |
| 448 | break; |
| 449 | } |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 450 | vim_snprintf((char *)buf, NUMBUFLEN, "%lld", |
| 451 | (varnumber_T)varp->vval.v_number); |
| 452 | return buf; |
| 453 | case VAR_FUNC: |
| 454 | case VAR_PARTIAL: |
| 455 | emsg(_("E729: using Funcref as a String")); |
| 456 | break; |
| 457 | case VAR_LIST: |
| 458 | emsg(_("E730: using List as a String")); |
| 459 | break; |
| 460 | case VAR_DICT: |
| 461 | emsg(_("E731: using Dictionary as a String")); |
| 462 | break; |
| 463 | case VAR_FLOAT: |
| 464 | #ifdef FEAT_FLOAT |
| 465 | emsg(_(e_float_as_string)); |
| 466 | break; |
| 467 | #endif |
| 468 | case VAR_STRING: |
| 469 | if (varp->vval.v_string != NULL) |
| 470 | return varp->vval.v_string; |
| 471 | return (char_u *)""; |
| 472 | case VAR_BOOL: |
| 473 | case VAR_SPECIAL: |
| 474 | STRCPY(buf, get_var_special_name(varp->vval.v_number)); |
| 475 | return buf; |
| 476 | case VAR_BLOB: |
| 477 | emsg(_("E976: using Blob as a String")); |
| 478 | break; |
| 479 | case VAR_JOB: |
| 480 | #ifdef FEAT_JOB_CHANNEL |
| 481 | { |
| 482 | job_T *job = varp->vval.v_job; |
| 483 | char *status; |
| 484 | |
| 485 | if (job == NULL) |
| 486 | return (char_u *)"no process"; |
| 487 | status = job->jv_status == JOB_FAILED ? "fail" |
| 488 | : job->jv_status >= JOB_ENDED ? "dead" |
| 489 | : "run"; |
| 490 | # ifdef UNIX |
| 491 | vim_snprintf((char *)buf, NUMBUFLEN, |
| 492 | "process %ld %s", (long)job->jv_pid, status); |
| 493 | # elif defined(MSWIN) |
| 494 | vim_snprintf((char *)buf, NUMBUFLEN, |
| 495 | "process %ld %s", |
| 496 | (long)job->jv_proc_info.dwProcessId, |
| 497 | status); |
| 498 | # else |
| 499 | // fall-back |
| 500 | vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status); |
| 501 | # endif |
| 502 | return buf; |
| 503 | } |
| 504 | #endif |
| 505 | break; |
| 506 | case VAR_CHANNEL: |
| 507 | #ifdef FEAT_JOB_CHANNEL |
| 508 | { |
| 509 | channel_T *channel = varp->vval.v_channel; |
| 510 | char *status = channel_status(channel, -1); |
| 511 | |
| 512 | if (channel == NULL) |
| 513 | vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status); |
| 514 | else |
| 515 | vim_snprintf((char *)buf, NUMBUFLEN, |
| 516 | "channel %d %s", channel->ch_id, status); |
| 517 | return buf; |
| 518 | } |
| 519 | #endif |
| 520 | break; |
| 521 | case VAR_UNKNOWN: |
| 522 | case VAR_ANY: |
| 523 | case VAR_VOID: |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 524 | case VAR_INSTR: |
Bram Moolenaar | 68db996 | 2021-05-09 23:19:22 +0200 | [diff] [blame] | 525 | semsg(_(e_using_invalid_value_as_string_str), |
| 526 | vartype_name(varp->v_type)); |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 527 | break; |
| 528 | } |
| 529 | return NULL; |
| 530 | } |
| 531 | |
| 532 | /* |
| 533 | * Turn a typeval into a string. Similar to tv_get_string_buf() but uses |
| 534 | * string() on Dict, List, etc. |
| 535 | */ |
| 536 | char_u * |
| 537 | tv_stringify(typval_T *varp, char_u *buf) |
| 538 | { |
| 539 | if (varp->v_type == VAR_LIST |
| 540 | || varp->v_type == VAR_DICT |
| 541 | || varp->v_type == VAR_BLOB |
| 542 | || varp->v_type == VAR_FUNC |
| 543 | || varp->v_type == VAR_PARTIAL |
| 544 | || varp->v_type == VAR_FLOAT) |
| 545 | { |
| 546 | typval_T tmp; |
| 547 | |
| 548 | f_string(varp, &tmp); |
| 549 | tv_get_string_buf(&tmp, buf); |
| 550 | clear_tv(varp); |
| 551 | *varp = tmp; |
| 552 | return tmp.vval.v_string; |
| 553 | } |
| 554 | return tv_get_string_buf(varp, buf); |
| 555 | } |
| 556 | |
| 557 | /* |
| 558 | * Return TRUE if typeval "tv" and its value are set to be locked (immutable). |
| 559 | * Also give an error message, using "name" or _("name") when use_gettext is |
| 560 | * TRUE. |
| 561 | */ |
| 562 | int |
| 563 | tv_check_lock(typval_T *tv, char_u *name, int use_gettext) |
| 564 | { |
| 565 | int lock = 0; |
| 566 | |
| 567 | switch (tv->v_type) |
| 568 | { |
| 569 | case VAR_BLOB: |
| 570 | if (tv->vval.v_blob != NULL) |
| 571 | lock = tv->vval.v_blob->bv_lock; |
| 572 | break; |
| 573 | case VAR_LIST: |
| 574 | if (tv->vval.v_list != NULL) |
| 575 | lock = tv->vval.v_list->lv_lock; |
| 576 | break; |
| 577 | case VAR_DICT: |
| 578 | if (tv->vval.v_dict != NULL) |
| 579 | lock = tv->vval.v_dict->dv_lock; |
| 580 | break; |
| 581 | default: |
| 582 | break; |
| 583 | } |
Bram Moolenaar | a187c43 | 2020-09-16 21:08:28 +0200 | [diff] [blame] | 584 | return value_check_lock(tv->v_lock, name, use_gettext) |
| 585 | || (lock != 0 && value_check_lock(lock, name, use_gettext)); |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | /* |
| 589 | * Copy the values from typval_T "from" to typval_T "to". |
| 590 | * When needed allocates string or increases reference count. |
| 591 | * Does not make a copy of a list, blob or dict but copies the reference! |
| 592 | * It is OK for "from" and "to" to point to the same item. This is used to |
| 593 | * make a copy later. |
| 594 | */ |
| 595 | void |
| 596 | copy_tv(typval_T *from, typval_T *to) |
| 597 | { |
| 598 | to->v_type = from->v_type; |
| 599 | to->v_lock = 0; |
| 600 | switch (from->v_type) |
| 601 | { |
| 602 | case VAR_NUMBER: |
| 603 | case VAR_BOOL: |
| 604 | case VAR_SPECIAL: |
| 605 | to->vval.v_number = from->vval.v_number; |
| 606 | break; |
| 607 | case VAR_FLOAT: |
| 608 | #ifdef FEAT_FLOAT |
| 609 | to->vval.v_float = from->vval.v_float; |
| 610 | break; |
| 611 | #endif |
| 612 | case VAR_JOB: |
| 613 | #ifdef FEAT_JOB_CHANNEL |
| 614 | to->vval.v_job = from->vval.v_job; |
| 615 | if (to->vval.v_job != NULL) |
| 616 | ++to->vval.v_job->jv_refcount; |
| 617 | break; |
| 618 | #endif |
| 619 | case VAR_CHANNEL: |
| 620 | #ifdef FEAT_JOB_CHANNEL |
| 621 | to->vval.v_channel = from->vval.v_channel; |
| 622 | if (to->vval.v_channel != NULL) |
| 623 | ++to->vval.v_channel->ch_refcount; |
| 624 | break; |
| 625 | #endif |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 626 | case VAR_INSTR: |
| 627 | to->vval.v_instr = from->vval.v_instr; |
| 628 | break; |
| 629 | |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 630 | case VAR_STRING: |
| 631 | case VAR_FUNC: |
| 632 | if (from->vval.v_string == NULL) |
| 633 | to->vval.v_string = NULL; |
| 634 | else |
| 635 | { |
| 636 | to->vval.v_string = vim_strsave(from->vval.v_string); |
| 637 | if (from->v_type == VAR_FUNC) |
| 638 | func_ref(to->vval.v_string); |
| 639 | } |
| 640 | break; |
| 641 | case VAR_PARTIAL: |
| 642 | if (from->vval.v_partial == NULL) |
| 643 | to->vval.v_partial = NULL; |
| 644 | else |
| 645 | { |
| 646 | to->vval.v_partial = from->vval.v_partial; |
| 647 | ++to->vval.v_partial->pt_refcount; |
| 648 | } |
| 649 | break; |
| 650 | case VAR_BLOB: |
| 651 | if (from->vval.v_blob == NULL) |
| 652 | to->vval.v_blob = NULL; |
| 653 | else |
| 654 | { |
| 655 | to->vval.v_blob = from->vval.v_blob; |
| 656 | ++to->vval.v_blob->bv_refcount; |
| 657 | } |
| 658 | break; |
| 659 | case VAR_LIST: |
| 660 | if (from->vval.v_list == NULL) |
| 661 | to->vval.v_list = NULL; |
| 662 | else |
| 663 | { |
| 664 | to->vval.v_list = from->vval.v_list; |
| 665 | ++to->vval.v_list->lv_refcount; |
| 666 | } |
| 667 | break; |
| 668 | case VAR_DICT: |
| 669 | if (from->vval.v_dict == NULL) |
| 670 | to->vval.v_dict = NULL; |
| 671 | else |
| 672 | { |
| 673 | to->vval.v_dict = from->vval.v_dict; |
| 674 | ++to->vval.v_dict->dv_refcount; |
| 675 | } |
| 676 | break; |
| 677 | case VAR_UNKNOWN: |
| 678 | case VAR_ANY: |
| 679 | case VAR_VOID: |
| 680 | internal_error_no_abort("copy_tv(UNKNOWN)"); |
| 681 | break; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | /* |
| 686 | * Compare "typ1" and "typ2". Put the result in "typ1". |
| 687 | */ |
| 688 | int |
| 689 | typval_compare( |
| 690 | typval_T *typ1, // first operand |
| 691 | typval_T *typ2, // second operand |
Bram Moolenaar | 657137c | 2021-01-09 15:45:23 +0100 | [diff] [blame] | 692 | exprtype_T type, // operator |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 693 | int ic) // ignore case |
| 694 | { |
| 695 | int i; |
| 696 | varnumber_T n1, n2; |
| 697 | char_u *s1, *s2; |
| 698 | char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN]; |
| 699 | int type_is = type == EXPR_IS || type == EXPR_ISNOT; |
| 700 | |
| 701 | if (type_is && typ1->v_type != typ2->v_type) |
| 702 | { |
| 703 | // For "is" a different type always means FALSE, for "notis" |
| 704 | // it means TRUE. |
| 705 | n1 = (type == EXPR_ISNOT); |
| 706 | } |
| 707 | else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB) |
| 708 | { |
| 709 | if (type_is) |
| 710 | { |
| 711 | n1 = (typ1->v_type == typ2->v_type |
| 712 | && typ1->vval.v_blob == typ2->vval.v_blob); |
| 713 | if (type == EXPR_ISNOT) |
| 714 | n1 = !n1; |
| 715 | } |
| 716 | else if (typ1->v_type != typ2->v_type |
| 717 | || (type != EXPR_EQUAL && type != EXPR_NEQUAL)) |
| 718 | { |
| 719 | if (typ1->v_type != typ2->v_type) |
| 720 | emsg(_("E977: Can only compare Blob with Blob")); |
| 721 | else |
| 722 | emsg(_(e_invalblob)); |
| 723 | clear_tv(typ1); |
| 724 | return FAIL; |
| 725 | } |
| 726 | else |
| 727 | { |
| 728 | // Compare two Blobs for being equal or unequal. |
| 729 | n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob); |
| 730 | if (type == EXPR_NEQUAL) |
| 731 | n1 = !n1; |
| 732 | } |
| 733 | } |
| 734 | else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST) |
| 735 | { |
| 736 | if (type_is) |
| 737 | { |
| 738 | n1 = (typ1->v_type == typ2->v_type |
| 739 | && typ1->vval.v_list == typ2->vval.v_list); |
| 740 | if (type == EXPR_ISNOT) |
| 741 | n1 = !n1; |
| 742 | } |
| 743 | else if (typ1->v_type != typ2->v_type |
| 744 | || (type != EXPR_EQUAL && type != EXPR_NEQUAL)) |
| 745 | { |
| 746 | if (typ1->v_type != typ2->v_type) |
| 747 | emsg(_("E691: Can only compare List with List")); |
| 748 | else |
| 749 | emsg(_("E692: Invalid operation for List")); |
| 750 | clear_tv(typ1); |
| 751 | return FAIL; |
| 752 | } |
| 753 | else |
| 754 | { |
| 755 | // Compare two Lists for being equal or unequal. |
| 756 | n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list, |
| 757 | ic, FALSE); |
| 758 | if (type == EXPR_NEQUAL) |
| 759 | n1 = !n1; |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT) |
| 764 | { |
| 765 | if (type_is) |
| 766 | { |
| 767 | n1 = (typ1->v_type == typ2->v_type |
| 768 | && typ1->vval.v_dict == typ2->vval.v_dict); |
| 769 | if (type == EXPR_ISNOT) |
| 770 | n1 = !n1; |
| 771 | } |
| 772 | else if (typ1->v_type != typ2->v_type |
| 773 | || (type != EXPR_EQUAL && type != EXPR_NEQUAL)) |
| 774 | { |
| 775 | if (typ1->v_type != typ2->v_type) |
| 776 | emsg(_("E735: Can only compare Dictionary with Dictionary")); |
| 777 | else |
| 778 | emsg(_("E736: Invalid operation for Dictionary")); |
| 779 | clear_tv(typ1); |
| 780 | return FAIL; |
| 781 | } |
| 782 | else |
| 783 | { |
| 784 | // Compare two Dictionaries for being equal or unequal. |
| 785 | n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict, |
| 786 | ic, FALSE); |
| 787 | if (type == EXPR_NEQUAL) |
| 788 | n1 = !n1; |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC |
| 793 | || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL) |
| 794 | { |
| 795 | if (type != EXPR_EQUAL && type != EXPR_NEQUAL |
| 796 | && type != EXPR_IS && type != EXPR_ISNOT) |
| 797 | { |
| 798 | emsg(_("E694: Invalid operation for Funcrefs")); |
| 799 | clear_tv(typ1); |
| 800 | return FAIL; |
| 801 | } |
| 802 | if ((typ1->v_type == VAR_PARTIAL |
| 803 | && typ1->vval.v_partial == NULL) |
| 804 | || (typ2->v_type == VAR_PARTIAL |
| 805 | && typ2->vval.v_partial == NULL)) |
| 806 | // When both partials are NULL, then they are equal. |
| 807 | // Otherwise they are not equal. |
| 808 | n1 = (typ1->vval.v_partial == typ2->vval.v_partial); |
| 809 | else if (type_is) |
| 810 | { |
| 811 | if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC) |
| 812 | // strings are considered the same if their value is |
| 813 | // the same |
| 814 | n1 = tv_equal(typ1, typ2, ic, FALSE); |
| 815 | else if (typ1->v_type == VAR_PARTIAL |
| 816 | && typ2->v_type == VAR_PARTIAL) |
| 817 | n1 = (typ1->vval.v_partial == typ2->vval.v_partial); |
| 818 | else |
| 819 | n1 = FALSE; |
| 820 | } |
| 821 | else |
| 822 | n1 = tv_equal(typ1, typ2, ic, FALSE); |
| 823 | if (type == EXPR_NEQUAL || type == EXPR_ISNOT) |
| 824 | n1 = !n1; |
| 825 | } |
| 826 | |
| 827 | #ifdef FEAT_FLOAT |
| 828 | // If one of the two variables is a float, compare as a float. |
| 829 | // When using "=~" or "!~", always compare as string. |
| 830 | else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT) |
| 831 | && type != EXPR_MATCH && type != EXPR_NOMATCH) |
| 832 | { |
| 833 | float_T f1, f2; |
| 834 | |
| 835 | f1 = tv_get_float(typ1); |
| 836 | f2 = tv_get_float(typ2); |
| 837 | n1 = FALSE; |
| 838 | switch (type) |
| 839 | { |
| 840 | case EXPR_IS: |
| 841 | case EXPR_EQUAL: n1 = (f1 == f2); break; |
| 842 | case EXPR_ISNOT: |
| 843 | case EXPR_NEQUAL: n1 = (f1 != f2); break; |
| 844 | case EXPR_GREATER: n1 = (f1 > f2); break; |
| 845 | case EXPR_GEQUAL: n1 = (f1 >= f2); break; |
| 846 | case EXPR_SMALLER: n1 = (f1 < f2); break; |
| 847 | case EXPR_SEQUAL: n1 = (f1 <= f2); break; |
| 848 | case EXPR_UNKNOWN: |
| 849 | case EXPR_MATCH: |
| 850 | default: break; // avoid gcc warning |
| 851 | } |
| 852 | } |
| 853 | #endif |
| 854 | |
| 855 | // If one of the two variables is a number, compare as a number. |
| 856 | // When using "=~" or "!~", always compare as string. |
| 857 | else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER) |
| 858 | && type != EXPR_MATCH && type != EXPR_NOMATCH) |
| 859 | { |
| 860 | n1 = tv_get_number(typ1); |
| 861 | n2 = tv_get_number(typ2); |
| 862 | switch (type) |
| 863 | { |
| 864 | case EXPR_IS: |
| 865 | case EXPR_EQUAL: n1 = (n1 == n2); break; |
| 866 | case EXPR_ISNOT: |
| 867 | case EXPR_NEQUAL: n1 = (n1 != n2); break; |
| 868 | case EXPR_GREATER: n1 = (n1 > n2); break; |
| 869 | case EXPR_GEQUAL: n1 = (n1 >= n2); break; |
| 870 | case EXPR_SMALLER: n1 = (n1 < n2); break; |
| 871 | case EXPR_SEQUAL: n1 = (n1 <= n2); break; |
| 872 | case EXPR_UNKNOWN: |
| 873 | case EXPR_MATCH: |
| 874 | default: break; // avoid gcc warning |
| 875 | } |
| 876 | } |
Bram Moolenaar | cff40ff | 2021-01-09 16:21:37 +0100 | [diff] [blame] | 877 | else if (in_vim9script() && (typ1->v_type == VAR_BOOL |
| 878 | || typ2->v_type == VAR_BOOL)) |
| 879 | { |
| 880 | if (typ1->v_type != typ2->v_type) |
| 881 | { |
| 882 | semsg(_(e_cannot_compare_str_with_str), |
| 883 | vartype_name(typ1->v_type), vartype_name(typ2->v_type)); |
| 884 | clear_tv(typ1); |
| 885 | return FAIL; |
| 886 | } |
| 887 | n1 = typ1->vval.v_number; |
| 888 | n2 = typ2->vval.v_number; |
| 889 | switch (type) |
| 890 | { |
| 891 | case EXPR_IS: |
| 892 | case EXPR_EQUAL: n1 = (n1 == n2); break; |
| 893 | case EXPR_ISNOT: |
| 894 | case EXPR_NEQUAL: n1 = (n1 != n2); break; |
| 895 | default: |
| 896 | emsg(_(e_invalid_operation_for_bool)); |
| 897 | clear_tv(typ1); |
| 898 | return FAIL; |
| 899 | } |
| 900 | } |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 901 | else |
| 902 | { |
| 903 | s1 = tv_get_string_buf(typ1, buf1); |
| 904 | s2 = tv_get_string_buf(typ2, buf2); |
| 905 | if (type != EXPR_MATCH && type != EXPR_NOMATCH) |
| 906 | i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2); |
| 907 | else |
| 908 | i = 0; |
| 909 | n1 = FALSE; |
| 910 | switch (type) |
| 911 | { |
| 912 | case EXPR_IS: |
| 913 | case EXPR_EQUAL: n1 = (i == 0); break; |
| 914 | case EXPR_ISNOT: |
| 915 | case EXPR_NEQUAL: n1 = (i != 0); break; |
| 916 | case EXPR_GREATER: n1 = (i > 0); break; |
| 917 | case EXPR_GEQUAL: n1 = (i >= 0); break; |
| 918 | case EXPR_SMALLER: n1 = (i < 0); break; |
| 919 | case EXPR_SEQUAL: n1 = (i <= 0); break; |
| 920 | |
| 921 | case EXPR_MATCH: |
| 922 | case EXPR_NOMATCH: |
| 923 | n1 = pattern_match(s2, s1, ic); |
| 924 | if (type == EXPR_NOMATCH) |
| 925 | n1 = !n1; |
| 926 | break; |
| 927 | |
| 928 | default: break; // avoid gcc warning |
| 929 | } |
| 930 | } |
| 931 | clear_tv(typ1); |
Bram Moolenaar | c71f36a | 2020-07-21 21:31:00 +0200 | [diff] [blame] | 932 | if (in_vim9script()) |
| 933 | { |
| 934 | typ1->v_type = VAR_BOOL; |
| 935 | typ1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE; |
| 936 | } |
| 937 | else |
| 938 | { |
| 939 | typ1->v_type = VAR_NUMBER; |
| 940 | typ1->vval.v_number = n1; |
| 941 | } |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 942 | |
| 943 | return OK; |
| 944 | } |
| 945 | |
Bram Moolenaar | 3445320 | 2021-01-31 13:08:38 +0100 | [diff] [blame] | 946 | /* |
| 947 | * Convert any type to a string, never give an error. |
| 948 | * When "quotes" is TRUE add quotes to a string. |
| 949 | * Returns an allocated string. |
| 950 | */ |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 951 | char_u * |
Bram Moolenaar | 3445320 | 2021-01-31 13:08:38 +0100 | [diff] [blame] | 952 | typval_tostring(typval_T *arg, int quotes) |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 953 | { |
| 954 | char_u *tofree; |
| 955 | char_u numbuf[NUMBUFLEN]; |
| 956 | char_u *ret = NULL; |
| 957 | |
| 958 | if (arg == NULL) |
| 959 | return vim_strsave((char_u *)"(does not exist)"); |
Bram Moolenaar | 3445320 | 2021-01-31 13:08:38 +0100 | [diff] [blame] | 960 | if (!quotes && arg->v_type == VAR_STRING) |
| 961 | { |
| 962 | ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)"" |
| 963 | : arg->vval.v_string); |
| 964 | } |
| 965 | else |
| 966 | { |
| 967 | ret = tv2string(arg, &tofree, numbuf, 0); |
| 968 | // Make a copy if we have a value but it's not in allocated memory. |
| 969 | if (ret != NULL && tofree == NULL) |
| 970 | ret = vim_strsave(ret); |
| 971 | } |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 972 | return ret; |
| 973 | } |
| 974 | |
| 975 | /* |
| 976 | * Return TRUE if typeval "tv" is locked: Either that value is locked itself |
| 977 | * or it refers to a List or Dictionary that is locked. |
| 978 | */ |
| 979 | int |
| 980 | tv_islocked(typval_T *tv) |
| 981 | { |
| 982 | return (tv->v_lock & VAR_LOCKED) |
| 983 | || (tv->v_type == VAR_LIST |
| 984 | && tv->vval.v_list != NULL |
| 985 | && (tv->vval.v_list->lv_lock & VAR_LOCKED)) |
| 986 | || (tv->v_type == VAR_DICT |
| 987 | && tv->vval.v_dict != NULL |
| 988 | && (tv->vval.v_dict->dv_lock & VAR_LOCKED)); |
| 989 | } |
| 990 | |
| 991 | static int |
| 992 | func_equal( |
| 993 | typval_T *tv1, |
| 994 | typval_T *tv2, |
| 995 | int ic) // ignore case |
| 996 | { |
| 997 | char_u *s1, *s2; |
| 998 | dict_T *d1, *d2; |
| 999 | int a1, a2; |
| 1000 | int i; |
| 1001 | |
| 1002 | // empty and NULL function name considered the same |
| 1003 | s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string |
| 1004 | : partial_name(tv1->vval.v_partial); |
| 1005 | if (s1 != NULL && *s1 == NUL) |
| 1006 | s1 = NULL; |
| 1007 | s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string |
| 1008 | : partial_name(tv2->vval.v_partial); |
| 1009 | if (s2 != NULL && *s2 == NUL) |
| 1010 | s2 = NULL; |
| 1011 | if (s1 == NULL || s2 == NULL) |
| 1012 | { |
| 1013 | if (s1 != s2) |
| 1014 | return FALSE; |
| 1015 | } |
| 1016 | else if (STRCMP(s1, s2) != 0) |
| 1017 | return FALSE; |
| 1018 | |
| 1019 | // empty dict and NULL dict is different |
| 1020 | d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict; |
| 1021 | d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict; |
| 1022 | if (d1 == NULL || d2 == NULL) |
| 1023 | { |
| 1024 | if (d1 != d2) |
| 1025 | return FALSE; |
| 1026 | } |
| 1027 | else if (!dict_equal(d1, d2, ic, TRUE)) |
| 1028 | return FALSE; |
| 1029 | |
| 1030 | // empty list and no list considered the same |
| 1031 | a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc; |
| 1032 | a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc; |
| 1033 | if (a1 != a2) |
| 1034 | return FALSE; |
| 1035 | for (i = 0; i < a1; ++i) |
| 1036 | if (!tv_equal(tv1->vval.v_partial->pt_argv + i, |
| 1037 | tv2->vval.v_partial->pt_argv + i, ic, TRUE)) |
| 1038 | return FALSE; |
| 1039 | |
| 1040 | return TRUE; |
| 1041 | } |
| 1042 | |
| 1043 | /* |
| 1044 | * Return TRUE if "tv1" and "tv2" have the same value. |
| 1045 | * Compares the items just like "==" would compare them, but strings and |
| 1046 | * numbers are different. Floats and numbers are also different. |
| 1047 | */ |
| 1048 | int |
| 1049 | tv_equal( |
| 1050 | typval_T *tv1, |
| 1051 | typval_T *tv2, |
| 1052 | int ic, // ignore case |
| 1053 | int recursive) // TRUE when used recursively |
| 1054 | { |
| 1055 | char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN]; |
| 1056 | char_u *s1, *s2; |
| 1057 | static int recursive_cnt = 0; // catch recursive loops |
| 1058 | int r; |
| 1059 | static int tv_equal_recurse_limit; |
| 1060 | |
| 1061 | // Catch lists and dicts that have an endless loop by limiting |
| 1062 | // recursiveness to a limit. We guess they are equal then. |
| 1063 | // A fixed limit has the problem of still taking an awful long time. |
| 1064 | // Reduce the limit every time running into it. That should work fine for |
| 1065 | // deeply linked structures that are not recursively linked and catch |
| 1066 | // recursiveness quickly. |
| 1067 | if (!recursive) |
| 1068 | tv_equal_recurse_limit = 1000; |
| 1069 | if (recursive_cnt >= tv_equal_recurse_limit) |
| 1070 | { |
| 1071 | --tv_equal_recurse_limit; |
| 1072 | return TRUE; |
| 1073 | } |
| 1074 | |
| 1075 | // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and |
| 1076 | // arguments. |
| 1077 | if ((tv1->v_type == VAR_FUNC |
| 1078 | || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL)) |
| 1079 | && (tv2->v_type == VAR_FUNC |
| 1080 | || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL))) |
| 1081 | { |
| 1082 | ++recursive_cnt; |
| 1083 | r = func_equal(tv1, tv2, ic); |
| 1084 | --recursive_cnt; |
| 1085 | return r; |
| 1086 | } |
| 1087 | |
Bram Moolenaar | 418a29f | 2021-02-10 22:23:41 +0100 | [diff] [blame] | 1088 | if (tv1->v_type != tv2->v_type |
| 1089 | && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL) |
| 1090 | || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL))) |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1091 | return FALSE; |
| 1092 | |
| 1093 | switch (tv1->v_type) |
| 1094 | { |
| 1095 | case VAR_LIST: |
| 1096 | ++recursive_cnt; |
| 1097 | r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE); |
| 1098 | --recursive_cnt; |
| 1099 | return r; |
| 1100 | |
| 1101 | case VAR_DICT: |
| 1102 | ++recursive_cnt; |
| 1103 | r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE); |
| 1104 | --recursive_cnt; |
| 1105 | return r; |
| 1106 | |
| 1107 | case VAR_BLOB: |
| 1108 | return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob); |
| 1109 | |
| 1110 | case VAR_NUMBER: |
| 1111 | case VAR_BOOL: |
| 1112 | case VAR_SPECIAL: |
| 1113 | return tv1->vval.v_number == tv2->vval.v_number; |
| 1114 | |
| 1115 | case VAR_STRING: |
| 1116 | s1 = tv_get_string_buf(tv1, buf1); |
| 1117 | s2 = tv_get_string_buf(tv2, buf2); |
| 1118 | return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0); |
| 1119 | |
| 1120 | case VAR_FLOAT: |
| 1121 | #ifdef FEAT_FLOAT |
| 1122 | return tv1->vval.v_float == tv2->vval.v_float; |
| 1123 | #endif |
| 1124 | case VAR_JOB: |
| 1125 | #ifdef FEAT_JOB_CHANNEL |
| 1126 | return tv1->vval.v_job == tv2->vval.v_job; |
| 1127 | #endif |
| 1128 | case VAR_CHANNEL: |
| 1129 | #ifdef FEAT_JOB_CHANNEL |
| 1130 | return tv1->vval.v_channel == tv2->vval.v_channel; |
| 1131 | #endif |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 1132 | case VAR_INSTR: |
| 1133 | return tv1->vval.v_instr == tv2->vval.v_instr; |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1134 | |
| 1135 | case VAR_PARTIAL: |
| 1136 | return tv1->vval.v_partial == tv2->vval.v_partial; |
| 1137 | |
| 1138 | case VAR_FUNC: |
| 1139 | return tv1->vval.v_string == tv2->vval.v_string; |
| 1140 | |
| 1141 | case VAR_UNKNOWN: |
| 1142 | case VAR_ANY: |
| 1143 | case VAR_VOID: |
| 1144 | break; |
| 1145 | } |
| 1146 | |
| 1147 | // VAR_UNKNOWN can be the result of a invalid expression, let's say it |
| 1148 | // does not equal anything, not even itself. |
| 1149 | return FALSE; |
| 1150 | } |
| 1151 | |
| 1152 | /* |
| 1153 | * Get an option value. |
| 1154 | * "arg" points to the '&' or '+' before the option name. |
| 1155 | * "arg" is advanced to character after the option name. |
| 1156 | * Return OK or FAIL. |
| 1157 | */ |
| 1158 | int |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 1159 | eval_option( |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1160 | char_u **arg, |
| 1161 | typval_T *rettv, // when NULL, only check if option exists |
| 1162 | int evaluate) |
| 1163 | { |
| 1164 | char_u *option_end; |
| 1165 | long numval; |
| 1166 | char_u *stringval; |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 1167 | getoption_T opt_type; |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1168 | int c; |
| 1169 | int working = (**arg == '+'); // has("+option") |
| 1170 | int ret = OK; |
| 1171 | int opt_flags; |
| 1172 | |
| 1173 | // Isolate the option name and find its value. |
| 1174 | option_end = find_option_end(arg, &opt_flags); |
| 1175 | if (option_end == NULL) |
| 1176 | { |
| 1177 | if (rettv != NULL) |
| 1178 | semsg(_("E112: Option name missing: %s"), *arg); |
| 1179 | return FAIL; |
| 1180 | } |
| 1181 | |
| 1182 | if (!evaluate) |
| 1183 | { |
| 1184 | *arg = option_end; |
| 1185 | return OK; |
| 1186 | } |
| 1187 | |
| 1188 | c = *option_end; |
| 1189 | *option_end = NUL; |
| 1190 | opt_type = get_option_value(*arg, &numval, |
| 1191 | rettv == NULL ? NULL : &stringval, opt_flags); |
| 1192 | |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 1193 | if (opt_type == gov_unknown) |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1194 | { |
| 1195 | if (rettv != NULL) |
| 1196 | semsg(_(e_unknown_option), *arg); |
| 1197 | ret = FAIL; |
| 1198 | } |
| 1199 | else if (rettv != NULL) |
| 1200 | { |
Bram Moolenaar | a79925a | 2021-01-05 17:50:28 +0100 | [diff] [blame] | 1201 | rettv->v_lock = 0; |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 1202 | if (opt_type == gov_hidden_string) |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1203 | { |
| 1204 | rettv->v_type = VAR_STRING; |
| 1205 | rettv->vval.v_string = NULL; |
| 1206 | } |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 1207 | else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number) |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1208 | { |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 1209 | rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool |
| 1210 | ? VAR_BOOL : VAR_NUMBER; |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1211 | rettv->vval.v_number = 0; |
| 1212 | } |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 1213 | else if (opt_type == gov_bool || opt_type == gov_number) |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1214 | { |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 1215 | if (in_vim9script() && opt_type == gov_bool) |
| 1216 | { |
| 1217 | rettv->v_type = VAR_BOOL; |
| 1218 | rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE; |
| 1219 | } |
| 1220 | else |
| 1221 | { |
| 1222 | rettv->v_type = VAR_NUMBER; |
| 1223 | rettv->vval.v_number = numval; |
| 1224 | } |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1225 | } |
| 1226 | else // string option |
| 1227 | { |
| 1228 | rettv->v_type = VAR_STRING; |
| 1229 | rettv->vval.v_string = stringval; |
| 1230 | } |
| 1231 | } |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 1232 | else if (working && (opt_type == gov_hidden_bool |
| 1233 | || opt_type == gov_hidden_number |
| 1234 | || opt_type == gov_hidden_string)) |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1235 | ret = FAIL; |
| 1236 | |
| 1237 | *option_end = c; // put back for error messages |
| 1238 | *arg = option_end; |
| 1239 | |
| 1240 | return ret; |
| 1241 | } |
| 1242 | |
| 1243 | /* |
| 1244 | * Allocate a variable for a number constant. Also deals with "0z" for blob. |
| 1245 | * Return OK or FAIL. |
| 1246 | */ |
| 1247 | int |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 1248 | eval_number( |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1249 | char_u **arg, |
| 1250 | typval_T *rettv, |
| 1251 | int evaluate, |
| 1252 | int want_string UNUSED) |
| 1253 | { |
| 1254 | int len; |
| 1255 | #ifdef FEAT_FLOAT |
| 1256 | char_u *p; |
| 1257 | int get_float = FALSE; |
| 1258 | |
| 1259 | // We accept a float when the format matches |
| 1260 | // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very |
| 1261 | // strict to avoid backwards compatibility problems. |
| 1262 | // With script version 2 and later the leading digit can be |
| 1263 | // omitted. |
| 1264 | // Don't look for a float after the "." operator, so that |
| 1265 | // ":let vers = 1.2.3" doesn't fail. |
| 1266 | if (**arg == '.') |
| 1267 | p = *arg; |
| 1268 | else |
| 1269 | p = skipdigits(*arg + 1); |
| 1270 | if (!want_string && p[0] == '.' && vim_isdigit(p[1])) |
| 1271 | { |
| 1272 | get_float = TRUE; |
| 1273 | p = skipdigits(p + 2); |
| 1274 | if (*p == 'e' || *p == 'E') |
| 1275 | { |
| 1276 | ++p; |
| 1277 | if (*p == '-' || *p == '+') |
| 1278 | ++p; |
| 1279 | if (!vim_isdigit(*p)) |
| 1280 | get_float = FALSE; |
| 1281 | else |
| 1282 | p = skipdigits(p + 1); |
| 1283 | } |
| 1284 | if (ASCII_ISALPHA(*p) || *p == '.') |
| 1285 | get_float = FALSE; |
| 1286 | } |
| 1287 | if (get_float) |
| 1288 | { |
| 1289 | float_T f; |
| 1290 | |
| 1291 | *arg += string2float(*arg, &f); |
| 1292 | if (evaluate) |
| 1293 | { |
| 1294 | rettv->v_type = VAR_FLOAT; |
| 1295 | rettv->vval.v_float = f; |
| 1296 | } |
| 1297 | } |
| 1298 | else |
| 1299 | #endif |
| 1300 | if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z')) |
| 1301 | { |
| 1302 | char_u *bp; |
| 1303 | blob_T *blob = NULL; // init for gcc |
| 1304 | |
| 1305 | // Blob constant: 0z0123456789abcdef |
| 1306 | if (evaluate) |
| 1307 | blob = blob_alloc(); |
| 1308 | for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2) |
| 1309 | { |
| 1310 | if (!vim_isxdigit(bp[1])) |
| 1311 | { |
| 1312 | if (blob != NULL) |
| 1313 | { |
| 1314 | emsg(_("E973: Blob literal should have an even number of hex characters")); |
| 1315 | ga_clear(&blob->bv_ga); |
| 1316 | VIM_CLEAR(blob); |
| 1317 | } |
| 1318 | return FAIL; |
| 1319 | } |
| 1320 | if (blob != NULL) |
| 1321 | ga_append(&blob->bv_ga, |
| 1322 | (hex2nr(*bp) << 4) + hex2nr(*(bp+1))); |
| 1323 | if (bp[2] == '.' && vim_isxdigit(bp[3])) |
| 1324 | ++bp; |
| 1325 | } |
| 1326 | if (blob != NULL) |
| 1327 | rettv_blob_set(rettv, blob); |
| 1328 | *arg = bp; |
| 1329 | } |
| 1330 | else |
| 1331 | { |
| 1332 | varnumber_T n; |
| 1333 | |
| 1334 | // decimal, hex or octal number |
| 1335 | vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4 |
| 1336 | ? STR2NR_NO_OCT + STR2NR_QUOTE |
| 1337 | : STR2NR_ALL, &n, NULL, 0, TRUE); |
| 1338 | if (len == 0) |
| 1339 | { |
| 1340 | semsg(_(e_invexpr2), *arg); |
| 1341 | return FAIL; |
| 1342 | } |
| 1343 | *arg += len; |
| 1344 | if (evaluate) |
| 1345 | { |
| 1346 | rettv->v_type = VAR_NUMBER; |
| 1347 | rettv->vval.v_number = n; |
| 1348 | } |
| 1349 | } |
| 1350 | return OK; |
| 1351 | } |
| 1352 | |
| 1353 | /* |
| 1354 | * Allocate a variable for a string constant. |
| 1355 | * Return OK or FAIL. |
| 1356 | */ |
| 1357 | int |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 1358 | eval_string(char_u **arg, typval_T *rettv, int evaluate) |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1359 | { |
| 1360 | char_u *p; |
Bram Moolenaar | 1e0b7b1 | 2020-06-19 19:30:53 +0200 | [diff] [blame] | 1361 | char_u *end; |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1362 | int extra = 0; |
| 1363 | int len; |
| 1364 | |
| 1365 | // Find the end of the string, skipping backslashed characters. |
| 1366 | for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p)) |
| 1367 | { |
| 1368 | if (*p == '\\' && p[1] != NUL) |
| 1369 | { |
| 1370 | ++p; |
| 1371 | // A "\<x>" form occupies at least 4 characters, and produces up |
| 1372 | // to 21 characters (3 * 6 for the char and 3 for a modifier): |
| 1373 | // reserve space for 18 extra. |
| 1374 | // Each byte in the char could be encoded as K_SPECIAL K_EXTRA x. |
| 1375 | if (*p == '<') |
| 1376 | extra += 18; |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | if (*p != '"') |
| 1381 | { |
| 1382 | semsg(_("E114: Missing quote: %s"), *arg); |
| 1383 | return FAIL; |
| 1384 | } |
| 1385 | |
| 1386 | // If only parsing, set *arg and return here |
| 1387 | if (!evaluate) |
| 1388 | { |
| 1389 | *arg = p + 1; |
| 1390 | return OK; |
| 1391 | } |
| 1392 | |
| 1393 | // Copy the string into allocated memory, handling backslashed |
| 1394 | // characters. |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1395 | rettv->v_type = VAR_STRING; |
Bram Moolenaar | 1e0b7b1 | 2020-06-19 19:30:53 +0200 | [diff] [blame] | 1396 | len = (int)(p - *arg + extra); |
| 1397 | rettv->vval.v_string = alloc(len); |
| 1398 | if (rettv->vval.v_string == NULL) |
| 1399 | return FAIL; |
| 1400 | end = rettv->vval.v_string; |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1401 | |
| 1402 | for (p = *arg + 1; *p != NUL && *p != '"'; ) |
| 1403 | { |
| 1404 | if (*p == '\\') |
| 1405 | { |
| 1406 | switch (*++p) |
| 1407 | { |
Bram Moolenaar | 1e0b7b1 | 2020-06-19 19:30:53 +0200 | [diff] [blame] | 1408 | case 'b': *end++ = BS; ++p; break; |
| 1409 | case 'e': *end++ = ESC; ++p; break; |
| 1410 | case 'f': *end++ = FF; ++p; break; |
| 1411 | case 'n': *end++ = NL; ++p; break; |
| 1412 | case 'r': *end++ = CAR; ++p; break; |
| 1413 | case 't': *end++ = TAB; ++p; break; |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1414 | |
| 1415 | case 'X': // hex: "\x1", "\x12" |
| 1416 | case 'x': |
| 1417 | case 'u': // Unicode: "\u0023" |
| 1418 | case 'U': |
| 1419 | if (vim_isxdigit(p[1])) |
| 1420 | { |
| 1421 | int n, nr; |
| 1422 | int c = toupper(*p); |
| 1423 | |
| 1424 | if (c == 'X') |
| 1425 | n = 2; |
| 1426 | else if (*p == 'u') |
| 1427 | n = 4; |
| 1428 | else |
| 1429 | n = 8; |
| 1430 | nr = 0; |
| 1431 | while (--n >= 0 && vim_isxdigit(p[1])) |
| 1432 | { |
| 1433 | ++p; |
| 1434 | nr = (nr << 4) + hex2nr(*p); |
| 1435 | } |
| 1436 | ++p; |
| 1437 | // For "\u" store the number according to |
| 1438 | // 'encoding'. |
| 1439 | if (c != 'X') |
Bram Moolenaar | 1e0b7b1 | 2020-06-19 19:30:53 +0200 | [diff] [blame] | 1440 | end += (*mb_char2bytes)(nr, end); |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1441 | else |
Bram Moolenaar | 1e0b7b1 | 2020-06-19 19:30:53 +0200 | [diff] [blame] | 1442 | *end++ = nr; |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1443 | } |
| 1444 | break; |
| 1445 | |
| 1446 | // octal: "\1", "\12", "\123" |
| 1447 | case '0': |
| 1448 | case '1': |
| 1449 | case '2': |
| 1450 | case '3': |
| 1451 | case '4': |
| 1452 | case '5': |
| 1453 | case '6': |
Bram Moolenaar | 1e0b7b1 | 2020-06-19 19:30:53 +0200 | [diff] [blame] | 1454 | case '7': *end = *p++ - '0'; |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1455 | if (*p >= '0' && *p <= '7') |
| 1456 | { |
Bram Moolenaar | 1e0b7b1 | 2020-06-19 19:30:53 +0200 | [diff] [blame] | 1457 | *end = (*end << 3) + *p++ - '0'; |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1458 | if (*p >= '0' && *p <= '7') |
Bram Moolenaar | 1e0b7b1 | 2020-06-19 19:30:53 +0200 | [diff] [blame] | 1459 | *end = (*end << 3) + *p++ - '0'; |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1460 | } |
Bram Moolenaar | 1e0b7b1 | 2020-06-19 19:30:53 +0200 | [diff] [blame] | 1461 | ++end; |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1462 | break; |
| 1463 | |
Bram Moolenaar | fccd93f | 2020-05-31 22:06:51 +0200 | [diff] [blame] | 1464 | // Special key, e.g.: "\<C-W>" |
Bram Moolenaar | ebe9d34 | 2020-05-30 21:52:54 +0200 | [diff] [blame] | 1465 | case '<': |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1466 | { |
Bram Moolenaar | ebe9d34 | 2020-05-30 21:52:54 +0200 | [diff] [blame] | 1467 | int flags = FSK_KEYCODE | FSK_IN_STRING; |
| 1468 | |
Bram Moolenaar | fccd93f | 2020-05-31 22:06:51 +0200 | [diff] [blame] | 1469 | if (p[1] != '*') |
Bram Moolenaar | ebe9d34 | 2020-05-30 21:52:54 +0200 | [diff] [blame] | 1470 | flags |= FSK_SIMPLIFY; |
Bram Moolenaar | 1e0b7b1 | 2020-06-19 19:30:53 +0200 | [diff] [blame] | 1471 | extra = trans_special(&p, end, flags, NULL); |
Bram Moolenaar | ebe9d34 | 2020-05-30 21:52:54 +0200 | [diff] [blame] | 1472 | if (extra != 0) |
| 1473 | { |
Bram Moolenaar | 1e0b7b1 | 2020-06-19 19:30:53 +0200 | [diff] [blame] | 1474 | end += extra; |
| 1475 | if (end >= rettv->vval.v_string + len) |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 1476 | iemsg("eval_string() used more space than allocated"); |
Bram Moolenaar | ebe9d34 | 2020-05-30 21:52:54 +0200 | [diff] [blame] | 1477 | break; |
| 1478 | } |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1479 | } |
| 1480 | // FALLTHROUGH |
| 1481 | |
Bram Moolenaar | 1e0b7b1 | 2020-06-19 19:30:53 +0200 | [diff] [blame] | 1482 | default: MB_COPY_CHAR(p, end); |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1483 | break; |
| 1484 | } |
| 1485 | } |
| 1486 | else |
Bram Moolenaar | 1e0b7b1 | 2020-06-19 19:30:53 +0200 | [diff] [blame] | 1487 | MB_COPY_CHAR(p, end); |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1488 | } |
Bram Moolenaar | 1e0b7b1 | 2020-06-19 19:30:53 +0200 | [diff] [blame] | 1489 | *end = NUL; |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1490 | if (*p != NUL) // just in case |
| 1491 | ++p; |
| 1492 | *arg = p; |
| 1493 | |
| 1494 | return OK; |
| 1495 | } |
| 1496 | |
| 1497 | /* |
| 1498 | * Allocate a variable for a 'str''ing' constant. |
| 1499 | * Return OK or FAIL. |
| 1500 | */ |
| 1501 | int |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 1502 | eval_lit_string(char_u **arg, typval_T *rettv, int evaluate) |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1503 | { |
| 1504 | char_u *p; |
| 1505 | char_u *str; |
| 1506 | int reduce = 0; |
| 1507 | |
| 1508 | // Find the end of the string, skipping ''. |
| 1509 | for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p)) |
| 1510 | { |
| 1511 | if (*p == '\'') |
| 1512 | { |
| 1513 | if (p[1] != '\'') |
| 1514 | break; |
| 1515 | ++reduce; |
| 1516 | ++p; |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | if (*p != '\'') |
| 1521 | { |
| 1522 | semsg(_("E115: Missing quote: %s"), *arg); |
| 1523 | return FAIL; |
| 1524 | } |
| 1525 | |
| 1526 | // If only parsing return after setting "*arg" |
| 1527 | if (!evaluate) |
| 1528 | { |
| 1529 | *arg = p + 1; |
| 1530 | return OK; |
| 1531 | } |
| 1532 | |
| 1533 | // Copy the string into allocated memory, handling '' to ' reduction. |
| 1534 | str = alloc((p - *arg) - reduce); |
| 1535 | if (str == NULL) |
| 1536 | return FAIL; |
| 1537 | rettv->v_type = VAR_STRING; |
| 1538 | rettv->vval.v_string = str; |
| 1539 | |
| 1540 | for (p = *arg + 1; *p != NUL; ) |
| 1541 | { |
| 1542 | if (*p == '\'') |
| 1543 | { |
| 1544 | if (p[1] != '\'') |
| 1545 | break; |
| 1546 | ++p; |
| 1547 | } |
| 1548 | MB_COPY_CHAR(p, str); |
| 1549 | } |
| 1550 | *str = NUL; |
| 1551 | *arg = p + 1; |
| 1552 | |
| 1553 | return OK; |
| 1554 | } |
| 1555 | |
| 1556 | /* |
| 1557 | * Return a string with the string representation of a variable. |
| 1558 | * If the memory is allocated "tofree" is set to it, otherwise NULL. |
| 1559 | * "numbuf" is used for a number. |
| 1560 | * Puts quotes around strings, so that they can be parsed back by eval(). |
| 1561 | * May return NULL. |
| 1562 | */ |
| 1563 | char_u * |
| 1564 | tv2string( |
| 1565 | typval_T *tv, |
| 1566 | char_u **tofree, |
| 1567 | char_u *numbuf, |
| 1568 | int copyID) |
| 1569 | { |
| 1570 | return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE); |
| 1571 | } |
| 1572 | |
| 1573 | /* |
| 1574 | * Get the value of an environment variable. |
| 1575 | * "arg" is pointing to the '$'. It is advanced to after the name. |
| 1576 | * If the environment variable was not set, silently assume it is empty. |
| 1577 | * Return FAIL if the name is invalid. |
| 1578 | */ |
| 1579 | int |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 1580 | eval_env_var(char_u **arg, typval_T *rettv, int evaluate) |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1581 | { |
| 1582 | char_u *string = NULL; |
| 1583 | int len; |
| 1584 | int cc; |
| 1585 | char_u *name; |
| 1586 | int mustfree = FALSE; |
| 1587 | |
| 1588 | ++*arg; |
| 1589 | name = *arg; |
| 1590 | len = get_env_len(arg); |
| 1591 | if (evaluate) |
| 1592 | { |
| 1593 | if (len == 0) |
| 1594 | return FAIL; // invalid empty name |
| 1595 | |
| 1596 | cc = name[len]; |
| 1597 | name[len] = NUL; |
| 1598 | // first try vim_getenv(), fast for normal environment vars |
| 1599 | string = vim_getenv(name, &mustfree); |
| 1600 | if (string != NULL && *string != NUL) |
| 1601 | { |
| 1602 | if (!mustfree) |
| 1603 | string = vim_strsave(string); |
| 1604 | } |
| 1605 | else |
| 1606 | { |
| 1607 | if (mustfree) |
| 1608 | vim_free(string); |
| 1609 | |
| 1610 | // next try expanding things like $VIM and ${HOME} |
| 1611 | string = expand_env_save(name - 1); |
| 1612 | if (string != NULL && *string == '$') |
| 1613 | VIM_CLEAR(string); |
| 1614 | } |
| 1615 | name[len] = cc; |
| 1616 | |
| 1617 | rettv->v_type = VAR_STRING; |
| 1618 | rettv->vval.v_string = string; |
| 1619 | } |
| 1620 | |
| 1621 | return OK; |
| 1622 | } |
| 1623 | |
| 1624 | /* |
| 1625 | * Get the lnum from the first argument. |
| 1626 | * Also accepts ".", "$", etc., but that only works for the current buffer. |
| 1627 | * Returns -1 on error. |
| 1628 | */ |
| 1629 | linenr_T |
| 1630 | tv_get_lnum(typval_T *argvars) |
| 1631 | { |
Bram Moolenaar | 9a96337 | 2020-12-21 21:58:46 +0100 | [diff] [blame] | 1632 | linenr_T lnum = -1; |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1633 | |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 1634 | if (argvars[0].v_type != VAR_STRING || !in_vim9script()) |
| 1635 | lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL); |
Bram Moolenaar | f6bdd82 | 2021-03-28 16:26:41 +0200 | [diff] [blame] | 1636 | if (lnum <= 0 && argvars[0].v_type != VAR_NUMBER) |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1637 | { |
| 1638 | int fnum; |
Bram Moolenaar | 6f02b00 | 2021-01-10 20:22:54 +0100 | [diff] [blame] | 1639 | pos_T *fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE); |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1640 | |
Bram Moolenaar | f6bdd82 | 2021-03-28 16:26:41 +0200 | [diff] [blame] | 1641 | // no valid number, try using arg like line() |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1642 | if (fp != NULL) |
| 1643 | lnum = fp->lnum; |
| 1644 | } |
| 1645 | return lnum; |
| 1646 | } |
| 1647 | |
| 1648 | /* |
| 1649 | * Get the lnum from the first argument. |
| 1650 | * Also accepts "$", then "buf" is used. |
| 1651 | * Returns 0 on error. |
| 1652 | */ |
| 1653 | linenr_T |
| 1654 | tv_get_lnum_buf(typval_T *argvars, buf_T *buf) |
| 1655 | { |
| 1656 | if (argvars[0].v_type == VAR_STRING |
| 1657 | && argvars[0].vval.v_string != NULL |
| 1658 | && argvars[0].vval.v_string[0] == '$' |
| 1659 | && buf != NULL) |
| 1660 | return buf->b_ml.ml_line_count; |
| 1661 | return (linenr_T)tv_get_number_chk(&argvars[0], NULL); |
| 1662 | } |
| 1663 | |
| 1664 | /* |
| 1665 | * Get buffer by number or pattern. |
| 1666 | */ |
| 1667 | buf_T * |
| 1668 | tv_get_buf(typval_T *tv, int curtab_only) |
| 1669 | { |
| 1670 | char_u *name = tv->vval.v_string; |
| 1671 | buf_T *buf; |
| 1672 | |
| 1673 | if (tv->v_type == VAR_NUMBER) |
| 1674 | return buflist_findnr((int)tv->vval.v_number); |
| 1675 | if (tv->v_type != VAR_STRING) |
| 1676 | return NULL; |
| 1677 | if (name == NULL || *name == NUL) |
| 1678 | return curbuf; |
| 1679 | if (name[0] == '$' && name[1] == NUL) |
| 1680 | return lastbuf; |
| 1681 | |
| 1682 | buf = buflist_find_by_name(name, curtab_only); |
| 1683 | |
| 1684 | // If not found, try expanding the name, like done for bufexists(). |
| 1685 | if (buf == NULL) |
| 1686 | buf = find_buffer(tv); |
| 1687 | |
| 1688 | return buf; |
| 1689 | } |
| 1690 | |
Bram Moolenaar | 3767e3a | 2020-09-01 23:06:01 +0200 | [diff] [blame] | 1691 | /* |
| 1692 | * Like tv_get_buf() but give an error message is the type is wrong. |
| 1693 | */ |
| 1694 | buf_T * |
| 1695 | tv_get_buf_from_arg(typval_T *tv) |
| 1696 | { |
| 1697 | buf_T *buf; |
| 1698 | |
| 1699 | ++emsg_off; |
| 1700 | buf = tv_get_buf(tv, FALSE); |
| 1701 | --emsg_off; |
| 1702 | if (buf == NULL |
| 1703 | && tv->v_type != VAR_NUMBER |
| 1704 | && tv->v_type != VAR_STRING) |
| 1705 | // issue errmsg for type error |
| 1706 | (void)tv_get_number(tv); |
| 1707 | return buf; |
| 1708 | } |
| 1709 | |
Bram Moolenaar | 367d59e | 2020-05-30 17:06:14 +0200 | [diff] [blame] | 1710 | #endif // FEAT_EVAL |