1 /* vi:set ts=8 sts=4 sw=4 noet:
2  *
3  * VIM - Vi IMproved	by Bram Moolenaar
4  *
5  * Do ":help uganda"  in Vim to read copying and usage conditions.
6  * Do ":help credits" in Vim to see a list of people who contributed.
7  * See README.txt for an overview of the Vim source code.
8  */
9 
10 /*
11  * scriptfile.c: functions for dealing with the runtime directories/files
12  */
13 
14 #include "vim.h"
15 
16 #if defined(FEAT_EVAL) || defined(PROTO)
17 // The names of packages that once were loaded are remembered.
18 static garray_T		ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
19 #endif
20 
21 /*
22  * Initialize the execution stack.
23  */
24     void
estack_init(void)25 estack_init(void)
26 {
27     estack_T *entry;
28 
29     if (ga_grow(&exestack, 10) == FAIL)
30 	mch_exit(0);
31     entry = ((estack_T *)exestack.ga_data) + exestack.ga_len;
32     entry->es_type = ETYPE_TOP;
33     entry->es_name = NULL;
34     entry->es_lnum = 0;
35 #ifdef FEAT_EVAL
36     entry->es_info.ufunc = NULL;
37 #endif
38     ++exestack.ga_len;
39 }
40 
41 /*
42  * Add an item to the execution stack.
43  * Returns the new entry or NULL when out of memory.
44  */
45     estack_T *
estack_push(etype_T type,char_u * name,long lnum)46 estack_push(etype_T type, char_u *name, long lnum)
47 {
48     estack_T *entry;
49 
50     // If memory allocation fails then we'll pop more than we push, eventually
51     // at the top level it will be OK again.
52     if (ga_grow(&exestack, 1) == OK)
53     {
54 	entry = ((estack_T *)exestack.ga_data) + exestack.ga_len;
55 	entry->es_type = type;
56 	entry->es_name = name;
57 	entry->es_lnum = lnum;
58 #ifdef FEAT_EVAL
59 	entry->es_info.ufunc = NULL;
60 #endif
61 	++exestack.ga_len;
62 	return entry;
63     }
64     return NULL;
65 }
66 
67 #if defined(FEAT_EVAL) || defined(PROTO)
68 /*
69  * Add a user function to the execution stack.
70  */
71     estack_T *
estack_push_ufunc(ufunc_T * ufunc,long lnum)72 estack_push_ufunc(ufunc_T *ufunc, long lnum)
73 {
74     estack_T *entry = estack_push(ETYPE_UFUNC,
75 	    ufunc->uf_name_exp != NULL
76 				  ? ufunc->uf_name_exp : ufunc->uf_name, lnum);
77     if (entry != NULL)
78 	entry->es_info.ufunc = ufunc;
79     return entry;
80 }
81 
82 /*
83  * Return TRUE if "ufunc" with "lnum" is already at the top of the exe stack.
84  */
85     int
estack_top_is_ufunc(ufunc_T * ufunc,long lnum)86 estack_top_is_ufunc(ufunc_T *ufunc, long lnum)
87 {
88     estack_T *entry;
89 
90     if (exestack.ga_len == 0)
91 	return FALSE;
92     entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
93     return entry->es_type == ETYPE_UFUNC
94 	&& STRCMP( entry->es_name, ufunc->uf_name_exp != NULL
95 				    ? ufunc->uf_name_exp : ufunc->uf_name) == 0
96 	&& entry->es_lnum == lnum;
97 }
98 #endif
99 
100 /*
101  * Take an item off of the execution stack and return it.
102  */
103     estack_T *
estack_pop(void)104 estack_pop(void)
105 {
106     if (exestack.ga_len == 0)
107 	return NULL;
108     --exestack.ga_len;
109     return ((estack_T *)exestack.ga_data) + exestack.ga_len;
110 }
111 
112 /*
113  * Get the current value for <sfile> in allocated memory.
114  * "which" is ESTACK_SFILE for <sfile> and ESTACK_STACK for <stack>.
115  */
116     char_u *
estack_sfile(estack_arg_T which UNUSED)117 estack_sfile(estack_arg_T which UNUSED)
118 {
119     estack_T	*entry;
120 #ifdef FEAT_EVAL
121     garray_T	ga;
122     size_t	len;
123     int		idx;
124     etype_T	last_type = ETYPE_SCRIPT;
125     char	*type_name;
126 #endif
127 
128     entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
129 #ifdef FEAT_EVAL
130     if (which == ESTACK_SFILE && entry->es_type != ETYPE_UFUNC)
131 #endif
132     {
133 	if (entry->es_name == NULL)
134 	    return NULL;
135 	return vim_strsave(entry->es_name);
136     }
137 #ifdef FEAT_EVAL
138     // 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 
152     // Give information about each stack entry up to the root.
153     // For a function we compose the call stack, as it was done in the past:
154     //   "function One[123]..Two[456]..Three"
155     ga_init2(&ga, sizeof(char), 100);
156     for (idx = 0; idx < exestack.ga_len; ++idx)
157     {
158 	entry = ((estack_T *)exestack.ga_data) + idx;
159 	if (entry->es_name != NULL)
160 	{
161 	    long    lnum = 0;
162 	    char    *dots;
163 
164 	    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);
177 	    if (ga_grow(&ga, (int)len) == FAIL)
178 		break;
179 	    if (idx == exestack.ga_len - 1)
180 		lnum = which == ESTACK_STACK ? SOURCING_LNUM : 0;
181 	    else
182 		lnum = entry->es_lnum;
183 	    dots = idx == exestack.ga_len - 1 ? "" : "..";
184 	    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.
188 		vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s%s",
189 				type_name, entry->es_name, dots);
190 	    else
191 		vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s[%ld]%s",
192 				    type_name, entry->es_name, lnum, dots);
193 	    ga.ga_len += (int)STRLEN((char *)ga.ga_data + ga.ga_len);
194 	}
195     }
196 
197     return (char_u *)ga.ga_data;
198 #endif
199 }
200 
201 /*
202  * ":runtime [what] {name}"
203  */
204     void
ex_runtime(exarg_T * eap)205 ex_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
source_callback(char_u * fname,void * cookie)237 source_callback(char_u *fname, void *cookie)
238 {
239     (void)do_source(fname, FALSE, DOSO_NONE, cookie);
240 }
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
do_in_path(char_u * path,char_u * name,int flags,void (* callback)(char_u * fname,void * ck),void * cookie)253 do_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     {
283 	if (p_verbose > 10 && name != NULL)
284 	{
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 
331 		    if (p_verbose > 10)
332 		    {
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)
362 	    semsg(_(e_dirnotf), basepath, name);
363 	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
do_in_path_and_pp(char_u * path,char_u * name,int flags,void (* callback)(char_u * fname,void * ck),void * cookie)390 do_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
do_in_runtimepath(char_u * name,int flags,void (* callback)(char_u * fname,void * ck),void * cookie)435 do_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
source_runtime(char_u * name,int flags)452 source_runtime(char_u *name, int flags)
453 {
454     return source_in_path(p_rtp, name, flags, NULL);
455 }
456 
457 /*
458  * Just like source_runtime(), but use "path" instead of 'runtimepath'.
459  */
460     int
source_in_path(char_u * path,char_u * name,int flags,int * ret_sid)461 source_in_path(char_u *path, char_u *name, int flags, int *ret_sid)
462 {
463     return do_in_path_and_pp(path, name, flags, source_callback, ret_sid);
464 }
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
source_all_matches(char_u * pat)473 source_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)
482 	    (void)do_source(files[i], FALSE, DOSO_NONE, NULL);
483 	FreeWild(num_files, files);
484     }
485 }
486 
487 /*
488  * Add the package directory to 'runtimepath'.
489  */
490     static int
add_pack_dir_to_rtp(char_u * fname)491 add_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 
628 theend:
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
load_pack_plugin(char_u * fname)639 load_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 
674 theend:
675     vim_free(ffname);
676     return retval;
677 }
678 
679 // used for "cookie" of add_pack_plugin()
680 static int APP_ADD_DIR;
681 static int APP_LOAD;
682 static int APP_BOTH;
683 
684     static void
add_pack_plugin(char_u * fname,void * cookie)685 add_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
add_pack_start_dirs(void)720 add_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
load_start_packages(void)730 load_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
ex_packloadall(exarg_T * eap)742 ex_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
ex_packadd(exarg_T * eap)758 ex_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 /*
789  * Sort "gap" and remove duplicate entries.  "gap" is expected to contain a
790  * list of file names in allocated memory.
791  */
792     void
remove_duplicates(garray_T * gap)793 remove_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 /*
811  * 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
ExpandRTDir(char_u * pat,int flags,int * num_file,char_u *** file,char * dirnames[])821 ExpandRTDir(
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
ExpandPackAddDir(char_u * pat,int * num_file,char_u *** file)917 ExpandPackAddDir(
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
cmd_source(char_u * fname,exarg_T * eap)965 cmd_source(char_u *fname, exarg_T *eap)
966 {
967     if (*fname == NUL)
968 	emsg(_(e_argreq));
969 
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
985     else if (do_source(fname, FALSE, DOSO_NONE, NULL) == FAIL)
986 	semsg(_(e_notopen), fname);
987 }
988 
989 /*
990  * ":source {fname}"
991  */
992     void
ex_source(exarg_T * eap)993 ex_source(exarg_T *eap)
994 {
995 #ifdef FEAT_BROWSE
996     if (cmdmod.cmod_flags & CMOD_BROWSE)
997     {
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
ex_options(exarg_T * eap UNUSED)1019 ex_options(
1020     exarg_T	*eap UNUSED)
1021 {
1022     char_u  buf[500];
1023     int	    multi_mods = 0;
1024 
1025     buf[0] = NUL;
1026     (void)add_win_cmd_modifers(buf, &cmdmod, &multi_mods);
1027 
1028     vim_setenv((char_u *)"OPTWIN_CMD", buf);
1029     cmd_source((char_u *)SYS_OPTWIN_FILE, NULL);
1030 }
1031 #endif
1032 
1033 /*
1034  * ":source" and associated commands.
1035  */
1036 
1037 #ifdef FEAT_EVAL
1038 /*
1039  * Return the address holding the next breakpoint line for a source cookie.
1040  */
1041     linenr_T *
source_breakpoint(void * cookie)1042 source_breakpoint(void *cookie)
1043 {
1044     return &((source_cookie_T *)cookie)->breakpoint;
1045 }
1046 
1047 /*
1048  * Return the address holding the debug tick for a source cookie.
1049  */
1050     int *
source_dbg_tick(void * cookie)1051 source_dbg_tick(void *cookie)
1052 {
1053     return &((source_cookie_T *)cookie)->dbg_tick;
1054 }
1055 
1056 /*
1057  * Return the nesting level for a source cookie.
1058  */
1059     int
source_level(void * cookie)1060 source_level(void *cookie)
1061 {
1062     return ((source_cookie_T *)cookie)->level;
1063 }
1064 
1065 /*
1066  * 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.
1068  */
1069     char_u *
source_nextline(void * cookie)1070 source_nextline(void *cookie)
1071 {
1072     return ((source_cookie_T *)cookie)->nextline;
1073 }
1074 #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 *
fopen_noinh_readbin(char * filename)1083 fopen_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.
1108  * When "ret_sid" is not NULL and we loaded the script before, don't load it
1109  * again.
1110  *
1111  * This function may be called recursively!
1112  *
1113  * 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.
1115  */
1116     int
do_source(char_u * fname,int check_other,int is_vimrc,int * ret_sid UNUSED)1117 do_source(
1118     char_u	*fname,
1119     int		check_other,	    // check for .vimrc and _vimrc
1120     int		is_vimrc,	    // DOSO_ value
1121     int		*ret_sid UNUSED)
1122 {
1123     source_cookie_T	    cookie;
1124     char_u		    *p;
1125     char_u		    *fname_exp;
1126     char_u		    *firstline = NULL;
1127     int			    retval = FAIL;
1128     sctx_T		    save_current_sctx;
1129 #ifdef FEAT_EVAL
1130     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;
1134     int			    sid;
1135     scriptitem_T	    *si = NULL;
1136     int			    save_estack_compiling = estack_compiling;
1137 #endif
1138 #ifdef STARTUPTIME
1139     struct timeval	    tv_rel;
1140     struct timeval	    tv_start;
1141 #endif
1142 #ifdef FEAT_PROFILE
1143     proftime_T		    wait_start;
1144 #endif
1145     int			    trigger_source_post = FALSE;
1146     ESTACK_CHECK_DECLARATION
1147 
1148     p = expand_env_save(fname);
1149     if (p == NULL)
1150 	return retval;
1151     fname_exp = fix_fname(p);
1152     vim_free(p);
1153     if (fname_exp == NULL)
1154 	return retval;
1155     if (mch_isdir(fname_exp))
1156     {
1157 	smsg(_("Cannot source a directory: \"%s\""), fname);
1158 	goto theend;
1159     }
1160 #ifdef FEAT_EVAL
1161     estack_compiling = FALSE;
1162 
1163     // See if we loaded this script before.
1164     for (sid = script_items.ga_len; sid > 0; --sid)
1165     {
1166 	// 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.
1171 	si = SCRIPT_ITEM(sid);
1172 	if (si->sn_name != NULL && fnamecmp(si->sn_name, fname_exp) == 0)
1173 		// Found it!
1174 		break;
1175     }
1176     if (sid > 0 && ret_sid != NULL)
1177     {
1178 	// Already loaded and no need to load again, return here.
1179 	*ret_sid = sid;
1180 	retval = OK;
1181 	goto theend;
1182     }
1183 #endif
1184 
1185     // 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();
1237 	    if (SOURCING_NAME == NULL)
1238 		smsg(_("could not source \"%s\""), fname);
1239 	    else
1240 		smsg(_("line %ld: could not source \"%s\""),
1241 							SOURCING_LNUM, fname);
1242 	    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();
1253 	if (SOURCING_NAME == NULL)
1254 	    smsg(_("sourcing \"%s\""), fname);
1255 	else
1256 	    smsg(_("line %ld: sourcing \"%s\""), SOURCING_LNUM, fname);
1257 	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.
1287     estack_push(ETYPE_SCRIPT, fname_exp, 0);
1288     ESTACK_CHECK_SETUP
1289 
1290 #ifdef STARTUPTIME
1291     if (time_fd != NULL)
1292 	time_push(&tv_rel, &tv_start);
1293 #endif
1294 
1295     save_current_sctx = current_sctx;
1296     current_sctx.sc_version = 1;  // default script version
1297 
1298 #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 
1308     current_sctx.sc_lnum = 0;
1309 
1310     // Check if this script was sourced before to finds its SID.
1311     // Always use a new sequence number.
1312     current_sctx.sc_seq = ++last_current_SID_seq;
1313     if (sid > 0)
1314     {
1315 	hashtab_T	*ht;
1316 	int		todo;
1317 	hashitem_T	*hi;
1318 	dictitem_T	*di;
1319 
1320 	// loading the same script again
1321 	si->sn_state = SN_STATE_RELOAD;
1322 	current_sctx.sc_sid = sid;
1323 
1324 	// 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.
1327 	ht = &SCRIPT_VARS(sid);
1328 	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 	    }
1336 	// imports can be redefined once
1337 	mark_imports_for_reload(sid);
1338 
1339 	// reset version, "vim9script" may have been added or removed.
1340 	si->sn_version = 1;
1341     }
1342     else
1343     {
1344 	// It's new, generate a new SID.
1345 	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 	{
1351 	    si = ALLOC_CLEAR_ONE(scriptitem_T);
1352 	    if (si == NULL)
1353 		goto almosttheend;
1354 	    ++script_items.ga_len;
1355 	    SCRIPT_ITEM(script_items.ga_len) = si;
1356 	    si->sn_name = NULL;
1357 	    si->sn_version = 1;
1358 
1359 	    // Allocate the local script variables to use for this script.
1360 	    new_script_vars(script_items.ga_len);
1361 	    ga_init2(&si->sn_var_vals, sizeof(svar_T), 10);
1362 	    hash_init(&si->sn_all_vars.dv_hashtab);
1363 	    ga_init2(&si->sn_imports, sizeof(imported_T), 10);
1364 	    ga_init2(&si->sn_type_list, sizeof(type_T), 10);
1365 # ifdef FEAT_PROFILE
1366 	    si->sn_prof_on = FALSE;
1367 # endif
1368 	}
1369 	si = SCRIPT_ITEM(current_sctx.sc_sid);
1370 	si->sn_name = fname_exp;
1371 	fname_exp = vim_strsave(si->sn_name);  // used for autocmd
1372 	if (ret_sid != NULL)
1373 	    *ret_sid = current_sctx.sc_sid;
1374 
1375 	// Used to check script variable index is still valid.
1376 	si->sn_script_seq = current_sctx.sc_seq;
1377     }
1378 
1379 # ifdef FEAT_PROFILE
1380     if (do_profiling == PROF_YES)
1381     {
1382 	int	forceit;
1383 
1384 	// Check if we do profiling for this script.
1385 	if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit))
1386 	{
1387 	    script_do_profile(si);
1388 	    si->sn_pr_force = forceit;
1389 	}
1390 	if (si->sn_prof_on)
1391 	{
1392 	    ++si->sn_pr_count;
1393 	    profile_start(&si->sn_pr_start);
1394 	    profile_zero(&si->sn_pr_children);
1395 	}
1396     }
1397 # endif
1398 #endif
1399 
1400     cookie.conv.vc_type = CONV_NONE;		// no conversion
1401 
1402     // Read the first line so we can check for a UTF-8 BOM.
1403     firstline = getsourceline(0, (void *)&cookie, 0, TRUE);
1404     if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef
1405 			      && firstline[1] == 0xbb && firstline[2] == 0xbf)
1406     {
1407 	// Found BOM; setup conversion, skip over BOM and recode the line.
1408 	convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
1409 	p = string_convert(&cookie.conv, firstline + 3, NULL);
1410 	if (p == NULL)
1411 	    p = vim_strsave(firstline + 3);
1412 	if (p != NULL)
1413 	{
1414 	    vim_free(firstline);
1415 	    firstline = p;
1416 	}
1417     }
1418 
1419     // Call do_cmdline, which will call getsourceline() to get the lines.
1420     do_cmdline(firstline, getsourceline, (void *)&cookie,
1421 				     DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT);
1422     retval = OK;
1423 
1424 #ifdef FEAT_PROFILE
1425     if (do_profiling == PROF_YES)
1426     {
1427 	// Get "si" again, "script_items" may have been reallocated.
1428 	si = SCRIPT_ITEM(current_sctx.sc_sid);
1429 	if (si->sn_prof_on)
1430 	{
1431 	    profile_end(&si->sn_pr_start);
1432 	    profile_sub_wait(&wait_start, &si->sn_pr_start);
1433 	    profile_add(&si->sn_pr_total, &si->sn_pr_start);
1434 	    profile_self(&si->sn_pr_self, &si->sn_pr_start,
1435 							 &si->sn_pr_children);
1436 	}
1437     }
1438 #endif
1439 
1440     if (got_int)
1441 	emsg(_(e_interr));
1442     ESTACK_CHECK_NOW
1443     estack_pop();
1444     if (p_verbose > 1)
1445     {
1446 	verbose_enter();
1447 	smsg(_("finished sourcing %s"), fname);
1448 	if (SOURCING_NAME != NULL)
1449 	    smsg(_("continuing in %s"), SOURCING_NAME);
1450 	verbose_leave();
1451     }
1452 #ifdef STARTUPTIME
1453     if (time_fd != NULL)
1454     {
1455 	vim_snprintf((char *)IObuff, IOSIZE, "sourcing %s", fname);
1456 	time_msg((char *)IObuff, &tv_start);
1457 	time_pop(&tv_rel);
1458     }
1459 #endif
1460 
1461     if (!got_int)
1462 	trigger_source_post = TRUE;
1463 
1464 #ifdef FEAT_EVAL
1465     // After a "finish" in debug mode, need to break at first command of next
1466     // sourced file.
1467     if (save_debug_break_level > ex_nesting_level
1468 	    && debug_break_level == ex_nesting_level)
1469 	++debug_break_level;
1470 #endif
1471 
1472 #ifdef FEAT_EVAL
1473 almosttheend:
1474     // Get "si" again, "script_items" may have been reallocated.
1475     si = SCRIPT_ITEM(current_sctx.sc_sid);
1476     if (si->sn_save_cpo != NULL)
1477     {
1478 	if (STRCMP(p_cpo, CPO_VIM) != 0)
1479 	{
1480 	    char_u *f;
1481 	    char_u *t;
1482 
1483 	    // 'cpo' was changed in the script.  Apply the same change to the
1484 	    // saved value, if possible.
1485 	    for (f = (char_u *)CPO_VIM; *f != NUL; ++f)
1486 		if (vim_strchr(p_cpo, *f) == NULL
1487 			&& (t = vim_strchr(si->sn_save_cpo, *f)) != NULL)
1488 		    // flag was removed, also remove it from the saved 'cpo'
1489 		    mch_memmove(t, t + 1, STRLEN(t));
1490 	    for (f = p_cpo; *f != NUL; ++f)
1491 		if (vim_strchr((char_u *)CPO_VIM, *f) == NULL
1492 			&& vim_strchr(si->sn_save_cpo, *f) == NULL)
1493 		{
1494 		    // flag was added, also add it to the saved 'cpo'
1495 		    t = alloc(STRLEN(si->sn_save_cpo) + 2);
1496 		    if (t != NULL)
1497 		    {
1498 			*t = *f;
1499 			STRCPY(t + 1, si->sn_save_cpo);
1500 			vim_free(si->sn_save_cpo);
1501 			si->sn_save_cpo = t;
1502 		    }
1503 		}
1504 	}
1505 	set_option_value((char_u *)"cpo", 0L, si->sn_save_cpo, OPT_NO_REDRAW);
1506 	VIM_CLEAR(si->sn_save_cpo);
1507     }
1508 
1509     restore_funccal();
1510 # ifdef FEAT_PROFILE
1511     if (do_profiling == PROF_YES)
1512 	prof_child_exit(&wait_start);		// leaving a child now
1513 # endif
1514 #endif
1515     current_sctx = save_current_sctx;
1516 
1517     fclose(cookie.fp);
1518     vim_free(cookie.nextline);
1519     vim_free(firstline);
1520     convert_setup(&cookie.conv, NULL, NULL);
1521 
1522     if (trigger_source_post)
1523 	apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp, FALSE, curbuf);
1524 
1525 theend:
1526     vim_free(fname_exp);
1527 #ifdef FEAT_EVAL
1528     estack_compiling = save_estack_compiling;
1529 #endif
1530     return retval;
1531 }
1532 
1533 #if defined(FEAT_EVAL) || defined(PROTO)
1534 
1535 /*
1536  * ":scriptnames"
1537  */
1538     void
ex_scriptnames(exarg_T * eap)1539 ex_scriptnames(exarg_T *eap)
1540 {
1541     int i;
1542 
1543     if (eap->addr_count > 0)
1544     {
1545 	// :script {scriptId}: edit the script
1546 	if (!SCRIPT_ID_VALID(eap->line2))
1547 	    emsg(_(e_invarg));
1548 	else
1549 	{
1550 	    eap->arg = SCRIPT_ITEM(eap->line2)->sn_name;
1551 	    do_exedit(eap, NULL);
1552 	}
1553 	return;
1554     }
1555 
1556     for (i = 1; i <= script_items.ga_len && !got_int; ++i)
1557 	if (SCRIPT_ITEM(i)->sn_name != NULL)
1558 	{
1559 	    home_replace(NULL, SCRIPT_ITEM(i)->sn_name,
1560 						    NameBuff, MAXPATHL, TRUE);
1561 	    smsg("%3d: %s", i, NameBuff);
1562 	}
1563 }
1564 
1565 # if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1566 /*
1567  * Fix slashes in the list of script names for 'shellslash'.
1568  */
1569     void
scriptnames_slash_adjust(void)1570 scriptnames_slash_adjust(void)
1571 {
1572     int i;
1573 
1574     for (i = 1; i <= script_items.ga_len; ++i)
1575 	if (SCRIPT_ITEM(i)->sn_name != NULL)
1576 	    slash_adjust(SCRIPT_ITEM(i)->sn_name);
1577 }
1578 # endif
1579 
1580 /*
1581  * Get a pointer to a script name.  Used for ":verbose set".
1582  * Message appended to "Last set from "
1583  */
1584     char_u *
get_scriptname(scid_T id)1585 get_scriptname(scid_T id)
1586 {
1587     if (id == SID_MODELINE)
1588 	return (char_u *)_("modeline");
1589     if (id == SID_CMDARG)
1590 	return (char_u *)_("--cmd argument");
1591     if (id == SID_CARG)
1592 	return (char_u *)_("-c argument");
1593     if (id == SID_ENV)
1594 	return (char_u *)_("environment variable");
1595     if (id == SID_ERROR)
1596 	return (char_u *)_("error handler");
1597     if (id == SID_WINLAYOUT)
1598 	return (char_u *)_("changed window size");
1599     return SCRIPT_ITEM(id)->sn_name;
1600 }
1601 
1602 # if defined(EXITFREE) || defined(PROTO)
1603     void
free_scriptnames(void)1604 free_scriptnames(void)
1605 {
1606     int			i;
1607 
1608     for (i = script_items.ga_len; i > 0; --i)
1609     {
1610 	scriptitem_T *si = SCRIPT_ITEM(i);
1611 
1612 	// the variables themselves are cleared in evalvars_clear()
1613 	vim_free(si->sn_vars);
1614 
1615 	vim_free(si->sn_name);
1616 	free_imports_and_script_vars(i);
1617 	free_string_option(si->sn_save_cpo);
1618 #  ifdef FEAT_PROFILE
1619 	ga_clear(&si->sn_prl_ga);
1620 #  endif
1621 	vim_free(si);
1622     }
1623     ga_clear(&script_items);
1624 }
1625 
1626     void
free_autoload_scriptnames(void)1627 free_autoload_scriptnames(void)
1628 {
1629     ga_clear_strings(&ga_loaded);
1630 }
1631 # endif
1632 
1633 #endif
1634 
1635     linenr_T
get_sourced_lnum(char_u * (* fgetline)(int,void *,int,getline_opt_T),void * cookie)1636 get_sourced_lnum(
1637 	char_u *(*fgetline)(int, void *, int, getline_opt_T),
1638 	void *cookie)
1639 {
1640     return fgetline == getsourceline
1641 			? ((source_cookie_T *)cookie)->sourcing_lnum
1642 			: SOURCING_LNUM;
1643 }
1644 
1645     static char_u *
get_one_sourceline(source_cookie_T * sp)1646 get_one_sourceline(source_cookie_T *sp)
1647 {
1648     garray_T		ga;
1649     int			len;
1650     int			c;
1651     char_u		*buf;
1652 #ifdef USE_CRNL
1653     int			has_cr;		// CR-LF found
1654 #endif
1655     int			have_read = FALSE;
1656 
1657     // use a growarray to store the sourced line
1658     ga_init2(&ga, 1, 250);
1659 
1660     // Loop until there is a finished line (or end-of-file).
1661     ++sp->sourcing_lnum;
1662     for (;;)
1663     {
1664 	// make room to read at least 120 (more) characters
1665 	if (ga_grow(&ga, 120) == FAIL)
1666 	    break;
1667 	buf = (char_u *)ga.ga_data;
1668 
1669 	if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
1670 							      sp->fp) == NULL)
1671 	    break;
1672 	len = ga.ga_len + (int)STRLEN(buf + ga.ga_len);
1673 #ifdef USE_CRNL
1674 	// Ignore a trailing CTRL-Z, when in Dos mode.	Only recognize the
1675 	// CTRL-Z by its own, or after a NL.
1676 	if (	   (len == 1 || (len >= 2 && buf[len - 2] == '\n'))
1677 		&& sp->fileformat == EOL_DOS
1678 		&& buf[len - 1] == Ctrl_Z)
1679 	{
1680 	    buf[len - 1] = NUL;
1681 	    break;
1682 	}
1683 #endif
1684 
1685 	have_read = TRUE;
1686 	ga.ga_len = len;
1687 
1688 	// If the line was longer than the buffer, read more.
1689 	if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n')
1690 	    continue;
1691 
1692 	if (len >= 1 && buf[len - 1] == '\n')	// remove trailing NL
1693 	{
1694 #ifdef USE_CRNL
1695 	    has_cr = (len >= 2 && buf[len - 2] == '\r');
1696 	    if (sp->fileformat == EOL_UNKNOWN)
1697 	    {
1698 		if (has_cr)
1699 		    sp->fileformat = EOL_DOS;
1700 		else
1701 		    sp->fileformat = EOL_UNIX;
1702 	    }
1703 
1704 	    if (sp->fileformat == EOL_DOS)
1705 	    {
1706 		if (has_cr)	    // replace trailing CR
1707 		{
1708 		    buf[len - 2] = '\n';
1709 		    --len;
1710 		    --ga.ga_len;
1711 		}
1712 		else	    // lines like ":map xx yy^M" will have failed
1713 		{
1714 		    if (!sp->error)
1715 		    {
1716 			msg_source(HL_ATTR(HLF_W));
1717 			emsg(_("W15: Warning: Wrong line separator, ^M may be missing"));
1718 		    }
1719 		    sp->error = TRUE;
1720 		    sp->fileformat = EOL_UNIX;
1721 		}
1722 	    }
1723 #endif
1724 	    // The '\n' is escaped if there is an odd number of ^V's just
1725 	    // before it, first set "c" just before the 'V's and then check
1726 	    // len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo
1727 	    for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--)
1728 		;
1729 	    if ((len & 1) != (c & 1))	// escaped NL, read more
1730 	    {
1731 		++sp->sourcing_lnum;
1732 		continue;
1733 	    }
1734 
1735 	    buf[len - 1] = NUL;		// remove the NL
1736 	}
1737 
1738 	// Check for ^C here now and then, so recursive :so can be broken.
1739 	line_breakcheck();
1740 	break;
1741     }
1742 
1743     if (have_read)
1744 	return (char_u *)ga.ga_data;
1745 
1746     vim_free(ga.ga_data);
1747     return NULL;
1748 }
1749 
1750 /*
1751  * Get one full line from a sourced file.
1752  * Called by do_cmdline() when it's called from do_source().
1753  *
1754  * Return a pointer to the line in allocated memory.
1755  * Return NULL for end-of-file or some error.
1756  */
1757     char_u *
getsourceline(int c UNUSED,void * cookie,int indent UNUSED,getline_opt_T options)1758 getsourceline(
1759 	int c UNUSED,
1760 	void *cookie,
1761 	int indent UNUSED,
1762 	getline_opt_T options)
1763 {
1764     source_cookie_T	*sp = (source_cookie_T *)cookie;
1765     char_u		*line;
1766     char_u		*p;
1767     int			do_vim9_all = in_vim9script()
1768 					      && options == GETLINE_CONCAT_ALL;
1769     int			do_bar_cont = do_vim9_all
1770 					 || options == GETLINE_CONCAT_CONTBAR;
1771 
1772 #ifdef FEAT_EVAL
1773     // If breakpoints have been added/deleted need to check for it.
1774     if (sp->dbg_tick < debug_tick)
1775     {
1776 	sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
1777 	sp->dbg_tick = debug_tick;
1778     }
1779 # ifdef FEAT_PROFILE
1780     if (do_profiling == PROF_YES)
1781 	script_line_end();
1782 # endif
1783 #endif
1784 
1785     // Set the current sourcing line number.
1786     SOURCING_LNUM = sp->sourcing_lnum + 1;
1787 
1788     // Get current line.  If there is a read-ahead line, use it, otherwise get
1789     // one now.  "fp" is NULL if actually using a string.
1790     if (sp->finished || sp->fp == NULL)
1791 	line = NULL;
1792     else if (sp->nextline == NULL)
1793 	line = get_one_sourceline(sp);
1794     else
1795     {
1796 	line = sp->nextline;
1797 	sp->nextline = NULL;
1798 	++sp->sourcing_lnum;
1799     }
1800 #ifdef FEAT_PROFILE
1801     if (line != NULL && do_profiling == PROF_YES)
1802 	script_line_start();
1803 #endif
1804 
1805     // Only concatenate lines starting with a \ when 'cpoptions' doesn't
1806     // contain the 'C' flag.
1807     if (line != NULL && options != GETLINE_NONE
1808 				      && vim_strchr(p_cpo, CPO_CONCAT) == NULL)
1809     {
1810 	int comment_char = in_vim9script() ? '#' : '"';
1811 
1812 	// compensate for the one line read-ahead
1813 	--sp->sourcing_lnum;
1814 
1815 	// Get the next line and concatenate it when it starts with a
1816 	// backslash. We always need to read the next line, keep it in
1817 	// sp->nextline.
1818 	/* Also check for a comment in between continuation lines: "\ */
1819 	// Also check for a Vim9 comment, empty line, line starting with '|',
1820 	// but not "||".
1821 	sp->nextline = get_one_sourceline(sp);
1822 	if (sp->nextline != NULL
1823 		&& (*(p = skipwhite(sp->nextline)) == '\\'
1824 			      || (p[0] == comment_char
1825 						&& p[1] == '\\' && p[2] == ' ')
1826 			      || (do_vim9_all && (*p == NUL
1827 						     || vim9_comment_start(p)))
1828 			      || (do_bar_cont && p[0] == '|' && p[1] != '|')))
1829 	{
1830 	    garray_T    ga;
1831 
1832 	    ga_init2(&ga, (int)sizeof(char_u), 400);
1833 	    ga_concat(&ga, line);
1834 	    if (*p == '\\')
1835 		ga_concat(&ga, p + 1);
1836 	    else if (*p == '|')
1837 	    {
1838 		ga_concat(&ga, (char_u *)" ");
1839 		ga_concat(&ga, p);
1840 	    }
1841 	    for (;;)
1842 	    {
1843 		vim_free(sp->nextline);
1844 		sp->nextline = get_one_sourceline(sp);
1845 		if (sp->nextline == NULL)
1846 		    break;
1847 		p = skipwhite(sp->nextline);
1848 		if (*p == '\\' || (do_bar_cont && p[0] == '|' && p[1] != '|'))
1849 		{
1850 		    // Adjust the growsize to the current length to speed up
1851 		    // concatenating many lines.
1852 		    if (ga.ga_len > 400)
1853 		    {
1854 			if (ga.ga_len > 8000)
1855 			    ga.ga_growsize = 8000;
1856 			else
1857 			    ga.ga_growsize = ga.ga_len;
1858 		    }
1859 		    if (*p == '\\')
1860 			ga_concat(&ga, p + 1);
1861 		    else
1862 		    {
1863 			ga_concat(&ga, (char_u *)" ");
1864 			ga_concat(&ga, p);
1865 		    }
1866 		}
1867 		else if (!(p[0] == (comment_char)
1868 						&& p[1] == '\\' && p[2] == ' ')
1869 		     && !(do_vim9_all && (*p == NUL || vim9_comment_start(p))))
1870 		    break;
1871 		/* drop a # comment or "\ comment line */
1872 	    }
1873 	    ga_append(&ga, NUL);
1874 	    vim_free(line);
1875 	    line = ga.ga_data;
1876 	}
1877     }
1878 
1879     if (line != NULL && sp->conv.vc_type != CONV_NONE)
1880     {
1881 	char_u	*s;
1882 
1883 	// Convert the encoding of the script line.
1884 	s = string_convert(&sp->conv, line, NULL);
1885 	if (s != NULL)
1886 	{
1887 	    vim_free(line);
1888 	    line = s;
1889 	}
1890     }
1891 
1892 #ifdef FEAT_EVAL
1893     // Did we encounter a breakpoint?
1894     if (sp->breakpoint != 0 && sp->breakpoint <= SOURCING_LNUM)
1895     {
1896 	dbg_breakpoint(sp->fname, SOURCING_LNUM);
1897 	// Find next breakpoint.
1898 	sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
1899 	sp->dbg_tick = debug_tick;
1900     }
1901 #endif
1902 
1903     return line;
1904 }
1905 
1906 /*
1907  * ":scriptencoding": Set encoding conversion for a sourced script.
1908  */
1909     void
ex_scriptencoding(exarg_T * eap)1910 ex_scriptencoding(exarg_T *eap)
1911 {
1912     source_cookie_T	*sp;
1913     char_u		*name;
1914 
1915     if (!getline_equal(eap->getline, eap->cookie, getsourceline))
1916     {
1917 	emsg(_("E167: :scriptencoding used outside of a sourced file"));
1918 	return;
1919     }
1920 
1921     if (*eap->arg != NUL)
1922     {
1923 	name = enc_canonize(eap->arg);
1924 	if (name == NULL)	// out of memory
1925 	    return;
1926     }
1927     else
1928 	name = eap->arg;
1929 
1930     // Setup for conversion from the specified encoding to 'encoding'.
1931     sp = (source_cookie_T *)getline_cookie(eap->getline, eap->cookie);
1932     convert_setup(&sp->conv, name, p_enc);
1933 
1934     if (name != eap->arg)
1935 	vim_free(name);
1936 }
1937 
1938 /*
1939  * ":scriptversion": Set Vim script version for a sourced script.
1940  */
1941     void
ex_scriptversion(exarg_T * eap UNUSED)1942 ex_scriptversion(exarg_T *eap UNUSED)
1943 {
1944     int		nr;
1945 
1946     if (!getline_equal(eap->getline, eap->cookie, getsourceline))
1947     {
1948 	emsg(_("E984: :scriptversion used outside of a sourced file"));
1949 	return;
1950     }
1951     if (in_vim9script())
1952     {
1953 	emsg(_(e_cannot_use_scriptversion_after_vim9script));
1954 	return;
1955     }
1956 
1957     nr = getdigits(&eap->arg);
1958     if (nr == 0 || *eap->arg != NUL)
1959 	emsg(_(e_invarg));
1960     else if (nr > SCRIPT_VERSION_MAX)
1961 	semsg(_("E999: scriptversion not supported: %d"), nr);
1962     else
1963     {
1964 	current_sctx.sc_version = nr;
1965 #ifdef FEAT_EVAL
1966 	SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = nr;
1967 #endif
1968     }
1969 }
1970 
1971 #if defined(FEAT_EVAL) || defined(PROTO)
1972 /*
1973  * ":finish": Mark a sourced file as finished.
1974  */
1975     void
ex_finish(exarg_T * eap)1976 ex_finish(exarg_T *eap)
1977 {
1978     if (getline_equal(eap->getline, eap->cookie, getsourceline))
1979 	do_finish(eap, FALSE);
1980     else
1981 	emsg(_("E168: :finish used outside of a sourced file"));
1982 }
1983 
1984 /*
1985  * Mark a sourced file as finished.  Possibly makes the ":finish" pending.
1986  * Also called for a pending finish at the ":endtry" or after returning from
1987  * an extra do_cmdline().  "reanimate" is used in the latter case.
1988  */
1989     void
do_finish(exarg_T * eap,int reanimate)1990 do_finish(exarg_T *eap, int reanimate)
1991 {
1992     int		idx;
1993 
1994     if (reanimate)
1995 	((source_cookie_T *)getline_cookie(eap->getline,
1996 					      eap->cookie))->finished = FALSE;
1997 
1998     // Cleanup (and inactivate) conditionals, but stop when a try conditional
1999     // not in its finally clause (which then is to be executed next) is found.
2000     // In this case, make the ":finish" pending for execution at the ":endtry".
2001     // Otherwise, finish normally.
2002     idx = cleanup_conditionals(eap->cstack, 0, TRUE);
2003     if (idx >= 0)
2004     {
2005 	eap->cstack->cs_pending[idx] = CSTP_FINISH;
2006 	report_make_pending(CSTP_FINISH, NULL);
2007     }
2008     else
2009 	((source_cookie_T *)getline_cookie(eap->getline,
2010 					       eap->cookie))->finished = TRUE;
2011 }
2012 
2013 
2014 /*
2015  * Return TRUE when a sourced file had the ":finish" command: Don't give error
2016  * message for missing ":endif".
2017  * Return FALSE when not sourcing a file.
2018  */
2019     int
source_finished(char_u * (* fgetline)(int,void *,int,getline_opt_T),void * cookie)2020 source_finished(
2021     char_u	*(*fgetline)(int, void *, int, getline_opt_T),
2022     void	*cookie)
2023 {
2024     return (getline_equal(fgetline, cookie, getsourceline)
2025 	    && ((source_cookie_T *)getline_cookie(
2026 						fgetline, cookie))->finished);
2027 }
2028 
2029 /*
2030  * Return the autoload script name for a function or variable name.
2031  * Returns NULL when out of memory.
2032  * Caller must make sure that "name" contains AUTOLOAD_CHAR.
2033  */
2034     char_u *
autoload_name(char_u * name)2035 autoload_name(char_u *name)
2036 {
2037     char_u	*p, *q = NULL;
2038     char_u	*scriptname;
2039 
2040     // Get the script file name: replace '#' with '/', append ".vim".
2041     scriptname = alloc(STRLEN(name) + 14);
2042     if (scriptname == NULL)
2043 	return NULL;
2044     STRCPY(scriptname, "autoload/");
2045     STRCAT(scriptname, name[0] == 'g' && name[1] == ':' ? name + 2: name);
2046     for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
2047 								    q = p, ++p)
2048 	*p = '/';
2049     STRCPY(q, ".vim");
2050     return scriptname;
2051 }
2052 
2053 /*
2054  * If "name" has a package name try autoloading the script for it.
2055  * Return TRUE if a package was loaded.
2056  */
2057     int
script_autoload(char_u * name,int reload)2058 script_autoload(
2059     char_u	*name,
2060     int		reload)	    // load script again when already loaded
2061 {
2062     char_u	*p;
2063     char_u	*scriptname, *tofree;
2064     int		ret = FALSE;
2065     int		i;
2066     int		ret_sid;
2067 
2068     // If there is no '#' after name[0] there is no package name.
2069     p = vim_strchr(name, AUTOLOAD_CHAR);
2070     if (p == NULL || p == name)
2071 	return FALSE;
2072 
2073     tofree = scriptname = autoload_name(name);
2074     if (scriptname == NULL)
2075 	return FALSE;
2076 
2077     // Find the name in the list of previously loaded package names.  Skip
2078     // "autoload/", it's always the same.
2079     for (i = 0; i < ga_loaded.ga_len; ++i)
2080 	if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
2081 	    break;
2082     if (!reload && i < ga_loaded.ga_len)
2083 	ret = FALSE;	    // was loaded already
2084     else
2085     {
2086 	// Remember the name if it wasn't loaded already.
2087 	if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
2088 	{
2089 	    ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
2090 	    tofree = NULL;
2091 	}
2092 
2093 	// Try loading the package from $VIMRUNTIME/autoload/<name>.vim
2094 	// Use "ret_sid" to avoid loading the same script again.
2095 	if (source_in_path(p_rtp, scriptname, 0, &ret_sid) == OK)
2096 	    ret = TRUE;
2097     }
2098 
2099     vim_free(tofree);
2100     return ret;
2101 }
2102 #endif
2103