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