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