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