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