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