blob: 39936a42650a4f5e21fdec8332a2aca13e8aff58 [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
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +000021// last used sequence number for sourcing scripts (current_sctx.sc_seq)
22#ifdef FEAT_EVAL
23static int last_current_SID_seq = 0;
24#endif
25
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +000026static int do_source_ext(char_u *fname, int check_other, int is_vimrc, int *ret_sid, exarg_T *eap, int clearvars);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +000027
Bram Moolenaar307c5a52019-08-25 15:41:00 +020028/*
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010029 * Initialize the execution stack.
30 */
31 void
32estack_init(void)
33{
34 estack_T *entry;
35
36 if (ga_grow(&exestack, 10) == FAIL)
37 mch_exit(0);
38 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len;
39 entry->es_type = ETYPE_TOP;
40 entry->es_name = NULL;
41 entry->es_lnum = 0;
Bram Moolenaar09d46402019-12-29 23:53:01 +010042#ifdef FEAT_EVAL
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010043 entry->es_info.ufunc = NULL;
Bram Moolenaar09d46402019-12-29 23:53:01 +010044#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010045 ++exestack.ga_len;
46}
47
48/*
49 * Add an item to the execution stack.
50 * Returns the new entry or NULL when out of memory.
51 */
52 estack_T *
53estack_push(etype_T type, char_u *name, long lnum)
54{
55 estack_T *entry;
56
57 // If memory allocation fails then we'll pop more than we push, eventually
58 // at the top level it will be OK again.
59 if (ga_grow(&exestack, 1) == OK)
60 {
61 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len;
62 entry->es_type = type;
63 entry->es_name = name;
64 entry->es_lnum = lnum;
Bram Moolenaar09d46402019-12-29 23:53:01 +010065#ifdef FEAT_EVAL
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010066 entry->es_info.ufunc = NULL;
Bram Moolenaar09d46402019-12-29 23:53:01 +010067#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010068 ++exestack.ga_len;
69 return entry;
70 }
71 return NULL;
72}
73
Bram Moolenaar09d46402019-12-29 23:53:01 +010074#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010075/*
76 * Add a user function to the execution stack.
77 */
Bram Moolenaarc620c052020-07-08 15:16:19 +020078 estack_T *
Bram Moolenaar25e0f582020-05-25 22:36:50 +020079estack_push_ufunc(ufunc_T *ufunc, long lnum)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010080{
Bram Moolenaar25e0f582020-05-25 22:36:50 +020081 estack_T *entry = estack_push(ETYPE_UFUNC,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010082 ufunc->uf_name_exp != NULL
83 ? ufunc->uf_name_exp : ufunc->uf_name, lnum);
84 if (entry != NULL)
85 entry->es_info.ufunc = ufunc;
Bram Moolenaarc620c052020-07-08 15:16:19 +020086 return entry;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010087}
Bram Moolenaar25e0f582020-05-25 22:36:50 +020088
89/*
90 * Return TRUE if "ufunc" with "lnum" is already at the top of the exe stack.
91 */
92 int
93estack_top_is_ufunc(ufunc_T *ufunc, long lnum)
94{
95 estack_T *entry;
96
97 if (exestack.ga_len == 0)
98 return FALSE;
99 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
100 return entry->es_type == ETYPE_UFUNC
101 && STRCMP( entry->es_name, ufunc->uf_name_exp != NULL
102 ? ufunc->uf_name_exp : ufunc->uf_name) == 0
103 && entry->es_lnum == lnum;
104}
Bram Moolenaar09d46402019-12-29 23:53:01 +0100105#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100106
107/*
Bram Moolenaarc620c052020-07-08 15:16:19 +0200108 * Take an item off of the execution stack and return it.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100109 */
Bram Moolenaarc620c052020-07-08 15:16:19 +0200110 estack_T *
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100111estack_pop(void)
112{
Bram Moolenaarc620c052020-07-08 15:16:19 +0200113 if (exestack.ga_len == 0)
114 return NULL;
115 --exestack.ga_len;
116 return ((estack_T *)exestack.ga_data) + exestack.ga_len;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100117}
118
119/*
120 * Get the current value for <sfile> in allocated memory.
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200121 * "which" is ESTACK_SFILE for <sfile> and ESTACK_STACK for <stack>.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100122 */
123 char_u *
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200124estack_sfile(estack_arg_T which UNUSED)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100125{
Bram Moolenaar85b09572019-12-30 10:57:00 +0100126 estack_T *entry;
127#ifdef FEAT_EVAL
Bram Moolenaara5d04232020-07-26 15:37:02 +0200128 garray_T ga;
Bram Moolenaar4d7a2482020-01-06 19:53:43 +0100129 size_t len;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100130 int idx;
Bram Moolenaara5d04232020-07-26 15:37:02 +0200131 etype_T last_type = ETYPE_SCRIPT;
132 char *type_name;
Bram Moolenaar85b09572019-12-30 10:57:00 +0100133#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100134
135 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100136#ifdef FEAT_EVAL
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200137 if (which == ESTACK_SFILE && entry->es_type != ETYPE_UFUNC)
Bram Moolenaar09d46402019-12-29 23:53:01 +0100138#endif
Bram Moolenaara5d04232020-07-26 15:37:02 +0200139 {
140 if (entry->es_name == NULL)
141 return NULL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100142 return vim_strsave(entry->es_name);
Bram Moolenaara5d04232020-07-26 15:37:02 +0200143 }
Bram Moolenaar09d46402019-12-29 23:53:01 +0100144#ifdef FEAT_EVAL
Bram Moolenaarc4492712021-11-22 15:37:15 +0000145 // expand('<sfile>') works in a function for backwards compatibility, but
146 // may give an unexpected result. Disallow it in Vim 9 script.
147 if (which == ESTACK_SFILE && in_vim9script())
148 {
149 int save_emsg_off = emsg_off;
150
151 if (emsg_off == 1)
152 // f_expand() silences errors but we do want this one
153 emsg_off = 0;
154 emsg(_(e_cannot_expand_sfile_in_vim9_function));
155 emsg_off = save_emsg_off;
156 return NULL;
157 }
158
Bram Moolenaara5d04232020-07-26 15:37:02 +0200159 // Give information about each stack entry up to the root.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100160 // For a function we compose the call stack, as it was done in the past:
161 // "function One[123]..Two[456]..Three"
Bram Moolenaara5d04232020-07-26 15:37:02 +0200162 ga_init2(&ga, sizeof(char), 100);
163 for (idx = 0; idx < exestack.ga_len; ++idx)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100164 {
165 entry = ((estack_T *)exestack.ga_data) + idx;
Bram Moolenaara5d04232020-07-26 15:37:02 +0200166 if (entry->es_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100167 {
Bram Moolenaara810db32020-09-11 17:59:23 +0200168 long lnum = 0;
169 char *dots;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200170
Bram Moolenaara5d04232020-07-26 15:37:02 +0200171 len = STRLEN(entry->es_name) + 15;
172 type_name = "";
173 if (entry->es_type != last_type)
174 {
175 switch (entry->es_type)
176 {
177 case ETYPE_SCRIPT: type_name = "script "; break;
178 case ETYPE_UFUNC: type_name = "function "; break;
179 default: type_name = ""; break;
180 }
181 last_type = entry->es_type;
182 }
183 len += STRLEN(type_name);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200184 if (ga_grow(&ga, (int)len) == FAIL)
Bram Moolenaara5d04232020-07-26 15:37:02 +0200185 break;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200186 if (idx == exestack.ga_len - 1)
187 lnum = which == ESTACK_STACK ? SOURCING_LNUM : 0;
188 else
189 lnum = entry->es_lnum;
Bram Moolenaara810db32020-09-11 17:59:23 +0200190 dots = idx == exestack.ga_len - 1 ? "" : "..";
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200191 if (lnum == 0)
192 // For the bottom entry of <sfile>: do not add the line number,
193 // it is used in <slnum>. Also leave it out when the number is
194 // not set.
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200195 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s%s",
Bram Moolenaara810db32020-09-11 17:59:23 +0200196 type_name, entry->es_name, dots);
Bram Moolenaara5d04232020-07-26 15:37:02 +0200197 else
Bram Moolenaara810db32020-09-11 17:59:23 +0200198 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s[%ld]%s",
199 type_name, entry->es_name, lnum, dots);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200200 ga.ga_len += (int)STRLEN((char *)ga.ga_data + ga.ga_len);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100201 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100202 }
203
Bram Moolenaara5d04232020-07-26 15:37:02 +0200204 return (char_u *)ga.ga_data;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100205#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100206}
207
208/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200209 * ":runtime [what] {name}"
210 */
211 void
212ex_runtime(exarg_T *eap)
213{
214 char_u *arg = eap->arg;
215 char_u *p = skiptowhite(arg);
216 int len = (int)(p - arg);
217 int flags = eap->forceit ? DIP_ALL : 0;
218
219 if (STRNCMP(arg, "START", len) == 0)
220 {
221 flags += DIP_START + DIP_NORTP;
222 arg = skipwhite(arg + len);
223 }
224 else if (STRNCMP(arg, "OPT", len) == 0)
225 {
226 flags += DIP_OPT + DIP_NORTP;
227 arg = skipwhite(arg + len);
228 }
229 else if (STRNCMP(arg, "PACK", len) == 0)
230 {
231 flags += DIP_START + DIP_OPT + DIP_NORTP;
232 arg = skipwhite(arg + len);
233 }
234 else if (STRNCMP(arg, "ALL", len) == 0)
235 {
236 flags += DIP_START + DIP_OPT;
237 arg = skipwhite(arg + len);
238 }
239
240 source_runtime(arg, flags);
241}
242
243 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244source_callback(char_u *fname, void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200245{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246 (void)do_source(fname, FALSE, DOSO_NONE, cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200247}
248
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000249#ifdef FEAT_EVAL
250/*
251 * Find an already loaded script "name".
252 * If found returns its script ID. If not found returns -1.
253 */
254 static int
255find_script_by_name(char_u *name)
256{
257 int sid;
258 scriptitem_T *si;
259
260 for (sid = script_items.ga_len; sid > 0; --sid)
261 {
262 // We used to check inode here, but that doesn't work:
263 // - If a script is edited and written, it may get a different
264 // inode number, even though to the user it is the same script.
265 // - If a script is deleted and another script is written, with a
266 // different name, the inode may be re-used.
267 si = SCRIPT_ITEM(sid);
268 if (si->sn_name != NULL && fnamecmp(si->sn_name, name) == 0)
269 return sid;
270 }
271 return -1;
272}
273
274/*
275 * Add a new scriptitem with all items initialized.
276 * When running out of memory "error" is set to FAIL.
277 * Returns the script ID.
278 */
279 static int
280get_new_scriptitem(int *error)
281{
282 static scid_T last_current_SID = 0;
283 int sid = ++last_current_SID;
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000284 scriptitem_T *si = NULL;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000285
286 if (ga_grow(&script_items, (int)(sid - script_items.ga_len)) == FAIL)
287 {
288 *error = FAIL;
289 return sid;
290 }
291 while (script_items.ga_len < sid)
292 {
293 si = ALLOC_CLEAR_ONE(scriptitem_T);
294 if (si == NULL)
295 {
296 *error = FAIL;
297 return sid;
298 }
299 ++script_items.ga_len;
300 SCRIPT_ITEM(script_items.ga_len) = si;
301 si->sn_name = NULL;
302 si->sn_version = 1;
303
304 // Allocate the local script variables to use for this script.
305 new_script_vars(script_items.ga_len);
306 ga_init2(&si->sn_var_vals, sizeof(svar_T), 10);
307 hash_init(&si->sn_all_vars.dv_hashtab);
308 ga_init2(&si->sn_imports, sizeof(imported_T), 10);
309 ga_init2(&si->sn_type_list, sizeof(type_T), 10);
310# ifdef FEAT_PROFILE
311 si->sn_prof_on = FALSE;
312# endif
313 }
314
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000315 // "si" can't be NULL, check only to avoid a compiler warning
316 if (si != NULL)
317 // Used to check script variable index is still valid.
318 si->sn_script_seq = current_sctx.sc_seq;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000319
320 return sid;
321}
322
323 static void
324find_script_callback(char_u *fname, void *cookie)
325{
326 int sid;
327 int error = OK;
328 int *ret_sid = cookie;
329
330 sid = find_script_by_name(fname);
331 if (sid < 0)
332 {
333 // script does not exist yet, create a new scriptitem
334 sid = get_new_scriptitem(&error);
335 if (error == OK)
336 {
337 scriptitem_T *si = SCRIPT_ITEM(sid);
338
339 si->sn_name = vim_strsave(fname);
340 si->sn_state = SN_STATE_NOT_LOADED;
341 }
342 }
343 *ret_sid = sid;
344}
345#endif
346
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200347/*
348 * Find the file "name" in all directories in "path" and invoke
349 * "callback(fname, cookie)".
350 * "name" can contain wildcards.
351 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
352 * When "flags" has DIP_DIR: find directories instead of files.
353 * When "flags" has DIP_ERR: give an error message if there is no match.
354 *
355 * return FAIL when no file could be sourced, OK otherwise.
356 */
357 int
358do_in_path(
359 char_u *path,
360 char_u *name,
361 int flags,
362 void (*callback)(char_u *fname, void *ck),
363 void *cookie)
364{
365 char_u *rtp;
366 char_u *np;
367 char_u *buf;
368 char_u *rtp_copy;
369 char_u *tail;
370 int num_files;
371 char_u **files;
372 int i;
373 int did_one = FALSE;
374#ifdef AMIGA
375 struct Process *proc = (struct Process *)FindTask(0L);
376 APTR save_winptr = proc->pr_WindowPtr;
377
378 // Avoid a requester here for a volume that doesn't exist.
379 proc->pr_WindowPtr = (APTR)-1L;
380#endif
381
382 // Make a copy of 'runtimepath'. Invoking the callback may change the
383 // value.
384 rtp_copy = vim_strsave(path);
385 buf = alloc(MAXPATHL);
386 if (buf != NULL && rtp_copy != NULL)
387 {
Bram Moolenaar647a5302020-05-03 17:01:24 +0200388 if (p_verbose > 10 && name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200389 {
390 verbose_enter();
391 smsg(_("Searching for \"%s\" in \"%s\""),
392 (char *)name, (char *)path);
393 verbose_leave();
394 }
395
396 // Loop over all entries in 'runtimepath'.
397 rtp = rtp_copy;
398 while (*rtp != NUL && ((flags & DIP_ALL) || !did_one))
399 {
400 size_t buflen;
401
402 // Copy the path from 'runtimepath' to buf[].
403 copy_option_part(&rtp, buf, MAXPATHL, ",");
404 buflen = STRLEN(buf);
405
406 // Skip after or non-after directories.
407 if (flags & (DIP_NOAFTER | DIP_AFTER))
408 {
409 int is_after = buflen >= 5
410 && STRCMP(buf + buflen - 5, "after") == 0;
411
412 if ((is_after && (flags & DIP_NOAFTER))
413 || (!is_after && (flags & DIP_AFTER)))
414 continue;
415 }
416
417 if (name == NULL)
418 {
419 (*callback)(buf, (void *) &cookie);
420 if (!did_one)
421 did_one = (cookie == NULL);
422 }
423 else if (buflen + STRLEN(name) + 2 < MAXPATHL)
424 {
425 add_pathsep(buf);
426 tail = buf + STRLEN(buf);
427
428 // Loop over all patterns in "name"
429 np = name;
430 while (*np != NUL && ((flags & DIP_ALL) || !did_one))
431 {
432 // Append the pattern from "name" to buf[].
433 copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)),
434 "\t ");
435
Bram Moolenaar647a5302020-05-03 17:01:24 +0200436 if (p_verbose > 10)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200437 {
438 verbose_enter();
439 smsg(_("Searching for \"%s\""), buf);
440 verbose_leave();
441 }
442
443 // Expand wildcards, invoke the callback for each match.
444 if (gen_expand_wildcards(1, &buf, &num_files, &files,
445 (flags & DIP_DIR) ? EW_DIR : EW_FILE) == OK)
446 {
447 for (i = 0; i < num_files; ++i)
448 {
449 (*callback)(files[i], cookie);
450 did_one = TRUE;
451 if (!(flags & DIP_ALL))
452 break;
453 }
454 FreeWild(num_files, files);
455 }
456 }
457 }
458 }
459 }
460 vim_free(buf);
461 vim_free(rtp_copy);
462 if (!did_one && name != NULL)
463 {
464 char *basepath = path == p_rtp ? "runtimepath" : "packpath";
465
466 if (flags & DIP_ERR)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000467 semsg(_(e_directory_not_found_in_str_str), basepath, name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200468 else if (p_verbose > 0)
469 {
470 verbose_enter();
471 smsg(_("not found in '%s': \"%s\""), basepath, name);
472 verbose_leave();
473 }
474 }
475
476#ifdef AMIGA
477 proc->pr_WindowPtr = save_winptr;
478#endif
479
480 return did_one ? OK : FAIL;
481}
482
483/*
484 * Find "name" in "path". When found, invoke the callback function for
485 * it: callback(fname, "cookie")
486 * When "flags" has DIP_ALL repeat for all matches, otherwise only the first
487 * one is used.
488 * Returns OK when at least one match found, FAIL otherwise.
489 *
490 * If "name" is NULL calls callback for each entry in "path". Cookie is
491 * passed by reference in this case, setting it to NULL indicates that callback
492 * has done its job.
493 */
494 static int
495do_in_path_and_pp(
496 char_u *path,
497 char_u *name,
498 int flags,
499 void (*callback)(char_u *fname, void *ck),
500 void *cookie)
501{
502 int done = FAIL;
503 char_u *s;
504 int len;
505 char *start_dir = "pack/*/start/*/%s";
506 char *opt_dir = "pack/*/opt/*/%s";
507
508 if ((flags & DIP_NORTP) == 0)
509 done = do_in_path(path, name, flags, callback, cookie);
510
511 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_START))
512 {
513 len = (int)(STRLEN(start_dir) + STRLEN(name));
514 s = alloc(len);
515 if (s == NULL)
516 return FAIL;
517 vim_snprintf((char *)s, len, start_dir, name);
518 done = do_in_path(p_pp, s, flags, callback, cookie);
519 vim_free(s);
520 }
521
522 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_OPT))
523 {
524 len = (int)(STRLEN(opt_dir) + STRLEN(name));
525 s = alloc(len);
526 if (s == NULL)
527 return FAIL;
528 vim_snprintf((char *)s, len, opt_dir, name);
529 done = do_in_path(p_pp, s, flags, callback, cookie);
530 vim_free(s);
531 }
532
533 return done;
534}
535
536/*
537 * Just like do_in_path_and_pp(), using 'runtimepath' for "path".
538 */
539 int
540do_in_runtimepath(
541 char_u *name,
542 int flags,
543 void (*callback)(char_u *fname, void *ck),
544 void *cookie)
545{
546 return do_in_path_and_pp(p_rtp, name, flags, callback, cookie);
547}
548
549/*
550 * Source the file "name" from all directories in 'runtimepath'.
551 * "name" can contain wildcards.
552 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
553 *
554 * return FAIL when no file could be sourced, OK otherwise.
555 */
556 int
557source_runtime(char_u *name, int flags)
558{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100559 return source_in_path(p_rtp, name, flags, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200560}
561
562/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000563 * Just like source_runtime(), but use "path" instead of 'runtimepath'
564 * and return the script ID in "ret_sid".
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200565 */
566 int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100567source_in_path(char_u *path, char_u *name, int flags, int *ret_sid)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200568{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100569 return do_in_path_and_pp(path, name, flags, source_callback, ret_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200570}
571
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200572#if defined(FEAT_EVAL) || defined(PROTO)
573
574/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000575 * Find "name" in 'runtimepath'. If found a new scriptitem is created for it
576 * and it's script ID is returned.
577 * If not found returns -1.
578 */
579 int
580find_script_in_rtp(char_u *name)
581{
582 int sid = -1;
583
584 (void)do_in_path_and_pp(p_rtp, name, DIP_NOAFTER,
585 find_script_callback, &sid);
586 return sid;
587}
588
589/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200590 * Expand wildcards in "pat" and invoke do_source() for each match.
591 */
592 static void
593source_all_matches(char_u *pat)
594{
595 int num_files;
596 char_u **files;
597 int i;
598
599 if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) == OK)
600 {
601 for (i = 0; i < num_files; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100602 (void)do_source(files[i], FALSE, DOSO_NONE, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200603 FreeWild(num_files, files);
604 }
605}
606
607/*
608 * Add the package directory to 'runtimepath'.
609 */
610 static int
611add_pack_dir_to_rtp(char_u *fname)
612{
613 char_u *p4, *p3, *p2, *p1, *p;
614 char_u *entry;
615 char_u *insp = NULL;
616 int c;
617 char_u *new_rtp;
618 int keep;
619 size_t oldlen;
620 size_t addlen;
621 size_t new_rtp_len;
622 char_u *afterdir = NULL;
623 size_t afterlen = 0;
624 char_u *after_insp = NULL;
625 char_u *ffname = NULL;
626 size_t fname_len;
627 char_u *buf = NULL;
628 char_u *rtp_ffname;
629 int match;
630 int retval = FAIL;
631
632 p4 = p3 = p2 = p1 = get_past_head(fname);
633 for (p = p1; *p; MB_PTR_ADV(p))
634 if (vim_ispathsep_nocolon(*p))
635 {
636 p4 = p3; p3 = p2; p2 = p1; p1 = p;
637 }
638
639 // now we have:
640 // rtp/pack/name/start/name
641 // p4 p3 p2 p1
642 //
643 // find the part up to "pack" in 'runtimepath'
644 c = *++p4; // append pathsep in order to expand symlink
645 *p4 = NUL;
646 ffname = fix_fname(fname);
647 *p4 = c;
648 if (ffname == NULL)
649 return FAIL;
650
651 // Find "ffname" in "p_rtp", ignoring '/' vs '\' differences.
652 // Also stop at the first "after" directory.
653 fname_len = STRLEN(ffname);
654 buf = alloc(MAXPATHL);
655 if (buf == NULL)
656 goto theend;
657 for (entry = p_rtp; *entry != NUL; )
658 {
659 char_u *cur_entry = entry;
660
661 copy_option_part(&entry, buf, MAXPATHL, ",");
662 if (insp == NULL)
663 {
664 add_pathsep(buf);
665 rtp_ffname = fix_fname(buf);
666 if (rtp_ffname == NULL)
667 goto theend;
668 match = vim_fnamencmp(rtp_ffname, ffname, fname_len) == 0;
669 vim_free(rtp_ffname);
670 if (match)
671 // Insert "ffname" after this entry (and comma).
672 insp = entry;
673 }
674
675 if ((p = (char_u *)strstr((char *)buf, "after")) != NULL
676 && p > buf
677 && vim_ispathsep(p[-1])
678 && (vim_ispathsep(p[5]) || p[5] == NUL || p[5] == ','))
679 {
680 if (insp == NULL)
681 // Did not find "ffname" before the first "after" directory,
682 // insert it before this entry.
683 insp = cur_entry;
684 after_insp = cur_entry;
685 break;
686 }
687 }
688
689 if (insp == NULL)
690 // Both "fname" and "after" not found, append at the end.
691 insp = p_rtp + STRLEN(p_rtp);
692
693 // check if rtp/pack/name/start/name/after exists
694 afterdir = concat_fnames(fname, (char_u *)"after", TRUE);
695 if (afterdir != NULL && mch_isdir(afterdir))
696 afterlen = STRLEN(afterdir) + 1; // add one for comma
697
698 oldlen = STRLEN(p_rtp);
699 addlen = STRLEN(fname) + 1; // add one for comma
700 new_rtp = alloc(oldlen + addlen + afterlen + 1); // add one for NUL
701 if (new_rtp == NULL)
702 goto theend;
703
704 // We now have 'rtp' parts: {keep}{keep_after}{rest}.
705 // Create new_rtp, first: {keep},{fname}
706 keep = (int)(insp - p_rtp);
707 mch_memmove(new_rtp, p_rtp, keep);
708 new_rtp_len = keep;
709 if (*insp == NUL)
710 new_rtp[new_rtp_len++] = ','; // add comma before
711 mch_memmove(new_rtp + new_rtp_len, fname, addlen - 1);
712 new_rtp_len += addlen - 1;
713 if (*insp != NUL)
714 new_rtp[new_rtp_len++] = ','; // add comma after
715
716 if (afterlen > 0 && after_insp != NULL)
717 {
718 int keep_after = (int)(after_insp - p_rtp);
719
720 // Add to new_rtp: {keep},{fname}{keep_after},{afterdir}
721 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep,
722 keep_after - keep);
723 new_rtp_len += keep_after - keep;
724 mch_memmove(new_rtp + new_rtp_len, afterdir, afterlen - 1);
725 new_rtp_len += afterlen - 1;
726 new_rtp[new_rtp_len++] = ',';
727 keep = keep_after;
728 }
729
730 if (p_rtp[keep] != NUL)
731 // Append rest: {keep},{fname}{keep_after},{afterdir}{rest}
732 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep, oldlen - keep + 1);
733 else
734 new_rtp[new_rtp_len] = NUL;
735
736 if (afterlen > 0 && after_insp == NULL)
737 {
738 // Append afterdir when "after" was not found:
739 // {keep},{fname}{rest},{afterdir}
740 STRCAT(new_rtp, ",");
741 STRCAT(new_rtp, afterdir);
742 }
743
744 set_option_value((char_u *)"rtp", 0L, new_rtp, 0);
745 vim_free(new_rtp);
746 retval = OK;
747
748theend:
749 vim_free(buf);
750 vim_free(ffname);
751 vim_free(afterdir);
752 return retval;
753}
754
755/*
756 * Load scripts in "plugin" and "ftdetect" directories of the package.
757 */
758 static int
759load_pack_plugin(char_u *fname)
760{
761 static char *plugpat = "%s/plugin/**/*.vim";
762 static char *ftpat = "%s/ftdetect/*.vim";
763 int len;
764 char_u *ffname = fix_fname(fname);
765 char_u *pat = NULL;
766 int retval = FAIL;
767
768 if (ffname == NULL)
769 return FAIL;
770 len = (int)STRLEN(ffname) + (int)STRLEN(ftpat);
771 pat = alloc(len);
772 if (pat == NULL)
773 goto theend;
774 vim_snprintf((char *)pat, len, plugpat, ffname);
775 source_all_matches(pat);
776
777 {
778 char_u *cmd = vim_strsave((char_u *)"g:did_load_filetypes");
779
780 // If runtime/filetype.vim wasn't loaded yet, the scripts will be
781 // found when it loads.
782 if (cmd != NULL && eval_to_number(cmd) > 0)
783 {
784 do_cmdline_cmd((char_u *)"augroup filetypedetect");
785 vim_snprintf((char *)pat, len, ftpat, ffname);
786 source_all_matches(pat);
787 do_cmdline_cmd((char_u *)"augroup END");
788 }
789 vim_free(cmd);
790 }
791 vim_free(pat);
792 retval = OK;
793
794theend:
795 vim_free(ffname);
796 return retval;
797}
798
799// used for "cookie" of add_pack_plugin()
800static int APP_ADD_DIR;
801static int APP_LOAD;
802static int APP_BOTH;
803
804 static void
805add_pack_plugin(char_u *fname, void *cookie)
806{
807 if (cookie != &APP_LOAD)
808 {
809 char_u *buf = alloc(MAXPATHL);
810 char_u *p;
811 int found = FALSE;
812
813 if (buf == NULL)
814 return;
815 p = p_rtp;
816 while (*p != NUL)
817 {
818 copy_option_part(&p, buf, MAXPATHL, ",");
819 if (pathcmp((char *)buf, (char *)fname, -1) == 0)
820 {
821 found = TRUE;
822 break;
823 }
824 }
825 vim_free(buf);
826 if (!found)
827 // directory is not yet in 'runtimepath', add it
828 if (add_pack_dir_to_rtp(fname) == FAIL)
829 return;
830 }
831
832 if (cookie != &APP_ADD_DIR)
833 load_pack_plugin(fname);
834}
835
836/*
837 * Add all packages in the "start" directory to 'runtimepath'.
838 */
839 void
840add_pack_start_dirs(void)
841{
842 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
843 add_pack_plugin, &APP_ADD_DIR);
844}
845
846/*
847 * Load plugins from all packages in the "start" directory.
848 */
849 void
850load_start_packages(void)
851{
852 did_source_packages = TRUE;
853 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
854 add_pack_plugin, &APP_LOAD);
855}
856
857/*
858 * ":packloadall"
859 * Find plugins in the package directories and source them.
860 */
861 void
862ex_packloadall(exarg_T *eap)
863{
864 if (!did_source_packages || eap->forceit)
865 {
866 // First do a round to add all directories to 'runtimepath', then load
867 // the plugins. This allows for plugins to use an autoload directory
868 // of another plugin.
869 add_pack_start_dirs();
870 load_start_packages();
871 }
872}
873
874/*
875 * ":packadd[!] {name}"
876 */
877 void
878ex_packadd(exarg_T *eap)
879{
880 static char *plugpat = "pack/*/%s/%s";
881 int len;
882 char *pat;
883 int round;
884 int res = OK;
885
886 // Round 1: use "start", round 2: use "opt".
887 for (round = 1; round <= 2; ++round)
888 {
889 // Only look under "start" when loading packages wasn't done yet.
890 if (round == 1 && did_source_packages)
891 continue;
892
893 len = (int)STRLEN(plugpat) + (int)STRLEN(eap->arg) + 5;
894 pat = alloc(len);
895 if (pat == NULL)
896 return;
897 vim_snprintf(pat, len, plugpat, round == 1 ? "start" : "opt", eap->arg);
898 // The first round don't give a "not found" error, in the second round
899 // only when nothing was found in the first round.
900 res = do_in_path(p_pp, (char_u *)pat,
901 DIP_ALL + DIP_DIR + (round == 2 && res == FAIL ? DIP_ERR : 0),
902 add_pack_plugin, eap->forceit ? &APP_ADD_DIR : &APP_BOTH);
903 vim_free(pat);
904 }
905}
906#endif
907
908/*
Bram Moolenaar26262f82019-09-04 20:59:15 +0200909 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
910 * list of file names in allocated memory.
911 */
912 void
913remove_duplicates(garray_T *gap)
914{
915 int i;
916 int j;
917 char_u **fnames = (char_u **)gap->ga_data;
918
919 sort_strings(fnames, gap->ga_len);
920 for (i = gap->ga_len - 1; i > 0; --i)
921 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
922 {
923 vim_free(fnames[i]);
924 for (j = i + 1; j < gap->ga_len; ++j)
925 fnames[j - 1] = fnames[j];
926 --gap->ga_len;
927 }
928}
929
930/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200931 * Expand color scheme, compiler or filetype names.
932 * Search from 'runtimepath':
933 * 'runtimepath'/{dirnames}/{pat}.vim
934 * When "flags" has DIP_START: search also from 'start' of 'packpath':
935 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
936 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
937 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
938 * "dirnames" is an array with one or more directory names.
939 */
940 int
941ExpandRTDir(
942 char_u *pat,
943 int flags,
944 int *num_file,
945 char_u ***file,
946 char *dirnames[])
947{
948 char_u *s;
949 char_u *e;
950 char_u *match;
951 garray_T ga;
952 int i;
953 int pat_len;
954
955 *num_file = 0;
956 *file = NULL;
957 pat_len = (int)STRLEN(pat);
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000958 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200959
960 for (i = 0; dirnames[i] != NULL; ++i)
961 {
962 s = alloc(STRLEN(dirnames[i]) + pat_len + 7);
963 if (s == NULL)
964 {
965 ga_clear_strings(&ga);
966 return FAIL;
967 }
968 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
969 globpath(p_rtp, s, &ga, 0);
970 vim_free(s);
971 }
972
973 if (flags & DIP_START) {
974 for (i = 0; dirnames[i] != NULL; ++i)
975 {
976 s = alloc(STRLEN(dirnames[i]) + pat_len + 22);
977 if (s == NULL)
978 {
979 ga_clear_strings(&ga);
980 return FAIL;
981 }
982 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
983 globpath(p_pp, s, &ga, 0);
984 vim_free(s);
985 }
986 }
987
988 if (flags & DIP_OPT) {
989 for (i = 0; dirnames[i] != NULL; ++i)
990 {
991 s = alloc(STRLEN(dirnames[i]) + pat_len + 20);
992 if (s == NULL)
993 {
994 ga_clear_strings(&ga);
995 return FAIL;
996 }
997 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
998 globpath(p_pp, s, &ga, 0);
999 vim_free(s);
1000 }
1001 }
1002
1003 for (i = 0; i < ga.ga_len; ++i)
1004 {
1005 match = ((char_u **)ga.ga_data)[i];
1006 s = match;
1007 e = s + STRLEN(s);
1008 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
1009 {
1010 e -= 4;
1011 for (s = e; s > match; MB_PTR_BACK(match, s))
1012 if (s < match || vim_ispathsep(*s))
1013 break;
1014 ++s;
1015 *e = NUL;
1016 mch_memmove(match, s, e - s + 1);
1017 }
1018 }
1019
1020 if (ga.ga_len == 0)
1021 return FAIL;
1022
1023 // Sort and remove duplicates which can happen when specifying multiple
1024 // directories in dirnames.
1025 remove_duplicates(&ga);
1026
1027 *file = ga.ga_data;
1028 *num_file = ga.ga_len;
1029 return OK;
1030}
1031
1032/*
1033 * Expand loadplugin names:
1034 * 'packpath'/pack/ * /opt/{pat}
1035 */
1036 int
1037ExpandPackAddDir(
1038 char_u *pat,
1039 int *num_file,
1040 char_u ***file)
1041{
1042 char_u *s;
1043 char_u *e;
1044 char_u *match;
1045 garray_T ga;
1046 int i;
1047 int pat_len;
1048
1049 *num_file = 0;
1050 *file = NULL;
1051 pat_len = (int)STRLEN(pat);
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001052 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001053
1054 s = alloc(pat_len + 26);
1055 if (s == NULL)
1056 {
1057 ga_clear_strings(&ga);
1058 return FAIL;
1059 }
1060 sprintf((char *)s, "pack/*/opt/%s*", pat);
1061 globpath(p_pp, s, &ga, 0);
1062 vim_free(s);
1063
1064 for (i = 0; i < ga.ga_len; ++i)
1065 {
1066 match = ((char_u **)ga.ga_data)[i];
1067 s = gettail(match);
1068 e = s + STRLEN(s);
1069 mch_memmove(match, s, e - s + 1);
1070 }
1071
1072 if (ga.ga_len == 0)
1073 return FAIL;
1074
1075 // Sort and remove duplicates which can happen when specifying multiple
1076 // directories in dirnames.
1077 remove_duplicates(&ga);
1078
1079 *file = ga.ga_data;
1080 *num_file = ga.ga_len;
1081 return OK;
1082}
1083
1084 static void
1085cmd_source(char_u *fname, exarg_T *eap)
1086{
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001087 int clearvars = FALSE;
1088
1089 if (*fname != NUL && STRNCMP(fname, "++clear", 7) == 0)
1090 {
1091 // ++clear argument is supplied
1092 clearvars = TRUE;
1093 fname = fname + 7;
1094 if (*fname != NUL)
1095 {
1096 semsg(_(e_invalid_argument_str), eap->arg);
1097 return;
1098 }
1099 }
1100
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001101 if (*fname != NUL && eap != NULL && eap->addr_count > 0)
1102 {
1103 // if a filename is specified to :source, then a range is not allowed
1104 emsg(_(e_no_range_allowed));
1105 return;
1106 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001107
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001108 if (eap != NULL && *fname == NUL)
1109 {
1110 if (eap->forceit)
1111 // a file name is needed to source normal mode commands
1112 emsg(_(e_argument_required));
1113 else
1114 // source ex commands from the current buffer
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001115 do_source_ext(NULL, FALSE, FALSE, NULL, eap, clearvars);
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001116 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001117 else if (eap != NULL && eap->forceit)
1118 // ":source!": read Normal mode commands
1119 // Need to execute the commands directly. This is required at least
1120 // for:
1121 // - ":g" command busy
1122 // - after ":argdo", ":windo" or ":bufdo"
1123 // - another command follows
1124 // - inside a loop
1125 openscript(fname, global_busy || listcmd_busy || eap->nextcmd != NULL
1126#ifdef FEAT_EVAL
1127 || eap->cstack->cs_idx >= 0
1128#endif
1129 );
1130
1131 // ":source" read ex commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132 else if (do_source(fname, FALSE, DOSO_NONE, NULL) == FAIL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001133 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001134}
1135
1136/*
1137 * ":source {fname}"
1138 */
1139 void
1140ex_source(exarg_T *eap)
1141{
1142#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02001143 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001144 {
1145 char_u *fname = NULL;
1146
1147 fname = do_browse(0, (char_u *)_("Source Vim script"), eap->arg,
1148 NULL, NULL,
1149 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
1150 if (fname != NULL)
1151 {
1152 cmd_source(fname, eap);
1153 vim_free(fname);
1154 }
1155 }
1156 else
1157#endif
1158 cmd_source(eap->arg, eap);
1159}
1160
1161#if defined(FEAT_EVAL) || defined(PROTO)
1162/*
1163 * ":options"
1164 */
1165 void
1166ex_options(
1167 exarg_T *eap UNUSED)
1168{
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001169 char_u buf[500];
1170 int multi_mods = 0;
1171
1172 buf[0] = NUL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001173 (void)add_win_cmd_modifers(buf, &cmdmod, &multi_mods);
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001174
1175 vim_setenv((char_u *)"OPTWIN_CMD", buf);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001176 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL);
1177}
1178#endif
1179
1180/*
1181 * ":source" and associated commands.
1182 */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001183
1184#ifdef FEAT_EVAL
1185/*
1186 * Return the address holding the next breakpoint line for a source cookie.
1187 */
1188 linenr_T *
1189source_breakpoint(void *cookie)
1190{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001191 return &((source_cookie_T *)cookie)->breakpoint;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001192}
1193
1194/*
1195 * Return the address holding the debug tick for a source cookie.
1196 */
1197 int *
1198source_dbg_tick(void *cookie)
1199{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001200 return &((source_cookie_T *)cookie)->dbg_tick;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001201}
1202
1203/*
1204 * Return the nesting level for a source cookie.
1205 */
1206 int
1207source_level(void *cookie)
1208{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001209 return ((source_cookie_T *)cookie)->level;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001210}
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001211
1212/*
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02001213 * Return the readahead line. Note that the pointer may become invalid when
1214 * getting the next line, if it's concatenated with the next one.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001215 */
1216 char_u *
1217source_nextline(void *cookie)
1218{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001219 return ((source_cookie_T *)cookie)->nextline;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001220}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001221#endif
1222
1223#if (defined(MSWIN) && defined(FEAT_CSCOPE)) || defined(HAVE_FD_CLOEXEC)
1224# define USE_FOPEN_NOINH
1225/*
1226 * Special function to open a file without handle inheritance.
1227 * When possible the handle is closed on exec().
1228 */
1229 static FILE *
1230fopen_noinh_readbin(char *filename)
1231{
1232# ifdef MSWIN
1233 int fd_tmp = mch_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0);
1234# else
1235 int fd_tmp = mch_open(filename, O_RDONLY, 0);
1236# endif
1237
1238 if (fd_tmp == -1)
1239 return NULL;
1240
1241# ifdef HAVE_FD_CLOEXEC
1242 {
1243 int fdflags = fcntl(fd_tmp, F_GETFD);
1244 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
1245 (void)fcntl(fd_tmp, F_SETFD, fdflags | FD_CLOEXEC);
1246 }
1247# endif
1248
1249 return fdopen(fd_tmp, READBIN);
1250}
1251#endif
1252
1253/*
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001254 * Initialization for sourcing lines from the current buffer. Reads all the
1255 * lines from the buffer and stores it in the cookie grow array.
1256 * Returns a pointer to the name ":source buffer=<n>" on success and NULL on
1257 * failure.
1258 */
1259 static char_u *
1260do_source_buffer_init(source_cookie_T *sp, exarg_T *eap)
1261{
1262 linenr_T curr_lnum;
1263 char_u *line = NULL;
1264 char_u *fname;
1265
1266 CLEAR_FIELD(*sp);
1267
1268 if (curbuf == NULL)
1269 return NULL;
1270
1271 // Use ":source buffer=<num>" as the script name
1272 vim_snprintf((char *)IObuff, IOSIZE, ":source buffer=%d", curbuf->b_fnum);
1273 fname = vim_strsave(IObuff);
1274 if (fname == NULL)
1275 return NULL;
1276
1277 ga_init2(&sp->buflines, sizeof(char_u *), 100);
1278
1279 // Copy the lines from the buffer into a grow array
1280 for (curr_lnum = eap->line1; curr_lnum <= eap->line2; curr_lnum++)
1281 {
1282 line = vim_strsave(ml_get(curr_lnum));
1283 if (line == NULL)
1284 goto errret;
1285 if (ga_add_string(&sp->buflines, line) == FAIL)
1286 goto errret;
1287 line = NULL;
1288 }
1289 sp->buf_lnum = 0;
1290 sp->source_from_buf = TRUE;
1291
1292 return fname;
1293
1294errret:
1295 vim_free(fname);
1296 vim_free(line);
1297 ga_clear_strings(&sp->buflines);
1298 return NULL;
1299}
1300
1301/*
1302 * Read the file "fname" and execute its lines as EX commands.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303 * When "ret_sid" is not NULL and we loaded the script before, don't load it
1304 * again.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001305 *
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001306 * The 'eap' argument is used when sourcing lines from a buffer instead of a
1307 * file.
1308 *
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001309 * If 'clearvars' is TRUE, then for scripts which are loaded more than
1310 * once, clear all the functions and variables previously defined in that
1311 * script.
1312 *
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001313 * This function may be called recursively!
1314 *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315 * Return FAIL if file could not be opened, OK otherwise.
1316 * If a scriptitem_T was found or created "*ret_sid" is set to the SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001317 */
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001318 static int
1319do_source_ext(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001320 char_u *fname,
1321 int check_other, // check for .vimrc and _vimrc
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001322 int is_vimrc, // DOSO_ value
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001323 int *ret_sid UNUSED,
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001324 exarg_T *eap,
1325 int clearvars UNUSED)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001326{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001327 source_cookie_T cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001328 char_u *p;
1329 char_u *fname_exp;
1330 char_u *firstline = NULL;
1331 int retval = FAIL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001332 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001333#ifdef FEAT_EVAL
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001334 funccal_entry_T funccalp_entry;
1335 int save_debug_break_level = debug_break_level;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336 int sid;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001337 scriptitem_T *si = NULL;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001338 int save_estack_compiling = estack_compiling;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001339#endif
1340#ifdef STARTUPTIME
1341 struct timeval tv_rel;
1342 struct timeval tv_start;
1343#endif
1344#ifdef FEAT_PROFILE
1345 proftime_T wait_start;
1346#endif
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001347 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001348 int trigger_source_post = FALSE;
Bram Moolenaare31ee862020-01-07 20:59:34 +01001349 ESTACK_CHECK_DECLARATION
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001350
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001351 CLEAR_FIELD(cookie);
1352 if (fname == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001353 {
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001354 // sourcing lines from a buffer
1355 fname_exp = do_source_buffer_init(&cookie, eap);
1356 if (fname_exp == NULL)
1357 return FAIL;
1358 }
1359 else
1360 {
1361 p = expand_env_save(fname);
1362 if (p == NULL)
1363 return retval;
1364 fname_exp = fix_fname(p);
1365 vim_free(p);
1366 if (fname_exp == NULL)
1367 return retval;
1368 if (mch_isdir(fname_exp))
1369 {
1370 smsg(_("Cannot source a directory: \"%s\""), fname);
1371 goto theend;
1372 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001373 }
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001374#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001375 estack_compiling = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001376
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377 // See if we loaded this script before.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001378 sid = find_script_by_name(fname_exp);
Bram Moolenaarf479cac2022-01-12 12:54:55 +00001379 if (sid > 0 && ret_sid != NULL
1380 && SCRIPT_ITEM(sid)->sn_state != SN_STATE_NOT_LOADED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001381 {
1382 // Already loaded and no need to load again, return here.
1383 *ret_sid = sid;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001384 retval = OK;
1385 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001386 }
1387#endif
1388
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001389 // Apply SourceCmd autocommands, they should get the file and source it.
1390 if (has_autocmd(EVENT_SOURCECMD, fname_exp, NULL)
1391 && apply_autocmds(EVENT_SOURCECMD, fname_exp, fname_exp,
1392 FALSE, curbuf))
1393 {
1394#ifdef FEAT_EVAL
1395 retval = aborting() ? FAIL : OK;
1396#else
1397 retval = OK;
1398#endif
1399 if (retval == OK)
1400 // Apply SourcePost autocommands.
1401 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp,
1402 FALSE, curbuf);
1403 goto theend;
1404 }
1405
1406 // Apply SourcePre autocommands, they may get the file.
1407 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf);
1408
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001409 if (!cookie.source_from_buf)
1410 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001411#ifdef USE_FOPEN_NOINH
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001412 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001413#else
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001414 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001415#endif
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001416 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001417 if (cookie.fp == NULL && check_other)
1418 {
1419 // Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
1420 // and ".exrc" by "_exrc" or vice versa.
1421 p = gettail(fname_exp);
1422 if ((*p == '.' || *p == '_')
1423 && (STRICMP(p + 1, "vimrc") == 0
1424 || STRICMP(p + 1, "gvimrc") == 0
1425 || STRICMP(p + 1, "exrc") == 0))
1426 {
1427 if (*p == '_')
1428 *p = '.';
1429 else
1430 *p = '_';
1431#ifdef USE_FOPEN_NOINH
1432 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1433#else
1434 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1435#endif
1436 }
1437 }
1438
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001439 if (cookie.fp == NULL && !cookie.source_from_buf)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001440 {
1441 if (p_verbose > 0)
1442 {
1443 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001444 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001445 smsg(_("could not source \"%s\""), fname);
1446 else
1447 smsg(_("line %ld: could not source \"%s\""),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001448 SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001449 verbose_leave();
1450 }
1451 goto theend;
1452 }
1453
1454 // The file exists.
1455 // - In verbose mode, give a message.
1456 // - For a vimrc file, may want to set 'compatible', call vimrc_found().
1457 if (p_verbose > 1)
1458 {
1459 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001460 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001461 smsg(_("sourcing \"%s\""), fname);
1462 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001463 smsg(_("line %ld: sourcing \"%s\""), SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001464 verbose_leave();
1465 }
1466 if (is_vimrc == DOSO_VIMRC)
1467 vimrc_found(fname_exp, (char_u *)"MYVIMRC");
1468 else if (is_vimrc == DOSO_GVIMRC)
1469 vimrc_found(fname_exp, (char_u *)"MYGVIMRC");
1470
1471#ifdef USE_CRNL
1472 // If no automatic file format: Set default to CR-NL.
1473 if (*p_ffs == NUL)
1474 cookie.fileformat = EOL_DOS;
1475 else
1476 cookie.fileformat = EOL_UNKNOWN;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001477#endif
1478
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001479 if (fname == NULL)
1480 // When sourcing a range of lines from a buffer, use the buffer line
1481 // number.
1482 cookie.sourcing_lnum = eap->line1 - 1;
1483 else
1484 cookie.sourcing_lnum = 0;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001485
1486#ifdef FEAT_EVAL
1487 // Check if this script has a breakpoint.
1488 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0);
1489 cookie.fname = fname_exp;
1490 cookie.dbg_tick = debug_tick;
1491
1492 cookie.level = ex_nesting_level;
1493#endif
1494
1495 // Keep the sourcing name/lnum, for recursive calls.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001496 estack_push(ETYPE_SCRIPT, fname_exp, 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001497 ESTACK_CHECK_SETUP
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001498
1499#ifdef STARTUPTIME
1500 if (time_fd != NULL)
1501 time_push(&tv_rel, &tv_start);
1502#endif
1503
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001504 // "legacy" does not apply to commands in the script
1505 sticky_cmdmod_flags = 0;
1506
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001507 save_current_sctx = current_sctx;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001508 if (cmdmod.cmod_flags & CMOD_VIM9CMD)
1509 // When the ":vim9cmd" command modifier is used, source the script as a
1510 // Vim9 script.
1511 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1512 else
1513 current_sctx.sc_version = 1; // default script version
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001514
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001515#ifdef FEAT_EVAL
1516# ifdef FEAT_PROFILE
1517 if (do_profiling == PROF_YES)
1518 prof_child_enter(&wait_start); // entering a child now
1519# endif
1520
1521 // Don't use local function variables, if called from a function.
1522 // Also starts profiling timer for nested script.
1523 save_funccal(&funccalp_entry);
1524
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001525 current_sctx.sc_lnum = 0;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001526
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001527 // Check if this script was sourced before to find its SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001528 // Always use a new sequence number.
1529 current_sctx.sc_seq = ++last_current_SID_seq;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001530 if (sid > 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001531 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001532 hashtab_T *ht;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001533 int todo;
1534 hashitem_T *hi;
1535 dictitem_T *di;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536
1537 // loading the same script again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001538 current_sctx.sc_sid = sid;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001539 si = SCRIPT_ITEM(sid);
1540 if (si->sn_state == SN_STATE_NOT_LOADED)
1541 {
1542 // this script was found but not loaded yet
1543 si->sn_state = SN_STATE_NEW;
1544 }
1545 else
1546 {
1547 si->sn_state = SN_STATE_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001548
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001549 if (!clearvars)
1550 {
1551 // Script-local variables remain but "const" can be set again.
1552 // In Vim9 script variables will be cleared when "vim9script"
1553 // is encountered without the "noclear" argument.
1554 ht = &SCRIPT_VARS(sid);
1555 todo = (int)ht->ht_used;
1556 for (hi = ht->ht_array; todo > 0; ++hi)
1557 if (!HASHITEM_EMPTY(hi))
1558 {
1559 --todo;
1560 di = HI2DI(hi);
1561 di->di_flags |= DI_FLAGS_RELOAD;
1562 }
1563 // imports can be redefined once
1564 mark_imports_for_reload(sid);
1565 }
1566 else
1567 clear_vim9_scriptlocal_vars(sid);
Bram Moolenaar0123cc12021-02-07 17:17:58 +01001568
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001569 // reset version, "vim9script" may have been added or removed.
1570 si->sn_version = 1;
1571 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001572 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573 else
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001574 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001575 int error = OK;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001576
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001577 // It's new, generate a new SID and initialize the scriptitem.
1578 current_sctx.sc_sid = get_new_scriptitem(&error);
1579 if (error == FAIL)
1580 goto almosttheend;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001581 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001582 si->sn_name = fname_exp;
1583 fname_exp = vim_strsave(si->sn_name); // used for autocmd
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001584 if (ret_sid != NULL)
1585 *ret_sid = current_sctx.sc_sid;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001586
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001587 // Remember the "is_vimrc" flag for when the file is sourced again.
1588 si->sn_is_vimrc = is_vimrc;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001589 }
1590
1591# ifdef FEAT_PROFILE
1592 if (do_profiling == PROF_YES)
1593 {
1594 int forceit;
1595
1596 // Check if we do profiling for this script.
1597 if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit))
1598 {
1599 script_do_profile(si);
1600 si->sn_pr_force = forceit;
1601 }
1602 if (si->sn_prof_on)
1603 {
1604 ++si->sn_pr_count;
1605 profile_start(&si->sn_pr_start);
1606 profile_zero(&si->sn_pr_children);
1607 }
1608 }
1609# endif
1610#endif
1611
1612 cookie.conv.vc_type = CONV_NONE; // no conversion
1613
1614 // Read the first line so we can check for a UTF-8 BOM.
1615 firstline = getsourceline(0, (void *)&cookie, 0, TRUE);
1616 if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef
1617 && firstline[1] == 0xbb && firstline[2] == 0xbf)
1618 {
1619 // Found BOM; setup conversion, skip over BOM and recode the line.
1620 convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
1621 p = string_convert(&cookie.conv, firstline + 3, NULL);
1622 if (p == NULL)
1623 p = vim_strsave(firstline + 3);
1624 if (p != NULL)
1625 {
1626 vim_free(firstline);
1627 firstline = p;
1628 }
1629 }
1630
1631 // Call do_cmdline, which will call getsourceline() to get the lines.
1632 do_cmdline(firstline, getsourceline, (void *)&cookie,
1633 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT);
1634 retval = OK;
1635
1636#ifdef FEAT_PROFILE
1637 if (do_profiling == PROF_YES)
1638 {
1639 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001640 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001641 if (si->sn_prof_on)
1642 {
1643 profile_end(&si->sn_pr_start);
1644 profile_sub_wait(&wait_start, &si->sn_pr_start);
1645 profile_add(&si->sn_pr_total, &si->sn_pr_start);
1646 profile_self(&si->sn_pr_self, &si->sn_pr_start,
1647 &si->sn_pr_children);
1648 }
1649 }
1650#endif
1651
1652 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001653 emsg(_(e_interrupted));
Bram Moolenaare31ee862020-01-07 20:59:34 +01001654 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001655 estack_pop();
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001656 if (p_verbose > 1)
1657 {
1658 verbose_enter();
1659 smsg(_("finished sourcing %s"), fname);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001660 if (SOURCING_NAME != NULL)
1661 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001662 verbose_leave();
1663 }
1664#ifdef STARTUPTIME
1665 if (time_fd != NULL)
1666 {
1667 vim_snprintf((char *)IObuff, IOSIZE, "sourcing %s", fname);
1668 time_msg((char *)IObuff, &tv_start);
1669 time_pop(&tv_rel);
1670 }
1671#endif
1672
1673 if (!got_int)
1674 trigger_source_post = TRUE;
1675
1676#ifdef FEAT_EVAL
1677 // After a "finish" in debug mode, need to break at first command of next
1678 // sourced file.
1679 if (save_debug_break_level > ex_nesting_level
1680 && debug_break_level == ex_nesting_level)
1681 ++debug_break_level;
1682#endif
1683
1684#ifdef FEAT_EVAL
1685almosttheend:
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001686 // If "sn_save_cpo" is set that means we encountered "vim9script": restore
1687 // 'cpoptions', unless in the main .vimrc file.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001688 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001689 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001690 if (si->sn_save_cpo != NULL && si->sn_is_vimrc == DOSO_NONE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691 {
Bram Moolenaar3e191692021-03-17 17:46:00 +01001692 if (STRCMP(p_cpo, CPO_VIM) != 0)
1693 {
1694 char_u *f;
1695 char_u *t;
1696
1697 // 'cpo' was changed in the script. Apply the same change to the
1698 // saved value, if possible.
1699 for (f = (char_u *)CPO_VIM; *f != NUL; ++f)
1700 if (vim_strchr(p_cpo, *f) == NULL
1701 && (t = vim_strchr(si->sn_save_cpo, *f)) != NULL)
1702 // flag was removed, also remove it from the saved 'cpo'
1703 mch_memmove(t, t + 1, STRLEN(t));
1704 for (f = p_cpo; *f != NUL; ++f)
1705 if (vim_strchr((char_u *)CPO_VIM, *f) == NULL
1706 && vim_strchr(si->sn_save_cpo, *f) == NULL)
1707 {
1708 // flag was added, also add it to the saved 'cpo'
1709 t = alloc(STRLEN(si->sn_save_cpo) + 2);
1710 if (t != NULL)
1711 {
1712 *t = *f;
1713 STRCPY(t + 1, si->sn_save_cpo);
1714 vim_free(si->sn_save_cpo);
1715 si->sn_save_cpo = t;
1716 }
1717 }
1718 }
Bram Moolenaar37294bd2021-03-10 13:40:08 +01001719 set_option_value((char_u *)"cpo", 0L, si->sn_save_cpo, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 }
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001721 VIM_CLEAR(si->sn_save_cpo);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001722
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001723 restore_funccal();
1724# ifdef FEAT_PROFILE
1725 if (do_profiling == PROF_YES)
1726 prof_child_exit(&wait_start); // leaving a child now
1727# endif
1728#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001729 current_sctx = save_current_sctx;
1730
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001731 if (cookie.fp != NULL)
1732 fclose(cookie.fp);
1733 if (cookie.source_from_buf)
1734 ga_clear_strings(&cookie.buflines);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001735 vim_free(cookie.nextline);
1736 vim_free(firstline);
1737 convert_setup(&cookie.conv, NULL, NULL);
1738
1739 if (trigger_source_post)
1740 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp, FALSE, curbuf);
1741
1742theend:
1743 vim_free(fname_exp);
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001744 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001745#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001746 estack_compiling = save_estack_compiling;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001747#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001748 return retval;
1749}
1750
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001751 int
1752do_source(
1753 char_u *fname,
1754 int check_other, // check for .vimrc and _vimrc
1755 int is_vimrc, // DOSO_ value
1756 int *ret_sid UNUSED)
1757{
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001758 return do_source_ext(fname, check_other, is_vimrc, ret_sid, NULL, FALSE);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001759}
1760
1761
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001762#if defined(FEAT_EVAL) || defined(PROTO)
1763
1764/*
1765 * ":scriptnames"
1766 */
1767 void
1768ex_scriptnames(exarg_T *eap)
1769{
1770 int i;
1771
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001772 if (eap->addr_count > 0 || *eap->arg != NUL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001773 {
1774 // :script {scriptId}: edit the script
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001775 if (eap->addr_count > 0 && !SCRIPT_ID_VALID(eap->line2))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001776 emsg(_(e_invalid_argument));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001777 else
1778 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001779 if (eap->addr_count > 0)
1780 eap->arg = SCRIPT_ITEM(eap->line2)->sn_name;
1781 else
1782 {
1783 expand_env(eap->arg, NameBuff, MAXPATHL);
1784 eap->arg = NameBuff;
1785 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001786 do_exedit(eap, NULL);
1787 }
1788 return;
1789 }
1790
1791 for (i = 1; i <= script_items.ga_len && !got_int; ++i)
Bram Moolenaar6079da72022-01-18 14:16:59 +00001792 {
1793 scriptitem_T *si = SCRIPT_ITEM(i);
1794
1795 if (si->sn_name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001796 {
Bram Moolenaar6079da72022-01-18 14:16:59 +00001797 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
1798 vim_snprintf((char *)IObuff, IOSIZE, "%3d%s: %s",
1799 i,
1800 si->sn_state == SN_STATE_NOT_LOADED ? " A" : "",
1801 NameBuff);
Bram Moolenaar769f5892022-02-09 14:31:05 +00001802 if (!message_filtered(IObuff))
1803 {
1804 msg_putchar('\n');
1805 msg_outtrans(IObuff);
1806 out_flush(); // output one line at a time
1807 ui_breakcheck();
1808 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001809 }
Bram Moolenaar6079da72022-01-18 14:16:59 +00001810 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001811}
1812
1813# if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1814/*
1815 * Fix slashes in the list of script names for 'shellslash'.
1816 */
1817 void
1818scriptnames_slash_adjust(void)
1819{
1820 int i;
1821
1822 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001823 if (SCRIPT_ITEM(i)->sn_name != NULL)
1824 slash_adjust(SCRIPT_ITEM(i)->sn_name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001825}
1826# endif
1827
1828/*
1829 * Get a pointer to a script name. Used for ":verbose set".
Bram Moolenaar74667062020-12-28 15:41:41 +01001830 * Message appended to "Last set from "
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001831 */
1832 char_u *
1833get_scriptname(scid_T id)
1834{
1835 if (id == SID_MODELINE)
1836 return (char_u *)_("modeline");
1837 if (id == SID_CMDARG)
1838 return (char_u *)_("--cmd argument");
1839 if (id == SID_CARG)
1840 return (char_u *)_("-c argument");
1841 if (id == SID_ENV)
1842 return (char_u *)_("environment variable");
1843 if (id == SID_ERROR)
1844 return (char_u *)_("error handler");
Bram Moolenaar74667062020-12-28 15:41:41 +01001845 if (id == SID_WINLAYOUT)
1846 return (char_u *)_("changed window size");
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001847 return SCRIPT_ITEM(id)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001848}
1849
1850# if defined(EXITFREE) || defined(PROTO)
1851 void
1852free_scriptnames(void)
1853{
1854 int i;
1855
1856 for (i = script_items.ga_len; i > 0; --i)
Bram Moolenaar86173482019-10-01 17:02:16 +02001857 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001858 scriptitem_T *si = SCRIPT_ITEM(i);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001859
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001860 // the variables themselves are cleared in evalvars_clear()
1861 vim_free(si->sn_vars);
1862
1863 vim_free(si->sn_name);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001864 free_imports_and_script_vars(i);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001865 free_string_option(si->sn_save_cpo);
Bram Moolenaara720be72019-10-22 21:45:19 +02001866# ifdef FEAT_PROFILE
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001867 ga_clear(&si->sn_prl_ga);
Bram Moolenaara720be72019-10-22 21:45:19 +02001868# endif
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00001869 vim_free(si->sn_autoload_prefix);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001870 vim_free(si);
Bram Moolenaar86173482019-10-01 17:02:16 +02001871 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001872 ga_clear(&script_items);
1873}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001874
1875 void
1876free_autoload_scriptnames(void)
1877{
1878 ga_clear_strings(&ga_loaded);
1879}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001880# endif
1881
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001882 linenr_T
Bram Moolenaar66250c92020-08-20 15:02:42 +02001883get_sourced_lnum(
1884 char_u *(*fgetline)(int, void *, int, getline_opt_T),
1885 void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001886{
1887 return fgetline == getsourceline
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001888 ? ((source_cookie_T *)cookie)->sourcing_lnum
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001889 : SOURCING_LNUM;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001890}
Dominique Pelle748b3082022-01-08 12:41:16 +00001891#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001892
1893 static char_u *
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001894get_one_sourceline(source_cookie_T *sp)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001895{
1896 garray_T ga;
1897 int len;
1898 int c;
1899 char_u *buf;
1900#ifdef USE_CRNL
1901 int has_cr; // CR-LF found
1902#endif
1903 int have_read = FALSE;
1904
1905 // use a growarray to store the sourced line
1906 ga_init2(&ga, 1, 250);
1907
1908 // Loop until there is a finished line (or end-of-file).
1909 ++sp->sourcing_lnum;
1910 for (;;)
1911 {
1912 // make room to read at least 120 (more) characters
1913 if (ga_grow(&ga, 120) == FAIL)
1914 break;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001915 if (sp->source_from_buf)
1916 {
1917 if (sp->buf_lnum >= sp->buflines.ga_len)
1918 break; // all the lines are processed
1919 ga_concat(&ga, ((char_u **)sp->buflines.ga_data)[sp->buf_lnum]);
1920 sp->buf_lnum++;
Bram Moolenaar2bdad612022-03-29 19:52:12 +01001921 if (ga_grow(&ga, 1) == FAIL)
1922 break;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001923 buf = (char_u *)ga.ga_data;
Bram Moolenaar2bdad612022-03-29 19:52:12 +01001924 buf[ga.ga_len++] = NUL;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001925 }
1926 else
1927 {
1928 buf = (char_u *)ga.ga_data;
1929 if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
1930 sp->fp) == NULL)
1931 break;
1932 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001933 len = ga.ga_len + (int)STRLEN(buf + ga.ga_len);
1934#ifdef USE_CRNL
1935 // Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the
1936 // CTRL-Z by its own, or after a NL.
1937 if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n'))
1938 && sp->fileformat == EOL_DOS
1939 && buf[len - 1] == Ctrl_Z)
1940 {
1941 buf[len - 1] = NUL;
1942 break;
1943 }
1944#endif
1945
1946 have_read = TRUE;
1947 ga.ga_len = len;
1948
1949 // If the line was longer than the buffer, read more.
1950 if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n')
1951 continue;
1952
1953 if (len >= 1 && buf[len - 1] == '\n') // remove trailing NL
1954 {
1955#ifdef USE_CRNL
1956 has_cr = (len >= 2 && buf[len - 2] == '\r');
1957 if (sp->fileformat == EOL_UNKNOWN)
1958 {
1959 if (has_cr)
1960 sp->fileformat = EOL_DOS;
1961 else
1962 sp->fileformat = EOL_UNIX;
1963 }
1964
1965 if (sp->fileformat == EOL_DOS)
1966 {
1967 if (has_cr) // replace trailing CR
1968 {
1969 buf[len - 2] = '\n';
1970 --len;
1971 --ga.ga_len;
1972 }
1973 else // lines like ":map xx yy^M" will have failed
1974 {
1975 if (!sp->error)
1976 {
1977 msg_source(HL_ATTR(HLF_W));
1978 emsg(_("W15: Warning: Wrong line separator, ^M may be missing"));
1979 }
1980 sp->error = TRUE;
1981 sp->fileformat = EOL_UNIX;
1982 }
1983 }
1984#endif
1985 // The '\n' is escaped if there is an odd number of ^V's just
1986 // before it, first set "c" just before the 'V's and then check
1987 // len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo
1988 for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--)
1989 ;
1990 if ((len & 1) != (c & 1)) // escaped NL, read more
1991 {
1992 ++sp->sourcing_lnum;
1993 continue;
1994 }
1995
1996 buf[len - 1] = NUL; // remove the NL
1997 }
1998
1999 // Check for ^C here now and then, so recursive :so can be broken.
2000 line_breakcheck();
2001 break;
2002 }
2003
2004 if (have_read)
2005 return (char_u *)ga.ga_data;
2006
2007 vim_free(ga.ga_data);
2008 return NULL;
2009}
2010
2011/*
2012 * Get one full line from a sourced file.
2013 * Called by do_cmdline() when it's called from do_source().
2014 *
2015 * Return a pointer to the line in allocated memory.
2016 * Return NULL for end-of-file or some error.
2017 */
2018 char_u *
Bram Moolenaar66250c92020-08-20 15:02:42 +02002019getsourceline(
2020 int c UNUSED,
2021 void *cookie,
2022 int indent UNUSED,
2023 getline_opt_T options)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002024{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002025 source_cookie_T *sp = (source_cookie_T *)cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002026 char_u *line;
2027 char_u *p;
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002028 int do_vim9_all = in_vim9script()
2029 && options == GETLINE_CONCAT_ALL;
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002030 int do_bar_cont = do_vim9_all
2031 || options == GETLINE_CONCAT_CONTBAR;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002032
2033#ifdef FEAT_EVAL
2034 // If breakpoints have been added/deleted need to check for it.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002035 if ((sp->dbg_tick < debug_tick) && !sp->source_from_buf)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002036 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002037 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002038 sp->dbg_tick = debug_tick;
2039 }
2040# ifdef FEAT_PROFILE
2041 if (do_profiling == PROF_YES)
2042 script_line_end();
2043# endif
2044#endif
2045
2046 // Set the current sourcing line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002047 SOURCING_LNUM = sp->sourcing_lnum + 1;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002048
2049 // Get current line. If there is a read-ahead line, use it, otherwise get
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002050 // one now. "fp" is NULL if actually using a string.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002051 if (sp->finished || (!sp->source_from_buf && sp->fp == NULL))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002052 line = NULL;
2053 else if (sp->nextline == NULL)
2054 line = get_one_sourceline(sp);
2055 else
2056 {
2057 line = sp->nextline;
2058 sp->nextline = NULL;
2059 ++sp->sourcing_lnum;
2060 }
2061#ifdef FEAT_PROFILE
2062 if (line != NULL && do_profiling == PROF_YES)
2063 script_line_start();
2064#endif
2065
2066 // Only concatenate lines starting with a \ when 'cpoptions' doesn't
2067 // contain the 'C' flag.
Bram Moolenaar66250c92020-08-20 15:02:42 +02002068 if (line != NULL && options != GETLINE_NONE
2069 && vim_strchr(p_cpo, CPO_CONCAT) == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002070 {
Bram Moolenaar5072b472021-06-03 21:56:10 +02002071 int comment_char = in_vim9script() ? '#' : '"';
2072
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002073 // compensate for the one line read-ahead
2074 --sp->sourcing_lnum;
2075
2076 // Get the next line and concatenate it when it starts with a
2077 // backslash. We always need to read the next line, keep it in
2078 // sp->nextline.
2079 /* Also check for a comment in between continuation lines: "\ */
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002080 // Also check for a Vim9 comment, empty line, line starting with '|',
2081 // but not "||".
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002082 sp->nextline = get_one_sourceline(sp);
2083 if (sp->nextline != NULL
2084 && (*(p = skipwhite(sp->nextline)) == '\\'
Bram Moolenaar5072b472021-06-03 21:56:10 +02002085 || (p[0] == comment_char
2086 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002087 || (do_vim9_all && (*p == NUL
2088 || vim9_comment_start(p)))
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002089 || (do_bar_cont && p[0] == '|' && p[1] != '|')))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002090 {
2091 garray_T ga;
2092
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002093 ga_init2(&ga, sizeof(char_u), 400);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002094 ga_concat(&ga, line);
2095 if (*p == '\\')
2096 ga_concat(&ga, p + 1);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002097 else if (*p == '|')
2098 {
2099 ga_concat(&ga, (char_u *)" ");
2100 ga_concat(&ga, p);
2101 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002102 for (;;)
2103 {
2104 vim_free(sp->nextline);
2105 sp->nextline = get_one_sourceline(sp);
2106 if (sp->nextline == NULL)
2107 break;
2108 p = skipwhite(sp->nextline);
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002109 if (*p == '\\' || (do_bar_cont && p[0] == '|' && p[1] != '|'))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002110 {
2111 // Adjust the growsize to the current length to speed up
2112 // concatenating many lines.
2113 if (ga.ga_len > 400)
2114 {
2115 if (ga.ga_len > 8000)
2116 ga.ga_growsize = 8000;
2117 else
2118 ga.ga_growsize = ga.ga_len;
2119 }
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002120 if (*p == '\\')
2121 ga_concat(&ga, p + 1);
2122 else
2123 {
2124 ga_concat(&ga, (char_u *)" ");
2125 ga_concat(&ga, p);
2126 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002127 }
Bram Moolenaar5072b472021-06-03 21:56:10 +02002128 else if (!(p[0] == (comment_char)
Bram Moolenaar0f37e352021-06-02 15:28:15 +02002129 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002130 && !(do_vim9_all && (*p == NUL || vim9_comment_start(p))))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002131 break;
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002132 /* drop a # comment or "\ comment line */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002133 }
2134 ga_append(&ga, NUL);
2135 vim_free(line);
2136 line = ga.ga_data;
2137 }
2138 }
2139
2140 if (line != NULL && sp->conv.vc_type != CONV_NONE)
2141 {
2142 char_u *s;
2143
2144 // Convert the encoding of the script line.
2145 s = string_convert(&sp->conv, line, NULL);
2146 if (s != NULL)
2147 {
2148 vim_free(line);
2149 line = s;
2150 }
2151 }
2152
2153#ifdef FEAT_EVAL
2154 // Did we encounter a breakpoint?
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002155 if (!sp->source_from_buf && sp->breakpoint != 0
2156 && sp->breakpoint <= SOURCING_LNUM)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002157 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002158 dbg_breakpoint(sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002159 // Find next breakpoint.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002160 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002161 sp->dbg_tick = debug_tick;
2162 }
2163#endif
2164
2165 return line;
2166}
2167
2168/*
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002169 * Returns TRUE if sourcing a script either from a file or a buffer.
2170 * Otherwise returns FALSE.
2171 */
2172 int
2173sourcing_a_script(exarg_T *eap)
2174{
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002175 return (getline_equal(eap->getline, eap->cookie, getsourceline));
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002176}
2177
2178/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002179 * ":scriptencoding": Set encoding conversion for a sourced script.
2180 */
2181 void
2182ex_scriptencoding(exarg_T *eap)
2183{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002184 source_cookie_T *sp;
2185 char_u *name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002186
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002187 if (!sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002188 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00002189 emsg(_(e_scriptencoding_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002190 return;
2191 }
2192
2193 if (*eap->arg != NUL)
2194 {
2195 name = enc_canonize(eap->arg);
2196 if (name == NULL) // out of memory
2197 return;
2198 }
2199 else
2200 name = eap->arg;
2201
2202 // Setup for conversion from the specified encoding to 'encoding'.
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002203 sp = (source_cookie_T *)getline_cookie(eap->getline, eap->cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002204 convert_setup(&sp->conv, name, p_enc);
2205
2206 if (name != eap->arg)
2207 vim_free(name);
2208}
2209
2210/*
2211 * ":scriptversion": Set Vim script version for a sourced script.
2212 */
2213 void
2214ex_scriptversion(exarg_T *eap UNUSED)
2215{
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002216 int nr;
2217
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002218 if (!sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002219 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002220 emsg(_(e_scriptversion_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002221 return;
2222 }
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002223 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002224 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002225 emsg(_(e_cannot_use_scriptversion_after_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002226 return;
2227 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002228
2229 nr = getdigits(&eap->arg);
2230 if (nr == 0 || *eap->arg != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002231 emsg(_(e_invalid_argument));
Bram Moolenaar67979662020-06-20 22:50:47 +02002232 else if (nr > SCRIPT_VERSION_MAX)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002233 semsg(_(e_scriptversion_not_supported_nr), nr);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002234 else
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002235 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002236 current_sctx.sc_version = nr;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002237#ifdef FEAT_EVAL
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002238 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = nr;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002239#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002240 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002241}
2242
2243#if defined(FEAT_EVAL) || defined(PROTO)
2244/*
2245 * ":finish": Mark a sourced file as finished.
2246 */
2247 void
2248ex_finish(exarg_T *eap)
2249{
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002250 if (sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002251 do_finish(eap, FALSE);
2252 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00002253 emsg(_(e_finish_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002254}
2255
2256/*
2257 * Mark a sourced file as finished. Possibly makes the ":finish" pending.
2258 * Also called for a pending finish at the ":endtry" or after returning from
2259 * an extra do_cmdline(). "reanimate" is used in the latter case.
2260 */
2261 void
2262do_finish(exarg_T *eap, int reanimate)
2263{
2264 int idx;
2265
2266 if (reanimate)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002267 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002268 eap->cookie))->finished = FALSE;
2269
2270 // Cleanup (and inactivate) conditionals, but stop when a try conditional
2271 // not in its finally clause (which then is to be executed next) is found.
2272 // In this case, make the ":finish" pending for execution at the ":endtry".
2273 // Otherwise, finish normally.
2274 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
2275 if (idx >= 0)
2276 {
2277 eap->cstack->cs_pending[idx] = CSTP_FINISH;
2278 report_make_pending(CSTP_FINISH, NULL);
2279 }
2280 else
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002281 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002282 eap->cookie))->finished = TRUE;
2283}
2284
2285
2286/*
2287 * Return TRUE when a sourced file had the ":finish" command: Don't give error
2288 * message for missing ":endif".
2289 * Return FALSE when not sourcing a file.
2290 */
2291 int
2292source_finished(
Bram Moolenaar66250c92020-08-20 15:02:42 +02002293 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002294 void *cookie)
2295{
2296 return (getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002297 && ((source_cookie_T *)getline_cookie(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002298 fgetline, cookie))->finished);
2299}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002300
2301/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002302 * Find the path of a script below the "autoload" directory.
2303 * Returns NULL if there is no "/autoload/" in the script name.
2304 */
2305 char_u *
2306script_name_after_autoload(scriptitem_T *si)
2307{
2308 char_u *p = si->sn_name;
2309 char_u *res = NULL;
2310
2311 for (;;)
2312 {
2313 char_u *n = (char_u *)strstr((char *)p, "autoload");
2314
2315 if (n == NULL)
2316 break;
2317 if (n > p && vim_ispathsep(n[-1]) && vim_ispathsep(n[8]))
2318 res = n + 9;
2319 p = n + 8;
2320 }
2321 return res;
2322}
2323
2324/*
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002325 * For an autoload script "autoload/dir/script.vim" return the prefix
2326 * "dir#script#" in allocated memory.
2327 * Returns NULL if anything is wrong.
2328 */
2329 char_u *
2330get_autoload_prefix(scriptitem_T *si)
2331{
2332 char_u *p = script_name_after_autoload(si);
2333 char_u *prefix;
2334
2335 if (p == NULL)
2336 return NULL;
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002337 prefix = vim_strsave(p);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002338 if (prefix == NULL)
2339 return NULL;
2340
2341 // replace all '/' with '#' and locate ".vim" at the end
2342 for (p = prefix; *p != NUL; p += mb_ptr2len(p))
2343 {
2344 if (vim_ispathsep(*p))
2345 *p = '#';
2346 else if (STRCMP(p, ".vim") == 0)
2347 {
2348 p[0] = '#';
2349 p[1] = NUL;
2350 return prefix;
2351 }
2352 }
2353
2354 // did not find ".vim" at the end
2355 vim_free(prefix);
2356 return NULL;
2357}
2358
2359/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002360 * If in a Vim9 autoload script return "name" with the autoload prefix for the
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002361 * script. If successful the returned name is allocated.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002362 * Otherwise it returns "name" unmodified.
2363 */
2364 char_u *
2365may_prefix_autoload(char_u *name)
2366{
2367 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
2368 {
2369 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
2370
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002371 if (si->sn_autoload_prefix != NULL)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002372 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002373 char_u *basename = name;
2374 size_t len;
2375 char_u *res;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002376
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002377 if (*name == K_SPECIAL)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002378 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002379 char_u *p = vim_strchr(name, '_');
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002380
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002381 // skip over "<SNR>99_"
2382 if (p != NULL)
2383 basename = p + 1;
2384 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002385
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002386 len = STRLEN(si->sn_autoload_prefix) + STRLEN(basename) + 2;
2387 res = alloc(len);
2388 if (res != NULL)
2389 {
2390 vim_snprintf((char *)res, len, "%s%s",
2391 si->sn_autoload_prefix, basename);
2392 return res;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002393 }
2394 }
2395 }
2396 return name;
2397}
2398
2399/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002400 * Return the autoload script name for a function or variable name.
2401 * Returns NULL when out of memory.
2402 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
2403 */
2404 char_u *
2405autoload_name(char_u *name)
2406{
2407 char_u *p, *q = NULL;
2408 char_u *scriptname;
2409
2410 // Get the script file name: replace '#' with '/', append ".vim".
2411 scriptname = alloc(STRLEN(name) + 14);
2412 if (scriptname == NULL)
2413 return NULL;
2414 STRCPY(scriptname, "autoload/");
Bram Moolenaara1773442020-08-12 15:21:22 +02002415 STRCAT(scriptname, name[0] == 'g' && name[1] == ':' ? name + 2: name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002416 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
2417 q = p, ++p)
2418 *p = '/';
2419 STRCPY(q, ".vim");
2420 return scriptname;
2421}
2422
2423/*
2424 * If "name" has a package name try autoloading the script for it.
2425 * Return TRUE if a package was loaded.
2426 */
2427 int
2428script_autoload(
2429 char_u *name,
2430 int reload) // load script again when already loaded
2431{
2432 char_u *p;
2433 char_u *scriptname, *tofree;
2434 int ret = FALSE;
2435 int i;
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002436 int ret_sid;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002437
2438 // If there is no '#' after name[0] there is no package name.
2439 p = vim_strchr(name, AUTOLOAD_CHAR);
2440 if (p == NULL || p == name)
2441 return FALSE;
2442
2443 tofree = scriptname = autoload_name(name);
2444 if (scriptname == NULL)
2445 return FALSE;
2446
2447 // Find the name in the list of previously loaded package names. Skip
2448 // "autoload/", it's always the same.
2449 for (i = 0; i < ga_loaded.ga_len; ++i)
2450 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
2451 break;
2452 if (!reload && i < ga_loaded.ga_len)
2453 ret = FALSE; // was loaded already
2454 else
2455 {
2456 // Remember the name if it wasn't loaded already.
2457 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
2458 {
2459 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
2460 tofree = NULL;
2461 }
2462
2463 // Try loading the package from $VIMRUNTIME/autoload/<name>.vim
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002464 // Use "ret_sid" to avoid loading the same script again.
=?UTF-8?q?Bj=C3=B6rn=20Linse?=223a9502022-01-31 17:26:05 +00002465 if (source_in_path(p_rtp, scriptname, DIP_START, &ret_sid) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002466 ret = TRUE;
2467 }
2468
2469 vim_free(tofree);
2470 return ret;
2471}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002472#endif