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