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 * eval.c: Expression evaluation. 12 */ 13 #define USING_FLOAT_STUFF 14 15 #include "vim.h" 16 17 #if defined(FEAT_EVAL) || defined(PROTO) 18 19 #ifdef VMS 20 # include <float.h> 21 #endif 22 23 #define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */ 24 25 static char *e_letunexp = N_("E18: Unexpected characters in :let"); 26 static char *e_undefvar = N_("E121: Undefined variable: %s"); 27 static char *e_missbrac = N_("E111: Missing ']'"); 28 static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary"); 29 static char *e_letwrong = N_("E734: Wrong variable type for %s="); 30 static char *e_illvar = N_("E461: Illegal variable name: %s"); 31 #ifdef FEAT_FLOAT 32 static char *e_float_as_string = N_("E806: using Float as a String"); 33 #endif 34 35 #define NAMESPACE_CHAR (char_u *)"abglstvw" 36 37 static dictitem_T globvars_var; /* variable used for g: */ 38 #define globvarht globvardict.dv_hashtab 39 40 /* 41 * Old Vim variables such as "v:version" are also available without the "v:". 42 * Also in functions. We need a special hashtable for them. 43 */ 44 static hashtab_T compat_hashtab; 45 46 /* 47 * When recursively copying lists and dicts we need to remember which ones we 48 * have done to avoid endless recursiveness. This unique ID is used for that. 49 * The last bit is used for previous_funccal, ignored when comparing. 50 */ 51 static int current_copyID = 0; 52 53 /* 54 * Array to hold the hashtab with variables local to each sourced script. 55 * Each item holds a variable (nameless) that points to the dict_T. 56 */ 57 typedef struct 58 { 59 dictitem_T sv_var; 60 dict_T sv_dict; 61 } scriptvar_T; 62 63 static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL}; 64 #define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1]) 65 #define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab) 66 67 static int echo_attr = 0; /* attributes used for ":echo" */ 68 69 /* The names of packages that once were loaded are remembered. */ 70 static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL}; 71 72 /* 73 * Info used by a ":for" loop. 74 */ 75 typedef struct 76 { 77 int fi_semicolon; /* TRUE if ending in '; var]' */ 78 int fi_varcount; /* nr of variables in the list */ 79 listwatch_T fi_lw; /* keep an eye on the item used. */ 80 list_T *fi_list; /* list being used */ 81 int fi_bi; /* index of blob */ 82 blob_T *fi_blob; /* blob being used */ 83 } forinfo_T; 84 85 86 /* 87 * Array to hold the value of v: variables. 88 * The value is in a dictitem, so that it can also be used in the v: scope. 89 * The reason to use this table anyway is for very quick access to the 90 * variables with the VV_ defines. 91 */ 92 93 /* values for vv_flags: */ 94 #define VV_COMPAT 1 /* compatible, also used without "v:" */ 95 #define VV_RO 2 /* read-only */ 96 #define VV_RO_SBX 4 /* read-only in the sandbox */ 97 98 #define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}} 99 100 static struct vimvar 101 { 102 char *vv_name; /* name of variable, without v: */ 103 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */ 104 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */ 105 } vimvars[VV_LEN] = 106 { 107 /* 108 * The order here must match the VV_ defines in vim.h! 109 * Initializing a union does not work, leave tv.vval empty to get zero's. 110 */ 111 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO}, 112 {VV_NAME("count1", VAR_NUMBER), VV_RO}, 113 {VV_NAME("prevcount", VAR_NUMBER), VV_RO}, 114 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT}, 115 {VV_NAME("warningmsg", VAR_STRING), 0}, 116 {VV_NAME("statusmsg", VAR_STRING), 0}, 117 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO}, 118 {VV_NAME("this_session", VAR_STRING), VV_COMPAT}, 119 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO}, 120 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX}, 121 {VV_NAME("termresponse", VAR_STRING), VV_RO}, 122 {VV_NAME("fname", VAR_STRING), VV_RO}, 123 {VV_NAME("lang", VAR_STRING), VV_RO}, 124 {VV_NAME("lc_time", VAR_STRING), VV_RO}, 125 {VV_NAME("ctype", VAR_STRING), VV_RO}, 126 {VV_NAME("charconvert_from", VAR_STRING), VV_RO}, 127 {VV_NAME("charconvert_to", VAR_STRING), VV_RO}, 128 {VV_NAME("fname_in", VAR_STRING), VV_RO}, 129 {VV_NAME("fname_out", VAR_STRING), VV_RO}, 130 {VV_NAME("fname_new", VAR_STRING), VV_RO}, 131 {VV_NAME("fname_diff", VAR_STRING), VV_RO}, 132 {VV_NAME("cmdarg", VAR_STRING), VV_RO}, 133 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX}, 134 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX}, 135 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX}, 136 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX}, 137 {VV_NAME("progname", VAR_STRING), VV_RO}, 138 {VV_NAME("servername", VAR_STRING), VV_RO}, 139 {VV_NAME("dying", VAR_NUMBER), VV_RO}, 140 {VV_NAME("exception", VAR_STRING), VV_RO}, 141 {VV_NAME("throwpoint", VAR_STRING), VV_RO}, 142 {VV_NAME("register", VAR_STRING), VV_RO}, 143 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO}, 144 {VV_NAME("insertmode", VAR_STRING), VV_RO}, 145 {VV_NAME("val", VAR_UNKNOWN), VV_RO}, 146 {VV_NAME("key", VAR_UNKNOWN), VV_RO}, 147 {VV_NAME("profiling", VAR_NUMBER), VV_RO}, 148 {VV_NAME("fcs_reason", VAR_STRING), VV_RO}, 149 {VV_NAME("fcs_choice", VAR_STRING), 0}, 150 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO}, 151 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO}, 152 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO}, 153 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO}, 154 {VV_NAME("beval_col", VAR_NUMBER), VV_RO}, 155 {VV_NAME("beval_text", VAR_STRING), VV_RO}, 156 {VV_NAME("scrollstart", VAR_STRING), 0}, 157 {VV_NAME("swapname", VAR_STRING), VV_RO}, 158 {VV_NAME("swapchoice", VAR_STRING), 0}, 159 {VV_NAME("swapcommand", VAR_STRING), VV_RO}, 160 {VV_NAME("char", VAR_STRING), 0}, 161 {VV_NAME("mouse_win", VAR_NUMBER), 0}, 162 {VV_NAME("mouse_winid", VAR_NUMBER), 0}, 163 {VV_NAME("mouse_lnum", VAR_NUMBER), 0}, 164 {VV_NAME("mouse_col", VAR_NUMBER), 0}, 165 {VV_NAME("operator", VAR_STRING), VV_RO}, 166 {VV_NAME("searchforward", VAR_NUMBER), 0}, 167 {VV_NAME("hlsearch", VAR_NUMBER), 0}, 168 {VV_NAME("oldfiles", VAR_LIST), 0}, 169 {VV_NAME("windowid", VAR_NUMBER), VV_RO}, 170 {VV_NAME("progpath", VAR_STRING), VV_RO}, 171 {VV_NAME("completed_item", VAR_DICT), VV_RO}, 172 {VV_NAME("option_new", VAR_STRING), VV_RO}, 173 {VV_NAME("option_old", VAR_STRING), VV_RO}, 174 {VV_NAME("option_type", VAR_STRING), VV_RO}, 175 {VV_NAME("errors", VAR_LIST), 0}, 176 {VV_NAME("false", VAR_SPECIAL), VV_RO}, 177 {VV_NAME("true", VAR_SPECIAL), VV_RO}, 178 {VV_NAME("null", VAR_SPECIAL), VV_RO}, 179 {VV_NAME("none", VAR_SPECIAL), VV_RO}, 180 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO}, 181 {VV_NAME("testing", VAR_NUMBER), 0}, 182 {VV_NAME("t_number", VAR_NUMBER), VV_RO}, 183 {VV_NAME("t_string", VAR_NUMBER), VV_RO}, 184 {VV_NAME("t_func", VAR_NUMBER), VV_RO}, 185 {VV_NAME("t_list", VAR_NUMBER), VV_RO}, 186 {VV_NAME("t_dict", VAR_NUMBER), VV_RO}, 187 {VV_NAME("t_float", VAR_NUMBER), VV_RO}, 188 {VV_NAME("t_bool", VAR_NUMBER), VV_RO}, 189 {VV_NAME("t_none", VAR_NUMBER), VV_RO}, 190 {VV_NAME("t_job", VAR_NUMBER), VV_RO}, 191 {VV_NAME("t_channel", VAR_NUMBER), VV_RO}, 192 {VV_NAME("t_blob", VAR_NUMBER), VV_RO}, 193 {VV_NAME("termrfgresp", VAR_STRING), VV_RO}, 194 {VV_NAME("termrbgresp", VAR_STRING), VV_RO}, 195 {VV_NAME("termu7resp", VAR_STRING), VV_RO}, 196 {VV_NAME("termstyleresp", VAR_STRING), VV_RO}, 197 {VV_NAME("termblinkresp", VAR_STRING), VV_RO}, 198 {VV_NAME("event", VAR_DICT), VV_RO}, 199 }; 200 201 /* shorthand */ 202 #define vv_type vv_di.di_tv.v_type 203 #define vv_nr vv_di.di_tv.vval.v_number 204 #define vv_float vv_di.di_tv.vval.v_float 205 #define vv_str vv_di.di_tv.vval.v_string 206 #define vv_list vv_di.di_tv.vval.v_list 207 #define vv_dict vv_di.di_tv.vval.v_dict 208 #define vv_blob vv_di.di_tv.vval.v_blob 209 #define vv_tv vv_di.di_tv 210 211 static dictitem_T vimvars_var; /* variable used for v: */ 212 #define vimvarht vimvardict.dv_hashtab 213 214 static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars); 215 static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon); 216 static char_u *skip_var_one(char_u *arg); 217 static void list_glob_vars(int *first); 218 static void list_buf_vars(int *first); 219 static void list_win_vars(int *first); 220 static void list_tab_vars(int *first); 221 static void list_vim_vars(int *first); 222 static void list_script_vars(int *first); 223 static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first); 224 static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op); 225 static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op); 226 static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op); 227 static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep); 228 static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit); 229 static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock); 230 static void item_lock(typval_T *tv, int deep, int lock); 231 232 static int eval2(char_u **arg, typval_T *rettv, int evaluate); 233 static int eval3(char_u **arg, typval_T *rettv, int evaluate); 234 static int eval4(char_u **arg, typval_T *rettv, int evaluate); 235 static int eval5(char_u **arg, typval_T *rettv, int evaluate); 236 static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string); 237 static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string); 238 239 static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate); 240 static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate); 241 static int free_unref_items(int copyID); 242 static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate); 243 static int get_env_len(char_u **arg); 244 static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end); 245 static void check_vars(char_u *name, int len); 246 static typval_T *alloc_string_tv(char_u *string); 247 static void delete_var(hashtab_T *ht, hashitem_T *hi); 248 static void list_one_var(dictitem_T *v, char *prefix, int *first); 249 static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first); 250 static int tv_check_lock(typval_T *tv, char_u *name, int use_gettext); 251 static char_u *find_option_end(char_u **arg, int *opt_flags); 252 253 /* for VIM_VERSION_ defines */ 254 #include "version.h" 255 256 /* 257 * Return "n1" divided by "n2", taking care of dividing by zero. 258 */ 259 static varnumber_T 260 num_divide(varnumber_T n1, varnumber_T n2) 261 { 262 varnumber_T result; 263 264 if (n2 == 0) // give an error message? 265 { 266 if (n1 == 0) 267 result = VARNUM_MIN; // similar to NaN 268 else if (n1 < 0) 269 result = -VARNUM_MAX; 270 else 271 result = VARNUM_MAX; 272 } 273 else 274 result = n1 / n2; 275 276 return result; 277 } 278 279 /* 280 * Return "n1" modulus "n2", taking care of dividing by zero. 281 */ 282 static varnumber_T 283 num_modulus(varnumber_T n1, varnumber_T n2) 284 { 285 // Give an error when n2 is 0? 286 return (n2 == 0) ? 0 : (n1 % n2); 287 } 288 289 290 #if defined(EBCDIC) || defined(PROTO) 291 /* 292 * Compare struct fst by function name. 293 */ 294 static int 295 compare_func_name(const void *s1, const void *s2) 296 { 297 struct fst *p1 = (struct fst *)s1; 298 struct fst *p2 = (struct fst *)s2; 299 300 return STRCMP(p1->f_name, p2->f_name); 301 } 302 303 /* 304 * Sort the function table by function name. 305 * The sorting of the table above is ASCII dependent. 306 * On machines using EBCDIC we have to sort it. 307 */ 308 static void 309 sortFunctions(void) 310 { 311 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1; 312 313 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name); 314 } 315 #endif 316 317 318 /* 319 * Initialize the global and v: variables. 320 */ 321 void 322 eval_init(void) 323 { 324 int i; 325 struct vimvar *p; 326 327 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE); 328 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE); 329 vimvardict.dv_lock = VAR_FIXED; 330 hash_init(&compat_hashtab); 331 func_init(); 332 333 for (i = 0; i < VV_LEN; ++i) 334 { 335 p = &vimvars[i]; 336 if (STRLEN(p->vv_name) > 16) 337 { 338 iemsg("INTERNAL: name too long, increase size of dictitem16_T"); 339 getout(1); 340 } 341 STRCPY(p->vv_di.di_key, p->vv_name); 342 if (p->vv_flags & VV_RO) 343 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; 344 else if (p->vv_flags & VV_RO_SBX) 345 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX; 346 else 347 p->vv_di.di_flags = DI_FLAGS_FIX; 348 349 /* add to v: scope dict, unless the value is not always available */ 350 if (p->vv_type != VAR_UNKNOWN) 351 hash_add(&vimvarht, p->vv_di.di_key); 352 if (p->vv_flags & VV_COMPAT) 353 /* add to compat scope dict */ 354 hash_add(&compat_hashtab, p->vv_di.di_key); 355 } 356 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100; 357 358 set_vim_var_nr(VV_SEARCHFORWARD, 1L); 359 set_vim_var_nr(VV_HLSEARCH, 1L); 360 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED)); 361 set_vim_var_list(VV_ERRORS, list_alloc()); 362 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED)); 363 364 set_vim_var_nr(VV_FALSE, VVAL_FALSE); 365 set_vim_var_nr(VV_TRUE, VVAL_TRUE); 366 set_vim_var_nr(VV_NONE, VVAL_NONE); 367 set_vim_var_nr(VV_NULL, VVAL_NULL); 368 369 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER); 370 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING); 371 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC); 372 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST); 373 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT); 374 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT); 375 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL); 376 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE); 377 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB); 378 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL); 379 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB); 380 381 set_reg_var(0); /* default for v:register is not 0 but '"' */ 382 383 #ifdef EBCDIC 384 /* 385 * Sort the function table, to enable binary search. 386 */ 387 sortFunctions(); 388 #endif 389 } 390 391 #if defined(EXITFREE) || defined(PROTO) 392 void 393 eval_clear(void) 394 { 395 int i; 396 struct vimvar *p; 397 398 for (i = 0; i < VV_LEN; ++i) 399 { 400 p = &vimvars[i]; 401 if (p->vv_di.di_tv.v_type == VAR_STRING) 402 VIM_CLEAR(p->vv_str); 403 else if (p->vv_di.di_tv.v_type == VAR_LIST) 404 { 405 list_unref(p->vv_list); 406 p->vv_list = NULL; 407 } 408 } 409 hash_clear(&vimvarht); 410 hash_init(&vimvarht); /* garbage_collect() will access it */ 411 hash_clear(&compat_hashtab); 412 413 free_scriptnames(); 414 # if defined(FEAT_CMDL_COMPL) 415 free_locales(); 416 # endif 417 418 /* global variables */ 419 vars_clear(&globvarht); 420 421 /* autoloaded script names */ 422 ga_clear_strings(&ga_loaded); 423 424 /* Script-local variables. First clear all the variables and in a second 425 * loop free the scriptvar_T, because a variable in one script might hold 426 * a reference to the whole scope of another script. */ 427 for (i = 1; i <= ga_scripts.ga_len; ++i) 428 vars_clear(&SCRIPT_VARS(i)); 429 for (i = 1; i <= ga_scripts.ga_len; ++i) 430 vim_free(SCRIPT_SV(i)); 431 ga_clear(&ga_scripts); 432 433 /* unreferenced lists and dicts */ 434 (void)garbage_collect(FALSE); 435 436 /* functions */ 437 free_all_functions(); 438 } 439 #endif 440 441 442 /* 443 * Set an internal variable to a string value. Creates the variable if it does 444 * not already exist. 445 */ 446 void 447 set_internal_string_var(char_u *name, char_u *value) 448 { 449 char_u *val; 450 typval_T *tvp; 451 452 val = vim_strsave(value); 453 if (val != NULL) 454 { 455 tvp = alloc_string_tv(val); 456 if (tvp != NULL) 457 { 458 set_var(name, tvp, FALSE); 459 free_tv(tvp); 460 } 461 } 462 } 463 464 static lval_T *redir_lval = NULL; 465 #define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval) 466 static garray_T redir_ga; /* only valid when redir_lval is not NULL */ 467 static char_u *redir_endp = NULL; 468 static char_u *redir_varname = NULL; 469 470 /* 471 * Start recording command output to a variable 472 * When "append" is TRUE append to an existing variable. 473 * Returns OK if successfully completed the setup. FAIL otherwise. 474 */ 475 int 476 var_redir_start(char_u *name, int append) 477 { 478 int save_emsg; 479 int err; 480 typval_T tv; 481 482 /* Catch a bad name early. */ 483 if (!eval_isnamec1(*name)) 484 { 485 emsg(_(e_invarg)); 486 return FAIL; 487 } 488 489 /* Make a copy of the name, it is used in redir_lval until redir ends. */ 490 redir_varname = vim_strsave(name); 491 if (redir_varname == NULL) 492 return FAIL; 493 494 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T)); 495 if (redir_lval == NULL) 496 { 497 var_redir_stop(); 498 return FAIL; 499 } 500 501 /* The output is stored in growarray "redir_ga" until redirection ends. */ 502 ga_init2(&redir_ga, (int)sizeof(char), 500); 503 504 /* Parse the variable name (can be a dict or list entry). */ 505 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0, 506 FNE_CHECK_START); 507 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL) 508 { 509 clear_lval(redir_lval); 510 if (redir_endp != NULL && *redir_endp != NUL) 511 /* Trailing characters are present after the variable name */ 512 emsg(_(e_trailing)); 513 else 514 emsg(_(e_invarg)); 515 redir_endp = NULL; /* don't store a value, only cleanup */ 516 var_redir_stop(); 517 return FAIL; 518 } 519 520 /* check if we can write to the variable: set it to or append an empty 521 * string */ 522 save_emsg = did_emsg; 523 did_emsg = FALSE; 524 tv.v_type = VAR_STRING; 525 tv.vval.v_string = (char_u *)""; 526 if (append) 527 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"."); 528 else 529 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"="); 530 clear_lval(redir_lval); 531 err = did_emsg; 532 did_emsg |= save_emsg; 533 if (err) 534 { 535 redir_endp = NULL; /* don't store a value, only cleanup */ 536 var_redir_stop(); 537 return FAIL; 538 } 539 540 return OK; 541 } 542 543 /* 544 * Append "value[value_len]" to the variable set by var_redir_start(). 545 * The actual appending is postponed until redirection ends, because the value 546 * appended may in fact be the string we write to, changing it may cause freed 547 * memory to be used: 548 * :redir => foo 549 * :let foo 550 * :redir END 551 */ 552 void 553 var_redir_str(char_u *value, int value_len) 554 { 555 int len; 556 557 if (redir_lval == NULL) 558 return; 559 560 if (value_len == -1) 561 len = (int)STRLEN(value); /* Append the entire string */ 562 else 563 len = value_len; /* Append only "value_len" characters */ 564 565 if (ga_grow(&redir_ga, len) == OK) 566 { 567 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len); 568 redir_ga.ga_len += len; 569 } 570 else 571 var_redir_stop(); 572 } 573 574 /* 575 * Stop redirecting command output to a variable. 576 * Frees the allocated memory. 577 */ 578 void 579 var_redir_stop(void) 580 { 581 typval_T tv; 582 583 if (EVALCMD_BUSY) 584 { 585 redir_lval = NULL; 586 return; 587 } 588 589 if (redir_lval != NULL) 590 { 591 /* If there was no error: assign the text to the variable. */ 592 if (redir_endp != NULL) 593 { 594 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */ 595 tv.v_type = VAR_STRING; 596 tv.vval.v_string = redir_ga.ga_data; 597 /* Call get_lval() again, if it's inside a Dict or List it may 598 * have changed. */ 599 redir_endp = get_lval(redir_varname, NULL, redir_lval, 600 FALSE, FALSE, 0, FNE_CHECK_START); 601 if (redir_endp != NULL && redir_lval->ll_name != NULL) 602 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)"."); 603 clear_lval(redir_lval); 604 } 605 606 /* free the collected output */ 607 VIM_CLEAR(redir_ga.ga_data); 608 609 VIM_CLEAR(redir_lval); 610 } 611 VIM_CLEAR(redir_varname); 612 } 613 614 int 615 eval_charconvert( 616 char_u *enc_from, 617 char_u *enc_to, 618 char_u *fname_from, 619 char_u *fname_to) 620 { 621 int err = FALSE; 622 623 set_vim_var_string(VV_CC_FROM, enc_from, -1); 624 set_vim_var_string(VV_CC_TO, enc_to, -1); 625 set_vim_var_string(VV_FNAME_IN, fname_from, -1); 626 set_vim_var_string(VV_FNAME_OUT, fname_to, -1); 627 if (eval_to_bool(p_ccv, &err, NULL, FALSE)) 628 err = TRUE; 629 set_vim_var_string(VV_CC_FROM, NULL, -1); 630 set_vim_var_string(VV_CC_TO, NULL, -1); 631 set_vim_var_string(VV_FNAME_IN, NULL, -1); 632 set_vim_var_string(VV_FNAME_OUT, NULL, -1); 633 634 if (err) 635 return FAIL; 636 return OK; 637 } 638 639 # if defined(FEAT_POSTSCRIPT) || defined(PROTO) 640 int 641 eval_printexpr(char_u *fname, char_u *args) 642 { 643 int err = FALSE; 644 645 set_vim_var_string(VV_FNAME_IN, fname, -1); 646 set_vim_var_string(VV_CMDARG, args, -1); 647 if (eval_to_bool(p_pexpr, &err, NULL, FALSE)) 648 err = TRUE; 649 set_vim_var_string(VV_FNAME_IN, NULL, -1); 650 set_vim_var_string(VV_CMDARG, NULL, -1); 651 652 if (err) 653 { 654 mch_remove(fname); 655 return FAIL; 656 } 657 return OK; 658 } 659 # endif 660 661 # if defined(FEAT_DIFF) || defined(PROTO) 662 void 663 eval_diff( 664 char_u *origfile, 665 char_u *newfile, 666 char_u *outfile) 667 { 668 int err = FALSE; 669 670 set_vim_var_string(VV_FNAME_IN, origfile, -1); 671 set_vim_var_string(VV_FNAME_NEW, newfile, -1); 672 set_vim_var_string(VV_FNAME_OUT, outfile, -1); 673 (void)eval_to_bool(p_dex, &err, NULL, FALSE); 674 set_vim_var_string(VV_FNAME_IN, NULL, -1); 675 set_vim_var_string(VV_FNAME_NEW, NULL, -1); 676 set_vim_var_string(VV_FNAME_OUT, NULL, -1); 677 } 678 679 void 680 eval_patch( 681 char_u *origfile, 682 char_u *difffile, 683 char_u *outfile) 684 { 685 int err; 686 687 set_vim_var_string(VV_FNAME_IN, origfile, -1); 688 set_vim_var_string(VV_FNAME_DIFF, difffile, -1); 689 set_vim_var_string(VV_FNAME_OUT, outfile, -1); 690 (void)eval_to_bool(p_pex, &err, NULL, FALSE); 691 set_vim_var_string(VV_FNAME_IN, NULL, -1); 692 set_vim_var_string(VV_FNAME_DIFF, NULL, -1); 693 set_vim_var_string(VV_FNAME_OUT, NULL, -1); 694 } 695 # endif 696 697 /* 698 * Top level evaluation function, returning a boolean. 699 * Sets "error" to TRUE if there was an error. 700 * Return TRUE or FALSE. 701 */ 702 int 703 eval_to_bool( 704 char_u *arg, 705 int *error, 706 char_u **nextcmd, 707 int skip) /* only parse, don't execute */ 708 { 709 typval_T tv; 710 varnumber_T retval = FALSE; 711 712 if (skip) 713 ++emsg_skip; 714 if (eval0(arg, &tv, nextcmd, !skip) == FAIL) 715 *error = TRUE; 716 else 717 { 718 *error = FALSE; 719 if (!skip) 720 { 721 retval = (tv_get_number_chk(&tv, error) != 0); 722 clear_tv(&tv); 723 } 724 } 725 if (skip) 726 --emsg_skip; 727 728 return (int)retval; 729 } 730 731 /* 732 * Call eval1() and give an error message if not done at a lower level. 733 */ 734 static int 735 eval1_emsg(char_u **arg, typval_T *rettv, int evaluate) 736 { 737 char_u *start = *arg; 738 int ret; 739 int did_emsg_before = did_emsg; 740 int called_emsg_before = called_emsg; 741 742 ret = eval1(arg, rettv, evaluate); 743 if (ret == FAIL) 744 { 745 // Report the invalid expression unless the expression evaluation has 746 // been cancelled due to an aborting error, an interrupt, or an 747 // exception, or we already gave a more specific error. 748 // Also check called_emsg for when using assert_fails(). 749 if (!aborting() && did_emsg == did_emsg_before 750 && called_emsg == called_emsg_before) 751 semsg(_(e_invexpr2), start); 752 } 753 return ret; 754 } 755 756 int 757 eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv) 758 { 759 char_u *s; 760 int dummy; 761 char_u buf[NUMBUFLEN]; 762 763 if (expr->v_type == VAR_FUNC) 764 { 765 s = expr->vval.v_string; 766 if (s == NULL || *s == NUL) 767 return FAIL; 768 if (call_func(s, -1, rettv, argc, argv, NULL, 769 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL) 770 return FAIL; 771 } 772 else if (expr->v_type == VAR_PARTIAL) 773 { 774 partial_T *partial = expr->vval.v_partial; 775 776 s = partial_name(partial); 777 if (s == NULL || *s == NUL) 778 return FAIL; 779 if (call_func(s, -1, rettv, argc, argv, NULL, 780 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL) 781 return FAIL; 782 } 783 else 784 { 785 s = tv_get_string_buf_chk(expr, buf); 786 if (s == NULL) 787 return FAIL; 788 s = skipwhite(s); 789 if (eval1_emsg(&s, rettv, TRUE) == FAIL) 790 return FAIL; 791 if (*s != NUL) /* check for trailing chars after expr */ 792 { 793 clear_tv(rettv); 794 semsg(_(e_invexpr2), s); 795 return FAIL; 796 } 797 } 798 return OK; 799 } 800 801 /* 802 * Like eval_to_bool() but using a typval_T instead of a string. 803 * Works for string, funcref and partial. 804 */ 805 int 806 eval_expr_to_bool(typval_T *expr, int *error) 807 { 808 typval_T rettv; 809 int res; 810 811 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL) 812 { 813 *error = TRUE; 814 return FALSE; 815 } 816 res = (tv_get_number_chk(&rettv, error) != 0); 817 clear_tv(&rettv); 818 return res; 819 } 820 821 /* 822 * Top level evaluation function, returning a string. If "skip" is TRUE, 823 * only parsing to "nextcmd" is done, without reporting errors. Return 824 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE. 825 */ 826 char_u * 827 eval_to_string_skip( 828 char_u *arg, 829 char_u **nextcmd, 830 int skip) /* only parse, don't execute */ 831 { 832 typval_T tv; 833 char_u *retval; 834 835 if (skip) 836 ++emsg_skip; 837 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip) 838 retval = NULL; 839 else 840 { 841 retval = vim_strsave(tv_get_string(&tv)); 842 clear_tv(&tv); 843 } 844 if (skip) 845 --emsg_skip; 846 847 return retval; 848 } 849 850 /* 851 * Skip over an expression at "*pp". 852 * Return FAIL for an error, OK otherwise. 853 */ 854 int 855 skip_expr(char_u **pp) 856 { 857 typval_T rettv; 858 859 *pp = skipwhite(*pp); 860 return eval1(pp, &rettv, FALSE); 861 } 862 863 /* 864 * Top level evaluation function, returning a string. 865 * When "convert" is TRUE convert a List into a sequence of lines and convert 866 * a Float to a String. 867 * Return pointer to allocated memory, or NULL for failure. 868 */ 869 char_u * 870 eval_to_string( 871 char_u *arg, 872 char_u **nextcmd, 873 int convert) 874 { 875 typval_T tv; 876 char_u *retval; 877 garray_T ga; 878 #ifdef FEAT_FLOAT 879 char_u numbuf[NUMBUFLEN]; 880 #endif 881 882 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL) 883 retval = NULL; 884 else 885 { 886 if (convert && tv.v_type == VAR_LIST) 887 { 888 ga_init2(&ga, (int)sizeof(char), 80); 889 if (tv.vval.v_list != NULL) 890 { 891 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0); 892 if (tv.vval.v_list->lv_len > 0) 893 ga_append(&ga, NL); 894 } 895 ga_append(&ga, NUL); 896 retval = (char_u *)ga.ga_data; 897 } 898 #ifdef FEAT_FLOAT 899 else if (convert && tv.v_type == VAR_FLOAT) 900 { 901 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float); 902 retval = vim_strsave(numbuf); 903 } 904 #endif 905 else 906 retval = vim_strsave(tv_get_string(&tv)); 907 clear_tv(&tv); 908 } 909 910 return retval; 911 } 912 913 /* 914 * Call eval_to_string() without using current local variables and using 915 * textlock. When "use_sandbox" is TRUE use the sandbox. 916 */ 917 char_u * 918 eval_to_string_safe( 919 char_u *arg, 920 char_u **nextcmd, 921 int use_sandbox) 922 { 923 char_u *retval; 924 funccal_entry_T funccal_entry; 925 926 save_funccal(&funccal_entry); 927 if (use_sandbox) 928 ++sandbox; 929 ++textlock; 930 retval = eval_to_string(arg, nextcmd, FALSE); 931 if (use_sandbox) 932 --sandbox; 933 --textlock; 934 restore_funccal(); 935 return retval; 936 } 937 938 /* 939 * Top level evaluation function, returning a number. 940 * Evaluates "expr" silently. 941 * Returns -1 for an error. 942 */ 943 varnumber_T 944 eval_to_number(char_u *expr) 945 { 946 typval_T rettv; 947 varnumber_T retval; 948 char_u *p = skipwhite(expr); 949 950 ++emsg_off; 951 952 if (eval1(&p, &rettv, TRUE) == FAIL) 953 retval = -1; 954 else 955 { 956 retval = tv_get_number_chk(&rettv, NULL); 957 clear_tv(&rettv); 958 } 959 --emsg_off; 960 961 return retval; 962 } 963 964 /* 965 * Prepare v: variable "idx" to be used. 966 * Save the current typeval in "save_tv". 967 * When not used yet add the variable to the v: hashtable. 968 */ 969 void 970 prepare_vimvar(int idx, typval_T *save_tv) 971 { 972 *save_tv = vimvars[idx].vv_tv; 973 if (vimvars[idx].vv_type == VAR_UNKNOWN) 974 hash_add(&vimvarht, vimvars[idx].vv_di.di_key); 975 } 976 977 /* 978 * Restore v: variable "idx" to typeval "save_tv". 979 * When no longer defined, remove the variable from the v: hashtable. 980 */ 981 void 982 restore_vimvar(int idx, typval_T *save_tv) 983 { 984 hashitem_T *hi; 985 986 vimvars[idx].vv_tv = *save_tv; 987 if (vimvars[idx].vv_type == VAR_UNKNOWN) 988 { 989 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key); 990 if (HASHITEM_EMPTY(hi)) 991 internal_error("restore_vimvar()"); 992 else 993 hash_remove(&vimvarht, hi); 994 } 995 } 996 997 #if defined(FEAT_SPELL) || defined(PROTO) 998 /* 999 * Evaluate an expression to a list with suggestions. 1000 * For the "expr:" part of 'spellsuggest'. 1001 * Returns NULL when there is an error. 1002 */ 1003 list_T * 1004 eval_spell_expr(char_u *badword, char_u *expr) 1005 { 1006 typval_T save_val; 1007 typval_T rettv; 1008 list_T *list = NULL; 1009 char_u *p = skipwhite(expr); 1010 1011 /* Set "v:val" to the bad word. */ 1012 prepare_vimvar(VV_VAL, &save_val); 1013 vimvars[VV_VAL].vv_type = VAR_STRING; 1014 vimvars[VV_VAL].vv_str = badword; 1015 if (p_verbose == 0) 1016 ++emsg_off; 1017 1018 if (eval1(&p, &rettv, TRUE) == OK) 1019 { 1020 if (rettv.v_type != VAR_LIST) 1021 clear_tv(&rettv); 1022 else 1023 list = rettv.vval.v_list; 1024 } 1025 1026 if (p_verbose == 0) 1027 --emsg_off; 1028 restore_vimvar(VV_VAL, &save_val); 1029 1030 return list; 1031 } 1032 1033 /* 1034 * "list" is supposed to contain two items: a word and a number. Return the 1035 * word in "pp" and the number as the return value. 1036 * Return -1 if anything isn't right. 1037 * Used to get the good word and score from the eval_spell_expr() result. 1038 */ 1039 int 1040 get_spellword(list_T *list, char_u **pp) 1041 { 1042 listitem_T *li; 1043 1044 li = list->lv_first; 1045 if (li == NULL) 1046 return -1; 1047 *pp = tv_get_string(&li->li_tv); 1048 1049 li = li->li_next; 1050 if (li == NULL) 1051 return -1; 1052 return (int)tv_get_number(&li->li_tv); 1053 } 1054 #endif 1055 1056 /* 1057 * Top level evaluation function. 1058 * Returns an allocated typval_T with the result. 1059 * Returns NULL when there is an error. 1060 */ 1061 typval_T * 1062 eval_expr(char_u *arg, char_u **nextcmd) 1063 { 1064 typval_T *tv; 1065 1066 tv = (typval_T *)alloc(sizeof(typval_T)); 1067 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL) 1068 VIM_CLEAR(tv); 1069 1070 return tv; 1071 } 1072 1073 1074 /* 1075 * Call some Vim script function and return the result in "*rettv". 1076 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] 1077 * should have type VAR_UNKNOWN. 1078 * Returns OK or FAIL. 1079 */ 1080 int 1081 call_vim_function( 1082 char_u *func, 1083 int argc, 1084 typval_T *argv, 1085 typval_T *rettv) 1086 { 1087 int doesrange; 1088 int ret; 1089 1090 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */ 1091 ret = call_func(func, -1, rettv, argc, argv, NULL, 1092 curwin->w_cursor.lnum, curwin->w_cursor.lnum, 1093 &doesrange, TRUE, NULL, NULL); 1094 if (ret == FAIL) 1095 clear_tv(rettv); 1096 1097 return ret; 1098 } 1099 1100 /* 1101 * Call Vim script function "func" and return the result as a number. 1102 * Returns -1 when calling the function fails. 1103 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should 1104 * have type VAR_UNKNOWN. 1105 */ 1106 varnumber_T 1107 call_func_retnr( 1108 char_u *func, 1109 int argc, 1110 typval_T *argv) 1111 { 1112 typval_T rettv; 1113 varnumber_T retval; 1114 1115 if (call_vim_function(func, argc, argv, &rettv) == FAIL) 1116 return -1; 1117 1118 retval = tv_get_number_chk(&rettv, NULL); 1119 clear_tv(&rettv); 1120 return retval; 1121 } 1122 1123 #if defined(FEAT_CMDL_COMPL) \ 1124 || defined(FEAT_COMPL_FUNC) || defined(PROTO) 1125 1126 # if defined(FEAT_CMDL_COMPL) || defined(PROTO) 1127 /* 1128 * Call Vim script function "func" and return the result as a string. 1129 * Returns NULL when calling the function fails. 1130 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should 1131 * have type VAR_UNKNOWN. 1132 */ 1133 void * 1134 call_func_retstr( 1135 char_u *func, 1136 int argc, 1137 typval_T *argv) 1138 { 1139 typval_T rettv; 1140 char_u *retval; 1141 1142 if (call_vim_function(func, argc, argv, &rettv) == FAIL) 1143 return NULL; 1144 1145 retval = vim_strsave(tv_get_string(&rettv)); 1146 clear_tv(&rettv); 1147 return retval; 1148 } 1149 # endif 1150 1151 /* 1152 * Call Vim script function "func" and return the result as a List. 1153 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should 1154 * have type VAR_UNKNOWN. 1155 * Returns NULL when there is something wrong. 1156 */ 1157 void * 1158 call_func_retlist( 1159 char_u *func, 1160 int argc, 1161 typval_T *argv) 1162 { 1163 typval_T rettv; 1164 1165 if (call_vim_function(func, argc, argv, &rettv) == FAIL) 1166 return NULL; 1167 1168 if (rettv.v_type != VAR_LIST) 1169 { 1170 clear_tv(&rettv); 1171 return NULL; 1172 } 1173 1174 return rettv.vval.v_list; 1175 } 1176 #endif 1177 1178 1179 #ifdef FEAT_FOLDING 1180 /* 1181 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding 1182 * it in "*cp". Doesn't give error messages. 1183 */ 1184 int 1185 eval_foldexpr(char_u *arg, int *cp) 1186 { 1187 typval_T tv; 1188 varnumber_T retval; 1189 char_u *s; 1190 int use_sandbox = was_set_insecurely((char_u *)"foldexpr", 1191 OPT_LOCAL); 1192 1193 ++emsg_off; 1194 if (use_sandbox) 1195 ++sandbox; 1196 ++textlock; 1197 *cp = NUL; 1198 if (eval0(arg, &tv, NULL, TRUE) == FAIL) 1199 retval = 0; 1200 else 1201 { 1202 /* If the result is a number, just return the number. */ 1203 if (tv.v_type == VAR_NUMBER) 1204 retval = tv.vval.v_number; 1205 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL) 1206 retval = 0; 1207 else 1208 { 1209 /* If the result is a string, check if there is a non-digit before 1210 * the number. */ 1211 s = tv.vval.v_string; 1212 if (!VIM_ISDIGIT(*s) && *s != '-') 1213 *cp = *s++; 1214 retval = atol((char *)s); 1215 } 1216 clear_tv(&tv); 1217 } 1218 --emsg_off; 1219 if (use_sandbox) 1220 --sandbox; 1221 --textlock; 1222 1223 return (int)retval; 1224 } 1225 #endif 1226 1227 /* 1228 * ":let" list all variable values 1229 * ":let var1 var2" list variable values 1230 * ":let var = expr" assignment command. 1231 * ":let var += expr" assignment command. 1232 * ":let var -= expr" assignment command. 1233 * ":let var *= expr" assignment command. 1234 * ":let var /= expr" assignment command. 1235 * ":let var %= expr" assignment command. 1236 * ":let var .= expr" assignment command. 1237 * ":let var ..= expr" assignment command. 1238 * ":let [var1, var2] = expr" unpack list. 1239 */ 1240 void 1241 ex_let(exarg_T *eap) 1242 { 1243 char_u *arg = eap->arg; 1244 char_u *expr = NULL; 1245 typval_T rettv; 1246 int i; 1247 int var_count = 0; 1248 int semicolon = 0; 1249 char_u op[2]; 1250 char_u *argend; 1251 int first = TRUE; 1252 int concat; 1253 1254 argend = skip_var_list(arg, &var_count, &semicolon); 1255 if (argend == NULL) 1256 return; 1257 if (argend > arg && argend[-1] == '.') // for var.='str' 1258 --argend; 1259 expr = skipwhite(argend); 1260 concat = expr[0] == '.' 1261 && ((expr[1] == '=' && current_sctx.sc_version < 2) 1262 || (expr[1] == '.' && expr[2] == '=')); 1263 if (*expr != '=' && !((vim_strchr((char_u *)"+-*/%", *expr) != NULL 1264 && expr[1] == '=') || concat)) 1265 { 1266 /* 1267 * ":let" without "=": list variables 1268 */ 1269 if (*arg == '[') 1270 emsg(_(e_invarg)); 1271 else if (expr[0] == '.') 1272 emsg(_("E985: .= is not supported with script version 2")); 1273 else if (!ends_excmd(*arg)) 1274 /* ":let var1 var2" */ 1275 arg = list_arg_vars(eap, arg, &first); 1276 else if (!eap->skip) 1277 { 1278 /* ":let" */ 1279 list_glob_vars(&first); 1280 list_buf_vars(&first); 1281 list_win_vars(&first); 1282 list_tab_vars(&first); 1283 list_script_vars(&first); 1284 list_func_vars(&first); 1285 list_vim_vars(&first); 1286 } 1287 eap->nextcmd = check_nextcmd(arg); 1288 } 1289 else 1290 { 1291 op[0] = '='; 1292 op[1] = NUL; 1293 if (*expr != '=') 1294 { 1295 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL) 1296 { 1297 op[0] = *expr; // +=, -=, *=, /=, %= or .= 1298 if (expr[0] == '.' && expr[1] == '.') // ..= 1299 ++expr; 1300 } 1301 expr = skipwhite(expr + 2); 1302 } 1303 else 1304 expr = skipwhite(expr + 1); 1305 1306 if (eap->skip) 1307 ++emsg_skip; 1308 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip); 1309 if (eap->skip) 1310 { 1311 if (i != FAIL) 1312 clear_tv(&rettv); 1313 --emsg_skip; 1314 } 1315 else if (i != FAIL) 1316 { 1317 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count, 1318 op); 1319 clear_tv(&rettv); 1320 } 1321 } 1322 } 1323 1324 /* 1325 * Assign the typevalue "tv" to the variable or variables at "arg_start". 1326 * Handles both "var" with any type and "[var, var; var]" with a list type. 1327 * When "nextchars" is not NULL it points to a string with characters that 1328 * must appear after the variable(s). Use "+", "-" or "." for add, subtract 1329 * or concatenate. 1330 * Returns OK or FAIL; 1331 */ 1332 static int 1333 ex_let_vars( 1334 char_u *arg_start, 1335 typval_T *tv, 1336 int copy, /* copy values from "tv", don't move */ 1337 int semicolon, /* from skip_var_list() */ 1338 int var_count, /* from skip_var_list() */ 1339 char_u *nextchars) 1340 { 1341 char_u *arg = arg_start; 1342 list_T *l; 1343 int i; 1344 listitem_T *item; 1345 typval_T ltv; 1346 1347 if (*arg != '[') 1348 { 1349 /* 1350 * ":let var = expr" or ":for var in list" 1351 */ 1352 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL) 1353 return FAIL; 1354 return OK; 1355 } 1356 1357 /* 1358 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist" 1359 */ 1360 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL) 1361 { 1362 emsg(_(e_listreq)); 1363 return FAIL; 1364 } 1365 1366 i = list_len(l); 1367 if (semicolon == 0 && var_count < i) 1368 { 1369 emsg(_("E687: Less targets than List items")); 1370 return FAIL; 1371 } 1372 if (var_count - semicolon > i) 1373 { 1374 emsg(_("E688: More targets than List items")); 1375 return FAIL; 1376 } 1377 1378 item = l->lv_first; 1379 while (*arg != ']') 1380 { 1381 arg = skipwhite(arg + 1); 1382 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars); 1383 item = item->li_next; 1384 if (arg == NULL) 1385 return FAIL; 1386 1387 arg = skipwhite(arg); 1388 if (*arg == ';') 1389 { 1390 /* Put the rest of the list (may be empty) in the var after ';'. 1391 * Create a new list for this. */ 1392 l = list_alloc(); 1393 if (l == NULL) 1394 return FAIL; 1395 while (item != NULL) 1396 { 1397 list_append_tv(l, &item->li_tv); 1398 item = item->li_next; 1399 } 1400 1401 ltv.v_type = VAR_LIST; 1402 ltv.v_lock = 0; 1403 ltv.vval.v_list = l; 1404 l->lv_refcount = 1; 1405 1406 arg = ex_let_one(skipwhite(arg + 1), <v, FALSE, 1407 (char_u *)"]", nextchars); 1408 clear_tv(<v); 1409 if (arg == NULL) 1410 return FAIL; 1411 break; 1412 } 1413 else if (*arg != ',' && *arg != ']') 1414 { 1415 internal_error("ex_let_vars()"); 1416 return FAIL; 1417 } 1418 } 1419 1420 return OK; 1421 } 1422 1423 /* 1424 * Skip over assignable variable "var" or list of variables "[var, var]". 1425 * Used for ":let varvar = expr" and ":for varvar in expr". 1426 * For "[var, var]" increment "*var_count" for each variable. 1427 * for "[var, var; var]" set "semicolon". 1428 * Return NULL for an error. 1429 */ 1430 static char_u * 1431 skip_var_list( 1432 char_u *arg, 1433 int *var_count, 1434 int *semicolon) 1435 { 1436 char_u *p, *s; 1437 1438 if (*arg == '[') 1439 { 1440 /* "[var, var]": find the matching ']'. */ 1441 p = arg; 1442 for (;;) 1443 { 1444 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */ 1445 s = skip_var_one(p); 1446 if (s == p) 1447 { 1448 semsg(_(e_invarg2), p); 1449 return NULL; 1450 } 1451 ++*var_count; 1452 1453 p = skipwhite(s); 1454 if (*p == ']') 1455 break; 1456 else if (*p == ';') 1457 { 1458 if (*semicolon == 1) 1459 { 1460 emsg(_("Double ; in list of variables")); 1461 return NULL; 1462 } 1463 *semicolon = 1; 1464 } 1465 else if (*p != ',') 1466 { 1467 semsg(_(e_invarg2), p); 1468 return NULL; 1469 } 1470 } 1471 return p + 1; 1472 } 1473 else 1474 return skip_var_one(arg); 1475 } 1476 1477 /* 1478 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key, 1479 * l[idx]. 1480 */ 1481 static char_u * 1482 skip_var_one(char_u *arg) 1483 { 1484 if (*arg == '@' && arg[1] != NUL) 1485 return arg + 2; 1486 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg, 1487 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START); 1488 } 1489 1490 /* 1491 * List variables for hashtab "ht" with prefix "prefix". 1492 * If "empty" is TRUE also list NULL strings as empty strings. 1493 */ 1494 void 1495 list_hashtable_vars( 1496 hashtab_T *ht, 1497 char *prefix, 1498 int empty, 1499 int *first) 1500 { 1501 hashitem_T *hi; 1502 dictitem_T *di; 1503 int todo; 1504 char_u buf[IOSIZE]; 1505 1506 todo = (int)ht->ht_used; 1507 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi) 1508 { 1509 if (!HASHITEM_EMPTY(hi)) 1510 { 1511 --todo; 1512 di = HI2DI(hi); 1513 1514 // apply :filter /pat/ to variable name 1515 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1); 1516 vim_strcat((char_u *)buf, di->di_key, IOSIZE); 1517 if (message_filtered(buf)) 1518 continue; 1519 1520 if (empty || di->di_tv.v_type != VAR_STRING 1521 || di->di_tv.vval.v_string != NULL) 1522 list_one_var(di, prefix, first); 1523 } 1524 } 1525 } 1526 1527 /* 1528 * List global variables. 1529 */ 1530 static void 1531 list_glob_vars(int *first) 1532 { 1533 list_hashtable_vars(&globvarht, "", TRUE, first); 1534 } 1535 1536 /* 1537 * List buffer variables. 1538 */ 1539 static void 1540 list_buf_vars(int *first) 1541 { 1542 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first); 1543 } 1544 1545 /* 1546 * List window variables. 1547 */ 1548 static void 1549 list_win_vars(int *first) 1550 { 1551 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first); 1552 } 1553 1554 /* 1555 * List tab page variables. 1556 */ 1557 static void 1558 list_tab_vars(int *first) 1559 { 1560 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first); 1561 } 1562 1563 /* 1564 * List Vim variables. 1565 */ 1566 static void 1567 list_vim_vars(int *first) 1568 { 1569 list_hashtable_vars(&vimvarht, "v:", FALSE, first); 1570 } 1571 1572 /* 1573 * List script-local variables, if there is a script. 1574 */ 1575 static void 1576 list_script_vars(int *first) 1577 { 1578 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len) 1579 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid), 1580 "s:", FALSE, first); 1581 } 1582 1583 /* 1584 * List variables in "arg". 1585 */ 1586 static char_u * 1587 list_arg_vars(exarg_T *eap, char_u *arg, int *first) 1588 { 1589 int error = FALSE; 1590 int len; 1591 char_u *name; 1592 char_u *name_start; 1593 char_u *arg_subsc; 1594 char_u *tofree; 1595 typval_T tv; 1596 1597 while (!ends_excmd(*arg) && !got_int) 1598 { 1599 if (error || eap->skip) 1600 { 1601 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START); 1602 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg)) 1603 { 1604 emsg_severe = TRUE; 1605 emsg(_(e_trailing)); 1606 break; 1607 } 1608 } 1609 else 1610 { 1611 /* get_name_len() takes care of expanding curly braces */ 1612 name_start = name = arg; 1613 len = get_name_len(&arg, &tofree, TRUE, TRUE); 1614 if (len <= 0) 1615 { 1616 /* This is mainly to keep test 49 working: when expanding 1617 * curly braces fails overrule the exception error message. */ 1618 if (len < 0 && !aborting()) 1619 { 1620 emsg_severe = TRUE; 1621 semsg(_(e_invarg2), arg); 1622 break; 1623 } 1624 error = TRUE; 1625 } 1626 else 1627 { 1628 if (tofree != NULL) 1629 name = tofree; 1630 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL) 1631 error = TRUE; 1632 else 1633 { 1634 /* handle d.key, l[idx], f(expr) */ 1635 arg_subsc = arg; 1636 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL) 1637 error = TRUE; 1638 else 1639 { 1640 if (arg == arg_subsc && len == 2 && name[1] == ':') 1641 { 1642 switch (*name) 1643 { 1644 case 'g': list_glob_vars(first); break; 1645 case 'b': list_buf_vars(first); break; 1646 case 'w': list_win_vars(first); break; 1647 case 't': list_tab_vars(first); break; 1648 case 'v': list_vim_vars(first); break; 1649 case 's': list_script_vars(first); break; 1650 case 'l': list_func_vars(first); break; 1651 default: 1652 semsg(_("E738: Can't list variables for %s"), name); 1653 } 1654 } 1655 else 1656 { 1657 char_u numbuf[NUMBUFLEN]; 1658 char_u *tf; 1659 int c; 1660 char_u *s; 1661 1662 s = echo_string(&tv, &tf, numbuf, 0); 1663 c = *arg; 1664 *arg = NUL; 1665 list_one_var_a("", 1666 arg == arg_subsc ? name : name_start, 1667 tv.v_type, 1668 s == NULL ? (char_u *)"" : s, 1669 first); 1670 *arg = c; 1671 vim_free(tf); 1672 } 1673 clear_tv(&tv); 1674 } 1675 } 1676 } 1677 1678 vim_free(tofree); 1679 } 1680 1681 arg = skipwhite(arg); 1682 } 1683 1684 return arg; 1685 } 1686 1687 /* 1688 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value. 1689 * Returns a pointer to the char just after the var name. 1690 * Returns NULL if there is an error. 1691 */ 1692 static char_u * 1693 ex_let_one( 1694 char_u *arg, /* points to variable name */ 1695 typval_T *tv, /* value to assign to variable */ 1696 int copy, /* copy value from "tv" */ 1697 char_u *endchars, /* valid chars after variable name or NULL */ 1698 char_u *op) /* "+", "-", "." or NULL*/ 1699 { 1700 int c1; 1701 char_u *name; 1702 char_u *p; 1703 char_u *arg_end = NULL; 1704 int len; 1705 int opt_flags; 1706 char_u *tofree = NULL; 1707 1708 /* 1709 * ":let $VAR = expr": Set environment variable. 1710 */ 1711 if (*arg == '$') 1712 { 1713 /* Find the end of the name. */ 1714 ++arg; 1715 name = arg; 1716 len = get_env_len(&arg); 1717 if (len == 0) 1718 semsg(_(e_invarg2), name - 1); 1719 else 1720 { 1721 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL) 1722 semsg(_(e_letwrong), op); 1723 else if (endchars != NULL 1724 && vim_strchr(endchars, *skipwhite(arg)) == NULL) 1725 emsg(_(e_letunexp)); 1726 else if (!check_secure()) 1727 { 1728 c1 = name[len]; 1729 name[len] = NUL; 1730 p = tv_get_string_chk(tv); 1731 if (p != NULL && op != NULL && *op == '.') 1732 { 1733 int mustfree = FALSE; 1734 char_u *s = vim_getenv(name, &mustfree); 1735 1736 if (s != NULL) 1737 { 1738 p = tofree = concat_str(s, p); 1739 if (mustfree) 1740 vim_free(s); 1741 } 1742 } 1743 if (p != NULL) 1744 { 1745 vim_setenv(name, p); 1746 if (STRICMP(name, "HOME") == 0) 1747 init_homedir(); 1748 else if (didset_vim && STRICMP(name, "VIM") == 0) 1749 didset_vim = FALSE; 1750 else if (didset_vimruntime 1751 && STRICMP(name, "VIMRUNTIME") == 0) 1752 didset_vimruntime = FALSE; 1753 arg_end = arg; 1754 } 1755 name[len] = c1; 1756 vim_free(tofree); 1757 } 1758 } 1759 } 1760 1761 /* 1762 * ":let &option = expr": Set option value. 1763 * ":let &l:option = expr": Set local option value. 1764 * ":let &g:option = expr": Set global option value. 1765 */ 1766 else if (*arg == '&') 1767 { 1768 /* Find the end of the name. */ 1769 p = find_option_end(&arg, &opt_flags); 1770 if (p == NULL || (endchars != NULL 1771 && vim_strchr(endchars, *skipwhite(p)) == NULL)) 1772 emsg(_(e_letunexp)); 1773 else 1774 { 1775 long n; 1776 int opt_type; 1777 long numval; 1778 char_u *stringval = NULL; 1779 char_u *s; 1780 1781 c1 = *p; 1782 *p = NUL; 1783 1784 n = (long)tv_get_number(tv); 1785 s = tv_get_string_chk(tv); /* != NULL if number or string */ 1786 if (s != NULL && op != NULL && *op != '=') 1787 { 1788 opt_type = get_option_value(arg, &numval, 1789 &stringval, opt_flags); 1790 if ((opt_type == 1 && *op == '.') 1791 || (opt_type == 0 && *op != '.')) 1792 { 1793 semsg(_(e_letwrong), op); 1794 s = NULL; // don't set the value 1795 } 1796 else 1797 { 1798 if (opt_type == 1) // number 1799 { 1800 switch (*op) 1801 { 1802 case '+': n = numval + n; break; 1803 case '-': n = numval - n; break; 1804 case '*': n = numval * n; break; 1805 case '/': n = (long)num_divide(numval, n); break; 1806 case '%': n = (long)num_modulus(numval, n); break; 1807 } 1808 } 1809 else if (opt_type == 0 && stringval != NULL) // string 1810 { 1811 s = concat_str(stringval, s); 1812 vim_free(stringval); 1813 stringval = s; 1814 } 1815 } 1816 } 1817 if (s != NULL) 1818 { 1819 set_option_value(arg, n, s, opt_flags); 1820 arg_end = p; 1821 } 1822 *p = c1; 1823 vim_free(stringval); 1824 } 1825 } 1826 1827 /* 1828 * ":let @r = expr": Set register contents. 1829 */ 1830 else if (*arg == '@') 1831 { 1832 ++arg; 1833 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL) 1834 semsg(_(e_letwrong), op); 1835 else if (endchars != NULL 1836 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL) 1837 emsg(_(e_letunexp)); 1838 else 1839 { 1840 char_u *ptofree = NULL; 1841 char_u *s; 1842 1843 p = tv_get_string_chk(tv); 1844 if (p != NULL && op != NULL && *op == '.') 1845 { 1846 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC); 1847 if (s != NULL) 1848 { 1849 p = ptofree = concat_str(s, p); 1850 vim_free(s); 1851 } 1852 } 1853 if (p != NULL) 1854 { 1855 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE); 1856 arg_end = arg + 1; 1857 } 1858 vim_free(ptofree); 1859 } 1860 } 1861 1862 /* 1863 * ":let var = expr": Set internal variable. 1864 * ":let {expr} = expr": Idem, name made with curly braces 1865 */ 1866 else if (eval_isnamec1(*arg) || *arg == '{') 1867 { 1868 lval_T lv; 1869 1870 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START); 1871 if (p != NULL && lv.ll_name != NULL) 1872 { 1873 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL) 1874 emsg(_(e_letunexp)); 1875 else 1876 { 1877 set_var_lval(&lv, p, tv, copy, op); 1878 arg_end = p; 1879 } 1880 } 1881 clear_lval(&lv); 1882 } 1883 1884 else 1885 semsg(_(e_invarg2), arg); 1886 1887 return arg_end; 1888 } 1889 1890 /* 1891 * Get an lval: variable, Dict item or List item that can be assigned a value 1892 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]", 1893 * "name.key", "name.key[expr]" etc. 1894 * Indexing only works if "name" is an existing List or Dictionary. 1895 * "name" points to the start of the name. 1896 * If "rettv" is not NULL it points to the value to be assigned. 1897 * "unlet" is TRUE for ":unlet": slightly different behavior when something is 1898 * wrong; must end in space or cmd separator. 1899 * 1900 * flags: 1901 * GLV_QUIET: do not give error messages 1902 * GLV_READ_ONLY: will not change the variable 1903 * GLV_NO_AUTOLOAD: do not use script autoloading 1904 * 1905 * Returns a pointer to just after the name, including indexes. 1906 * When an evaluation error occurs "lp->ll_name" is NULL; 1907 * Returns NULL for a parsing error. Still need to free items in "lp"! 1908 */ 1909 char_u * 1910 get_lval( 1911 char_u *name, 1912 typval_T *rettv, 1913 lval_T *lp, 1914 int unlet, 1915 int skip, 1916 int flags, /* GLV_ values */ 1917 int fne_flags) /* flags for find_name_end() */ 1918 { 1919 char_u *p; 1920 char_u *expr_start, *expr_end; 1921 int cc; 1922 dictitem_T *v; 1923 typval_T var1; 1924 typval_T var2; 1925 int empty1 = FALSE; 1926 listitem_T *ni; 1927 char_u *key = NULL; 1928 int len; 1929 hashtab_T *ht; 1930 int quiet = flags & GLV_QUIET; 1931 1932 /* Clear everything in "lp". */ 1933 vim_memset(lp, 0, sizeof(lval_T)); 1934 1935 if (skip) 1936 { 1937 /* When skipping just find the end of the name. */ 1938 lp->ll_name = name; 1939 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags); 1940 } 1941 1942 /* Find the end of the name. */ 1943 p = find_name_end(name, &expr_start, &expr_end, fne_flags); 1944 if (expr_start != NULL) 1945 { 1946 /* Don't expand the name when we already know there is an error. */ 1947 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p) 1948 && *p != '[' && *p != '.') 1949 { 1950 emsg(_(e_trailing)); 1951 return NULL; 1952 } 1953 1954 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p); 1955 if (lp->ll_exp_name == NULL) 1956 { 1957 /* Report an invalid expression in braces, unless the 1958 * expression evaluation has been cancelled due to an 1959 * aborting error, an interrupt, or an exception. */ 1960 if (!aborting() && !quiet) 1961 { 1962 emsg_severe = TRUE; 1963 semsg(_(e_invarg2), name); 1964 return NULL; 1965 } 1966 } 1967 lp->ll_name = lp->ll_exp_name; 1968 } 1969 else 1970 lp->ll_name = name; 1971 1972 /* Without [idx] or .key we are done. */ 1973 if ((*p != '[' && *p != '.') || lp->ll_name == NULL) 1974 return p; 1975 1976 cc = *p; 1977 *p = NUL; 1978 /* Only pass &ht when we would write to the variable, it prevents autoload 1979 * as well. */ 1980 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht, 1981 flags & GLV_NO_AUTOLOAD); 1982 if (v == NULL && !quiet) 1983 semsg(_(e_undefvar), lp->ll_name); 1984 *p = cc; 1985 if (v == NULL) 1986 return NULL; 1987 1988 /* 1989 * Loop until no more [idx] or .key is following. 1990 */ 1991 lp->ll_tv = &v->di_tv; 1992 var1.v_type = VAR_UNKNOWN; 1993 var2.v_type = VAR_UNKNOWN; 1994 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT)) 1995 { 1996 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL) 1997 && !(lp->ll_tv->v_type == VAR_DICT 1998 && lp->ll_tv->vval.v_dict != NULL) 1999 && !(lp->ll_tv->v_type == VAR_BLOB 2000 && lp->ll_tv->vval.v_blob != NULL)) 2001 { 2002 if (!quiet) 2003 emsg(_("E689: Can only index a List, Dictionary or Blob")); 2004 return NULL; 2005 } 2006 if (lp->ll_range) 2007 { 2008 if (!quiet) 2009 emsg(_("E708: [:] must come last")); 2010 return NULL; 2011 } 2012 2013 len = -1; 2014 if (*p == '.') 2015 { 2016 key = p + 1; 2017 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len) 2018 ; 2019 if (len == 0) 2020 { 2021 if (!quiet) 2022 emsg(_(e_emptykey)); 2023 return NULL; 2024 } 2025 p = key + len; 2026 } 2027 else 2028 { 2029 /* Get the index [expr] or the first index [expr: ]. */ 2030 p = skipwhite(p + 1); 2031 if (*p == ':') 2032 empty1 = TRUE; 2033 else 2034 { 2035 empty1 = FALSE; 2036 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */ 2037 return NULL; 2038 if (tv_get_string_chk(&var1) == NULL) 2039 { 2040 /* not a number or string */ 2041 clear_tv(&var1); 2042 return NULL; 2043 } 2044 } 2045 2046 /* Optionally get the second index [ :expr]. */ 2047 if (*p == ':') 2048 { 2049 if (lp->ll_tv->v_type == VAR_DICT) 2050 { 2051 if (!quiet) 2052 emsg(_(e_dictrange)); 2053 clear_tv(&var1); 2054 return NULL; 2055 } 2056 if (rettv != NULL 2057 && !(rettv->v_type == VAR_LIST 2058 && rettv->vval.v_list != NULL) 2059 && !(rettv->v_type == VAR_BLOB 2060 && rettv->vval.v_blob != NULL)) 2061 { 2062 if (!quiet) 2063 emsg(_("E709: [:] requires a List or Blob value")); 2064 clear_tv(&var1); 2065 return NULL; 2066 } 2067 p = skipwhite(p + 1); 2068 if (*p == ']') 2069 lp->ll_empty2 = TRUE; 2070 else 2071 { 2072 lp->ll_empty2 = FALSE; 2073 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */ 2074 { 2075 clear_tv(&var1); 2076 return NULL; 2077 } 2078 if (tv_get_string_chk(&var2) == NULL) 2079 { 2080 /* not a number or string */ 2081 clear_tv(&var1); 2082 clear_tv(&var2); 2083 return NULL; 2084 } 2085 } 2086 lp->ll_range = TRUE; 2087 } 2088 else 2089 lp->ll_range = FALSE; 2090 2091 if (*p != ']') 2092 { 2093 if (!quiet) 2094 emsg(_(e_missbrac)); 2095 clear_tv(&var1); 2096 clear_tv(&var2); 2097 return NULL; 2098 } 2099 2100 /* Skip to past ']'. */ 2101 ++p; 2102 } 2103 2104 if (lp->ll_tv->v_type == VAR_DICT) 2105 { 2106 if (len == -1) 2107 { 2108 /* "[key]": get key from "var1" */ 2109 key = tv_get_string_chk(&var1); /* is number or string */ 2110 if (key == NULL) 2111 { 2112 clear_tv(&var1); 2113 return NULL; 2114 } 2115 } 2116 lp->ll_list = NULL; 2117 lp->ll_dict = lp->ll_tv->vval.v_dict; 2118 lp->ll_di = dict_find(lp->ll_dict, key, len); 2119 2120 /* When assigning to a scope dictionary check that a function and 2121 * variable name is valid (only variable name unless it is l: or 2122 * g: dictionary). Disallow overwriting a builtin function. */ 2123 if (rettv != NULL && lp->ll_dict->dv_scope != 0) 2124 { 2125 int prevval; 2126 int wrong; 2127 2128 if (len != -1) 2129 { 2130 prevval = key[len]; 2131 key[len] = NUL; 2132 } 2133 else 2134 prevval = 0; /* avoid compiler warning */ 2135 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE 2136 && rettv->v_type == VAR_FUNC 2137 && var_check_func_name(key, lp->ll_di == NULL)) 2138 || !valid_varname(key); 2139 if (len != -1) 2140 key[len] = prevval; 2141 if (wrong) 2142 return NULL; 2143 } 2144 2145 if (lp->ll_di == NULL) 2146 { 2147 // Can't add "v:" or "a:" variable. 2148 if (lp->ll_dict == &vimvardict 2149 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht()) 2150 { 2151 semsg(_(e_illvar), name); 2152 clear_tv(&var1); 2153 return NULL; 2154 } 2155 2156 // Key does not exist in dict: may need to add it. 2157 if (*p == '[' || *p == '.' || unlet) 2158 { 2159 if (!quiet) 2160 semsg(_(e_dictkey), key); 2161 clear_tv(&var1); 2162 return NULL; 2163 } 2164 if (len == -1) 2165 lp->ll_newkey = vim_strsave(key); 2166 else 2167 lp->ll_newkey = vim_strnsave(key, len); 2168 clear_tv(&var1); 2169 if (lp->ll_newkey == NULL) 2170 p = NULL; 2171 break; 2172 } 2173 /* existing variable, need to check if it can be changed */ 2174 else if ((flags & GLV_READ_ONLY) == 0 2175 && var_check_ro(lp->ll_di->di_flags, name, FALSE)) 2176 { 2177 clear_tv(&var1); 2178 return NULL; 2179 } 2180 2181 clear_tv(&var1); 2182 lp->ll_tv = &lp->ll_di->di_tv; 2183 } 2184 else if (lp->ll_tv->v_type == VAR_BLOB) 2185 { 2186 long bloblen = blob_len(lp->ll_tv->vval.v_blob); 2187 2188 /* 2189 * Get the number and item for the only or first index of the List. 2190 */ 2191 if (empty1) 2192 lp->ll_n1 = 0; 2193 else 2194 // is number or string 2195 lp->ll_n1 = (long)tv_get_number(&var1); 2196 clear_tv(&var1); 2197 2198 if (lp->ll_n1 < 0 2199 || lp->ll_n1 > bloblen 2200 || (lp->ll_range && lp->ll_n1 == bloblen)) 2201 { 2202 if (!quiet) 2203 semsg(_(e_blobidx), lp->ll_n1); 2204 clear_tv(&var2); 2205 return NULL; 2206 } 2207 if (lp->ll_range && !lp->ll_empty2) 2208 { 2209 lp->ll_n2 = (long)tv_get_number(&var2); 2210 clear_tv(&var2); 2211 if (lp->ll_n2 < 0 2212 || lp->ll_n2 >= bloblen 2213 || lp->ll_n2 < lp->ll_n1) 2214 { 2215 if (!quiet) 2216 semsg(_(e_blobidx), lp->ll_n2); 2217 return NULL; 2218 } 2219 } 2220 lp->ll_blob = lp->ll_tv->vval.v_blob; 2221 lp->ll_tv = NULL; 2222 break; 2223 } 2224 else 2225 { 2226 /* 2227 * Get the number and item for the only or first index of the List. 2228 */ 2229 if (empty1) 2230 lp->ll_n1 = 0; 2231 else 2232 /* is number or string */ 2233 lp->ll_n1 = (long)tv_get_number(&var1); 2234 clear_tv(&var1); 2235 2236 lp->ll_dict = NULL; 2237 lp->ll_list = lp->ll_tv->vval.v_list; 2238 lp->ll_li = list_find(lp->ll_list, lp->ll_n1); 2239 if (lp->ll_li == NULL) 2240 { 2241 if (lp->ll_n1 < 0) 2242 { 2243 lp->ll_n1 = 0; 2244 lp->ll_li = list_find(lp->ll_list, lp->ll_n1); 2245 } 2246 } 2247 if (lp->ll_li == NULL) 2248 { 2249 clear_tv(&var2); 2250 if (!quiet) 2251 semsg(_(e_listidx), lp->ll_n1); 2252 return NULL; 2253 } 2254 2255 /* 2256 * May need to find the item or absolute index for the second 2257 * index of a range. 2258 * When no index given: "lp->ll_empty2" is TRUE. 2259 * Otherwise "lp->ll_n2" is set to the second index. 2260 */ 2261 if (lp->ll_range && !lp->ll_empty2) 2262 { 2263 lp->ll_n2 = (long)tv_get_number(&var2); 2264 /* is number or string */ 2265 clear_tv(&var2); 2266 if (lp->ll_n2 < 0) 2267 { 2268 ni = list_find(lp->ll_list, lp->ll_n2); 2269 if (ni == NULL) 2270 { 2271 if (!quiet) 2272 semsg(_(e_listidx), lp->ll_n2); 2273 return NULL; 2274 } 2275 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni); 2276 } 2277 2278 /* Check that lp->ll_n2 isn't before lp->ll_n1. */ 2279 if (lp->ll_n1 < 0) 2280 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li); 2281 if (lp->ll_n2 < lp->ll_n1) 2282 { 2283 if (!quiet) 2284 semsg(_(e_listidx), lp->ll_n2); 2285 return NULL; 2286 } 2287 } 2288 2289 lp->ll_tv = &lp->ll_li->li_tv; 2290 } 2291 } 2292 2293 clear_tv(&var1); 2294 return p; 2295 } 2296 2297 /* 2298 * Clear lval "lp" that was filled by get_lval(). 2299 */ 2300 void 2301 clear_lval(lval_T *lp) 2302 { 2303 vim_free(lp->ll_exp_name); 2304 vim_free(lp->ll_newkey); 2305 } 2306 2307 /* 2308 * Set a variable that was parsed by get_lval() to "rettv". 2309 * "endp" points to just after the parsed name. 2310 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=", 2311 * "%" for "%=", "." for ".=" or "=" for "=". 2312 */ 2313 static void 2314 set_var_lval( 2315 lval_T *lp, 2316 char_u *endp, 2317 typval_T *rettv, 2318 int copy, 2319 char_u *op) 2320 { 2321 int cc; 2322 listitem_T *ri; 2323 dictitem_T *di; 2324 2325 if (lp->ll_tv == NULL) 2326 { 2327 cc = *endp; 2328 *endp = NUL; 2329 if (lp->ll_blob != NULL) 2330 { 2331 int error = FALSE, val; 2332 2333 if (op != NULL && *op != '=') 2334 { 2335 semsg(_(e_letwrong), op); 2336 return; 2337 } 2338 2339 if (lp->ll_range && rettv->v_type == VAR_BLOB) 2340 { 2341 int il, ir; 2342 2343 if (lp->ll_empty2) 2344 lp->ll_n2 = blob_len(lp->ll_blob) - 1; 2345 2346 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob)) 2347 { 2348 emsg(_("E972: Blob value does not have the right number of bytes")); 2349 return; 2350 } 2351 if (lp->ll_empty2) 2352 lp->ll_n2 = blob_len(lp->ll_blob); 2353 2354 ir = 0; 2355 for (il = lp->ll_n1; il <= lp->ll_n2; il++) 2356 blob_set(lp->ll_blob, il, 2357 blob_get(rettv->vval.v_blob, ir++)); 2358 } 2359 else 2360 { 2361 val = (int)tv_get_number_chk(rettv, &error); 2362 if (!error) 2363 { 2364 garray_T *gap = &lp->ll_blob->bv_ga; 2365 2366 // Allow for appending a byte. Setting a byte beyond 2367 // the end is an error otherwise. 2368 if (lp->ll_n1 < gap->ga_len 2369 || (lp->ll_n1 == gap->ga_len 2370 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK)) 2371 { 2372 blob_set(lp->ll_blob, lp->ll_n1, val); 2373 if (lp->ll_n1 == gap->ga_len) 2374 ++gap->ga_len; 2375 } 2376 // error for invalid range was already given in get_lval() 2377 } 2378 } 2379 } 2380 else if (op != NULL && *op != '=') 2381 { 2382 typval_T tv; 2383 2384 // handle +=, -=, *=, /=, %= and .= 2385 di = NULL; 2386 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name), 2387 &tv, &di, TRUE, FALSE) == OK) 2388 { 2389 if ((di == NULL 2390 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE) 2391 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE))) 2392 && tv_op(&tv, rettv, op) == OK) 2393 set_var(lp->ll_name, &tv, FALSE); 2394 clear_tv(&tv); 2395 } 2396 } 2397 else 2398 set_var(lp->ll_name, rettv, copy); 2399 *endp = cc; 2400 } 2401 else if (var_check_lock(lp->ll_newkey == NULL 2402 ? lp->ll_tv->v_lock 2403 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE)) 2404 ; 2405 else if (lp->ll_range) 2406 { 2407 listitem_T *ll_li = lp->ll_li; 2408 int ll_n1 = lp->ll_n1; 2409 2410 /* 2411 * Check whether any of the list items is locked 2412 */ 2413 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; ) 2414 { 2415 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE)) 2416 return; 2417 ri = ri->li_next; 2418 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1)) 2419 break; 2420 ll_li = ll_li->li_next; 2421 ++ll_n1; 2422 } 2423 2424 /* 2425 * Assign the List values to the list items. 2426 */ 2427 for (ri = rettv->vval.v_list->lv_first; ri != NULL; ) 2428 { 2429 if (op != NULL && *op != '=') 2430 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op); 2431 else 2432 { 2433 clear_tv(&lp->ll_li->li_tv); 2434 copy_tv(&ri->li_tv, &lp->ll_li->li_tv); 2435 } 2436 ri = ri->li_next; 2437 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1)) 2438 break; 2439 if (lp->ll_li->li_next == NULL) 2440 { 2441 /* Need to add an empty item. */ 2442 if (list_append_number(lp->ll_list, 0) == FAIL) 2443 { 2444 ri = NULL; 2445 break; 2446 } 2447 } 2448 lp->ll_li = lp->ll_li->li_next; 2449 ++lp->ll_n1; 2450 } 2451 if (ri != NULL) 2452 emsg(_("E710: List value has more items than target")); 2453 else if (lp->ll_empty2 2454 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL) 2455 : lp->ll_n1 != lp->ll_n2) 2456 emsg(_("E711: List value has not enough items")); 2457 } 2458 else 2459 { 2460 /* 2461 * Assign to a List or Dictionary item. 2462 */ 2463 if (lp->ll_newkey != NULL) 2464 { 2465 if (op != NULL && *op != '=') 2466 { 2467 semsg(_(e_letwrong), op); 2468 return; 2469 } 2470 2471 /* Need to add an item to the Dictionary. */ 2472 di = dictitem_alloc(lp->ll_newkey); 2473 if (di == NULL) 2474 return; 2475 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL) 2476 { 2477 vim_free(di); 2478 return; 2479 } 2480 lp->ll_tv = &di->di_tv; 2481 } 2482 else if (op != NULL && *op != '=') 2483 { 2484 tv_op(lp->ll_tv, rettv, op); 2485 return; 2486 } 2487 else 2488 clear_tv(lp->ll_tv); 2489 2490 /* 2491 * Assign the value to the variable or list item. 2492 */ 2493 if (copy) 2494 copy_tv(rettv, lp->ll_tv); 2495 else 2496 { 2497 *lp->ll_tv = *rettv; 2498 lp->ll_tv->v_lock = 0; 2499 init_tv(rettv); 2500 } 2501 } 2502 } 2503 2504 /* 2505 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2" 2506 * and "tv1 .= tv2" 2507 * Returns OK or FAIL. 2508 */ 2509 static int 2510 tv_op(typval_T *tv1, typval_T *tv2, char_u *op) 2511 { 2512 varnumber_T n; 2513 char_u numbuf[NUMBUFLEN]; 2514 char_u *s; 2515 2516 /* Can't do anything with a Funcref, Dict, v:true on the right. */ 2517 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT 2518 && tv2->v_type != VAR_SPECIAL) 2519 { 2520 switch (tv1->v_type) 2521 { 2522 case VAR_UNKNOWN: 2523 case VAR_DICT: 2524 case VAR_FUNC: 2525 case VAR_PARTIAL: 2526 case VAR_SPECIAL: 2527 case VAR_JOB: 2528 case VAR_CHANNEL: 2529 break; 2530 2531 case VAR_BLOB: 2532 if (*op != '+' || tv2->v_type != VAR_BLOB) 2533 break; 2534 // BLOB += BLOB 2535 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL) 2536 { 2537 blob_T *b1 = tv1->vval.v_blob; 2538 blob_T *b2 = tv2->vval.v_blob; 2539 int i, len = blob_len(b2); 2540 for (i = 0; i < len; i++) 2541 ga_append(&b1->bv_ga, blob_get(b2, i)); 2542 } 2543 return OK; 2544 2545 case VAR_LIST: 2546 if (*op != '+' || tv2->v_type != VAR_LIST) 2547 break; 2548 // List += List 2549 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL) 2550 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL); 2551 return OK; 2552 2553 case VAR_NUMBER: 2554 case VAR_STRING: 2555 if (tv2->v_type == VAR_LIST) 2556 break; 2557 if (vim_strchr((char_u *)"+-*/%", *op) != NULL) 2558 { 2559 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr 2560 n = tv_get_number(tv1); 2561 #ifdef FEAT_FLOAT 2562 if (tv2->v_type == VAR_FLOAT) 2563 { 2564 float_T f = n; 2565 2566 if (*op == '%') 2567 break; 2568 switch (*op) 2569 { 2570 case '+': f += tv2->vval.v_float; break; 2571 case '-': f -= tv2->vval.v_float; break; 2572 case '*': f *= tv2->vval.v_float; break; 2573 case '/': f /= tv2->vval.v_float; break; 2574 } 2575 clear_tv(tv1); 2576 tv1->v_type = VAR_FLOAT; 2577 tv1->vval.v_float = f; 2578 } 2579 else 2580 #endif 2581 { 2582 switch (*op) 2583 { 2584 case '+': n += tv_get_number(tv2); break; 2585 case '-': n -= tv_get_number(tv2); break; 2586 case '*': n *= tv_get_number(tv2); break; 2587 case '/': n = num_divide(n, tv_get_number(tv2)); break; 2588 case '%': n = num_modulus(n, tv_get_number(tv2)); break; 2589 } 2590 clear_tv(tv1); 2591 tv1->v_type = VAR_NUMBER; 2592 tv1->vval.v_number = n; 2593 } 2594 } 2595 else 2596 { 2597 if (tv2->v_type == VAR_FLOAT) 2598 break; 2599 2600 // str .= str 2601 s = tv_get_string(tv1); 2602 s = concat_str(s, tv_get_string_buf(tv2, numbuf)); 2603 clear_tv(tv1); 2604 tv1->v_type = VAR_STRING; 2605 tv1->vval.v_string = s; 2606 } 2607 return OK; 2608 2609 case VAR_FLOAT: 2610 #ifdef FEAT_FLOAT 2611 { 2612 float_T f; 2613 2614 if (*op == '%' || *op == '.' 2615 || (tv2->v_type != VAR_FLOAT 2616 && tv2->v_type != VAR_NUMBER 2617 && tv2->v_type != VAR_STRING)) 2618 break; 2619 if (tv2->v_type == VAR_FLOAT) 2620 f = tv2->vval.v_float; 2621 else 2622 f = tv_get_number(tv2); 2623 switch (*op) 2624 { 2625 case '+': tv1->vval.v_float += f; break; 2626 case '-': tv1->vval.v_float -= f; break; 2627 case '*': tv1->vval.v_float *= f; break; 2628 case '/': tv1->vval.v_float /= f; break; 2629 } 2630 } 2631 #endif 2632 return OK; 2633 } 2634 } 2635 2636 semsg(_(e_letwrong), op); 2637 return FAIL; 2638 } 2639 2640 /* 2641 * Evaluate the expression used in a ":for var in expr" command. 2642 * "arg" points to "var". 2643 * Set "*errp" to TRUE for an error, FALSE otherwise; 2644 * Return a pointer that holds the info. Null when there is an error. 2645 */ 2646 void * 2647 eval_for_line( 2648 char_u *arg, 2649 int *errp, 2650 char_u **nextcmdp, 2651 int skip) 2652 { 2653 forinfo_T *fi; 2654 char_u *expr; 2655 typval_T tv; 2656 list_T *l; 2657 2658 *errp = TRUE; /* default: there is an error */ 2659 2660 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T)); 2661 if (fi == NULL) 2662 return NULL; 2663 2664 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon); 2665 if (expr == NULL) 2666 return fi; 2667 2668 expr = skipwhite(expr); 2669 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2])) 2670 { 2671 emsg(_("E690: Missing \"in\" after :for")); 2672 return fi; 2673 } 2674 2675 if (skip) 2676 ++emsg_skip; 2677 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK) 2678 { 2679 *errp = FALSE; 2680 if (!skip) 2681 { 2682 if (tv.v_type == VAR_LIST) 2683 { 2684 l = tv.vval.v_list; 2685 if (l == NULL) 2686 { 2687 // a null list is like an empty list: do nothing 2688 clear_tv(&tv); 2689 } 2690 else 2691 { 2692 // No need to increment the refcount, it's already set for 2693 // the list being used in "tv". 2694 fi->fi_list = l; 2695 list_add_watch(l, &fi->fi_lw); 2696 fi->fi_lw.lw_item = l->lv_first; 2697 } 2698 } 2699 else if (tv.v_type == VAR_BLOB) 2700 { 2701 fi->fi_bi = 0; 2702 if (tv.vval.v_blob != NULL) 2703 { 2704 typval_T btv; 2705 2706 // Make a copy, so that the iteration still works when the 2707 // blob is changed. 2708 blob_copy(&tv, &btv); 2709 fi->fi_blob = btv.vval.v_blob; 2710 } 2711 clear_tv(&tv); 2712 } 2713 else 2714 { 2715 emsg(_(e_listreq)); 2716 clear_tv(&tv); 2717 } 2718 } 2719 } 2720 if (skip) 2721 --emsg_skip; 2722 2723 return fi; 2724 } 2725 2726 /* 2727 * Use the first item in a ":for" list. Advance to the next. 2728 * Assign the values to the variable (list). "arg" points to the first one. 2729 * Return TRUE when a valid item was found, FALSE when at end of list or 2730 * something wrong. 2731 */ 2732 int 2733 next_for_item(void *fi_void, char_u *arg) 2734 { 2735 forinfo_T *fi = (forinfo_T *)fi_void; 2736 int result; 2737 listitem_T *item; 2738 2739 if (fi->fi_blob != NULL) 2740 { 2741 typval_T tv; 2742 2743 if (fi->fi_bi >= blob_len(fi->fi_blob)) 2744 return FALSE; 2745 tv.v_type = VAR_NUMBER; 2746 tv.v_lock = VAR_FIXED; 2747 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi); 2748 ++fi->fi_bi; 2749 return ex_let_vars(arg, &tv, TRUE, 2750 fi->fi_semicolon, fi->fi_varcount, NULL) == OK; 2751 } 2752 2753 item = fi->fi_lw.lw_item; 2754 if (item == NULL) 2755 result = FALSE; 2756 else 2757 { 2758 fi->fi_lw.lw_item = item->li_next; 2759 result = (ex_let_vars(arg, &item->li_tv, TRUE, 2760 fi->fi_semicolon, fi->fi_varcount, NULL) == OK); 2761 } 2762 return result; 2763 } 2764 2765 /* 2766 * Free the structure used to store info used by ":for". 2767 */ 2768 void 2769 free_for_info(void *fi_void) 2770 { 2771 forinfo_T *fi = (forinfo_T *)fi_void; 2772 2773 if (fi != NULL && fi->fi_list != NULL) 2774 { 2775 list_rem_watch(fi->fi_list, &fi->fi_lw); 2776 list_unref(fi->fi_list); 2777 } 2778 if (fi != NULL && fi->fi_blob != NULL) 2779 blob_unref(fi->fi_blob); 2780 vim_free(fi); 2781 } 2782 2783 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) 2784 2785 void 2786 set_context_for_expression( 2787 expand_T *xp, 2788 char_u *arg, 2789 cmdidx_T cmdidx) 2790 { 2791 int got_eq = FALSE; 2792 int c; 2793 char_u *p; 2794 2795 if (cmdidx == CMD_let) 2796 { 2797 xp->xp_context = EXPAND_USER_VARS; 2798 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL) 2799 { 2800 /* ":let var1 var2 ...": find last space. */ 2801 for (p = arg + STRLEN(arg); p >= arg; ) 2802 { 2803 xp->xp_pattern = p; 2804 MB_PTR_BACK(arg, p); 2805 if (VIM_ISWHITE(*p)) 2806 break; 2807 } 2808 return; 2809 } 2810 } 2811 else 2812 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS 2813 : EXPAND_EXPRESSION; 2814 while ((xp->xp_pattern = vim_strpbrk(arg, 2815 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL) 2816 { 2817 c = *xp->xp_pattern; 2818 if (c == '&') 2819 { 2820 c = xp->xp_pattern[1]; 2821 if (c == '&') 2822 { 2823 ++xp->xp_pattern; 2824 xp->xp_context = cmdidx != CMD_let || got_eq 2825 ? EXPAND_EXPRESSION : EXPAND_NOTHING; 2826 } 2827 else if (c != ' ') 2828 { 2829 xp->xp_context = EXPAND_SETTINGS; 2830 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':') 2831 xp->xp_pattern += 2; 2832 2833 } 2834 } 2835 else if (c == '$') 2836 { 2837 /* environment variable */ 2838 xp->xp_context = EXPAND_ENV_VARS; 2839 } 2840 else if (c == '=') 2841 { 2842 got_eq = TRUE; 2843 xp->xp_context = EXPAND_EXPRESSION; 2844 } 2845 else if (c == '#' 2846 && xp->xp_context == EXPAND_EXPRESSION) 2847 { 2848 /* Autoload function/variable contains '#'. */ 2849 break; 2850 } 2851 else if ((c == '<' || c == '#') 2852 && xp->xp_context == EXPAND_FUNCTIONS 2853 && vim_strchr(xp->xp_pattern, '(') == NULL) 2854 { 2855 /* Function name can start with "<SNR>" and contain '#'. */ 2856 break; 2857 } 2858 else if (cmdidx != CMD_let || got_eq) 2859 { 2860 if (c == '"') /* string */ 2861 { 2862 while ((c = *++xp->xp_pattern) != NUL && c != '"') 2863 if (c == '\\' && xp->xp_pattern[1] != NUL) 2864 ++xp->xp_pattern; 2865 xp->xp_context = EXPAND_NOTHING; 2866 } 2867 else if (c == '\'') /* literal string */ 2868 { 2869 /* Trick: '' is like stopping and starting a literal string. */ 2870 while ((c = *++xp->xp_pattern) != NUL && c != '\'') 2871 /* skip */ ; 2872 xp->xp_context = EXPAND_NOTHING; 2873 } 2874 else if (c == '|') 2875 { 2876 if (xp->xp_pattern[1] == '|') 2877 { 2878 ++xp->xp_pattern; 2879 xp->xp_context = EXPAND_EXPRESSION; 2880 } 2881 else 2882 xp->xp_context = EXPAND_COMMANDS; 2883 } 2884 else 2885 xp->xp_context = EXPAND_EXPRESSION; 2886 } 2887 else 2888 /* Doesn't look like something valid, expand as an expression 2889 * anyway. */ 2890 xp->xp_context = EXPAND_EXPRESSION; 2891 arg = xp->xp_pattern; 2892 if (*arg != NUL) 2893 while ((c = *++arg) != NUL && (c == ' ' || c == '\t')) 2894 /* skip */ ; 2895 } 2896 xp->xp_pattern = arg; 2897 } 2898 2899 #endif /* FEAT_CMDL_COMPL */ 2900 2901 /* 2902 * ":unlet[!] var1 ... " command. 2903 */ 2904 void 2905 ex_unlet(exarg_T *eap) 2906 { 2907 ex_unletlock(eap, eap->arg, 0); 2908 } 2909 2910 /* 2911 * ":lockvar" and ":unlockvar" commands 2912 */ 2913 void 2914 ex_lockvar(exarg_T *eap) 2915 { 2916 char_u *arg = eap->arg; 2917 int deep = 2; 2918 2919 if (eap->forceit) 2920 deep = -1; 2921 else if (vim_isdigit(*arg)) 2922 { 2923 deep = getdigits(&arg); 2924 arg = skipwhite(arg); 2925 } 2926 2927 ex_unletlock(eap, arg, deep); 2928 } 2929 2930 /* 2931 * ":unlet", ":lockvar" and ":unlockvar" are quite similar. 2932 */ 2933 static void 2934 ex_unletlock( 2935 exarg_T *eap, 2936 char_u *argstart, 2937 int deep) 2938 { 2939 char_u *arg = argstart; 2940 char_u *name_end; 2941 int error = FALSE; 2942 lval_T lv; 2943 2944 do 2945 { 2946 if (*arg == '$') 2947 { 2948 char_u *name = ++arg; 2949 2950 if (get_env_len(&arg) == 0) 2951 { 2952 semsg(_(e_invarg2), name - 1); 2953 return; 2954 } 2955 vim_unsetenv(name); 2956 arg = skipwhite(arg); 2957 continue; 2958 } 2959 2960 /* Parse the name and find the end. */ 2961 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0, 2962 FNE_CHECK_START); 2963 if (lv.ll_name == NULL) 2964 error = TRUE; /* error but continue parsing */ 2965 if (name_end == NULL || (!VIM_ISWHITE(*name_end) 2966 && !ends_excmd(*name_end))) 2967 { 2968 if (name_end != NULL) 2969 { 2970 emsg_severe = TRUE; 2971 emsg(_(e_trailing)); 2972 } 2973 if (!(eap->skip || error)) 2974 clear_lval(&lv); 2975 break; 2976 } 2977 2978 if (!error && !eap->skip) 2979 { 2980 if (eap->cmdidx == CMD_unlet) 2981 { 2982 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL) 2983 error = TRUE; 2984 } 2985 else 2986 { 2987 if (do_lock_var(&lv, name_end, deep, 2988 eap->cmdidx == CMD_lockvar) == FAIL) 2989 error = TRUE; 2990 } 2991 } 2992 2993 if (!eap->skip) 2994 clear_lval(&lv); 2995 2996 arg = skipwhite(name_end); 2997 } while (!ends_excmd(*arg)); 2998 2999 eap->nextcmd = check_nextcmd(arg); 3000 } 3001 3002 static int 3003 do_unlet_var( 3004 lval_T *lp, 3005 char_u *name_end, 3006 int forceit) 3007 { 3008 int ret = OK; 3009 int cc; 3010 3011 if (lp->ll_tv == NULL) 3012 { 3013 cc = *name_end; 3014 *name_end = NUL; 3015 3016 /* Normal name or expanded name. */ 3017 if (do_unlet(lp->ll_name, forceit) == FAIL) 3018 ret = FAIL; 3019 *name_end = cc; 3020 } 3021 else if ((lp->ll_list != NULL 3022 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE)) 3023 || (lp->ll_dict != NULL 3024 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE))) 3025 return FAIL; 3026 else if (lp->ll_range) 3027 { 3028 listitem_T *li; 3029 listitem_T *ll_li = lp->ll_li; 3030 int ll_n1 = lp->ll_n1; 3031 3032 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1)) 3033 { 3034 li = ll_li->li_next; 3035 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE)) 3036 return FAIL; 3037 ll_li = li; 3038 ++ll_n1; 3039 } 3040 3041 /* Delete a range of List items. */ 3042 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1)) 3043 { 3044 li = lp->ll_li->li_next; 3045 listitem_remove(lp->ll_list, lp->ll_li); 3046 lp->ll_li = li; 3047 ++lp->ll_n1; 3048 } 3049 } 3050 else 3051 { 3052 if (lp->ll_list != NULL) 3053 /* unlet a List item. */ 3054 listitem_remove(lp->ll_list, lp->ll_li); 3055 else 3056 /* unlet a Dictionary item. */ 3057 dictitem_remove(lp->ll_dict, lp->ll_di); 3058 } 3059 3060 return ret; 3061 } 3062 3063 /* 3064 * "unlet" a variable. Return OK if it existed, FAIL if not. 3065 * When "forceit" is TRUE don't complain if the variable doesn't exist. 3066 */ 3067 int 3068 do_unlet(char_u *name, int forceit) 3069 { 3070 hashtab_T *ht; 3071 hashitem_T *hi; 3072 char_u *varname; 3073 dict_T *d; 3074 dictitem_T *di; 3075 3076 ht = find_var_ht(name, &varname); 3077 if (ht != NULL && *varname != NUL) 3078 { 3079 d = get_current_funccal_dict(ht); 3080 if (d == NULL) 3081 { 3082 if (ht == &globvarht) 3083 d = &globvardict; 3084 else if (ht == &compat_hashtab) 3085 d = &vimvardict; 3086 else 3087 { 3088 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE); 3089 d = di == NULL ? NULL : di->di_tv.vval.v_dict; 3090 } 3091 if (d == NULL) 3092 { 3093 internal_error("do_unlet()"); 3094 return FAIL; 3095 } 3096 } 3097 hi = hash_find(ht, varname); 3098 if (HASHITEM_EMPTY(hi)) 3099 hi = find_hi_in_scoped_ht(name, &ht); 3100 if (hi != NULL && !HASHITEM_EMPTY(hi)) 3101 { 3102 di = HI2DI(hi); 3103 if (var_check_fixed(di->di_flags, name, FALSE) 3104 || var_check_ro(di->di_flags, name, FALSE) 3105 || var_check_lock(d->dv_lock, name, FALSE)) 3106 return FAIL; 3107 3108 delete_var(ht, hi); 3109 return OK; 3110 } 3111 } 3112 if (forceit) 3113 return OK; 3114 semsg(_("E108: No such variable: \"%s\""), name); 3115 return FAIL; 3116 } 3117 3118 /* 3119 * Lock or unlock variable indicated by "lp". 3120 * "deep" is the levels to go (-1 for unlimited); 3121 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar". 3122 */ 3123 static int 3124 do_lock_var( 3125 lval_T *lp, 3126 char_u *name_end, 3127 int deep, 3128 int lock) 3129 { 3130 int ret = OK; 3131 int cc; 3132 dictitem_T *di; 3133 3134 if (deep == 0) /* nothing to do */ 3135 return OK; 3136 3137 if (lp->ll_tv == NULL) 3138 { 3139 cc = *name_end; 3140 *name_end = NUL; 3141 3142 /* Normal name or expanded name. */ 3143 di = find_var(lp->ll_name, NULL, TRUE); 3144 if (di == NULL) 3145 ret = FAIL; 3146 else if ((di->di_flags & DI_FLAGS_FIX) 3147 && di->di_tv.v_type != VAR_DICT 3148 && di->di_tv.v_type != VAR_LIST) 3149 /* For historic reasons this error is not given for a list or dict. 3150 * E.g., the b: dict could be locked/unlocked. */ 3151 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name); 3152 else 3153 { 3154 if (lock) 3155 di->di_flags |= DI_FLAGS_LOCK; 3156 else 3157 di->di_flags &= ~DI_FLAGS_LOCK; 3158 item_lock(&di->di_tv, deep, lock); 3159 } 3160 *name_end = cc; 3161 } 3162 else if (lp->ll_range) 3163 { 3164 listitem_T *li = lp->ll_li; 3165 3166 /* (un)lock a range of List items. */ 3167 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1)) 3168 { 3169 item_lock(&li->li_tv, deep, lock); 3170 li = li->li_next; 3171 ++lp->ll_n1; 3172 } 3173 } 3174 else if (lp->ll_list != NULL) 3175 /* (un)lock a List item. */ 3176 item_lock(&lp->ll_li->li_tv, deep, lock); 3177 else 3178 /* (un)lock a Dictionary item. */ 3179 item_lock(&lp->ll_di->di_tv, deep, lock); 3180 3181 return ret; 3182 } 3183 3184 /* 3185 * Lock or unlock an item. "deep" is nr of levels to go. 3186 */ 3187 static void 3188 item_lock(typval_T *tv, int deep, int lock) 3189 { 3190 static int recurse = 0; 3191 list_T *l; 3192 listitem_T *li; 3193 dict_T *d; 3194 blob_T *b; 3195 hashitem_T *hi; 3196 int todo; 3197 3198 if (recurse >= DICT_MAXNEST) 3199 { 3200 emsg(_("E743: variable nested too deep for (un)lock")); 3201 return; 3202 } 3203 if (deep == 0) 3204 return; 3205 ++recurse; 3206 3207 /* lock/unlock the item itself */ 3208 if (lock) 3209 tv->v_lock |= VAR_LOCKED; 3210 else 3211 tv->v_lock &= ~VAR_LOCKED; 3212 3213 switch (tv->v_type) 3214 { 3215 case VAR_UNKNOWN: 3216 case VAR_NUMBER: 3217 case VAR_STRING: 3218 case VAR_FUNC: 3219 case VAR_PARTIAL: 3220 case VAR_FLOAT: 3221 case VAR_SPECIAL: 3222 case VAR_JOB: 3223 case VAR_CHANNEL: 3224 break; 3225 3226 case VAR_BLOB: 3227 if ((b = tv->vval.v_blob) != NULL) 3228 { 3229 if (lock) 3230 b->bv_lock |= VAR_LOCKED; 3231 else 3232 b->bv_lock &= ~VAR_LOCKED; 3233 } 3234 break; 3235 case VAR_LIST: 3236 if ((l = tv->vval.v_list) != NULL) 3237 { 3238 if (lock) 3239 l->lv_lock |= VAR_LOCKED; 3240 else 3241 l->lv_lock &= ~VAR_LOCKED; 3242 if (deep < 0 || deep > 1) 3243 /* recursive: lock/unlock the items the List contains */ 3244 for (li = l->lv_first; li != NULL; li = li->li_next) 3245 item_lock(&li->li_tv, deep - 1, lock); 3246 } 3247 break; 3248 case VAR_DICT: 3249 if ((d = tv->vval.v_dict) != NULL) 3250 { 3251 if (lock) 3252 d->dv_lock |= VAR_LOCKED; 3253 else 3254 d->dv_lock &= ~VAR_LOCKED; 3255 if (deep < 0 || deep > 1) 3256 { 3257 /* recursive: lock/unlock the items the List contains */ 3258 todo = (int)d->dv_hashtab.ht_used; 3259 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi) 3260 { 3261 if (!HASHITEM_EMPTY(hi)) 3262 { 3263 --todo; 3264 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock); 3265 } 3266 } 3267 } 3268 } 3269 } 3270 --recurse; 3271 } 3272 3273 #if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO) 3274 /* 3275 * Delete all "menutrans_" variables. 3276 */ 3277 void 3278 del_menutrans_vars(void) 3279 { 3280 hashitem_T *hi; 3281 int todo; 3282 3283 hash_lock(&globvarht); 3284 todo = (int)globvarht.ht_used; 3285 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi) 3286 { 3287 if (!HASHITEM_EMPTY(hi)) 3288 { 3289 --todo; 3290 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0) 3291 delete_var(&globvarht, hi); 3292 } 3293 } 3294 hash_unlock(&globvarht); 3295 } 3296 #endif 3297 3298 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) 3299 3300 /* 3301 * Local string buffer for the next two functions to store a variable name 3302 * with its prefix. Allocated in cat_prefix_varname(), freed later in 3303 * get_user_var_name(). 3304 */ 3305 3306 static char_u *varnamebuf = NULL; 3307 static int varnamebuflen = 0; 3308 3309 /* 3310 * Function to concatenate a prefix and a variable name. 3311 */ 3312 static char_u * 3313 cat_prefix_varname(int prefix, char_u *name) 3314 { 3315 int len; 3316 3317 len = (int)STRLEN(name) + 3; 3318 if (len > varnamebuflen) 3319 { 3320 vim_free(varnamebuf); 3321 len += 10; /* some additional space */ 3322 varnamebuf = alloc(len); 3323 if (varnamebuf == NULL) 3324 { 3325 varnamebuflen = 0; 3326 return NULL; 3327 } 3328 varnamebuflen = len; 3329 } 3330 *varnamebuf = prefix; 3331 varnamebuf[1] = ':'; 3332 STRCPY(varnamebuf + 2, name); 3333 return varnamebuf; 3334 } 3335 3336 /* 3337 * Function given to ExpandGeneric() to obtain the list of user defined 3338 * (global/buffer/window/built-in) variable names. 3339 */ 3340 char_u * 3341 get_user_var_name(expand_T *xp, int idx) 3342 { 3343 static long_u gdone; 3344 static long_u bdone; 3345 static long_u wdone; 3346 static long_u tdone; 3347 static int vidx; 3348 static hashitem_T *hi; 3349 hashtab_T *ht; 3350 3351 if (idx == 0) 3352 { 3353 gdone = bdone = wdone = vidx = 0; 3354 tdone = 0; 3355 } 3356 3357 /* Global variables */ 3358 if (gdone < globvarht.ht_used) 3359 { 3360 if (gdone++ == 0) 3361 hi = globvarht.ht_array; 3362 else 3363 ++hi; 3364 while (HASHITEM_EMPTY(hi)) 3365 ++hi; 3366 if (STRNCMP("g:", xp->xp_pattern, 2) == 0) 3367 return cat_prefix_varname('g', hi->hi_key); 3368 return hi->hi_key; 3369 } 3370 3371 /* b: variables */ 3372 ht = &curbuf->b_vars->dv_hashtab; 3373 if (bdone < ht->ht_used) 3374 { 3375 if (bdone++ == 0) 3376 hi = ht->ht_array; 3377 else 3378 ++hi; 3379 while (HASHITEM_EMPTY(hi)) 3380 ++hi; 3381 return cat_prefix_varname('b', hi->hi_key); 3382 } 3383 3384 /* w: variables */ 3385 ht = &curwin->w_vars->dv_hashtab; 3386 if (wdone < ht->ht_used) 3387 { 3388 if (wdone++ == 0) 3389 hi = ht->ht_array; 3390 else 3391 ++hi; 3392 while (HASHITEM_EMPTY(hi)) 3393 ++hi; 3394 return cat_prefix_varname('w', hi->hi_key); 3395 } 3396 3397 /* t: variables */ 3398 ht = &curtab->tp_vars->dv_hashtab; 3399 if (tdone < ht->ht_used) 3400 { 3401 if (tdone++ == 0) 3402 hi = ht->ht_array; 3403 else 3404 ++hi; 3405 while (HASHITEM_EMPTY(hi)) 3406 ++hi; 3407 return cat_prefix_varname('t', hi->hi_key); 3408 } 3409 3410 /* v: variables */ 3411 if (vidx < VV_LEN) 3412 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name); 3413 3414 VIM_CLEAR(varnamebuf); 3415 varnamebuflen = 0; 3416 return NULL; 3417 } 3418 3419 #endif /* FEAT_CMDL_COMPL */ 3420 3421 /* 3422 * Return TRUE if "pat" matches "text". 3423 * Does not use 'cpo' and always uses 'magic'. 3424 */ 3425 static int 3426 pattern_match(char_u *pat, char_u *text, int ic) 3427 { 3428 int matches = FALSE; 3429 char_u *save_cpo; 3430 regmatch_T regmatch; 3431 3432 /* avoid 'l' flag in 'cpoptions' */ 3433 save_cpo = p_cpo; 3434 p_cpo = (char_u *)""; 3435 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING); 3436 if (regmatch.regprog != NULL) 3437 { 3438 regmatch.rm_ic = ic; 3439 matches = vim_regexec_nl(®match, text, (colnr_T)0); 3440 vim_regfree(regmatch.regprog); 3441 } 3442 p_cpo = save_cpo; 3443 return matches; 3444 } 3445 3446 /* 3447 * The "evaluate" argument: When FALSE, the argument is only parsed but not 3448 * executed. The function may return OK, but the rettv will be of type 3449 * VAR_UNKNOWN. The function still returns FAIL for a syntax error. 3450 */ 3451 3452 /* 3453 * Handle zero level expression. 3454 * This calls eval1() and handles error message and nextcmd. 3455 * Put the result in "rettv" when returning OK and "evaluate" is TRUE. 3456 * Note: "rettv.v_lock" is not set. 3457 * Return OK or FAIL. 3458 */ 3459 int 3460 eval0( 3461 char_u *arg, 3462 typval_T *rettv, 3463 char_u **nextcmd, 3464 int evaluate) 3465 { 3466 int ret; 3467 char_u *p; 3468 int did_emsg_before = did_emsg; 3469 int called_emsg_before = called_emsg; 3470 3471 p = skipwhite(arg); 3472 ret = eval1(&p, rettv, evaluate); 3473 if (ret == FAIL || !ends_excmd(*p)) 3474 { 3475 if (ret != FAIL) 3476 clear_tv(rettv); 3477 /* 3478 * Report the invalid expression unless the expression evaluation has 3479 * been cancelled due to an aborting error, an interrupt, or an 3480 * exception, or we already gave a more specific error. 3481 * Also check called_emsg for when using assert_fails(). 3482 */ 3483 if (!aborting() && did_emsg == did_emsg_before 3484 && called_emsg == called_emsg_before) 3485 semsg(_(e_invexpr2), arg); 3486 ret = FAIL; 3487 } 3488 if (nextcmd != NULL) 3489 *nextcmd = check_nextcmd(p); 3490 3491 return ret; 3492 } 3493 3494 /* 3495 * Handle top level expression: 3496 * expr2 ? expr1 : expr1 3497 * 3498 * "arg" must point to the first non-white of the expression. 3499 * "arg" is advanced to the next non-white after the recognized expression. 3500 * 3501 * Note: "rettv.v_lock" is not set. 3502 * 3503 * Return OK or FAIL. 3504 */ 3505 int 3506 eval1(char_u **arg, typval_T *rettv, int evaluate) 3507 { 3508 int result; 3509 typval_T var2; 3510 3511 /* 3512 * Get the first variable. 3513 */ 3514 if (eval2(arg, rettv, evaluate) == FAIL) 3515 return FAIL; 3516 3517 if ((*arg)[0] == '?') 3518 { 3519 result = FALSE; 3520 if (evaluate) 3521 { 3522 int error = FALSE; 3523 3524 if (tv_get_number_chk(rettv, &error) != 0) 3525 result = TRUE; 3526 clear_tv(rettv); 3527 if (error) 3528 return FAIL; 3529 } 3530 3531 /* 3532 * Get the second variable. 3533 */ 3534 *arg = skipwhite(*arg + 1); 3535 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */ 3536 return FAIL; 3537 3538 /* 3539 * Check for the ":". 3540 */ 3541 if ((*arg)[0] != ':') 3542 { 3543 emsg(_("E109: Missing ':' after '?'")); 3544 if (evaluate && result) 3545 clear_tv(rettv); 3546 return FAIL; 3547 } 3548 3549 /* 3550 * Get the third variable. 3551 */ 3552 *arg = skipwhite(*arg + 1); 3553 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */ 3554 { 3555 if (evaluate && result) 3556 clear_tv(rettv); 3557 return FAIL; 3558 } 3559 if (evaluate && !result) 3560 *rettv = var2; 3561 } 3562 3563 return OK; 3564 } 3565 3566 /* 3567 * Handle first level expression: 3568 * expr2 || expr2 || expr2 logical OR 3569 * 3570 * "arg" must point to the first non-white of the expression. 3571 * "arg" is advanced to the next non-white after the recognized expression. 3572 * 3573 * Return OK or FAIL. 3574 */ 3575 static int 3576 eval2(char_u **arg, typval_T *rettv, int evaluate) 3577 { 3578 typval_T var2; 3579 long result; 3580 int first; 3581 int error = FALSE; 3582 3583 /* 3584 * Get the first variable. 3585 */ 3586 if (eval3(arg, rettv, evaluate) == FAIL) 3587 return FAIL; 3588 3589 /* 3590 * Repeat until there is no following "||". 3591 */ 3592 first = TRUE; 3593 result = FALSE; 3594 while ((*arg)[0] == '|' && (*arg)[1] == '|') 3595 { 3596 if (evaluate && first) 3597 { 3598 if (tv_get_number_chk(rettv, &error) != 0) 3599 result = TRUE; 3600 clear_tv(rettv); 3601 if (error) 3602 return FAIL; 3603 first = FALSE; 3604 } 3605 3606 /* 3607 * Get the second variable. 3608 */ 3609 *arg = skipwhite(*arg + 2); 3610 if (eval3(arg, &var2, evaluate && !result) == FAIL) 3611 return FAIL; 3612 3613 /* 3614 * Compute the result. 3615 */ 3616 if (evaluate && !result) 3617 { 3618 if (tv_get_number_chk(&var2, &error) != 0) 3619 result = TRUE; 3620 clear_tv(&var2); 3621 if (error) 3622 return FAIL; 3623 } 3624 if (evaluate) 3625 { 3626 rettv->v_type = VAR_NUMBER; 3627 rettv->vval.v_number = result; 3628 } 3629 } 3630 3631 return OK; 3632 } 3633 3634 /* 3635 * Handle second level expression: 3636 * expr3 && expr3 && expr3 logical AND 3637 * 3638 * "arg" must point to the first non-white of the expression. 3639 * "arg" is advanced to the next non-white after the recognized expression. 3640 * 3641 * Return OK or FAIL. 3642 */ 3643 static int 3644 eval3(char_u **arg, typval_T *rettv, int evaluate) 3645 { 3646 typval_T var2; 3647 long result; 3648 int first; 3649 int error = FALSE; 3650 3651 /* 3652 * Get the first variable. 3653 */ 3654 if (eval4(arg, rettv, evaluate) == FAIL) 3655 return FAIL; 3656 3657 /* 3658 * Repeat until there is no following "&&". 3659 */ 3660 first = TRUE; 3661 result = TRUE; 3662 while ((*arg)[0] == '&' && (*arg)[1] == '&') 3663 { 3664 if (evaluate && first) 3665 { 3666 if (tv_get_number_chk(rettv, &error) == 0) 3667 result = FALSE; 3668 clear_tv(rettv); 3669 if (error) 3670 return FAIL; 3671 first = FALSE; 3672 } 3673 3674 /* 3675 * Get the second variable. 3676 */ 3677 *arg = skipwhite(*arg + 2); 3678 if (eval4(arg, &var2, evaluate && result) == FAIL) 3679 return FAIL; 3680 3681 /* 3682 * Compute the result. 3683 */ 3684 if (evaluate && result) 3685 { 3686 if (tv_get_number_chk(&var2, &error) == 0) 3687 result = FALSE; 3688 clear_tv(&var2); 3689 if (error) 3690 return FAIL; 3691 } 3692 if (evaluate) 3693 { 3694 rettv->v_type = VAR_NUMBER; 3695 rettv->vval.v_number = result; 3696 } 3697 } 3698 3699 return OK; 3700 } 3701 3702 /* 3703 * Handle third level expression: 3704 * var1 == var2 3705 * var1 =~ var2 3706 * var1 != var2 3707 * var1 !~ var2 3708 * var1 > var2 3709 * var1 >= var2 3710 * var1 < var2 3711 * var1 <= var2 3712 * var1 is var2 3713 * var1 isnot var2 3714 * 3715 * "arg" must point to the first non-white of the expression. 3716 * "arg" is advanced to the next non-white after the recognized expression. 3717 * 3718 * Return OK or FAIL. 3719 */ 3720 static int 3721 eval4(char_u **arg, typval_T *rettv, int evaluate) 3722 { 3723 typval_T var2; 3724 char_u *p; 3725 int i; 3726 exptype_T type = TYPE_UNKNOWN; 3727 int type_is = FALSE; /* TRUE for "is" and "isnot" */ 3728 int len = 2; 3729 int ic; 3730 3731 /* 3732 * Get the first variable. 3733 */ 3734 if (eval5(arg, rettv, evaluate) == FAIL) 3735 return FAIL; 3736 3737 p = *arg; 3738 switch (p[0]) 3739 { 3740 case '=': if (p[1] == '=') 3741 type = TYPE_EQUAL; 3742 else if (p[1] == '~') 3743 type = TYPE_MATCH; 3744 break; 3745 case '!': if (p[1] == '=') 3746 type = TYPE_NEQUAL; 3747 else if (p[1] == '~') 3748 type = TYPE_NOMATCH; 3749 break; 3750 case '>': if (p[1] != '=') 3751 { 3752 type = TYPE_GREATER; 3753 len = 1; 3754 } 3755 else 3756 type = TYPE_GEQUAL; 3757 break; 3758 case '<': if (p[1] != '=') 3759 { 3760 type = TYPE_SMALLER; 3761 len = 1; 3762 } 3763 else 3764 type = TYPE_SEQUAL; 3765 break; 3766 case 'i': if (p[1] == 's') 3767 { 3768 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') 3769 len = 5; 3770 i = p[len]; 3771 if (!isalnum(i) && i != '_') 3772 { 3773 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL; 3774 type_is = TRUE; 3775 } 3776 } 3777 break; 3778 } 3779 3780 /* 3781 * If there is a comparative operator, use it. 3782 */ 3783 if (type != TYPE_UNKNOWN) 3784 { 3785 /* extra question mark appended: ignore case */ 3786 if (p[len] == '?') 3787 { 3788 ic = TRUE; 3789 ++len; 3790 } 3791 /* extra '#' appended: match case */ 3792 else if (p[len] == '#') 3793 { 3794 ic = FALSE; 3795 ++len; 3796 } 3797 /* nothing appended: use 'ignorecase' */ 3798 else 3799 ic = p_ic; 3800 3801 /* 3802 * Get the second variable. 3803 */ 3804 *arg = skipwhite(p + len); 3805 if (eval5(arg, &var2, evaluate) == FAIL) 3806 { 3807 clear_tv(rettv); 3808 return FAIL; 3809 } 3810 if (evaluate) 3811 { 3812 int ret = typval_compare(rettv, &var2, type, type_is, ic); 3813 3814 clear_tv(&var2); 3815 return ret; 3816 } 3817 } 3818 3819 return OK; 3820 } 3821 3822 /* 3823 * Handle fourth level expression: 3824 * + number addition 3825 * - number subtraction 3826 * . string concatenation (if script version is 1) 3827 * .. string concatenation 3828 * 3829 * "arg" must point to the first non-white of the expression. 3830 * "arg" is advanced to the next non-white after the recognized expression. 3831 * 3832 * Return OK or FAIL. 3833 */ 3834 static int 3835 eval5(char_u **arg, typval_T *rettv, int evaluate) 3836 { 3837 typval_T var2; 3838 typval_T var3; 3839 int op; 3840 varnumber_T n1, n2; 3841 #ifdef FEAT_FLOAT 3842 float_T f1 = 0, f2 = 0; 3843 #endif 3844 char_u *s1, *s2; 3845 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN]; 3846 char_u *p; 3847 int concat; 3848 3849 /* 3850 * Get the first variable. 3851 */ 3852 if (eval6(arg, rettv, evaluate, FALSE) == FAIL) 3853 return FAIL; 3854 3855 /* 3856 * Repeat computing, until no '+', '-' or '.' is following. 3857 */ 3858 for (;;) 3859 { 3860 // "." is only string concatenation when scriptversion is 1 3861 op = **arg; 3862 concat = op == '.' 3863 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2); 3864 if (op != '+' && op != '-' && !concat) 3865 break; 3866 3867 if ((op != '+' || (rettv->v_type != VAR_LIST 3868 && rettv->v_type != VAR_BLOB)) 3869 #ifdef FEAT_FLOAT 3870 && (op == '.' || rettv->v_type != VAR_FLOAT) 3871 #endif 3872 ) 3873 { 3874 /* For "list + ...", an illegal use of the first operand as 3875 * a number cannot be determined before evaluating the 2nd 3876 * operand: if this is also a list, all is ok. 3877 * For "something . ...", "something - ..." or "non-list + ...", 3878 * we know that the first operand needs to be a string or number 3879 * without evaluating the 2nd operand. So check before to avoid 3880 * side effects after an error. */ 3881 if (evaluate && tv_get_string_chk(rettv) == NULL) 3882 { 3883 clear_tv(rettv); 3884 return FAIL; 3885 } 3886 } 3887 3888 /* 3889 * Get the second variable. 3890 */ 3891 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation 3892 ++*arg; 3893 *arg = skipwhite(*arg + 1); 3894 if (eval6(arg, &var2, evaluate, op == '.') == FAIL) 3895 { 3896 clear_tv(rettv); 3897 return FAIL; 3898 } 3899 3900 if (evaluate) 3901 { 3902 /* 3903 * Compute the result. 3904 */ 3905 if (op == '.') 3906 { 3907 s1 = tv_get_string_buf(rettv, buf1); /* already checked */ 3908 s2 = tv_get_string_buf_chk(&var2, buf2); 3909 if (s2 == NULL) /* type error ? */ 3910 { 3911 clear_tv(rettv); 3912 clear_tv(&var2); 3913 return FAIL; 3914 } 3915 p = concat_str(s1, s2); 3916 clear_tv(rettv); 3917 rettv->v_type = VAR_STRING; 3918 rettv->vval.v_string = p; 3919 } 3920 else if (op == '+' && rettv->v_type == VAR_BLOB 3921 && var2.v_type == VAR_BLOB) 3922 { 3923 blob_T *b1 = rettv->vval.v_blob; 3924 blob_T *b2 = var2.vval.v_blob; 3925 blob_T *b = blob_alloc(); 3926 int i; 3927 3928 if (b != NULL) 3929 { 3930 for (i = 0; i < blob_len(b1); i++) 3931 ga_append(&b->bv_ga, blob_get(b1, i)); 3932 for (i = 0; i < blob_len(b2); i++) 3933 ga_append(&b->bv_ga, blob_get(b2, i)); 3934 3935 clear_tv(rettv); 3936 rettv_blob_set(rettv, b); 3937 } 3938 } 3939 else if (op == '+' && rettv->v_type == VAR_LIST 3940 && var2.v_type == VAR_LIST) 3941 { 3942 /* concatenate Lists */ 3943 if (list_concat(rettv->vval.v_list, var2.vval.v_list, 3944 &var3) == FAIL) 3945 { 3946 clear_tv(rettv); 3947 clear_tv(&var2); 3948 return FAIL; 3949 } 3950 clear_tv(rettv); 3951 *rettv = var3; 3952 } 3953 else 3954 { 3955 int error = FALSE; 3956 3957 #ifdef FEAT_FLOAT 3958 if (rettv->v_type == VAR_FLOAT) 3959 { 3960 f1 = rettv->vval.v_float; 3961 n1 = 0; 3962 } 3963 else 3964 #endif 3965 { 3966 n1 = tv_get_number_chk(rettv, &error); 3967 if (error) 3968 { 3969 /* This can only happen for "list + non-list". For 3970 * "non-list + ..." or "something - ...", we returned 3971 * before evaluating the 2nd operand. */ 3972 clear_tv(rettv); 3973 return FAIL; 3974 } 3975 #ifdef FEAT_FLOAT 3976 if (var2.v_type == VAR_FLOAT) 3977 f1 = n1; 3978 #endif 3979 } 3980 #ifdef FEAT_FLOAT 3981 if (var2.v_type == VAR_FLOAT) 3982 { 3983 f2 = var2.vval.v_float; 3984 n2 = 0; 3985 } 3986 else 3987 #endif 3988 { 3989 n2 = tv_get_number_chk(&var2, &error); 3990 if (error) 3991 { 3992 clear_tv(rettv); 3993 clear_tv(&var2); 3994 return FAIL; 3995 } 3996 #ifdef FEAT_FLOAT 3997 if (rettv->v_type == VAR_FLOAT) 3998 f2 = n2; 3999 #endif 4000 } 4001 clear_tv(rettv); 4002 4003 #ifdef FEAT_FLOAT 4004 /* If there is a float on either side the result is a float. */ 4005 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT) 4006 { 4007 if (op == '+') 4008 f1 = f1 + f2; 4009 else 4010 f1 = f1 - f2; 4011 rettv->v_type = VAR_FLOAT; 4012 rettv->vval.v_float = f1; 4013 } 4014 else 4015 #endif 4016 { 4017 if (op == '+') 4018 n1 = n1 + n2; 4019 else 4020 n1 = n1 - n2; 4021 rettv->v_type = VAR_NUMBER; 4022 rettv->vval.v_number = n1; 4023 } 4024 } 4025 clear_tv(&var2); 4026 } 4027 } 4028 return OK; 4029 } 4030 4031 /* 4032 * Handle fifth level expression: 4033 * * number multiplication 4034 * / number division 4035 * % number modulo 4036 * 4037 * "arg" must point to the first non-white of the expression. 4038 * "arg" is advanced to the next non-white after the recognized expression. 4039 * 4040 * Return OK or FAIL. 4041 */ 4042 static int 4043 eval6( 4044 char_u **arg, 4045 typval_T *rettv, 4046 int evaluate, 4047 int want_string) /* after "." operator */ 4048 { 4049 typval_T var2; 4050 int op; 4051 varnumber_T n1, n2; 4052 #ifdef FEAT_FLOAT 4053 int use_float = FALSE; 4054 float_T f1 = 0, f2 = 0; 4055 #endif 4056 int error = FALSE; 4057 4058 /* 4059 * Get the first variable. 4060 */ 4061 if (eval7(arg, rettv, evaluate, want_string) == FAIL) 4062 return FAIL; 4063 4064 /* 4065 * Repeat computing, until no '*', '/' or '%' is following. 4066 */ 4067 for (;;) 4068 { 4069 op = **arg; 4070 if (op != '*' && op != '/' && op != '%') 4071 break; 4072 4073 if (evaluate) 4074 { 4075 #ifdef FEAT_FLOAT 4076 if (rettv->v_type == VAR_FLOAT) 4077 { 4078 f1 = rettv->vval.v_float; 4079 use_float = TRUE; 4080 n1 = 0; 4081 } 4082 else 4083 #endif 4084 n1 = tv_get_number_chk(rettv, &error); 4085 clear_tv(rettv); 4086 if (error) 4087 return FAIL; 4088 } 4089 else 4090 n1 = 0; 4091 4092 /* 4093 * Get the second variable. 4094 */ 4095 *arg = skipwhite(*arg + 1); 4096 if (eval7(arg, &var2, evaluate, FALSE) == FAIL) 4097 return FAIL; 4098 4099 if (evaluate) 4100 { 4101 #ifdef FEAT_FLOAT 4102 if (var2.v_type == VAR_FLOAT) 4103 { 4104 if (!use_float) 4105 { 4106 f1 = n1; 4107 use_float = TRUE; 4108 } 4109 f2 = var2.vval.v_float; 4110 n2 = 0; 4111 } 4112 else 4113 #endif 4114 { 4115 n2 = tv_get_number_chk(&var2, &error); 4116 clear_tv(&var2); 4117 if (error) 4118 return FAIL; 4119 #ifdef FEAT_FLOAT 4120 if (use_float) 4121 f2 = n2; 4122 #endif 4123 } 4124 4125 /* 4126 * Compute the result. 4127 * When either side is a float the result is a float. 4128 */ 4129 #ifdef FEAT_FLOAT 4130 if (use_float) 4131 { 4132 if (op == '*') 4133 f1 = f1 * f2; 4134 else if (op == '/') 4135 { 4136 # ifdef VMS 4137 /* VMS crashes on divide by zero, work around it */ 4138 if (f2 == 0.0) 4139 { 4140 if (f1 == 0) 4141 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */ 4142 else if (f1 < 0) 4143 f1 = -1 * __F_FLT_MAX; 4144 else 4145 f1 = __F_FLT_MAX; 4146 } 4147 else 4148 f1 = f1 / f2; 4149 # else 4150 /* We rely on the floating point library to handle divide 4151 * by zero to result in "inf" and not a crash. */ 4152 f1 = f1 / f2; 4153 # endif 4154 } 4155 else 4156 { 4157 emsg(_("E804: Cannot use '%' with Float")); 4158 return FAIL; 4159 } 4160 rettv->v_type = VAR_FLOAT; 4161 rettv->vval.v_float = f1; 4162 } 4163 else 4164 #endif 4165 { 4166 if (op == '*') 4167 n1 = n1 * n2; 4168 else if (op == '/') 4169 n1 = num_divide(n1, n2); 4170 else 4171 n1 = num_modulus(n1, n2); 4172 4173 rettv->v_type = VAR_NUMBER; 4174 rettv->vval.v_number = n1; 4175 } 4176 } 4177 } 4178 4179 return OK; 4180 } 4181 4182 /* 4183 * Handle sixth level expression: 4184 * number number constant 4185 * 0zFFFFFFFF Blob constant 4186 * "string" string constant 4187 * 'string' literal string constant 4188 * &option-name option value 4189 * @r register contents 4190 * identifier variable value 4191 * function() function call 4192 * $VAR environment variable 4193 * (expression) nested expression 4194 * [expr, expr] List 4195 * {key: val, key: val} Dictionary 4196 * 4197 * Also handle: 4198 * ! in front logical NOT 4199 * - in front unary minus 4200 * + in front unary plus (ignored) 4201 * trailing [] subscript in String or List 4202 * trailing .name entry in Dictionary 4203 * 4204 * "arg" must point to the first non-white of the expression. 4205 * "arg" is advanced to the next non-white after the recognized expression. 4206 * 4207 * Return OK or FAIL. 4208 */ 4209 static int 4210 eval7( 4211 char_u **arg, 4212 typval_T *rettv, 4213 int evaluate, 4214 int want_string UNUSED) /* after "." operator */ 4215 { 4216 varnumber_T n; 4217 int len; 4218 char_u *s; 4219 char_u *start_leader, *end_leader; 4220 int ret = OK; 4221 char_u *alias; 4222 4223 /* 4224 * Initialise variable so that clear_tv() can't mistake this for a 4225 * string and free a string that isn't there. 4226 */ 4227 rettv->v_type = VAR_UNKNOWN; 4228 4229 /* 4230 * Skip '!', '-' and '+' characters. They are handled later. 4231 */ 4232 start_leader = *arg; 4233 while (**arg == '!' || **arg == '-' || **arg == '+') 4234 *arg = skipwhite(*arg + 1); 4235 end_leader = *arg; 4236 4237 if (**arg == '.' && (!isdigit(*(*arg + 1)) 4238 #ifdef FEAT_FLOAT 4239 || current_sctx.sc_version < 2 4240 #endif 4241 )) 4242 { 4243 semsg(_(e_invexpr2), *arg); 4244 ++*arg; 4245 return FAIL; 4246 } 4247 4248 switch (**arg) 4249 { 4250 /* 4251 * Number constant. 4252 */ 4253 case '0': 4254 case '1': 4255 case '2': 4256 case '3': 4257 case '4': 4258 case '5': 4259 case '6': 4260 case '7': 4261 case '8': 4262 case '9': 4263 case '.': 4264 { 4265 #ifdef FEAT_FLOAT 4266 char_u *p; 4267 int get_float = FALSE; 4268 4269 /* We accept a float when the format matches 4270 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very 4271 * strict to avoid backwards compatibility problems. 4272 * With script version 2 and later the leading digit can be 4273 * omitted. 4274 * Don't look for a float after the "." operator, so that 4275 * ":let vers = 1.2.3" doesn't fail. */ 4276 if (**arg == '.') 4277 p = *arg; 4278 else 4279 p = skipdigits(*arg + 1); 4280 if (!want_string && p[0] == '.' && vim_isdigit(p[1])) 4281 { 4282 get_float = TRUE; 4283 p = skipdigits(p + 2); 4284 if (*p == 'e' || *p == 'E') 4285 { 4286 ++p; 4287 if (*p == '-' || *p == '+') 4288 ++p; 4289 if (!vim_isdigit(*p)) 4290 get_float = FALSE; 4291 else 4292 p = skipdigits(p + 1); 4293 } 4294 if (ASCII_ISALPHA(*p) || *p == '.') 4295 get_float = FALSE; 4296 } 4297 if (get_float) 4298 { 4299 float_T f; 4300 4301 *arg += string2float(*arg, &f); 4302 if (evaluate) 4303 { 4304 rettv->v_type = VAR_FLOAT; 4305 rettv->vval.v_float = f; 4306 } 4307 } 4308 else 4309 #endif 4310 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z')) 4311 { 4312 char_u *bp; 4313 blob_T *blob = NULL; // init for gcc 4314 4315 // Blob constant: 0z0123456789abcdef 4316 if (evaluate) 4317 blob = blob_alloc(); 4318 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2) 4319 { 4320 if (!vim_isxdigit(bp[1])) 4321 { 4322 if (blob != NULL) 4323 { 4324 emsg(_("E973: Blob literal should have an even number of hex characters")); 4325 ga_clear(&blob->bv_ga); 4326 VIM_CLEAR(blob); 4327 } 4328 ret = FAIL; 4329 break; 4330 } 4331 if (blob != NULL) 4332 ga_append(&blob->bv_ga, 4333 (hex2nr(*bp) << 4) + hex2nr(*(bp+1))); 4334 if (bp[2] == '.' && vim_isxdigit(bp[3])) 4335 ++bp; 4336 } 4337 if (blob != NULL) 4338 rettv_blob_set(rettv, blob); 4339 *arg = bp; 4340 } 4341 else 4342 { 4343 // decimal, hex or octal number 4344 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0); 4345 *arg += len; 4346 if (evaluate) 4347 { 4348 rettv->v_type = VAR_NUMBER; 4349 rettv->vval.v_number = n; 4350 } 4351 } 4352 break; 4353 } 4354 4355 /* 4356 * String constant: "string". 4357 */ 4358 case '"': ret = get_string_tv(arg, rettv, evaluate); 4359 break; 4360 4361 /* 4362 * Literal string constant: 'str''ing'. 4363 */ 4364 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate); 4365 break; 4366 4367 /* 4368 * List: [expr, expr] 4369 */ 4370 case '[': ret = get_list_tv(arg, rettv, evaluate); 4371 break; 4372 4373 /* 4374 * Lambda: {arg, arg -> expr} 4375 * Dictionary: {key: val, key: val} 4376 */ 4377 case '{': ret = get_lambda_tv(arg, rettv, evaluate); 4378 if (ret == NOTDONE) 4379 ret = dict_get_tv(arg, rettv, evaluate); 4380 break; 4381 4382 /* 4383 * Option value: &name 4384 */ 4385 case '&': ret = get_option_tv(arg, rettv, evaluate); 4386 break; 4387 4388 /* 4389 * Environment variable: $VAR. 4390 */ 4391 case '$': ret = get_env_tv(arg, rettv, evaluate); 4392 break; 4393 4394 /* 4395 * Register contents: @r. 4396 */ 4397 case '@': ++*arg; 4398 if (evaluate) 4399 { 4400 rettv->v_type = VAR_STRING; 4401 rettv->vval.v_string = get_reg_contents(**arg, 4402 GREG_EXPR_SRC); 4403 } 4404 if (**arg != NUL) 4405 ++*arg; 4406 break; 4407 4408 /* 4409 * nested expression: (expression). 4410 */ 4411 case '(': *arg = skipwhite(*arg + 1); 4412 ret = eval1(arg, rettv, evaluate); /* recursive! */ 4413 if (**arg == ')') 4414 ++*arg; 4415 else if (ret == OK) 4416 { 4417 emsg(_("E110: Missing ')'")); 4418 clear_tv(rettv); 4419 ret = FAIL; 4420 } 4421 break; 4422 4423 default: ret = NOTDONE; 4424 break; 4425 } 4426 4427 if (ret == NOTDONE) 4428 { 4429 /* 4430 * Must be a variable or function name. 4431 * Can also be a curly-braces kind of name: {expr}. 4432 */ 4433 s = *arg; 4434 len = get_name_len(arg, &alias, evaluate, TRUE); 4435 if (alias != NULL) 4436 s = alias; 4437 4438 if (len <= 0) 4439 ret = FAIL; 4440 else 4441 { 4442 if (**arg == '(') /* recursive! */ 4443 { 4444 partial_T *partial; 4445 4446 if (!evaluate) 4447 check_vars(s, len); 4448 4449 /* If "s" is the name of a variable of type VAR_FUNC 4450 * use its contents. */ 4451 s = deref_func_name(s, &len, &partial, !evaluate); 4452 4453 /* Need to make a copy, in case evaluating the arguments makes 4454 * the name invalid. */ 4455 s = vim_strsave(s); 4456 if (s == NULL) 4457 ret = FAIL; 4458 else 4459 /* Invoke the function. */ 4460 ret = get_func_tv(s, len, rettv, arg, 4461 curwin->w_cursor.lnum, curwin->w_cursor.lnum, 4462 &len, evaluate, partial, NULL); 4463 vim_free(s); 4464 4465 /* If evaluate is FALSE rettv->v_type was not set in 4466 * get_func_tv, but it's needed in handle_subscript() to parse 4467 * what follows. So set it here. */ 4468 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(') 4469 { 4470 rettv->vval.v_string = NULL; 4471 rettv->v_type = VAR_FUNC; 4472 } 4473 4474 /* Stop the expression evaluation when immediately 4475 * aborting on error, or when an interrupt occurred or 4476 * an exception was thrown but not caught. */ 4477 if (aborting()) 4478 { 4479 if (ret == OK) 4480 clear_tv(rettv); 4481 ret = FAIL; 4482 } 4483 } 4484 else if (evaluate) 4485 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE); 4486 else 4487 { 4488 check_vars(s, len); 4489 ret = OK; 4490 } 4491 } 4492 vim_free(alias); 4493 } 4494 4495 *arg = skipwhite(*arg); 4496 4497 /* Handle following '[', '(' and '.' for expr[expr], expr.name, 4498 * expr(expr). */ 4499 if (ret == OK) 4500 ret = handle_subscript(arg, rettv, evaluate, TRUE); 4501 4502 /* 4503 * Apply logical NOT and unary '-', from right to left, ignore '+'. 4504 */ 4505 if (ret == OK && evaluate && end_leader > start_leader) 4506 { 4507 int error = FALSE; 4508 varnumber_T val = 0; 4509 #ifdef FEAT_FLOAT 4510 float_T f = 0.0; 4511 4512 if (rettv->v_type == VAR_FLOAT) 4513 f = rettv->vval.v_float; 4514 else 4515 #endif 4516 val = tv_get_number_chk(rettv, &error); 4517 if (error) 4518 { 4519 clear_tv(rettv); 4520 ret = FAIL; 4521 } 4522 else 4523 { 4524 while (end_leader > start_leader) 4525 { 4526 --end_leader; 4527 if (*end_leader == '!') 4528 { 4529 #ifdef FEAT_FLOAT 4530 if (rettv->v_type == VAR_FLOAT) 4531 f = !f; 4532 else 4533 #endif 4534 val = !val; 4535 } 4536 else if (*end_leader == '-') 4537 { 4538 #ifdef FEAT_FLOAT 4539 if (rettv->v_type == VAR_FLOAT) 4540 f = -f; 4541 else 4542 #endif 4543 val = -val; 4544 } 4545 } 4546 #ifdef FEAT_FLOAT 4547 if (rettv->v_type == VAR_FLOAT) 4548 { 4549 clear_tv(rettv); 4550 rettv->vval.v_float = f; 4551 } 4552 else 4553 #endif 4554 { 4555 clear_tv(rettv); 4556 rettv->v_type = VAR_NUMBER; 4557 rettv->vval.v_number = val; 4558 } 4559 } 4560 } 4561 4562 return ret; 4563 } 4564 4565 /* 4566 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key". 4567 * "*arg" points to the '[' or '.'. 4568 * Returns FAIL or OK. "*arg" is advanced to after the ']'. 4569 */ 4570 static int 4571 eval_index( 4572 char_u **arg, 4573 typval_T *rettv, 4574 int evaluate, 4575 int verbose) /* give error messages */ 4576 { 4577 int empty1 = FALSE, empty2 = FALSE; 4578 typval_T var1, var2; 4579 long i; 4580 long n1, n2 = 0; 4581 long len = -1; 4582 int range = FALSE; 4583 char_u *s; 4584 char_u *key = NULL; 4585 4586 switch (rettv->v_type) 4587 { 4588 case VAR_FUNC: 4589 case VAR_PARTIAL: 4590 if (verbose) 4591 emsg(_("E695: Cannot index a Funcref")); 4592 return FAIL; 4593 case VAR_FLOAT: 4594 #ifdef FEAT_FLOAT 4595 if (verbose) 4596 emsg(_(e_float_as_string)); 4597 return FAIL; 4598 #endif 4599 case VAR_SPECIAL: 4600 case VAR_JOB: 4601 case VAR_CHANNEL: 4602 if (verbose) 4603 emsg(_("E909: Cannot index a special variable")); 4604 return FAIL; 4605 case VAR_UNKNOWN: 4606 if (evaluate) 4607 return FAIL; 4608 /* FALLTHROUGH */ 4609 4610 case VAR_STRING: 4611 case VAR_NUMBER: 4612 case VAR_LIST: 4613 case VAR_DICT: 4614 case VAR_BLOB: 4615 break; 4616 } 4617 4618 init_tv(&var1); 4619 init_tv(&var2); 4620 if (**arg == '.') 4621 { 4622 /* 4623 * dict.name 4624 */ 4625 key = *arg + 1; 4626 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len) 4627 ; 4628 if (len == 0) 4629 return FAIL; 4630 *arg = skipwhite(key + len); 4631 } 4632 else 4633 { 4634 /* 4635 * something[idx] 4636 * 4637 * Get the (first) variable from inside the []. 4638 */ 4639 *arg = skipwhite(*arg + 1); 4640 if (**arg == ':') 4641 empty1 = TRUE; 4642 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */ 4643 return FAIL; 4644 else if (evaluate && tv_get_string_chk(&var1) == NULL) 4645 { 4646 /* not a number or string */ 4647 clear_tv(&var1); 4648 return FAIL; 4649 } 4650 4651 /* 4652 * Get the second variable from inside the [:]. 4653 */ 4654 if (**arg == ':') 4655 { 4656 range = TRUE; 4657 *arg = skipwhite(*arg + 1); 4658 if (**arg == ']') 4659 empty2 = TRUE; 4660 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */ 4661 { 4662 if (!empty1) 4663 clear_tv(&var1); 4664 return FAIL; 4665 } 4666 else if (evaluate && tv_get_string_chk(&var2) == NULL) 4667 { 4668 /* not a number or string */ 4669 if (!empty1) 4670 clear_tv(&var1); 4671 clear_tv(&var2); 4672 return FAIL; 4673 } 4674 } 4675 4676 /* Check for the ']'. */ 4677 if (**arg != ']') 4678 { 4679 if (verbose) 4680 emsg(_(e_missbrac)); 4681 clear_tv(&var1); 4682 if (range) 4683 clear_tv(&var2); 4684 return FAIL; 4685 } 4686 *arg = skipwhite(*arg + 1); /* skip the ']' */ 4687 } 4688 4689 if (evaluate) 4690 { 4691 n1 = 0; 4692 if (!empty1 && rettv->v_type != VAR_DICT) 4693 { 4694 n1 = tv_get_number(&var1); 4695 clear_tv(&var1); 4696 } 4697 if (range) 4698 { 4699 if (empty2) 4700 n2 = -1; 4701 else 4702 { 4703 n2 = tv_get_number(&var2); 4704 clear_tv(&var2); 4705 } 4706 } 4707 4708 switch (rettv->v_type) 4709 { 4710 case VAR_UNKNOWN: 4711 case VAR_FUNC: 4712 case VAR_PARTIAL: 4713 case VAR_FLOAT: 4714 case VAR_SPECIAL: 4715 case VAR_JOB: 4716 case VAR_CHANNEL: 4717 break; /* not evaluating, skipping over subscript */ 4718 4719 case VAR_NUMBER: 4720 case VAR_STRING: 4721 s = tv_get_string(rettv); 4722 len = (long)STRLEN(s); 4723 if (range) 4724 { 4725 /* The resulting variable is a substring. If the indexes 4726 * are out of range the result is empty. */ 4727 if (n1 < 0) 4728 { 4729 n1 = len + n1; 4730 if (n1 < 0) 4731 n1 = 0; 4732 } 4733 if (n2 < 0) 4734 n2 = len + n2; 4735 else if (n2 >= len) 4736 n2 = len; 4737 if (n1 >= len || n2 < 0 || n1 > n2) 4738 s = NULL; 4739 else 4740 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1)); 4741 } 4742 else 4743 { 4744 /* The resulting variable is a string of a single 4745 * character. If the index is too big or negative the 4746 * result is empty. */ 4747 if (n1 >= len || n1 < 0) 4748 s = NULL; 4749 else 4750 s = vim_strnsave(s + n1, 1); 4751 } 4752 clear_tv(rettv); 4753 rettv->v_type = VAR_STRING; 4754 rettv->vval.v_string = s; 4755 break; 4756 4757 case VAR_BLOB: 4758 len = blob_len(rettv->vval.v_blob); 4759 if (range) 4760 { 4761 // The resulting variable is a sub-blob. If the indexes 4762 // are out of range the result is empty. 4763 if (n1 < 0) 4764 { 4765 n1 = len + n1; 4766 if (n1 < 0) 4767 n1 = 0; 4768 } 4769 if (n2 < 0) 4770 n2 = len + n2; 4771 else if (n2 >= len) 4772 n2 = len - 1; 4773 if (n1 >= len || n2 < 0 || n1 > n2) 4774 { 4775 clear_tv(rettv); 4776 rettv->v_type = VAR_BLOB; 4777 rettv->vval.v_blob = NULL; 4778 } 4779 else 4780 { 4781 blob_T *blob = blob_alloc(); 4782 4783 if (blob != NULL) 4784 { 4785 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL) 4786 { 4787 blob_free(blob); 4788 return FAIL; 4789 } 4790 blob->bv_ga.ga_len = n2 - n1 + 1; 4791 for (i = n1; i <= n2; i++) 4792 blob_set(blob, i - n1, 4793 blob_get(rettv->vval.v_blob, i)); 4794 4795 clear_tv(rettv); 4796 rettv_blob_set(rettv, blob); 4797 } 4798 } 4799 } 4800 else 4801 { 4802 // The resulting variable is a byte value. 4803 // If the index is too big or negative that is an error. 4804 if (n1 < 0) 4805 n1 = len + n1; 4806 if (n1 < len && n1 >= 0) 4807 { 4808 int v = blob_get(rettv->vval.v_blob, n1); 4809 4810 clear_tv(rettv); 4811 rettv->v_type = VAR_NUMBER; 4812 rettv->vval.v_number = v; 4813 } 4814 else 4815 semsg(_(e_blobidx), n1); 4816 } 4817 break; 4818 4819 case VAR_LIST: 4820 len = list_len(rettv->vval.v_list); 4821 if (n1 < 0) 4822 n1 = len + n1; 4823 if (!empty1 && (n1 < 0 || n1 >= len)) 4824 { 4825 /* For a range we allow invalid values and return an empty 4826 * list. A list index out of range is an error. */ 4827 if (!range) 4828 { 4829 if (verbose) 4830 semsg(_(e_listidx), n1); 4831 return FAIL; 4832 } 4833 n1 = len; 4834 } 4835 if (range) 4836 { 4837 list_T *l; 4838 listitem_T *item; 4839 4840 if (n2 < 0) 4841 n2 = len + n2; 4842 else if (n2 >= len) 4843 n2 = len - 1; 4844 if (!empty2 && (n2 < 0 || n2 + 1 < n1)) 4845 n2 = -1; 4846 l = list_alloc(); 4847 if (l == NULL) 4848 return FAIL; 4849 for (item = list_find(rettv->vval.v_list, n1); 4850 n1 <= n2; ++n1) 4851 { 4852 if (list_append_tv(l, &item->li_tv) == FAIL) 4853 { 4854 list_free(l); 4855 return FAIL; 4856 } 4857 item = item->li_next; 4858 } 4859 clear_tv(rettv); 4860 rettv_list_set(rettv, l); 4861 } 4862 else 4863 { 4864 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1); 4865 clear_tv(rettv); 4866 *rettv = var1; 4867 } 4868 break; 4869 4870 case VAR_DICT: 4871 if (range) 4872 { 4873 if (verbose) 4874 emsg(_(e_dictrange)); 4875 if (len == -1) 4876 clear_tv(&var1); 4877 return FAIL; 4878 } 4879 { 4880 dictitem_T *item; 4881 4882 if (len == -1) 4883 { 4884 key = tv_get_string_chk(&var1); 4885 if (key == NULL) 4886 { 4887 clear_tv(&var1); 4888 return FAIL; 4889 } 4890 } 4891 4892 item = dict_find(rettv->vval.v_dict, key, (int)len); 4893 4894 if (item == NULL && verbose) 4895 semsg(_(e_dictkey), key); 4896 if (len == -1) 4897 clear_tv(&var1); 4898 if (item == NULL) 4899 return FAIL; 4900 4901 copy_tv(&item->di_tv, &var1); 4902 clear_tv(rettv); 4903 *rettv = var1; 4904 } 4905 break; 4906 } 4907 } 4908 4909 return OK; 4910 } 4911 4912 /* 4913 * Get an option value. 4914 * "arg" points to the '&' or '+' before the option name. 4915 * "arg" is advanced to character after the option name. 4916 * Return OK or FAIL. 4917 */ 4918 int 4919 get_option_tv( 4920 char_u **arg, 4921 typval_T *rettv, /* when NULL, only check if option exists */ 4922 int evaluate) 4923 { 4924 char_u *option_end; 4925 long numval; 4926 char_u *stringval; 4927 int opt_type; 4928 int c; 4929 int working = (**arg == '+'); /* has("+option") */ 4930 int ret = OK; 4931 int opt_flags; 4932 4933 /* 4934 * Isolate the option name and find its value. 4935 */ 4936 option_end = find_option_end(arg, &opt_flags); 4937 if (option_end == NULL) 4938 { 4939 if (rettv != NULL) 4940 semsg(_("E112: Option name missing: %s"), *arg); 4941 return FAIL; 4942 } 4943 4944 if (!evaluate) 4945 { 4946 *arg = option_end; 4947 return OK; 4948 } 4949 4950 c = *option_end; 4951 *option_end = NUL; 4952 opt_type = get_option_value(*arg, &numval, 4953 rettv == NULL ? NULL : &stringval, opt_flags); 4954 4955 if (opt_type == -3) /* invalid name */ 4956 { 4957 if (rettv != NULL) 4958 semsg(_("E113: Unknown option: %s"), *arg); 4959 ret = FAIL; 4960 } 4961 else if (rettv != NULL) 4962 { 4963 if (opt_type == -2) /* hidden string option */ 4964 { 4965 rettv->v_type = VAR_STRING; 4966 rettv->vval.v_string = NULL; 4967 } 4968 else if (opt_type == -1) /* hidden number option */ 4969 { 4970 rettv->v_type = VAR_NUMBER; 4971 rettv->vval.v_number = 0; 4972 } 4973 else if (opt_type == 1) /* number option */ 4974 { 4975 rettv->v_type = VAR_NUMBER; 4976 rettv->vval.v_number = numval; 4977 } 4978 else /* string option */ 4979 { 4980 rettv->v_type = VAR_STRING; 4981 rettv->vval.v_string = stringval; 4982 } 4983 } 4984 else if (working && (opt_type == -2 || opt_type == -1)) 4985 ret = FAIL; 4986 4987 *option_end = c; /* put back for error messages */ 4988 *arg = option_end; 4989 4990 return ret; 4991 } 4992 4993 /* 4994 * Allocate a variable for a string constant. 4995 * Return OK or FAIL. 4996 */ 4997 static int 4998 get_string_tv(char_u **arg, typval_T *rettv, int evaluate) 4999 { 5000 char_u *p; 5001 char_u *name; 5002 int extra = 0; 5003 5004 /* 5005 * Find the end of the string, skipping backslashed characters. 5006 */ 5007 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p)) 5008 { 5009 if (*p == '\\' && p[1] != NUL) 5010 { 5011 ++p; 5012 /* A "\<x>" form occupies at least 4 characters, and produces up 5013 * to 6 characters: reserve space for 2 extra */ 5014 if (*p == '<') 5015 extra += 2; 5016 } 5017 } 5018 5019 if (*p != '"') 5020 { 5021 semsg(_("E114: Missing quote: %s"), *arg); 5022 return FAIL; 5023 } 5024 5025 /* If only parsing, set *arg and return here */ 5026 if (!evaluate) 5027 { 5028 *arg = p + 1; 5029 return OK; 5030 } 5031 5032 /* 5033 * Copy the string into allocated memory, handling backslashed 5034 * characters. 5035 */ 5036 name = alloc((unsigned)(p - *arg + extra)); 5037 if (name == NULL) 5038 return FAIL; 5039 rettv->v_type = VAR_STRING; 5040 rettv->vval.v_string = name; 5041 5042 for (p = *arg + 1; *p != NUL && *p != '"'; ) 5043 { 5044 if (*p == '\\') 5045 { 5046 switch (*++p) 5047 { 5048 case 'b': *name++ = BS; ++p; break; 5049 case 'e': *name++ = ESC; ++p; break; 5050 case 'f': *name++ = FF; ++p; break; 5051 case 'n': *name++ = NL; ++p; break; 5052 case 'r': *name++ = CAR; ++p; break; 5053 case 't': *name++ = TAB; ++p; break; 5054 5055 case 'X': /* hex: "\x1", "\x12" */ 5056 case 'x': 5057 case 'u': /* Unicode: "\u0023" */ 5058 case 'U': 5059 if (vim_isxdigit(p[1])) 5060 { 5061 int n, nr; 5062 int c = toupper(*p); 5063 5064 if (c == 'X') 5065 n = 2; 5066 else if (*p == 'u') 5067 n = 4; 5068 else 5069 n = 8; 5070 nr = 0; 5071 while (--n >= 0 && vim_isxdigit(p[1])) 5072 { 5073 ++p; 5074 nr = (nr << 4) + hex2nr(*p); 5075 } 5076 ++p; 5077 /* For "\u" store the number according to 5078 * 'encoding'. */ 5079 if (c != 'X') 5080 name += (*mb_char2bytes)(nr, name); 5081 else 5082 *name++ = nr; 5083 } 5084 break; 5085 5086 /* octal: "\1", "\12", "\123" */ 5087 case '0': 5088 case '1': 5089 case '2': 5090 case '3': 5091 case '4': 5092 case '5': 5093 case '6': 5094 case '7': *name = *p++ - '0'; 5095 if (*p >= '0' && *p <= '7') 5096 { 5097 *name = (*name << 3) + *p++ - '0'; 5098 if (*p >= '0' && *p <= '7') 5099 *name = (*name << 3) + *p++ - '0'; 5100 } 5101 ++name; 5102 break; 5103 5104 /* Special key, e.g.: "\<C-W>" */ 5105 case '<': extra = trans_special(&p, name, TRUE, TRUE); 5106 if (extra != 0) 5107 { 5108 name += extra; 5109 break; 5110 } 5111 /* FALLTHROUGH */ 5112 5113 default: MB_COPY_CHAR(p, name); 5114 break; 5115 } 5116 } 5117 else 5118 MB_COPY_CHAR(p, name); 5119 5120 } 5121 *name = NUL; 5122 if (*p != NUL) /* just in case */ 5123 ++p; 5124 *arg = p; 5125 5126 return OK; 5127 } 5128 5129 /* 5130 * Allocate a variable for a 'str''ing' constant. 5131 * Return OK or FAIL. 5132 */ 5133 static int 5134 get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate) 5135 { 5136 char_u *p; 5137 char_u *str; 5138 int reduce = 0; 5139 5140 /* 5141 * Find the end of the string, skipping ''. 5142 */ 5143 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p)) 5144 { 5145 if (*p == '\'') 5146 { 5147 if (p[1] != '\'') 5148 break; 5149 ++reduce; 5150 ++p; 5151 } 5152 } 5153 5154 if (*p != '\'') 5155 { 5156 semsg(_("E115: Missing quote: %s"), *arg); 5157 return FAIL; 5158 } 5159 5160 /* If only parsing return after setting "*arg" */ 5161 if (!evaluate) 5162 { 5163 *arg = p + 1; 5164 return OK; 5165 } 5166 5167 /* 5168 * Copy the string into allocated memory, handling '' to ' reduction. 5169 */ 5170 str = alloc((unsigned)((p - *arg) - reduce)); 5171 if (str == NULL) 5172 return FAIL; 5173 rettv->v_type = VAR_STRING; 5174 rettv->vval.v_string = str; 5175 5176 for (p = *arg + 1; *p != NUL; ) 5177 { 5178 if (*p == '\'') 5179 { 5180 if (p[1] != '\'') 5181 break; 5182 ++p; 5183 } 5184 MB_COPY_CHAR(p, str); 5185 } 5186 *str = NUL; 5187 *arg = p + 1; 5188 5189 return OK; 5190 } 5191 5192 /* 5193 * Return the function name of the partial. 5194 */ 5195 char_u * 5196 partial_name(partial_T *pt) 5197 { 5198 if (pt->pt_name != NULL) 5199 return pt->pt_name; 5200 return pt->pt_func->uf_name; 5201 } 5202 5203 static void 5204 partial_free(partial_T *pt) 5205 { 5206 int i; 5207 5208 for (i = 0; i < pt->pt_argc; ++i) 5209 clear_tv(&pt->pt_argv[i]); 5210 vim_free(pt->pt_argv); 5211 dict_unref(pt->pt_dict); 5212 if (pt->pt_name != NULL) 5213 { 5214 func_unref(pt->pt_name); 5215 vim_free(pt->pt_name); 5216 } 5217 else 5218 func_ptr_unref(pt->pt_func); 5219 vim_free(pt); 5220 } 5221 5222 /* 5223 * Unreference a closure: decrement the reference count and free it when it 5224 * becomes zero. 5225 */ 5226 void 5227 partial_unref(partial_T *pt) 5228 { 5229 if (pt != NULL && --pt->pt_refcount <= 0) 5230 partial_free(pt); 5231 } 5232 5233 static int tv_equal_recurse_limit; 5234 5235 static int 5236 func_equal( 5237 typval_T *tv1, 5238 typval_T *tv2, 5239 int ic) /* ignore case */ 5240 { 5241 char_u *s1, *s2; 5242 dict_T *d1, *d2; 5243 int a1, a2; 5244 int i; 5245 5246 /* empty and NULL function name considered the same */ 5247 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string 5248 : partial_name(tv1->vval.v_partial); 5249 if (s1 != NULL && *s1 == NUL) 5250 s1 = NULL; 5251 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string 5252 : partial_name(tv2->vval.v_partial); 5253 if (s2 != NULL && *s2 == NUL) 5254 s2 = NULL; 5255 if (s1 == NULL || s2 == NULL) 5256 { 5257 if (s1 != s2) 5258 return FALSE; 5259 } 5260 else if (STRCMP(s1, s2) != 0) 5261 return FALSE; 5262 5263 /* empty dict and NULL dict is different */ 5264 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict; 5265 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict; 5266 if (d1 == NULL || d2 == NULL) 5267 { 5268 if (d1 != d2) 5269 return FALSE; 5270 } 5271 else if (!dict_equal(d1, d2, ic, TRUE)) 5272 return FALSE; 5273 5274 /* empty list and no list considered the same */ 5275 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc; 5276 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc; 5277 if (a1 != a2) 5278 return FALSE; 5279 for (i = 0; i < a1; ++i) 5280 if (!tv_equal(tv1->vval.v_partial->pt_argv + i, 5281 tv2->vval.v_partial->pt_argv + i, ic, TRUE)) 5282 return FALSE; 5283 5284 return TRUE; 5285 } 5286 5287 /* 5288 * Return TRUE if "tv1" and "tv2" have the same value. 5289 * Compares the items just like "==" would compare them, but strings and 5290 * numbers are different. Floats and numbers are also different. 5291 */ 5292 int 5293 tv_equal( 5294 typval_T *tv1, 5295 typval_T *tv2, 5296 int ic, /* ignore case */ 5297 int recursive) /* TRUE when used recursively */ 5298 { 5299 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN]; 5300 char_u *s1, *s2; 5301 static int recursive_cnt = 0; /* catch recursive loops */ 5302 int r; 5303 5304 /* Catch lists and dicts that have an endless loop by limiting 5305 * recursiveness to a limit. We guess they are equal then. 5306 * A fixed limit has the problem of still taking an awful long time. 5307 * Reduce the limit every time running into it. That should work fine for 5308 * deeply linked structures that are not recursively linked and catch 5309 * recursiveness quickly. */ 5310 if (!recursive) 5311 tv_equal_recurse_limit = 1000; 5312 if (recursive_cnt >= tv_equal_recurse_limit) 5313 { 5314 --tv_equal_recurse_limit; 5315 return TRUE; 5316 } 5317 5318 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and 5319 * arguments. */ 5320 if ((tv1->v_type == VAR_FUNC 5321 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL)) 5322 && (tv2->v_type == VAR_FUNC 5323 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL))) 5324 { 5325 ++recursive_cnt; 5326 r = func_equal(tv1, tv2, ic); 5327 --recursive_cnt; 5328 return r; 5329 } 5330 5331 if (tv1->v_type != tv2->v_type) 5332 return FALSE; 5333 5334 switch (tv1->v_type) 5335 { 5336 case VAR_LIST: 5337 ++recursive_cnt; 5338 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE); 5339 --recursive_cnt; 5340 return r; 5341 5342 case VAR_DICT: 5343 ++recursive_cnt; 5344 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE); 5345 --recursive_cnt; 5346 return r; 5347 5348 case VAR_BLOB: 5349 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob); 5350 5351 case VAR_NUMBER: 5352 return tv1->vval.v_number == tv2->vval.v_number; 5353 5354 case VAR_STRING: 5355 s1 = tv_get_string_buf(tv1, buf1); 5356 s2 = tv_get_string_buf(tv2, buf2); 5357 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0); 5358 5359 case VAR_SPECIAL: 5360 return tv1->vval.v_number == tv2->vval.v_number; 5361 5362 case VAR_FLOAT: 5363 #ifdef FEAT_FLOAT 5364 return tv1->vval.v_float == tv2->vval.v_float; 5365 #endif 5366 case VAR_JOB: 5367 #ifdef FEAT_JOB_CHANNEL 5368 return tv1->vval.v_job == tv2->vval.v_job; 5369 #endif 5370 case VAR_CHANNEL: 5371 #ifdef FEAT_JOB_CHANNEL 5372 return tv1->vval.v_channel == tv2->vval.v_channel; 5373 #endif 5374 case VAR_FUNC: 5375 case VAR_PARTIAL: 5376 case VAR_UNKNOWN: 5377 break; 5378 } 5379 5380 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it 5381 * does not equal anything, not even itself. */ 5382 return FALSE; 5383 } 5384 5385 /* 5386 * Return the next (unique) copy ID. 5387 * Used for serializing nested structures. 5388 */ 5389 int 5390 get_copyID(void) 5391 { 5392 current_copyID += COPYID_INC; 5393 return current_copyID; 5394 } 5395 5396 /* 5397 * Garbage collection for lists and dictionaries. 5398 * 5399 * We use reference counts to be able to free most items right away when they 5400 * are no longer used. But for composite items it's possible that it becomes 5401 * unused while the reference count is > 0: When there is a recursive 5402 * reference. Example: 5403 * :let l = [1, 2, 3] 5404 * :let d = {9: l} 5405 * :let l[1] = d 5406 * 5407 * Since this is quite unusual we handle this with garbage collection: every 5408 * once in a while find out which lists and dicts are not referenced from any 5409 * variable. 5410 * 5411 * Here is a good reference text about garbage collection (refers to Python 5412 * but it applies to all reference-counting mechanisms): 5413 * http://python.ca/nas/python/gc/ 5414 */ 5415 5416 /* 5417 * Do garbage collection for lists and dicts. 5418 * When "testing" is TRUE this is called from test_garbagecollect_now(). 5419 * Return TRUE if some memory was freed. 5420 */ 5421 int 5422 garbage_collect(int testing) 5423 { 5424 int copyID; 5425 int abort = FALSE; 5426 buf_T *buf; 5427 win_T *wp; 5428 int i; 5429 int did_free = FALSE; 5430 tabpage_T *tp; 5431 5432 if (!testing) 5433 { 5434 /* Only do this once. */ 5435 want_garbage_collect = FALSE; 5436 may_garbage_collect = FALSE; 5437 garbage_collect_at_exit = FALSE; 5438 } 5439 5440 /* We advance by two because we add one for items referenced through 5441 * previous_funccal. */ 5442 copyID = get_copyID(); 5443 5444 /* 5445 * 1. Go through all accessible variables and mark all lists and dicts 5446 * with copyID. 5447 */ 5448 5449 /* Don't free variables in the previous_funccal list unless they are only 5450 * referenced through previous_funccal. This must be first, because if 5451 * the item is referenced elsewhere the funccal must not be freed. */ 5452 abort = abort || set_ref_in_previous_funccal(copyID); 5453 5454 /* script-local variables */ 5455 for (i = 1; i <= ga_scripts.ga_len; ++i) 5456 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL); 5457 5458 /* buffer-local variables */ 5459 FOR_ALL_BUFFERS(buf) 5460 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID, 5461 NULL, NULL); 5462 5463 /* window-local variables */ 5464 FOR_ALL_TAB_WINDOWS(tp, wp) 5465 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID, 5466 NULL, NULL); 5467 if (aucmd_win != NULL) 5468 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID, 5469 NULL, NULL); 5470 5471 /* tabpage-local variables */ 5472 FOR_ALL_TABPAGES(tp) 5473 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID, 5474 NULL, NULL); 5475 /* global variables */ 5476 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL); 5477 5478 /* function-local variables */ 5479 abort = abort || set_ref_in_call_stack(copyID); 5480 5481 /* named functions (matters for closures) */ 5482 abort = abort || set_ref_in_functions(copyID); 5483 5484 /* function call arguments, if v:testing is set. */ 5485 abort = abort || set_ref_in_func_args(copyID); 5486 5487 /* v: vars */ 5488 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL); 5489 5490 #ifdef FEAT_LUA 5491 abort = abort || set_ref_in_lua(copyID); 5492 #endif 5493 5494 #ifdef FEAT_PYTHON 5495 abort = abort || set_ref_in_python(copyID); 5496 #endif 5497 5498 #ifdef FEAT_PYTHON3 5499 abort = abort || set_ref_in_python3(copyID); 5500 #endif 5501 5502 #ifdef FEAT_JOB_CHANNEL 5503 abort = abort || set_ref_in_channel(copyID); 5504 abort = abort || set_ref_in_job(copyID); 5505 #endif 5506 #ifdef FEAT_NETBEANS_INTG 5507 abort = abort || set_ref_in_nb_channel(copyID); 5508 #endif 5509 5510 #ifdef FEAT_TIMERS 5511 abort = abort || set_ref_in_timer(copyID); 5512 #endif 5513 5514 #ifdef FEAT_QUICKFIX 5515 abort = abort || set_ref_in_quickfix(copyID); 5516 #endif 5517 5518 #ifdef FEAT_TERMINAL 5519 abort = abort || set_ref_in_term(copyID); 5520 #endif 5521 5522 if (!abort) 5523 { 5524 /* 5525 * 2. Free lists and dictionaries that are not referenced. 5526 */ 5527 did_free = free_unref_items(copyID); 5528 5529 /* 5530 * 3. Check if any funccal can be freed now. 5531 * This may call us back recursively. 5532 */ 5533 free_unref_funccal(copyID, testing); 5534 } 5535 else if (p_verbose > 0) 5536 { 5537 verb_msg(_("Not enough memory to set references, garbage collection aborted!")); 5538 } 5539 5540 return did_free; 5541 } 5542 5543 /* 5544 * Free lists, dictionaries, channels and jobs that are no longer referenced. 5545 */ 5546 static int 5547 free_unref_items(int copyID) 5548 { 5549 int did_free = FALSE; 5550 5551 /* Let all "free" functions know that we are here. This means no 5552 * dictionaries, lists, channels or jobs are to be freed, because we will 5553 * do that here. */ 5554 in_free_unref_items = TRUE; 5555 5556 /* 5557 * PASS 1: free the contents of the items. We don't free the items 5558 * themselves yet, so that it is possible to decrement refcount counters 5559 */ 5560 5561 /* Go through the list of dicts and free items without the copyID. */ 5562 did_free |= dict_free_nonref(copyID); 5563 5564 /* Go through the list of lists and free items without the copyID. */ 5565 did_free |= list_free_nonref(copyID); 5566 5567 #ifdef FEAT_JOB_CHANNEL 5568 /* Go through the list of jobs and free items without the copyID. This 5569 * must happen before doing channels, because jobs refer to channels, but 5570 * the reference from the channel to the job isn't tracked. */ 5571 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK); 5572 5573 /* Go through the list of channels and free items without the copyID. */ 5574 did_free |= free_unused_channels_contents(copyID, COPYID_MASK); 5575 #endif 5576 5577 /* 5578 * PASS 2: free the items themselves. 5579 */ 5580 dict_free_items(copyID); 5581 list_free_items(copyID); 5582 5583 #ifdef FEAT_JOB_CHANNEL 5584 /* Go through the list of jobs and free items without the copyID. This 5585 * must happen before doing channels, because jobs refer to channels, but 5586 * the reference from the channel to the job isn't tracked. */ 5587 free_unused_jobs(copyID, COPYID_MASK); 5588 5589 /* Go through the list of channels and free items without the copyID. */ 5590 free_unused_channels(copyID, COPYID_MASK); 5591 #endif 5592 5593 in_free_unref_items = FALSE; 5594 5595 return did_free; 5596 } 5597 5598 /* 5599 * Mark all lists and dicts referenced through hashtab "ht" with "copyID". 5600 * "list_stack" is used to add lists to be marked. Can be NULL. 5601 * 5602 * Returns TRUE if setting references failed somehow. 5603 */ 5604 int 5605 set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack) 5606 { 5607 int todo; 5608 int abort = FALSE; 5609 hashitem_T *hi; 5610 hashtab_T *cur_ht; 5611 ht_stack_T *ht_stack = NULL; 5612 ht_stack_T *tempitem; 5613 5614 cur_ht = ht; 5615 for (;;) 5616 { 5617 if (!abort) 5618 { 5619 /* Mark each item in the hashtab. If the item contains a hashtab 5620 * it is added to ht_stack, if it contains a list it is added to 5621 * list_stack. */ 5622 todo = (int)cur_ht->ht_used; 5623 for (hi = cur_ht->ht_array; todo > 0; ++hi) 5624 if (!HASHITEM_EMPTY(hi)) 5625 { 5626 --todo; 5627 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID, 5628 &ht_stack, list_stack); 5629 } 5630 } 5631 5632 if (ht_stack == NULL) 5633 break; 5634 5635 /* take an item from the stack */ 5636 cur_ht = ht_stack->ht; 5637 tempitem = ht_stack; 5638 ht_stack = ht_stack->prev; 5639 free(tempitem); 5640 } 5641 5642 return abort; 5643 } 5644 5645 /* 5646 * Mark all lists and dicts referenced through list "l" with "copyID". 5647 * "ht_stack" is used to add hashtabs to be marked. Can be NULL. 5648 * 5649 * Returns TRUE if setting references failed somehow. 5650 */ 5651 int 5652 set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack) 5653 { 5654 listitem_T *li; 5655 int abort = FALSE; 5656 list_T *cur_l; 5657 list_stack_T *list_stack = NULL; 5658 list_stack_T *tempitem; 5659 5660 cur_l = l; 5661 for (;;) 5662 { 5663 if (!abort) 5664 /* Mark each item in the list. If the item contains a hashtab 5665 * it is added to ht_stack, if it contains a list it is added to 5666 * list_stack. */ 5667 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next) 5668 abort = abort || set_ref_in_item(&li->li_tv, copyID, 5669 ht_stack, &list_stack); 5670 if (list_stack == NULL) 5671 break; 5672 5673 /* take an item from the stack */ 5674 cur_l = list_stack->list; 5675 tempitem = list_stack; 5676 list_stack = list_stack->prev; 5677 free(tempitem); 5678 } 5679 5680 return abort; 5681 } 5682 5683 /* 5684 * Mark all lists and dicts referenced through typval "tv" with "copyID". 5685 * "list_stack" is used to add lists to be marked. Can be NULL. 5686 * "ht_stack" is used to add hashtabs to be marked. Can be NULL. 5687 * 5688 * Returns TRUE if setting references failed somehow. 5689 */ 5690 int 5691 set_ref_in_item( 5692 typval_T *tv, 5693 int copyID, 5694 ht_stack_T **ht_stack, 5695 list_stack_T **list_stack) 5696 { 5697 int abort = FALSE; 5698 5699 if (tv->v_type == VAR_DICT) 5700 { 5701 dict_T *dd = tv->vval.v_dict; 5702 5703 if (dd != NULL && dd->dv_copyID != copyID) 5704 { 5705 /* Didn't see this dict yet. */ 5706 dd->dv_copyID = copyID; 5707 if (ht_stack == NULL) 5708 { 5709 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack); 5710 } 5711 else 5712 { 5713 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T)); 5714 if (newitem == NULL) 5715 abort = TRUE; 5716 else 5717 { 5718 newitem->ht = &dd->dv_hashtab; 5719 newitem->prev = *ht_stack; 5720 *ht_stack = newitem; 5721 } 5722 } 5723 } 5724 } 5725 else if (tv->v_type == VAR_LIST) 5726 { 5727 list_T *ll = tv->vval.v_list; 5728 5729 if (ll != NULL && ll->lv_copyID != copyID) 5730 { 5731 /* Didn't see this list yet. */ 5732 ll->lv_copyID = copyID; 5733 if (list_stack == NULL) 5734 { 5735 abort = set_ref_in_list(ll, copyID, ht_stack); 5736 } 5737 else 5738 { 5739 list_stack_T *newitem = (list_stack_T*)malloc( 5740 sizeof(list_stack_T)); 5741 if (newitem == NULL) 5742 abort = TRUE; 5743 else 5744 { 5745 newitem->list = ll; 5746 newitem->prev = *list_stack; 5747 *list_stack = newitem; 5748 } 5749 } 5750 } 5751 } 5752 else if (tv->v_type == VAR_FUNC) 5753 { 5754 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID); 5755 } 5756 else if (tv->v_type == VAR_PARTIAL) 5757 { 5758 partial_T *pt = tv->vval.v_partial; 5759 int i; 5760 5761 /* A partial does not have a copyID, because it cannot contain itself. 5762 */ 5763 if (pt != NULL) 5764 { 5765 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID); 5766 5767 if (pt->pt_dict != NULL) 5768 { 5769 typval_T dtv; 5770 5771 dtv.v_type = VAR_DICT; 5772 dtv.vval.v_dict = pt->pt_dict; 5773 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 5774 } 5775 5776 for (i = 0; i < pt->pt_argc; ++i) 5777 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID, 5778 ht_stack, list_stack); 5779 } 5780 } 5781 #ifdef FEAT_JOB_CHANNEL 5782 else if (tv->v_type == VAR_JOB) 5783 { 5784 job_T *job = tv->vval.v_job; 5785 typval_T dtv; 5786 5787 if (job != NULL && job->jv_copyID != copyID) 5788 { 5789 job->jv_copyID = copyID; 5790 if (job->jv_channel != NULL) 5791 { 5792 dtv.v_type = VAR_CHANNEL; 5793 dtv.vval.v_channel = job->jv_channel; 5794 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 5795 } 5796 if (job->jv_exit_partial != NULL) 5797 { 5798 dtv.v_type = VAR_PARTIAL; 5799 dtv.vval.v_partial = job->jv_exit_partial; 5800 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 5801 } 5802 } 5803 } 5804 else if (tv->v_type == VAR_CHANNEL) 5805 { 5806 channel_T *ch =tv->vval.v_channel; 5807 ch_part_T part; 5808 typval_T dtv; 5809 jsonq_T *jq; 5810 cbq_T *cq; 5811 5812 if (ch != NULL && ch->ch_copyID != copyID) 5813 { 5814 ch->ch_copyID = copyID; 5815 for (part = PART_SOCK; part < PART_COUNT; ++part) 5816 { 5817 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL; 5818 jq = jq->jq_next) 5819 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack); 5820 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL; 5821 cq = cq->cq_next) 5822 if (cq->cq_partial != NULL) 5823 { 5824 dtv.v_type = VAR_PARTIAL; 5825 dtv.vval.v_partial = cq->cq_partial; 5826 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 5827 } 5828 if (ch->ch_part[part].ch_partial != NULL) 5829 { 5830 dtv.v_type = VAR_PARTIAL; 5831 dtv.vval.v_partial = ch->ch_part[part].ch_partial; 5832 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 5833 } 5834 } 5835 if (ch->ch_partial != NULL) 5836 { 5837 dtv.v_type = VAR_PARTIAL; 5838 dtv.vval.v_partial = ch->ch_partial; 5839 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 5840 } 5841 if (ch->ch_close_partial != NULL) 5842 { 5843 dtv.v_type = VAR_PARTIAL; 5844 dtv.vval.v_partial = ch->ch_close_partial; 5845 set_ref_in_item(&dtv, copyID, ht_stack, list_stack); 5846 } 5847 } 5848 } 5849 #endif 5850 return abort; 5851 } 5852 5853 static char * 5854 get_var_special_name(int nr) 5855 { 5856 switch (nr) 5857 { 5858 case VVAL_FALSE: return "v:false"; 5859 case VVAL_TRUE: return "v:true"; 5860 case VVAL_NONE: return "v:none"; 5861 case VVAL_NULL: return "v:null"; 5862 } 5863 internal_error("get_var_special_name()"); 5864 return "42"; 5865 } 5866 5867 /* 5868 * Return a string with the string representation of a variable. 5869 * If the memory is allocated "tofree" is set to it, otherwise NULL. 5870 * "numbuf" is used for a number. 5871 * When "copyID" is not NULL replace recursive lists and dicts with "...". 5872 * When both "echo_style" and "composite_val" are FALSE, put quotes around 5873 * stings as "string()", otherwise does not put quotes around strings, as 5874 * ":echo" displays values. 5875 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists 5876 * are replaced with "...". 5877 * May return NULL. 5878 */ 5879 char_u * 5880 echo_string_core( 5881 typval_T *tv, 5882 char_u **tofree, 5883 char_u *numbuf, 5884 int copyID, 5885 int echo_style, 5886 int restore_copyID, 5887 int composite_val) 5888 { 5889 static int recurse = 0; 5890 char_u *r = NULL; 5891 5892 if (recurse >= DICT_MAXNEST) 5893 { 5894 if (!did_echo_string_emsg) 5895 { 5896 /* Only give this message once for a recursive call to avoid 5897 * flooding the user with errors. And stop iterating over lists 5898 * and dicts. */ 5899 did_echo_string_emsg = TRUE; 5900 emsg(_("E724: variable nested too deep for displaying")); 5901 } 5902 *tofree = NULL; 5903 return (char_u *)"{E724}"; 5904 } 5905 ++recurse; 5906 5907 switch (tv->v_type) 5908 { 5909 case VAR_STRING: 5910 if (echo_style && !composite_val) 5911 { 5912 *tofree = NULL; 5913 r = tv->vval.v_string; 5914 if (r == NULL) 5915 r = (char_u *)""; 5916 } 5917 else 5918 { 5919 *tofree = string_quote(tv->vval.v_string, FALSE); 5920 r = *tofree; 5921 } 5922 break; 5923 5924 case VAR_FUNC: 5925 if (echo_style) 5926 { 5927 *tofree = NULL; 5928 r = tv->vval.v_string; 5929 } 5930 else 5931 { 5932 *tofree = string_quote(tv->vval.v_string, TRUE); 5933 r = *tofree; 5934 } 5935 break; 5936 5937 case VAR_PARTIAL: 5938 { 5939 partial_T *pt = tv->vval.v_partial; 5940 char_u *fname = string_quote(pt == NULL ? NULL 5941 : partial_name(pt), FALSE); 5942 garray_T ga; 5943 int i; 5944 char_u *tf; 5945 5946 ga_init2(&ga, 1, 100); 5947 ga_concat(&ga, (char_u *)"function("); 5948 if (fname != NULL) 5949 { 5950 ga_concat(&ga, fname); 5951 vim_free(fname); 5952 } 5953 if (pt != NULL && pt->pt_argc > 0) 5954 { 5955 ga_concat(&ga, (char_u *)", ["); 5956 for (i = 0; i < pt->pt_argc; ++i) 5957 { 5958 if (i > 0) 5959 ga_concat(&ga, (char_u *)", "); 5960 ga_concat(&ga, 5961 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID)); 5962 vim_free(tf); 5963 } 5964 ga_concat(&ga, (char_u *)"]"); 5965 } 5966 if (pt != NULL && pt->pt_dict != NULL) 5967 { 5968 typval_T dtv; 5969 5970 ga_concat(&ga, (char_u *)", "); 5971 dtv.v_type = VAR_DICT; 5972 dtv.vval.v_dict = pt->pt_dict; 5973 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID)); 5974 vim_free(tf); 5975 } 5976 ga_concat(&ga, (char_u *)")"); 5977 5978 *tofree = ga.ga_data; 5979 r = *tofree; 5980 break; 5981 } 5982 5983 case VAR_BLOB: 5984 r = blob2string(tv->vval.v_blob, tofree, numbuf); 5985 break; 5986 5987 case VAR_LIST: 5988 if (tv->vval.v_list == NULL) 5989 { 5990 *tofree = NULL; 5991 r = NULL; 5992 } 5993 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID 5994 && tv->vval.v_list->lv_len > 0) 5995 { 5996 *tofree = NULL; 5997 r = (char_u *)"[...]"; 5998 } 5999 else 6000 { 6001 int old_copyID = tv->vval.v_list->lv_copyID; 6002 6003 tv->vval.v_list->lv_copyID = copyID; 6004 *tofree = list2string(tv, copyID, restore_copyID); 6005 if (restore_copyID) 6006 tv->vval.v_list->lv_copyID = old_copyID; 6007 r = *tofree; 6008 } 6009 break; 6010 6011 case VAR_DICT: 6012 if (tv->vval.v_dict == NULL) 6013 { 6014 *tofree = NULL; 6015 r = NULL; 6016 } 6017 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID 6018 && tv->vval.v_dict->dv_hashtab.ht_used != 0) 6019 { 6020 *tofree = NULL; 6021 r = (char_u *)"{...}"; 6022 } 6023 else 6024 { 6025 int old_copyID = tv->vval.v_dict->dv_copyID; 6026 tv->vval.v_dict->dv_copyID = copyID; 6027 *tofree = dict2string(tv, copyID, restore_copyID); 6028 if (restore_copyID) 6029 tv->vval.v_dict->dv_copyID = old_copyID; 6030 r = *tofree; 6031 } 6032 break; 6033 6034 case VAR_NUMBER: 6035 case VAR_UNKNOWN: 6036 *tofree = NULL; 6037 r = tv_get_string_buf(tv, numbuf); 6038 break; 6039 6040 case VAR_JOB: 6041 case VAR_CHANNEL: 6042 *tofree = NULL; 6043 r = tv_get_string_buf(tv, numbuf); 6044 if (composite_val) 6045 { 6046 *tofree = string_quote(r, FALSE); 6047 r = *tofree; 6048 } 6049 break; 6050 6051 case VAR_FLOAT: 6052 #ifdef FEAT_FLOAT 6053 *tofree = NULL; 6054 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float); 6055 r = numbuf; 6056 break; 6057 #endif 6058 6059 case VAR_SPECIAL: 6060 *tofree = NULL; 6061 r = (char_u *)get_var_special_name(tv->vval.v_number); 6062 break; 6063 } 6064 6065 if (--recurse == 0) 6066 did_echo_string_emsg = FALSE; 6067 return r; 6068 } 6069 6070 /* 6071 * Return a string with the string representation of a variable. 6072 * If the memory is allocated "tofree" is set to it, otherwise NULL. 6073 * "numbuf" is used for a number. 6074 * Does not put quotes around strings, as ":echo" displays values. 6075 * When "copyID" is not NULL replace recursive lists and dicts with "...". 6076 * May return NULL. 6077 */ 6078 char_u * 6079 echo_string( 6080 typval_T *tv, 6081 char_u **tofree, 6082 char_u *numbuf, 6083 int copyID) 6084 { 6085 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE); 6086 } 6087 6088 /* 6089 * Return a string with the string representation of a variable. 6090 * If the memory is allocated "tofree" is set to it, otherwise NULL. 6091 * "numbuf" is used for a number. 6092 * Puts quotes around strings, so that they can be parsed back by eval(). 6093 * May return NULL. 6094 */ 6095 char_u * 6096 tv2string( 6097 typval_T *tv, 6098 char_u **tofree, 6099 char_u *numbuf, 6100 int copyID) 6101 { 6102 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE); 6103 } 6104 6105 /* 6106 * Return string "str" in ' quotes, doubling ' characters. 6107 * If "str" is NULL an empty string is assumed. 6108 * If "function" is TRUE make it function('string'). 6109 */ 6110 char_u * 6111 string_quote(char_u *str, int function) 6112 { 6113 unsigned len; 6114 char_u *p, *r, *s; 6115 6116 len = (function ? 13 : 3); 6117 if (str != NULL) 6118 { 6119 len += (unsigned)STRLEN(str); 6120 for (p = str; *p != NUL; MB_PTR_ADV(p)) 6121 if (*p == '\'') 6122 ++len; 6123 } 6124 s = r = alloc(len); 6125 if (r != NULL) 6126 { 6127 if (function) 6128 { 6129 STRCPY(r, "function('"); 6130 r += 10; 6131 } 6132 else 6133 *r++ = '\''; 6134 if (str != NULL) 6135 for (p = str; *p != NUL; ) 6136 { 6137 if (*p == '\'') 6138 *r++ = '\''; 6139 MB_COPY_CHAR(p, r); 6140 } 6141 *r++ = '\''; 6142 if (function) 6143 *r++ = ')'; 6144 *r++ = NUL; 6145 } 6146 return s; 6147 } 6148 6149 #if defined(FEAT_FLOAT) || defined(PROTO) 6150 /* 6151 * Convert the string "text" to a floating point number. 6152 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure 6153 * this always uses a decimal point. 6154 * Returns the length of the text that was consumed. 6155 */ 6156 int 6157 string2float( 6158 char_u *text, 6159 float_T *value) /* result stored here */ 6160 { 6161 char *s = (char *)text; 6162 float_T f; 6163 6164 /* MS-Windows does not deal with "inf" and "nan" properly. */ 6165 if (STRNICMP(text, "inf", 3) == 0) 6166 { 6167 *value = INFINITY; 6168 return 3; 6169 } 6170 if (STRNICMP(text, "-inf", 3) == 0) 6171 { 6172 *value = -INFINITY; 6173 return 4; 6174 } 6175 if (STRNICMP(text, "nan", 3) == 0) 6176 { 6177 *value = NAN; 6178 return 3; 6179 } 6180 f = strtod(s, &s); 6181 *value = f; 6182 return (int)((char_u *)s - text); 6183 } 6184 #endif 6185 6186 /* 6187 * Get the value of an environment variable. 6188 * "arg" is pointing to the '$'. It is advanced to after the name. 6189 * If the environment variable was not set, silently assume it is empty. 6190 * Return FAIL if the name is invalid. 6191 */ 6192 static int 6193 get_env_tv(char_u **arg, typval_T *rettv, int evaluate) 6194 { 6195 char_u *string = NULL; 6196 int len; 6197 int cc; 6198 char_u *name; 6199 int mustfree = FALSE; 6200 6201 ++*arg; 6202 name = *arg; 6203 len = get_env_len(arg); 6204 if (evaluate) 6205 { 6206 if (len == 0) 6207 return FAIL; /* invalid empty name */ 6208 6209 cc = name[len]; 6210 name[len] = NUL; 6211 /* first try vim_getenv(), fast for normal environment vars */ 6212 string = vim_getenv(name, &mustfree); 6213 if (string != NULL && *string != NUL) 6214 { 6215 if (!mustfree) 6216 string = vim_strsave(string); 6217 } 6218 else 6219 { 6220 if (mustfree) 6221 vim_free(string); 6222 6223 /* next try expanding things like $VIM and ${HOME} */ 6224 string = expand_env_save(name - 1); 6225 if (string != NULL && *string == '$') 6226 VIM_CLEAR(string); 6227 } 6228 name[len] = cc; 6229 6230 rettv->v_type = VAR_STRING; 6231 rettv->vval.v_string = string; 6232 } 6233 6234 return OK; 6235 } 6236 6237 6238 6239 /* 6240 * Translate a String variable into a position. 6241 * Returns NULL when there is an error. 6242 */ 6243 pos_T * 6244 var2fpos( 6245 typval_T *varp, 6246 int dollar_lnum, /* TRUE when $ is last line */ 6247 int *fnum) /* set to fnum for '0, 'A, etc. */ 6248 { 6249 char_u *name; 6250 static pos_T pos; 6251 pos_T *pp; 6252 6253 /* Argument can be [lnum, col, coladd]. */ 6254 if (varp->v_type == VAR_LIST) 6255 { 6256 list_T *l; 6257 int len; 6258 int error = FALSE; 6259 listitem_T *li; 6260 6261 l = varp->vval.v_list; 6262 if (l == NULL) 6263 return NULL; 6264 6265 /* Get the line number */ 6266 pos.lnum = list_find_nr(l, 0L, &error); 6267 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count) 6268 return NULL; /* invalid line number */ 6269 6270 /* Get the column number */ 6271 pos.col = list_find_nr(l, 1L, &error); 6272 if (error) 6273 return NULL; 6274 len = (long)STRLEN(ml_get(pos.lnum)); 6275 6276 /* We accept "$" for the column number: last column. */ 6277 li = list_find(l, 1L); 6278 if (li != NULL && li->li_tv.v_type == VAR_STRING 6279 && li->li_tv.vval.v_string != NULL 6280 && STRCMP(li->li_tv.vval.v_string, "$") == 0) 6281 pos.col = len + 1; 6282 6283 /* Accept a position up to the NUL after the line. */ 6284 if (pos.col == 0 || (int)pos.col > len + 1) 6285 return NULL; /* invalid column number */ 6286 --pos.col; 6287 6288 /* Get the virtual offset. Defaults to zero. */ 6289 pos.coladd = list_find_nr(l, 2L, &error); 6290 if (error) 6291 pos.coladd = 0; 6292 6293 return &pos; 6294 } 6295 6296 name = tv_get_string_chk(varp); 6297 if (name == NULL) 6298 return NULL; 6299 if (name[0] == '.') /* cursor */ 6300 return &curwin->w_cursor; 6301 if (name[0] == 'v' && name[1] == NUL) /* Visual start */ 6302 { 6303 if (VIsual_active) 6304 return &VIsual; 6305 return &curwin->w_cursor; 6306 } 6307 if (name[0] == '\'') /* mark */ 6308 { 6309 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum); 6310 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0) 6311 return NULL; 6312 return pp; 6313 } 6314 6315 pos.coladd = 0; 6316 6317 if (name[0] == 'w' && dollar_lnum) 6318 { 6319 pos.col = 0; 6320 if (name[1] == '0') /* "w0": first visible line */ 6321 { 6322 update_topline(); 6323 /* In silent Ex mode topline is zero, but that's not a valid line 6324 * number; use one instead. */ 6325 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1; 6326 return &pos; 6327 } 6328 else if (name[1] == '$') /* "w$": last visible line */ 6329 { 6330 validate_botline(); 6331 /* In silent Ex mode botline is zero, return zero then. */ 6332 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0; 6333 return &pos; 6334 } 6335 } 6336 else if (name[0] == '$') /* last column or line */ 6337 { 6338 if (dollar_lnum) 6339 { 6340 pos.lnum = curbuf->b_ml.ml_line_count; 6341 pos.col = 0; 6342 } 6343 else 6344 { 6345 pos.lnum = curwin->w_cursor.lnum; 6346 pos.col = (colnr_T)STRLEN(ml_get_curline()); 6347 } 6348 return &pos; 6349 } 6350 return NULL; 6351 } 6352 6353 /* 6354 * Convert list in "arg" into a position and optional file number. 6355 * When "fnump" is NULL there is no file number, only 3 items. 6356 * Note that the column is passed on as-is, the caller may want to decrement 6357 * it to use 1 for the first column. 6358 * Return FAIL when conversion is not possible, doesn't check the position for 6359 * validity. 6360 */ 6361 int 6362 list2fpos( 6363 typval_T *arg, 6364 pos_T *posp, 6365 int *fnump, 6366 colnr_T *curswantp) 6367 { 6368 list_T *l = arg->vval.v_list; 6369 long i = 0; 6370 long n; 6371 6372 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only 6373 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */ 6374 if (arg->v_type != VAR_LIST 6375 || l == NULL 6376 || l->lv_len < (fnump == NULL ? 2 : 3) 6377 || l->lv_len > (fnump == NULL ? 4 : 5)) 6378 return FAIL; 6379 6380 if (fnump != NULL) 6381 { 6382 n = list_find_nr(l, i++, NULL); /* fnum */ 6383 if (n < 0) 6384 return FAIL; 6385 if (n == 0) 6386 n = curbuf->b_fnum; /* current buffer */ 6387 *fnump = n; 6388 } 6389 6390 n = list_find_nr(l, i++, NULL); /* lnum */ 6391 if (n < 0) 6392 return FAIL; 6393 posp->lnum = n; 6394 6395 n = list_find_nr(l, i++, NULL); /* col */ 6396 if (n < 0) 6397 return FAIL; 6398 posp->col = n; 6399 6400 n = list_find_nr(l, i, NULL); /* off */ 6401 if (n < 0) 6402 posp->coladd = 0; 6403 else 6404 posp->coladd = n; 6405 6406 if (curswantp != NULL) 6407 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */ 6408 6409 return OK; 6410 } 6411 6412 /* 6413 * Get the length of an environment variable name. 6414 * Advance "arg" to the first character after the name. 6415 * Return 0 for error. 6416 */ 6417 static int 6418 get_env_len(char_u **arg) 6419 { 6420 char_u *p; 6421 int len; 6422 6423 for (p = *arg; vim_isIDc(*p); ++p) 6424 ; 6425 if (p == *arg) /* no name found */ 6426 return 0; 6427 6428 len = (int)(p - *arg); 6429 *arg = p; 6430 return len; 6431 } 6432 6433 /* 6434 * Get the length of the name of a function or internal variable. 6435 * "arg" is advanced to the first non-white character after the name. 6436 * Return 0 if something is wrong. 6437 */ 6438 int 6439 get_id_len(char_u **arg) 6440 { 6441 char_u *p; 6442 int len; 6443 6444 /* Find the end of the name. */ 6445 for (p = *arg; eval_isnamec(*p); ++p) 6446 { 6447 if (*p == ':') 6448 { 6449 /* "s:" is start of "s:var", but "n:" is not and can be used in 6450 * slice "[n:]". Also "xx:" is not a namespace. */ 6451 len = (int)(p - *arg); 6452 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL) 6453 || len > 1) 6454 break; 6455 } 6456 } 6457 if (p == *arg) /* no name found */ 6458 return 0; 6459 6460 len = (int)(p - *arg); 6461 *arg = skipwhite(p); 6462 6463 return len; 6464 } 6465 6466 /* 6467 * Get the length of the name of a variable or function. 6468 * Only the name is recognized, does not handle ".key" or "[idx]". 6469 * "arg" is advanced to the first non-white character after the name. 6470 * Return -1 if curly braces expansion failed. 6471 * Return 0 if something else is wrong. 6472 * If the name contains 'magic' {}'s, expand them and return the 6473 * expanded name in an allocated string via 'alias' - caller must free. 6474 */ 6475 int 6476 get_name_len( 6477 char_u **arg, 6478 char_u **alias, 6479 int evaluate, 6480 int verbose) 6481 { 6482 int len; 6483 char_u *p; 6484 char_u *expr_start; 6485 char_u *expr_end; 6486 6487 *alias = NULL; /* default to no alias */ 6488 6489 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA 6490 && (*arg)[2] == (int)KE_SNR) 6491 { 6492 /* hard coded <SNR>, already translated */ 6493 *arg += 3; 6494 return get_id_len(arg) + 3; 6495 } 6496 len = eval_fname_script(*arg); 6497 if (len > 0) 6498 { 6499 /* literal "<SID>", "s:" or "<SNR>" */ 6500 *arg += len; 6501 } 6502 6503 /* 6504 * Find the end of the name; check for {} construction. 6505 */ 6506 p = find_name_end(*arg, &expr_start, &expr_end, 6507 len > 0 ? 0 : FNE_CHECK_START); 6508 if (expr_start != NULL) 6509 { 6510 char_u *temp_string; 6511 6512 if (!evaluate) 6513 { 6514 len += (int)(p - *arg); 6515 *arg = skipwhite(p); 6516 return len; 6517 } 6518 6519 /* 6520 * Include any <SID> etc in the expanded string: 6521 * Thus the -len here. 6522 */ 6523 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p); 6524 if (temp_string == NULL) 6525 return -1; 6526 *alias = temp_string; 6527 *arg = skipwhite(p); 6528 return (int)STRLEN(temp_string); 6529 } 6530 6531 len += get_id_len(arg); 6532 // Only give an error when there is something, otherwise it will be 6533 // reported at a higher level. 6534 if (len == 0 && verbose && **arg != NUL) 6535 semsg(_(e_invexpr2), *arg); 6536 6537 return len; 6538 } 6539 6540 /* 6541 * Find the end of a variable or function name, taking care of magic braces. 6542 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the 6543 * start and end of the first magic braces item. 6544 * "flags" can have FNE_INCL_BR and FNE_CHECK_START. 6545 * Return a pointer to just after the name. Equal to "arg" if there is no 6546 * valid name. 6547 */ 6548 char_u * 6549 find_name_end( 6550 char_u *arg, 6551 char_u **expr_start, 6552 char_u **expr_end, 6553 int flags) 6554 { 6555 int mb_nest = 0; 6556 int br_nest = 0; 6557 char_u *p; 6558 int len; 6559 6560 if (expr_start != NULL) 6561 { 6562 *expr_start = NULL; 6563 *expr_end = NULL; 6564 } 6565 6566 /* Quick check for valid starting character. */ 6567 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{') 6568 return arg; 6569 6570 for (p = arg; *p != NUL 6571 && (eval_isnamec(*p) 6572 || *p == '{' 6573 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.')) 6574 || mb_nest != 0 6575 || br_nest != 0); MB_PTR_ADV(p)) 6576 { 6577 if (*p == '\'') 6578 { 6579 /* skip over 'string' to avoid counting [ and ] inside it. */ 6580 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p)) 6581 ; 6582 if (*p == NUL) 6583 break; 6584 } 6585 else if (*p == '"') 6586 { 6587 /* skip over "str\"ing" to avoid counting [ and ] inside it. */ 6588 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p)) 6589 if (*p == '\\' && p[1] != NUL) 6590 ++p; 6591 if (*p == NUL) 6592 break; 6593 } 6594 else if (br_nest == 0 && mb_nest == 0 && *p == ':') 6595 { 6596 /* "s:" is start of "s:var", but "n:" is not and can be used in 6597 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */ 6598 len = (int)(p - arg); 6599 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL) 6600 || (len > 1 && p[-1] != '}')) 6601 break; 6602 } 6603 6604 if (mb_nest == 0) 6605 { 6606 if (*p == '[') 6607 ++br_nest; 6608 else if (*p == ']') 6609 --br_nest; 6610 } 6611 6612 if (br_nest == 0) 6613 { 6614 if (*p == '{') 6615 { 6616 mb_nest++; 6617 if (expr_start != NULL && *expr_start == NULL) 6618 *expr_start = p; 6619 } 6620 else if (*p == '}') 6621 { 6622 mb_nest--; 6623 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL) 6624 *expr_end = p; 6625 } 6626 } 6627 } 6628 6629 return p; 6630 } 6631 6632 /* 6633 * Expands out the 'magic' {}'s in a variable/function name. 6634 * Note that this can call itself recursively, to deal with 6635 * constructs like foo{bar}{baz}{bam} 6636 * The four pointer arguments point to "foo{expre}ss{ion}bar" 6637 * "in_start" ^ 6638 * "expr_start" ^ 6639 * "expr_end" ^ 6640 * "in_end" ^ 6641 * 6642 * Returns a new allocated string, which the caller must free. 6643 * Returns NULL for failure. 6644 */ 6645 static char_u * 6646 make_expanded_name( 6647 char_u *in_start, 6648 char_u *expr_start, 6649 char_u *expr_end, 6650 char_u *in_end) 6651 { 6652 char_u c1; 6653 char_u *retval = NULL; 6654 char_u *temp_result; 6655 char_u *nextcmd = NULL; 6656 6657 if (expr_end == NULL || in_end == NULL) 6658 return NULL; 6659 *expr_start = NUL; 6660 *expr_end = NUL; 6661 c1 = *in_end; 6662 *in_end = NUL; 6663 6664 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE); 6665 if (temp_result != NULL && nextcmd == NULL) 6666 { 6667 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start) 6668 + (in_end - expr_end) + 1)); 6669 if (retval != NULL) 6670 { 6671 STRCPY(retval, in_start); 6672 STRCAT(retval, temp_result); 6673 STRCAT(retval, expr_end + 1); 6674 } 6675 } 6676 vim_free(temp_result); 6677 6678 *in_end = c1; /* put char back for error messages */ 6679 *expr_start = '{'; 6680 *expr_end = '}'; 6681 6682 if (retval != NULL) 6683 { 6684 temp_result = find_name_end(retval, &expr_start, &expr_end, 0); 6685 if (expr_start != NULL) 6686 { 6687 /* Further expansion! */ 6688 temp_result = make_expanded_name(retval, expr_start, 6689 expr_end, temp_result); 6690 vim_free(retval); 6691 retval = temp_result; 6692 } 6693 } 6694 6695 return retval; 6696 } 6697 6698 /* 6699 * Return TRUE if character "c" can be used in a variable or function name. 6700 * Does not include '{' or '}' for magic braces. 6701 */ 6702 int 6703 eval_isnamec(int c) 6704 { 6705 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR); 6706 } 6707 6708 /* 6709 * Return TRUE if character "c" can be used as the first character in a 6710 * variable or function name (excluding '{' and '}'). 6711 */ 6712 int 6713 eval_isnamec1(int c) 6714 { 6715 return (ASCII_ISALPHA(c) || c == '_'); 6716 } 6717 6718 /* 6719 * Set number v: variable to "val". 6720 */ 6721 void 6722 set_vim_var_nr(int idx, varnumber_T val) 6723 { 6724 vimvars[idx].vv_nr = val; 6725 } 6726 6727 /* 6728 * Get number v: variable value. 6729 */ 6730 varnumber_T 6731 get_vim_var_nr(int idx) 6732 { 6733 return vimvars[idx].vv_nr; 6734 } 6735 6736 /* 6737 * Get string v: variable value. Uses a static buffer, can only be used once. 6738 * If the String variable has never been set, return an empty string. 6739 * Never returns NULL; 6740 */ 6741 char_u * 6742 get_vim_var_str(int idx) 6743 { 6744 return tv_get_string(&vimvars[idx].vv_tv); 6745 } 6746 6747 /* 6748 * Get List v: variable value. Caller must take care of reference count when 6749 * needed. 6750 */ 6751 list_T * 6752 get_vim_var_list(int idx) 6753 { 6754 return vimvars[idx].vv_list; 6755 } 6756 6757 /* 6758 * Get Dict v: variable value. Caller must take care of reference count when 6759 * needed. 6760 */ 6761 dict_T * 6762 get_vim_var_dict(int idx) 6763 { 6764 return vimvars[idx].vv_dict; 6765 } 6766 6767 /* 6768 * Set v:char to character "c". 6769 */ 6770 void 6771 set_vim_var_char(int c) 6772 { 6773 char_u buf[MB_MAXBYTES + 1]; 6774 6775 if (has_mbyte) 6776 buf[(*mb_char2bytes)(c, buf)] = NUL; 6777 else 6778 { 6779 buf[0] = c; 6780 buf[1] = NUL; 6781 } 6782 set_vim_var_string(VV_CHAR, buf, -1); 6783 } 6784 6785 /* 6786 * Set v:count to "count" and v:count1 to "count1". 6787 * When "set_prevcount" is TRUE first set v:prevcount from v:count. 6788 */ 6789 void 6790 set_vcount( 6791 long count, 6792 long count1, 6793 int set_prevcount) 6794 { 6795 if (set_prevcount) 6796 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr; 6797 vimvars[VV_COUNT].vv_nr = count; 6798 vimvars[VV_COUNT1].vv_nr = count1; 6799 } 6800 6801 /* 6802 * Save variables that might be changed as a side effect. Used when executing 6803 * a timer callback. 6804 */ 6805 void 6806 save_vimvars(vimvars_save_T *vvsave) 6807 { 6808 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr; 6809 vvsave->vv_count = vimvars[VV_COUNT].vv_nr; 6810 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr; 6811 } 6812 6813 /* 6814 * Restore variables saved by save_vimvars(). 6815 */ 6816 void 6817 restore_vimvars(vimvars_save_T *vvsave) 6818 { 6819 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount; 6820 vimvars[VV_COUNT].vv_nr = vvsave->vv_count; 6821 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1; 6822 } 6823 6824 /* 6825 * Set string v: variable to a copy of "val". 6826 */ 6827 void 6828 set_vim_var_string( 6829 int idx, 6830 char_u *val, 6831 int len) /* length of "val" to use or -1 (whole string) */ 6832 { 6833 clear_tv(&vimvars[idx].vv_di.di_tv); 6834 vimvars[idx].vv_type = VAR_STRING; 6835 if (val == NULL) 6836 vimvars[idx].vv_str = NULL; 6837 else if (len == -1) 6838 vimvars[idx].vv_str = vim_strsave(val); 6839 else 6840 vimvars[idx].vv_str = vim_strnsave(val, len); 6841 } 6842 6843 /* 6844 * Set List v: variable to "val". 6845 */ 6846 void 6847 set_vim_var_list(int idx, list_T *val) 6848 { 6849 clear_tv(&vimvars[idx].vv_di.di_tv); 6850 vimvars[idx].vv_type = VAR_LIST; 6851 vimvars[idx].vv_list = val; 6852 if (val != NULL) 6853 ++val->lv_refcount; 6854 } 6855 6856 /* 6857 * Set Dictionary v: variable to "val". 6858 */ 6859 void 6860 set_vim_var_dict(int idx, dict_T *val) 6861 { 6862 clear_tv(&vimvars[idx].vv_di.di_tv); 6863 vimvars[idx].vv_type = VAR_DICT; 6864 vimvars[idx].vv_dict = val; 6865 if (val != NULL) 6866 { 6867 ++val->dv_refcount; 6868 dict_set_items_ro(val); 6869 } 6870 } 6871 6872 /* 6873 * Set v:register if needed. 6874 */ 6875 void 6876 set_reg_var(int c) 6877 { 6878 char_u regname; 6879 6880 if (c == 0 || c == ' ') 6881 regname = '"'; 6882 else 6883 regname = c; 6884 /* Avoid free/alloc when the value is already right. */ 6885 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c) 6886 set_vim_var_string(VV_REG, ®name, 1); 6887 } 6888 6889 /* 6890 * Get or set v:exception. If "oldval" == NULL, return the current value. 6891 * Otherwise, restore the value to "oldval" and return NULL. 6892 * Must always be called in pairs to save and restore v:exception! Does not 6893 * take care of memory allocations. 6894 */ 6895 char_u * 6896 v_exception(char_u *oldval) 6897 { 6898 if (oldval == NULL) 6899 return vimvars[VV_EXCEPTION].vv_str; 6900 6901 vimvars[VV_EXCEPTION].vv_str = oldval; 6902 return NULL; 6903 } 6904 6905 /* 6906 * Get or set v:throwpoint. If "oldval" == NULL, return the current value. 6907 * Otherwise, restore the value to "oldval" and return NULL. 6908 * Must always be called in pairs to save and restore v:throwpoint! Does not 6909 * take care of memory allocations. 6910 */ 6911 char_u * 6912 v_throwpoint(char_u *oldval) 6913 { 6914 if (oldval == NULL) 6915 return vimvars[VV_THROWPOINT].vv_str; 6916 6917 vimvars[VV_THROWPOINT].vv_str = oldval; 6918 return NULL; 6919 } 6920 6921 /* 6922 * Set v:cmdarg. 6923 * If "eap" != NULL, use "eap" to generate the value and return the old value. 6924 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL. 6925 * Must always be called in pairs! 6926 */ 6927 char_u * 6928 set_cmdarg(exarg_T *eap, char_u *oldarg) 6929 { 6930 char_u *oldval; 6931 char_u *newval; 6932 unsigned len; 6933 6934 oldval = vimvars[VV_CMDARG].vv_str; 6935 if (eap == NULL) 6936 { 6937 vim_free(oldval); 6938 vimvars[VV_CMDARG].vv_str = oldarg; 6939 return NULL; 6940 } 6941 6942 if (eap->force_bin == FORCE_BIN) 6943 len = 6; 6944 else if (eap->force_bin == FORCE_NOBIN) 6945 len = 8; 6946 else 6947 len = 0; 6948 6949 if (eap->read_edit) 6950 len += 7; 6951 6952 if (eap->force_ff != 0) 6953 len += 10; /* " ++ff=unix" */ 6954 if (eap->force_enc != 0) 6955 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7; 6956 if (eap->bad_char != 0) 6957 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */ 6958 6959 newval = alloc(len + 1); 6960 if (newval == NULL) 6961 return NULL; 6962 6963 if (eap->force_bin == FORCE_BIN) 6964 sprintf((char *)newval, " ++bin"); 6965 else if (eap->force_bin == FORCE_NOBIN) 6966 sprintf((char *)newval, " ++nobin"); 6967 else 6968 *newval = NUL; 6969 6970 if (eap->read_edit) 6971 STRCAT(newval, " ++edit"); 6972 6973 if (eap->force_ff != 0) 6974 sprintf((char *)newval + STRLEN(newval), " ++ff=%s", 6975 eap->force_ff == 'u' ? "unix" 6976 : eap->force_ff == 'd' ? "dos" 6977 : "mac"); 6978 if (eap->force_enc != 0) 6979 sprintf((char *)newval + STRLEN(newval), " ++enc=%s", 6980 eap->cmd + eap->force_enc); 6981 if (eap->bad_char == BAD_KEEP) 6982 STRCPY(newval + STRLEN(newval), " ++bad=keep"); 6983 else if (eap->bad_char == BAD_DROP) 6984 STRCPY(newval + STRLEN(newval), " ++bad=drop"); 6985 else if (eap->bad_char != 0) 6986 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char); 6987 vimvars[VV_CMDARG].vv_str = newval; 6988 return oldval; 6989 } 6990 6991 /* 6992 * Get the value of internal variable "name". 6993 * Return OK or FAIL. If OK is returned "rettv" must be cleared. 6994 */ 6995 int 6996 get_var_tv( 6997 char_u *name, 6998 int len, /* length of "name" */ 6999 typval_T *rettv, /* NULL when only checking existence */ 7000 dictitem_T **dip, /* non-NULL when typval's dict item is needed */ 7001 int verbose, /* may give error message */ 7002 int no_autoload) /* do not use script autoloading */ 7003 { 7004 int ret = OK; 7005 typval_T *tv = NULL; 7006 dictitem_T *v; 7007 int cc; 7008 7009 /* truncate the name, so that we can use strcmp() */ 7010 cc = name[len]; 7011 name[len] = NUL; 7012 7013 /* 7014 * Check for user-defined variables. 7015 */ 7016 v = find_var(name, NULL, no_autoload); 7017 if (v != NULL) 7018 { 7019 tv = &v->di_tv; 7020 if (dip != NULL) 7021 *dip = v; 7022 } 7023 7024 if (tv == NULL) 7025 { 7026 if (rettv != NULL && verbose) 7027 semsg(_(e_undefvar), name); 7028 ret = FAIL; 7029 } 7030 else if (rettv != NULL) 7031 copy_tv(tv, rettv); 7032 7033 name[len] = cc; 7034 7035 return ret; 7036 } 7037 7038 /* 7039 * Check if variable "name[len]" is a local variable or an argument. 7040 * If so, "*eval_lavars_used" is set to TRUE. 7041 */ 7042 static void 7043 check_vars(char_u *name, int len) 7044 { 7045 int cc; 7046 char_u *varname; 7047 hashtab_T *ht; 7048 7049 if (eval_lavars_used == NULL) 7050 return; 7051 7052 /* truncate the name, so that we can use strcmp() */ 7053 cc = name[len]; 7054 name[len] = NUL; 7055 7056 ht = find_var_ht(name, &varname); 7057 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht()) 7058 { 7059 if (find_var(name, NULL, TRUE) != NULL) 7060 *eval_lavars_used = TRUE; 7061 } 7062 7063 name[len] = cc; 7064 } 7065 7066 /* 7067 * Handle expr[expr], expr[expr:expr] subscript and .name lookup. 7068 * Also handle function call with Funcref variable: func(expr) 7069 * Can all be combined: dict.func(expr)[idx]['func'](expr) 7070 */ 7071 int 7072 handle_subscript( 7073 char_u **arg, 7074 typval_T *rettv, 7075 int evaluate, /* do more than finding the end */ 7076 int verbose) /* give error messages */ 7077 { 7078 int ret = OK; 7079 dict_T *selfdict = NULL; 7080 char_u *s; 7081 int len; 7082 typval_T functv; 7083 7084 while (ret == OK 7085 && (**arg == '[' 7086 || (**arg == '.' && rettv->v_type == VAR_DICT) 7087 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC 7088 || rettv->v_type == VAR_PARTIAL))) 7089 && !VIM_ISWHITE(*(*arg - 1))) 7090 { 7091 if (**arg == '(') 7092 { 7093 partial_T *pt = NULL; 7094 7095 /* need to copy the funcref so that we can clear rettv */ 7096 if (evaluate) 7097 { 7098 functv = *rettv; 7099 rettv->v_type = VAR_UNKNOWN; 7100 7101 /* Invoke the function. Recursive! */ 7102 if (functv.v_type == VAR_PARTIAL) 7103 { 7104 pt = functv.vval.v_partial; 7105 s = partial_name(pt); 7106 } 7107 else 7108 s = functv.vval.v_string; 7109 } 7110 else 7111 s = (char_u *)""; 7112 ret = get_func_tv(s, -1, rettv, arg, 7113 curwin->w_cursor.lnum, curwin->w_cursor.lnum, 7114 &len, evaluate, pt, selfdict); 7115 7116 /* Clear the funcref afterwards, so that deleting it while 7117 * evaluating the arguments is possible (see test55). */ 7118 if (evaluate) 7119 clear_tv(&functv); 7120 7121 /* Stop the expression evaluation when immediately aborting on 7122 * error, or when an interrupt occurred or an exception was thrown 7123 * but not caught. */ 7124 if (aborting()) 7125 { 7126 if (ret == OK) 7127 clear_tv(rettv); 7128 ret = FAIL; 7129 } 7130 dict_unref(selfdict); 7131 selfdict = NULL; 7132 } 7133 else /* **arg == '[' || **arg == '.' */ 7134 { 7135 dict_unref(selfdict); 7136 if (rettv->v_type == VAR_DICT) 7137 { 7138 selfdict = rettv->vval.v_dict; 7139 if (selfdict != NULL) 7140 ++selfdict->dv_refcount; 7141 } 7142 else 7143 selfdict = NULL; 7144 if (eval_index(arg, rettv, evaluate, verbose) == FAIL) 7145 { 7146 clear_tv(rettv); 7147 ret = FAIL; 7148 } 7149 } 7150 } 7151 7152 /* Turn "dict.Func" into a partial for "Func" bound to "dict". 7153 * Don't do this when "Func" is already a partial that was bound 7154 * explicitly (pt_auto is FALSE). */ 7155 if (selfdict != NULL 7156 && (rettv->v_type == VAR_FUNC 7157 || (rettv->v_type == VAR_PARTIAL 7158 && (rettv->vval.v_partial->pt_auto 7159 || rettv->vval.v_partial->pt_dict == NULL)))) 7160 selfdict = make_partial(selfdict, rettv); 7161 7162 dict_unref(selfdict); 7163 return ret; 7164 } 7165 7166 /* 7167 * Allocate memory for a variable type-value, and make it empty (0 or NULL 7168 * value). 7169 */ 7170 typval_T * 7171 alloc_tv(void) 7172 { 7173 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T)); 7174 } 7175 7176 /* 7177 * Allocate memory for a variable type-value, and assign a string to it. 7178 * The string "s" must have been allocated, it is consumed. 7179 * Return NULL for out of memory, the variable otherwise. 7180 */ 7181 static typval_T * 7182 alloc_string_tv(char_u *s) 7183 { 7184 typval_T *rettv; 7185 7186 rettv = alloc_tv(); 7187 if (rettv != NULL) 7188 { 7189 rettv->v_type = VAR_STRING; 7190 rettv->vval.v_string = s; 7191 } 7192 else 7193 vim_free(s); 7194 return rettv; 7195 } 7196 7197 /* 7198 * Free the memory for a variable type-value. 7199 */ 7200 void 7201 free_tv(typval_T *varp) 7202 { 7203 if (varp != NULL) 7204 { 7205 switch (varp->v_type) 7206 { 7207 case VAR_FUNC: 7208 func_unref(varp->vval.v_string); 7209 /* FALLTHROUGH */ 7210 case VAR_STRING: 7211 vim_free(varp->vval.v_string); 7212 break; 7213 case VAR_PARTIAL: 7214 partial_unref(varp->vval.v_partial); 7215 break; 7216 case VAR_BLOB: 7217 blob_unref(varp->vval.v_blob); 7218 break; 7219 case VAR_LIST: 7220 list_unref(varp->vval.v_list); 7221 break; 7222 case VAR_DICT: 7223 dict_unref(varp->vval.v_dict); 7224 break; 7225 case VAR_JOB: 7226 #ifdef FEAT_JOB_CHANNEL 7227 job_unref(varp->vval.v_job); 7228 break; 7229 #endif 7230 case VAR_CHANNEL: 7231 #ifdef FEAT_JOB_CHANNEL 7232 channel_unref(varp->vval.v_channel); 7233 break; 7234 #endif 7235 case VAR_NUMBER: 7236 case VAR_FLOAT: 7237 case VAR_UNKNOWN: 7238 case VAR_SPECIAL: 7239 break; 7240 } 7241 vim_free(varp); 7242 } 7243 } 7244 7245 /* 7246 * Free the memory for a variable value and set the value to NULL or 0. 7247 */ 7248 void 7249 clear_tv(typval_T *varp) 7250 { 7251 if (varp != NULL) 7252 { 7253 switch (varp->v_type) 7254 { 7255 case VAR_FUNC: 7256 func_unref(varp->vval.v_string); 7257 /* FALLTHROUGH */ 7258 case VAR_STRING: 7259 VIM_CLEAR(varp->vval.v_string); 7260 break; 7261 case VAR_PARTIAL: 7262 partial_unref(varp->vval.v_partial); 7263 varp->vval.v_partial = NULL; 7264 break; 7265 case VAR_BLOB: 7266 blob_unref(varp->vval.v_blob); 7267 varp->vval.v_blob = NULL; 7268 break; 7269 case VAR_LIST: 7270 list_unref(varp->vval.v_list); 7271 varp->vval.v_list = NULL; 7272 break; 7273 case VAR_DICT: 7274 dict_unref(varp->vval.v_dict); 7275 varp->vval.v_dict = NULL; 7276 break; 7277 case VAR_NUMBER: 7278 case VAR_SPECIAL: 7279 varp->vval.v_number = 0; 7280 break; 7281 case VAR_FLOAT: 7282 #ifdef FEAT_FLOAT 7283 varp->vval.v_float = 0.0; 7284 break; 7285 #endif 7286 case VAR_JOB: 7287 #ifdef FEAT_JOB_CHANNEL 7288 job_unref(varp->vval.v_job); 7289 varp->vval.v_job = NULL; 7290 #endif 7291 break; 7292 case VAR_CHANNEL: 7293 #ifdef FEAT_JOB_CHANNEL 7294 channel_unref(varp->vval.v_channel); 7295 varp->vval.v_channel = NULL; 7296 #endif 7297 case VAR_UNKNOWN: 7298 break; 7299 } 7300 varp->v_lock = 0; 7301 } 7302 } 7303 7304 /* 7305 * Set the value of a variable to NULL without freeing items. 7306 */ 7307 void 7308 init_tv(typval_T *varp) 7309 { 7310 if (varp != NULL) 7311 vim_memset(varp, 0, sizeof(typval_T)); 7312 } 7313 7314 /* 7315 * Get the number value of a variable. 7316 * If it is a String variable, uses vim_str2nr(). 7317 * For incompatible types, return 0. 7318 * tv_get_number_chk() is similar to tv_get_number(), but informs the 7319 * caller of incompatible types: it sets *denote to TRUE if "denote" 7320 * is not NULL or returns -1 otherwise. 7321 */ 7322 varnumber_T 7323 tv_get_number(typval_T *varp) 7324 { 7325 int error = FALSE; 7326 7327 return tv_get_number_chk(varp, &error); /* return 0L on error */ 7328 } 7329 7330 varnumber_T 7331 tv_get_number_chk(typval_T *varp, int *denote) 7332 { 7333 varnumber_T n = 0L; 7334 7335 switch (varp->v_type) 7336 { 7337 case VAR_NUMBER: 7338 return varp->vval.v_number; 7339 case VAR_FLOAT: 7340 #ifdef FEAT_FLOAT 7341 emsg(_("E805: Using a Float as a Number")); 7342 break; 7343 #endif 7344 case VAR_FUNC: 7345 case VAR_PARTIAL: 7346 emsg(_("E703: Using a Funcref as a Number")); 7347 break; 7348 case VAR_STRING: 7349 if (varp->vval.v_string != NULL) 7350 vim_str2nr(varp->vval.v_string, NULL, NULL, 7351 STR2NR_ALL, &n, NULL, 0); 7352 return n; 7353 case VAR_LIST: 7354 emsg(_("E745: Using a List as a Number")); 7355 break; 7356 case VAR_DICT: 7357 emsg(_("E728: Using a Dictionary as a Number")); 7358 break; 7359 case VAR_SPECIAL: 7360 return varp->vval.v_number == VVAL_TRUE ? 1 : 0; 7361 break; 7362 case VAR_JOB: 7363 #ifdef FEAT_JOB_CHANNEL 7364 emsg(_("E910: Using a Job as a Number")); 7365 break; 7366 #endif 7367 case VAR_CHANNEL: 7368 #ifdef FEAT_JOB_CHANNEL 7369 emsg(_("E913: Using a Channel as a Number")); 7370 break; 7371 #endif 7372 case VAR_BLOB: 7373 emsg(_("E974: Using a Blob as a Number")); 7374 break; 7375 case VAR_UNKNOWN: 7376 internal_error("tv_get_number(UNKNOWN)"); 7377 break; 7378 } 7379 if (denote == NULL) /* useful for values that must be unsigned */ 7380 n = -1; 7381 else 7382 *denote = TRUE; 7383 return n; 7384 } 7385 7386 #ifdef FEAT_FLOAT 7387 float_T 7388 tv_get_float(typval_T *varp) 7389 { 7390 switch (varp->v_type) 7391 { 7392 case VAR_NUMBER: 7393 return (float_T)(varp->vval.v_number); 7394 case VAR_FLOAT: 7395 return varp->vval.v_float; 7396 case VAR_FUNC: 7397 case VAR_PARTIAL: 7398 emsg(_("E891: Using a Funcref as a Float")); 7399 break; 7400 case VAR_STRING: 7401 emsg(_("E892: Using a String as a Float")); 7402 break; 7403 case VAR_LIST: 7404 emsg(_("E893: Using a List as a Float")); 7405 break; 7406 case VAR_DICT: 7407 emsg(_("E894: Using a Dictionary as a Float")); 7408 break; 7409 case VAR_SPECIAL: 7410 emsg(_("E907: Using a special value as a Float")); 7411 break; 7412 case VAR_JOB: 7413 # ifdef FEAT_JOB_CHANNEL 7414 emsg(_("E911: Using a Job as a Float")); 7415 break; 7416 # endif 7417 case VAR_CHANNEL: 7418 # ifdef FEAT_JOB_CHANNEL 7419 emsg(_("E914: Using a Channel as a Float")); 7420 break; 7421 # endif 7422 case VAR_BLOB: 7423 emsg(_("E975: Using a Blob as a Float")); 7424 break; 7425 case VAR_UNKNOWN: 7426 internal_error("tv_get_float(UNKNOWN)"); 7427 break; 7428 } 7429 return 0; 7430 } 7431 #endif 7432 7433 /* 7434 * Get the string value of a variable. 7435 * If it is a Number variable, the number is converted into a string. 7436 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE! 7437 * tv_get_string_buf() uses a given buffer. 7438 * If the String variable has never been set, return an empty string. 7439 * Never returns NULL; 7440 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return 7441 * NULL on error. 7442 */ 7443 char_u * 7444 tv_get_string(typval_T *varp) 7445 { 7446 static char_u mybuf[NUMBUFLEN]; 7447 7448 return tv_get_string_buf(varp, mybuf); 7449 } 7450 7451 char_u * 7452 tv_get_string_buf(typval_T *varp, char_u *buf) 7453 { 7454 char_u *res = tv_get_string_buf_chk(varp, buf); 7455 7456 return res != NULL ? res : (char_u *)""; 7457 } 7458 7459 /* 7460 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE! 7461 */ 7462 char_u * 7463 tv_get_string_chk(typval_T *varp) 7464 { 7465 static char_u mybuf[NUMBUFLEN]; 7466 7467 return tv_get_string_buf_chk(varp, mybuf); 7468 } 7469 7470 char_u * 7471 tv_get_string_buf_chk(typval_T *varp, char_u *buf) 7472 { 7473 switch (varp->v_type) 7474 { 7475 case VAR_NUMBER: 7476 vim_snprintf((char *)buf, NUMBUFLEN, "%lld", 7477 (long_long_T)varp->vval.v_number); 7478 return buf; 7479 case VAR_FUNC: 7480 case VAR_PARTIAL: 7481 emsg(_("E729: using Funcref as a String")); 7482 break; 7483 case VAR_LIST: 7484 emsg(_("E730: using List as a String")); 7485 break; 7486 case VAR_DICT: 7487 emsg(_("E731: using Dictionary as a String")); 7488 break; 7489 case VAR_FLOAT: 7490 #ifdef FEAT_FLOAT 7491 emsg(_(e_float_as_string)); 7492 break; 7493 #endif 7494 case VAR_STRING: 7495 if (varp->vval.v_string != NULL) 7496 return varp->vval.v_string; 7497 return (char_u *)""; 7498 case VAR_SPECIAL: 7499 STRCPY(buf, get_var_special_name(varp->vval.v_number)); 7500 return buf; 7501 case VAR_BLOB: 7502 emsg(_("E976: using Blob as a String")); 7503 break; 7504 case VAR_JOB: 7505 #ifdef FEAT_JOB_CHANNEL 7506 { 7507 job_T *job = varp->vval.v_job; 7508 char *status; 7509 7510 if (job == NULL) 7511 return (char_u *)"no process"; 7512 status = job->jv_status == JOB_FAILED ? "fail" 7513 : job->jv_status >= JOB_ENDED ? "dead" 7514 : "run"; 7515 # ifdef UNIX 7516 vim_snprintf((char *)buf, NUMBUFLEN, 7517 "process %ld %s", (long)job->jv_pid, status); 7518 # elif defined(MSWIN) 7519 vim_snprintf((char *)buf, NUMBUFLEN, 7520 "process %ld %s", 7521 (long)job->jv_proc_info.dwProcessId, 7522 status); 7523 # else 7524 /* fall-back */ 7525 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status); 7526 # endif 7527 return buf; 7528 } 7529 #endif 7530 break; 7531 case VAR_CHANNEL: 7532 #ifdef FEAT_JOB_CHANNEL 7533 { 7534 channel_T *channel = varp->vval.v_channel; 7535 char *status = channel_status(channel, -1); 7536 7537 if (channel == NULL) 7538 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status); 7539 else 7540 vim_snprintf((char *)buf, NUMBUFLEN, 7541 "channel %d %s", channel->ch_id, status); 7542 return buf; 7543 } 7544 #endif 7545 break; 7546 case VAR_UNKNOWN: 7547 emsg(_("E908: using an invalid value as a String")); 7548 break; 7549 } 7550 return NULL; 7551 } 7552 7553 /* 7554 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses 7555 * string() on Dict, List, etc. 7556 */ 7557 char_u * 7558 tv_stringify(typval_T *varp, char_u *buf) 7559 { 7560 if (varp->v_type == VAR_LIST 7561 || varp->v_type == VAR_DICT 7562 || varp->v_type == VAR_FUNC 7563 || varp->v_type == VAR_PARTIAL 7564 || varp->v_type == VAR_FLOAT) 7565 { 7566 typval_T tmp; 7567 7568 f_string(varp, &tmp); 7569 tv_get_string_buf(&tmp, buf); 7570 clear_tv(varp); 7571 *varp = tmp; 7572 return tmp.vval.v_string; 7573 } 7574 return tv_get_string_buf(varp, buf); 7575 } 7576 7577 /* 7578 * Find variable "name" in the list of variables. 7579 * Return a pointer to it if found, NULL if not found. 7580 * Careful: "a:0" variables don't have a name. 7581 * When "htp" is not NULL we are writing to the variable, set "htp" to the 7582 * hashtab_T used. 7583 */ 7584 dictitem_T * 7585 find_var(char_u *name, hashtab_T **htp, int no_autoload) 7586 { 7587 char_u *varname; 7588 hashtab_T *ht; 7589 dictitem_T *ret = NULL; 7590 7591 ht = find_var_ht(name, &varname); 7592 if (htp != NULL) 7593 *htp = ht; 7594 if (ht == NULL) 7595 return NULL; 7596 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL); 7597 if (ret != NULL) 7598 return ret; 7599 7600 /* Search in parent scope for lambda */ 7601 return find_var_in_scoped_ht(name, no_autoload || htp != NULL); 7602 } 7603 7604 /* 7605 * Find variable "varname" in hashtab "ht" with name "htname". 7606 * Returns NULL if not found. 7607 */ 7608 dictitem_T * 7609 find_var_in_ht( 7610 hashtab_T *ht, 7611 int htname, 7612 char_u *varname, 7613 int no_autoload) 7614 { 7615 hashitem_T *hi; 7616 7617 if (*varname == NUL) 7618 { 7619 /* Must be something like "s:", otherwise "ht" would be NULL. */ 7620 switch (htname) 7621 { 7622 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var; 7623 case 'g': return &globvars_var; 7624 case 'v': return &vimvars_var; 7625 case 'b': return &curbuf->b_bufvar; 7626 case 'w': return &curwin->w_winvar; 7627 case 't': return &curtab->tp_winvar; 7628 case 'l': return get_funccal_local_var(); 7629 case 'a': return get_funccal_args_var(); 7630 } 7631 return NULL; 7632 } 7633 7634 hi = hash_find(ht, varname); 7635 if (HASHITEM_EMPTY(hi)) 7636 { 7637 /* For global variables we may try auto-loading the script. If it 7638 * worked find the variable again. Don't auto-load a script if it was 7639 * loaded already, otherwise it would be loaded every time when 7640 * checking if a function name is a Funcref variable. */ 7641 if (ht == &globvarht && !no_autoload) 7642 { 7643 /* Note: script_autoload() may make "hi" invalid. It must either 7644 * be obtained again or not used. */ 7645 if (!script_autoload(varname, FALSE) || aborting()) 7646 return NULL; 7647 hi = hash_find(ht, varname); 7648 } 7649 if (HASHITEM_EMPTY(hi)) 7650 return NULL; 7651 } 7652 return HI2DI(hi); 7653 } 7654 7655 /* 7656 * Find the hashtab used for a variable name. 7657 * Return NULL if the name is not valid. 7658 * Set "varname" to the start of name without ':'. 7659 */ 7660 hashtab_T * 7661 find_var_ht(char_u *name, char_u **varname) 7662 { 7663 hashitem_T *hi; 7664 hashtab_T *ht; 7665 7666 if (name[0] == NUL) 7667 return NULL; 7668 if (name[1] != ':') 7669 { 7670 /* The name must not start with a colon or #. */ 7671 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR) 7672 return NULL; 7673 *varname = name; 7674 7675 // "version" is "v:version" in all scopes if scriptversion < 3. 7676 // Same for a few other variables marked with VV_COMPAT. 7677 if (current_sctx.sc_version < 3) 7678 { 7679 hi = hash_find(&compat_hashtab, name); 7680 if (!HASHITEM_EMPTY(hi)) 7681 return &compat_hashtab; 7682 } 7683 7684 ht = get_funccal_local_ht(); 7685 if (ht == NULL) 7686 return &globvarht; /* global variable */ 7687 return ht; /* local variable */ 7688 } 7689 *varname = name + 2; 7690 if (*name == 'g') /* global variable */ 7691 return &globvarht; 7692 /* There must be no ':' or '#' in the rest of the name, unless g: is used 7693 */ 7694 if (vim_strchr(name + 2, ':') != NULL 7695 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL) 7696 return NULL; 7697 if (*name == 'b') /* buffer variable */ 7698 return &curbuf->b_vars->dv_hashtab; 7699 if (*name == 'w') /* window variable */ 7700 return &curwin->w_vars->dv_hashtab; 7701 if (*name == 't') /* tab page variable */ 7702 return &curtab->tp_vars->dv_hashtab; 7703 if (*name == 'v') /* v: variable */ 7704 return &vimvarht; 7705 if (*name == 'a') /* a: function argument */ 7706 return get_funccal_args_ht(); 7707 if (*name == 'l') /* l: local function variable */ 7708 return get_funccal_local_ht(); 7709 if (*name == 's' /* script variable */ 7710 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len) 7711 return &SCRIPT_VARS(current_sctx.sc_sid); 7712 return NULL; 7713 } 7714 7715 /* 7716 * Get the string value of a (global/local) variable. 7717 * Note: see tv_get_string() for how long the pointer remains valid. 7718 * Returns NULL when it doesn't exist. 7719 */ 7720 char_u * 7721 get_var_value(char_u *name) 7722 { 7723 dictitem_T *v; 7724 7725 v = find_var(name, NULL, FALSE); 7726 if (v == NULL) 7727 return NULL; 7728 return tv_get_string(&v->di_tv); 7729 } 7730 7731 /* 7732 * Allocate a new hashtab for a sourced script. It will be used while 7733 * sourcing this script and when executing functions defined in the script. 7734 */ 7735 void 7736 new_script_vars(scid_T id) 7737 { 7738 int i; 7739 hashtab_T *ht; 7740 scriptvar_T *sv; 7741 7742 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK) 7743 { 7744 /* Re-allocating ga_data means that an ht_array pointing to 7745 * ht_smallarray becomes invalid. We can recognize this: ht_mask is 7746 * at its init value. Also reset "v_dict", it's always the same. */ 7747 for (i = 1; i <= ga_scripts.ga_len; ++i) 7748 { 7749 ht = &SCRIPT_VARS(i); 7750 if (ht->ht_mask == HT_INIT_SIZE - 1) 7751 ht->ht_array = ht->ht_smallarray; 7752 sv = SCRIPT_SV(i); 7753 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict; 7754 } 7755 7756 while (ga_scripts.ga_len < id) 7757 { 7758 sv = SCRIPT_SV(ga_scripts.ga_len + 1) = 7759 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T)); 7760 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE); 7761 ++ga_scripts.ga_len; 7762 } 7763 } 7764 } 7765 7766 /* 7767 * Initialize dictionary "dict" as a scope and set variable "dict_var" to 7768 * point to it. 7769 */ 7770 void 7771 init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope) 7772 { 7773 hash_init(&dict->dv_hashtab); 7774 dict->dv_lock = 0; 7775 dict->dv_scope = scope; 7776 dict->dv_refcount = DO_NOT_FREE_CNT; 7777 dict->dv_copyID = 0; 7778 dict_var->di_tv.vval.v_dict = dict; 7779 dict_var->di_tv.v_type = VAR_DICT; 7780 dict_var->di_tv.v_lock = VAR_FIXED; 7781 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; 7782 dict_var->di_key[0] = NUL; 7783 } 7784 7785 /* 7786 * Unreference a dictionary initialized by init_var_dict(). 7787 */ 7788 void 7789 unref_var_dict(dict_T *dict) 7790 { 7791 /* Now the dict needs to be freed if no one else is using it, go back to 7792 * normal reference counting. */ 7793 dict->dv_refcount -= DO_NOT_FREE_CNT - 1; 7794 dict_unref(dict); 7795 } 7796 7797 /* 7798 * Clean up a list of internal variables. 7799 * Frees all allocated variables and the value they contain. 7800 * Clears hashtab "ht", does not free it. 7801 */ 7802 void 7803 vars_clear(hashtab_T *ht) 7804 { 7805 vars_clear_ext(ht, TRUE); 7806 } 7807 7808 /* 7809 * Like vars_clear(), but only free the value if "free_val" is TRUE. 7810 */ 7811 void 7812 vars_clear_ext(hashtab_T *ht, int free_val) 7813 { 7814 int todo; 7815 hashitem_T *hi; 7816 dictitem_T *v; 7817 7818 hash_lock(ht); 7819 todo = (int)ht->ht_used; 7820 for (hi = ht->ht_array; todo > 0; ++hi) 7821 { 7822 if (!HASHITEM_EMPTY(hi)) 7823 { 7824 --todo; 7825 7826 /* Free the variable. Don't remove it from the hashtab, 7827 * ht_array might change then. hash_clear() takes care of it 7828 * later. */ 7829 v = HI2DI(hi); 7830 if (free_val) 7831 clear_tv(&v->di_tv); 7832 if (v->di_flags & DI_FLAGS_ALLOC) 7833 vim_free(v); 7834 } 7835 } 7836 hash_clear(ht); 7837 ht->ht_used = 0; 7838 } 7839 7840 /* 7841 * Delete a variable from hashtab "ht" at item "hi". 7842 * Clear the variable value and free the dictitem. 7843 */ 7844 static void 7845 delete_var(hashtab_T *ht, hashitem_T *hi) 7846 { 7847 dictitem_T *di = HI2DI(hi); 7848 7849 hash_remove(ht, hi); 7850 clear_tv(&di->di_tv); 7851 vim_free(di); 7852 } 7853 7854 /* 7855 * List the value of one internal variable. 7856 */ 7857 static void 7858 list_one_var(dictitem_T *v, char *prefix, int *first) 7859 { 7860 char_u *tofree; 7861 char_u *s; 7862 char_u numbuf[NUMBUFLEN]; 7863 7864 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID()); 7865 list_one_var_a(prefix, v->di_key, v->di_tv.v_type, 7866 s == NULL ? (char_u *)"" : s, first); 7867 vim_free(tofree); 7868 } 7869 7870 static void 7871 list_one_var_a( 7872 char *prefix, 7873 char_u *name, 7874 int type, 7875 char_u *string, 7876 int *first) /* when TRUE clear rest of screen and set to FALSE */ 7877 { 7878 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */ 7879 msg_start(); 7880 msg_puts(prefix); 7881 if (name != NULL) /* "a:" vars don't have a name stored */ 7882 msg_puts((char *)name); 7883 msg_putchar(' '); 7884 msg_advance(22); 7885 if (type == VAR_NUMBER) 7886 msg_putchar('#'); 7887 else if (type == VAR_FUNC || type == VAR_PARTIAL) 7888 msg_putchar('*'); 7889 else if (type == VAR_LIST) 7890 { 7891 msg_putchar('['); 7892 if (*string == '[') 7893 ++string; 7894 } 7895 else if (type == VAR_DICT) 7896 { 7897 msg_putchar('{'); 7898 if (*string == '{') 7899 ++string; 7900 } 7901 else 7902 msg_putchar(' '); 7903 7904 msg_outtrans(string); 7905 7906 if (type == VAR_FUNC || type == VAR_PARTIAL) 7907 msg_puts("()"); 7908 if (*first) 7909 { 7910 msg_clr_eos(); 7911 *first = FALSE; 7912 } 7913 } 7914 7915 /* 7916 * Set variable "name" to value in "tv". 7917 * If the variable already exists, the value is updated. 7918 * Otherwise the variable is created. 7919 */ 7920 void 7921 set_var( 7922 char_u *name, 7923 typval_T *tv, 7924 int copy) /* make copy of value in "tv" */ 7925 { 7926 dictitem_T *v; 7927 char_u *varname; 7928 hashtab_T *ht; 7929 7930 ht = find_var_ht(name, &varname); 7931 if (ht == NULL || *varname == NUL) 7932 { 7933 semsg(_(e_illvar), name); 7934 return; 7935 } 7936 v = find_var_in_ht(ht, 0, varname, TRUE); 7937 7938 /* Search in parent scope which is possible to reference from lambda */ 7939 if (v == NULL) 7940 v = find_var_in_scoped_ht(name, TRUE); 7941 7942 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL) 7943 && var_check_func_name(name, v == NULL)) 7944 return; 7945 7946 if (v != NULL) 7947 { 7948 /* existing variable, need to clear the value */ 7949 if (var_check_ro(v->di_flags, name, FALSE) 7950 || var_check_lock(v->di_tv.v_lock, name, FALSE)) 7951 return; 7952 7953 /* 7954 * Handle setting internal v: variables separately where needed to 7955 * prevent changing the type. 7956 */ 7957 if (ht == &vimvarht) 7958 { 7959 if (v->di_tv.v_type == VAR_STRING) 7960 { 7961 VIM_CLEAR(v->di_tv.vval.v_string); 7962 if (copy || tv->v_type != VAR_STRING) 7963 { 7964 char_u *val = tv_get_string(tv); 7965 7966 // Careful: when assigning to v:errmsg and tv_get_string() 7967 // causes an error message the variable will alrady be set. 7968 if (v->di_tv.vval.v_string == NULL) 7969 v->di_tv.vval.v_string = vim_strsave(val); 7970 } 7971 else 7972 { 7973 /* Take over the string to avoid an extra alloc/free. */ 7974 v->di_tv.vval.v_string = tv->vval.v_string; 7975 tv->vval.v_string = NULL; 7976 } 7977 return; 7978 } 7979 else if (v->di_tv.v_type == VAR_NUMBER) 7980 { 7981 v->di_tv.vval.v_number = tv_get_number(tv); 7982 if (STRCMP(varname, "searchforward") == 0) 7983 set_search_direction(v->di_tv.vval.v_number ? '/' : '?'); 7984 #ifdef FEAT_SEARCH_EXTRA 7985 else if (STRCMP(varname, "hlsearch") == 0) 7986 { 7987 no_hlsearch = !v->di_tv.vval.v_number; 7988 redraw_all_later(SOME_VALID); 7989 } 7990 #endif 7991 return; 7992 } 7993 else if (v->di_tv.v_type != tv->v_type) 7994 { 7995 semsg(_("E963: setting %s to value with wrong type"), name); 7996 return; 7997 } 7998 } 7999 8000 clear_tv(&v->di_tv); 8001 } 8002 else /* add a new variable */ 8003 { 8004 // Can't add "v:" or "a:" variable. 8005 if (ht == &vimvarht || ht == get_funccal_args_ht()) 8006 { 8007 semsg(_(e_illvar), name); 8008 return; 8009 } 8010 8011 // Make sure the variable name is valid. 8012 if (!valid_varname(varname)) 8013 return; 8014 8015 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) 8016 + STRLEN(varname))); 8017 if (v == NULL) 8018 return; 8019 STRCPY(v->di_key, varname); 8020 if (hash_add(ht, DI2HIKEY(v)) == FAIL) 8021 { 8022 vim_free(v); 8023 return; 8024 } 8025 v->di_flags = DI_FLAGS_ALLOC; 8026 } 8027 8028 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT) 8029 copy_tv(tv, &v->di_tv); 8030 else 8031 { 8032 v->di_tv = *tv; 8033 v->di_tv.v_lock = 0; 8034 init_tv(tv); 8035 } 8036 } 8037 8038 /* 8039 * Return TRUE if di_flags "flags" indicates variable "name" is read-only. 8040 * Also give an error message. 8041 */ 8042 int 8043 var_check_ro(int flags, char_u *name, int use_gettext) 8044 { 8045 if (flags & DI_FLAGS_RO) 8046 { 8047 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name); 8048 return TRUE; 8049 } 8050 if ((flags & DI_FLAGS_RO_SBX) && sandbox) 8051 { 8052 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name); 8053 return TRUE; 8054 } 8055 return FALSE; 8056 } 8057 8058 /* 8059 * Return TRUE if di_flags "flags" indicates variable "name" is fixed. 8060 * Also give an error message. 8061 */ 8062 int 8063 var_check_fixed(int flags, char_u *name, int use_gettext) 8064 { 8065 if (flags & DI_FLAGS_FIX) 8066 { 8067 semsg(_("E795: Cannot delete variable %s"), 8068 use_gettext ? (char_u *)_(name) : name); 8069 return TRUE; 8070 } 8071 return FALSE; 8072 } 8073 8074 /* 8075 * Check if a funcref is assigned to a valid variable name. 8076 * Return TRUE and give an error if not. 8077 */ 8078 int 8079 var_check_func_name( 8080 char_u *name, /* points to start of variable name */ 8081 int new_var) /* TRUE when creating the variable */ 8082 { 8083 /* Allow for w: b: s: and t:. */ 8084 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':') 8085 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':') 8086 ? name[2] : name[0])) 8087 { 8088 semsg(_("E704: Funcref variable name must start with a capital: %s"), 8089 name); 8090 return TRUE; 8091 } 8092 /* Don't allow hiding a function. When "v" is not NULL we might be 8093 * assigning another function to the same var, the type is checked 8094 * below. */ 8095 if (new_var && function_exists(name, FALSE)) 8096 { 8097 semsg(_("E705: Variable name conflicts with existing function: %s"), 8098 name); 8099 return TRUE; 8100 } 8101 return FALSE; 8102 } 8103 8104 /* 8105 * Return TRUE if "flags" indicates variable "name" is locked (immutable). 8106 * Also give an error message, using "name" or _("name") when use_gettext is 8107 * TRUE. 8108 */ 8109 int 8110 var_check_lock(int lock, char_u *name, int use_gettext) 8111 { 8112 if (lock & VAR_LOCKED) 8113 { 8114 semsg(_("E741: Value is locked: %s"), 8115 name == NULL ? (char_u *)_("Unknown") 8116 : use_gettext ? (char_u *)_(name) 8117 : name); 8118 return TRUE; 8119 } 8120 if (lock & VAR_FIXED) 8121 { 8122 semsg(_("E742: Cannot change value of %s"), 8123 name == NULL ? (char_u *)_("Unknown") 8124 : use_gettext ? (char_u *)_(name) 8125 : name); 8126 return TRUE; 8127 } 8128 return FALSE; 8129 } 8130 8131 /* 8132 * Return TRUE if typeval "tv" and its value are set to be locked (immutable). 8133 * Also give an error message, using "name" or _("name") when use_gettext is 8134 * TRUE. 8135 */ 8136 static int 8137 tv_check_lock(typval_T *tv, char_u *name, int use_gettext) 8138 { 8139 int lock = 0; 8140 8141 switch (tv->v_type) 8142 { 8143 case VAR_BLOB: 8144 if (tv->vval.v_blob != NULL) 8145 lock = tv->vval.v_blob->bv_lock; 8146 break; 8147 case VAR_LIST: 8148 if (tv->vval.v_list != NULL) 8149 lock = tv->vval.v_list->lv_lock; 8150 break; 8151 case VAR_DICT: 8152 if (tv->vval.v_dict != NULL) 8153 lock = tv->vval.v_dict->dv_lock; 8154 break; 8155 default: 8156 break; 8157 } 8158 return var_check_lock(tv->v_lock, name, use_gettext) 8159 || (lock != 0 && var_check_lock(lock, name, use_gettext)); 8160 } 8161 8162 /* 8163 * Check if a variable name is valid. 8164 * Return FALSE and give an error if not. 8165 */ 8166 int 8167 valid_varname(char_u *varname) 8168 { 8169 char_u *p; 8170 8171 for (p = varname; *p != NUL; ++p) 8172 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p)) 8173 && *p != AUTOLOAD_CHAR) 8174 { 8175 semsg(_(e_illvar), varname); 8176 return FALSE; 8177 } 8178 return TRUE; 8179 } 8180 8181 /* 8182 * Copy the values from typval_T "from" to typval_T "to". 8183 * When needed allocates string or increases reference count. 8184 * Does not make a copy of a list, blob or dict but copies the reference! 8185 * It is OK for "from" and "to" to point to the same item. This is used to 8186 * make a copy later. 8187 */ 8188 void 8189 copy_tv(typval_T *from, typval_T *to) 8190 { 8191 to->v_type = from->v_type; 8192 to->v_lock = 0; 8193 switch (from->v_type) 8194 { 8195 case VAR_NUMBER: 8196 case VAR_SPECIAL: 8197 to->vval.v_number = from->vval.v_number; 8198 break; 8199 case VAR_FLOAT: 8200 #ifdef FEAT_FLOAT 8201 to->vval.v_float = from->vval.v_float; 8202 break; 8203 #endif 8204 case VAR_JOB: 8205 #ifdef FEAT_JOB_CHANNEL 8206 to->vval.v_job = from->vval.v_job; 8207 if (to->vval.v_job != NULL) 8208 ++to->vval.v_job->jv_refcount; 8209 break; 8210 #endif 8211 case VAR_CHANNEL: 8212 #ifdef FEAT_JOB_CHANNEL 8213 to->vval.v_channel = from->vval.v_channel; 8214 if (to->vval.v_channel != NULL) 8215 ++to->vval.v_channel->ch_refcount; 8216 break; 8217 #endif 8218 case VAR_STRING: 8219 case VAR_FUNC: 8220 if (from->vval.v_string == NULL) 8221 to->vval.v_string = NULL; 8222 else 8223 { 8224 to->vval.v_string = vim_strsave(from->vval.v_string); 8225 if (from->v_type == VAR_FUNC) 8226 func_ref(to->vval.v_string); 8227 } 8228 break; 8229 case VAR_PARTIAL: 8230 if (from->vval.v_partial == NULL) 8231 to->vval.v_partial = NULL; 8232 else 8233 { 8234 to->vval.v_partial = from->vval.v_partial; 8235 ++to->vval.v_partial->pt_refcount; 8236 } 8237 break; 8238 case VAR_BLOB: 8239 if (from->vval.v_blob == NULL) 8240 to->vval.v_blob = NULL; 8241 else 8242 { 8243 to->vval.v_blob = from->vval.v_blob; 8244 ++to->vval.v_blob->bv_refcount; 8245 } 8246 break; 8247 case VAR_LIST: 8248 if (from->vval.v_list == NULL) 8249 to->vval.v_list = NULL; 8250 else 8251 { 8252 to->vval.v_list = from->vval.v_list; 8253 ++to->vval.v_list->lv_refcount; 8254 } 8255 break; 8256 case VAR_DICT: 8257 if (from->vval.v_dict == NULL) 8258 to->vval.v_dict = NULL; 8259 else 8260 { 8261 to->vval.v_dict = from->vval.v_dict; 8262 ++to->vval.v_dict->dv_refcount; 8263 } 8264 break; 8265 case VAR_UNKNOWN: 8266 internal_error("copy_tv(UNKNOWN)"); 8267 break; 8268 } 8269 } 8270 8271 /* 8272 * Make a copy of an item. 8273 * Lists and Dictionaries are also copied. A deep copy if "deep" is set. 8274 * For deepcopy() "copyID" is zero for a full copy or the ID for when a 8275 * reference to an already copied list/dict can be used. 8276 * Returns FAIL or OK. 8277 */ 8278 int 8279 item_copy( 8280 typval_T *from, 8281 typval_T *to, 8282 int deep, 8283 int copyID) 8284 { 8285 static int recurse = 0; 8286 int ret = OK; 8287 8288 if (recurse >= DICT_MAXNEST) 8289 { 8290 emsg(_("E698: variable nested too deep for making a copy")); 8291 return FAIL; 8292 } 8293 ++recurse; 8294 8295 switch (from->v_type) 8296 { 8297 case VAR_NUMBER: 8298 case VAR_FLOAT: 8299 case VAR_STRING: 8300 case VAR_FUNC: 8301 case VAR_PARTIAL: 8302 case VAR_SPECIAL: 8303 case VAR_JOB: 8304 case VAR_CHANNEL: 8305 copy_tv(from, to); 8306 break; 8307 case VAR_LIST: 8308 to->v_type = VAR_LIST; 8309 to->v_lock = 0; 8310 if (from->vval.v_list == NULL) 8311 to->vval.v_list = NULL; 8312 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID) 8313 { 8314 /* use the copy made earlier */ 8315 to->vval.v_list = from->vval.v_list->lv_copylist; 8316 ++to->vval.v_list->lv_refcount; 8317 } 8318 else 8319 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID); 8320 if (to->vval.v_list == NULL) 8321 ret = FAIL; 8322 break; 8323 case VAR_BLOB: 8324 ret = blob_copy(from, to); 8325 break; 8326 case VAR_DICT: 8327 to->v_type = VAR_DICT; 8328 to->v_lock = 0; 8329 if (from->vval.v_dict == NULL) 8330 to->vval.v_dict = NULL; 8331 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID) 8332 { 8333 /* use the copy made earlier */ 8334 to->vval.v_dict = from->vval.v_dict->dv_copydict; 8335 ++to->vval.v_dict->dv_refcount; 8336 } 8337 else 8338 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID); 8339 if (to->vval.v_dict == NULL) 8340 ret = FAIL; 8341 break; 8342 case VAR_UNKNOWN: 8343 internal_error("item_copy(UNKNOWN)"); 8344 ret = FAIL; 8345 } 8346 --recurse; 8347 return ret; 8348 } 8349 8350 /* 8351 * This function is used by f_input() and f_inputdialog() functions. The third 8352 * argument to f_input() specifies the type of completion to use at the 8353 * prompt. The third argument to f_inputdialog() specifies the value to return 8354 * when the user cancels the prompt. 8355 */ 8356 void 8357 get_user_input( 8358 typval_T *argvars, 8359 typval_T *rettv, 8360 int inputdialog, 8361 int secret) 8362 { 8363 char_u *prompt = tv_get_string_chk(&argvars[0]); 8364 char_u *p = NULL; 8365 int c; 8366 char_u buf[NUMBUFLEN]; 8367 int cmd_silent_save = cmd_silent; 8368 char_u *defstr = (char_u *)""; 8369 int xp_type = EXPAND_NOTHING; 8370 char_u *xp_arg = NULL; 8371 8372 rettv->v_type = VAR_STRING; 8373 rettv->vval.v_string = NULL; 8374 8375 #ifdef NO_CONSOLE_INPUT 8376 /* While starting up, there is no place to enter text. When running tests 8377 * with --not-a-term we assume feedkeys() will be used. */ 8378 if (no_console_input() && !is_not_a_term()) 8379 return; 8380 #endif 8381 8382 cmd_silent = FALSE; /* Want to see the prompt. */ 8383 if (prompt != NULL) 8384 { 8385 /* Only the part of the message after the last NL is considered as 8386 * prompt for the command line */ 8387 p = vim_strrchr(prompt, '\n'); 8388 if (p == NULL) 8389 p = prompt; 8390 else 8391 { 8392 ++p; 8393 c = *p; 8394 *p = NUL; 8395 msg_start(); 8396 msg_clr_eos(); 8397 msg_puts_attr((char *)prompt, echo_attr); 8398 msg_didout = FALSE; 8399 msg_starthere(); 8400 *p = c; 8401 } 8402 cmdline_row = msg_row; 8403 8404 if (argvars[1].v_type != VAR_UNKNOWN) 8405 { 8406 defstr = tv_get_string_buf_chk(&argvars[1], buf); 8407 if (defstr != NULL) 8408 stuffReadbuffSpec(defstr); 8409 8410 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN) 8411 { 8412 char_u *xp_name; 8413 int xp_namelen; 8414 long argt; 8415 8416 /* input() with a third argument: completion */ 8417 rettv->vval.v_string = NULL; 8418 8419 xp_name = tv_get_string_buf_chk(&argvars[2], buf); 8420 if (xp_name == NULL) 8421 return; 8422 8423 xp_namelen = (int)STRLEN(xp_name); 8424 8425 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt, 8426 &xp_arg) == FAIL) 8427 return; 8428 } 8429 } 8430 8431 if (defstr != NULL) 8432 { 8433 int save_ex_normal_busy = ex_normal_busy; 8434 8435 ex_normal_busy = 0; 8436 rettv->vval.v_string = 8437 getcmdline_prompt(secret ? NUL : '@', p, echo_attr, 8438 xp_type, xp_arg); 8439 ex_normal_busy = save_ex_normal_busy; 8440 } 8441 if (inputdialog && rettv->vval.v_string == NULL 8442 && argvars[1].v_type != VAR_UNKNOWN 8443 && argvars[2].v_type != VAR_UNKNOWN) 8444 rettv->vval.v_string = vim_strsave(tv_get_string_buf( 8445 &argvars[2], buf)); 8446 8447 vim_free(xp_arg); 8448 8449 /* since the user typed this, no need to wait for return */ 8450 need_wait_return = FALSE; 8451 msg_didout = FALSE; 8452 } 8453 cmd_silent = cmd_silent_save; 8454 } 8455 8456 /* 8457 * ":echo expr1 ..." print each argument separated with a space, add a 8458 * newline at the end. 8459 * ":echon expr1 ..." print each argument plain. 8460 */ 8461 void 8462 ex_echo(exarg_T *eap) 8463 { 8464 char_u *arg = eap->arg; 8465 typval_T rettv; 8466 char_u *tofree; 8467 char_u *p; 8468 int needclr = TRUE; 8469 int atstart = TRUE; 8470 char_u numbuf[NUMBUFLEN]; 8471 int did_emsg_before = did_emsg; 8472 int called_emsg_before = called_emsg; 8473 8474 if (eap->skip) 8475 ++emsg_skip; 8476 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int) 8477 { 8478 /* If eval1() causes an error message the text from the command may 8479 * still need to be cleared. E.g., "echo 22,44". */ 8480 need_clr_eos = needclr; 8481 8482 p = arg; 8483 if (eval1(&arg, &rettv, !eap->skip) == FAIL) 8484 { 8485 /* 8486 * Report the invalid expression unless the expression evaluation 8487 * has been cancelled due to an aborting error, an interrupt, or an 8488 * exception. 8489 */ 8490 if (!aborting() && did_emsg == did_emsg_before 8491 && called_emsg == called_emsg_before) 8492 semsg(_(e_invexpr2), p); 8493 need_clr_eos = FALSE; 8494 break; 8495 } 8496 need_clr_eos = FALSE; 8497 8498 if (!eap->skip) 8499 { 8500 if (atstart) 8501 { 8502 atstart = FALSE; 8503 /* Call msg_start() after eval1(), evaluating the expression 8504 * may cause a message to appear. */ 8505 if (eap->cmdidx == CMD_echo) 8506 { 8507 /* Mark the saved text as finishing the line, so that what 8508 * follows is displayed on a new line when scrolling back 8509 * at the more prompt. */ 8510 msg_sb_eol(); 8511 msg_start(); 8512 } 8513 } 8514 else if (eap->cmdidx == CMD_echo) 8515 msg_puts_attr(" ", echo_attr); 8516 p = echo_string(&rettv, &tofree, numbuf, get_copyID()); 8517 if (p != NULL) 8518 for ( ; *p != NUL && !got_int; ++p) 8519 { 8520 if (*p == '\n' || *p == '\r' || *p == TAB) 8521 { 8522 if (*p != TAB && needclr) 8523 { 8524 /* remove any text still there from the command */ 8525 msg_clr_eos(); 8526 needclr = FALSE; 8527 } 8528 msg_putchar_attr(*p, echo_attr); 8529 } 8530 else 8531 { 8532 if (has_mbyte) 8533 { 8534 int i = (*mb_ptr2len)(p); 8535 8536 (void)msg_outtrans_len_attr(p, i, echo_attr); 8537 p += i - 1; 8538 } 8539 else 8540 (void)msg_outtrans_len_attr(p, 1, echo_attr); 8541 } 8542 } 8543 vim_free(tofree); 8544 } 8545 clear_tv(&rettv); 8546 arg = skipwhite(arg); 8547 } 8548 eap->nextcmd = check_nextcmd(arg); 8549 8550 if (eap->skip) 8551 --emsg_skip; 8552 else 8553 { 8554 /* remove text that may still be there from the command */ 8555 if (needclr) 8556 msg_clr_eos(); 8557 if (eap->cmdidx == CMD_echo) 8558 msg_end(); 8559 } 8560 } 8561 8562 /* 8563 * ":echohl {name}". 8564 */ 8565 void 8566 ex_echohl(exarg_T *eap) 8567 { 8568 echo_attr = syn_name2attr(eap->arg); 8569 } 8570 8571 /* 8572 * ":execute expr1 ..." execute the result of an expression. 8573 * ":echomsg expr1 ..." Print a message 8574 * ":echoerr expr1 ..." Print an error 8575 * Each gets spaces around each argument and a newline at the end for 8576 * echo commands 8577 */ 8578 void 8579 ex_execute(exarg_T *eap) 8580 { 8581 char_u *arg = eap->arg; 8582 typval_T rettv; 8583 int ret = OK; 8584 char_u *p; 8585 garray_T ga; 8586 int len; 8587 int save_did_emsg; 8588 8589 ga_init2(&ga, 1, 80); 8590 8591 if (eap->skip) 8592 ++emsg_skip; 8593 while (*arg != NUL && *arg != '|' && *arg != '\n') 8594 { 8595 ret = eval1_emsg(&arg, &rettv, !eap->skip); 8596 if (ret == FAIL) 8597 break; 8598 8599 if (!eap->skip) 8600 { 8601 char_u buf[NUMBUFLEN]; 8602 8603 if (eap->cmdidx == CMD_execute) 8604 p = tv_get_string_buf(&rettv, buf); 8605 else 8606 p = tv_stringify(&rettv, buf); 8607 len = (int)STRLEN(p); 8608 if (ga_grow(&ga, len + 2) == FAIL) 8609 { 8610 clear_tv(&rettv); 8611 ret = FAIL; 8612 break; 8613 } 8614 if (ga.ga_len) 8615 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' '; 8616 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p); 8617 ga.ga_len += len; 8618 } 8619 8620 clear_tv(&rettv); 8621 arg = skipwhite(arg); 8622 } 8623 8624 if (ret != FAIL && ga.ga_data != NULL) 8625 { 8626 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr) 8627 { 8628 /* Mark the already saved text as finishing the line, so that what 8629 * follows is displayed on a new line when scrolling back at the 8630 * more prompt. */ 8631 msg_sb_eol(); 8632 } 8633 8634 if (eap->cmdidx == CMD_echomsg) 8635 { 8636 msg_attr(ga.ga_data, echo_attr); 8637 out_flush(); 8638 } 8639 else if (eap->cmdidx == CMD_echoerr) 8640 { 8641 /* We don't want to abort following commands, restore did_emsg. */ 8642 save_did_emsg = did_emsg; 8643 emsg(ga.ga_data); 8644 if (!force_abort) 8645 did_emsg = save_did_emsg; 8646 } 8647 else if (eap->cmdidx == CMD_execute) 8648 do_cmdline((char_u *)ga.ga_data, 8649 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE); 8650 } 8651 8652 ga_clear(&ga); 8653 8654 if (eap->skip) 8655 --emsg_skip; 8656 8657 eap->nextcmd = check_nextcmd(arg); 8658 } 8659 8660 /* 8661 * Find window specified by "vp" in tabpage "tp". 8662 */ 8663 win_T * 8664 find_win_by_nr( 8665 typval_T *vp, 8666 tabpage_T *tp) /* NULL for current tab page */ 8667 { 8668 win_T *wp; 8669 int nr = (int)tv_get_number_chk(vp, NULL); 8670 8671 if (nr < 0) 8672 return NULL; 8673 if (nr == 0) 8674 return curwin; 8675 8676 FOR_ALL_WINDOWS_IN_TAB(tp, wp) 8677 { 8678 if (nr >= LOWEST_WIN_ID) 8679 { 8680 if (wp->w_id == nr) 8681 return wp; 8682 } 8683 else if (--nr <= 0) 8684 break; 8685 } 8686 if (nr >= LOWEST_WIN_ID) 8687 return NULL; 8688 return wp; 8689 } 8690 8691 /* 8692 * Find a window: When using a Window ID in any tab page, when using a number 8693 * in the current tab page. 8694 */ 8695 win_T * 8696 find_win_by_nr_or_id(typval_T *vp) 8697 { 8698 int nr = (int)tv_get_number_chk(vp, NULL); 8699 8700 if (nr >= LOWEST_WIN_ID) 8701 return win_id2wp(tv_get_number(vp)); 8702 return find_win_by_nr(vp, NULL); 8703 } 8704 8705 /* 8706 * Find window specified by "wvp" in tabpage "tvp". 8707 * Returns the tab page in 'ptp' 8708 */ 8709 win_T * 8710 find_tabwin( 8711 typval_T *wvp, // VAR_UNKNOWN for current window 8712 typval_T *tvp, // VAR_UNKNOWN for current tab page 8713 tabpage_T **ptp) 8714 { 8715 win_T *wp = NULL; 8716 tabpage_T *tp = NULL; 8717 long n; 8718 8719 if (wvp->v_type != VAR_UNKNOWN) 8720 { 8721 if (tvp->v_type != VAR_UNKNOWN) 8722 { 8723 n = (long)tv_get_number(tvp); 8724 if (n >= 0) 8725 tp = find_tabpage(n); 8726 } 8727 else 8728 tp = curtab; 8729 8730 if (tp != NULL) 8731 { 8732 wp = find_win_by_nr(wvp, tp); 8733 if (wp == NULL && wvp->v_type == VAR_NUMBER 8734 && wvp->vval.v_number != -1) 8735 // A window with the specified number is not found 8736 tp = NULL; 8737 } 8738 } 8739 else 8740 { 8741 wp = curwin; 8742 tp = curtab; 8743 } 8744 8745 if (ptp != NULL) 8746 *ptp = tp; 8747 8748 return wp; 8749 } 8750 8751 /* 8752 * getwinvar() and gettabwinvar() 8753 */ 8754 void 8755 getwinvar( 8756 typval_T *argvars, 8757 typval_T *rettv, 8758 int off) /* 1 for gettabwinvar() */ 8759 { 8760 win_T *win; 8761 char_u *varname; 8762 dictitem_T *v; 8763 tabpage_T *tp = NULL; 8764 int done = FALSE; 8765 win_T *oldcurwin; 8766 tabpage_T *oldtabpage; 8767 int need_switch_win; 8768 8769 if (off == 1) 8770 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL)); 8771 else 8772 tp = curtab; 8773 win = find_win_by_nr(&argvars[off], tp); 8774 varname = tv_get_string_chk(&argvars[off + 1]); 8775 ++emsg_off; 8776 8777 rettv->v_type = VAR_STRING; 8778 rettv->vval.v_string = NULL; 8779 8780 if (win != NULL && varname != NULL) 8781 { 8782 /* Set curwin to be our win, temporarily. Also set the tabpage, 8783 * otherwise the window is not valid. Only do this when needed, 8784 * autocommands get blocked. */ 8785 need_switch_win = !(tp == curtab && win == curwin); 8786 if (!need_switch_win 8787 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK) 8788 { 8789 if (*varname == '&') 8790 { 8791 if (varname[1] == NUL) 8792 { 8793 /* get all window-local options in a dict */ 8794 dict_T *opts = get_winbuf_options(FALSE); 8795 8796 if (opts != NULL) 8797 { 8798 rettv_dict_set(rettv, opts); 8799 done = TRUE; 8800 } 8801 } 8802 else if (get_option_tv(&varname, rettv, 1) == OK) 8803 /* window-local-option */ 8804 done = TRUE; 8805 } 8806 else 8807 { 8808 /* Look up the variable. */ 8809 /* Let getwinvar({nr}, "") return the "w:" dictionary. */ 8810 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', 8811 varname, FALSE); 8812 if (v != NULL) 8813 { 8814 copy_tv(&v->di_tv, rettv); 8815 done = TRUE; 8816 } 8817 } 8818 } 8819 8820 if (need_switch_win) 8821 /* restore previous notion of curwin */ 8822 restore_win(oldcurwin, oldtabpage, TRUE); 8823 } 8824 8825 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN) 8826 /* use the default return value */ 8827 copy_tv(&argvars[off + 2], rettv); 8828 8829 --emsg_off; 8830 } 8831 8832 /* 8833 * "setwinvar()" and "settabwinvar()" functions 8834 */ 8835 void 8836 setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off) 8837 { 8838 win_T *win; 8839 win_T *save_curwin; 8840 tabpage_T *save_curtab; 8841 int need_switch_win; 8842 char_u *varname, *winvarname; 8843 typval_T *varp; 8844 char_u nbuf[NUMBUFLEN]; 8845 tabpage_T *tp = NULL; 8846 8847 if (check_secure()) 8848 return; 8849 8850 if (off == 1) 8851 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL)); 8852 else 8853 tp = curtab; 8854 win = find_win_by_nr(&argvars[off], tp); 8855 varname = tv_get_string_chk(&argvars[off + 1]); 8856 varp = &argvars[off + 2]; 8857 8858 if (win != NULL && varname != NULL && varp != NULL) 8859 { 8860 need_switch_win = !(tp == curtab && win == curwin); 8861 if (!need_switch_win 8862 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK) 8863 { 8864 if (*varname == '&') 8865 { 8866 long numval; 8867 char_u *strval; 8868 int error = FALSE; 8869 8870 ++varname; 8871 numval = (long)tv_get_number_chk(varp, &error); 8872 strval = tv_get_string_buf_chk(varp, nbuf); 8873 if (!error && strval != NULL) 8874 set_option_value(varname, numval, strval, OPT_LOCAL); 8875 } 8876 else 8877 { 8878 winvarname = alloc((unsigned)STRLEN(varname) + 3); 8879 if (winvarname != NULL) 8880 { 8881 STRCPY(winvarname, "w:"); 8882 STRCPY(winvarname + 2, varname); 8883 set_var(winvarname, varp, TRUE); 8884 vim_free(winvarname); 8885 } 8886 } 8887 } 8888 if (need_switch_win) 8889 restore_win(save_curwin, save_curtab, TRUE); 8890 } 8891 } 8892 8893 /* 8894 * Skip over the name of an option: "&option", "&g:option" or "&l:option". 8895 * "arg" points to the "&" or '+' when called, to "option" when returning. 8896 * Returns NULL when no option name found. Otherwise pointer to the char 8897 * after the option name. 8898 */ 8899 static char_u * 8900 find_option_end(char_u **arg, int *opt_flags) 8901 { 8902 char_u *p = *arg; 8903 8904 ++p; 8905 if (*p == 'g' && p[1] == ':') 8906 { 8907 *opt_flags = OPT_GLOBAL; 8908 p += 2; 8909 } 8910 else if (*p == 'l' && p[1] == ':') 8911 { 8912 *opt_flags = OPT_LOCAL; 8913 p += 2; 8914 } 8915 else 8916 *opt_flags = 0; 8917 8918 if (!ASCII_ISALPHA(*p)) 8919 return NULL; 8920 *arg = p; 8921 8922 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL) 8923 p += 4; /* termcap option */ 8924 else 8925 while (ASCII_ISALPHA(*p)) 8926 ++p; 8927 return p; 8928 } 8929 8930 /* 8931 * Return the autoload script name for a function or variable name. 8932 * Returns NULL when out of memory. 8933 */ 8934 char_u * 8935 autoload_name(char_u *name) 8936 { 8937 char_u *p; 8938 char_u *scriptname; 8939 8940 /* Get the script file name: replace '#' with '/', append ".vim". */ 8941 scriptname = alloc((unsigned)(STRLEN(name) + 14)); 8942 if (scriptname == NULL) 8943 return FALSE; 8944 STRCPY(scriptname, "autoload/"); 8945 STRCAT(scriptname, name); 8946 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL; 8947 STRCAT(scriptname, ".vim"); 8948 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL) 8949 *p = '/'; 8950 return scriptname; 8951 } 8952 8953 /* 8954 * If "name" has a package name try autoloading the script for it. 8955 * Return TRUE if a package was loaded. 8956 */ 8957 int 8958 script_autoload( 8959 char_u *name, 8960 int reload) /* load script again when already loaded */ 8961 { 8962 char_u *p; 8963 char_u *scriptname, *tofree; 8964 int ret = FALSE; 8965 int i; 8966 8967 /* If there is no '#' after name[0] there is no package name. */ 8968 p = vim_strchr(name, AUTOLOAD_CHAR); 8969 if (p == NULL || p == name) 8970 return FALSE; 8971 8972 tofree = scriptname = autoload_name(name); 8973 8974 /* Find the name in the list of previously loaded package names. Skip 8975 * "autoload/", it's always the same. */ 8976 for (i = 0; i < ga_loaded.ga_len; ++i) 8977 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0) 8978 break; 8979 if (!reload && i < ga_loaded.ga_len) 8980 ret = FALSE; /* was loaded already */ 8981 else 8982 { 8983 /* Remember the name if it wasn't loaded already. */ 8984 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK) 8985 { 8986 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname; 8987 tofree = NULL; 8988 } 8989 8990 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */ 8991 if (source_runtime(scriptname, 0) == OK) 8992 ret = TRUE; 8993 } 8994 8995 vim_free(tofree); 8996 return ret; 8997 } 8998 8999 #if defined(FEAT_VIMINFO) || defined(FEAT_SESSION) 9000 typedef enum 9001 { 9002 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */ 9003 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */ 9004 VAR_FLAVOUR_VIMINFO /* all uppercase */ 9005 } var_flavour_T; 9006 9007 static var_flavour_T 9008 var_flavour(char_u *varname) 9009 { 9010 char_u *p = varname; 9011 9012 if (ASCII_ISUPPER(*p)) 9013 { 9014 while (*(++p)) 9015 if (ASCII_ISLOWER(*p)) 9016 return VAR_FLAVOUR_SESSION; 9017 return VAR_FLAVOUR_VIMINFO; 9018 } 9019 else 9020 return VAR_FLAVOUR_DEFAULT; 9021 } 9022 #endif 9023 9024 #if defined(FEAT_VIMINFO) || defined(PROTO) 9025 /* 9026 * Restore global vars that start with a capital from the viminfo file 9027 */ 9028 int 9029 read_viminfo_varlist(vir_T *virp, int writing) 9030 { 9031 char_u *tab; 9032 int type = VAR_NUMBER; 9033 typval_T tv; 9034 funccal_entry_T funccal_entry; 9035 9036 if (!writing && (find_viminfo_parameter('!') != NULL)) 9037 { 9038 tab = vim_strchr(virp->vir_line + 1, '\t'); 9039 if (tab != NULL) 9040 { 9041 *tab++ = '\0'; /* isolate the variable name */ 9042 switch (*tab) 9043 { 9044 case 'S': type = VAR_STRING; break; 9045 #ifdef FEAT_FLOAT 9046 case 'F': type = VAR_FLOAT; break; 9047 #endif 9048 case 'D': type = VAR_DICT; break; 9049 case 'L': type = VAR_LIST; break; 9050 case 'B': type = VAR_BLOB; break; 9051 case 'X': type = VAR_SPECIAL; break; 9052 } 9053 9054 tab = vim_strchr(tab, '\t'); 9055 if (tab != NULL) 9056 { 9057 tv.v_type = type; 9058 if (type == VAR_STRING || type == VAR_DICT 9059 || type == VAR_LIST || type == VAR_BLOB) 9060 tv.vval.v_string = viminfo_readstring(virp, 9061 (int)(tab - virp->vir_line + 1), TRUE); 9062 #ifdef FEAT_FLOAT 9063 else if (type == VAR_FLOAT) 9064 (void)string2float(tab + 1, &tv.vval.v_float); 9065 #endif 9066 else 9067 tv.vval.v_number = atol((char *)tab + 1); 9068 if (type == VAR_DICT || type == VAR_LIST) 9069 { 9070 typval_T *etv = eval_expr(tv.vval.v_string, NULL); 9071 9072 if (etv == NULL) 9073 /* Failed to parse back the dict or list, use it as a 9074 * string. */ 9075 tv.v_type = VAR_STRING; 9076 else 9077 { 9078 vim_free(tv.vval.v_string); 9079 tv = *etv; 9080 vim_free(etv); 9081 } 9082 } 9083 else if (type == VAR_BLOB) 9084 { 9085 blob_T *blob = string2blob(tv.vval.v_string); 9086 9087 if (blob == NULL) 9088 // Failed to parse back the blob, use it as a string. 9089 tv.v_type = VAR_STRING; 9090 else 9091 { 9092 vim_free(tv.vval.v_string); 9093 tv.v_type = VAR_BLOB; 9094 tv.vval.v_blob = blob; 9095 } 9096 } 9097 9098 /* when in a function use global variables */ 9099 save_funccal(&funccal_entry); 9100 set_var(virp->vir_line + 1, &tv, FALSE); 9101 restore_funccal(); 9102 9103 if (tv.v_type == VAR_STRING) 9104 vim_free(tv.vval.v_string); 9105 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST || 9106 tv.v_type == VAR_BLOB) 9107 clear_tv(&tv); 9108 } 9109 } 9110 } 9111 9112 return viminfo_readline(virp); 9113 } 9114 9115 /* 9116 * Write global vars that start with a capital to the viminfo file 9117 */ 9118 void 9119 write_viminfo_varlist(FILE *fp) 9120 { 9121 hashitem_T *hi; 9122 dictitem_T *this_var; 9123 int todo; 9124 char *s = ""; 9125 char_u *p; 9126 char_u *tofree; 9127 char_u numbuf[NUMBUFLEN]; 9128 9129 if (find_viminfo_parameter('!') == NULL) 9130 return; 9131 9132 fputs(_("\n# global variables:\n"), fp); 9133 9134 todo = (int)globvarht.ht_used; 9135 for (hi = globvarht.ht_array; todo > 0; ++hi) 9136 { 9137 if (!HASHITEM_EMPTY(hi)) 9138 { 9139 --todo; 9140 this_var = HI2DI(hi); 9141 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO) 9142 { 9143 switch (this_var->di_tv.v_type) 9144 { 9145 case VAR_STRING: s = "STR"; break; 9146 case VAR_NUMBER: s = "NUM"; break; 9147 case VAR_FLOAT: s = "FLO"; break; 9148 case VAR_DICT: s = "DIC"; break; 9149 case VAR_LIST: s = "LIS"; break; 9150 case VAR_BLOB: s = "BLO"; break; 9151 case VAR_SPECIAL: s = "XPL"; break; 9152 9153 case VAR_UNKNOWN: 9154 case VAR_FUNC: 9155 case VAR_PARTIAL: 9156 case VAR_JOB: 9157 case VAR_CHANNEL: 9158 continue; 9159 } 9160 fprintf(fp, "!%s\t%s\t", this_var->di_key, s); 9161 if (this_var->di_tv.v_type == VAR_SPECIAL) 9162 { 9163 sprintf((char *)numbuf, "%ld", 9164 (long)this_var->di_tv.vval.v_number); 9165 p = numbuf; 9166 tofree = NULL; 9167 } 9168 else 9169 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0); 9170 if (p != NULL) 9171 viminfo_writestring(fp, p); 9172 vim_free(tofree); 9173 } 9174 } 9175 } 9176 } 9177 #endif 9178 9179 #if defined(FEAT_SESSION) || defined(PROTO) 9180 int 9181 store_session_globals(FILE *fd) 9182 { 9183 hashitem_T *hi; 9184 dictitem_T *this_var; 9185 int todo; 9186 char_u *p, *t; 9187 9188 todo = (int)globvarht.ht_used; 9189 for (hi = globvarht.ht_array; todo > 0; ++hi) 9190 { 9191 if (!HASHITEM_EMPTY(hi)) 9192 { 9193 --todo; 9194 this_var = HI2DI(hi); 9195 if ((this_var->di_tv.v_type == VAR_NUMBER 9196 || this_var->di_tv.v_type == VAR_STRING) 9197 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION) 9198 { 9199 /* Escape special characters with a backslash. Turn a LF and 9200 * CR into \n and \r. */ 9201 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv), 9202 (char_u *)"\\\"\n\r"); 9203 if (p == NULL) /* out of memory */ 9204 break; 9205 for (t = p; *t != NUL; ++t) 9206 if (*t == '\n') 9207 *t = 'n'; 9208 else if (*t == '\r') 9209 *t = 'r'; 9210 if ((fprintf(fd, "let %s = %c%s%c", 9211 this_var->di_key, 9212 (this_var->di_tv.v_type == VAR_STRING) ? '"' 9213 : ' ', 9214 p, 9215 (this_var->di_tv.v_type == VAR_STRING) ? '"' 9216 : ' ') < 0) 9217 || put_eol(fd) == FAIL) 9218 { 9219 vim_free(p); 9220 return FAIL; 9221 } 9222 vim_free(p); 9223 } 9224 #ifdef FEAT_FLOAT 9225 else if (this_var->di_tv.v_type == VAR_FLOAT 9226 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION) 9227 { 9228 float_T f = this_var->di_tv.vval.v_float; 9229 int sign = ' '; 9230 9231 if (f < 0) 9232 { 9233 f = -f; 9234 sign = '-'; 9235 } 9236 if ((fprintf(fd, "let %s = %c%f", 9237 this_var->di_key, sign, f) < 0) 9238 || put_eol(fd) == FAIL) 9239 return FAIL; 9240 } 9241 #endif 9242 } 9243 } 9244 return OK; 9245 } 9246 #endif 9247 9248 /* 9249 * Display script name where an item was last set. 9250 * Should only be invoked when 'verbose' is non-zero. 9251 */ 9252 void 9253 last_set_msg(sctx_T script_ctx) 9254 { 9255 char_u *p; 9256 9257 if (script_ctx.sc_sid != 0) 9258 { 9259 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid)); 9260 if (p != NULL) 9261 { 9262 verbose_enter(); 9263 msg_puts(_("\n\tLast set from ")); 9264 msg_puts((char *)p); 9265 if (script_ctx.sc_lnum > 0) 9266 { 9267 msg_puts(_(" line ")); 9268 msg_outnum((long)script_ctx.sc_lnum); 9269 } 9270 verbose_leave(); 9271 vim_free(p); 9272 } 9273 } 9274 } 9275 9276 /* 9277 * Reset v:option_new, v:option_old and v:option_type. 9278 */ 9279 void 9280 reset_v_option_vars(void) 9281 { 9282 set_vim_var_string(VV_OPTION_NEW, NULL, -1); 9283 set_vim_var_string(VV_OPTION_OLD, NULL, -1); 9284 set_vim_var_string(VV_OPTION_TYPE, NULL, -1); 9285 } 9286 9287 /* 9288 * Prepare "gap" for an assert error and add the sourcing position. 9289 */ 9290 void 9291 prepare_assert_error(garray_T *gap) 9292 { 9293 char buf[NUMBUFLEN]; 9294 9295 ga_init2(gap, 1, 100); 9296 if (sourcing_name != NULL) 9297 { 9298 ga_concat(gap, sourcing_name); 9299 if (sourcing_lnum > 0) 9300 ga_concat(gap, (char_u *)" "); 9301 } 9302 if (sourcing_lnum > 0) 9303 { 9304 sprintf(buf, "line %ld", (long)sourcing_lnum); 9305 ga_concat(gap, (char_u *)buf); 9306 } 9307 if (sourcing_name != NULL || sourcing_lnum > 0) 9308 ga_concat(gap, (char_u *)": "); 9309 } 9310 9311 /* 9312 * Add an assert error to v:errors. 9313 */ 9314 void 9315 assert_error(garray_T *gap) 9316 { 9317 struct vimvar *vp = &vimvars[VV_ERRORS]; 9318 9319 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL) 9320 /* Make sure v:errors is a list. */ 9321 set_vim_var_list(VV_ERRORS, list_alloc()); 9322 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len); 9323 } 9324 9325 int 9326 assert_equal_common(typval_T *argvars, assert_type_T atype) 9327 { 9328 garray_T ga; 9329 9330 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE) 9331 != (atype == ASSERT_EQUAL)) 9332 { 9333 prepare_assert_error(&ga); 9334 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1], 9335 atype); 9336 assert_error(&ga); 9337 ga_clear(&ga); 9338 return 1; 9339 } 9340 return 0; 9341 } 9342 9343 int 9344 assert_equalfile(typval_T *argvars) 9345 { 9346 char_u buf1[NUMBUFLEN]; 9347 char_u buf2[NUMBUFLEN]; 9348 char_u *fname1 = tv_get_string_buf_chk(&argvars[0], buf1); 9349 char_u *fname2 = tv_get_string_buf_chk(&argvars[1], buf2); 9350 garray_T ga; 9351 FILE *fd1; 9352 FILE *fd2; 9353 9354 if (fname1 == NULL || fname2 == NULL) 9355 return 0; 9356 9357 IObuff[0] = NUL; 9358 fd1 = mch_fopen((char *)fname1, READBIN); 9359 if (fd1 == NULL) 9360 { 9361 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1); 9362 } 9363 else 9364 { 9365 fd2 = mch_fopen((char *)fname2, READBIN); 9366 if (fd2 == NULL) 9367 { 9368 fclose(fd1); 9369 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2); 9370 } 9371 else 9372 { 9373 int c1, c2; 9374 long count = 0; 9375 9376 for (;;) 9377 { 9378 c1 = fgetc(fd1); 9379 c2 = fgetc(fd2); 9380 if (c1 == EOF) 9381 { 9382 if (c2 != EOF) 9383 STRCPY(IObuff, "first file is shorter"); 9384 break; 9385 } 9386 else if (c2 == EOF) 9387 { 9388 STRCPY(IObuff, "second file is shorter"); 9389 break; 9390 } 9391 else if (c1 != c2) 9392 { 9393 vim_snprintf((char *)IObuff, IOSIZE, 9394 "difference at byte %ld", count); 9395 break; 9396 } 9397 ++count; 9398 } 9399 fclose(fd1); 9400 fclose(fd2); 9401 } 9402 } 9403 if (IObuff[0] != NUL) 9404 { 9405 prepare_assert_error(&ga); 9406 ga_concat(&ga, IObuff); 9407 assert_error(&ga); 9408 ga_clear(&ga); 9409 return 1; 9410 } 9411 return 0; 9412 } 9413 9414 int 9415 assert_match_common(typval_T *argvars, assert_type_T atype) 9416 { 9417 garray_T ga; 9418 char_u buf1[NUMBUFLEN]; 9419 char_u buf2[NUMBUFLEN]; 9420 char_u *pat = tv_get_string_buf_chk(&argvars[0], buf1); 9421 char_u *text = tv_get_string_buf_chk(&argvars[1], buf2); 9422 9423 if (pat == NULL || text == NULL) 9424 emsg(_(e_invarg)); 9425 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH)) 9426 { 9427 prepare_assert_error(&ga); 9428 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1], 9429 atype); 9430 assert_error(&ga); 9431 ga_clear(&ga); 9432 return 1; 9433 } 9434 return 0; 9435 } 9436 9437 int 9438 assert_inrange(typval_T *argvars) 9439 { 9440 garray_T ga; 9441 int error = FALSE; 9442 char_u *tofree; 9443 char msg[200]; 9444 char_u numbuf[NUMBUFLEN]; 9445 9446 #ifdef FEAT_FLOAT 9447 if (argvars[0].v_type == VAR_FLOAT 9448 || argvars[1].v_type == VAR_FLOAT 9449 || argvars[2].v_type == VAR_FLOAT) 9450 { 9451 float_T flower = tv_get_float(&argvars[0]); 9452 float_T fupper = tv_get_float(&argvars[1]); 9453 float_T factual = tv_get_float(&argvars[2]); 9454 9455 if (factual < flower || factual > fupper) 9456 { 9457 prepare_assert_error(&ga); 9458 if (argvars[3].v_type != VAR_UNKNOWN) 9459 { 9460 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0)); 9461 vim_free(tofree); 9462 } 9463 else 9464 { 9465 vim_snprintf(msg, 200, "Expected range %g - %g, but got %g", 9466 flower, fupper, factual); 9467 ga_concat(&ga, (char_u *)msg); 9468 } 9469 assert_error(&ga); 9470 ga_clear(&ga); 9471 return 1; 9472 } 9473 } 9474 else 9475 #endif 9476 { 9477 varnumber_T lower = tv_get_number_chk(&argvars[0], &error); 9478 varnumber_T upper = tv_get_number_chk(&argvars[1], &error); 9479 varnumber_T actual = tv_get_number_chk(&argvars[2], &error); 9480 9481 if (error) 9482 return 0; 9483 if (actual < lower || actual > upper) 9484 { 9485 prepare_assert_error(&ga); 9486 if (argvars[3].v_type != VAR_UNKNOWN) 9487 { 9488 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0)); 9489 vim_free(tofree); 9490 } 9491 else 9492 { 9493 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld", 9494 (long)lower, (long)upper, (long)actual); 9495 ga_concat(&ga, (char_u *)msg); 9496 } 9497 assert_error(&ga); 9498 ga_clear(&ga); 9499 return 1; 9500 } 9501 } 9502 return 0; 9503 } 9504 9505 /* 9506 * Common for assert_true() and assert_false(). 9507 * Return non-zero for failure. 9508 */ 9509 int 9510 assert_bool(typval_T *argvars, int isTrue) 9511 { 9512 int error = FALSE; 9513 garray_T ga; 9514 9515 if (argvars[0].v_type == VAR_SPECIAL 9516 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE)) 9517 return 0; 9518 if (argvars[0].v_type != VAR_NUMBER 9519 || (tv_get_number_chk(&argvars[0], &error) == 0) == isTrue 9520 || error) 9521 { 9522 prepare_assert_error(&ga); 9523 fill_assert_error(&ga, &argvars[1], 9524 (char_u *)(isTrue ? "True" : "False"), 9525 NULL, &argvars[0], ASSERT_OTHER); 9526 assert_error(&ga); 9527 ga_clear(&ga); 9528 return 1; 9529 } 9530 return 0; 9531 } 9532 9533 int 9534 assert_report(typval_T *argvars) 9535 { 9536 garray_T ga; 9537 9538 prepare_assert_error(&ga); 9539 ga_concat(&ga, tv_get_string(&argvars[0])); 9540 assert_error(&ga); 9541 ga_clear(&ga); 9542 return 1; 9543 } 9544 9545 int 9546 assert_exception(typval_T *argvars) 9547 { 9548 garray_T ga; 9549 char_u *error = tv_get_string_chk(&argvars[0]); 9550 9551 if (vimvars[VV_EXCEPTION].vv_str == NULL) 9552 { 9553 prepare_assert_error(&ga); 9554 ga_concat(&ga, (char_u *)"v:exception is not set"); 9555 assert_error(&ga); 9556 ga_clear(&ga); 9557 return 1; 9558 } 9559 else if (error != NULL 9560 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL) 9561 { 9562 prepare_assert_error(&ga); 9563 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0], 9564 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER); 9565 assert_error(&ga); 9566 ga_clear(&ga); 9567 return 1; 9568 } 9569 return 0; 9570 } 9571 9572 int 9573 assert_beeps(typval_T *argvars) 9574 { 9575 char_u *cmd = tv_get_string_chk(&argvars[0]); 9576 garray_T ga; 9577 int ret = 0; 9578 9579 called_vim_beep = FALSE; 9580 suppress_errthrow = TRUE; 9581 emsg_silent = FALSE; 9582 do_cmdline_cmd(cmd); 9583 if (!called_vim_beep) 9584 { 9585 prepare_assert_error(&ga); 9586 ga_concat(&ga, (char_u *)"command did not beep: "); 9587 ga_concat(&ga, cmd); 9588 assert_error(&ga); 9589 ga_clear(&ga); 9590 ret = 1; 9591 } 9592 9593 suppress_errthrow = FALSE; 9594 emsg_on_display = FALSE; 9595 return ret; 9596 } 9597 9598 static void 9599 assert_append_cmd_or_arg(garray_T *gap, typval_T *argvars, char_u *cmd) 9600 { 9601 char_u *tofree; 9602 char_u numbuf[NUMBUFLEN]; 9603 9604 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN) 9605 { 9606 ga_concat(gap, echo_string(&argvars[2], &tofree, numbuf, 0)); 9607 vim_free(tofree); 9608 } 9609 else 9610 ga_concat(gap, cmd); 9611 } 9612 9613 int 9614 assert_fails(typval_T *argvars) 9615 { 9616 char_u *cmd = tv_get_string_chk(&argvars[0]); 9617 garray_T ga; 9618 int ret = 0; 9619 9620 called_emsg = FALSE; 9621 suppress_errthrow = TRUE; 9622 emsg_silent = TRUE; 9623 do_cmdline_cmd(cmd); 9624 if (!called_emsg) 9625 { 9626 prepare_assert_error(&ga); 9627 ga_concat(&ga, (char_u *)"command did not fail: "); 9628 assert_append_cmd_or_arg(&ga, argvars, cmd); 9629 assert_error(&ga); 9630 ga_clear(&ga); 9631 ret = 1; 9632 } 9633 else if (argvars[1].v_type != VAR_UNKNOWN) 9634 { 9635 char_u buf[NUMBUFLEN]; 9636 char *error = (char *)tv_get_string_buf_chk(&argvars[1], buf); 9637 9638 if (error == NULL 9639 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL) 9640 { 9641 prepare_assert_error(&ga); 9642 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1], 9643 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER); 9644 ga_concat(&ga, (char_u *)": "); 9645 assert_append_cmd_or_arg(&ga, argvars, cmd); 9646 assert_error(&ga); 9647 ga_clear(&ga); 9648 ret = 1; 9649 } 9650 } 9651 9652 called_emsg = FALSE; 9653 suppress_errthrow = FALSE; 9654 emsg_silent = FALSE; 9655 emsg_on_display = FALSE; 9656 set_vim_var_string(VV_ERRMSG, NULL, 0); 9657 return ret; 9658 } 9659 9660 /* 9661 * Append "p[clen]" to "gap", escaping unprintable characters. 9662 * Changes NL to \n, CR to \r, etc. 9663 */ 9664 static void 9665 ga_concat_esc(garray_T *gap, char_u *p, int clen) 9666 { 9667 char_u buf[NUMBUFLEN]; 9668 9669 if (clen > 1) 9670 { 9671 mch_memmove(buf, p, clen); 9672 buf[clen] = NUL; 9673 ga_concat(gap, buf); 9674 } 9675 else switch (*p) 9676 { 9677 case BS: ga_concat(gap, (char_u *)"\\b"); break; 9678 case ESC: ga_concat(gap, (char_u *)"\\e"); break; 9679 case FF: ga_concat(gap, (char_u *)"\\f"); break; 9680 case NL: ga_concat(gap, (char_u *)"\\n"); break; 9681 case TAB: ga_concat(gap, (char_u *)"\\t"); break; 9682 case CAR: ga_concat(gap, (char_u *)"\\r"); break; 9683 case '\\': ga_concat(gap, (char_u *)"\\\\"); break; 9684 default: 9685 if (*p < ' ') 9686 { 9687 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p); 9688 ga_concat(gap, buf); 9689 } 9690 else 9691 ga_append(gap, *p); 9692 break; 9693 } 9694 } 9695 9696 /* 9697 * Append "str" to "gap", escaping unprintable characters. 9698 * Changes NL to \n, CR to \r, etc. 9699 */ 9700 static void 9701 ga_concat_shorten_esc(garray_T *gap, char_u *str) 9702 { 9703 char_u *p; 9704 char_u *s; 9705 int c; 9706 int clen; 9707 char_u buf[NUMBUFLEN]; 9708 int same_len; 9709 9710 if (str == NULL) 9711 { 9712 ga_concat(gap, (char_u *)"NULL"); 9713 return; 9714 } 9715 9716 for (p = str; *p != NUL; ++p) 9717 { 9718 same_len = 1; 9719 s = p; 9720 c = mb_ptr2char_adv(&s); 9721 clen = s - p; 9722 while (*s != NUL && c == mb_ptr2char(s)) 9723 { 9724 ++same_len; 9725 s += clen; 9726 } 9727 if (same_len > 20) 9728 { 9729 ga_concat(gap, (char_u *)"\\["); 9730 ga_concat_esc(gap, p, clen); 9731 ga_concat(gap, (char_u *)" occurs "); 9732 vim_snprintf((char *)buf, NUMBUFLEN, "%d", same_len); 9733 ga_concat(gap, buf); 9734 ga_concat(gap, (char_u *)" times]"); 9735 p = s - 1; 9736 } 9737 else 9738 ga_concat_esc(gap, p, clen); 9739 } 9740 } 9741 9742 /* 9743 * Fill "gap" with information about an assert error. 9744 */ 9745 void 9746 fill_assert_error( 9747 garray_T *gap, 9748 typval_T *opt_msg_tv, 9749 char_u *exp_str, 9750 typval_T *exp_tv, 9751 typval_T *got_tv, 9752 assert_type_T atype) 9753 { 9754 char_u numbuf[NUMBUFLEN]; 9755 char_u *tofree; 9756 9757 if (opt_msg_tv->v_type != VAR_UNKNOWN) 9758 { 9759 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0)); 9760 vim_free(tofree); 9761 ga_concat(gap, (char_u *)": "); 9762 } 9763 9764 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH) 9765 ga_concat(gap, (char_u *)"Pattern "); 9766 else if (atype == ASSERT_NOTEQUAL) 9767 ga_concat(gap, (char_u *)"Expected not equal to "); 9768 else 9769 ga_concat(gap, (char_u *)"Expected "); 9770 if (exp_str == NULL) 9771 { 9772 ga_concat_shorten_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0)); 9773 vim_free(tofree); 9774 } 9775 else 9776 ga_concat_shorten_esc(gap, exp_str); 9777 if (atype != ASSERT_NOTEQUAL) 9778 { 9779 if (atype == ASSERT_MATCH) 9780 ga_concat(gap, (char_u *)" does not match "); 9781 else if (atype == ASSERT_NOTMATCH) 9782 ga_concat(gap, (char_u *)" does match "); 9783 else 9784 ga_concat(gap, (char_u *)" but got "); 9785 ga_concat_shorten_esc(gap, tv2string(got_tv, &tofree, numbuf, 0)); 9786 vim_free(tofree); 9787 } 9788 } 9789 9790 /* 9791 * Compare "typ1" and "typ2". Put the result in "typ1". 9792 */ 9793 int 9794 typval_compare( 9795 typval_T *typ1, /* first operand */ 9796 typval_T *typ2, /* second operand */ 9797 exptype_T type, /* operator */ 9798 int type_is, /* TRUE for "is" and "isnot" */ 9799 int ic) /* ignore case */ 9800 { 9801 int i; 9802 varnumber_T n1, n2; 9803 char_u *s1, *s2; 9804 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN]; 9805 9806 if (type_is && typ1->v_type != typ2->v_type) 9807 { 9808 /* For "is" a different type always means FALSE, for "notis" 9809 * it means TRUE. */ 9810 n1 = (type == TYPE_NEQUAL); 9811 } 9812 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB) 9813 { 9814 if (type_is) 9815 { 9816 n1 = (typ1->v_type == typ2->v_type 9817 && typ1->vval.v_blob == typ2->vval.v_blob); 9818 if (type == TYPE_NEQUAL) 9819 n1 = !n1; 9820 } 9821 else if (typ1->v_type != typ2->v_type 9822 || (type != TYPE_EQUAL && type != TYPE_NEQUAL)) 9823 { 9824 if (typ1->v_type != typ2->v_type) 9825 emsg(_("E977: Can only compare Blob with Blob")); 9826 else 9827 emsg(_(e_invalblob)); 9828 clear_tv(typ1); 9829 return FAIL; 9830 } 9831 else 9832 { 9833 // Compare two Blobs for being equal or unequal. 9834 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob); 9835 if (type == TYPE_NEQUAL) 9836 n1 = !n1; 9837 } 9838 } 9839 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST) 9840 { 9841 if (type_is) 9842 { 9843 n1 = (typ1->v_type == typ2->v_type 9844 && typ1->vval.v_list == typ2->vval.v_list); 9845 if (type == TYPE_NEQUAL) 9846 n1 = !n1; 9847 } 9848 else if (typ1->v_type != typ2->v_type 9849 || (type != TYPE_EQUAL && type != TYPE_NEQUAL)) 9850 { 9851 if (typ1->v_type != typ2->v_type) 9852 emsg(_("E691: Can only compare List with List")); 9853 else 9854 emsg(_("E692: Invalid operation for List")); 9855 clear_tv(typ1); 9856 return FAIL; 9857 } 9858 else 9859 { 9860 /* Compare two Lists for being equal or unequal. */ 9861 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list, 9862 ic, FALSE); 9863 if (type == TYPE_NEQUAL) 9864 n1 = !n1; 9865 } 9866 } 9867 9868 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT) 9869 { 9870 if (type_is) 9871 { 9872 n1 = (typ1->v_type == typ2->v_type 9873 && typ1->vval.v_dict == typ2->vval.v_dict); 9874 if (type == TYPE_NEQUAL) 9875 n1 = !n1; 9876 } 9877 else if (typ1->v_type != typ2->v_type 9878 || (type != TYPE_EQUAL && type != TYPE_NEQUAL)) 9879 { 9880 if (typ1->v_type != typ2->v_type) 9881 emsg(_("E735: Can only compare Dictionary with Dictionary")); 9882 else 9883 emsg(_("E736: Invalid operation for Dictionary")); 9884 clear_tv(typ1); 9885 return FAIL; 9886 } 9887 else 9888 { 9889 /* Compare two Dictionaries for being equal or unequal. */ 9890 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict, 9891 ic, FALSE); 9892 if (type == TYPE_NEQUAL) 9893 n1 = !n1; 9894 } 9895 } 9896 9897 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC 9898 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL) 9899 { 9900 if (type != TYPE_EQUAL && type != TYPE_NEQUAL) 9901 { 9902 emsg(_("E694: Invalid operation for Funcrefs")); 9903 clear_tv(typ1); 9904 return FAIL; 9905 } 9906 if ((typ1->v_type == VAR_PARTIAL 9907 && typ1->vval.v_partial == NULL) 9908 || (typ2->v_type == VAR_PARTIAL 9909 && typ2->vval.v_partial == NULL)) 9910 /* when a partial is NULL assume not equal */ 9911 n1 = FALSE; 9912 else if (type_is) 9913 { 9914 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC) 9915 /* strings are considered the same if their value is 9916 * the same */ 9917 n1 = tv_equal(typ1, typ2, ic, FALSE); 9918 else if (typ1->v_type == VAR_PARTIAL 9919 && typ2->v_type == VAR_PARTIAL) 9920 n1 = (typ1->vval.v_partial == typ2->vval.v_partial); 9921 else 9922 n1 = FALSE; 9923 } 9924 else 9925 n1 = tv_equal(typ1, typ2, ic, FALSE); 9926 if (type == TYPE_NEQUAL) 9927 n1 = !n1; 9928 } 9929 9930 #ifdef FEAT_FLOAT 9931 /* 9932 * If one of the two variables is a float, compare as a float. 9933 * When using "=~" or "!~", always compare as string. 9934 */ 9935 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT) 9936 && type != TYPE_MATCH && type != TYPE_NOMATCH) 9937 { 9938 float_T f1, f2; 9939 9940 f1 = tv_get_float(typ1); 9941 f2 = tv_get_float(typ2); 9942 n1 = FALSE; 9943 switch (type) 9944 { 9945 case TYPE_EQUAL: n1 = (f1 == f2); break; 9946 case TYPE_NEQUAL: n1 = (f1 != f2); break; 9947 case TYPE_GREATER: n1 = (f1 > f2); break; 9948 case TYPE_GEQUAL: n1 = (f1 >= f2); break; 9949 case TYPE_SMALLER: n1 = (f1 < f2); break; 9950 case TYPE_SEQUAL: n1 = (f1 <= f2); break; 9951 case TYPE_UNKNOWN: 9952 case TYPE_MATCH: 9953 case TYPE_NOMATCH: break; /* avoid gcc warning */ 9954 } 9955 } 9956 #endif 9957 9958 /* 9959 * If one of the two variables is a number, compare as a number. 9960 * When using "=~" or "!~", always compare as string. 9961 */ 9962 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER) 9963 && type != TYPE_MATCH && type != TYPE_NOMATCH) 9964 { 9965 n1 = tv_get_number(typ1); 9966 n2 = tv_get_number(typ2); 9967 switch (type) 9968 { 9969 case TYPE_EQUAL: n1 = (n1 == n2); break; 9970 case TYPE_NEQUAL: n1 = (n1 != n2); break; 9971 case TYPE_GREATER: n1 = (n1 > n2); break; 9972 case TYPE_GEQUAL: n1 = (n1 >= n2); break; 9973 case TYPE_SMALLER: n1 = (n1 < n2); break; 9974 case TYPE_SEQUAL: n1 = (n1 <= n2); break; 9975 case TYPE_UNKNOWN: 9976 case TYPE_MATCH: 9977 case TYPE_NOMATCH: break; /* avoid gcc warning */ 9978 } 9979 } 9980 else 9981 { 9982 s1 = tv_get_string_buf(typ1, buf1); 9983 s2 = tv_get_string_buf(typ2, buf2); 9984 if (type != TYPE_MATCH && type != TYPE_NOMATCH) 9985 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2); 9986 else 9987 i = 0; 9988 n1 = FALSE; 9989 switch (type) 9990 { 9991 case TYPE_EQUAL: n1 = (i == 0); break; 9992 case TYPE_NEQUAL: n1 = (i != 0); break; 9993 case TYPE_GREATER: n1 = (i > 0); break; 9994 case TYPE_GEQUAL: n1 = (i >= 0); break; 9995 case TYPE_SMALLER: n1 = (i < 0); break; 9996 case TYPE_SEQUAL: n1 = (i <= 0); break; 9997 9998 case TYPE_MATCH: 9999 case TYPE_NOMATCH: 10000 n1 = pattern_match(s2, s1, ic); 10001 if (type == TYPE_NOMATCH) 10002 n1 = !n1; 10003 break; 10004 10005 case TYPE_UNKNOWN: break; /* avoid gcc warning */ 10006 } 10007 } 10008 clear_tv(typ1); 10009 typ1->v_type = VAR_NUMBER; 10010 typ1->vval.v_number = n1; 10011 10012 return OK; 10013 } 10014 10015 char_u * 10016 typval_tostring(typval_T *arg) 10017 { 10018 char_u *tofree; 10019 char_u numbuf[NUMBUFLEN]; 10020 char_u *ret = NULL; 10021 10022 if (arg == NULL) 10023 return vim_strsave((char_u *)"(does not exist)"); 10024 ret = tv2string(arg, &tofree, numbuf, 0); 10025 /* Make a copy if we have a value but it's not in allocated memory. */ 10026 if (ret != NULL && tofree == NULL) 10027 ret = vim_strsave(ret); 10028 return ret; 10029 } 10030 10031 int 10032 var_exists(char_u *var) 10033 { 10034 char_u *name; 10035 char_u *tofree; 10036 typval_T tv; 10037 int len = 0; 10038 int n = FALSE; 10039 10040 /* get_name_len() takes care of expanding curly braces */ 10041 name = var; 10042 len = get_name_len(&var, &tofree, TRUE, FALSE); 10043 if (len > 0) 10044 { 10045 if (tofree != NULL) 10046 name = tofree; 10047 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK); 10048 if (n) 10049 { 10050 /* handle d.key, l[idx], f(expr) */ 10051 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK); 10052 if (n) 10053 clear_tv(&tv); 10054 } 10055 } 10056 if (*var != NUL) 10057 n = FALSE; 10058 10059 vim_free(tofree); 10060 return n; 10061 } 10062 10063 #endif /* FEAT_EVAL */ 10064 10065 10066 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO) 10067 10068 #ifdef MSWIN 10069 /* 10070 * Functions for ":8" filename modifier: get 8.3 version of a filename. 10071 */ 10072 10073 /* 10074 * Get the short path (8.3) for the filename in "fnamep". 10075 * Only works for a valid file name. 10076 * When the path gets longer "fnamep" is changed and the allocated buffer 10077 * is put in "bufp". 10078 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path. 10079 * Returns OK on success, FAIL on failure. 10080 */ 10081 static int 10082 get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen) 10083 { 10084 int l, len; 10085 char_u *newbuf; 10086 10087 len = *fnamelen; 10088 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len); 10089 if (l > len - 1) 10090 { 10091 /* If that doesn't work (not enough space), then save the string 10092 * and try again with a new buffer big enough. */ 10093 newbuf = vim_strnsave(*fnamep, l); 10094 if (newbuf == NULL) 10095 return FAIL; 10096 10097 vim_free(*bufp); 10098 *fnamep = *bufp = newbuf; 10099 10100 /* Really should always succeed, as the buffer is big enough. */ 10101 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1); 10102 } 10103 10104 *fnamelen = l; 10105 return OK; 10106 } 10107 10108 /* 10109 * Get the short path (8.3) for the filename in "fname". The converted 10110 * path is returned in "bufp". 10111 * 10112 * Some of the directories specified in "fname" may not exist. This function 10113 * will shorten the existing directories at the beginning of the path and then 10114 * append the remaining non-existing path. 10115 * 10116 * fname - Pointer to the filename to shorten. On return, contains the 10117 * pointer to the shortened pathname 10118 * bufp - Pointer to an allocated buffer for the filename. 10119 * fnamelen - Length of the filename pointed to by fname 10120 * 10121 * Returns OK on success (or nothing done) and FAIL on failure (out of memory). 10122 */ 10123 static int 10124 shortpath_for_invalid_fname( 10125 char_u **fname, 10126 char_u **bufp, 10127 int *fnamelen) 10128 { 10129 char_u *short_fname, *save_fname, *pbuf_unused; 10130 char_u *endp, *save_endp; 10131 char_u ch; 10132 int old_len, len; 10133 int new_len, sfx_len; 10134 int retval = OK; 10135 10136 /* Make a copy */ 10137 old_len = *fnamelen; 10138 save_fname = vim_strnsave(*fname, old_len); 10139 pbuf_unused = NULL; 10140 short_fname = NULL; 10141 10142 endp = save_fname + old_len - 1; /* Find the end of the copy */ 10143 save_endp = endp; 10144 10145 /* 10146 * Try shortening the supplied path till it succeeds by removing one 10147 * directory at a time from the tail of the path. 10148 */ 10149 len = 0; 10150 for (;;) 10151 { 10152 /* go back one path-separator */ 10153 while (endp > save_fname && !after_pathsep(save_fname, endp + 1)) 10154 --endp; 10155 if (endp <= save_fname) 10156 break; /* processed the complete path */ 10157 10158 /* 10159 * Replace the path separator with a NUL and try to shorten the 10160 * resulting path. 10161 */ 10162 ch = *endp; 10163 *endp = 0; 10164 short_fname = save_fname; 10165 len = (int)STRLEN(short_fname) + 1; 10166 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL) 10167 { 10168 retval = FAIL; 10169 goto theend; 10170 } 10171 *endp = ch; /* preserve the string */ 10172 10173 if (len > 0) 10174 break; /* successfully shortened the path */ 10175 10176 /* failed to shorten the path. Skip the path separator */ 10177 --endp; 10178 } 10179 10180 if (len > 0) 10181 { 10182 /* 10183 * Succeeded in shortening the path. Now concatenate the shortened 10184 * path with the remaining path at the tail. 10185 */ 10186 10187 /* Compute the length of the new path. */ 10188 sfx_len = (int)(save_endp - endp) + 1; 10189 new_len = len + sfx_len; 10190 10191 *fnamelen = new_len; 10192 vim_free(*bufp); 10193 if (new_len > old_len) 10194 { 10195 /* There is not enough space in the currently allocated string, 10196 * copy it to a buffer big enough. */ 10197 *fname = *bufp = vim_strnsave(short_fname, new_len); 10198 if (*fname == NULL) 10199 { 10200 retval = FAIL; 10201 goto theend; 10202 } 10203 } 10204 else 10205 { 10206 /* Transfer short_fname to the main buffer (it's big enough), 10207 * unless get_short_pathname() did its work in-place. */ 10208 *fname = *bufp = save_fname; 10209 if (short_fname != save_fname) 10210 vim_strncpy(save_fname, short_fname, len); 10211 save_fname = NULL; 10212 } 10213 10214 /* concat the not-shortened part of the path */ 10215 vim_strncpy(*fname + len, endp, sfx_len); 10216 (*fname)[new_len] = NUL; 10217 } 10218 10219 theend: 10220 vim_free(pbuf_unused); 10221 vim_free(save_fname); 10222 10223 return retval; 10224 } 10225 10226 /* 10227 * Get a pathname for a partial path. 10228 * Returns OK for success, FAIL for failure. 10229 */ 10230 static int 10231 shortpath_for_partial( 10232 char_u **fnamep, 10233 char_u **bufp, 10234 int *fnamelen) 10235 { 10236 int sepcount, len, tflen; 10237 char_u *p; 10238 char_u *pbuf, *tfname; 10239 int hasTilde; 10240 10241 /* Count up the path separators from the RHS.. so we know which part 10242 * of the path to return. */ 10243 sepcount = 0; 10244 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p)) 10245 if (vim_ispathsep(*p)) 10246 ++sepcount; 10247 10248 /* Need full path first (use expand_env() to remove a "~/") */ 10249 hasTilde = (**fnamep == '~'); 10250 if (hasTilde) 10251 pbuf = tfname = expand_env_save(*fnamep); 10252 else 10253 pbuf = tfname = FullName_save(*fnamep, FALSE); 10254 10255 len = tflen = (int)STRLEN(tfname); 10256 10257 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL) 10258 return FAIL; 10259 10260 if (len == 0) 10261 { 10262 /* Don't have a valid filename, so shorten the rest of the 10263 * path if we can. This CAN give us invalid 8.3 filenames, but 10264 * there's not a lot of point in guessing what it might be. 10265 */ 10266 len = tflen; 10267 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL) 10268 return FAIL; 10269 } 10270 10271 /* Count the paths backward to find the beginning of the desired string. */ 10272 for (p = tfname + len - 1; p >= tfname; --p) 10273 { 10274 if (has_mbyte) 10275 p -= mb_head_off(tfname, p); 10276 if (vim_ispathsep(*p)) 10277 { 10278 if (sepcount == 0 || (hasTilde && sepcount == 1)) 10279 break; 10280 else 10281 sepcount --; 10282 } 10283 } 10284 if (hasTilde) 10285 { 10286 --p; 10287 if (p >= tfname) 10288 *p = '~'; 10289 else 10290 return FAIL; 10291 } 10292 else 10293 ++p; 10294 10295 /* Copy in the string - p indexes into tfname - allocated at pbuf */ 10296 vim_free(*bufp); 10297 *fnamelen = (int)STRLEN(p); 10298 *bufp = pbuf; 10299 *fnamep = p; 10300 10301 return OK; 10302 } 10303 #endif // MSWIN 10304 10305 /* 10306 * Adjust a filename, according to a string of modifiers. 10307 * *fnamep must be NUL terminated when called. When returning, the length is 10308 * determined by *fnamelen. 10309 * Returns VALID_ flags or -1 for failure. 10310 * When there is an error, *fnamep is set to NULL. 10311 */ 10312 int 10313 modify_fname( 10314 char_u *src, // string with modifiers 10315 int tilde_file, // "~" is a file name, not $HOME 10316 int *usedlen, // characters after src that are used 10317 char_u **fnamep, // file name so far 10318 char_u **bufp, // buffer for allocated file name or NULL 10319 int *fnamelen) // length of fnamep 10320 { 10321 int valid = 0; 10322 char_u *tail; 10323 char_u *s, *p, *pbuf; 10324 char_u dirname[MAXPATHL]; 10325 int c; 10326 int has_fullname = 0; 10327 #ifdef MSWIN 10328 char_u *fname_start = *fnamep; 10329 int has_shortname = 0; 10330 #endif 10331 10332 repeat: 10333 /* ":p" - full path/file_name */ 10334 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p') 10335 { 10336 has_fullname = 1; 10337 10338 valid |= VALID_PATH; 10339 *usedlen += 2; 10340 10341 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */ 10342 if ((*fnamep)[0] == '~' 10343 #if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME)) 10344 && ((*fnamep)[1] == '/' 10345 # ifdef BACKSLASH_IN_FILENAME 10346 || (*fnamep)[1] == '\\' 10347 # endif 10348 || (*fnamep)[1] == NUL) 10349 #endif 10350 && !(tilde_file && (*fnamep)[1] == NUL) 10351 ) 10352 { 10353 *fnamep = expand_env_save(*fnamep); 10354 vim_free(*bufp); /* free any allocated file name */ 10355 *bufp = *fnamep; 10356 if (*fnamep == NULL) 10357 return -1; 10358 } 10359 10360 /* When "/." or "/.." is used: force expansion to get rid of it. */ 10361 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p)) 10362 { 10363 if (vim_ispathsep(*p) 10364 && p[1] == '.' 10365 && (p[2] == NUL 10366 || vim_ispathsep(p[2]) 10367 || (p[2] == '.' 10368 && (p[3] == NUL || vim_ispathsep(p[3]))))) 10369 break; 10370 } 10371 10372 /* FullName_save() is slow, don't use it when not needed. */ 10373 if (*p != NUL || !vim_isAbsName(*fnamep)) 10374 { 10375 *fnamep = FullName_save(*fnamep, *p != NUL); 10376 vim_free(*bufp); /* free any allocated file name */ 10377 *bufp = *fnamep; 10378 if (*fnamep == NULL) 10379 return -1; 10380 } 10381 10382 #ifdef MSWIN 10383 # if _WIN32_WINNT >= 0x0500 10384 if (vim_strchr(*fnamep, '~') != NULL) 10385 { 10386 // Expand 8.3 filename to full path. Needed to make sure the same 10387 // file does not have two different names. 10388 // Note: problem does not occur if _WIN32_WINNT < 0x0500. 10389 WCHAR *wfname = enc_to_utf16(*fnamep, NULL); 10390 WCHAR buf[_MAX_PATH]; 10391 10392 if (wfname != NULL) 10393 { 10394 if (GetLongPathNameW(wfname, buf, _MAX_PATH)) 10395 { 10396 char_u *p = utf16_to_enc(buf, NULL); 10397 10398 if (p != NULL) 10399 { 10400 vim_free(*bufp); // free any allocated file name 10401 *bufp = *fnamep = p; 10402 } 10403 } 10404 vim_free(wfname); 10405 } 10406 } 10407 # endif 10408 #endif 10409 /* Append a path separator to a directory. */ 10410 if (mch_isdir(*fnamep)) 10411 { 10412 /* Make room for one or two extra characters. */ 10413 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2); 10414 vim_free(*bufp); /* free any allocated file name */ 10415 *bufp = *fnamep; 10416 if (*fnamep == NULL) 10417 return -1; 10418 add_pathsep(*fnamep); 10419 } 10420 } 10421 10422 /* ":." - path relative to the current directory */ 10423 /* ":~" - path relative to the home directory */ 10424 /* ":8" - shortname path - postponed till after */ 10425 while (src[*usedlen] == ':' 10426 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8')) 10427 { 10428 *usedlen += 2; 10429 if (c == '8') 10430 { 10431 #ifdef MSWIN 10432 has_shortname = 1; /* Postpone this. */ 10433 #endif 10434 continue; 10435 } 10436 pbuf = NULL; 10437 /* Need full path first (use expand_env() to remove a "~/") */ 10438 if (!has_fullname) 10439 { 10440 if (c == '.' && **fnamep == '~') 10441 p = pbuf = expand_env_save(*fnamep); 10442 else 10443 p = pbuf = FullName_save(*fnamep, FALSE); 10444 } 10445 else 10446 p = *fnamep; 10447 10448 has_fullname = 0; 10449 10450 if (p != NULL) 10451 { 10452 if (c == '.') 10453 { 10454 mch_dirname(dirname, MAXPATHL); 10455 s = shorten_fname(p, dirname); 10456 if (s != NULL) 10457 { 10458 *fnamep = s; 10459 if (pbuf != NULL) 10460 { 10461 vim_free(*bufp); /* free any allocated file name */ 10462 *bufp = pbuf; 10463 pbuf = NULL; 10464 } 10465 } 10466 } 10467 else 10468 { 10469 home_replace(NULL, p, dirname, MAXPATHL, TRUE); 10470 /* Only replace it when it starts with '~' */ 10471 if (*dirname == '~') 10472 { 10473 s = vim_strsave(dirname); 10474 if (s != NULL) 10475 { 10476 *fnamep = s; 10477 vim_free(*bufp); 10478 *bufp = s; 10479 } 10480 } 10481 } 10482 vim_free(pbuf); 10483 } 10484 } 10485 10486 tail = gettail(*fnamep); 10487 *fnamelen = (int)STRLEN(*fnamep); 10488 10489 /* ":h" - head, remove "/file_name", can be repeated */ 10490 /* Don't remove the first "/" or "c:\" */ 10491 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h') 10492 { 10493 valid |= VALID_HEAD; 10494 *usedlen += 2; 10495 s = get_past_head(*fnamep); 10496 while (tail > s && after_pathsep(s, tail)) 10497 MB_PTR_BACK(*fnamep, tail); 10498 *fnamelen = (int)(tail - *fnamep); 10499 #ifdef VMS 10500 if (*fnamelen > 0) 10501 *fnamelen += 1; /* the path separator is part of the path */ 10502 #endif 10503 if (*fnamelen == 0) 10504 { 10505 /* Result is empty. Turn it into "." to make ":cd %:h" work. */ 10506 p = vim_strsave((char_u *)"."); 10507 if (p == NULL) 10508 return -1; 10509 vim_free(*bufp); 10510 *bufp = *fnamep = tail = p; 10511 *fnamelen = 1; 10512 } 10513 else 10514 { 10515 while (tail > s && !after_pathsep(s, tail)) 10516 MB_PTR_BACK(*fnamep, tail); 10517 } 10518 } 10519 10520 /* ":8" - shortname */ 10521 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8') 10522 { 10523 *usedlen += 2; 10524 #ifdef MSWIN 10525 has_shortname = 1; 10526 #endif 10527 } 10528 10529 #ifdef MSWIN 10530 /* 10531 * Handle ":8" after we have done 'heads' and before we do 'tails'. 10532 */ 10533 if (has_shortname) 10534 { 10535 /* Copy the string if it is shortened by :h and when it wasn't copied 10536 * yet, because we are going to change it in place. Avoids changing 10537 * the buffer name for "%:8". */ 10538 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start) 10539 { 10540 p = vim_strnsave(*fnamep, *fnamelen); 10541 if (p == NULL) 10542 return -1; 10543 vim_free(*bufp); 10544 *bufp = *fnamep = p; 10545 } 10546 10547 /* Split into two implementations - makes it easier. First is where 10548 * there isn't a full name already, second is where there is. */ 10549 if (!has_fullname && !vim_isAbsName(*fnamep)) 10550 { 10551 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL) 10552 return -1; 10553 } 10554 else 10555 { 10556 int l = *fnamelen; 10557 10558 /* Simple case, already have the full-name. 10559 * Nearly always shorter, so try first time. */ 10560 if (get_short_pathname(fnamep, bufp, &l) == FAIL) 10561 return -1; 10562 10563 if (l == 0) 10564 { 10565 /* Couldn't find the filename, search the paths. */ 10566 l = *fnamelen; 10567 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL) 10568 return -1; 10569 } 10570 *fnamelen = l; 10571 } 10572 } 10573 #endif // MSWIN 10574 10575 /* ":t" - tail, just the basename */ 10576 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't') 10577 { 10578 *usedlen += 2; 10579 *fnamelen -= (int)(tail - *fnamep); 10580 *fnamep = tail; 10581 } 10582 10583 /* ":e" - extension, can be repeated */ 10584 /* ":r" - root, without extension, can be repeated */ 10585 while (src[*usedlen] == ':' 10586 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r')) 10587 { 10588 /* find a '.' in the tail: 10589 * - for second :e: before the current fname 10590 * - otherwise: The last '.' 10591 */ 10592 if (src[*usedlen + 1] == 'e' && *fnamep > tail) 10593 s = *fnamep - 2; 10594 else 10595 s = *fnamep + *fnamelen - 1; 10596 for ( ; s > tail; --s) 10597 if (s[0] == '.') 10598 break; 10599 if (src[*usedlen + 1] == 'e') /* :e */ 10600 { 10601 if (s > tail) 10602 { 10603 *fnamelen += (int)(*fnamep - (s + 1)); 10604 *fnamep = s + 1; 10605 #ifdef VMS 10606 /* cut version from the extension */ 10607 s = *fnamep + *fnamelen - 1; 10608 for ( ; s > *fnamep; --s) 10609 if (s[0] == ';') 10610 break; 10611 if (s > *fnamep) 10612 *fnamelen = s - *fnamep; 10613 #endif 10614 } 10615 else if (*fnamep <= tail) 10616 *fnamelen = 0; 10617 } 10618 else /* :r */ 10619 { 10620 if (s > tail) /* remove one extension */ 10621 *fnamelen = (int)(s - *fnamep); 10622 } 10623 *usedlen += 2; 10624 } 10625 10626 /* ":s?pat?foo?" - substitute */ 10627 /* ":gs?pat?foo?" - global substitute */ 10628 if (src[*usedlen] == ':' 10629 && (src[*usedlen + 1] == 's' 10630 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's'))) 10631 { 10632 char_u *str; 10633 char_u *pat; 10634 char_u *sub; 10635 int sep; 10636 char_u *flags; 10637 int didit = FALSE; 10638 10639 flags = (char_u *)""; 10640 s = src + *usedlen + 2; 10641 if (src[*usedlen + 1] == 'g') 10642 { 10643 flags = (char_u *)"g"; 10644 ++s; 10645 } 10646 10647 sep = *s++; 10648 if (sep) 10649 { 10650 /* find end of pattern */ 10651 p = vim_strchr(s, sep); 10652 if (p != NULL) 10653 { 10654 pat = vim_strnsave(s, (int)(p - s)); 10655 if (pat != NULL) 10656 { 10657 s = p + 1; 10658 /* find end of substitution */ 10659 p = vim_strchr(s, sep); 10660 if (p != NULL) 10661 { 10662 sub = vim_strnsave(s, (int)(p - s)); 10663 str = vim_strnsave(*fnamep, *fnamelen); 10664 if (sub != NULL && str != NULL) 10665 { 10666 *usedlen = (int)(p + 1 - src); 10667 s = do_string_sub(str, pat, sub, NULL, flags); 10668 if (s != NULL) 10669 { 10670 *fnamep = s; 10671 *fnamelen = (int)STRLEN(s); 10672 vim_free(*bufp); 10673 *bufp = s; 10674 didit = TRUE; 10675 } 10676 } 10677 vim_free(sub); 10678 vim_free(str); 10679 } 10680 vim_free(pat); 10681 } 10682 } 10683 /* after using ":s", repeat all the modifiers */ 10684 if (didit) 10685 goto repeat; 10686 } 10687 } 10688 10689 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S') 10690 { 10691 /* vim_strsave_shellescape() needs a NUL terminated string. */ 10692 c = (*fnamep)[*fnamelen]; 10693 if (c != NUL) 10694 (*fnamep)[*fnamelen] = NUL; 10695 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE); 10696 if (c != NUL) 10697 (*fnamep)[*fnamelen] = c; 10698 if (p == NULL) 10699 return -1; 10700 vim_free(*bufp); 10701 *bufp = *fnamep = p; 10702 *fnamelen = (int)STRLEN(p); 10703 *usedlen += 2; 10704 } 10705 10706 return valid; 10707 } 10708 10709 /* 10710 * Perform a substitution on "str" with pattern "pat" and substitute "sub". 10711 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL. 10712 * "flags" can be "g" to do a global substitute. 10713 * Returns an allocated string, NULL for error. 10714 */ 10715 char_u * 10716 do_string_sub( 10717 char_u *str, 10718 char_u *pat, 10719 char_u *sub, 10720 typval_T *expr, 10721 char_u *flags) 10722 { 10723 int sublen; 10724 regmatch_T regmatch; 10725 int i; 10726 int do_all; 10727 char_u *tail; 10728 char_u *end; 10729 garray_T ga; 10730 char_u *ret; 10731 char_u *save_cpo; 10732 char_u *zero_width = NULL; 10733 10734 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */ 10735 save_cpo = p_cpo; 10736 p_cpo = empty_option; 10737 10738 ga_init2(&ga, 1, 200); 10739 10740 do_all = (flags[0] == 'g'); 10741 10742 regmatch.rm_ic = p_ic; 10743 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING); 10744 if (regmatch.regprog != NULL) 10745 { 10746 tail = str; 10747 end = str + STRLEN(str); 10748 while (vim_regexec_nl(®match, str, (colnr_T)(tail - str))) 10749 { 10750 /* Skip empty match except for first match. */ 10751 if (regmatch.startp[0] == regmatch.endp[0]) 10752 { 10753 if (zero_width == regmatch.startp[0]) 10754 { 10755 /* avoid getting stuck on a match with an empty string */ 10756 i = MB_PTR2LEN(tail); 10757 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, 10758 (size_t)i); 10759 ga.ga_len += i; 10760 tail += i; 10761 continue; 10762 } 10763 zero_width = regmatch.startp[0]; 10764 } 10765 10766 /* 10767 * Get some space for a temporary buffer to do the substitution 10768 * into. It will contain: 10769 * - The text up to where the match is. 10770 * - The substituted text. 10771 * - The text after the match. 10772 */ 10773 sublen = vim_regsub(®match, sub, expr, tail, FALSE, TRUE, FALSE); 10774 if (ga_grow(&ga, (int)((end - tail) + sublen - 10775 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL) 10776 { 10777 ga_clear(&ga); 10778 break; 10779 } 10780 10781 /* copy the text up to where the match is */ 10782 i = (int)(regmatch.startp[0] - tail); 10783 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i); 10784 /* add the substituted text */ 10785 (void)vim_regsub(®match, sub, expr, (char_u *)ga.ga_data 10786 + ga.ga_len + i, TRUE, TRUE, FALSE); 10787 ga.ga_len += i + sublen - 1; 10788 tail = regmatch.endp[0]; 10789 if (*tail == NUL) 10790 break; 10791 if (!do_all) 10792 break; 10793 } 10794 10795 if (ga.ga_data != NULL) 10796 STRCPY((char *)ga.ga_data + ga.ga_len, tail); 10797 10798 vim_regfree(regmatch.regprog); 10799 } 10800 10801 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data); 10802 ga_clear(&ga); 10803 if (p_cpo == empty_option) 10804 p_cpo = save_cpo; 10805 else 10806 /* Darn, evaluating {sub} expression or {expr} changed the value. */ 10807 free_string_option(save_cpo); 10808 10809 return ret; 10810 } 10811 10812 static int 10813 filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp) 10814 { 10815 typval_T rettv; 10816 typval_T argv[3]; 10817 int retval = FAIL; 10818 10819 copy_tv(tv, &vimvars[VV_VAL].vv_tv); 10820 argv[0] = vimvars[VV_KEY].vv_tv; 10821 argv[1] = vimvars[VV_VAL].vv_tv; 10822 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL) 10823 goto theend; 10824 if (map) 10825 { 10826 /* map(): replace the list item value */ 10827 clear_tv(tv); 10828 rettv.v_lock = 0; 10829 *tv = rettv; 10830 } 10831 else 10832 { 10833 int error = FALSE; 10834 10835 /* filter(): when expr is zero remove the item */ 10836 *remp = (tv_get_number_chk(&rettv, &error) == 0); 10837 clear_tv(&rettv); 10838 /* On type error, nothing has been removed; return FAIL to stop the 10839 * loop. The error message was given by tv_get_number_chk(). */ 10840 if (error) 10841 goto theend; 10842 } 10843 retval = OK; 10844 theend: 10845 clear_tv(&vimvars[VV_VAL].vv_tv); 10846 return retval; 10847 } 10848 10849 10850 /* 10851 * Implementation of map() and filter(). 10852 */ 10853 void 10854 filter_map(typval_T *argvars, typval_T *rettv, int map) 10855 { 10856 typval_T *expr; 10857 listitem_T *li, *nli; 10858 list_T *l = NULL; 10859 dictitem_T *di; 10860 hashtab_T *ht; 10861 hashitem_T *hi; 10862 dict_T *d = NULL; 10863 typval_T save_val; 10864 typval_T save_key; 10865 blob_T *b = NULL; 10866 int rem; 10867 int todo; 10868 char_u *ermsg = (char_u *)(map ? "map()" : "filter()"); 10869 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument") 10870 : N_("filter() argument")); 10871 int save_did_emsg; 10872 int idx = 0; 10873 10874 if (argvars[0].v_type == VAR_BLOB) 10875 { 10876 if ((b = argvars[0].vval.v_blob) == NULL) 10877 return; 10878 } 10879 else if (argvars[0].v_type == VAR_LIST) 10880 { 10881 if ((l = argvars[0].vval.v_list) == NULL 10882 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE))) 10883 return; 10884 } 10885 else if (argvars[0].v_type == VAR_DICT) 10886 { 10887 if ((d = argvars[0].vval.v_dict) == NULL 10888 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE))) 10889 return; 10890 } 10891 else 10892 { 10893 semsg(_(e_listdictarg), ermsg); 10894 return; 10895 } 10896 10897 expr = &argvars[1]; 10898 /* On type errors, the preceding call has already displayed an error 10899 * message. Avoid a misleading error message for an empty string that 10900 * was not passed as argument. */ 10901 if (expr->v_type != VAR_UNKNOWN) 10902 { 10903 prepare_vimvar(VV_VAL, &save_val); 10904 10905 /* We reset "did_emsg" to be able to detect whether an error 10906 * occurred during evaluation of the expression. */ 10907 save_did_emsg = did_emsg; 10908 did_emsg = FALSE; 10909 10910 prepare_vimvar(VV_KEY, &save_key); 10911 if (argvars[0].v_type == VAR_DICT) 10912 { 10913 vimvars[VV_KEY].vv_type = VAR_STRING; 10914 10915 ht = &d->dv_hashtab; 10916 hash_lock(ht); 10917 todo = (int)ht->ht_used; 10918 for (hi = ht->ht_array; todo > 0; ++hi) 10919 { 10920 if (!HASHITEM_EMPTY(hi)) 10921 { 10922 int r; 10923 10924 --todo; 10925 di = HI2DI(hi); 10926 if (map && (var_check_lock(di->di_tv.v_lock, 10927 arg_errmsg, TRUE) 10928 || var_check_ro(di->di_flags, 10929 arg_errmsg, TRUE))) 10930 break; 10931 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key); 10932 r = filter_map_one(&di->di_tv, expr, map, &rem); 10933 clear_tv(&vimvars[VV_KEY].vv_tv); 10934 if (r == FAIL || did_emsg) 10935 break; 10936 if (!map && rem) 10937 { 10938 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE) 10939 || var_check_ro(di->di_flags, arg_errmsg, TRUE)) 10940 break; 10941 dictitem_remove(d, di); 10942 } 10943 } 10944 } 10945 hash_unlock(ht); 10946 } 10947 else if (argvars[0].v_type == VAR_BLOB) 10948 { 10949 int i; 10950 typval_T tv; 10951 10952 vimvars[VV_KEY].vv_type = VAR_NUMBER; 10953 for (i = 0; i < b->bv_ga.ga_len; i++) 10954 { 10955 tv.v_type = VAR_NUMBER; 10956 tv.vval.v_number = blob_get(b, i); 10957 vimvars[VV_KEY].vv_nr = idx; 10958 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg) 10959 break; 10960 if (tv.v_type != VAR_NUMBER) 10961 { 10962 emsg(_(e_invalblob)); 10963 return; 10964 } 10965 tv.v_type = VAR_NUMBER; 10966 blob_set(b, i, tv.vval.v_number); 10967 if (!map && rem) 10968 { 10969 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data; 10970 10971 mch_memmove(p + idx, p + i + 1, 10972 (size_t)b->bv_ga.ga_len - i - 1); 10973 --b->bv_ga.ga_len; 10974 --i; 10975 } 10976 } 10977 } 10978 else 10979 { 10980 // argvars[0].v_type == VAR_LIST 10981 vimvars[VV_KEY].vv_type = VAR_NUMBER; 10982 10983 for (li = l->lv_first; li != NULL; li = nli) 10984 { 10985 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE)) 10986 break; 10987 nli = li->li_next; 10988 vimvars[VV_KEY].vv_nr = idx; 10989 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL 10990 || did_emsg) 10991 break; 10992 if (!map && rem) 10993 listitem_remove(l, li); 10994 ++idx; 10995 } 10996 } 10997 10998 restore_vimvar(VV_KEY, &save_key); 10999 restore_vimvar(VV_VAL, &save_val); 11000 11001 did_emsg |= save_did_emsg; 11002 } 11003 11004 copy_tv(&argvars[0], rettv); 11005 } 11006 11007 #endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */ 11008