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