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 * scriptfile.c: functions for dealing with the runtime directories/files 12 */ 13 14 #include "vim.h" 15 16 #if defined(FEAT_EVAL) || defined(PROTO) 17 // The names of packages that once were loaded are remembered. 18 static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL}; 19 #endif 20 21 /* 22 * Initialize the execution stack. 23 */ 24 void 25 estack_init(void) 26 { 27 estack_T *entry; 28 29 if (ga_grow(&exestack, 10) == FAIL) 30 mch_exit(0); 31 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len; 32 entry->es_type = ETYPE_TOP; 33 entry->es_name = NULL; 34 entry->es_lnum = 0; 35 #ifdef FEAT_EVAL 36 entry->es_info.ufunc = NULL; 37 #endif 38 ++exestack.ga_len; 39 } 40 41 /* 42 * Add an item to the execution stack. 43 * Returns the new entry or NULL when out of memory. 44 */ 45 estack_T * 46 estack_push(etype_T type, char_u *name, long lnum) 47 { 48 estack_T *entry; 49 50 // If memory allocation fails then we'll pop more than we push, eventually 51 // at the top level it will be OK again. 52 if (ga_grow(&exestack, 1) == OK) 53 { 54 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len; 55 entry->es_type = type; 56 entry->es_name = name; 57 entry->es_lnum = lnum; 58 #ifdef FEAT_EVAL 59 entry->es_info.ufunc = NULL; 60 #endif 61 ++exestack.ga_len; 62 return entry; 63 } 64 return NULL; 65 } 66 67 #if defined(FEAT_EVAL) || defined(PROTO) 68 /* 69 * Add a user function to the execution stack. 70 */ 71 void 72 estack_push_ufunc(ufunc_T *ufunc, long lnum) 73 { 74 estack_T *entry = estack_push(ETYPE_UFUNC, 75 ufunc->uf_name_exp != NULL 76 ? ufunc->uf_name_exp : ufunc->uf_name, lnum); 77 if (entry != NULL) 78 entry->es_info.ufunc = ufunc; 79 } 80 81 /* 82 * Return TRUE if "ufunc" with "lnum" is already at the top of the exe stack. 83 */ 84 int 85 estack_top_is_ufunc(ufunc_T *ufunc, long lnum) 86 { 87 estack_T *entry; 88 89 if (exestack.ga_len == 0) 90 return FALSE; 91 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1; 92 return entry->es_type == ETYPE_UFUNC 93 && STRCMP( entry->es_name, ufunc->uf_name_exp != NULL 94 ? ufunc->uf_name_exp : ufunc->uf_name) == 0 95 && entry->es_lnum == lnum; 96 } 97 #endif 98 99 /* 100 * Take an item off of the execution stack. 101 */ 102 void 103 estack_pop(void) 104 { 105 if (exestack.ga_len > 1) 106 --exestack.ga_len; 107 } 108 109 /* 110 * Get the current value for <sfile> in allocated memory. 111 */ 112 char_u * 113 estack_sfile(void) 114 { 115 estack_T *entry; 116 #ifdef FEAT_EVAL 117 size_t len; 118 int idx; 119 char *res; 120 size_t done; 121 #endif 122 123 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1; 124 if (entry->es_name == NULL) 125 return NULL; 126 #ifdef FEAT_EVAL 127 if (entry->es_info.ufunc == NULL) 128 #endif 129 return vim_strsave(entry->es_name); 130 131 #ifdef FEAT_EVAL 132 // For a function we compose the call stack, as it was done in the past: 133 // "function One[123]..Two[456]..Three" 134 len = STRLEN(entry->es_name) + 10; 135 for (idx = exestack.ga_len - 2; idx >= 0; --idx) 136 { 137 entry = ((estack_T *)exestack.ga_data) + idx; 138 if (entry->es_name == NULL || entry->es_info.ufunc == NULL) 139 { 140 ++idx; 141 break; 142 } 143 len += STRLEN(entry->es_name) + 15; 144 } 145 146 res = (char *)alloc((int)len); 147 if (res != NULL) 148 { 149 STRCPY(res, "function "); 150 while (idx < exestack.ga_len - 1) 151 { 152 done = STRLEN(res); 153 entry = ((estack_T *)exestack.ga_data) + idx; 154 vim_snprintf(res + done, len - done, "%s[%ld]..", 155 entry->es_name, entry->es_lnum); 156 ++idx; 157 } 158 done = STRLEN(res); 159 entry = ((estack_T *)exestack.ga_data) + idx; 160 vim_snprintf(res + done, len - done, "%s", entry->es_name); 161 } 162 return (char_u *)res; 163 #endif 164 } 165 166 /* 167 * ":runtime [what] {name}" 168 */ 169 void 170 ex_runtime(exarg_T *eap) 171 { 172 char_u *arg = eap->arg; 173 char_u *p = skiptowhite(arg); 174 int len = (int)(p - arg); 175 int flags = eap->forceit ? DIP_ALL : 0; 176 177 if (STRNCMP(arg, "START", len) == 0) 178 { 179 flags += DIP_START + DIP_NORTP; 180 arg = skipwhite(arg + len); 181 } 182 else if (STRNCMP(arg, "OPT", len) == 0) 183 { 184 flags += DIP_OPT + DIP_NORTP; 185 arg = skipwhite(arg + len); 186 } 187 else if (STRNCMP(arg, "PACK", len) == 0) 188 { 189 flags += DIP_START + DIP_OPT + DIP_NORTP; 190 arg = skipwhite(arg + len); 191 } 192 else if (STRNCMP(arg, "ALL", len) == 0) 193 { 194 flags += DIP_START + DIP_OPT; 195 arg = skipwhite(arg + len); 196 } 197 198 source_runtime(arg, flags); 199 } 200 201 static void 202 source_callback(char_u *fname, void *cookie) 203 { 204 (void)do_source(fname, FALSE, DOSO_NONE, cookie); 205 } 206 207 /* 208 * Find the file "name" in all directories in "path" and invoke 209 * "callback(fname, cookie)". 210 * "name" can contain wildcards. 211 * When "flags" has DIP_ALL: source all files, otherwise only the first one. 212 * When "flags" has DIP_DIR: find directories instead of files. 213 * When "flags" has DIP_ERR: give an error message if there is no match. 214 * 215 * return FAIL when no file could be sourced, OK otherwise. 216 */ 217 int 218 do_in_path( 219 char_u *path, 220 char_u *name, 221 int flags, 222 void (*callback)(char_u *fname, void *ck), 223 void *cookie) 224 { 225 char_u *rtp; 226 char_u *np; 227 char_u *buf; 228 char_u *rtp_copy; 229 char_u *tail; 230 int num_files; 231 char_u **files; 232 int i; 233 int did_one = FALSE; 234 #ifdef AMIGA 235 struct Process *proc = (struct Process *)FindTask(0L); 236 APTR save_winptr = proc->pr_WindowPtr; 237 238 // Avoid a requester here for a volume that doesn't exist. 239 proc->pr_WindowPtr = (APTR)-1L; 240 #endif 241 242 // Make a copy of 'runtimepath'. Invoking the callback may change the 243 // value. 244 rtp_copy = vim_strsave(path); 245 buf = alloc(MAXPATHL); 246 if (buf != NULL && rtp_copy != NULL) 247 { 248 if (p_verbose > 10 && name != NULL) 249 { 250 verbose_enter(); 251 smsg(_("Searching for \"%s\" in \"%s\""), 252 (char *)name, (char *)path); 253 verbose_leave(); 254 } 255 256 // Loop over all entries in 'runtimepath'. 257 rtp = rtp_copy; 258 while (*rtp != NUL && ((flags & DIP_ALL) || !did_one)) 259 { 260 size_t buflen; 261 262 // Copy the path from 'runtimepath' to buf[]. 263 copy_option_part(&rtp, buf, MAXPATHL, ","); 264 buflen = STRLEN(buf); 265 266 // Skip after or non-after directories. 267 if (flags & (DIP_NOAFTER | DIP_AFTER)) 268 { 269 int is_after = buflen >= 5 270 && STRCMP(buf + buflen - 5, "after") == 0; 271 272 if ((is_after && (flags & DIP_NOAFTER)) 273 || (!is_after && (flags & DIP_AFTER))) 274 continue; 275 } 276 277 if (name == NULL) 278 { 279 (*callback)(buf, (void *) &cookie); 280 if (!did_one) 281 did_one = (cookie == NULL); 282 } 283 else if (buflen + STRLEN(name) + 2 < MAXPATHL) 284 { 285 add_pathsep(buf); 286 tail = buf + STRLEN(buf); 287 288 // Loop over all patterns in "name" 289 np = name; 290 while (*np != NUL && ((flags & DIP_ALL) || !did_one)) 291 { 292 // Append the pattern from "name" to buf[]. 293 copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)), 294 "\t "); 295 296 if (p_verbose > 10) 297 { 298 verbose_enter(); 299 smsg(_("Searching for \"%s\""), buf); 300 verbose_leave(); 301 } 302 303 // Expand wildcards, invoke the callback for each match. 304 if (gen_expand_wildcards(1, &buf, &num_files, &files, 305 (flags & DIP_DIR) ? EW_DIR : EW_FILE) == OK) 306 { 307 for (i = 0; i < num_files; ++i) 308 { 309 (*callback)(files[i], cookie); 310 did_one = TRUE; 311 if (!(flags & DIP_ALL)) 312 break; 313 } 314 FreeWild(num_files, files); 315 } 316 } 317 } 318 } 319 } 320 vim_free(buf); 321 vim_free(rtp_copy); 322 if (!did_one && name != NULL) 323 { 324 char *basepath = path == p_rtp ? "runtimepath" : "packpath"; 325 326 if (flags & DIP_ERR) 327 semsg(_(e_dirnotf), basepath, name); 328 else if (p_verbose > 0) 329 { 330 verbose_enter(); 331 smsg(_("not found in '%s': \"%s\""), basepath, name); 332 verbose_leave(); 333 } 334 } 335 336 #ifdef AMIGA 337 proc->pr_WindowPtr = save_winptr; 338 #endif 339 340 return did_one ? OK : FAIL; 341 } 342 343 /* 344 * Find "name" in "path". When found, invoke the callback function for 345 * it: callback(fname, "cookie") 346 * When "flags" has DIP_ALL repeat for all matches, otherwise only the first 347 * one is used. 348 * Returns OK when at least one match found, FAIL otherwise. 349 * 350 * If "name" is NULL calls callback for each entry in "path". Cookie is 351 * passed by reference in this case, setting it to NULL indicates that callback 352 * has done its job. 353 */ 354 static int 355 do_in_path_and_pp( 356 char_u *path, 357 char_u *name, 358 int flags, 359 void (*callback)(char_u *fname, void *ck), 360 void *cookie) 361 { 362 int done = FAIL; 363 char_u *s; 364 int len; 365 char *start_dir = "pack/*/start/*/%s"; 366 char *opt_dir = "pack/*/opt/*/%s"; 367 368 if ((flags & DIP_NORTP) == 0) 369 done = do_in_path(path, name, flags, callback, cookie); 370 371 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_START)) 372 { 373 len = (int)(STRLEN(start_dir) + STRLEN(name)); 374 s = alloc(len); 375 if (s == NULL) 376 return FAIL; 377 vim_snprintf((char *)s, len, start_dir, name); 378 done = do_in_path(p_pp, s, flags, callback, cookie); 379 vim_free(s); 380 } 381 382 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_OPT)) 383 { 384 len = (int)(STRLEN(opt_dir) + STRLEN(name)); 385 s = alloc(len); 386 if (s == NULL) 387 return FAIL; 388 vim_snprintf((char *)s, len, opt_dir, name); 389 done = do_in_path(p_pp, s, flags, callback, cookie); 390 vim_free(s); 391 } 392 393 return done; 394 } 395 396 /* 397 * Just like do_in_path_and_pp(), using 'runtimepath' for "path". 398 */ 399 int 400 do_in_runtimepath( 401 char_u *name, 402 int flags, 403 void (*callback)(char_u *fname, void *ck), 404 void *cookie) 405 { 406 return do_in_path_and_pp(p_rtp, name, flags, callback, cookie); 407 } 408 409 /* 410 * Source the file "name" from all directories in 'runtimepath'. 411 * "name" can contain wildcards. 412 * When "flags" has DIP_ALL: source all files, otherwise only the first one. 413 * 414 * return FAIL when no file could be sourced, OK otherwise. 415 */ 416 int 417 source_runtime(char_u *name, int flags) 418 { 419 return source_in_path(p_rtp, name, flags, NULL); 420 } 421 422 /* 423 * Just like source_runtime(), but use "path" instead of 'runtimepath'. 424 */ 425 int 426 source_in_path(char_u *path, char_u *name, int flags, int *ret_sid) 427 { 428 return do_in_path_and_pp(path, name, flags, source_callback, ret_sid); 429 } 430 431 432 #if defined(FEAT_EVAL) || defined(PROTO) 433 434 /* 435 * Expand wildcards in "pat" and invoke do_source() for each match. 436 */ 437 static void 438 source_all_matches(char_u *pat) 439 { 440 int num_files; 441 char_u **files; 442 int i; 443 444 if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) == OK) 445 { 446 for (i = 0; i < num_files; ++i) 447 (void)do_source(files[i], FALSE, DOSO_NONE, NULL); 448 FreeWild(num_files, files); 449 } 450 } 451 452 /* 453 * Add the package directory to 'runtimepath'. 454 */ 455 static int 456 add_pack_dir_to_rtp(char_u *fname) 457 { 458 char_u *p4, *p3, *p2, *p1, *p; 459 char_u *entry; 460 char_u *insp = NULL; 461 int c; 462 char_u *new_rtp; 463 int keep; 464 size_t oldlen; 465 size_t addlen; 466 size_t new_rtp_len; 467 char_u *afterdir = NULL; 468 size_t afterlen = 0; 469 char_u *after_insp = NULL; 470 char_u *ffname = NULL; 471 size_t fname_len; 472 char_u *buf = NULL; 473 char_u *rtp_ffname; 474 int match; 475 int retval = FAIL; 476 477 p4 = p3 = p2 = p1 = get_past_head(fname); 478 for (p = p1; *p; MB_PTR_ADV(p)) 479 if (vim_ispathsep_nocolon(*p)) 480 { 481 p4 = p3; p3 = p2; p2 = p1; p1 = p; 482 } 483 484 // now we have: 485 // rtp/pack/name/start/name 486 // p4 p3 p2 p1 487 // 488 // find the part up to "pack" in 'runtimepath' 489 c = *++p4; // append pathsep in order to expand symlink 490 *p4 = NUL; 491 ffname = fix_fname(fname); 492 *p4 = c; 493 if (ffname == NULL) 494 return FAIL; 495 496 // Find "ffname" in "p_rtp", ignoring '/' vs '\' differences. 497 // Also stop at the first "after" directory. 498 fname_len = STRLEN(ffname); 499 buf = alloc(MAXPATHL); 500 if (buf == NULL) 501 goto theend; 502 for (entry = p_rtp; *entry != NUL; ) 503 { 504 char_u *cur_entry = entry; 505 506 copy_option_part(&entry, buf, MAXPATHL, ","); 507 if (insp == NULL) 508 { 509 add_pathsep(buf); 510 rtp_ffname = fix_fname(buf); 511 if (rtp_ffname == NULL) 512 goto theend; 513 match = vim_fnamencmp(rtp_ffname, ffname, fname_len) == 0; 514 vim_free(rtp_ffname); 515 if (match) 516 // Insert "ffname" after this entry (and comma). 517 insp = entry; 518 } 519 520 if ((p = (char_u *)strstr((char *)buf, "after")) != NULL 521 && p > buf 522 && vim_ispathsep(p[-1]) 523 && (vim_ispathsep(p[5]) || p[5] == NUL || p[5] == ',')) 524 { 525 if (insp == NULL) 526 // Did not find "ffname" before the first "after" directory, 527 // insert it before this entry. 528 insp = cur_entry; 529 after_insp = cur_entry; 530 break; 531 } 532 } 533 534 if (insp == NULL) 535 // Both "fname" and "after" not found, append at the end. 536 insp = p_rtp + STRLEN(p_rtp); 537 538 // check if rtp/pack/name/start/name/after exists 539 afterdir = concat_fnames(fname, (char_u *)"after", TRUE); 540 if (afterdir != NULL && mch_isdir(afterdir)) 541 afterlen = STRLEN(afterdir) + 1; // add one for comma 542 543 oldlen = STRLEN(p_rtp); 544 addlen = STRLEN(fname) + 1; // add one for comma 545 new_rtp = alloc(oldlen + addlen + afterlen + 1); // add one for NUL 546 if (new_rtp == NULL) 547 goto theend; 548 549 // We now have 'rtp' parts: {keep}{keep_after}{rest}. 550 // Create new_rtp, first: {keep},{fname} 551 keep = (int)(insp - p_rtp); 552 mch_memmove(new_rtp, p_rtp, keep); 553 new_rtp_len = keep; 554 if (*insp == NUL) 555 new_rtp[new_rtp_len++] = ','; // add comma before 556 mch_memmove(new_rtp + new_rtp_len, fname, addlen - 1); 557 new_rtp_len += addlen - 1; 558 if (*insp != NUL) 559 new_rtp[new_rtp_len++] = ','; // add comma after 560 561 if (afterlen > 0 && after_insp != NULL) 562 { 563 int keep_after = (int)(after_insp - p_rtp); 564 565 // Add to new_rtp: {keep},{fname}{keep_after},{afterdir} 566 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep, 567 keep_after - keep); 568 new_rtp_len += keep_after - keep; 569 mch_memmove(new_rtp + new_rtp_len, afterdir, afterlen - 1); 570 new_rtp_len += afterlen - 1; 571 new_rtp[new_rtp_len++] = ','; 572 keep = keep_after; 573 } 574 575 if (p_rtp[keep] != NUL) 576 // Append rest: {keep},{fname}{keep_after},{afterdir}{rest} 577 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep, oldlen - keep + 1); 578 else 579 new_rtp[new_rtp_len] = NUL; 580 581 if (afterlen > 0 && after_insp == NULL) 582 { 583 // Append afterdir when "after" was not found: 584 // {keep},{fname}{rest},{afterdir} 585 STRCAT(new_rtp, ","); 586 STRCAT(new_rtp, afterdir); 587 } 588 589 set_option_value((char_u *)"rtp", 0L, new_rtp, 0); 590 vim_free(new_rtp); 591 retval = OK; 592 593 theend: 594 vim_free(buf); 595 vim_free(ffname); 596 vim_free(afterdir); 597 return retval; 598 } 599 600 /* 601 * Load scripts in "plugin" and "ftdetect" directories of the package. 602 */ 603 static int 604 load_pack_plugin(char_u *fname) 605 { 606 static char *plugpat = "%s/plugin/**/*.vim"; 607 static char *ftpat = "%s/ftdetect/*.vim"; 608 int len; 609 char_u *ffname = fix_fname(fname); 610 char_u *pat = NULL; 611 int retval = FAIL; 612 613 if (ffname == NULL) 614 return FAIL; 615 len = (int)STRLEN(ffname) + (int)STRLEN(ftpat); 616 pat = alloc(len); 617 if (pat == NULL) 618 goto theend; 619 vim_snprintf((char *)pat, len, plugpat, ffname); 620 source_all_matches(pat); 621 622 { 623 char_u *cmd = vim_strsave((char_u *)"g:did_load_filetypes"); 624 625 // If runtime/filetype.vim wasn't loaded yet, the scripts will be 626 // found when it loads. 627 if (cmd != NULL && eval_to_number(cmd) > 0) 628 { 629 do_cmdline_cmd((char_u *)"augroup filetypedetect"); 630 vim_snprintf((char *)pat, len, ftpat, ffname); 631 source_all_matches(pat); 632 do_cmdline_cmd((char_u *)"augroup END"); 633 } 634 vim_free(cmd); 635 } 636 vim_free(pat); 637 retval = OK; 638 639 theend: 640 vim_free(ffname); 641 return retval; 642 } 643 644 // used for "cookie" of add_pack_plugin() 645 static int APP_ADD_DIR; 646 static int APP_LOAD; 647 static int APP_BOTH; 648 649 static void 650 add_pack_plugin(char_u *fname, void *cookie) 651 { 652 if (cookie != &APP_LOAD) 653 { 654 char_u *buf = alloc(MAXPATHL); 655 char_u *p; 656 int found = FALSE; 657 658 if (buf == NULL) 659 return; 660 p = p_rtp; 661 while (*p != NUL) 662 { 663 copy_option_part(&p, buf, MAXPATHL, ","); 664 if (pathcmp((char *)buf, (char *)fname, -1) == 0) 665 { 666 found = TRUE; 667 break; 668 } 669 } 670 vim_free(buf); 671 if (!found) 672 // directory is not yet in 'runtimepath', add it 673 if (add_pack_dir_to_rtp(fname) == FAIL) 674 return; 675 } 676 677 if (cookie != &APP_ADD_DIR) 678 load_pack_plugin(fname); 679 } 680 681 /* 682 * Add all packages in the "start" directory to 'runtimepath'. 683 */ 684 void 685 add_pack_start_dirs(void) 686 { 687 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR, 688 add_pack_plugin, &APP_ADD_DIR); 689 } 690 691 /* 692 * Load plugins from all packages in the "start" directory. 693 */ 694 void 695 load_start_packages(void) 696 { 697 did_source_packages = TRUE; 698 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR, 699 add_pack_plugin, &APP_LOAD); 700 } 701 702 /* 703 * ":packloadall" 704 * Find plugins in the package directories and source them. 705 */ 706 void 707 ex_packloadall(exarg_T *eap) 708 { 709 if (!did_source_packages || eap->forceit) 710 { 711 // First do a round to add all directories to 'runtimepath', then load 712 // the plugins. This allows for plugins to use an autoload directory 713 // of another plugin. 714 add_pack_start_dirs(); 715 load_start_packages(); 716 } 717 } 718 719 /* 720 * ":packadd[!] {name}" 721 */ 722 void 723 ex_packadd(exarg_T *eap) 724 { 725 static char *plugpat = "pack/*/%s/%s"; 726 int len; 727 char *pat; 728 int round; 729 int res = OK; 730 731 // Round 1: use "start", round 2: use "opt". 732 for (round = 1; round <= 2; ++round) 733 { 734 // Only look under "start" when loading packages wasn't done yet. 735 if (round == 1 && did_source_packages) 736 continue; 737 738 len = (int)STRLEN(plugpat) + (int)STRLEN(eap->arg) + 5; 739 pat = alloc(len); 740 if (pat == NULL) 741 return; 742 vim_snprintf(pat, len, plugpat, round == 1 ? "start" : "opt", eap->arg); 743 // The first round don't give a "not found" error, in the second round 744 // only when nothing was found in the first round. 745 res = do_in_path(p_pp, (char_u *)pat, 746 DIP_ALL + DIP_DIR + (round == 2 && res == FAIL ? DIP_ERR : 0), 747 add_pack_plugin, eap->forceit ? &APP_ADD_DIR : &APP_BOTH); 748 vim_free(pat); 749 } 750 } 751 #endif 752 753 /* 754 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a 755 * list of file names in allocated memory. 756 */ 757 void 758 remove_duplicates(garray_T *gap) 759 { 760 int i; 761 int j; 762 char_u **fnames = (char_u **)gap->ga_data; 763 764 sort_strings(fnames, gap->ga_len); 765 for (i = gap->ga_len - 1; i > 0; --i) 766 if (fnamecmp(fnames[i - 1], fnames[i]) == 0) 767 { 768 vim_free(fnames[i]); 769 for (j = i + 1; j < gap->ga_len; ++j) 770 fnames[j - 1] = fnames[j]; 771 --gap->ga_len; 772 } 773 } 774 775 /* 776 * Expand color scheme, compiler or filetype names. 777 * Search from 'runtimepath': 778 * 'runtimepath'/{dirnames}/{pat}.vim 779 * When "flags" has DIP_START: search also from 'start' of 'packpath': 780 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim 781 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath': 782 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim 783 * "dirnames" is an array with one or more directory names. 784 */ 785 int 786 ExpandRTDir( 787 char_u *pat, 788 int flags, 789 int *num_file, 790 char_u ***file, 791 char *dirnames[]) 792 { 793 char_u *s; 794 char_u *e; 795 char_u *match; 796 garray_T ga; 797 int i; 798 int pat_len; 799 800 *num_file = 0; 801 *file = NULL; 802 pat_len = (int)STRLEN(pat); 803 ga_init2(&ga, (int)sizeof(char *), 10); 804 805 for (i = 0; dirnames[i] != NULL; ++i) 806 { 807 s = alloc(STRLEN(dirnames[i]) + pat_len + 7); 808 if (s == NULL) 809 { 810 ga_clear_strings(&ga); 811 return FAIL; 812 } 813 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat); 814 globpath(p_rtp, s, &ga, 0); 815 vim_free(s); 816 } 817 818 if (flags & DIP_START) { 819 for (i = 0; dirnames[i] != NULL; ++i) 820 { 821 s = alloc(STRLEN(dirnames[i]) + pat_len + 22); 822 if (s == NULL) 823 { 824 ga_clear_strings(&ga); 825 return FAIL; 826 } 827 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat); 828 globpath(p_pp, s, &ga, 0); 829 vim_free(s); 830 } 831 } 832 833 if (flags & DIP_OPT) { 834 for (i = 0; dirnames[i] != NULL; ++i) 835 { 836 s = alloc(STRLEN(dirnames[i]) + pat_len + 20); 837 if (s == NULL) 838 { 839 ga_clear_strings(&ga); 840 return FAIL; 841 } 842 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat); 843 globpath(p_pp, s, &ga, 0); 844 vim_free(s); 845 } 846 } 847 848 for (i = 0; i < ga.ga_len; ++i) 849 { 850 match = ((char_u **)ga.ga_data)[i]; 851 s = match; 852 e = s + STRLEN(s); 853 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0) 854 { 855 e -= 4; 856 for (s = e; s > match; MB_PTR_BACK(match, s)) 857 if (s < match || vim_ispathsep(*s)) 858 break; 859 ++s; 860 *e = NUL; 861 mch_memmove(match, s, e - s + 1); 862 } 863 } 864 865 if (ga.ga_len == 0) 866 return FAIL; 867 868 // Sort and remove duplicates which can happen when specifying multiple 869 // directories in dirnames. 870 remove_duplicates(&ga); 871 872 *file = ga.ga_data; 873 *num_file = ga.ga_len; 874 return OK; 875 } 876 877 /* 878 * Expand loadplugin names: 879 * 'packpath'/pack/ * /opt/{pat} 880 */ 881 int 882 ExpandPackAddDir( 883 char_u *pat, 884 int *num_file, 885 char_u ***file) 886 { 887 char_u *s; 888 char_u *e; 889 char_u *match; 890 garray_T ga; 891 int i; 892 int pat_len; 893 894 *num_file = 0; 895 *file = NULL; 896 pat_len = (int)STRLEN(pat); 897 ga_init2(&ga, (int)sizeof(char *), 10); 898 899 s = alloc(pat_len + 26); 900 if (s == NULL) 901 { 902 ga_clear_strings(&ga); 903 return FAIL; 904 } 905 sprintf((char *)s, "pack/*/opt/%s*", pat); 906 globpath(p_pp, s, &ga, 0); 907 vim_free(s); 908 909 for (i = 0; i < ga.ga_len; ++i) 910 { 911 match = ((char_u **)ga.ga_data)[i]; 912 s = gettail(match); 913 e = s + STRLEN(s); 914 mch_memmove(match, s, e - s + 1); 915 } 916 917 if (ga.ga_len == 0) 918 return FAIL; 919 920 // Sort and remove duplicates which can happen when specifying multiple 921 // directories in dirnames. 922 remove_duplicates(&ga); 923 924 *file = ga.ga_data; 925 *num_file = ga.ga_len; 926 return OK; 927 } 928 929 static void 930 cmd_source(char_u *fname, exarg_T *eap) 931 { 932 if (*fname == NUL) 933 emsg(_(e_argreq)); 934 935 else if (eap != NULL && eap->forceit) 936 // ":source!": read Normal mode commands 937 // Need to execute the commands directly. This is required at least 938 // for: 939 // - ":g" command busy 940 // - after ":argdo", ":windo" or ":bufdo" 941 // - another command follows 942 // - inside a loop 943 openscript(fname, global_busy || listcmd_busy || eap->nextcmd != NULL 944 #ifdef FEAT_EVAL 945 || eap->cstack->cs_idx >= 0 946 #endif 947 ); 948 949 // ":source" read ex commands 950 else if (do_source(fname, FALSE, DOSO_NONE, NULL) == FAIL) 951 semsg(_(e_notopen), fname); 952 } 953 954 /* 955 * ":source {fname}" 956 */ 957 void 958 ex_source(exarg_T *eap) 959 { 960 #ifdef FEAT_BROWSE 961 if (cmdmod.browse) 962 { 963 char_u *fname = NULL; 964 965 fname = do_browse(0, (char_u *)_("Source Vim script"), eap->arg, 966 NULL, NULL, 967 (char_u *)_(BROWSE_FILTER_MACROS), NULL); 968 if (fname != NULL) 969 { 970 cmd_source(fname, eap); 971 vim_free(fname); 972 } 973 } 974 else 975 #endif 976 cmd_source(eap->arg, eap); 977 } 978 979 #if defined(FEAT_EVAL) || defined(PROTO) 980 /* 981 * ":options" 982 */ 983 void 984 ex_options( 985 exarg_T *eap UNUSED) 986 { 987 char_u buf[500]; 988 int multi_mods = 0; 989 990 buf[0] = NUL; 991 (void)add_win_cmd_modifers(buf, &multi_mods); 992 993 vim_setenv((char_u *)"OPTWIN_CMD", buf); 994 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL); 995 } 996 #endif 997 998 /* 999 * ":source" and associated commands. 1000 */ 1001 /* 1002 * Structure used to store info for each sourced file. 1003 * It is shared between do_source() and getsourceline(). 1004 * This is required, because it needs to be handed to do_cmdline() and 1005 * sourcing can be done recursively. 1006 */ 1007 struct source_cookie 1008 { 1009 FILE *fp; // opened file for sourcing 1010 char_u *nextline; // if not NULL: line that was read ahead 1011 linenr_T sourcing_lnum; // line number of the source file 1012 int finished; // ":finish" used 1013 #ifdef USE_CRNL 1014 int fileformat; // EOL_UNKNOWN, EOL_UNIX or EOL_DOS 1015 int error; // TRUE if LF found after CR-LF 1016 #endif 1017 #ifdef FEAT_EVAL 1018 linenr_T breakpoint; // next line with breakpoint or zero 1019 char_u *fname; // name of sourced file 1020 int dbg_tick; // debug_tick when breakpoint was set 1021 int level; // top nesting level of sourced file 1022 #endif 1023 vimconv_T conv; // type of conversion 1024 }; 1025 1026 #ifdef FEAT_EVAL 1027 /* 1028 * Return the address holding the next breakpoint line for a source cookie. 1029 */ 1030 linenr_T * 1031 source_breakpoint(void *cookie) 1032 { 1033 return &((struct source_cookie *)cookie)->breakpoint; 1034 } 1035 1036 /* 1037 * Return the address holding the debug tick for a source cookie. 1038 */ 1039 int * 1040 source_dbg_tick(void *cookie) 1041 { 1042 return &((struct source_cookie *)cookie)->dbg_tick; 1043 } 1044 1045 /* 1046 * Return the nesting level for a source cookie. 1047 */ 1048 int 1049 source_level(void *cookie) 1050 { 1051 return ((struct source_cookie *)cookie)->level; 1052 } 1053 #endif 1054 1055 #if (defined(MSWIN) && defined(FEAT_CSCOPE)) || defined(HAVE_FD_CLOEXEC) 1056 # define USE_FOPEN_NOINH 1057 /* 1058 * Special function to open a file without handle inheritance. 1059 * When possible the handle is closed on exec(). 1060 */ 1061 static FILE * 1062 fopen_noinh_readbin(char *filename) 1063 { 1064 # ifdef MSWIN 1065 int fd_tmp = mch_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0); 1066 # else 1067 int fd_tmp = mch_open(filename, O_RDONLY, 0); 1068 # endif 1069 1070 if (fd_tmp == -1) 1071 return NULL; 1072 1073 # ifdef HAVE_FD_CLOEXEC 1074 { 1075 int fdflags = fcntl(fd_tmp, F_GETFD); 1076 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0) 1077 (void)fcntl(fd_tmp, F_SETFD, fdflags | FD_CLOEXEC); 1078 } 1079 # endif 1080 1081 return fdopen(fd_tmp, READBIN); 1082 } 1083 #endif 1084 1085 /* 1086 * do_source: Read the file "fname" and execute its lines as EX commands. 1087 * When "ret_sid" is not NULL and we loaded the script before, don't load it 1088 * again. 1089 * 1090 * This function may be called recursively! 1091 * 1092 * Return FAIL if file could not be opened, OK otherwise. 1093 * If a scriptitem_T was found or created "*ret_sid" is set to the SID. 1094 */ 1095 int 1096 do_source( 1097 char_u *fname, 1098 int check_other, // check for .vimrc and _vimrc 1099 int is_vimrc, // DOSO_ value 1100 int *ret_sid UNUSED) 1101 { 1102 struct source_cookie cookie; 1103 char_u *p; 1104 char_u *fname_exp; 1105 char_u *firstline = NULL; 1106 int retval = FAIL; 1107 #ifdef FEAT_EVAL 1108 sctx_T save_current_sctx; 1109 static scid_T last_current_SID = 0; 1110 static int last_current_SID_seq = 0; 1111 funccal_entry_T funccalp_entry; 1112 int save_debug_break_level = debug_break_level; 1113 int sid; 1114 scriptitem_T *si = NULL; 1115 #endif 1116 #ifdef STARTUPTIME 1117 struct timeval tv_rel; 1118 struct timeval tv_start; 1119 #endif 1120 #ifdef FEAT_PROFILE 1121 proftime_T wait_start; 1122 #endif 1123 int trigger_source_post = FALSE; 1124 ESTACK_CHECK_DECLARATION 1125 1126 p = expand_env_save(fname); 1127 if (p == NULL) 1128 return retval; 1129 fname_exp = fix_fname(p); 1130 vim_free(p); 1131 if (fname_exp == NULL) 1132 return retval; 1133 if (mch_isdir(fname_exp)) 1134 { 1135 smsg(_("Cannot source a directory: \"%s\""), fname); 1136 goto theend; 1137 } 1138 1139 #ifdef FEAT_EVAL 1140 // See if we loaded this script before. 1141 for (sid = script_items.ga_len; sid > 0; --sid) 1142 { 1143 // We used to check inode here, but that doesn't work: 1144 // - If a script is edited and written, it may get a different 1145 // inode number, even though to the user it is the same script. 1146 // - If a script is deleted and another script is written, with a 1147 // different name, the inode may be re-used. 1148 si = SCRIPT_ITEM(sid); 1149 if (si->sn_name != NULL && fnamecmp(si->sn_name, fname_exp) == 0) 1150 // Found it! 1151 break; 1152 } 1153 if (sid > 0 && ret_sid != NULL) 1154 { 1155 // Already loaded and no need to load again, return here. 1156 *ret_sid = sid; 1157 retval = OK; 1158 goto theend; 1159 } 1160 #endif 1161 1162 // Apply SourceCmd autocommands, they should get the file and source it. 1163 if (has_autocmd(EVENT_SOURCECMD, fname_exp, NULL) 1164 && apply_autocmds(EVENT_SOURCECMD, fname_exp, fname_exp, 1165 FALSE, curbuf)) 1166 { 1167 #ifdef FEAT_EVAL 1168 retval = aborting() ? FAIL : OK; 1169 #else 1170 retval = OK; 1171 #endif 1172 if (retval == OK) 1173 // Apply SourcePost autocommands. 1174 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp, 1175 FALSE, curbuf); 1176 goto theend; 1177 } 1178 1179 // Apply SourcePre autocommands, they may get the file. 1180 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf); 1181 1182 #ifdef USE_FOPEN_NOINH 1183 cookie.fp = fopen_noinh_readbin((char *)fname_exp); 1184 #else 1185 cookie.fp = mch_fopen((char *)fname_exp, READBIN); 1186 #endif 1187 if (cookie.fp == NULL && check_other) 1188 { 1189 // Try again, replacing file name ".vimrc" by "_vimrc" or vice versa, 1190 // and ".exrc" by "_exrc" or vice versa. 1191 p = gettail(fname_exp); 1192 if ((*p == '.' || *p == '_') 1193 && (STRICMP(p + 1, "vimrc") == 0 1194 || STRICMP(p + 1, "gvimrc") == 0 1195 || STRICMP(p + 1, "exrc") == 0)) 1196 { 1197 if (*p == '_') 1198 *p = '.'; 1199 else 1200 *p = '_'; 1201 #ifdef USE_FOPEN_NOINH 1202 cookie.fp = fopen_noinh_readbin((char *)fname_exp); 1203 #else 1204 cookie.fp = mch_fopen((char *)fname_exp, READBIN); 1205 #endif 1206 } 1207 } 1208 1209 if (cookie.fp == NULL) 1210 { 1211 if (p_verbose > 0) 1212 { 1213 verbose_enter(); 1214 if (SOURCING_NAME == NULL) 1215 smsg(_("could not source \"%s\""), fname); 1216 else 1217 smsg(_("line %ld: could not source \"%s\""), 1218 SOURCING_LNUM, fname); 1219 verbose_leave(); 1220 } 1221 goto theend; 1222 } 1223 1224 // The file exists. 1225 // - In verbose mode, give a message. 1226 // - For a vimrc file, may want to set 'compatible', call vimrc_found(). 1227 if (p_verbose > 1) 1228 { 1229 verbose_enter(); 1230 if (SOURCING_NAME == NULL) 1231 smsg(_("sourcing \"%s\""), fname); 1232 else 1233 smsg(_("line %ld: sourcing \"%s\""), SOURCING_LNUM, fname); 1234 verbose_leave(); 1235 } 1236 if (is_vimrc == DOSO_VIMRC) 1237 vimrc_found(fname_exp, (char_u *)"MYVIMRC"); 1238 else if (is_vimrc == DOSO_GVIMRC) 1239 vimrc_found(fname_exp, (char_u *)"MYGVIMRC"); 1240 1241 #ifdef USE_CRNL 1242 // If no automatic file format: Set default to CR-NL. 1243 if (*p_ffs == NUL) 1244 cookie.fileformat = EOL_DOS; 1245 else 1246 cookie.fileformat = EOL_UNKNOWN; 1247 cookie.error = FALSE; 1248 #endif 1249 1250 cookie.nextline = NULL; 1251 cookie.sourcing_lnum = 0; 1252 cookie.finished = FALSE; 1253 1254 #ifdef FEAT_EVAL 1255 // Check if this script has a breakpoint. 1256 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0); 1257 cookie.fname = fname_exp; 1258 cookie.dbg_tick = debug_tick; 1259 1260 cookie.level = ex_nesting_level; 1261 #endif 1262 1263 // Keep the sourcing name/lnum, for recursive calls. 1264 estack_push(ETYPE_SCRIPT, fname_exp, 0); 1265 ESTACK_CHECK_SETUP 1266 1267 #ifdef STARTUPTIME 1268 if (time_fd != NULL) 1269 time_push(&tv_rel, &tv_start); 1270 #endif 1271 1272 #ifdef FEAT_EVAL 1273 # ifdef FEAT_PROFILE 1274 if (do_profiling == PROF_YES) 1275 prof_child_enter(&wait_start); // entering a child now 1276 # endif 1277 1278 // Don't use local function variables, if called from a function. 1279 // Also starts profiling timer for nested script. 1280 save_funccal(&funccalp_entry); 1281 1282 save_current_sctx = current_sctx; 1283 current_sctx.sc_lnum = 0; 1284 current_sctx.sc_version = 1; // default script version 1285 1286 // Check if this script was sourced before to finds its SID. 1287 // Always use a new sequence number. 1288 current_sctx.sc_seq = ++last_current_SID_seq; 1289 if (sid > 0) 1290 { 1291 hashtab_T *ht; 1292 int is_vim9 = si->sn_version == SCRIPT_VERSION_VIM9; 1293 1294 // loading the same script again 1295 si->sn_had_command = FALSE; 1296 si->sn_version = 1; 1297 current_sctx.sc_sid = sid; 1298 1299 // In Vim9 script all script-local variables are removed when reloading 1300 // the same script. In legacy script they remain but "const" can be 1301 // set again. 1302 ht = &SCRIPT_VARS(sid); 1303 if (is_vim9) 1304 hashtab_free_contents(ht); 1305 else 1306 { 1307 int todo = (int)ht->ht_used; 1308 hashitem_T *hi; 1309 dictitem_T *di; 1310 1311 for (hi = ht->ht_array; todo > 0; ++hi) 1312 if (!HASHITEM_EMPTY(hi)) 1313 { 1314 --todo; 1315 di = HI2DI(hi); 1316 di->di_flags |= DI_FLAGS_RELOAD; 1317 } 1318 } 1319 1320 // old imports are no longer valid 1321 free_imports(sid); 1322 1323 // in Vim9 script functions are marked deleted 1324 if (is_vim9) 1325 delete_script_functions(sid); 1326 } 1327 else 1328 { 1329 // It's new, generate a new SID. 1330 current_sctx.sc_sid = ++last_current_SID; 1331 if (ga_grow(&script_items, 1332 (int)(current_sctx.sc_sid - script_items.ga_len)) == FAIL) 1333 goto almosttheend; 1334 while (script_items.ga_len < current_sctx.sc_sid) 1335 { 1336 si = ALLOC_CLEAR_ONE(scriptitem_T); 1337 if (si == NULL) 1338 goto almosttheend; 1339 ++script_items.ga_len; 1340 SCRIPT_ITEM(script_items.ga_len) = si; 1341 si->sn_name = NULL; 1342 si->sn_version = 1; 1343 1344 // Allocate the local script variables to use for this script. 1345 new_script_vars(script_items.ga_len); 1346 ga_init2(&si->sn_var_vals, sizeof(svar_T), 10); 1347 ga_init2(&si->sn_imports, sizeof(imported_T), 10); 1348 ga_init2(&si->sn_type_list, sizeof(type_T), 10); 1349 # ifdef FEAT_PROFILE 1350 si->sn_prof_on = FALSE; 1351 # endif 1352 } 1353 si = SCRIPT_ITEM(current_sctx.sc_sid); 1354 si->sn_name = fname_exp; 1355 fname_exp = vim_strsave(si->sn_name); // used for autocmd 1356 if (ret_sid != NULL) 1357 *ret_sid = current_sctx.sc_sid; 1358 } 1359 1360 # ifdef FEAT_PROFILE 1361 if (do_profiling == PROF_YES) 1362 { 1363 int forceit; 1364 1365 // Check if we do profiling for this script. 1366 if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit)) 1367 { 1368 script_do_profile(si); 1369 si->sn_pr_force = forceit; 1370 } 1371 if (si->sn_prof_on) 1372 { 1373 ++si->sn_pr_count; 1374 profile_start(&si->sn_pr_start); 1375 profile_zero(&si->sn_pr_children); 1376 } 1377 } 1378 # endif 1379 #endif 1380 1381 cookie.conv.vc_type = CONV_NONE; // no conversion 1382 1383 // Read the first line so we can check for a UTF-8 BOM. 1384 firstline = getsourceline(0, (void *)&cookie, 0, TRUE); 1385 if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef 1386 && firstline[1] == 0xbb && firstline[2] == 0xbf) 1387 { 1388 // Found BOM; setup conversion, skip over BOM and recode the line. 1389 convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc); 1390 p = string_convert(&cookie.conv, firstline + 3, NULL); 1391 if (p == NULL) 1392 p = vim_strsave(firstline + 3); 1393 if (p != NULL) 1394 { 1395 vim_free(firstline); 1396 firstline = p; 1397 } 1398 } 1399 1400 // Call do_cmdline, which will call getsourceline() to get the lines. 1401 do_cmdline(firstline, getsourceline, (void *)&cookie, 1402 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT); 1403 retval = OK; 1404 1405 #ifdef FEAT_PROFILE 1406 if (do_profiling == PROF_YES) 1407 { 1408 // Get "si" again, "script_items" may have been reallocated. 1409 si = SCRIPT_ITEM(current_sctx.sc_sid); 1410 if (si->sn_prof_on) 1411 { 1412 profile_end(&si->sn_pr_start); 1413 profile_sub_wait(&wait_start, &si->sn_pr_start); 1414 profile_add(&si->sn_pr_total, &si->sn_pr_start); 1415 profile_self(&si->sn_pr_self, &si->sn_pr_start, 1416 &si->sn_pr_children); 1417 } 1418 } 1419 #endif 1420 1421 if (got_int) 1422 emsg(_(e_interr)); 1423 ESTACK_CHECK_NOW 1424 estack_pop(); 1425 if (p_verbose > 1) 1426 { 1427 verbose_enter(); 1428 smsg(_("finished sourcing %s"), fname); 1429 if (SOURCING_NAME != NULL) 1430 smsg(_("continuing in %s"), SOURCING_NAME); 1431 verbose_leave(); 1432 } 1433 #ifdef STARTUPTIME 1434 if (time_fd != NULL) 1435 { 1436 vim_snprintf((char *)IObuff, IOSIZE, "sourcing %s", fname); 1437 time_msg((char *)IObuff, &tv_start); 1438 time_pop(&tv_rel); 1439 } 1440 #endif 1441 1442 if (!got_int) 1443 trigger_source_post = TRUE; 1444 1445 #ifdef FEAT_EVAL 1446 // After a "finish" in debug mode, need to break at first command of next 1447 // sourced file. 1448 if (save_debug_break_level > ex_nesting_level 1449 && debug_break_level == ex_nesting_level) 1450 ++debug_break_level; 1451 #endif 1452 1453 #ifdef FEAT_EVAL 1454 almosttheend: 1455 // Get "si" again, "script_items" may have been reallocated. 1456 si = SCRIPT_ITEM(current_sctx.sc_sid); 1457 if (si->sn_save_cpo != NULL) 1458 { 1459 free_string_option(p_cpo); 1460 p_cpo = si->sn_save_cpo; 1461 si->sn_save_cpo = NULL; 1462 } 1463 1464 current_sctx = save_current_sctx; 1465 restore_funccal(); 1466 # ifdef FEAT_PROFILE 1467 if (do_profiling == PROF_YES) 1468 prof_child_exit(&wait_start); // leaving a child now 1469 # endif 1470 #endif 1471 fclose(cookie.fp); 1472 vim_free(cookie.nextline); 1473 vim_free(firstline); 1474 convert_setup(&cookie.conv, NULL, NULL); 1475 1476 if (trigger_source_post) 1477 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp, FALSE, curbuf); 1478 1479 theend: 1480 vim_free(fname_exp); 1481 return retval; 1482 } 1483 1484 #if defined(FEAT_EVAL) || defined(PROTO) 1485 1486 /* 1487 * ":scriptnames" 1488 */ 1489 void 1490 ex_scriptnames(exarg_T *eap) 1491 { 1492 int i; 1493 1494 if (eap->addr_count > 0) 1495 { 1496 // :script {scriptId}: edit the script 1497 if (eap->line2 < 1 || eap->line2 > script_items.ga_len) 1498 emsg(_(e_invarg)); 1499 else 1500 { 1501 eap->arg = SCRIPT_ITEM(eap->line2)->sn_name; 1502 do_exedit(eap, NULL); 1503 } 1504 return; 1505 } 1506 1507 for (i = 1; i <= script_items.ga_len && !got_int; ++i) 1508 if (SCRIPT_ITEM(i)->sn_name != NULL) 1509 { 1510 home_replace(NULL, SCRIPT_ITEM(i)->sn_name, 1511 NameBuff, MAXPATHL, TRUE); 1512 smsg("%3d: %s", i, NameBuff); 1513 } 1514 } 1515 1516 # if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) 1517 /* 1518 * Fix slashes in the list of script names for 'shellslash'. 1519 */ 1520 void 1521 scriptnames_slash_adjust(void) 1522 { 1523 int i; 1524 1525 for (i = 1; i <= script_items.ga_len; ++i) 1526 if (SCRIPT_ITEM(i)->sn_name != NULL) 1527 slash_adjust(SCRIPT_ITEM(i)->sn_name); 1528 } 1529 # endif 1530 1531 /* 1532 * Get a pointer to a script name. Used for ":verbose set". 1533 */ 1534 char_u * 1535 get_scriptname(scid_T id) 1536 { 1537 if (id == SID_MODELINE) 1538 return (char_u *)_("modeline"); 1539 if (id == SID_CMDARG) 1540 return (char_u *)_("--cmd argument"); 1541 if (id == SID_CARG) 1542 return (char_u *)_("-c argument"); 1543 if (id == SID_ENV) 1544 return (char_u *)_("environment variable"); 1545 if (id == SID_ERROR) 1546 return (char_u *)_("error handler"); 1547 return SCRIPT_ITEM(id)->sn_name; 1548 } 1549 1550 # if defined(EXITFREE) || defined(PROTO) 1551 void 1552 free_scriptnames(void) 1553 { 1554 int i; 1555 1556 for (i = script_items.ga_len; i > 0; --i) 1557 { 1558 scriptitem_T *si = SCRIPT_ITEM(i); 1559 1560 // the variables themselves are cleared in evalvars_clear() 1561 vim_free(si->sn_vars); 1562 1563 vim_free(si->sn_name); 1564 free_imports(i); 1565 free_string_option(si->sn_save_cpo); 1566 # ifdef FEAT_PROFILE 1567 ga_clear(&si->sn_prl_ga); 1568 # endif 1569 vim_free(si); 1570 } 1571 ga_clear(&script_items); 1572 } 1573 1574 void 1575 free_autoload_scriptnames(void) 1576 { 1577 ga_clear_strings(&ga_loaded); 1578 } 1579 # endif 1580 1581 #endif 1582 1583 linenr_T 1584 get_sourced_lnum(char_u *(*fgetline)(int, void *, int, int), void *cookie) 1585 { 1586 return fgetline == getsourceline 1587 ? ((struct source_cookie *)cookie)->sourcing_lnum 1588 : SOURCING_LNUM; 1589 } 1590 1591 static char_u * 1592 get_one_sourceline(struct source_cookie *sp) 1593 { 1594 garray_T ga; 1595 int len; 1596 int c; 1597 char_u *buf; 1598 #ifdef USE_CRNL 1599 int has_cr; // CR-LF found 1600 #endif 1601 int have_read = FALSE; 1602 1603 // use a growarray to store the sourced line 1604 ga_init2(&ga, 1, 250); 1605 1606 // Loop until there is a finished line (or end-of-file). 1607 ++sp->sourcing_lnum; 1608 for (;;) 1609 { 1610 // make room to read at least 120 (more) characters 1611 if (ga_grow(&ga, 120) == FAIL) 1612 break; 1613 buf = (char_u *)ga.ga_data; 1614 1615 if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len, 1616 sp->fp) == NULL) 1617 break; 1618 len = ga.ga_len + (int)STRLEN(buf + ga.ga_len); 1619 #ifdef USE_CRNL 1620 // Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the 1621 // CTRL-Z by its own, or after a NL. 1622 if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n')) 1623 && sp->fileformat == EOL_DOS 1624 && buf[len - 1] == Ctrl_Z) 1625 { 1626 buf[len - 1] = NUL; 1627 break; 1628 } 1629 #endif 1630 1631 have_read = TRUE; 1632 ga.ga_len = len; 1633 1634 // If the line was longer than the buffer, read more. 1635 if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n') 1636 continue; 1637 1638 if (len >= 1 && buf[len - 1] == '\n') // remove trailing NL 1639 { 1640 #ifdef USE_CRNL 1641 has_cr = (len >= 2 && buf[len - 2] == '\r'); 1642 if (sp->fileformat == EOL_UNKNOWN) 1643 { 1644 if (has_cr) 1645 sp->fileformat = EOL_DOS; 1646 else 1647 sp->fileformat = EOL_UNIX; 1648 } 1649 1650 if (sp->fileformat == EOL_DOS) 1651 { 1652 if (has_cr) // replace trailing CR 1653 { 1654 buf[len - 2] = '\n'; 1655 --len; 1656 --ga.ga_len; 1657 } 1658 else // lines like ":map xx yy^M" will have failed 1659 { 1660 if (!sp->error) 1661 { 1662 msg_source(HL_ATTR(HLF_W)); 1663 emsg(_("W15: Warning: Wrong line separator, ^M may be missing")); 1664 } 1665 sp->error = TRUE; 1666 sp->fileformat = EOL_UNIX; 1667 } 1668 } 1669 #endif 1670 // The '\n' is escaped if there is an odd number of ^V's just 1671 // before it, first set "c" just before the 'V's and then check 1672 // len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo 1673 for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--) 1674 ; 1675 if ((len & 1) != (c & 1)) // escaped NL, read more 1676 { 1677 ++sp->sourcing_lnum; 1678 continue; 1679 } 1680 1681 buf[len - 1] = NUL; // remove the NL 1682 } 1683 1684 // Check for ^C here now and then, so recursive :so can be broken. 1685 line_breakcheck(); 1686 break; 1687 } 1688 1689 if (have_read) 1690 return (char_u *)ga.ga_data; 1691 1692 vim_free(ga.ga_data); 1693 return NULL; 1694 } 1695 1696 /* 1697 * Get one full line from a sourced file. 1698 * Called by do_cmdline() when it's called from do_source(). 1699 * 1700 * Return a pointer to the line in allocated memory. 1701 * Return NULL for end-of-file or some error. 1702 */ 1703 char_u * 1704 getsourceline(int c UNUSED, void *cookie, int indent UNUSED, int do_concat) 1705 { 1706 struct source_cookie *sp = (struct source_cookie *)cookie; 1707 char_u *line; 1708 char_u *p; 1709 1710 #ifdef FEAT_EVAL 1711 // If breakpoints have been added/deleted need to check for it. 1712 if (sp->dbg_tick < debug_tick) 1713 { 1714 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM); 1715 sp->dbg_tick = debug_tick; 1716 } 1717 # ifdef FEAT_PROFILE 1718 if (do_profiling == PROF_YES) 1719 script_line_end(); 1720 # endif 1721 #endif 1722 1723 // Set the current sourcing line number. 1724 SOURCING_LNUM = sp->sourcing_lnum + 1; 1725 1726 // Get current line. If there is a read-ahead line, use it, otherwise get 1727 // one now. 1728 if (sp->finished) 1729 line = NULL; 1730 else if (sp->nextline == NULL) 1731 line = get_one_sourceline(sp); 1732 else 1733 { 1734 line = sp->nextline; 1735 sp->nextline = NULL; 1736 ++sp->sourcing_lnum; 1737 } 1738 #ifdef FEAT_PROFILE 1739 if (line != NULL && do_profiling == PROF_YES) 1740 script_line_start(); 1741 #endif 1742 1743 // Only concatenate lines starting with a \ when 'cpoptions' doesn't 1744 // contain the 'C' flag. 1745 if (line != NULL && do_concat && vim_strchr(p_cpo, CPO_CONCAT) == NULL) 1746 { 1747 // compensate for the one line read-ahead 1748 --sp->sourcing_lnum; 1749 1750 // Get the next line and concatenate it when it starts with a 1751 // backslash. We always need to read the next line, keep it in 1752 // sp->nextline. 1753 /* Also check for a comment in between continuation lines: "\ */ 1754 sp->nextline = get_one_sourceline(sp); 1755 if (sp->nextline != NULL 1756 && (*(p = skipwhite(sp->nextline)) == '\\' 1757 || (p[0] == '"' && p[1] == '\\' && p[2] == ' '))) 1758 { 1759 garray_T ga; 1760 1761 ga_init2(&ga, (int)sizeof(char_u), 400); 1762 ga_concat(&ga, line); 1763 if (*p == '\\') 1764 ga_concat(&ga, p + 1); 1765 for (;;) 1766 { 1767 vim_free(sp->nextline); 1768 sp->nextline = get_one_sourceline(sp); 1769 if (sp->nextline == NULL) 1770 break; 1771 p = skipwhite(sp->nextline); 1772 if (*p == '\\') 1773 { 1774 // Adjust the growsize to the current length to speed up 1775 // concatenating many lines. 1776 if (ga.ga_len > 400) 1777 { 1778 if (ga.ga_len > 8000) 1779 ga.ga_growsize = 8000; 1780 else 1781 ga.ga_growsize = ga.ga_len; 1782 } 1783 ga_concat(&ga, p + 1); 1784 } 1785 else if (p[0] != '"' || p[1] != '\\' || p[2] != ' ') 1786 break; 1787 } 1788 ga_append(&ga, NUL); 1789 vim_free(line); 1790 line = ga.ga_data; 1791 } 1792 } 1793 1794 if (line != NULL && sp->conv.vc_type != CONV_NONE) 1795 { 1796 char_u *s; 1797 1798 // Convert the encoding of the script line. 1799 s = string_convert(&sp->conv, line, NULL); 1800 if (s != NULL) 1801 { 1802 vim_free(line); 1803 line = s; 1804 } 1805 } 1806 1807 #ifdef FEAT_EVAL 1808 // Did we encounter a breakpoint? 1809 if (sp->breakpoint != 0 && sp->breakpoint <= SOURCING_LNUM) 1810 { 1811 dbg_breakpoint(sp->fname, SOURCING_LNUM); 1812 // Find next breakpoint. 1813 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM); 1814 sp->dbg_tick = debug_tick; 1815 } 1816 #endif 1817 1818 return line; 1819 } 1820 1821 /* 1822 * ":scriptencoding": Set encoding conversion for a sourced script. 1823 */ 1824 void 1825 ex_scriptencoding(exarg_T *eap) 1826 { 1827 struct source_cookie *sp; 1828 char_u *name; 1829 1830 if (!getline_equal(eap->getline, eap->cookie, getsourceline)) 1831 { 1832 emsg(_("E167: :scriptencoding used outside of a sourced file")); 1833 return; 1834 } 1835 1836 if (*eap->arg != NUL) 1837 { 1838 name = enc_canonize(eap->arg); 1839 if (name == NULL) // out of memory 1840 return; 1841 } 1842 else 1843 name = eap->arg; 1844 1845 // Setup for conversion from the specified encoding to 'encoding'. 1846 sp = (struct source_cookie *)getline_cookie(eap->getline, eap->cookie); 1847 convert_setup(&sp->conv, name, p_enc); 1848 1849 if (name != eap->arg) 1850 vim_free(name); 1851 } 1852 1853 /* 1854 * ":scriptversion": Set Vim script version for a sourced script. 1855 */ 1856 void 1857 ex_scriptversion(exarg_T *eap UNUSED) 1858 { 1859 #ifdef FEAT_EVAL 1860 int nr; 1861 1862 if (!getline_equal(eap->getline, eap->cookie, getsourceline)) 1863 { 1864 emsg(_("E984: :scriptversion used outside of a sourced file")); 1865 return; 1866 } 1867 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9) 1868 { 1869 emsg(_("E1040: Cannot use :scriptversion after :vim9script")); 1870 return; 1871 } 1872 1873 nr = getdigits(&eap->arg); 1874 if (nr == 0 || *eap->arg != NUL) 1875 emsg(_(e_invarg)); 1876 else if (nr > 4) 1877 semsg(_("E999: scriptversion not supported: %d"), nr); 1878 else 1879 { 1880 current_sctx.sc_version = nr; 1881 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = nr; 1882 } 1883 #endif 1884 } 1885 1886 #if defined(FEAT_EVAL) || defined(PROTO) 1887 /* 1888 * ":finish": Mark a sourced file as finished. 1889 */ 1890 void 1891 ex_finish(exarg_T *eap) 1892 { 1893 if (getline_equal(eap->getline, eap->cookie, getsourceline)) 1894 do_finish(eap, FALSE); 1895 else 1896 emsg(_("E168: :finish used outside of a sourced file")); 1897 } 1898 1899 /* 1900 * Mark a sourced file as finished. Possibly makes the ":finish" pending. 1901 * Also called for a pending finish at the ":endtry" or after returning from 1902 * an extra do_cmdline(). "reanimate" is used in the latter case. 1903 */ 1904 void 1905 do_finish(exarg_T *eap, int reanimate) 1906 { 1907 int idx; 1908 1909 if (reanimate) 1910 ((struct source_cookie *)getline_cookie(eap->getline, 1911 eap->cookie))->finished = FALSE; 1912 1913 // Cleanup (and inactivate) conditionals, but stop when a try conditional 1914 // not in its finally clause (which then is to be executed next) is found. 1915 // In this case, make the ":finish" pending for execution at the ":endtry". 1916 // Otherwise, finish normally. 1917 idx = cleanup_conditionals(eap->cstack, 0, TRUE); 1918 if (idx >= 0) 1919 { 1920 eap->cstack->cs_pending[idx] = CSTP_FINISH; 1921 report_make_pending(CSTP_FINISH, NULL); 1922 } 1923 else 1924 ((struct source_cookie *)getline_cookie(eap->getline, 1925 eap->cookie))->finished = TRUE; 1926 } 1927 1928 1929 /* 1930 * Return TRUE when a sourced file had the ":finish" command: Don't give error 1931 * message for missing ":endif". 1932 * Return FALSE when not sourcing a file. 1933 */ 1934 int 1935 source_finished( 1936 char_u *(*fgetline)(int, void *, int, int), 1937 void *cookie) 1938 { 1939 return (getline_equal(fgetline, cookie, getsourceline) 1940 && ((struct source_cookie *)getline_cookie( 1941 fgetline, cookie))->finished); 1942 } 1943 1944 /* 1945 * Return the autoload script name for a function or variable name. 1946 * Returns NULL when out of memory. 1947 * Caller must make sure that "name" contains AUTOLOAD_CHAR. 1948 */ 1949 char_u * 1950 autoload_name(char_u *name) 1951 { 1952 char_u *p, *q = NULL; 1953 char_u *scriptname; 1954 1955 // Get the script file name: replace '#' with '/', append ".vim". 1956 scriptname = alloc(STRLEN(name) + 14); 1957 if (scriptname == NULL) 1958 return NULL; 1959 STRCPY(scriptname, "autoload/"); 1960 STRCAT(scriptname, name); 1961 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL; 1962 q = p, ++p) 1963 *p = '/'; 1964 STRCPY(q, ".vim"); 1965 return scriptname; 1966 } 1967 1968 /* 1969 * If "name" has a package name try autoloading the script for it. 1970 * Return TRUE if a package was loaded. 1971 */ 1972 int 1973 script_autoload( 1974 char_u *name, 1975 int reload) // load script again when already loaded 1976 { 1977 char_u *p; 1978 char_u *scriptname, *tofree; 1979 int ret = FALSE; 1980 int i; 1981 1982 // If there is no '#' after name[0] there is no package name. 1983 p = vim_strchr(name, AUTOLOAD_CHAR); 1984 if (p == NULL || p == name) 1985 return FALSE; 1986 1987 tofree = scriptname = autoload_name(name); 1988 if (scriptname == NULL) 1989 return FALSE; 1990 1991 // Find the name in the list of previously loaded package names. Skip 1992 // "autoload/", it's always the same. 1993 for (i = 0; i < ga_loaded.ga_len; ++i) 1994 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0) 1995 break; 1996 if (!reload && i < ga_loaded.ga_len) 1997 ret = FALSE; // was loaded already 1998 else 1999 { 2000 // Remember the name if it wasn't loaded already. 2001 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK) 2002 { 2003 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname; 2004 tofree = NULL; 2005 } 2006 2007 // Try loading the package from $VIMRUNTIME/autoload/<name>.vim 2008 if (source_runtime(scriptname, 0) == OK) 2009 ret = TRUE; 2010 } 2011 2012 vim_free(tofree); 2013 return ret; 2014 } 2015 #endif 2016