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