blob: c1ab41503fe877652ff52f02144e2da087f62bbb [file] [log] [blame]
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * scriptfile.c: functions for dealing with the runtime directories/files
12 */
13
14#include "vim.h"
15
Bram Moolenaarda6c0332019-09-01 16:01:30 +020016#if defined(FEAT_EVAL) || defined(PROTO)
17// The names of packages that once were loaded are remembered.
18static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
19#endif
20
Bram Moolenaar307c5a52019-08-25 15:41:00 +020021/*
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010022 * Initialize the execution stack.
23 */
24 void
25estack_init(void)
26{
27 estack_T *entry;
28
29 if (ga_grow(&exestack, 10) == FAIL)
30 mch_exit(0);
31 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len;
32 entry->es_type = ETYPE_TOP;
33 entry->es_name = NULL;
34 entry->es_lnum = 0;
Bram Moolenaar09d46402019-12-29 23:53:01 +010035#ifdef FEAT_EVAL
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010036 entry->es_info.ufunc = NULL;
Bram Moolenaar09d46402019-12-29 23:53:01 +010037#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010038 ++exestack.ga_len;
39}
40
41/*
42 * Add an item to the execution stack.
43 * Returns the new entry or NULL when out of memory.
44 */
45 estack_T *
46estack_push(etype_T type, char_u *name, long lnum)
47{
48 estack_T *entry;
49
50 // If memory allocation fails then we'll pop more than we push, eventually
51 // at the top level it will be OK again.
52 if (ga_grow(&exestack, 1) == OK)
53 {
54 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len;
55 entry->es_type = type;
56 entry->es_name = name;
57 entry->es_lnum = lnum;
Bram Moolenaar09d46402019-12-29 23:53:01 +010058#ifdef FEAT_EVAL
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010059 entry->es_info.ufunc = NULL;
Bram Moolenaar09d46402019-12-29 23:53:01 +010060#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010061 ++exestack.ga_len;
62 return entry;
63 }
64 return NULL;
65}
66
Bram Moolenaar09d46402019-12-29 23:53:01 +010067#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010068/*
69 * Add a user function to the execution stack.
70 */
Bram Moolenaarc620c052020-07-08 15:16:19 +020071 estack_T *
Bram Moolenaar25e0f582020-05-25 22:36:50 +020072estack_push_ufunc(ufunc_T *ufunc, long lnum)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010073{
Bram Moolenaar25e0f582020-05-25 22:36:50 +020074 estack_T *entry = estack_push(ETYPE_UFUNC,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010075 ufunc->uf_name_exp != NULL
76 ? ufunc->uf_name_exp : ufunc->uf_name, lnum);
77 if (entry != NULL)
78 entry->es_info.ufunc = ufunc;
Bram Moolenaarc620c052020-07-08 15:16:19 +020079 return entry;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010080}
Bram Moolenaar25e0f582020-05-25 22:36:50 +020081
82/*
83 * Return TRUE if "ufunc" with "lnum" is already at the top of the exe stack.
84 */
85 int
86estack_top_is_ufunc(ufunc_T *ufunc, long lnum)
87{
88 estack_T *entry;
89
90 if (exestack.ga_len == 0)
91 return FALSE;
92 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
93 return entry->es_type == ETYPE_UFUNC
94 && STRCMP( entry->es_name, ufunc->uf_name_exp != NULL
95 ? ufunc->uf_name_exp : ufunc->uf_name) == 0
96 && entry->es_lnum == lnum;
97}
Bram Moolenaar09d46402019-12-29 23:53:01 +010098#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010099
100/*
Bram Moolenaarc620c052020-07-08 15:16:19 +0200101 * Take an item off of the execution stack and return it.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100102 */
Bram Moolenaarc620c052020-07-08 15:16:19 +0200103 estack_T *
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100104estack_pop(void)
105{
Bram Moolenaarc620c052020-07-08 15:16:19 +0200106 if (exestack.ga_len == 0)
107 return NULL;
108 --exestack.ga_len;
109 return ((estack_T *)exestack.ga_data) + exestack.ga_len;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100110}
111
112/*
113 * Get the current value for <sfile> in allocated memory.
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200114 * "which" is ESTACK_SFILE for <sfile> and ESTACK_STACK for <stack>.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100115 */
116 char_u *
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200117estack_sfile(estack_arg_T which UNUSED)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100118{
Bram Moolenaar85b09572019-12-30 10:57:00 +0100119 estack_T *entry;
120#ifdef FEAT_EVAL
Bram Moolenaara5d04232020-07-26 15:37:02 +0200121 garray_T ga;
Bram Moolenaar4d7a2482020-01-06 19:53:43 +0100122 size_t len;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100123 int idx;
Bram Moolenaara5d04232020-07-26 15:37:02 +0200124 etype_T last_type = ETYPE_SCRIPT;
125 char *type_name;
Bram Moolenaar85b09572019-12-30 10:57:00 +0100126#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100127
128 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100129#ifdef FEAT_EVAL
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200130 if (which == ESTACK_SFILE && entry->es_type != ETYPE_UFUNC)
Bram Moolenaar09d46402019-12-29 23:53:01 +0100131#endif
Bram Moolenaara5d04232020-07-26 15:37:02 +0200132 {
133 if (entry->es_name == NULL)
134 return NULL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100135 return vim_strsave(entry->es_name);
Bram Moolenaara5d04232020-07-26 15:37:02 +0200136 }
Bram Moolenaar09d46402019-12-29 23:53:01 +0100137#ifdef FEAT_EVAL
Bram Moolenaarc4492712021-11-22 15:37:15 +0000138 // expand('<sfile>') works in a function for backwards compatibility, but
139 // may give an unexpected result. Disallow it in Vim 9 script.
140 if (which == ESTACK_SFILE && in_vim9script())
141 {
142 int save_emsg_off = emsg_off;
143
144 if (emsg_off == 1)
145 // f_expand() silences errors but we do want this one
146 emsg_off = 0;
147 emsg(_(e_cannot_expand_sfile_in_vim9_function));
148 emsg_off = save_emsg_off;
149 return NULL;
150 }
151
Bram Moolenaara5d04232020-07-26 15:37:02 +0200152 // Give information about each stack entry up to the root.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100153 // For a function we compose the call stack, as it was done in the past:
154 // "function One[123]..Two[456]..Three"
Bram Moolenaara5d04232020-07-26 15:37:02 +0200155 ga_init2(&ga, sizeof(char), 100);
156 for (idx = 0; idx < exestack.ga_len; ++idx)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100157 {
158 entry = ((estack_T *)exestack.ga_data) + idx;
Bram Moolenaara5d04232020-07-26 15:37:02 +0200159 if (entry->es_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100160 {
Bram Moolenaara810db32020-09-11 17:59:23 +0200161 long lnum = 0;
162 char *dots;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200163
Bram Moolenaara5d04232020-07-26 15:37:02 +0200164 len = STRLEN(entry->es_name) + 15;
165 type_name = "";
166 if (entry->es_type != last_type)
167 {
168 switch (entry->es_type)
169 {
170 case ETYPE_SCRIPT: type_name = "script "; break;
171 case ETYPE_UFUNC: type_name = "function "; break;
172 default: type_name = ""; break;
173 }
174 last_type = entry->es_type;
175 }
176 len += STRLEN(type_name);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200177 if (ga_grow(&ga, (int)len) == FAIL)
Bram Moolenaara5d04232020-07-26 15:37:02 +0200178 break;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200179 if (idx == exestack.ga_len - 1)
180 lnum = which == ESTACK_STACK ? SOURCING_LNUM : 0;
181 else
182 lnum = entry->es_lnum;
Bram Moolenaara810db32020-09-11 17:59:23 +0200183 dots = idx == exestack.ga_len - 1 ? "" : "..";
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200184 if (lnum == 0)
185 // For the bottom entry of <sfile>: do not add the line number,
186 // it is used in <slnum>. Also leave it out when the number is
187 // not set.
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200188 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s%s",
Bram Moolenaara810db32020-09-11 17:59:23 +0200189 type_name, entry->es_name, dots);
Bram Moolenaara5d04232020-07-26 15:37:02 +0200190 else
Bram Moolenaara810db32020-09-11 17:59:23 +0200191 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s[%ld]%s",
192 type_name, entry->es_name, lnum, dots);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200193 ga.ga_len += (int)STRLEN((char *)ga.ga_data + ga.ga_len);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100194 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100195 }
196
Bram Moolenaara5d04232020-07-26 15:37:02 +0200197 return (char_u *)ga.ga_data;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100198#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100199}
200
201/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200202 * ":runtime [what] {name}"
203 */
204 void
205ex_runtime(exarg_T *eap)
206{
207 char_u *arg = eap->arg;
208 char_u *p = skiptowhite(arg);
209 int len = (int)(p - arg);
210 int flags = eap->forceit ? DIP_ALL : 0;
211
212 if (STRNCMP(arg, "START", len) == 0)
213 {
214 flags += DIP_START + DIP_NORTP;
215 arg = skipwhite(arg + len);
216 }
217 else if (STRNCMP(arg, "OPT", len) == 0)
218 {
219 flags += DIP_OPT + DIP_NORTP;
220 arg = skipwhite(arg + len);
221 }
222 else if (STRNCMP(arg, "PACK", len) == 0)
223 {
224 flags += DIP_START + DIP_OPT + DIP_NORTP;
225 arg = skipwhite(arg + len);
226 }
227 else if (STRNCMP(arg, "ALL", len) == 0)
228 {
229 flags += DIP_START + DIP_OPT;
230 arg = skipwhite(arg + len);
231 }
232
233 source_runtime(arg, flags);
234}
235
236 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100237source_callback(char_u *fname, void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200238{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239 (void)do_source(fname, FALSE, DOSO_NONE, cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200240}
241
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000242#ifdef FEAT_EVAL
243/*
244 * Find an already loaded script "name".
245 * If found returns its script ID. If not found returns -1.
246 */
247 static int
248find_script_by_name(char_u *name)
249{
250 int sid;
251 scriptitem_T *si;
252
253 for (sid = script_items.ga_len; sid > 0; --sid)
254 {
255 // We used to check inode here, but that doesn't work:
256 // - If a script is edited and written, it may get a different
257 // inode number, even though to the user it is the same script.
258 // - If a script is deleted and another script is written, with a
259 // different name, the inode may be re-used.
260 si = SCRIPT_ITEM(sid);
261 if (si->sn_name != NULL && fnamecmp(si->sn_name, name) == 0)
262 return sid;
263 }
264 return -1;
265}
266
267/*
268 * Add a new scriptitem with all items initialized.
269 * When running out of memory "error" is set to FAIL.
270 * Returns the script ID.
271 */
272 static int
273get_new_scriptitem(int *error)
274{
275 static scid_T last_current_SID = 0;
276 int sid = ++last_current_SID;
277 scriptitem_T *si;
278
279 if (ga_grow(&script_items, (int)(sid - script_items.ga_len)) == FAIL)
280 {
281 *error = FAIL;
282 return sid;
283 }
284 while (script_items.ga_len < sid)
285 {
286 si = ALLOC_CLEAR_ONE(scriptitem_T);
287 if (si == NULL)
288 {
289 *error = FAIL;
290 return sid;
291 }
292 ++script_items.ga_len;
293 SCRIPT_ITEM(script_items.ga_len) = si;
294 si->sn_name = NULL;
295 si->sn_version = 1;
296
297 // Allocate the local script variables to use for this script.
298 new_script_vars(script_items.ga_len);
299 ga_init2(&si->sn_var_vals, sizeof(svar_T), 10);
300 hash_init(&si->sn_all_vars.dv_hashtab);
301 ga_init2(&si->sn_imports, sizeof(imported_T), 10);
302 ga_init2(&si->sn_type_list, sizeof(type_T), 10);
303# ifdef FEAT_PROFILE
304 si->sn_prof_on = FALSE;
305# endif
306 }
307
308 // Used to check script variable index is still valid.
309 si->sn_script_seq = current_sctx.sc_seq;
310
311 return sid;
312}
313
314 static void
315find_script_callback(char_u *fname, void *cookie)
316{
317 int sid;
318 int error = OK;
319 int *ret_sid = cookie;
320
321 sid = find_script_by_name(fname);
322 if (sid < 0)
323 {
324 // script does not exist yet, create a new scriptitem
325 sid = get_new_scriptitem(&error);
326 if (error == OK)
327 {
328 scriptitem_T *si = SCRIPT_ITEM(sid);
329
330 si->sn_name = vim_strsave(fname);
331 si->sn_state = SN_STATE_NOT_LOADED;
332 }
333 }
334 *ret_sid = sid;
335}
336#endif
337
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200338/*
339 * Find the file "name" in all directories in "path" and invoke
340 * "callback(fname, cookie)".
341 * "name" can contain wildcards.
342 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
343 * When "flags" has DIP_DIR: find directories instead of files.
344 * When "flags" has DIP_ERR: give an error message if there is no match.
345 *
346 * return FAIL when no file could be sourced, OK otherwise.
347 */
348 int
349do_in_path(
350 char_u *path,
351 char_u *name,
352 int flags,
353 void (*callback)(char_u *fname, void *ck),
354 void *cookie)
355{
356 char_u *rtp;
357 char_u *np;
358 char_u *buf;
359 char_u *rtp_copy;
360 char_u *tail;
361 int num_files;
362 char_u **files;
363 int i;
364 int did_one = FALSE;
365#ifdef AMIGA
366 struct Process *proc = (struct Process *)FindTask(0L);
367 APTR save_winptr = proc->pr_WindowPtr;
368
369 // Avoid a requester here for a volume that doesn't exist.
370 proc->pr_WindowPtr = (APTR)-1L;
371#endif
372
373 // Make a copy of 'runtimepath'. Invoking the callback may change the
374 // value.
375 rtp_copy = vim_strsave(path);
376 buf = alloc(MAXPATHL);
377 if (buf != NULL && rtp_copy != NULL)
378 {
Bram Moolenaar647a5302020-05-03 17:01:24 +0200379 if (p_verbose > 10 && name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200380 {
381 verbose_enter();
382 smsg(_("Searching for \"%s\" in \"%s\""),
383 (char *)name, (char *)path);
384 verbose_leave();
385 }
386
387 // Loop over all entries in 'runtimepath'.
388 rtp = rtp_copy;
389 while (*rtp != NUL && ((flags & DIP_ALL) || !did_one))
390 {
391 size_t buflen;
392
393 // Copy the path from 'runtimepath' to buf[].
394 copy_option_part(&rtp, buf, MAXPATHL, ",");
395 buflen = STRLEN(buf);
396
397 // Skip after or non-after directories.
398 if (flags & (DIP_NOAFTER | DIP_AFTER))
399 {
400 int is_after = buflen >= 5
401 && STRCMP(buf + buflen - 5, "after") == 0;
402
403 if ((is_after && (flags & DIP_NOAFTER))
404 || (!is_after && (flags & DIP_AFTER)))
405 continue;
406 }
407
408 if (name == NULL)
409 {
410 (*callback)(buf, (void *) &cookie);
411 if (!did_one)
412 did_one = (cookie == NULL);
413 }
414 else if (buflen + STRLEN(name) + 2 < MAXPATHL)
415 {
416 add_pathsep(buf);
417 tail = buf + STRLEN(buf);
418
419 // Loop over all patterns in "name"
420 np = name;
421 while (*np != NUL && ((flags & DIP_ALL) || !did_one))
422 {
423 // Append the pattern from "name" to buf[].
424 copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)),
425 "\t ");
426
Bram Moolenaar647a5302020-05-03 17:01:24 +0200427 if (p_verbose > 10)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200428 {
429 verbose_enter();
430 smsg(_("Searching for \"%s\""), buf);
431 verbose_leave();
432 }
433
434 // Expand wildcards, invoke the callback for each match.
435 if (gen_expand_wildcards(1, &buf, &num_files, &files,
436 (flags & DIP_DIR) ? EW_DIR : EW_FILE) == OK)
437 {
438 for (i = 0; i < num_files; ++i)
439 {
440 (*callback)(files[i], cookie);
441 did_one = TRUE;
442 if (!(flags & DIP_ALL))
443 break;
444 }
445 FreeWild(num_files, files);
446 }
447 }
448 }
449 }
450 }
451 vim_free(buf);
452 vim_free(rtp_copy);
453 if (!did_one && name != NULL)
454 {
455 char *basepath = path == p_rtp ? "runtimepath" : "packpath";
456
457 if (flags & DIP_ERR)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000458 semsg(_(e_directory_not_found_in_str_str), basepath, name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200459 else if (p_verbose > 0)
460 {
461 verbose_enter();
462 smsg(_("not found in '%s': \"%s\""), basepath, name);
463 verbose_leave();
464 }
465 }
466
467#ifdef AMIGA
468 proc->pr_WindowPtr = save_winptr;
469#endif
470
471 return did_one ? OK : FAIL;
472}
473
474/*
475 * Find "name" in "path". When found, invoke the callback function for
476 * it: callback(fname, "cookie")
477 * When "flags" has DIP_ALL repeat for all matches, otherwise only the first
478 * one is used.
479 * Returns OK when at least one match found, FAIL otherwise.
480 *
481 * If "name" is NULL calls callback for each entry in "path". Cookie is
482 * passed by reference in this case, setting it to NULL indicates that callback
483 * has done its job.
484 */
485 static int
486do_in_path_and_pp(
487 char_u *path,
488 char_u *name,
489 int flags,
490 void (*callback)(char_u *fname, void *ck),
491 void *cookie)
492{
493 int done = FAIL;
494 char_u *s;
495 int len;
496 char *start_dir = "pack/*/start/*/%s";
497 char *opt_dir = "pack/*/opt/*/%s";
498
499 if ((flags & DIP_NORTP) == 0)
500 done = do_in_path(path, name, flags, callback, cookie);
501
502 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_START))
503 {
504 len = (int)(STRLEN(start_dir) + STRLEN(name));
505 s = alloc(len);
506 if (s == NULL)
507 return FAIL;
508 vim_snprintf((char *)s, len, start_dir, name);
509 done = do_in_path(p_pp, s, flags, callback, cookie);
510 vim_free(s);
511 }
512
513 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_OPT))
514 {
515 len = (int)(STRLEN(opt_dir) + STRLEN(name));
516 s = alloc(len);
517 if (s == NULL)
518 return FAIL;
519 vim_snprintf((char *)s, len, opt_dir, name);
520 done = do_in_path(p_pp, s, flags, callback, cookie);
521 vim_free(s);
522 }
523
524 return done;
525}
526
527/*
528 * Just like do_in_path_and_pp(), using 'runtimepath' for "path".
529 */
530 int
531do_in_runtimepath(
532 char_u *name,
533 int flags,
534 void (*callback)(char_u *fname, void *ck),
535 void *cookie)
536{
537 return do_in_path_and_pp(p_rtp, name, flags, callback, cookie);
538}
539
540/*
541 * Source the file "name" from all directories in 'runtimepath'.
542 * "name" can contain wildcards.
543 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
544 *
545 * return FAIL when no file could be sourced, OK otherwise.
546 */
547 int
548source_runtime(char_u *name, int flags)
549{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100550 return source_in_path(p_rtp, name, flags, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200551}
552
553/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000554 * Just like source_runtime(), but use "path" instead of 'runtimepath'
555 * and return the script ID in "ret_sid".
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200556 */
557 int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100558source_in_path(char_u *path, char_u *name, int flags, int *ret_sid)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200559{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100560 return do_in_path_and_pp(path, name, flags, source_callback, ret_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200561}
562
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200563#if defined(FEAT_EVAL) || defined(PROTO)
564
565/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000566 * Find "name" in 'runtimepath'. If found a new scriptitem is created for it
567 * and it's script ID is returned.
568 * If not found returns -1.
569 */
570 int
571find_script_in_rtp(char_u *name)
572{
573 int sid = -1;
574
575 (void)do_in_path_and_pp(p_rtp, name, DIP_NOAFTER,
576 find_script_callback, &sid);
577 return sid;
578}
579
580/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200581 * Expand wildcards in "pat" and invoke do_source() for each match.
582 */
583 static void
584source_all_matches(char_u *pat)
585{
586 int num_files;
587 char_u **files;
588 int i;
589
590 if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) == OK)
591 {
592 for (i = 0; i < num_files; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100593 (void)do_source(files[i], FALSE, DOSO_NONE, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200594 FreeWild(num_files, files);
595 }
596}
597
598/*
599 * Add the package directory to 'runtimepath'.
600 */
601 static int
602add_pack_dir_to_rtp(char_u *fname)
603{
604 char_u *p4, *p3, *p2, *p1, *p;
605 char_u *entry;
606 char_u *insp = NULL;
607 int c;
608 char_u *new_rtp;
609 int keep;
610 size_t oldlen;
611 size_t addlen;
612 size_t new_rtp_len;
613 char_u *afterdir = NULL;
614 size_t afterlen = 0;
615 char_u *after_insp = NULL;
616 char_u *ffname = NULL;
617 size_t fname_len;
618 char_u *buf = NULL;
619 char_u *rtp_ffname;
620 int match;
621 int retval = FAIL;
622
623 p4 = p3 = p2 = p1 = get_past_head(fname);
624 for (p = p1; *p; MB_PTR_ADV(p))
625 if (vim_ispathsep_nocolon(*p))
626 {
627 p4 = p3; p3 = p2; p2 = p1; p1 = p;
628 }
629
630 // now we have:
631 // rtp/pack/name/start/name
632 // p4 p3 p2 p1
633 //
634 // find the part up to "pack" in 'runtimepath'
635 c = *++p4; // append pathsep in order to expand symlink
636 *p4 = NUL;
637 ffname = fix_fname(fname);
638 *p4 = c;
639 if (ffname == NULL)
640 return FAIL;
641
642 // Find "ffname" in "p_rtp", ignoring '/' vs '\' differences.
643 // Also stop at the first "after" directory.
644 fname_len = STRLEN(ffname);
645 buf = alloc(MAXPATHL);
646 if (buf == NULL)
647 goto theend;
648 for (entry = p_rtp; *entry != NUL; )
649 {
650 char_u *cur_entry = entry;
651
652 copy_option_part(&entry, buf, MAXPATHL, ",");
653 if (insp == NULL)
654 {
655 add_pathsep(buf);
656 rtp_ffname = fix_fname(buf);
657 if (rtp_ffname == NULL)
658 goto theend;
659 match = vim_fnamencmp(rtp_ffname, ffname, fname_len) == 0;
660 vim_free(rtp_ffname);
661 if (match)
662 // Insert "ffname" after this entry (and comma).
663 insp = entry;
664 }
665
666 if ((p = (char_u *)strstr((char *)buf, "after")) != NULL
667 && p > buf
668 && vim_ispathsep(p[-1])
669 && (vim_ispathsep(p[5]) || p[5] == NUL || p[5] == ','))
670 {
671 if (insp == NULL)
672 // Did not find "ffname" before the first "after" directory,
673 // insert it before this entry.
674 insp = cur_entry;
675 after_insp = cur_entry;
676 break;
677 }
678 }
679
680 if (insp == NULL)
681 // Both "fname" and "after" not found, append at the end.
682 insp = p_rtp + STRLEN(p_rtp);
683
684 // check if rtp/pack/name/start/name/after exists
685 afterdir = concat_fnames(fname, (char_u *)"after", TRUE);
686 if (afterdir != NULL && mch_isdir(afterdir))
687 afterlen = STRLEN(afterdir) + 1; // add one for comma
688
689 oldlen = STRLEN(p_rtp);
690 addlen = STRLEN(fname) + 1; // add one for comma
691 new_rtp = alloc(oldlen + addlen + afterlen + 1); // add one for NUL
692 if (new_rtp == NULL)
693 goto theend;
694
695 // We now have 'rtp' parts: {keep}{keep_after}{rest}.
696 // Create new_rtp, first: {keep},{fname}
697 keep = (int)(insp - p_rtp);
698 mch_memmove(new_rtp, p_rtp, keep);
699 new_rtp_len = keep;
700 if (*insp == NUL)
701 new_rtp[new_rtp_len++] = ','; // add comma before
702 mch_memmove(new_rtp + new_rtp_len, fname, addlen - 1);
703 new_rtp_len += addlen - 1;
704 if (*insp != NUL)
705 new_rtp[new_rtp_len++] = ','; // add comma after
706
707 if (afterlen > 0 && after_insp != NULL)
708 {
709 int keep_after = (int)(after_insp - p_rtp);
710
711 // Add to new_rtp: {keep},{fname}{keep_after},{afterdir}
712 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep,
713 keep_after - keep);
714 new_rtp_len += keep_after - keep;
715 mch_memmove(new_rtp + new_rtp_len, afterdir, afterlen - 1);
716 new_rtp_len += afterlen - 1;
717 new_rtp[new_rtp_len++] = ',';
718 keep = keep_after;
719 }
720
721 if (p_rtp[keep] != NUL)
722 // Append rest: {keep},{fname}{keep_after},{afterdir}{rest}
723 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep, oldlen - keep + 1);
724 else
725 new_rtp[new_rtp_len] = NUL;
726
727 if (afterlen > 0 && after_insp == NULL)
728 {
729 // Append afterdir when "after" was not found:
730 // {keep},{fname}{rest},{afterdir}
731 STRCAT(new_rtp, ",");
732 STRCAT(new_rtp, afterdir);
733 }
734
735 set_option_value((char_u *)"rtp", 0L, new_rtp, 0);
736 vim_free(new_rtp);
737 retval = OK;
738
739theend:
740 vim_free(buf);
741 vim_free(ffname);
742 vim_free(afterdir);
743 return retval;
744}
745
746/*
747 * Load scripts in "plugin" and "ftdetect" directories of the package.
748 */
749 static int
750load_pack_plugin(char_u *fname)
751{
752 static char *plugpat = "%s/plugin/**/*.vim";
753 static char *ftpat = "%s/ftdetect/*.vim";
754 int len;
755 char_u *ffname = fix_fname(fname);
756 char_u *pat = NULL;
757 int retval = FAIL;
758
759 if (ffname == NULL)
760 return FAIL;
761 len = (int)STRLEN(ffname) + (int)STRLEN(ftpat);
762 pat = alloc(len);
763 if (pat == NULL)
764 goto theend;
765 vim_snprintf((char *)pat, len, plugpat, ffname);
766 source_all_matches(pat);
767
768 {
769 char_u *cmd = vim_strsave((char_u *)"g:did_load_filetypes");
770
771 // If runtime/filetype.vim wasn't loaded yet, the scripts will be
772 // found when it loads.
773 if (cmd != NULL && eval_to_number(cmd) > 0)
774 {
775 do_cmdline_cmd((char_u *)"augroup filetypedetect");
776 vim_snprintf((char *)pat, len, ftpat, ffname);
777 source_all_matches(pat);
778 do_cmdline_cmd((char_u *)"augroup END");
779 }
780 vim_free(cmd);
781 }
782 vim_free(pat);
783 retval = OK;
784
785theend:
786 vim_free(ffname);
787 return retval;
788}
789
790// used for "cookie" of add_pack_plugin()
791static int APP_ADD_DIR;
792static int APP_LOAD;
793static int APP_BOTH;
794
795 static void
796add_pack_plugin(char_u *fname, void *cookie)
797{
798 if (cookie != &APP_LOAD)
799 {
800 char_u *buf = alloc(MAXPATHL);
801 char_u *p;
802 int found = FALSE;
803
804 if (buf == NULL)
805 return;
806 p = p_rtp;
807 while (*p != NUL)
808 {
809 copy_option_part(&p, buf, MAXPATHL, ",");
810 if (pathcmp((char *)buf, (char *)fname, -1) == 0)
811 {
812 found = TRUE;
813 break;
814 }
815 }
816 vim_free(buf);
817 if (!found)
818 // directory is not yet in 'runtimepath', add it
819 if (add_pack_dir_to_rtp(fname) == FAIL)
820 return;
821 }
822
823 if (cookie != &APP_ADD_DIR)
824 load_pack_plugin(fname);
825}
826
827/*
828 * Add all packages in the "start" directory to 'runtimepath'.
829 */
830 void
831add_pack_start_dirs(void)
832{
833 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
834 add_pack_plugin, &APP_ADD_DIR);
835}
836
837/*
838 * Load plugins from all packages in the "start" directory.
839 */
840 void
841load_start_packages(void)
842{
843 did_source_packages = TRUE;
844 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
845 add_pack_plugin, &APP_LOAD);
846}
847
848/*
849 * ":packloadall"
850 * Find plugins in the package directories and source them.
851 */
852 void
853ex_packloadall(exarg_T *eap)
854{
855 if (!did_source_packages || eap->forceit)
856 {
857 // First do a round to add all directories to 'runtimepath', then load
858 // the plugins. This allows for plugins to use an autoload directory
859 // of another plugin.
860 add_pack_start_dirs();
861 load_start_packages();
862 }
863}
864
865/*
866 * ":packadd[!] {name}"
867 */
868 void
869ex_packadd(exarg_T *eap)
870{
871 static char *plugpat = "pack/*/%s/%s";
872 int len;
873 char *pat;
874 int round;
875 int res = OK;
876
877 // Round 1: use "start", round 2: use "opt".
878 for (round = 1; round <= 2; ++round)
879 {
880 // Only look under "start" when loading packages wasn't done yet.
881 if (round == 1 && did_source_packages)
882 continue;
883
884 len = (int)STRLEN(plugpat) + (int)STRLEN(eap->arg) + 5;
885 pat = alloc(len);
886 if (pat == NULL)
887 return;
888 vim_snprintf(pat, len, plugpat, round == 1 ? "start" : "opt", eap->arg);
889 // The first round don't give a "not found" error, in the second round
890 // only when nothing was found in the first round.
891 res = do_in_path(p_pp, (char_u *)pat,
892 DIP_ALL + DIP_DIR + (round == 2 && res == FAIL ? DIP_ERR : 0),
893 add_pack_plugin, eap->forceit ? &APP_ADD_DIR : &APP_BOTH);
894 vim_free(pat);
895 }
896}
897#endif
898
899/*
Bram Moolenaar26262f82019-09-04 20:59:15 +0200900 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
901 * list of file names in allocated memory.
902 */
903 void
904remove_duplicates(garray_T *gap)
905{
906 int i;
907 int j;
908 char_u **fnames = (char_u **)gap->ga_data;
909
910 sort_strings(fnames, gap->ga_len);
911 for (i = gap->ga_len - 1; i > 0; --i)
912 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
913 {
914 vim_free(fnames[i]);
915 for (j = i + 1; j < gap->ga_len; ++j)
916 fnames[j - 1] = fnames[j];
917 --gap->ga_len;
918 }
919}
920
921/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200922 * Expand color scheme, compiler or filetype names.
923 * Search from 'runtimepath':
924 * 'runtimepath'/{dirnames}/{pat}.vim
925 * When "flags" has DIP_START: search also from 'start' of 'packpath':
926 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
927 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
928 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
929 * "dirnames" is an array with one or more directory names.
930 */
931 int
932ExpandRTDir(
933 char_u *pat,
934 int flags,
935 int *num_file,
936 char_u ***file,
937 char *dirnames[])
938{
939 char_u *s;
940 char_u *e;
941 char_u *match;
942 garray_T ga;
943 int i;
944 int pat_len;
945
946 *num_file = 0;
947 *file = NULL;
948 pat_len = (int)STRLEN(pat);
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000949 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200950
951 for (i = 0; dirnames[i] != NULL; ++i)
952 {
953 s = alloc(STRLEN(dirnames[i]) + pat_len + 7);
954 if (s == NULL)
955 {
956 ga_clear_strings(&ga);
957 return FAIL;
958 }
959 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
960 globpath(p_rtp, s, &ga, 0);
961 vim_free(s);
962 }
963
964 if (flags & DIP_START) {
965 for (i = 0; dirnames[i] != NULL; ++i)
966 {
967 s = alloc(STRLEN(dirnames[i]) + pat_len + 22);
968 if (s == NULL)
969 {
970 ga_clear_strings(&ga);
971 return FAIL;
972 }
973 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
974 globpath(p_pp, s, &ga, 0);
975 vim_free(s);
976 }
977 }
978
979 if (flags & DIP_OPT) {
980 for (i = 0; dirnames[i] != NULL; ++i)
981 {
982 s = alloc(STRLEN(dirnames[i]) + pat_len + 20);
983 if (s == NULL)
984 {
985 ga_clear_strings(&ga);
986 return FAIL;
987 }
988 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
989 globpath(p_pp, s, &ga, 0);
990 vim_free(s);
991 }
992 }
993
994 for (i = 0; i < ga.ga_len; ++i)
995 {
996 match = ((char_u **)ga.ga_data)[i];
997 s = match;
998 e = s + STRLEN(s);
999 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
1000 {
1001 e -= 4;
1002 for (s = e; s > match; MB_PTR_BACK(match, s))
1003 if (s < match || vim_ispathsep(*s))
1004 break;
1005 ++s;
1006 *e = NUL;
1007 mch_memmove(match, s, e - s + 1);
1008 }
1009 }
1010
1011 if (ga.ga_len == 0)
1012 return FAIL;
1013
1014 // Sort and remove duplicates which can happen when specifying multiple
1015 // directories in dirnames.
1016 remove_duplicates(&ga);
1017
1018 *file = ga.ga_data;
1019 *num_file = ga.ga_len;
1020 return OK;
1021}
1022
1023/*
1024 * Expand loadplugin names:
1025 * 'packpath'/pack/ * /opt/{pat}
1026 */
1027 int
1028ExpandPackAddDir(
1029 char_u *pat,
1030 int *num_file,
1031 char_u ***file)
1032{
1033 char_u *s;
1034 char_u *e;
1035 char_u *match;
1036 garray_T ga;
1037 int i;
1038 int pat_len;
1039
1040 *num_file = 0;
1041 *file = NULL;
1042 pat_len = (int)STRLEN(pat);
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001043 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001044
1045 s = alloc(pat_len + 26);
1046 if (s == NULL)
1047 {
1048 ga_clear_strings(&ga);
1049 return FAIL;
1050 }
1051 sprintf((char *)s, "pack/*/opt/%s*", pat);
1052 globpath(p_pp, s, &ga, 0);
1053 vim_free(s);
1054
1055 for (i = 0; i < ga.ga_len; ++i)
1056 {
1057 match = ((char_u **)ga.ga_data)[i];
1058 s = gettail(match);
1059 e = s + STRLEN(s);
1060 mch_memmove(match, s, e - s + 1);
1061 }
1062
1063 if (ga.ga_len == 0)
1064 return FAIL;
1065
1066 // Sort and remove duplicates which can happen when specifying multiple
1067 // directories in dirnames.
1068 remove_duplicates(&ga);
1069
1070 *file = ga.ga_data;
1071 *num_file = ga.ga_len;
1072 return OK;
1073}
1074
1075 static void
1076cmd_source(char_u *fname, exarg_T *eap)
1077{
1078 if (*fname == NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001079 emsg(_(e_argument_required));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001080
1081 else if (eap != NULL && eap->forceit)
1082 // ":source!": read Normal mode commands
1083 // Need to execute the commands directly. This is required at least
1084 // for:
1085 // - ":g" command busy
1086 // - after ":argdo", ":windo" or ":bufdo"
1087 // - another command follows
1088 // - inside a loop
1089 openscript(fname, global_busy || listcmd_busy || eap->nextcmd != NULL
1090#ifdef FEAT_EVAL
1091 || eap->cstack->cs_idx >= 0
1092#endif
1093 );
1094
1095 // ":source" read ex commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096 else if (do_source(fname, FALSE, DOSO_NONE, NULL) == FAIL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001097 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001098}
1099
1100/*
1101 * ":source {fname}"
1102 */
1103 void
1104ex_source(exarg_T *eap)
1105{
1106#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02001107 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001108 {
1109 char_u *fname = NULL;
1110
1111 fname = do_browse(0, (char_u *)_("Source Vim script"), eap->arg,
1112 NULL, NULL,
1113 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
1114 if (fname != NULL)
1115 {
1116 cmd_source(fname, eap);
1117 vim_free(fname);
1118 }
1119 }
1120 else
1121#endif
1122 cmd_source(eap->arg, eap);
1123}
1124
1125#if defined(FEAT_EVAL) || defined(PROTO)
1126/*
1127 * ":options"
1128 */
1129 void
1130ex_options(
1131 exarg_T *eap UNUSED)
1132{
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001133 char_u buf[500];
1134 int multi_mods = 0;
1135
1136 buf[0] = NUL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001137 (void)add_win_cmd_modifers(buf, &cmdmod, &multi_mods);
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001138
1139 vim_setenv((char_u *)"OPTWIN_CMD", buf);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001140 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL);
1141}
1142#endif
1143
1144/*
1145 * ":source" and associated commands.
1146 */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001147
1148#ifdef FEAT_EVAL
1149/*
1150 * Return the address holding the next breakpoint line for a source cookie.
1151 */
1152 linenr_T *
1153source_breakpoint(void *cookie)
1154{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001155 return &((source_cookie_T *)cookie)->breakpoint;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001156}
1157
1158/*
1159 * Return the address holding the debug tick for a source cookie.
1160 */
1161 int *
1162source_dbg_tick(void *cookie)
1163{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001164 return &((source_cookie_T *)cookie)->dbg_tick;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001165}
1166
1167/*
1168 * Return the nesting level for a source cookie.
1169 */
1170 int
1171source_level(void *cookie)
1172{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001173 return ((source_cookie_T *)cookie)->level;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001174}
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001175
1176/*
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02001177 * Return the readahead line. Note that the pointer may become invalid when
1178 * getting the next line, if it's concatenated with the next one.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001179 */
1180 char_u *
1181source_nextline(void *cookie)
1182{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001183 return ((source_cookie_T *)cookie)->nextline;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001184}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001185#endif
1186
1187#if (defined(MSWIN) && defined(FEAT_CSCOPE)) || defined(HAVE_FD_CLOEXEC)
1188# define USE_FOPEN_NOINH
1189/*
1190 * Special function to open a file without handle inheritance.
1191 * When possible the handle is closed on exec().
1192 */
1193 static FILE *
1194fopen_noinh_readbin(char *filename)
1195{
1196# ifdef MSWIN
1197 int fd_tmp = mch_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0);
1198# else
1199 int fd_tmp = mch_open(filename, O_RDONLY, 0);
1200# endif
1201
1202 if (fd_tmp == -1)
1203 return NULL;
1204
1205# ifdef HAVE_FD_CLOEXEC
1206 {
1207 int fdflags = fcntl(fd_tmp, F_GETFD);
1208 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
1209 (void)fcntl(fd_tmp, F_SETFD, fdflags | FD_CLOEXEC);
1210 }
1211# endif
1212
1213 return fdopen(fd_tmp, READBIN);
1214}
1215#endif
1216
1217/*
1218 * do_source: Read the file "fname" and execute its lines as EX commands.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219 * When "ret_sid" is not NULL and we loaded the script before, don't load it
1220 * again.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001221 *
1222 * This function may be called recursively!
1223 *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001224 * Return FAIL if file could not be opened, OK otherwise.
1225 * If a scriptitem_T was found or created "*ret_sid" is set to the SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001226 */
1227 int
1228do_source(
1229 char_u *fname,
1230 int check_other, // check for .vimrc and _vimrc
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001231 int is_vimrc, // DOSO_ value
1232 int *ret_sid UNUSED)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001233{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001234 source_cookie_T cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001235 char_u *p;
1236 char_u *fname_exp;
1237 char_u *firstline = NULL;
1238 int retval = FAIL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001239 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001240#ifdef FEAT_EVAL
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001241 static int last_current_SID_seq = 0;
1242 funccal_entry_T funccalp_entry;
1243 int save_debug_break_level = debug_break_level;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001244 int sid;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001245 scriptitem_T *si = NULL;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001246 int save_estack_compiling = estack_compiling;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001247#endif
1248#ifdef STARTUPTIME
1249 struct timeval tv_rel;
1250 struct timeval tv_start;
1251#endif
1252#ifdef FEAT_PROFILE
1253 proftime_T wait_start;
1254#endif
1255 int trigger_source_post = FALSE;
Bram Moolenaare31ee862020-01-07 20:59:34 +01001256 ESTACK_CHECK_DECLARATION
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001257
1258 p = expand_env_save(fname);
1259 if (p == NULL)
1260 return retval;
1261 fname_exp = fix_fname(p);
1262 vim_free(p);
1263 if (fname_exp == NULL)
1264 return retval;
1265 if (mch_isdir(fname_exp))
1266 {
1267 smsg(_("Cannot source a directory: \"%s\""), fname);
1268 goto theend;
1269 }
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001270#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001271 estack_compiling = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001272
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 // See if we loaded this script before.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001274 sid = find_script_by_name(fname_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275 if (sid > 0 && ret_sid != NULL)
1276 {
1277 // Already loaded and no need to load again, return here.
1278 *ret_sid = sid;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001279 retval = OK;
1280 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001281 }
1282#endif
1283
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001284 // Apply SourceCmd autocommands, they should get the file and source it.
1285 if (has_autocmd(EVENT_SOURCECMD, fname_exp, NULL)
1286 && apply_autocmds(EVENT_SOURCECMD, fname_exp, fname_exp,
1287 FALSE, curbuf))
1288 {
1289#ifdef FEAT_EVAL
1290 retval = aborting() ? FAIL : OK;
1291#else
1292 retval = OK;
1293#endif
1294 if (retval == OK)
1295 // Apply SourcePost autocommands.
1296 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp,
1297 FALSE, curbuf);
1298 goto theend;
1299 }
1300
1301 // Apply SourcePre autocommands, they may get the file.
1302 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf);
1303
1304#ifdef USE_FOPEN_NOINH
1305 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1306#else
1307 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1308#endif
1309 if (cookie.fp == NULL && check_other)
1310 {
1311 // Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
1312 // and ".exrc" by "_exrc" or vice versa.
1313 p = gettail(fname_exp);
1314 if ((*p == '.' || *p == '_')
1315 && (STRICMP(p + 1, "vimrc") == 0
1316 || STRICMP(p + 1, "gvimrc") == 0
1317 || STRICMP(p + 1, "exrc") == 0))
1318 {
1319 if (*p == '_')
1320 *p = '.';
1321 else
1322 *p = '_';
1323#ifdef USE_FOPEN_NOINH
1324 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1325#else
1326 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1327#endif
1328 }
1329 }
1330
1331 if (cookie.fp == NULL)
1332 {
1333 if (p_verbose > 0)
1334 {
1335 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001336 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001337 smsg(_("could not source \"%s\""), fname);
1338 else
1339 smsg(_("line %ld: could not source \"%s\""),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001340 SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001341 verbose_leave();
1342 }
1343 goto theend;
1344 }
1345
1346 // The file exists.
1347 // - In verbose mode, give a message.
1348 // - For a vimrc file, may want to set 'compatible', call vimrc_found().
1349 if (p_verbose > 1)
1350 {
1351 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001352 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001353 smsg(_("sourcing \"%s\""), fname);
1354 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001355 smsg(_("line %ld: sourcing \"%s\""), SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001356 verbose_leave();
1357 }
1358 if (is_vimrc == DOSO_VIMRC)
1359 vimrc_found(fname_exp, (char_u *)"MYVIMRC");
1360 else if (is_vimrc == DOSO_GVIMRC)
1361 vimrc_found(fname_exp, (char_u *)"MYGVIMRC");
1362
1363#ifdef USE_CRNL
1364 // If no automatic file format: Set default to CR-NL.
1365 if (*p_ffs == NUL)
1366 cookie.fileformat = EOL_DOS;
1367 else
1368 cookie.fileformat = EOL_UNKNOWN;
1369 cookie.error = FALSE;
1370#endif
1371
1372 cookie.nextline = NULL;
1373 cookie.sourcing_lnum = 0;
1374 cookie.finished = FALSE;
1375
1376#ifdef FEAT_EVAL
1377 // Check if this script has a breakpoint.
1378 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0);
1379 cookie.fname = fname_exp;
1380 cookie.dbg_tick = debug_tick;
1381
1382 cookie.level = ex_nesting_level;
1383#endif
1384
1385 // Keep the sourcing name/lnum, for recursive calls.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001386 estack_push(ETYPE_SCRIPT, fname_exp, 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001387 ESTACK_CHECK_SETUP
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001388
1389#ifdef STARTUPTIME
1390 if (time_fd != NULL)
1391 time_push(&tv_rel, &tv_start);
1392#endif
1393
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001394 save_current_sctx = current_sctx;
1395 current_sctx.sc_version = 1; // default script version
1396
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001397#ifdef FEAT_EVAL
1398# ifdef FEAT_PROFILE
1399 if (do_profiling == PROF_YES)
1400 prof_child_enter(&wait_start); // entering a child now
1401# endif
1402
1403 // Don't use local function variables, if called from a function.
1404 // Also starts profiling timer for nested script.
1405 save_funccal(&funccalp_entry);
1406
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001407 current_sctx.sc_lnum = 0;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001408
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001409 // Check if this script was sourced before to find its SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001410 // Always use a new sequence number.
1411 current_sctx.sc_seq = ++last_current_SID_seq;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412 if (sid > 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001413 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001414 hashtab_T *ht;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001415 int todo;
1416 hashitem_T *hi;
1417 dictitem_T *di;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001418
1419 // loading the same script again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001420 current_sctx.sc_sid = sid;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001421 si = SCRIPT_ITEM(sid);
1422 if (si->sn_state == SN_STATE_NOT_LOADED)
1423 {
1424 // this script was found but not loaded yet
1425 si->sn_state = SN_STATE_NEW;
1426 }
1427 else
1428 {
1429 si->sn_state = SN_STATE_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001430
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001431 // Script-local variables remain but "const" can be set again.
1432 // In Vim9 script variables will be cleared when "vim9script" is
1433 // encountered without the "noclear" argument.
1434 ht = &SCRIPT_VARS(sid);
1435 todo = (int)ht->ht_used;
1436 for (hi = ht->ht_array; todo > 0; ++hi)
1437 if (!HASHITEM_EMPTY(hi))
1438 {
1439 --todo;
1440 di = HI2DI(hi);
1441 di->di_flags |= DI_FLAGS_RELOAD;
1442 }
1443 // imports can be redefined once
1444 mark_imports_for_reload(sid);
Bram Moolenaar0123cc12021-02-07 17:17:58 +01001445
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001446 // reset version, "vim9script" may have been added or removed.
1447 si->sn_version = 1;
1448 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001449 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001450 else
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001451 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001452 int error = OK;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001453
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001454 // It's new, generate a new SID and initialize the scriptitem.
1455 current_sctx.sc_sid = get_new_scriptitem(&error);
1456 if (error == FAIL)
1457 goto almosttheend;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001458 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001459 si->sn_name = fname_exp;
1460 fname_exp = vim_strsave(si->sn_name); // used for autocmd
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461 if (ret_sid != NULL)
1462 *ret_sid = current_sctx.sc_sid;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001463
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001464 // Remember the "is_vimrc" flag for when the file is sourced again.
1465 si->sn_is_vimrc = is_vimrc;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001466 }
1467
1468# ifdef FEAT_PROFILE
1469 if (do_profiling == PROF_YES)
1470 {
1471 int forceit;
1472
1473 // Check if we do profiling for this script.
1474 if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit))
1475 {
1476 script_do_profile(si);
1477 si->sn_pr_force = forceit;
1478 }
1479 if (si->sn_prof_on)
1480 {
1481 ++si->sn_pr_count;
1482 profile_start(&si->sn_pr_start);
1483 profile_zero(&si->sn_pr_children);
1484 }
1485 }
1486# endif
1487#endif
1488
1489 cookie.conv.vc_type = CONV_NONE; // no conversion
1490
1491 // Read the first line so we can check for a UTF-8 BOM.
1492 firstline = getsourceline(0, (void *)&cookie, 0, TRUE);
1493 if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef
1494 && firstline[1] == 0xbb && firstline[2] == 0xbf)
1495 {
1496 // Found BOM; setup conversion, skip over BOM and recode the line.
1497 convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
1498 p = string_convert(&cookie.conv, firstline + 3, NULL);
1499 if (p == NULL)
1500 p = vim_strsave(firstline + 3);
1501 if (p != NULL)
1502 {
1503 vim_free(firstline);
1504 firstline = p;
1505 }
1506 }
1507
1508 // Call do_cmdline, which will call getsourceline() to get the lines.
1509 do_cmdline(firstline, getsourceline, (void *)&cookie,
1510 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT);
1511 retval = OK;
1512
1513#ifdef FEAT_PROFILE
1514 if (do_profiling == PROF_YES)
1515 {
1516 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001517 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001518 if (si->sn_prof_on)
1519 {
1520 profile_end(&si->sn_pr_start);
1521 profile_sub_wait(&wait_start, &si->sn_pr_start);
1522 profile_add(&si->sn_pr_total, &si->sn_pr_start);
1523 profile_self(&si->sn_pr_self, &si->sn_pr_start,
1524 &si->sn_pr_children);
1525 }
1526 }
1527#endif
1528
1529 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001530 emsg(_(e_interrupted));
Bram Moolenaare31ee862020-01-07 20:59:34 +01001531 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001532 estack_pop();
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001533 if (p_verbose > 1)
1534 {
1535 verbose_enter();
1536 smsg(_("finished sourcing %s"), fname);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001537 if (SOURCING_NAME != NULL)
1538 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001539 verbose_leave();
1540 }
1541#ifdef STARTUPTIME
1542 if (time_fd != NULL)
1543 {
1544 vim_snprintf((char *)IObuff, IOSIZE, "sourcing %s", fname);
1545 time_msg((char *)IObuff, &tv_start);
1546 time_pop(&tv_rel);
1547 }
1548#endif
1549
1550 if (!got_int)
1551 trigger_source_post = TRUE;
1552
1553#ifdef FEAT_EVAL
1554 // After a "finish" in debug mode, need to break at first command of next
1555 // sourced file.
1556 if (save_debug_break_level > ex_nesting_level
1557 && debug_break_level == ex_nesting_level)
1558 ++debug_break_level;
1559#endif
1560
1561#ifdef FEAT_EVAL
1562almosttheend:
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001563 // If "sn_save_cpo" is set that means we encountered "vim9script": restore
1564 // 'cpoptions', unless in the main .vimrc file.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001565 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001566 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001567 if (si->sn_save_cpo != NULL && si->sn_is_vimrc == DOSO_NONE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568 {
Bram Moolenaar3e191692021-03-17 17:46:00 +01001569 if (STRCMP(p_cpo, CPO_VIM) != 0)
1570 {
1571 char_u *f;
1572 char_u *t;
1573
1574 // 'cpo' was changed in the script. Apply the same change to the
1575 // saved value, if possible.
1576 for (f = (char_u *)CPO_VIM; *f != NUL; ++f)
1577 if (vim_strchr(p_cpo, *f) == NULL
1578 && (t = vim_strchr(si->sn_save_cpo, *f)) != NULL)
1579 // flag was removed, also remove it from the saved 'cpo'
1580 mch_memmove(t, t + 1, STRLEN(t));
1581 for (f = p_cpo; *f != NUL; ++f)
1582 if (vim_strchr((char_u *)CPO_VIM, *f) == NULL
1583 && vim_strchr(si->sn_save_cpo, *f) == NULL)
1584 {
1585 // flag was added, also add it to the saved 'cpo'
1586 t = alloc(STRLEN(si->sn_save_cpo) + 2);
1587 if (t != NULL)
1588 {
1589 *t = *f;
1590 STRCPY(t + 1, si->sn_save_cpo);
1591 vim_free(si->sn_save_cpo);
1592 si->sn_save_cpo = t;
1593 }
1594 }
1595 }
Bram Moolenaar37294bd2021-03-10 13:40:08 +01001596 set_option_value((char_u *)"cpo", 0L, si->sn_save_cpo, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001597 }
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001598 VIM_CLEAR(si->sn_save_cpo);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001600 restore_funccal();
1601# ifdef FEAT_PROFILE
1602 if (do_profiling == PROF_YES)
1603 prof_child_exit(&wait_start); // leaving a child now
1604# endif
1605#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001606 current_sctx = save_current_sctx;
1607
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001608 fclose(cookie.fp);
1609 vim_free(cookie.nextline);
1610 vim_free(firstline);
1611 convert_setup(&cookie.conv, NULL, NULL);
1612
1613 if (trigger_source_post)
1614 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp, FALSE, curbuf);
1615
1616theend:
1617 vim_free(fname_exp);
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001618#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001619 estack_compiling = save_estack_compiling;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001620#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001621 return retval;
1622}
1623
1624#if defined(FEAT_EVAL) || defined(PROTO)
1625
1626/*
1627 * ":scriptnames"
1628 */
1629 void
1630ex_scriptnames(exarg_T *eap)
1631{
1632 int i;
1633
1634 if (eap->addr_count > 0)
1635 {
1636 // :script {scriptId}: edit the script
Bram Moolenaare3d46852020-08-29 13:39:17 +02001637 if (!SCRIPT_ID_VALID(eap->line2))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001638 emsg(_(e_invalid_argument));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001639 else
1640 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001641 eap->arg = SCRIPT_ITEM(eap->line2)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001642 do_exedit(eap, NULL);
1643 }
1644 return;
1645 }
1646
1647 for (i = 1; i <= script_items.ga_len && !got_int; ++i)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001648 if (SCRIPT_ITEM(i)->sn_name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001649 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001650 home_replace(NULL, SCRIPT_ITEM(i)->sn_name,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001651 NameBuff, MAXPATHL, TRUE);
1652 smsg("%3d: %s", i, NameBuff);
1653 }
1654}
1655
1656# if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1657/*
1658 * Fix slashes in the list of script names for 'shellslash'.
1659 */
1660 void
1661scriptnames_slash_adjust(void)
1662{
1663 int i;
1664
1665 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001666 if (SCRIPT_ITEM(i)->sn_name != NULL)
1667 slash_adjust(SCRIPT_ITEM(i)->sn_name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001668}
1669# endif
1670
1671/*
1672 * Get a pointer to a script name. Used for ":verbose set".
Bram Moolenaar74667062020-12-28 15:41:41 +01001673 * Message appended to "Last set from "
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001674 */
1675 char_u *
1676get_scriptname(scid_T id)
1677{
1678 if (id == SID_MODELINE)
1679 return (char_u *)_("modeline");
1680 if (id == SID_CMDARG)
1681 return (char_u *)_("--cmd argument");
1682 if (id == SID_CARG)
1683 return (char_u *)_("-c argument");
1684 if (id == SID_ENV)
1685 return (char_u *)_("environment variable");
1686 if (id == SID_ERROR)
1687 return (char_u *)_("error handler");
Bram Moolenaar74667062020-12-28 15:41:41 +01001688 if (id == SID_WINLAYOUT)
1689 return (char_u *)_("changed window size");
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001690 return SCRIPT_ITEM(id)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001691}
1692
1693# if defined(EXITFREE) || defined(PROTO)
1694 void
1695free_scriptnames(void)
1696{
1697 int i;
1698
1699 for (i = script_items.ga_len; i > 0; --i)
Bram Moolenaar86173482019-10-01 17:02:16 +02001700 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001701 scriptitem_T *si = SCRIPT_ITEM(i);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001703 // the variables themselves are cleared in evalvars_clear()
1704 vim_free(si->sn_vars);
1705
1706 vim_free(si->sn_name);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001707 free_imports_and_script_vars(i);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001708 free_string_option(si->sn_save_cpo);
Bram Moolenaara720be72019-10-22 21:45:19 +02001709# ifdef FEAT_PROFILE
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001710 ga_clear(&si->sn_prl_ga);
Bram Moolenaara720be72019-10-22 21:45:19 +02001711# endif
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001712 vim_free(si);
Bram Moolenaar86173482019-10-01 17:02:16 +02001713 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001714 ga_clear(&script_items);
1715}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001716
1717 void
1718free_autoload_scriptnames(void)
1719{
1720 ga_clear_strings(&ga_loaded);
1721}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001722# endif
1723
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001724 linenr_T
Bram Moolenaar66250c92020-08-20 15:02:42 +02001725get_sourced_lnum(
1726 char_u *(*fgetline)(int, void *, int, getline_opt_T),
1727 void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001728{
1729 return fgetline == getsourceline
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001730 ? ((source_cookie_T *)cookie)->sourcing_lnum
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001731 : SOURCING_LNUM;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001732}
Dominique Pelle748b3082022-01-08 12:41:16 +00001733#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001734
1735 static char_u *
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001736get_one_sourceline(source_cookie_T *sp)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001737{
1738 garray_T ga;
1739 int len;
1740 int c;
1741 char_u *buf;
1742#ifdef USE_CRNL
1743 int has_cr; // CR-LF found
1744#endif
1745 int have_read = FALSE;
1746
1747 // use a growarray to store the sourced line
1748 ga_init2(&ga, 1, 250);
1749
1750 // Loop until there is a finished line (or end-of-file).
1751 ++sp->sourcing_lnum;
1752 for (;;)
1753 {
1754 // make room to read at least 120 (more) characters
1755 if (ga_grow(&ga, 120) == FAIL)
1756 break;
1757 buf = (char_u *)ga.ga_data;
1758
1759 if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
1760 sp->fp) == NULL)
1761 break;
1762 len = ga.ga_len + (int)STRLEN(buf + ga.ga_len);
1763#ifdef USE_CRNL
1764 // Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the
1765 // CTRL-Z by its own, or after a NL.
1766 if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n'))
1767 && sp->fileformat == EOL_DOS
1768 && buf[len - 1] == Ctrl_Z)
1769 {
1770 buf[len - 1] = NUL;
1771 break;
1772 }
1773#endif
1774
1775 have_read = TRUE;
1776 ga.ga_len = len;
1777
1778 // If the line was longer than the buffer, read more.
1779 if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n')
1780 continue;
1781
1782 if (len >= 1 && buf[len - 1] == '\n') // remove trailing NL
1783 {
1784#ifdef USE_CRNL
1785 has_cr = (len >= 2 && buf[len - 2] == '\r');
1786 if (sp->fileformat == EOL_UNKNOWN)
1787 {
1788 if (has_cr)
1789 sp->fileformat = EOL_DOS;
1790 else
1791 sp->fileformat = EOL_UNIX;
1792 }
1793
1794 if (sp->fileformat == EOL_DOS)
1795 {
1796 if (has_cr) // replace trailing CR
1797 {
1798 buf[len - 2] = '\n';
1799 --len;
1800 --ga.ga_len;
1801 }
1802 else // lines like ":map xx yy^M" will have failed
1803 {
1804 if (!sp->error)
1805 {
1806 msg_source(HL_ATTR(HLF_W));
1807 emsg(_("W15: Warning: Wrong line separator, ^M may be missing"));
1808 }
1809 sp->error = TRUE;
1810 sp->fileformat = EOL_UNIX;
1811 }
1812 }
1813#endif
1814 // The '\n' is escaped if there is an odd number of ^V's just
1815 // before it, first set "c" just before the 'V's and then check
1816 // len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo
1817 for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--)
1818 ;
1819 if ((len & 1) != (c & 1)) // escaped NL, read more
1820 {
1821 ++sp->sourcing_lnum;
1822 continue;
1823 }
1824
1825 buf[len - 1] = NUL; // remove the NL
1826 }
1827
1828 // Check for ^C here now and then, so recursive :so can be broken.
1829 line_breakcheck();
1830 break;
1831 }
1832
1833 if (have_read)
1834 return (char_u *)ga.ga_data;
1835
1836 vim_free(ga.ga_data);
1837 return NULL;
1838}
1839
1840/*
1841 * Get one full line from a sourced file.
1842 * Called by do_cmdline() when it's called from do_source().
1843 *
1844 * Return a pointer to the line in allocated memory.
1845 * Return NULL for end-of-file or some error.
1846 */
1847 char_u *
Bram Moolenaar66250c92020-08-20 15:02:42 +02001848getsourceline(
1849 int c UNUSED,
1850 void *cookie,
1851 int indent UNUSED,
1852 getline_opt_T options)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001853{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001854 source_cookie_T *sp = (source_cookie_T *)cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001855 char_u *line;
1856 char_u *p;
Bram Moolenaardcc58e02020-12-28 20:53:21 +01001857 int do_vim9_all = in_vim9script()
1858 && options == GETLINE_CONCAT_ALL;
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01001859 int do_bar_cont = do_vim9_all
1860 || options == GETLINE_CONCAT_CONTBAR;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001861
1862#ifdef FEAT_EVAL
1863 // If breakpoints have been added/deleted need to check for it.
1864 if (sp->dbg_tick < debug_tick)
1865 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001866 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001867 sp->dbg_tick = debug_tick;
1868 }
1869# ifdef FEAT_PROFILE
1870 if (do_profiling == PROF_YES)
1871 script_line_end();
1872# endif
1873#endif
1874
1875 // Set the current sourcing line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001876 SOURCING_LNUM = sp->sourcing_lnum + 1;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001877
1878 // Get current line. If there is a read-ahead line, use it, otherwise get
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001879 // one now. "fp" is NULL if actually using a string.
1880 if (sp->finished || sp->fp == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001881 line = NULL;
1882 else if (sp->nextline == NULL)
1883 line = get_one_sourceline(sp);
1884 else
1885 {
1886 line = sp->nextline;
1887 sp->nextline = NULL;
1888 ++sp->sourcing_lnum;
1889 }
1890#ifdef FEAT_PROFILE
1891 if (line != NULL && do_profiling == PROF_YES)
1892 script_line_start();
1893#endif
1894
1895 // Only concatenate lines starting with a \ when 'cpoptions' doesn't
1896 // contain the 'C' flag.
Bram Moolenaar66250c92020-08-20 15:02:42 +02001897 if (line != NULL && options != GETLINE_NONE
1898 && vim_strchr(p_cpo, CPO_CONCAT) == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001899 {
Bram Moolenaar5072b472021-06-03 21:56:10 +02001900 int comment_char = in_vim9script() ? '#' : '"';
1901
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001902 // compensate for the one line read-ahead
1903 --sp->sourcing_lnum;
1904
1905 // Get the next line and concatenate it when it starts with a
1906 // backslash. We always need to read the next line, keep it in
1907 // sp->nextline.
1908 /* Also check for a comment in between continuation lines: "\ */
Bram Moolenaardcc58e02020-12-28 20:53:21 +01001909 // Also check for a Vim9 comment, empty line, line starting with '|',
1910 // but not "||".
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001911 sp->nextline = get_one_sourceline(sp);
1912 if (sp->nextline != NULL
1913 && (*(p = skipwhite(sp->nextline)) == '\\'
Bram Moolenaar5072b472021-06-03 21:56:10 +02001914 || (p[0] == comment_char
1915 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01001916 || (do_vim9_all && (*p == NUL
1917 || vim9_comment_start(p)))
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01001918 || (do_bar_cont && p[0] == '|' && p[1] != '|')))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001919 {
1920 garray_T ga;
1921
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001922 ga_init2(&ga, sizeof(char_u), 400);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001923 ga_concat(&ga, line);
1924 if (*p == '\\')
1925 ga_concat(&ga, p + 1);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01001926 else if (*p == '|')
1927 {
1928 ga_concat(&ga, (char_u *)" ");
1929 ga_concat(&ga, p);
1930 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001931 for (;;)
1932 {
1933 vim_free(sp->nextline);
1934 sp->nextline = get_one_sourceline(sp);
1935 if (sp->nextline == NULL)
1936 break;
1937 p = skipwhite(sp->nextline);
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01001938 if (*p == '\\' || (do_bar_cont && p[0] == '|' && p[1] != '|'))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001939 {
1940 // Adjust the growsize to the current length to speed up
1941 // concatenating many lines.
1942 if (ga.ga_len > 400)
1943 {
1944 if (ga.ga_len > 8000)
1945 ga.ga_growsize = 8000;
1946 else
1947 ga.ga_growsize = ga.ga_len;
1948 }
Bram Moolenaardcc58e02020-12-28 20:53:21 +01001949 if (*p == '\\')
1950 ga_concat(&ga, p + 1);
1951 else
1952 {
1953 ga_concat(&ga, (char_u *)" ");
1954 ga_concat(&ga, p);
1955 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001956 }
Bram Moolenaar5072b472021-06-03 21:56:10 +02001957 else if (!(p[0] == (comment_char)
Bram Moolenaar0f37e352021-06-02 15:28:15 +02001958 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01001959 && !(do_vim9_all && (*p == NUL || vim9_comment_start(p))))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001960 break;
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001961 /* drop a # comment or "\ comment line */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001962 }
1963 ga_append(&ga, NUL);
1964 vim_free(line);
1965 line = ga.ga_data;
1966 }
1967 }
1968
1969 if (line != NULL && sp->conv.vc_type != CONV_NONE)
1970 {
1971 char_u *s;
1972
1973 // Convert the encoding of the script line.
1974 s = string_convert(&sp->conv, line, NULL);
1975 if (s != NULL)
1976 {
1977 vim_free(line);
1978 line = s;
1979 }
1980 }
1981
1982#ifdef FEAT_EVAL
1983 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001984 if (sp->breakpoint != 0 && sp->breakpoint <= SOURCING_LNUM)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001985 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001986 dbg_breakpoint(sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001987 // Find next breakpoint.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001988 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001989 sp->dbg_tick = debug_tick;
1990 }
1991#endif
1992
1993 return line;
1994}
1995
1996/*
1997 * ":scriptencoding": Set encoding conversion for a sourced script.
1998 */
1999 void
2000ex_scriptencoding(exarg_T *eap)
2001{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002002 source_cookie_T *sp;
2003 char_u *name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002004
2005 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
2006 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00002007 emsg(_(e_scriptencoding_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002008 return;
2009 }
2010
2011 if (*eap->arg != NUL)
2012 {
2013 name = enc_canonize(eap->arg);
2014 if (name == NULL) // out of memory
2015 return;
2016 }
2017 else
2018 name = eap->arg;
2019
2020 // Setup for conversion from the specified encoding to 'encoding'.
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002021 sp = (source_cookie_T *)getline_cookie(eap->getline, eap->cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002022 convert_setup(&sp->conv, name, p_enc);
2023
2024 if (name != eap->arg)
2025 vim_free(name);
2026}
2027
2028/*
2029 * ":scriptversion": Set Vim script version for a sourced script.
2030 */
2031 void
2032ex_scriptversion(exarg_T *eap UNUSED)
2033{
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002034 int nr;
2035
2036 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
2037 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002038 emsg(_(e_scriptversion_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002039 return;
2040 }
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002041 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002043 emsg(_(e_cannot_use_scriptversion_after_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044 return;
2045 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002046
2047 nr = getdigits(&eap->arg);
2048 if (nr == 0 || *eap->arg != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002049 emsg(_(e_invalid_argument));
Bram Moolenaar67979662020-06-20 22:50:47 +02002050 else if (nr > SCRIPT_VERSION_MAX)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002051 semsg(_(e_scriptversion_not_supported_nr), nr);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002052 else
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002053 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002054 current_sctx.sc_version = nr;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002055#ifdef FEAT_EVAL
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002056 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = nr;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002057#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002058 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002059}
2060
2061#if defined(FEAT_EVAL) || defined(PROTO)
2062/*
2063 * ":finish": Mark a sourced file as finished.
2064 */
2065 void
2066ex_finish(exarg_T *eap)
2067{
2068 if (getline_equal(eap->getline, eap->cookie, getsourceline))
2069 do_finish(eap, FALSE);
2070 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00002071 emsg(_(e_finish_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002072}
2073
2074/*
2075 * Mark a sourced file as finished. Possibly makes the ":finish" pending.
2076 * Also called for a pending finish at the ":endtry" or after returning from
2077 * an extra do_cmdline(). "reanimate" is used in the latter case.
2078 */
2079 void
2080do_finish(exarg_T *eap, int reanimate)
2081{
2082 int idx;
2083
2084 if (reanimate)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002085 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002086 eap->cookie))->finished = FALSE;
2087
2088 // Cleanup (and inactivate) conditionals, but stop when a try conditional
2089 // not in its finally clause (which then is to be executed next) is found.
2090 // In this case, make the ":finish" pending for execution at the ":endtry".
2091 // Otherwise, finish normally.
2092 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
2093 if (idx >= 0)
2094 {
2095 eap->cstack->cs_pending[idx] = CSTP_FINISH;
2096 report_make_pending(CSTP_FINISH, NULL);
2097 }
2098 else
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002099 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002100 eap->cookie))->finished = TRUE;
2101}
2102
2103
2104/*
2105 * Return TRUE when a sourced file had the ":finish" command: Don't give error
2106 * message for missing ":endif".
2107 * Return FALSE when not sourcing a file.
2108 */
2109 int
2110source_finished(
Bram Moolenaar66250c92020-08-20 15:02:42 +02002111 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002112 void *cookie)
2113{
2114 return (getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002115 && ((source_cookie_T *)getline_cookie(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002116 fgetline, cookie))->finished);
2117}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002118
2119/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002120 * Find the path of a script below the "autoload" directory.
2121 * Returns NULL if there is no "/autoload/" in the script name.
2122 */
2123 char_u *
2124script_name_after_autoload(scriptitem_T *si)
2125{
2126 char_u *p = si->sn_name;
2127 char_u *res = NULL;
2128
2129 for (;;)
2130 {
2131 char_u *n = (char_u *)strstr((char *)p, "autoload");
2132
2133 if (n == NULL)
2134 break;
2135 if (n > p && vim_ispathsep(n[-1]) && vim_ispathsep(n[8]))
2136 res = n + 9;
2137 p = n + 8;
2138 }
2139 return res;
2140}
2141
2142/*
2143 * If in a Vim9 autoload script return "name" with the autoload prefix for the
2144 * script. If successful "name" is freed, the returned name is allocated.
2145 * Otherwise it returns "name" unmodified.
2146 */
2147 char_u *
2148may_prefix_autoload(char_u *name)
2149{
2150 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
2151 {
2152 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
2153
2154 if (si->sn_is_autoload)
2155 {
2156 char_u *p = script_name_after_autoload(si);
2157
2158 if (p != NULL)
2159 {
2160 char_u *tail = vim_strsave(p);
2161
2162 if (tail != NULL)
2163 {
2164 for (p = tail; *p != NUL; p += mb_ptr2len(p))
2165 {
2166 if (vim_ispathsep(*p))
2167 *p = '#';
2168 else if (STRCMP(p, ".vim"))
2169 {
2170 size_t len = (p - tail) + STRLEN(name) + 2;
2171 char_u *res = alloc(len);
2172
2173 if (res == NULL)
2174 break;
2175 *p = NUL;
2176 vim_snprintf((char *)res, len, "%s#%s", tail, name);
2177 vim_free(name);
2178 vim_free(tail);
2179 return res;
2180 }
2181 }
2182 }
2183 // did not find ".vim" at the end
2184 vim_free(tail);
2185 }
2186 }
2187 }
2188 return name;
2189}
2190
2191/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002192 * Return the autoload script name for a function or variable name.
2193 * Returns NULL when out of memory.
2194 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
2195 */
2196 char_u *
2197autoload_name(char_u *name)
2198{
2199 char_u *p, *q = NULL;
2200 char_u *scriptname;
2201
2202 // Get the script file name: replace '#' with '/', append ".vim".
2203 scriptname = alloc(STRLEN(name) + 14);
2204 if (scriptname == NULL)
2205 return NULL;
2206 STRCPY(scriptname, "autoload/");
Bram Moolenaara1773442020-08-12 15:21:22 +02002207 STRCAT(scriptname, name[0] == 'g' && name[1] == ':' ? name + 2: name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002208 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
2209 q = p, ++p)
2210 *p = '/';
2211 STRCPY(q, ".vim");
2212 return scriptname;
2213}
2214
2215/*
2216 * If "name" has a package name try autoloading the script for it.
2217 * Return TRUE if a package was loaded.
2218 */
2219 int
2220script_autoload(
2221 char_u *name,
2222 int reload) // load script again when already loaded
2223{
2224 char_u *p;
2225 char_u *scriptname, *tofree;
2226 int ret = FALSE;
2227 int i;
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002228 int ret_sid;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002229
2230 // If there is no '#' after name[0] there is no package name.
2231 p = vim_strchr(name, AUTOLOAD_CHAR);
2232 if (p == NULL || p == name)
2233 return FALSE;
2234
2235 tofree = scriptname = autoload_name(name);
2236 if (scriptname == NULL)
2237 return FALSE;
2238
2239 // Find the name in the list of previously loaded package names. Skip
2240 // "autoload/", it's always the same.
2241 for (i = 0; i < ga_loaded.ga_len; ++i)
2242 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
2243 break;
2244 if (!reload && i < ga_loaded.ga_len)
2245 ret = FALSE; // was loaded already
2246 else
2247 {
2248 // Remember the name if it wasn't loaded already.
2249 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
2250 {
2251 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
2252 tofree = NULL;
2253 }
2254
2255 // Try loading the package from $VIMRUNTIME/autoload/<name>.vim
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002256 // Use "ret_sid" to avoid loading the same script again.
2257 if (source_in_path(p_rtp, scriptname, 0, &ret_sid) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002258 ret = TRUE;
2259 }
2260
2261 vim_free(tofree);
2262 return ret;
2263}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002264#endif