blob: 8fb3cae015a75df6028c574e6ff2d2a22bccd106 [file] [log] [blame]
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001/* 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 Moolenaarda6c0332019-09-01 16:01:30 +020016#if defined(FEAT_EVAL) || defined(PROTO)
17// The names of packages that once were loaded are remembered.
18static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
19#endif
20
Bram Moolenaar307c5a52019-08-25 15:41:00 +020021/*
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010022 * Initialize the execution stack.
23 */
24 void
25estack_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;
Bram Moolenaar09d46402019-12-29 23:53:01 +010035#ifdef FEAT_EVAL
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010036 entry->es_info.ufunc = NULL;
Bram Moolenaar09d46402019-12-29 23:53:01 +010037#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010038 ++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 *
46estack_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;
Bram Moolenaar09d46402019-12-29 23:53:01 +010058#ifdef FEAT_EVAL
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010059 entry->es_info.ufunc = NULL;
Bram Moolenaar09d46402019-12-29 23:53:01 +010060#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010061 ++exestack.ga_len;
62 return entry;
63 }
64 return NULL;
65}
66
Bram Moolenaar09d46402019-12-29 23:53:01 +010067#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010068/*
69 * Add a user function to the execution stack.
70 */
Bram Moolenaarc620c052020-07-08 15:16:19 +020071 estack_T *
Bram Moolenaar25e0f582020-05-25 22:36:50 +020072estack_push_ufunc(ufunc_T *ufunc, long lnum)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010073{
Bram Moolenaar25e0f582020-05-25 22:36:50 +020074 estack_T *entry = estack_push(ETYPE_UFUNC,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010075 ufunc->uf_name_exp != NULL
76 ? ufunc->uf_name_exp : ufunc->uf_name, lnum);
77 if (entry != NULL)
78 entry->es_info.ufunc = ufunc;
Bram Moolenaarc620c052020-07-08 15:16:19 +020079 return entry;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010080}
Bram Moolenaar25e0f582020-05-25 22:36:50 +020081
82/*
83 * Return TRUE if "ufunc" with "lnum" is already at the top of the exe stack.
84 */
85 int
86estack_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}
Bram Moolenaar09d46402019-12-29 23:53:01 +010098#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010099
100/*
Bram Moolenaarc620c052020-07-08 15:16:19 +0200101 * Take an item off of the execution stack and return it.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100102 */
Bram Moolenaarc620c052020-07-08 15:16:19 +0200103 estack_T *
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100104estack_pop(void)
105{
Bram Moolenaarc620c052020-07-08 15:16:19 +0200106 if (exestack.ga_len == 0)
107 return NULL;
108 --exestack.ga_len;
109 return ((estack_T *)exestack.ga_data) + exestack.ga_len;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100110}
111
112/*
113 * Get the current value for <sfile> in allocated memory.
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200114 * "which" is ESTACK_SFILE for <sfile> and ESTACK_STACK for <stack>.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100115 */
116 char_u *
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200117estack_sfile(estack_arg_T which UNUSED)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100118{
Bram Moolenaar85b09572019-12-30 10:57:00 +0100119 estack_T *entry;
120#ifdef FEAT_EVAL
Bram Moolenaara5d04232020-07-26 15:37:02 +0200121 garray_T ga;
Bram Moolenaar4d7a2482020-01-06 19:53:43 +0100122 size_t len;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100123 int idx;
Bram Moolenaara5d04232020-07-26 15:37:02 +0200124 etype_T last_type = ETYPE_SCRIPT;
125 char *type_name;
Bram Moolenaar85b09572019-12-30 10:57:00 +0100126#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100127
128 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100129#ifdef FEAT_EVAL
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200130 if (which == ESTACK_SFILE && entry->es_type != ETYPE_UFUNC)
Bram Moolenaar09d46402019-12-29 23:53:01 +0100131#endif
Bram Moolenaara5d04232020-07-26 15:37:02 +0200132 {
133 if (entry->es_name == NULL)
134 return NULL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100135 return vim_strsave(entry->es_name);
Bram Moolenaara5d04232020-07-26 15:37:02 +0200136 }
Bram Moolenaar09d46402019-12-29 23:53:01 +0100137#ifdef FEAT_EVAL
Bram Moolenaarc4492712021-11-22 15:37:15 +0000138 // expand('<sfile>') works in a function for backwards compatibility, but
139 // may give an unexpected result. Disallow it in Vim 9 script.
140 if (which == ESTACK_SFILE && in_vim9script())
141 {
142 int save_emsg_off = emsg_off;
143
144 if (emsg_off == 1)
145 // f_expand() silences errors but we do want this one
146 emsg_off = 0;
147 emsg(_(e_cannot_expand_sfile_in_vim9_function));
148 emsg_off = save_emsg_off;
149 return NULL;
150 }
151
Bram Moolenaara5d04232020-07-26 15:37:02 +0200152 // Give information about each stack entry up to the root.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100153 // For a function we compose the call stack, as it was done in the past:
154 // "function One[123]..Two[456]..Three"
Bram Moolenaara5d04232020-07-26 15:37:02 +0200155 ga_init2(&ga, sizeof(char), 100);
156 for (idx = 0; idx < exestack.ga_len; ++idx)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100157 {
158 entry = ((estack_T *)exestack.ga_data) + idx;
Bram Moolenaara5d04232020-07-26 15:37:02 +0200159 if (entry->es_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100160 {
Bram Moolenaara810db32020-09-11 17:59:23 +0200161 long lnum = 0;
162 char *dots;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200163
Bram Moolenaara5d04232020-07-26 15:37:02 +0200164 len = STRLEN(entry->es_name) + 15;
165 type_name = "";
166 if (entry->es_type != last_type)
167 {
168 switch (entry->es_type)
169 {
170 case ETYPE_SCRIPT: type_name = "script "; break;
171 case ETYPE_UFUNC: type_name = "function "; break;
172 default: type_name = ""; break;
173 }
174 last_type = entry->es_type;
175 }
176 len += STRLEN(type_name);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200177 if (ga_grow(&ga, (int)len) == FAIL)
Bram Moolenaara5d04232020-07-26 15:37:02 +0200178 break;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200179 if (idx == exestack.ga_len - 1)
180 lnum = which == ESTACK_STACK ? SOURCING_LNUM : 0;
181 else
182 lnum = entry->es_lnum;
Bram Moolenaara810db32020-09-11 17:59:23 +0200183 dots = idx == exestack.ga_len - 1 ? "" : "..";
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200184 if (lnum == 0)
185 // For the bottom entry of <sfile>: do not add the line number,
186 // it is used in <slnum>. Also leave it out when the number is
187 // not set.
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200188 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s%s",
Bram Moolenaara810db32020-09-11 17:59:23 +0200189 type_name, entry->es_name, dots);
Bram Moolenaara5d04232020-07-26 15:37:02 +0200190 else
Bram Moolenaara810db32020-09-11 17:59:23 +0200191 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s[%ld]%s",
192 type_name, entry->es_name, lnum, dots);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200193 ga.ga_len += (int)STRLEN((char *)ga.ga_data + ga.ga_len);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100194 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100195 }
196
Bram Moolenaara5d04232020-07-26 15:37:02 +0200197 return (char_u *)ga.ga_data;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100198#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100199}
200
201/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200202 * ":runtime [what] {name}"
203 */
204 void
205ex_runtime(exarg_T *eap)
206{
207 char_u *arg = eap->arg;
208 char_u *p = skiptowhite(arg);
209 int len = (int)(p - arg);
210 int flags = eap->forceit ? DIP_ALL : 0;
211
212 if (STRNCMP(arg, "START", len) == 0)
213 {
214 flags += DIP_START + DIP_NORTP;
215 arg = skipwhite(arg + len);
216 }
217 else if (STRNCMP(arg, "OPT", len) == 0)
218 {
219 flags += DIP_OPT + DIP_NORTP;
220 arg = skipwhite(arg + len);
221 }
222 else if (STRNCMP(arg, "PACK", len) == 0)
223 {
224 flags += DIP_START + DIP_OPT + DIP_NORTP;
225 arg = skipwhite(arg + len);
226 }
227 else if (STRNCMP(arg, "ALL", len) == 0)
228 {
229 flags += DIP_START + DIP_OPT;
230 arg = skipwhite(arg + len);
231 }
232
233 source_runtime(arg, flags);
234}
235
236 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100237source_callback(char_u *fname, void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200238{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239 (void)do_source(fname, FALSE, DOSO_NONE, cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200240}
241
242/*
243 * Find the file "name" in all directories in "path" and invoke
244 * "callback(fname, cookie)".
245 * "name" can contain wildcards.
246 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
247 * When "flags" has DIP_DIR: find directories instead of files.
248 * When "flags" has DIP_ERR: give an error message if there is no match.
249 *
250 * return FAIL when no file could be sourced, OK otherwise.
251 */
252 int
253do_in_path(
254 char_u *path,
255 char_u *name,
256 int flags,
257 void (*callback)(char_u *fname, void *ck),
258 void *cookie)
259{
260 char_u *rtp;
261 char_u *np;
262 char_u *buf;
263 char_u *rtp_copy;
264 char_u *tail;
265 int num_files;
266 char_u **files;
267 int i;
268 int did_one = FALSE;
269#ifdef AMIGA
270 struct Process *proc = (struct Process *)FindTask(0L);
271 APTR save_winptr = proc->pr_WindowPtr;
272
273 // Avoid a requester here for a volume that doesn't exist.
274 proc->pr_WindowPtr = (APTR)-1L;
275#endif
276
277 // Make a copy of 'runtimepath'. Invoking the callback may change the
278 // value.
279 rtp_copy = vim_strsave(path);
280 buf = alloc(MAXPATHL);
281 if (buf != NULL && rtp_copy != NULL)
282 {
Bram Moolenaar647a5302020-05-03 17:01:24 +0200283 if (p_verbose > 10 && name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200284 {
285 verbose_enter();
286 smsg(_("Searching for \"%s\" in \"%s\""),
287 (char *)name, (char *)path);
288 verbose_leave();
289 }
290
291 // Loop over all entries in 'runtimepath'.
292 rtp = rtp_copy;
293 while (*rtp != NUL && ((flags & DIP_ALL) || !did_one))
294 {
295 size_t buflen;
296
297 // Copy the path from 'runtimepath' to buf[].
298 copy_option_part(&rtp, buf, MAXPATHL, ",");
299 buflen = STRLEN(buf);
300
301 // Skip after or non-after directories.
302 if (flags & (DIP_NOAFTER | DIP_AFTER))
303 {
304 int is_after = buflen >= 5
305 && STRCMP(buf + buflen - 5, "after") == 0;
306
307 if ((is_after && (flags & DIP_NOAFTER))
308 || (!is_after && (flags & DIP_AFTER)))
309 continue;
310 }
311
312 if (name == NULL)
313 {
314 (*callback)(buf, (void *) &cookie);
315 if (!did_one)
316 did_one = (cookie == NULL);
317 }
318 else if (buflen + STRLEN(name) + 2 < MAXPATHL)
319 {
320 add_pathsep(buf);
321 tail = buf + STRLEN(buf);
322
323 // Loop over all patterns in "name"
324 np = name;
325 while (*np != NUL && ((flags & DIP_ALL) || !did_one))
326 {
327 // Append the pattern from "name" to buf[].
328 copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)),
329 "\t ");
330
Bram Moolenaar647a5302020-05-03 17:01:24 +0200331 if (p_verbose > 10)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200332 {
333 verbose_enter();
334 smsg(_("Searching for \"%s\""), buf);
335 verbose_leave();
336 }
337
338 // Expand wildcards, invoke the callback for each match.
339 if (gen_expand_wildcards(1, &buf, &num_files, &files,
340 (flags & DIP_DIR) ? EW_DIR : EW_FILE) == OK)
341 {
342 for (i = 0; i < num_files; ++i)
343 {
344 (*callback)(files[i], cookie);
345 did_one = TRUE;
346 if (!(flags & DIP_ALL))
347 break;
348 }
349 FreeWild(num_files, files);
350 }
351 }
352 }
353 }
354 }
355 vim_free(buf);
356 vim_free(rtp_copy);
357 if (!did_one && name != NULL)
358 {
359 char *basepath = path == p_rtp ? "runtimepath" : "packpath";
360
361 if (flags & DIP_ERR)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000362 semsg(_(e_directory_not_found_in_str_str), basepath, name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200363 else if (p_verbose > 0)
364 {
365 verbose_enter();
366 smsg(_("not found in '%s': \"%s\""), basepath, name);
367 verbose_leave();
368 }
369 }
370
371#ifdef AMIGA
372 proc->pr_WindowPtr = save_winptr;
373#endif
374
375 return did_one ? OK : FAIL;
376}
377
378/*
379 * Find "name" in "path". When found, invoke the callback function for
380 * it: callback(fname, "cookie")
381 * When "flags" has DIP_ALL repeat for all matches, otherwise only the first
382 * one is used.
383 * Returns OK when at least one match found, FAIL otherwise.
384 *
385 * If "name" is NULL calls callback for each entry in "path". Cookie is
386 * passed by reference in this case, setting it to NULL indicates that callback
387 * has done its job.
388 */
389 static int
390do_in_path_and_pp(
391 char_u *path,
392 char_u *name,
393 int flags,
394 void (*callback)(char_u *fname, void *ck),
395 void *cookie)
396{
397 int done = FAIL;
398 char_u *s;
399 int len;
400 char *start_dir = "pack/*/start/*/%s";
401 char *opt_dir = "pack/*/opt/*/%s";
402
403 if ((flags & DIP_NORTP) == 0)
404 done = do_in_path(path, name, flags, callback, cookie);
405
406 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_START))
407 {
408 len = (int)(STRLEN(start_dir) + STRLEN(name));
409 s = alloc(len);
410 if (s == NULL)
411 return FAIL;
412 vim_snprintf((char *)s, len, start_dir, name);
413 done = do_in_path(p_pp, s, flags, callback, cookie);
414 vim_free(s);
415 }
416
417 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_OPT))
418 {
419 len = (int)(STRLEN(opt_dir) + STRLEN(name));
420 s = alloc(len);
421 if (s == NULL)
422 return FAIL;
423 vim_snprintf((char *)s, len, opt_dir, name);
424 done = do_in_path(p_pp, s, flags, callback, cookie);
425 vim_free(s);
426 }
427
428 return done;
429}
430
431/*
432 * Just like do_in_path_and_pp(), using 'runtimepath' for "path".
433 */
434 int
435do_in_runtimepath(
436 char_u *name,
437 int flags,
438 void (*callback)(char_u *fname, void *ck),
439 void *cookie)
440{
441 return do_in_path_and_pp(p_rtp, name, flags, callback, cookie);
442}
443
444/*
445 * Source the file "name" from all directories in 'runtimepath'.
446 * "name" can contain wildcards.
447 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
448 *
449 * return FAIL when no file could be sourced, OK otherwise.
450 */
451 int
452source_runtime(char_u *name, int flags)
453{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100454 return source_in_path(p_rtp, name, flags, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200455}
456
457/*
458 * Just like source_runtime(), but use "path" instead of 'runtimepath'.
459 */
460 int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100461source_in_path(char_u *path, char_u *name, int flags, int *ret_sid)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200462{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100463 return do_in_path_and_pp(path, name, flags, source_callback, ret_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200464}
465
466
467#if defined(FEAT_EVAL) || defined(PROTO)
468
469/*
470 * Expand wildcards in "pat" and invoke do_source() for each match.
471 */
472 static void
473source_all_matches(char_u *pat)
474{
475 int num_files;
476 char_u **files;
477 int i;
478
479 if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) == OK)
480 {
481 for (i = 0; i < num_files; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100482 (void)do_source(files[i], FALSE, DOSO_NONE, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200483 FreeWild(num_files, files);
484 }
485}
486
487/*
488 * Add the package directory to 'runtimepath'.
489 */
490 static int
491add_pack_dir_to_rtp(char_u *fname)
492{
493 char_u *p4, *p3, *p2, *p1, *p;
494 char_u *entry;
495 char_u *insp = NULL;
496 int c;
497 char_u *new_rtp;
498 int keep;
499 size_t oldlen;
500 size_t addlen;
501 size_t new_rtp_len;
502 char_u *afterdir = NULL;
503 size_t afterlen = 0;
504 char_u *after_insp = NULL;
505 char_u *ffname = NULL;
506 size_t fname_len;
507 char_u *buf = NULL;
508 char_u *rtp_ffname;
509 int match;
510 int retval = FAIL;
511
512 p4 = p3 = p2 = p1 = get_past_head(fname);
513 for (p = p1; *p; MB_PTR_ADV(p))
514 if (vim_ispathsep_nocolon(*p))
515 {
516 p4 = p3; p3 = p2; p2 = p1; p1 = p;
517 }
518
519 // now we have:
520 // rtp/pack/name/start/name
521 // p4 p3 p2 p1
522 //
523 // find the part up to "pack" in 'runtimepath'
524 c = *++p4; // append pathsep in order to expand symlink
525 *p4 = NUL;
526 ffname = fix_fname(fname);
527 *p4 = c;
528 if (ffname == NULL)
529 return FAIL;
530
531 // Find "ffname" in "p_rtp", ignoring '/' vs '\' differences.
532 // Also stop at the first "after" directory.
533 fname_len = STRLEN(ffname);
534 buf = alloc(MAXPATHL);
535 if (buf == NULL)
536 goto theend;
537 for (entry = p_rtp; *entry != NUL; )
538 {
539 char_u *cur_entry = entry;
540
541 copy_option_part(&entry, buf, MAXPATHL, ",");
542 if (insp == NULL)
543 {
544 add_pathsep(buf);
545 rtp_ffname = fix_fname(buf);
546 if (rtp_ffname == NULL)
547 goto theend;
548 match = vim_fnamencmp(rtp_ffname, ffname, fname_len) == 0;
549 vim_free(rtp_ffname);
550 if (match)
551 // Insert "ffname" after this entry (and comma).
552 insp = entry;
553 }
554
555 if ((p = (char_u *)strstr((char *)buf, "after")) != NULL
556 && p > buf
557 && vim_ispathsep(p[-1])
558 && (vim_ispathsep(p[5]) || p[5] == NUL || p[5] == ','))
559 {
560 if (insp == NULL)
561 // Did not find "ffname" before the first "after" directory,
562 // insert it before this entry.
563 insp = cur_entry;
564 after_insp = cur_entry;
565 break;
566 }
567 }
568
569 if (insp == NULL)
570 // Both "fname" and "after" not found, append at the end.
571 insp = p_rtp + STRLEN(p_rtp);
572
573 // check if rtp/pack/name/start/name/after exists
574 afterdir = concat_fnames(fname, (char_u *)"after", TRUE);
575 if (afterdir != NULL && mch_isdir(afterdir))
576 afterlen = STRLEN(afterdir) + 1; // add one for comma
577
578 oldlen = STRLEN(p_rtp);
579 addlen = STRLEN(fname) + 1; // add one for comma
580 new_rtp = alloc(oldlen + addlen + afterlen + 1); // add one for NUL
581 if (new_rtp == NULL)
582 goto theend;
583
584 // We now have 'rtp' parts: {keep}{keep_after}{rest}.
585 // Create new_rtp, first: {keep},{fname}
586 keep = (int)(insp - p_rtp);
587 mch_memmove(new_rtp, p_rtp, keep);
588 new_rtp_len = keep;
589 if (*insp == NUL)
590 new_rtp[new_rtp_len++] = ','; // add comma before
591 mch_memmove(new_rtp + new_rtp_len, fname, addlen - 1);
592 new_rtp_len += addlen - 1;
593 if (*insp != NUL)
594 new_rtp[new_rtp_len++] = ','; // add comma after
595
596 if (afterlen > 0 && after_insp != NULL)
597 {
598 int keep_after = (int)(after_insp - p_rtp);
599
600 // Add to new_rtp: {keep},{fname}{keep_after},{afterdir}
601 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep,
602 keep_after - keep);
603 new_rtp_len += keep_after - keep;
604 mch_memmove(new_rtp + new_rtp_len, afterdir, afterlen - 1);
605 new_rtp_len += afterlen - 1;
606 new_rtp[new_rtp_len++] = ',';
607 keep = keep_after;
608 }
609
610 if (p_rtp[keep] != NUL)
611 // Append rest: {keep},{fname}{keep_after},{afterdir}{rest}
612 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep, oldlen - keep + 1);
613 else
614 new_rtp[new_rtp_len] = NUL;
615
616 if (afterlen > 0 && after_insp == NULL)
617 {
618 // Append afterdir when "after" was not found:
619 // {keep},{fname}{rest},{afterdir}
620 STRCAT(new_rtp, ",");
621 STRCAT(new_rtp, afterdir);
622 }
623
624 set_option_value((char_u *)"rtp", 0L, new_rtp, 0);
625 vim_free(new_rtp);
626 retval = OK;
627
628theend:
629 vim_free(buf);
630 vim_free(ffname);
631 vim_free(afterdir);
632 return retval;
633}
634
635/*
636 * Load scripts in "plugin" and "ftdetect" directories of the package.
637 */
638 static int
639load_pack_plugin(char_u *fname)
640{
641 static char *plugpat = "%s/plugin/**/*.vim";
642 static char *ftpat = "%s/ftdetect/*.vim";
643 int len;
644 char_u *ffname = fix_fname(fname);
645 char_u *pat = NULL;
646 int retval = FAIL;
647
648 if (ffname == NULL)
649 return FAIL;
650 len = (int)STRLEN(ffname) + (int)STRLEN(ftpat);
651 pat = alloc(len);
652 if (pat == NULL)
653 goto theend;
654 vim_snprintf((char *)pat, len, plugpat, ffname);
655 source_all_matches(pat);
656
657 {
658 char_u *cmd = vim_strsave((char_u *)"g:did_load_filetypes");
659
660 // If runtime/filetype.vim wasn't loaded yet, the scripts will be
661 // found when it loads.
662 if (cmd != NULL && eval_to_number(cmd) > 0)
663 {
664 do_cmdline_cmd((char_u *)"augroup filetypedetect");
665 vim_snprintf((char *)pat, len, ftpat, ffname);
666 source_all_matches(pat);
667 do_cmdline_cmd((char_u *)"augroup END");
668 }
669 vim_free(cmd);
670 }
671 vim_free(pat);
672 retval = OK;
673
674theend:
675 vim_free(ffname);
676 return retval;
677}
678
679// used for "cookie" of add_pack_plugin()
680static int APP_ADD_DIR;
681static int APP_LOAD;
682static int APP_BOTH;
683
684 static void
685add_pack_plugin(char_u *fname, void *cookie)
686{
687 if (cookie != &APP_LOAD)
688 {
689 char_u *buf = alloc(MAXPATHL);
690 char_u *p;
691 int found = FALSE;
692
693 if (buf == NULL)
694 return;
695 p = p_rtp;
696 while (*p != NUL)
697 {
698 copy_option_part(&p, buf, MAXPATHL, ",");
699 if (pathcmp((char *)buf, (char *)fname, -1) == 0)
700 {
701 found = TRUE;
702 break;
703 }
704 }
705 vim_free(buf);
706 if (!found)
707 // directory is not yet in 'runtimepath', add it
708 if (add_pack_dir_to_rtp(fname) == FAIL)
709 return;
710 }
711
712 if (cookie != &APP_ADD_DIR)
713 load_pack_plugin(fname);
714}
715
716/*
717 * Add all packages in the "start" directory to 'runtimepath'.
718 */
719 void
720add_pack_start_dirs(void)
721{
722 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
723 add_pack_plugin, &APP_ADD_DIR);
724}
725
726/*
727 * Load plugins from all packages in the "start" directory.
728 */
729 void
730load_start_packages(void)
731{
732 did_source_packages = TRUE;
733 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
734 add_pack_plugin, &APP_LOAD);
735}
736
737/*
738 * ":packloadall"
739 * Find plugins in the package directories and source them.
740 */
741 void
742ex_packloadall(exarg_T *eap)
743{
744 if (!did_source_packages || eap->forceit)
745 {
746 // First do a round to add all directories to 'runtimepath', then load
747 // the plugins. This allows for plugins to use an autoload directory
748 // of another plugin.
749 add_pack_start_dirs();
750 load_start_packages();
751 }
752}
753
754/*
755 * ":packadd[!] {name}"
756 */
757 void
758ex_packadd(exarg_T *eap)
759{
760 static char *plugpat = "pack/*/%s/%s";
761 int len;
762 char *pat;
763 int round;
764 int res = OK;
765
766 // Round 1: use "start", round 2: use "opt".
767 for (round = 1; round <= 2; ++round)
768 {
769 // Only look under "start" when loading packages wasn't done yet.
770 if (round == 1 && did_source_packages)
771 continue;
772
773 len = (int)STRLEN(plugpat) + (int)STRLEN(eap->arg) + 5;
774 pat = alloc(len);
775 if (pat == NULL)
776 return;
777 vim_snprintf(pat, len, plugpat, round == 1 ? "start" : "opt", eap->arg);
778 // The first round don't give a "not found" error, in the second round
779 // only when nothing was found in the first round.
780 res = do_in_path(p_pp, (char_u *)pat,
781 DIP_ALL + DIP_DIR + (round == 2 && res == FAIL ? DIP_ERR : 0),
782 add_pack_plugin, eap->forceit ? &APP_ADD_DIR : &APP_BOTH);
783 vim_free(pat);
784 }
785}
786#endif
787
788/*
Bram Moolenaar26262f82019-09-04 20:59:15 +0200789 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
790 * list of file names in allocated memory.
791 */
792 void
793remove_duplicates(garray_T *gap)
794{
795 int i;
796 int j;
797 char_u **fnames = (char_u **)gap->ga_data;
798
799 sort_strings(fnames, gap->ga_len);
800 for (i = gap->ga_len - 1; i > 0; --i)
801 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
802 {
803 vim_free(fnames[i]);
804 for (j = i + 1; j < gap->ga_len; ++j)
805 fnames[j - 1] = fnames[j];
806 --gap->ga_len;
807 }
808}
809
810/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200811 * Expand color scheme, compiler or filetype names.
812 * Search from 'runtimepath':
813 * 'runtimepath'/{dirnames}/{pat}.vim
814 * When "flags" has DIP_START: search also from 'start' of 'packpath':
815 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
816 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
817 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
818 * "dirnames" is an array with one or more directory names.
819 */
820 int
821ExpandRTDir(
822 char_u *pat,
823 int flags,
824 int *num_file,
825 char_u ***file,
826 char *dirnames[])
827{
828 char_u *s;
829 char_u *e;
830 char_u *match;
831 garray_T ga;
832 int i;
833 int pat_len;
834
835 *num_file = 0;
836 *file = NULL;
837 pat_len = (int)STRLEN(pat);
838 ga_init2(&ga, (int)sizeof(char *), 10);
839
840 for (i = 0; dirnames[i] != NULL; ++i)
841 {
842 s = alloc(STRLEN(dirnames[i]) + pat_len + 7);
843 if (s == NULL)
844 {
845 ga_clear_strings(&ga);
846 return FAIL;
847 }
848 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
849 globpath(p_rtp, s, &ga, 0);
850 vim_free(s);
851 }
852
853 if (flags & DIP_START) {
854 for (i = 0; dirnames[i] != NULL; ++i)
855 {
856 s = alloc(STRLEN(dirnames[i]) + pat_len + 22);
857 if (s == NULL)
858 {
859 ga_clear_strings(&ga);
860 return FAIL;
861 }
862 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
863 globpath(p_pp, s, &ga, 0);
864 vim_free(s);
865 }
866 }
867
868 if (flags & DIP_OPT) {
869 for (i = 0; dirnames[i] != NULL; ++i)
870 {
871 s = alloc(STRLEN(dirnames[i]) + pat_len + 20);
872 if (s == NULL)
873 {
874 ga_clear_strings(&ga);
875 return FAIL;
876 }
877 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
878 globpath(p_pp, s, &ga, 0);
879 vim_free(s);
880 }
881 }
882
883 for (i = 0; i < ga.ga_len; ++i)
884 {
885 match = ((char_u **)ga.ga_data)[i];
886 s = match;
887 e = s + STRLEN(s);
888 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
889 {
890 e -= 4;
891 for (s = e; s > match; MB_PTR_BACK(match, s))
892 if (s < match || vim_ispathsep(*s))
893 break;
894 ++s;
895 *e = NUL;
896 mch_memmove(match, s, e - s + 1);
897 }
898 }
899
900 if (ga.ga_len == 0)
901 return FAIL;
902
903 // Sort and remove duplicates which can happen when specifying multiple
904 // directories in dirnames.
905 remove_duplicates(&ga);
906
907 *file = ga.ga_data;
908 *num_file = ga.ga_len;
909 return OK;
910}
911
912/*
913 * Expand loadplugin names:
914 * 'packpath'/pack/ * /opt/{pat}
915 */
916 int
917ExpandPackAddDir(
918 char_u *pat,
919 int *num_file,
920 char_u ***file)
921{
922 char_u *s;
923 char_u *e;
924 char_u *match;
925 garray_T ga;
926 int i;
927 int pat_len;
928
929 *num_file = 0;
930 *file = NULL;
931 pat_len = (int)STRLEN(pat);
932 ga_init2(&ga, (int)sizeof(char *), 10);
933
934 s = alloc(pat_len + 26);
935 if (s == NULL)
936 {
937 ga_clear_strings(&ga);
938 return FAIL;
939 }
940 sprintf((char *)s, "pack/*/opt/%s*", pat);
941 globpath(p_pp, s, &ga, 0);
942 vim_free(s);
943
944 for (i = 0; i < ga.ga_len; ++i)
945 {
946 match = ((char_u **)ga.ga_data)[i];
947 s = gettail(match);
948 e = s + STRLEN(s);
949 mch_memmove(match, s, e - s + 1);
950 }
951
952 if (ga.ga_len == 0)
953 return FAIL;
954
955 // Sort and remove duplicates which can happen when specifying multiple
956 // directories in dirnames.
957 remove_duplicates(&ga);
958
959 *file = ga.ga_data;
960 *num_file = ga.ga_len;
961 return OK;
962}
963
964 static void
965cmd_source(char_u *fname, exarg_T *eap)
966{
967 if (*fname == NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000968 emsg(_(e_argument_required));
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200969
970 else if (eap != NULL && eap->forceit)
971 // ":source!": read Normal mode commands
972 // Need to execute the commands directly. This is required at least
973 // for:
974 // - ":g" command busy
975 // - after ":argdo", ":windo" or ":bufdo"
976 // - another command follows
977 // - inside a loop
978 openscript(fname, global_busy || listcmd_busy || eap->nextcmd != NULL
979#ifdef FEAT_EVAL
980 || eap->cstack->cs_idx >= 0
981#endif
982 );
983
984 // ":source" read ex commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985 else if (do_source(fname, FALSE, DOSO_NONE, NULL) == FAIL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000986 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200987}
988
989/*
990 * ":source {fname}"
991 */
992 void
993ex_source(exarg_T *eap)
994{
995#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +0200996 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200997 {
998 char_u *fname = NULL;
999
1000 fname = do_browse(0, (char_u *)_("Source Vim script"), eap->arg,
1001 NULL, NULL,
1002 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
1003 if (fname != NULL)
1004 {
1005 cmd_source(fname, eap);
1006 vim_free(fname);
1007 }
1008 }
1009 else
1010#endif
1011 cmd_source(eap->arg, eap);
1012}
1013
1014#if defined(FEAT_EVAL) || defined(PROTO)
1015/*
1016 * ":options"
1017 */
1018 void
1019ex_options(
1020 exarg_T *eap UNUSED)
1021{
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001022 char_u buf[500];
1023 int multi_mods = 0;
1024
1025 buf[0] = NUL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001026 (void)add_win_cmd_modifers(buf, &cmdmod, &multi_mods);
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001027
1028 vim_setenv((char_u *)"OPTWIN_CMD", buf);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001029 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL);
1030}
1031#endif
1032
1033/*
1034 * ":source" and associated commands.
1035 */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001036
1037#ifdef FEAT_EVAL
1038/*
1039 * Return the address holding the next breakpoint line for a source cookie.
1040 */
1041 linenr_T *
1042source_breakpoint(void *cookie)
1043{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001044 return &((source_cookie_T *)cookie)->breakpoint;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001045}
1046
1047/*
1048 * Return the address holding the debug tick for a source cookie.
1049 */
1050 int *
1051source_dbg_tick(void *cookie)
1052{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001053 return &((source_cookie_T *)cookie)->dbg_tick;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001054}
1055
1056/*
1057 * Return the nesting level for a source cookie.
1058 */
1059 int
1060source_level(void *cookie)
1061{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001062 return ((source_cookie_T *)cookie)->level;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001063}
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001064
1065/*
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02001066 * Return the readahead line. Note that the pointer may become invalid when
1067 * getting the next line, if it's concatenated with the next one.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001068 */
1069 char_u *
1070source_nextline(void *cookie)
1071{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001072 return ((source_cookie_T *)cookie)->nextline;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001073}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001074#endif
1075
1076#if (defined(MSWIN) && defined(FEAT_CSCOPE)) || defined(HAVE_FD_CLOEXEC)
1077# define USE_FOPEN_NOINH
1078/*
1079 * Special function to open a file without handle inheritance.
1080 * When possible the handle is closed on exec().
1081 */
1082 static FILE *
1083fopen_noinh_readbin(char *filename)
1084{
1085# ifdef MSWIN
1086 int fd_tmp = mch_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0);
1087# else
1088 int fd_tmp = mch_open(filename, O_RDONLY, 0);
1089# endif
1090
1091 if (fd_tmp == -1)
1092 return NULL;
1093
1094# ifdef HAVE_FD_CLOEXEC
1095 {
1096 int fdflags = fcntl(fd_tmp, F_GETFD);
1097 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
1098 (void)fcntl(fd_tmp, F_SETFD, fdflags | FD_CLOEXEC);
1099 }
1100# endif
1101
1102 return fdopen(fd_tmp, READBIN);
1103}
1104#endif
1105
1106/*
1107 * do_source: Read the file "fname" and execute its lines as EX commands.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001108 * When "ret_sid" is not NULL and we loaded the script before, don't load it
1109 * again.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001110 *
1111 * This function may be called recursively!
1112 *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001113 * Return FAIL if file could not be opened, OK otherwise.
1114 * If a scriptitem_T was found or created "*ret_sid" is set to the SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001115 */
1116 int
1117do_source(
1118 char_u *fname,
1119 int check_other, // check for .vimrc and _vimrc
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001120 int is_vimrc, // DOSO_ value
1121 int *ret_sid UNUSED)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001122{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001123 source_cookie_T cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001124 char_u *p;
1125 char_u *fname_exp;
1126 char_u *firstline = NULL;
1127 int retval = FAIL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001128 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001129#ifdef FEAT_EVAL
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001130 static scid_T last_current_SID = 0;
1131 static int last_current_SID_seq = 0;
1132 funccal_entry_T funccalp_entry;
1133 int save_debug_break_level = debug_break_level;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001134 int sid;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001135 scriptitem_T *si = NULL;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001136 int save_estack_compiling = estack_compiling;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001137#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;
Bram Moolenaare31ee862020-01-07 20:59:34 +01001146 ESTACK_CHECK_DECLARATION
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001147
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 }
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001160#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001161 estack_compiling = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001162
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001163 // See if we loaded this script before.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001164 for (sid = script_items.ga_len; sid > 0; --sid)
1165 {
Bram Moolenaar978d1702020-01-26 17:38:12 +01001166 // We used to check inode here, but that doesn't work:
1167 // - If a script is edited and written, it may get a different
1168 // inode number, even though to the user it is the same script.
1169 // - If a script is deleted and another script is written, with a
1170 // different name, the inode may be re-used.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001171 si = SCRIPT_ITEM(sid);
Bram Moolenaar978d1702020-01-26 17:38:12 +01001172 if (si->sn_name != NULL && fnamecmp(si->sn_name, fname_exp) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001173 // Found it!
1174 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175 }
1176 if (sid > 0 && ret_sid != NULL)
1177 {
1178 // Already loaded and no need to load again, return here.
1179 *ret_sid = sid;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001180 retval = OK;
1181 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001182 }
1183#endif
1184
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001185 // Apply SourceCmd autocommands, they should get the file and source it.
1186 if (has_autocmd(EVENT_SOURCECMD, fname_exp, NULL)
1187 && apply_autocmds(EVENT_SOURCECMD, fname_exp, fname_exp,
1188 FALSE, curbuf))
1189 {
1190#ifdef FEAT_EVAL
1191 retval = aborting() ? FAIL : OK;
1192#else
1193 retval = OK;
1194#endif
1195 if (retval == OK)
1196 // Apply SourcePost autocommands.
1197 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp,
1198 FALSE, curbuf);
1199 goto theend;
1200 }
1201
1202 // Apply SourcePre autocommands, they may get the file.
1203 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf);
1204
1205#ifdef USE_FOPEN_NOINH
1206 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1207#else
1208 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1209#endif
1210 if (cookie.fp == NULL && check_other)
1211 {
1212 // Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
1213 // and ".exrc" by "_exrc" or vice versa.
1214 p = gettail(fname_exp);
1215 if ((*p == '.' || *p == '_')
1216 && (STRICMP(p + 1, "vimrc") == 0
1217 || STRICMP(p + 1, "gvimrc") == 0
1218 || STRICMP(p + 1, "exrc") == 0))
1219 {
1220 if (*p == '_')
1221 *p = '.';
1222 else
1223 *p = '_';
1224#ifdef USE_FOPEN_NOINH
1225 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1226#else
1227 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1228#endif
1229 }
1230 }
1231
1232 if (cookie.fp == NULL)
1233 {
1234 if (p_verbose > 0)
1235 {
1236 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001237 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001238 smsg(_("could not source \"%s\""), fname);
1239 else
1240 smsg(_("line %ld: could not source \"%s\""),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001241 SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001242 verbose_leave();
1243 }
1244 goto theend;
1245 }
1246
1247 // The file exists.
1248 // - In verbose mode, give a message.
1249 // - For a vimrc file, may want to set 'compatible', call vimrc_found().
1250 if (p_verbose > 1)
1251 {
1252 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001253 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001254 smsg(_("sourcing \"%s\""), fname);
1255 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001256 smsg(_("line %ld: sourcing \"%s\""), SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001257 verbose_leave();
1258 }
1259 if (is_vimrc == DOSO_VIMRC)
1260 vimrc_found(fname_exp, (char_u *)"MYVIMRC");
1261 else if (is_vimrc == DOSO_GVIMRC)
1262 vimrc_found(fname_exp, (char_u *)"MYGVIMRC");
1263
1264#ifdef USE_CRNL
1265 // If no automatic file format: Set default to CR-NL.
1266 if (*p_ffs == NUL)
1267 cookie.fileformat = EOL_DOS;
1268 else
1269 cookie.fileformat = EOL_UNKNOWN;
1270 cookie.error = FALSE;
1271#endif
1272
1273 cookie.nextline = NULL;
1274 cookie.sourcing_lnum = 0;
1275 cookie.finished = FALSE;
1276
1277#ifdef FEAT_EVAL
1278 // Check if this script has a breakpoint.
1279 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0);
1280 cookie.fname = fname_exp;
1281 cookie.dbg_tick = debug_tick;
1282
1283 cookie.level = ex_nesting_level;
1284#endif
1285
1286 // Keep the sourcing name/lnum, for recursive calls.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001287 estack_push(ETYPE_SCRIPT, fname_exp, 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001288 ESTACK_CHECK_SETUP
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001289
1290#ifdef STARTUPTIME
1291 if (time_fd != NULL)
1292 time_push(&tv_rel, &tv_start);
1293#endif
1294
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001295 save_current_sctx = current_sctx;
1296 current_sctx.sc_version = 1; // default script version
1297
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001298#ifdef FEAT_EVAL
1299# ifdef FEAT_PROFILE
1300 if (do_profiling == PROF_YES)
1301 prof_child_enter(&wait_start); // entering a child now
1302# endif
1303
1304 // Don't use local function variables, if called from a function.
1305 // Also starts profiling timer for nested script.
1306 save_funccal(&funccalp_entry);
1307
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001308 current_sctx.sc_lnum = 0;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001309
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001310 // Check if this script was sourced before to find its SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001311 // Always use a new sequence number.
1312 current_sctx.sc_seq = ++last_current_SID_seq;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001313 if (sid > 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001314 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315 hashtab_T *ht;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001316 int todo;
1317 hashitem_T *hi;
1318 dictitem_T *di;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001319
1320 // loading the same script again
Bram Moolenaar2b327002020-12-26 15:39:31 +01001321 si->sn_state = SN_STATE_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001322 current_sctx.sc_sid = sid;
1323
Bram Moolenaar2b327002020-12-26 15:39:31 +01001324 // Script-local variables remain but "const" can be set again.
1325 // In Vim9 script variables will be cleared when "vim9script" is
1326 // encountered without the "noclear" argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327 ht = &SCRIPT_VARS(sid);
Bram Moolenaar2b327002020-12-26 15:39:31 +01001328 todo = (int)ht->ht_used;
1329 for (hi = ht->ht_array; todo > 0; ++hi)
1330 if (!HASHITEM_EMPTY(hi))
1331 {
1332 --todo;
1333 di = HI2DI(hi);
1334 di->di_flags |= DI_FLAGS_RELOAD;
1335 }
Bram Moolenaar7e3ee782020-12-27 14:02:27 +01001336 // imports can be redefined once
1337 mark_imports_for_reload(sid);
Bram Moolenaar0123cc12021-02-07 17:17:58 +01001338
1339 // reset version, "vim9script" may have been added or removed.
1340 si->sn_version = 1;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001341 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342 else
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001343 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 // It's new, generate a new SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001345 current_sctx.sc_sid = ++last_current_SID;
1346 if (ga_grow(&script_items,
1347 (int)(current_sctx.sc_sid - script_items.ga_len)) == FAIL)
1348 goto almosttheend;
1349 while (script_items.ga_len < current_sctx.sc_sid)
1350 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001351 si = ALLOC_CLEAR_ONE(scriptitem_T);
1352 if (si == NULL)
1353 goto almosttheend;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001354 ++script_items.ga_len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001355 SCRIPT_ITEM(script_items.ga_len) = si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356 si->sn_name = NULL;
1357 si->sn_version = 1;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001358
1359 // Allocate the local script variables to use for this script.
1360 new_script_vars(script_items.ga_len);
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +02001361 ga_init2(&si->sn_var_vals, sizeof(svar_T), 10);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001362 hash_init(&si->sn_all_vars.dv_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363 ga_init2(&si->sn_imports, sizeof(imported_T), 10);
1364 ga_init2(&si->sn_type_list, sizeof(type_T), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001365# ifdef FEAT_PROFILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001366 si->sn_prof_on = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001367# endif
1368 }
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001369 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001370 si->sn_name = fname_exp;
1371 fname_exp = vim_strsave(si->sn_name); // used for autocmd
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001372 if (ret_sid != NULL)
1373 *ret_sid = current_sctx.sc_sid;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001374
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001375 // Remember the "is_vimrc" flag for when the file is sourced again.
1376 si->sn_is_vimrc = is_vimrc;
1377
Bram Moolenaar2b327002020-12-26 15:39:31 +01001378 // Used to check script variable index is still valid.
1379 si->sn_script_seq = current_sctx.sc_seq;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001380 }
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.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001431 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001432 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)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001444 emsg(_(e_interrupted));
Bram Moolenaare31ee862020-01-07 20:59:34 +01001445 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001446 estack_pop();
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001447 if (p_verbose > 1)
1448 {
1449 verbose_enter();
1450 smsg(_("finished sourcing %s"), fname);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001451 if (SOURCING_NAME != NULL)
1452 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001453 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
1476almosttheend:
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001477 // If "sn_save_cpo" is set that means we encountered "vim9script": restore
1478 // 'cpoptions', unless in the main .vimrc file.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001480 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001481 if (si->sn_save_cpo != NULL && si->sn_is_vimrc == DOSO_NONE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 {
Bram Moolenaar3e191692021-03-17 17:46:00 +01001483 if (STRCMP(p_cpo, CPO_VIM) != 0)
1484 {
1485 char_u *f;
1486 char_u *t;
1487
1488 // 'cpo' was changed in the script. Apply the same change to the
1489 // saved value, if possible.
1490 for (f = (char_u *)CPO_VIM; *f != NUL; ++f)
1491 if (vim_strchr(p_cpo, *f) == NULL
1492 && (t = vim_strchr(si->sn_save_cpo, *f)) != NULL)
1493 // flag was removed, also remove it from the saved 'cpo'
1494 mch_memmove(t, t + 1, STRLEN(t));
1495 for (f = p_cpo; *f != NUL; ++f)
1496 if (vim_strchr((char_u *)CPO_VIM, *f) == NULL
1497 && vim_strchr(si->sn_save_cpo, *f) == NULL)
1498 {
1499 // flag was added, also add it to the saved 'cpo'
1500 t = alloc(STRLEN(si->sn_save_cpo) + 2);
1501 if (t != NULL)
1502 {
1503 *t = *f;
1504 STRCPY(t + 1, si->sn_save_cpo);
1505 vim_free(si->sn_save_cpo);
1506 si->sn_save_cpo = t;
1507 }
1508 }
1509 }
Bram Moolenaar37294bd2021-03-10 13:40:08 +01001510 set_option_value((char_u *)"cpo", 0L, si->sn_save_cpo, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001511 }
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001512 VIM_CLEAR(si->sn_save_cpo);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001513
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001514 restore_funccal();
1515# ifdef FEAT_PROFILE
1516 if (do_profiling == PROF_YES)
1517 prof_child_exit(&wait_start); // leaving a child now
1518# endif
1519#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001520 current_sctx = save_current_sctx;
1521
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001522 fclose(cookie.fp);
1523 vim_free(cookie.nextline);
1524 vim_free(firstline);
1525 convert_setup(&cookie.conv, NULL, NULL);
1526
1527 if (trigger_source_post)
1528 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp, FALSE, curbuf);
1529
1530theend:
1531 vim_free(fname_exp);
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001532#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001533 estack_compiling = save_estack_compiling;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001534#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001535 return retval;
1536}
1537
1538#if defined(FEAT_EVAL) || defined(PROTO)
1539
1540/*
1541 * ":scriptnames"
1542 */
1543 void
1544ex_scriptnames(exarg_T *eap)
1545{
1546 int i;
1547
1548 if (eap->addr_count > 0)
1549 {
1550 // :script {scriptId}: edit the script
Bram Moolenaare3d46852020-08-29 13:39:17 +02001551 if (!SCRIPT_ID_VALID(eap->line2))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001552 emsg(_(e_invalid_argument));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001553 else
1554 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001555 eap->arg = SCRIPT_ITEM(eap->line2)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001556 do_exedit(eap, NULL);
1557 }
1558 return;
1559 }
1560
1561 for (i = 1; i <= script_items.ga_len && !got_int; ++i)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001562 if (SCRIPT_ITEM(i)->sn_name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001563 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001564 home_replace(NULL, SCRIPT_ITEM(i)->sn_name,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001565 NameBuff, MAXPATHL, TRUE);
1566 smsg("%3d: %s", i, NameBuff);
1567 }
1568}
1569
1570# if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1571/*
1572 * Fix slashes in the list of script names for 'shellslash'.
1573 */
1574 void
1575scriptnames_slash_adjust(void)
1576{
1577 int i;
1578
1579 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001580 if (SCRIPT_ITEM(i)->sn_name != NULL)
1581 slash_adjust(SCRIPT_ITEM(i)->sn_name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001582}
1583# endif
1584
1585/*
1586 * Get a pointer to a script name. Used for ":verbose set".
Bram Moolenaar74667062020-12-28 15:41:41 +01001587 * Message appended to "Last set from "
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001588 */
1589 char_u *
1590get_scriptname(scid_T id)
1591{
1592 if (id == SID_MODELINE)
1593 return (char_u *)_("modeline");
1594 if (id == SID_CMDARG)
1595 return (char_u *)_("--cmd argument");
1596 if (id == SID_CARG)
1597 return (char_u *)_("-c argument");
1598 if (id == SID_ENV)
1599 return (char_u *)_("environment variable");
1600 if (id == SID_ERROR)
1601 return (char_u *)_("error handler");
Bram Moolenaar74667062020-12-28 15:41:41 +01001602 if (id == SID_WINLAYOUT)
1603 return (char_u *)_("changed window size");
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001604 return SCRIPT_ITEM(id)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001605}
1606
1607# if defined(EXITFREE) || defined(PROTO)
1608 void
1609free_scriptnames(void)
1610{
1611 int i;
1612
1613 for (i = script_items.ga_len; i > 0; --i)
Bram Moolenaar86173482019-10-01 17:02:16 +02001614 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001615 scriptitem_T *si = SCRIPT_ITEM(i);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001616
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001617 // the variables themselves are cleared in evalvars_clear()
1618 vim_free(si->sn_vars);
1619
1620 vim_free(si->sn_name);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001621 free_imports_and_script_vars(i);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001622 free_string_option(si->sn_save_cpo);
Bram Moolenaara720be72019-10-22 21:45:19 +02001623# ifdef FEAT_PROFILE
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001624 ga_clear(&si->sn_prl_ga);
Bram Moolenaara720be72019-10-22 21:45:19 +02001625# endif
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001626 vim_free(si);
Bram Moolenaar86173482019-10-01 17:02:16 +02001627 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001628 ga_clear(&script_items);
1629}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001630
1631 void
1632free_autoload_scriptnames(void)
1633{
1634 ga_clear_strings(&ga_loaded);
1635}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001636# endif
1637
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001638 linenr_T
Bram Moolenaar66250c92020-08-20 15:02:42 +02001639get_sourced_lnum(
1640 char_u *(*fgetline)(int, void *, int, getline_opt_T),
1641 void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001642{
1643 return fgetline == getsourceline
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001644 ? ((source_cookie_T *)cookie)->sourcing_lnum
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001645 : SOURCING_LNUM;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001646}
Dominique Pelle748b3082022-01-08 12:41:16 +00001647#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001648
1649 static char_u *
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001650get_one_sourceline(source_cookie_T *sp)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001651{
1652 garray_T ga;
1653 int len;
1654 int c;
1655 char_u *buf;
1656#ifdef USE_CRNL
1657 int has_cr; // CR-LF found
1658#endif
1659 int have_read = FALSE;
1660
1661 // use a growarray to store the sourced line
1662 ga_init2(&ga, 1, 250);
1663
1664 // Loop until there is a finished line (or end-of-file).
1665 ++sp->sourcing_lnum;
1666 for (;;)
1667 {
1668 // make room to read at least 120 (more) characters
1669 if (ga_grow(&ga, 120) == FAIL)
1670 break;
1671 buf = (char_u *)ga.ga_data;
1672
1673 if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
1674 sp->fp) == NULL)
1675 break;
1676 len = ga.ga_len + (int)STRLEN(buf + ga.ga_len);
1677#ifdef USE_CRNL
1678 // Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the
1679 // CTRL-Z by its own, or after a NL.
1680 if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n'))
1681 && sp->fileformat == EOL_DOS
1682 && buf[len - 1] == Ctrl_Z)
1683 {
1684 buf[len - 1] = NUL;
1685 break;
1686 }
1687#endif
1688
1689 have_read = TRUE;
1690 ga.ga_len = len;
1691
1692 // If the line was longer than the buffer, read more.
1693 if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n')
1694 continue;
1695
1696 if (len >= 1 && buf[len - 1] == '\n') // remove trailing NL
1697 {
1698#ifdef USE_CRNL
1699 has_cr = (len >= 2 && buf[len - 2] == '\r');
1700 if (sp->fileformat == EOL_UNKNOWN)
1701 {
1702 if (has_cr)
1703 sp->fileformat = EOL_DOS;
1704 else
1705 sp->fileformat = EOL_UNIX;
1706 }
1707
1708 if (sp->fileformat == EOL_DOS)
1709 {
1710 if (has_cr) // replace trailing CR
1711 {
1712 buf[len - 2] = '\n';
1713 --len;
1714 --ga.ga_len;
1715 }
1716 else // lines like ":map xx yy^M" will have failed
1717 {
1718 if (!sp->error)
1719 {
1720 msg_source(HL_ATTR(HLF_W));
1721 emsg(_("W15: Warning: Wrong line separator, ^M may be missing"));
1722 }
1723 sp->error = TRUE;
1724 sp->fileformat = EOL_UNIX;
1725 }
1726 }
1727#endif
1728 // The '\n' is escaped if there is an odd number of ^V's just
1729 // before it, first set "c" just before the 'V's and then check
1730 // len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo
1731 for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--)
1732 ;
1733 if ((len & 1) != (c & 1)) // escaped NL, read more
1734 {
1735 ++sp->sourcing_lnum;
1736 continue;
1737 }
1738
1739 buf[len - 1] = NUL; // remove the NL
1740 }
1741
1742 // Check for ^C here now and then, so recursive :so can be broken.
1743 line_breakcheck();
1744 break;
1745 }
1746
1747 if (have_read)
1748 return (char_u *)ga.ga_data;
1749
1750 vim_free(ga.ga_data);
1751 return NULL;
1752}
1753
1754/*
1755 * Get one full line from a sourced file.
1756 * Called by do_cmdline() when it's called from do_source().
1757 *
1758 * Return a pointer to the line in allocated memory.
1759 * Return NULL for end-of-file or some error.
1760 */
1761 char_u *
Bram Moolenaar66250c92020-08-20 15:02:42 +02001762getsourceline(
1763 int c UNUSED,
1764 void *cookie,
1765 int indent UNUSED,
1766 getline_opt_T options)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001767{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001768 source_cookie_T *sp = (source_cookie_T *)cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001769 char_u *line;
1770 char_u *p;
Bram Moolenaardcc58e02020-12-28 20:53:21 +01001771 int do_vim9_all = in_vim9script()
1772 && options == GETLINE_CONCAT_ALL;
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01001773 int do_bar_cont = do_vim9_all
1774 || options == GETLINE_CONCAT_CONTBAR;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001775
1776#ifdef FEAT_EVAL
1777 // If breakpoints have been added/deleted need to check for it.
1778 if (sp->dbg_tick < debug_tick)
1779 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001780 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001781 sp->dbg_tick = debug_tick;
1782 }
1783# ifdef FEAT_PROFILE
1784 if (do_profiling == PROF_YES)
1785 script_line_end();
1786# endif
1787#endif
1788
1789 // Set the current sourcing line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001790 SOURCING_LNUM = sp->sourcing_lnum + 1;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001791
1792 // Get current line. If there is a read-ahead line, use it, otherwise get
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001793 // one now. "fp" is NULL if actually using a string.
1794 if (sp->finished || sp->fp == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001795 line = NULL;
1796 else if (sp->nextline == NULL)
1797 line = get_one_sourceline(sp);
1798 else
1799 {
1800 line = sp->nextline;
1801 sp->nextline = NULL;
1802 ++sp->sourcing_lnum;
1803 }
1804#ifdef FEAT_PROFILE
1805 if (line != NULL && do_profiling == PROF_YES)
1806 script_line_start();
1807#endif
1808
1809 // Only concatenate lines starting with a \ when 'cpoptions' doesn't
1810 // contain the 'C' flag.
Bram Moolenaar66250c92020-08-20 15:02:42 +02001811 if (line != NULL && options != GETLINE_NONE
1812 && vim_strchr(p_cpo, CPO_CONCAT) == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001813 {
Bram Moolenaar5072b472021-06-03 21:56:10 +02001814 int comment_char = in_vim9script() ? '#' : '"';
1815
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001816 // compensate for the one line read-ahead
1817 --sp->sourcing_lnum;
1818
1819 // Get the next line and concatenate it when it starts with a
1820 // backslash. We always need to read the next line, keep it in
1821 // sp->nextline.
1822 /* Also check for a comment in between continuation lines: "\ */
Bram Moolenaardcc58e02020-12-28 20:53:21 +01001823 // Also check for a Vim9 comment, empty line, line starting with '|',
1824 // but not "||".
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001825 sp->nextline = get_one_sourceline(sp);
1826 if (sp->nextline != NULL
1827 && (*(p = skipwhite(sp->nextline)) == '\\'
Bram Moolenaar5072b472021-06-03 21:56:10 +02001828 || (p[0] == comment_char
1829 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01001830 || (do_vim9_all && (*p == NUL
1831 || vim9_comment_start(p)))
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01001832 || (do_bar_cont && p[0] == '|' && p[1] != '|')))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001833 {
1834 garray_T ga;
1835
1836 ga_init2(&ga, (int)sizeof(char_u), 400);
1837 ga_concat(&ga, line);
1838 if (*p == '\\')
1839 ga_concat(&ga, p + 1);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01001840 else if (*p == '|')
1841 {
1842 ga_concat(&ga, (char_u *)" ");
1843 ga_concat(&ga, p);
1844 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001845 for (;;)
1846 {
1847 vim_free(sp->nextline);
1848 sp->nextline = get_one_sourceline(sp);
1849 if (sp->nextline == NULL)
1850 break;
1851 p = skipwhite(sp->nextline);
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01001852 if (*p == '\\' || (do_bar_cont && p[0] == '|' && p[1] != '|'))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001853 {
1854 // Adjust the growsize to the current length to speed up
1855 // concatenating many lines.
1856 if (ga.ga_len > 400)
1857 {
1858 if (ga.ga_len > 8000)
1859 ga.ga_growsize = 8000;
1860 else
1861 ga.ga_growsize = ga.ga_len;
1862 }
Bram Moolenaardcc58e02020-12-28 20:53:21 +01001863 if (*p == '\\')
1864 ga_concat(&ga, p + 1);
1865 else
1866 {
1867 ga_concat(&ga, (char_u *)" ");
1868 ga_concat(&ga, p);
1869 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001870 }
Bram Moolenaar5072b472021-06-03 21:56:10 +02001871 else if (!(p[0] == (comment_char)
Bram Moolenaar0f37e352021-06-02 15:28:15 +02001872 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01001873 && !(do_vim9_all && (*p == NUL || vim9_comment_start(p))))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001874 break;
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001875 /* drop a # comment or "\ comment line */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001876 }
1877 ga_append(&ga, NUL);
1878 vim_free(line);
1879 line = ga.ga_data;
1880 }
1881 }
1882
1883 if (line != NULL && sp->conv.vc_type != CONV_NONE)
1884 {
1885 char_u *s;
1886
1887 // Convert the encoding of the script line.
1888 s = string_convert(&sp->conv, line, NULL);
1889 if (s != NULL)
1890 {
1891 vim_free(line);
1892 line = s;
1893 }
1894 }
1895
1896#ifdef FEAT_EVAL
1897 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001898 if (sp->breakpoint != 0 && sp->breakpoint <= SOURCING_LNUM)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001899 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001900 dbg_breakpoint(sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001901 // Find next breakpoint.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001902 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001903 sp->dbg_tick = debug_tick;
1904 }
1905#endif
1906
1907 return line;
1908}
1909
1910/*
1911 * ":scriptencoding": Set encoding conversion for a sourced script.
1912 */
1913 void
1914ex_scriptencoding(exarg_T *eap)
1915{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001916 source_cookie_T *sp;
1917 char_u *name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001918
1919 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
1920 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00001921 emsg(_(e_scriptencoding_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001922 return;
1923 }
1924
1925 if (*eap->arg != NUL)
1926 {
1927 name = enc_canonize(eap->arg);
1928 if (name == NULL) // out of memory
1929 return;
1930 }
1931 else
1932 name = eap->arg;
1933
1934 // Setup for conversion from the specified encoding to 'encoding'.
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001935 sp = (source_cookie_T *)getline_cookie(eap->getline, eap->cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001936 convert_setup(&sp->conv, name, p_enc);
1937
1938 if (name != eap->arg)
1939 vim_free(name);
1940}
1941
1942/*
1943 * ":scriptversion": Set Vim script version for a sourced script.
1944 */
1945 void
1946ex_scriptversion(exarg_T *eap UNUSED)
1947{
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001948 int nr;
1949
1950 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
1951 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001952 emsg(_(e_scriptversion_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001953 return;
1954 }
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001955 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001957 emsg(_(e_cannot_use_scriptversion_after_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001958 return;
1959 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001960
1961 nr = getdigits(&eap->arg);
1962 if (nr == 0 || *eap->arg != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001963 emsg(_(e_invalid_argument));
Bram Moolenaar67979662020-06-20 22:50:47 +02001964 else if (nr > SCRIPT_VERSION_MAX)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001965 semsg(_(e_scriptversion_not_supported_nr), nr);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001966 else
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001967 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001968 current_sctx.sc_version = nr;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001969#ifdef FEAT_EVAL
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001970 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = nr;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001971#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001972 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001973}
1974
1975#if defined(FEAT_EVAL) || defined(PROTO)
1976/*
1977 * ":finish": Mark a sourced file as finished.
1978 */
1979 void
1980ex_finish(exarg_T *eap)
1981{
1982 if (getline_equal(eap->getline, eap->cookie, getsourceline))
1983 do_finish(eap, FALSE);
1984 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001985 emsg(_(e_finish_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001986}
1987
1988/*
1989 * Mark a sourced file as finished. Possibly makes the ":finish" pending.
1990 * Also called for a pending finish at the ":endtry" or after returning from
1991 * an extra do_cmdline(). "reanimate" is used in the latter case.
1992 */
1993 void
1994do_finish(exarg_T *eap, int reanimate)
1995{
1996 int idx;
1997
1998 if (reanimate)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001999 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002000 eap->cookie))->finished = FALSE;
2001
2002 // Cleanup (and inactivate) conditionals, but stop when a try conditional
2003 // not in its finally clause (which then is to be executed next) is found.
2004 // In this case, make the ":finish" pending for execution at the ":endtry".
2005 // Otherwise, finish normally.
2006 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
2007 if (idx >= 0)
2008 {
2009 eap->cstack->cs_pending[idx] = CSTP_FINISH;
2010 report_make_pending(CSTP_FINISH, NULL);
2011 }
2012 else
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002013 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002014 eap->cookie))->finished = TRUE;
2015}
2016
2017
2018/*
2019 * Return TRUE when a sourced file had the ":finish" command: Don't give error
2020 * message for missing ":endif".
2021 * Return FALSE when not sourcing a file.
2022 */
2023 int
2024source_finished(
Bram Moolenaar66250c92020-08-20 15:02:42 +02002025 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002026 void *cookie)
2027{
2028 return (getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002029 && ((source_cookie_T *)getline_cookie(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002030 fgetline, cookie))->finished);
2031}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002032
2033/*
2034 * Return the autoload script name for a function or variable name.
2035 * Returns NULL when out of memory.
2036 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
2037 */
2038 char_u *
2039autoload_name(char_u *name)
2040{
2041 char_u *p, *q = NULL;
2042 char_u *scriptname;
2043
2044 // Get the script file name: replace '#' with '/', append ".vim".
2045 scriptname = alloc(STRLEN(name) + 14);
2046 if (scriptname == NULL)
2047 return NULL;
2048 STRCPY(scriptname, "autoload/");
Bram Moolenaara1773442020-08-12 15:21:22 +02002049 STRCAT(scriptname, name[0] == 'g' && name[1] == ':' ? name + 2: name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002050 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
2051 q = p, ++p)
2052 *p = '/';
2053 STRCPY(q, ".vim");
2054 return scriptname;
2055}
2056
2057/*
2058 * If "name" has a package name try autoloading the script for it.
2059 * Return TRUE if a package was loaded.
2060 */
2061 int
2062script_autoload(
2063 char_u *name,
2064 int reload) // load script again when already loaded
2065{
2066 char_u *p;
2067 char_u *scriptname, *tofree;
2068 int ret = FALSE;
2069 int i;
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002070 int ret_sid;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002071
2072 // If there is no '#' after name[0] there is no package name.
2073 p = vim_strchr(name, AUTOLOAD_CHAR);
2074 if (p == NULL || p == name)
2075 return FALSE;
2076
2077 tofree = scriptname = autoload_name(name);
2078 if (scriptname == NULL)
2079 return FALSE;
2080
2081 // Find the name in the list of previously loaded package names. Skip
2082 // "autoload/", it's always the same.
2083 for (i = 0; i < ga_loaded.ga_len; ++i)
2084 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
2085 break;
2086 if (!reload && i < ga_loaded.ga_len)
2087 ret = FALSE; // was loaded already
2088 else
2089 {
2090 // Remember the name if it wasn't loaded already.
2091 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
2092 {
2093 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
2094 tofree = NULL;
2095 }
2096
2097 // Try loading the package from $VIMRUNTIME/autoload/<name>.vim
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002098 // Use "ret_sid" to avoid loading the same script again.
2099 if (source_in_path(p_rtp, scriptname, 0, &ret_sid) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002100 ret = TRUE;
2101 }
2102
2103 vim_free(tofree);
2104 return ret;
2105}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002106#endif