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