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