blob: 6c1003c4113a34e22b102326def7b1ab35362868 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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 */
8/*
9 * if_perl.xs: Main code for Perl interface support.
10 * Mostly written by Sven Verdoolaege.
11 */
12
13#define _memory_h /* avoid memset redeclaration */
14#define IN_PERL_FILE /* don't include if_perl.pro from proto.h */
15
Bram Moolenaaraee1f4a2013-08-02 16:10:32 +020016/*
Bram Moolenaar6b107212013-12-11 15:06:40 +010017 * Currently 32-bit version of ActivePerl is built with VC6 (or MinGW since
18 * ActivePerl 5.18).
Bram Moolenaaraee1f4a2013-08-02 16:10:32 +020019 * (http://community.activestate.com/faq/windows-compilers-perl-modules)
20 * It means that time_t should be 32-bit. However the default size of
21 * time_t is 64-bit since VC8. So we have to define _USE_32BIT_TIME_T.
22 */
23#if defined(WIN32) && !defined(_WIN64)
24# define _USE_32BIT_TIME_T
25#endif
26
Bram Moolenaar6b107212013-12-11 15:06:40 +010027/*
28 * Prevent including winsock.h. perl.h tries to detect whether winsock.h is
29 * already included before including winsock2.h, because winsock2.h isn't
30 * compatible with winsock.h. However the detection doesn't work with some
31 * versions of MinGW. If WIN32_LEAN_AND_MEAN is defined, windows.h will not
32 * include winsock.h.
33 */
34#ifdef WIN32
35# define WIN32_LEAN_AND_MEAN
36#endif
37
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#include "vim.h"
39
Bram Moolenaar7c0daf02013-12-14 11:46:08 +010040/* Work around for perl-5.18.
41 * Don't include "perl\lib\CORE\inline.h" for now,
42 * include it after Perl_sv_free2 is defined. */
Philip H1d7caa52023-06-22 08:55:47 +020043#if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
Bram Moolenaar7c0daf02013-12-14 11:46:08 +010044# define PERL_NO_INLINE_FUNCTIONS
45#endif
46
Bram Moolenaar207fd752013-12-14 11:50:35 +010047#ifdef _MSC_VER
Bram Moolenaar02890652020-10-31 13:05:11 +010048// Work around for using MSVC and ActivePerl 5.18.
Bram Moolenaar207fd752013-12-14 11:50:35 +010049# define __inline__ __inline
Bram Moolenaare73b38f2020-01-06 21:22:09 +010050// Work around for using MSVC and Strawberry Perl 5.30.
51# define __builtin_expect(expr, val) (expr)
Bram Moolenaar02890652020-10-31 13:05:11 +010052// Work around for using MSVC and Strawberry Perl 5.32.
53# define NO_THREAD_SAFE_LOCALE
Bram Moolenaar207fd752013-12-14 11:50:35 +010054#endif
55
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010056#ifdef __GNUC__
57# pragma GCC diagnostic push
58# pragma GCC diagnostic ignored "-Wunused-variable"
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010059#endif
60
Bram Moolenaaraee1f4a2013-08-02 16:10:32 +020061#include <EXTERN.h>
62#include <perl.h>
63#include <XSUB.h>
Bram Moolenaar6244a0f2016-04-14 14:09:25 +020064#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
65# include <perliol.h>
66#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
Bram Moolenaarcf190c62016-06-02 11:54:06 +020068/* Workaround for perl < 5.8.7 */
69#ifndef PERLIO_FUNCS_DECL
70# ifdef PERLIO_FUNCS_CONST
71# define PERLIO_FUNCS_DECL(funcs) const PerlIO_funcs funcs
72# define PERLIO_FUNCS_CAST(funcs) (PerlIO_funcs*)(funcs)
73# else
74# define PERLIO_FUNCS_DECL(funcs) PerlIO_funcs funcs
75# define PERLIO_FUNCS_CAST(funcs) (funcs)
76# endif
77#endif
Bram Moolenaarc4bc0e62016-06-02 13:54:49 +020078#ifndef SvREFCNT_inc_void_NN
79# define SvREFCNT_inc_void_NN SvREFCNT_inc
80#endif
Bram Moolenaarcf190c62016-06-02 11:54:06 +020081
Bram Moolenaar071d4272004-06-13 20:20:40 +000082/*
83 * Work around clashes between Perl and Vim namespace. proto.h doesn't
84 * include if_perl.pro and perlsfio.pro when IN_PERL_FILE is defined, because
85 * we need the CV typedef. proto.h can't be moved to after including
86 * if_perl.h, because we get all sorts of name clashes then.
87 */
88#ifndef PROTO
Bram Moolenaareeb50ab2016-06-26 17:19:46 +020089# ifndef __MINGW32__
90# include "proto/if_perl.pro"
91# include "proto/if_perlsfio.pro"
92# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000093#endif
94
Bram Moolenaar1f402802018-09-21 14:01:27 +020095// Perl compatibility stuff. This should ensure compatibility with older
96// versions of Perl.
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#ifndef PERL_VERSION
Bram Moolenaareeb50ab2016-06-26 17:19:46 +020098# include <patchlevel.h>
99# define PERL_REVISION 5
100# define PERL_VERSION PATCHLEVEL
101# define PERL_SUBVERSION SUBVERSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#endif
103
Bram Moolenaar1f402802018-09-21 14:01:27 +0200104
105// Work around for ActivePerl 5.20.3+: Avoid generating (g)vim.lib.
106#if defined(ACTIVEPERL_VERSION) && (ACTIVEPERL_VERSION >= 2003) \
Bram Moolenaar4f974752019-02-17 17:44:42 +0100107 && defined(MSWIN) && defined(USE_DYNAMIC_LOADING)
Bram Moolenaar1f402802018-09-21 14:01:27 +0200108# undef XS_EXTERNAL
109# define XS_EXTERNAL(name) XSPROTO(name)
110#endif
111
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000112/*
113 * Quoting Jan Dubois of Active State:
114 * ActivePerl build 822 still identifies itself as 5.8.8 but already
115 * contains many of the changes from the upcoming Perl 5.8.9 release.
116 *
117 * The changes include addition of two symbols (Perl_sv_2iv_flags,
118 * Perl_newXS_flags) not present in earlier releases.
119 *
Bram Moolenaar3b9b13e2007-09-15 12:49:35 +0000120 * Jan Dubois suggested the following guarding scheme.
121 *
122 * Active State defined ACTIVEPERL_VERSION as a string in versions before
123 * 5.8.8; and so the comparison to 822 below needs to be guarded.
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000124 */
Bram Moolenaar3b9b13e2007-09-15 12:49:35 +0000125#if (PERL_REVISION == 5) && (PERL_VERSION == 8) && (PERL_SUBVERSION >= 8)
126# if (ACTIVEPERL_VERSION >= 822) || (PERL_SUBVERSION >= 9)
127# define PERL589_OR_LATER
128# endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000129#endif
130#if (PERL_REVISION == 5) && (PERL_VERSION >= 9)
131# define PERL589_OR_LATER
132#endif
133
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100134#if (PERL_REVISION == 5) && ((PERL_VERSION > 10) || \
135 (PERL_VERSION == 10) && (PERL_SUBVERSION >= 1))
136# define PERL5101_OR_LATER
137#endif
138
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#ifndef pTHX
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200140# define pTHX void
141# define pTHX_
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#endif
143
144#ifndef EXTERN_C
145# define EXTERN_C
146#endif
147
Philip H53752052022-11-04 22:32:21 +0000148// Suppress Infinite warnings when compiling XS modules under macOS 12 Monterey.
149#if defined(__clang__) && defined(__clang_major__) && __clang_major__ > 11
K.Takata161b6ac2022-11-14 15:31:07 +0000150# pragma clang diagnostic ignored "-Wcompound-token-split-by-macro"
Philip H53752052022-11-04 22:32:21 +0000151#endif
152
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153/* Compatibility hacks over */
154
155static PerlInterpreter *perl_interp = NULL;
Bram Moolenaard99df422016-01-29 23:20:40 +0100156static void xs_init(pTHX);
157static void VIM_init(void);
158EXTERN_C void boot_DynaLoader(pTHX_ CV*);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159
160/*
Bram Moolenaare06c1882010-07-21 22:05:20 +0200161 * For dynamic linked perl.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 */
163#if defined(DYNAMIC_PERL) || defined(PROTO)
Bram Moolenaare06c1882010-07-21 22:05:20 +0200164
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200165# ifndef DYNAMIC_PERL /* just generating prototypes */
Bram Moolenaar4f974752019-02-17 17:44:42 +0100166# ifdef MSWIN
Bram Moolenaare06c1882010-07-21 22:05:20 +0200167typedef int HANDLE;
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200168# endif
Bram Moolenaare06c1882010-07-21 22:05:20 +0200169typedef int XSINIT_t;
170typedef int XSUBADDR_t;
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200171# endif
172# ifndef USE_ITHREADS
Bram Moolenaare06c1882010-07-21 22:05:20 +0200173typedef int perl_key;
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200174# endif
Bram Moolenaare06c1882010-07-21 22:05:20 +0200175
Bram Moolenaar4f974752019-02-17 17:44:42 +0100176# ifndef MSWIN
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200177# include <dlfcn.h>
178# define HANDLE void*
179# define PERL_PROC void*
180# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
181# define symbol_from_dll dlsym
182# define close_dll dlclose
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200183# define load_dll_error dlerror
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200184# else
185# define PERL_PROC FARPROC
186# define load_dll vimLoadLib
187# define symbol_from_dll GetProcAddress
188# define close_dll FreeLibrary
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200189# define load_dll_error GetWin32Error
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200190# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191/*
192 * Wrapper defines
193 */
194# define perl_alloc dll_perl_alloc
195# define perl_construct dll_perl_construct
196# define perl_parse dll_perl_parse
197# define perl_run dll_perl_run
198# define perl_destruct dll_perl_destruct
199# define perl_free dll_perl_free
200# define Perl_get_context dll_Perl_get_context
201# define Perl_croak dll_Perl_croak
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100202# ifdef PERL5101_OR_LATER
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100203# define Perl_croak_xs_usage dll_Perl_croak_xs_usage
204# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205# ifndef PROTO
Bram Moolenaar81ea1df2020-04-11 18:01:41 +0200206# ifdef PERL_IMPLICIT_CONTEXT
207# define Perl_croak_nocontext dll_Perl_croak_nocontext
208# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209# define Perl_call_argv dll_Perl_call_argv
210# define Perl_call_pv dll_Perl_call_pv
211# define Perl_eval_sv dll_Perl_eval_sv
212# define Perl_get_sv dll_Perl_get_sv
213# define Perl_eval_pv dll_Perl_eval_pv
214# define Perl_call_method dll_Perl_call_method
215# endif
216# define Perl_dowantarray dll_Perl_dowantarray
217# define Perl_free_tmps dll_Perl_free_tmps
218# define Perl_gv_stashpv dll_Perl_gv_stashpv
219# define Perl_markstack_grow dll_Perl_markstack_grow
220# define Perl_mg_find dll_Perl_mg_find
Bram Moolenaar578333b2018-07-22 07:31:09 +0200221# if (PERL_REVISION == 5) && (PERL_VERSION >= 28)
222# define Perl_mg_get dll_Perl_mg_get
223# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224# define Perl_newXS dll_Perl_newXS
225# define Perl_newSV dll_Perl_newSV
226# define Perl_newSViv dll_Perl_newSViv
227# define Perl_newSVpv dll_Perl_newSVpv
228# define Perl_pop_scope dll_Perl_pop_scope
229# define Perl_push_scope dll_Perl_push_scope
230# define Perl_save_int dll_Perl_save_int
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200231# if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
232# define Perl_save_strlen dll_Perl_save_strlen
233# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234# define Perl_stack_grow dll_Perl_stack_grow
235# define Perl_set_context dll_Perl_set_context
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200236# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200237# define Perl_sv_2bool_flags dll_Perl_sv_2bool_flags
238# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
239# define Perl_xs_apiversion_bootcheck dll_Perl_xs_apiversion_bootcheck
240# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200241# else
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200242# define Perl_sv_2bool dll_Perl_sv_2bool
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200243# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244# define Perl_sv_2iv dll_Perl_sv_2iv
245# define Perl_sv_2mortal dll_Perl_sv_2mortal
246# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
247# define Perl_sv_2pv_flags dll_Perl_sv_2pv_flags
248# define Perl_sv_2pv_nolen dll_Perl_sv_2pv_nolen
249# else
250# define Perl_sv_2pv dll_Perl_sv_2pv
251# endif
Bram Moolenaar895a7a42020-09-10 21:36:11 +0200252# if (PERL_REVISION == 5) && (PERL_VERSION >= 32)
253# define Perl_sv_2pvbyte_flags dll_Perl_sv_2pvbyte_flags
254# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100255# define Perl_sv_2pvbyte dll_Perl_sv_2pvbyte
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256# define Perl_sv_bless dll_Perl_sv_bless
257# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
258# define Perl_sv_catpvn_flags dll_Perl_sv_catpvn_flags
259# else
260# define Perl_sv_catpvn dll_Perl_sv_catpvn
261# endif
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200262# ifdef PERL589_OR_LATER
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000263# define Perl_sv_2iv_flags dll_Perl_sv_2iv_flags
264# define Perl_newXS_flags dll_Perl_newXS_flags
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200265# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266# define Perl_sv_free dll_Perl_sv_free
Bram Moolenaarccf22172008-09-01 15:56:45 +0000267# if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
268# define Perl_sv_free2 dll_Perl_sv_free2
269# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270# define Perl_sv_isa dll_Perl_sv_isa
271# define Perl_sv_magic dll_Perl_sv_magic
272# define Perl_sv_setiv dll_Perl_sv_setiv
273# define Perl_sv_setpv dll_Perl_sv_setpv
274# define Perl_sv_setpvn dll_Perl_sv_setpvn
275# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
276# define Perl_sv_setsv_flags dll_Perl_sv_setsv_flags
277# else
278# define Perl_sv_setsv dll_Perl_sv_setsv
279# endif
280# define Perl_sv_upgrade dll_Perl_sv_upgrade
281# define Perl_Tstack_sp_ptr dll_Perl_Tstack_sp_ptr
282# define Perl_Top_ptr dll_Perl_Top_ptr
283# define Perl_Tstack_base_ptr dll_Perl_Tstack_base_ptr
284# define Perl_Tstack_max_ptr dll_Perl_Tstack_max_ptr
285# define Perl_Ttmps_ix_ptr dll_Perl_Ttmps_ix_ptr
286# define Perl_Ttmps_floor_ptr dll_Perl_Ttmps_floor_ptr
287# define Perl_Tmarkstack_ptr_ptr dll_Perl_Tmarkstack_ptr_ptr
288# define Perl_Tmarkstack_max_ptr dll_Perl_Tmarkstack_max_ptr
289# define Perl_TSv_ptr dll_Perl_TSv_ptr
290# define Perl_TXpv_ptr dll_Perl_TXpv_ptr
291# define Perl_Tna_ptr dll_Perl_Tna_ptr
292# define Perl_Idefgv_ptr dll_Perl_Idefgv_ptr
293# define Perl_Ierrgv_ptr dll_Perl_Ierrgv_ptr
294# define Perl_Isv_yes_ptr dll_Perl_Isv_yes_ptr
295# define boot_DynaLoader dll_boot_DynaLoader
Bram Moolenaare06c1882010-07-21 22:05:20 +0200296# define Perl_Gthr_key_ptr dll_Perl_Gthr_key_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000298# define Perl_sys_init dll_Perl_sys_init
Bram Moolenaarc236c162008-07-13 17:41:49 +0000299# define Perl_sys_term dll_Perl_sys_term
300# define Perl_ISv_ptr dll_Perl_ISv_ptr
301# define Perl_Istack_max_ptr dll_Perl_Istack_max_ptr
302# define Perl_Istack_base_ptr dll_Perl_Istack_base_ptr
303# define Perl_Itmps_ix_ptr dll_Perl_Itmps_ix_ptr
304# define Perl_Itmps_floor_ptr dll_Perl_Itmps_floor_ptr
305# define Perl_IXpv_ptr dll_Perl_IXpv_ptr
306# define Perl_Ina_ptr dll_Perl_Ina_ptr
307# define Perl_Imarkstack_ptr_ptr dll_Perl_Imarkstack_ptr_ptr
308# define Perl_Imarkstack_max_ptr dll_Perl_Imarkstack_max_ptr
309# define Perl_Istack_sp_ptr dll_Perl_Istack_sp_ptr
310# define Perl_Iop_ptr dll_Perl_Iop_ptr
311# define Perl_call_list dll_Perl_call_list
312# define Perl_Iscopestack_ix_ptr dll_Perl_Iscopestack_ix_ptr
313# define Perl_Iunitcheckav_ptr dll_Perl_Iunitcheckav_ptr
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200314# if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
315# define Perl_xs_handshake dll_Perl_xs_handshake
316# define Perl_xs_boot_epilog dll_Perl_xs_boot_epilog
317# endif
Bram Moolenaar01c10522012-09-21 12:50:51 +0200318# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100319# ifdef USE_ITHREADS
320# define PL_thr_key *dll_PL_thr_key
321# endif
Bram Moolenaar01c10522012-09-21 12:50:51 +0200322# endif
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100323# define Perl_hv_iternext_flags dll_Perl_hv_iternext_flags
324# define Perl_hv_iterinit dll_Perl_hv_iterinit
325# define Perl_hv_iterkey dll_Perl_hv_iterkey
326# define Perl_hv_iterval dll_Perl_hv_iterval
327# define Perl_av_fetch dll_Perl_av_fetch
328# define Perl_av_len dll_Perl_av_len
329# define Perl_sv_2nv_flags dll_Perl_sv_2nv_flags
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200330# if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
331# define PerlIOBase_pushed dll_PerlIOBase_pushed
332# define PerlIO_define_layer dll_PerlIO_define_layer
333# endif
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200334# if (PERL_REVISION == 5) && (PERL_VERSION >= 24)
335# define Perl_savetmps dll_Perl_savetmps
336# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000337
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338/*
339 * Declare HANDLE for perl.dll and function pointers.
340 */
341static HANDLE hPerlLib = NULL;
342
343static PerlInterpreter* (*perl_alloc)();
344static void (*perl_construct)(PerlInterpreter*);
345static void (*perl_destruct)(PerlInterpreter*);
346static void (*perl_free)(PerlInterpreter*);
347static int (*perl_run)(PerlInterpreter*);
348static int (*perl_parse)(PerlInterpreter*, XSINIT_t, int, char**, char**);
349static void* (*Perl_get_context)(void);
Bram Moolenaar864733a2016-04-02 14:18:01 +0200350static void (*Perl_croak)(pTHX_ const char*, ...) __attribute__noreturn__;
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200351# ifdef PERL5101_OR_LATER
Bram Moolenaar6b107212013-12-11 15:06:40 +0100352/* Perl-5.18 has a different Perl_croak_xs_usage signature. */
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200353# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
Bram Moolenaar864733a2016-04-02 14:18:01 +0200354static void (*Perl_croak_xs_usage)(const CV *const, const char *const params)
355 __attribute__noreturn__;
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200356# else
Bram Moolenaar864733a2016-04-02 14:18:01 +0200357static void (*Perl_croak_xs_usage)(pTHX_ const CV *const, const char *const params)
358 __attribute__noreturn__;
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200359# endif
Bram Moolenaar6b107212013-12-11 15:06:40 +0100360# endif
Bram Moolenaar81ea1df2020-04-11 18:01:41 +0200361# ifdef PERL_IMPLICIT_CONTEXT
Bram Moolenaar864733a2016-04-02 14:18:01 +0200362static void (*Perl_croak_nocontext)(const char*, ...) __attribute__noreturn__;
Bram Moolenaar81ea1df2020-04-11 18:01:41 +0200363# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364static I32 (*Perl_dowantarray)(pTHX);
365static void (*Perl_free_tmps)(pTHX);
366static HV* (*Perl_gv_stashpv)(pTHX_ const char*, I32);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200367# if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200368static I32* (*Perl_markstack_grow)(pTHX);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200369# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370static void (*Perl_markstack_grow)(pTHX);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200371# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372static MAGIC* (*Perl_mg_find)(pTHX_ SV*, int);
Bram Moolenaar578333b2018-07-22 07:31:09 +0200373# if (PERL_REVISION == 5) && (PERL_VERSION >= 28)
374static int (*Perl_mg_get)(pTHX_ SV*);
375# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376static CV* (*Perl_newXS)(pTHX_ char*, XSUBADDR_t, char*);
377static SV* (*Perl_newSV)(pTHX_ STRLEN);
378static SV* (*Perl_newSViv)(pTHX_ IV);
379static SV* (*Perl_newSVpv)(pTHX_ const char*, STRLEN);
380static I32 (*Perl_call_argv)(pTHX_ const char*, I32, char**);
381static I32 (*Perl_call_pv)(pTHX_ const char*, I32);
382static I32 (*Perl_eval_sv)(pTHX_ SV*, I32);
383static SV* (*Perl_get_sv)(pTHX_ const char*, I32);
384static SV* (*Perl_eval_pv)(pTHX_ const char*, I32);
385static SV* (*Perl_call_method)(pTHX_ const char*, I32);
386static void (*Perl_pop_scope)(pTHX);
387static void (*Perl_push_scope)(pTHX);
388static void (*Perl_save_int)(pTHX_ int*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200389# if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200390static void (*Perl_save_strlen)(pTHX_ STRLEN* ptr);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200391# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392static SV** (*Perl_stack_grow)(pTHX_ SV**, SV**p, int);
393static SV** (*Perl_set_context)(void*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200394# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200395static bool (*Perl_sv_2bool_flags)(pTHX_ SV*, I32);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200396# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200397static void (*Perl_xs_apiversion_bootcheck)(pTHX_ SV *module, const char *api_p, STRLEN api_len);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200398# endif
399# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400static bool (*Perl_sv_2bool)(pTHX_ SV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200401# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402static IV (*Perl_sv_2iv)(pTHX_ SV*);
403static SV* (*Perl_sv_2mortal)(pTHX_ SV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200404# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
Philip H1d7caa52023-06-22 08:55:47 +0200405static char* (*Perl_sv_2pv_flags)(pTHX_ SV*, STRLEN* const, const U32);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406static char* (*Perl_sv_2pv_nolen)(pTHX_ SV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200407# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408static char* (*Perl_sv_2pv)(pTHX_ SV*, STRLEN*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200409# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100410static char* (*Perl_sv_2pvbyte)(pTHX_ SV*, STRLEN*);
Bram Moolenaar895a7a42020-09-10 21:36:11 +0200411# if (PERL_REVISION == 5) && (PERL_VERSION >= 32)
Philip H1d7caa52023-06-22 08:55:47 +0200412static char* (*Perl_sv_2pvbyte_flags)(pTHX_ SV*, STRLEN* const, const U32);
Bram Moolenaar895a7a42020-09-10 21:36:11 +0200413# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414static SV* (*Perl_sv_bless)(pTHX_ SV*, HV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200415# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416static void (*Perl_sv_catpvn_flags)(pTHX_ SV* , const char*, STRLEN, I32);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200417# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418static void (*Perl_sv_catpvn)(pTHX_ SV*, const char*, STRLEN);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200419# endif
420# ifdef PERL589_OR_LATER
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000421static IV (*Perl_sv_2iv_flags)(pTHX_ SV* sv, I32 flags);
422static CV * (*Perl_newXS_flags)(pTHX_ const char *name, XSUBADDR_t subaddr, const char *const filename, const char *const proto, U32 flags);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200423# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424static void (*Perl_sv_free)(pTHX_ SV*);
425static int (*Perl_sv_isa)(pTHX_ SV*, const char*);
426static void (*Perl_sv_magic)(pTHX_ SV*, SV*, int, const char*, I32);
427static void (*Perl_sv_setiv)(pTHX_ SV*, IV);
428static void (*Perl_sv_setpv)(pTHX_ SV*, const char*);
429static void (*Perl_sv_setpvn)(pTHX_ SV*, const char*, STRLEN);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200430# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431static void (*Perl_sv_setsv_flags)(pTHX_ SV*, SV*, I32);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200432# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433static void (*Perl_sv_setsv)(pTHX_ SV*, SV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200434# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435static bool (*Perl_sv_upgrade)(pTHX_ SV*, U32);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200436# if (PERL_REVISION == 5) && (PERL_VERSION < 10)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437static SV*** (*Perl_Tstack_sp_ptr)(register PerlInterpreter*);
438static OP** (*Perl_Top_ptr)(register PerlInterpreter*);
439static SV*** (*Perl_Tstack_base_ptr)(register PerlInterpreter*);
440static SV*** (*Perl_Tstack_max_ptr)(register PerlInterpreter*);
441static I32* (*Perl_Ttmps_ix_ptr)(register PerlInterpreter*);
442static I32* (*Perl_Ttmps_floor_ptr)(register PerlInterpreter*);
443static I32** (*Perl_Tmarkstack_ptr_ptr)(register PerlInterpreter*);
444static I32** (*Perl_Tmarkstack_max_ptr)(register PerlInterpreter*);
445static SV** (*Perl_TSv_ptr)(register PerlInterpreter*);
446static XPV** (*Perl_TXpv_ptr)(register PerlInterpreter*);
447static STRLEN* (*Perl_Tna_ptr)(register PerlInterpreter*);
Bram Moolenaar6b107212013-12-11 15:06:40 +0100448# else
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200449/* Perl-5.18 has a different Perl_sv_free2 signature. */
450# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
451static void (*Perl_sv_free2)(pTHX_ SV*, const U32);
452# else
Bram Moolenaarccf22172008-09-01 15:56:45 +0000453static void (*Perl_sv_free2)(pTHX_ SV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200454# endif
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000455static void (*Perl_sys_init)(int* argc, char*** argv);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000456static void (*Perl_sys_term)(void);
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200457static void (*Perl_call_list)(pTHX_ I32, AV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200458# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
459# else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000460static SV** (*Perl_ISv_ptr)(register PerlInterpreter*);
461static SV*** (*Perl_Istack_max_ptr)(register PerlInterpreter*);
462static SV*** (*Perl_Istack_base_ptr)(register PerlInterpreter*);
463static XPV** (*Perl_IXpv_ptr)(register PerlInterpreter*);
464static I32* (*Perl_Itmps_ix_ptr)(register PerlInterpreter*);
465static I32* (*Perl_Itmps_floor_ptr)(register PerlInterpreter*);
466static STRLEN* (*Perl_Ina_ptr)(register PerlInterpreter*);
467static I32** (*Perl_Imarkstack_ptr_ptr)(register PerlInterpreter*);
468static I32** (*Perl_Imarkstack_max_ptr)(register PerlInterpreter*);
469static SV*** (*Perl_Istack_sp_ptr)(register PerlInterpreter*);
470static OP** (*Perl_Iop_ptr)(register PerlInterpreter*);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000471static I32* (*Perl_Iscopestack_ix_ptr)(register PerlInterpreter*);
472static AV** (*Perl_Iunitcheckav_ptr)(register PerlInterpreter*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200473# endif
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200474# endif
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200475# if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200476static I32 (*Perl_xs_handshake)(const U32, void *, const char *, ...);
477static void (*Perl_xs_boot_epilog)(pTHX_ const U32);
Bram Moolenaard8619992014-03-12 17:08:05 +0100478# endif
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200479
480# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
481# ifdef USE_ITHREADS
482static perl_key* dll_PL_thr_key;
483# endif
484# else
Bram Moolenaare06c1882010-07-21 22:05:20 +0200485static GV** (*Perl_Idefgv_ptr)(register PerlInterpreter*);
486static GV** (*Perl_Ierrgv_ptr)(register PerlInterpreter*);
487static SV* (*Perl_Isv_yes_ptr)(register PerlInterpreter*);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200488static perl_key* (*Perl_Gthr_key_ptr)_((pTHX));
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200489# endif
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200490static void (*boot_DynaLoader)_((pTHX_ CV*));
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100491static HE * (*Perl_hv_iternext_flags)(pTHX_ HV *, I32);
492static I32 (*Perl_hv_iterinit)(pTHX_ HV *);
493static char * (*Perl_hv_iterkey)(pTHX_ HE *, I32 *);
494static SV * (*Perl_hv_iterval)(pTHX_ HV *, HE *);
495static SV** (*Perl_av_fetch)(pTHX_ AV *, SSize_t, I32);
496static SSize_t (*Perl_av_len)(pTHX_ AV *);
497static NV (*Perl_sv_2nv_flags)(pTHX_ SV *const, const I32);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200498# if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200499static IV (*PerlIOBase_pushed)(pTHX_ PerlIO *, const char *, SV *, PerlIO_funcs *);
500static void (*PerlIO_define_layer)(pTHX_ PerlIO_funcs *);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200501# endif
502# if (PERL_REVISION == 5) && (PERL_VERSION >= 24)
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200503static void (*Perl_savetmps)(pTHX);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200504# endif
Bram Moolenaare06c1882010-07-21 22:05:20 +0200505
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506/*
507 * Table of name to function pointer of perl.
508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509static struct {
510 char* name;
511 PERL_PROC* ptr;
512} perl_funcname_table[] = {
513 {"perl_alloc", (PERL_PROC*)&perl_alloc},
514 {"perl_construct", (PERL_PROC*)&perl_construct},
515 {"perl_destruct", (PERL_PROC*)&perl_destruct},
516 {"perl_free", (PERL_PROC*)&perl_free},
517 {"perl_run", (PERL_PROC*)&perl_run},
518 {"perl_parse", (PERL_PROC*)&perl_parse},
519 {"Perl_get_context", (PERL_PROC*)&Perl_get_context},
520 {"Perl_croak", (PERL_PROC*)&Perl_croak},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200521# ifdef PERL5101_OR_LATER
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100522 {"Perl_croak_xs_usage", (PERL_PROC*)&Perl_croak_xs_usage},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200523# endif
524# ifdef PERL_IMPLICIT_CONTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 {"Perl_croak_nocontext", (PERL_PROC*)&Perl_croak_nocontext},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200526# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 {"Perl_dowantarray", (PERL_PROC*)&Perl_dowantarray},
528 {"Perl_free_tmps", (PERL_PROC*)&Perl_free_tmps},
529 {"Perl_gv_stashpv", (PERL_PROC*)&Perl_gv_stashpv},
530 {"Perl_markstack_grow", (PERL_PROC*)&Perl_markstack_grow},
531 {"Perl_mg_find", (PERL_PROC*)&Perl_mg_find},
Bram Moolenaar578333b2018-07-22 07:31:09 +0200532# if (PERL_REVISION == 5) && (PERL_VERSION >= 28)
533 {"Perl_mg_get", (PERL_PROC*)&Perl_mg_get},
534# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 {"Perl_newXS", (PERL_PROC*)&Perl_newXS},
536 {"Perl_newSV", (PERL_PROC*)&Perl_newSV},
537 {"Perl_newSViv", (PERL_PROC*)&Perl_newSViv},
538 {"Perl_newSVpv", (PERL_PROC*)&Perl_newSVpv},
539 {"Perl_call_argv", (PERL_PROC*)&Perl_call_argv},
540 {"Perl_call_pv", (PERL_PROC*)&Perl_call_pv},
541 {"Perl_eval_sv", (PERL_PROC*)&Perl_eval_sv},
542 {"Perl_get_sv", (PERL_PROC*)&Perl_get_sv},
543 {"Perl_eval_pv", (PERL_PROC*)&Perl_eval_pv},
544 {"Perl_call_method", (PERL_PROC*)&Perl_call_method},
545 {"Perl_pop_scope", (PERL_PROC*)&Perl_pop_scope},
546 {"Perl_push_scope", (PERL_PROC*)&Perl_push_scope},
547 {"Perl_save_int", (PERL_PROC*)&Perl_save_int},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200548# if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200549 {"Perl_save_strlen", (PERL_PROC*)&Perl_save_strlen},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200550# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 {"Perl_stack_grow", (PERL_PROC*)&Perl_stack_grow},
552 {"Perl_set_context", (PERL_PROC*)&Perl_set_context},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200553# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200554 {"Perl_sv_2bool_flags", (PERL_PROC*)&Perl_sv_2bool_flags},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200555# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200556 {"Perl_xs_apiversion_bootcheck",(PERL_PROC*)&Perl_xs_apiversion_bootcheck},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200557# endif
558# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 {"Perl_sv_2bool", (PERL_PROC*)&Perl_sv_2bool},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200560# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 {"Perl_sv_2iv", (PERL_PROC*)&Perl_sv_2iv},
562 {"Perl_sv_2mortal", (PERL_PROC*)&Perl_sv_2mortal},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200563# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 {"Perl_sv_2pv_flags", (PERL_PROC*)&Perl_sv_2pv_flags},
565 {"Perl_sv_2pv_nolen", (PERL_PROC*)&Perl_sv_2pv_nolen},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200566# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 {"Perl_sv_2pv", (PERL_PROC*)&Perl_sv_2pv},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200568# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100569 {"Perl_sv_2pvbyte", (PERL_PROC*)&Perl_sv_2pvbyte},
Bram Moolenaar895a7a42020-09-10 21:36:11 +0200570# if (PERL_REVISION == 5) && (PERL_VERSION >= 32)
571 {"Perl_sv_2pvbyte_flags", (PERL_PROC*)&Perl_sv_2pvbyte_flags},
572# endif
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200573# ifdef PERL589_OR_LATER
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000574 {"Perl_sv_2iv_flags", (PERL_PROC*)&Perl_sv_2iv_flags},
575 {"Perl_newXS_flags", (PERL_PROC*)&Perl_newXS_flags},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200576# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 {"Perl_sv_bless", (PERL_PROC*)&Perl_sv_bless},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200578# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 {"Perl_sv_catpvn_flags", (PERL_PROC*)&Perl_sv_catpvn_flags},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200580# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 {"Perl_sv_catpvn", (PERL_PROC*)&Perl_sv_catpvn},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200582# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 {"Perl_sv_free", (PERL_PROC*)&Perl_sv_free},
584 {"Perl_sv_isa", (PERL_PROC*)&Perl_sv_isa},
585 {"Perl_sv_magic", (PERL_PROC*)&Perl_sv_magic},
586 {"Perl_sv_setiv", (PERL_PROC*)&Perl_sv_setiv},
587 {"Perl_sv_setpv", (PERL_PROC*)&Perl_sv_setpv},
588 {"Perl_sv_setpvn", (PERL_PROC*)&Perl_sv_setpvn},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200589# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 {"Perl_sv_setsv_flags", (PERL_PROC*)&Perl_sv_setsv_flags},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200591# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 {"Perl_sv_setsv", (PERL_PROC*)&Perl_sv_setsv},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200593# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 {"Perl_sv_upgrade", (PERL_PROC*)&Perl_sv_upgrade},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200595# if (PERL_REVISION == 5) && (PERL_VERSION < 10)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 {"Perl_Tstack_sp_ptr", (PERL_PROC*)&Perl_Tstack_sp_ptr},
597 {"Perl_Top_ptr", (PERL_PROC*)&Perl_Top_ptr},
598 {"Perl_Tstack_base_ptr", (PERL_PROC*)&Perl_Tstack_base_ptr},
599 {"Perl_Tstack_max_ptr", (PERL_PROC*)&Perl_Tstack_max_ptr},
600 {"Perl_Ttmps_ix_ptr", (PERL_PROC*)&Perl_Ttmps_ix_ptr},
601 {"Perl_Ttmps_floor_ptr", (PERL_PROC*)&Perl_Ttmps_floor_ptr},
602 {"Perl_Tmarkstack_ptr_ptr", (PERL_PROC*)&Perl_Tmarkstack_ptr_ptr},
603 {"Perl_Tmarkstack_max_ptr", (PERL_PROC*)&Perl_Tmarkstack_max_ptr},
604 {"Perl_TSv_ptr", (PERL_PROC*)&Perl_TSv_ptr},
605 {"Perl_TXpv_ptr", (PERL_PROC*)&Perl_TXpv_ptr},
606 {"Perl_Tna_ptr", (PERL_PROC*)&Perl_Tna_ptr},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200607# else
Bram Moolenaarccf22172008-09-01 15:56:45 +0000608 {"Perl_sv_free2", (PERL_PROC*)&Perl_sv_free2},
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000609 {"Perl_sys_init", (PERL_PROC*)&Perl_sys_init},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000610 {"Perl_sys_term", (PERL_PROC*)&Perl_sys_term},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200611 {"Perl_call_list", (PERL_PROC*)&Perl_call_list},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200612# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
613# else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000614 {"Perl_ISv_ptr", (PERL_PROC*)&Perl_ISv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000615 {"Perl_Istack_max_ptr", (PERL_PROC*)&Perl_Istack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200616 {"Perl_Istack_base_ptr", (PERL_PROC*)&Perl_Istack_base_ptr},
617 {"Perl_IXpv_ptr", (PERL_PROC*)&Perl_IXpv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000618 {"Perl_Itmps_ix_ptr", (PERL_PROC*)&Perl_Itmps_ix_ptr},
619 {"Perl_Itmps_floor_ptr", (PERL_PROC*)&Perl_Itmps_floor_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200620 {"Perl_Ina_ptr", (PERL_PROC*)&Perl_Ina_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000621 {"Perl_Imarkstack_ptr_ptr", (PERL_PROC*)&Perl_Imarkstack_ptr_ptr},
622 {"Perl_Imarkstack_max_ptr", (PERL_PROC*)&Perl_Imarkstack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200623 {"Perl_Istack_sp_ptr", (PERL_PROC*)&Perl_Istack_sp_ptr},
624 {"Perl_Iop_ptr", (PERL_PROC*)&Perl_Iop_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000625 {"Perl_Iscopestack_ix_ptr", (PERL_PROC*)&Perl_Iscopestack_ix_ptr},
626 {"Perl_Iunitcheckav_ptr", (PERL_PROC*)&Perl_Iunitcheckav_ptr},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200627# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200628# endif
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200629# if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200630 {"Perl_xs_handshake", (PERL_PROC*)&Perl_xs_handshake},
631 {"Perl_xs_boot_epilog", (PERL_PROC*)&Perl_xs_boot_epilog},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200632# endif
633# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100634# ifdef USE_ITHREADS
Bram Moolenaar01c10522012-09-21 12:50:51 +0200635 {"PL_thr_key", (PERL_PROC*)&dll_PL_thr_key},
Bram Moolenaard8619992014-03-12 17:08:05 +0100636# endif
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200637# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 {"Perl_Idefgv_ptr", (PERL_PROC*)&Perl_Idefgv_ptr},
639 {"Perl_Ierrgv_ptr", (PERL_PROC*)&Perl_Ierrgv_ptr},
640 {"Perl_Isv_yes_ptr", (PERL_PROC*)&Perl_Isv_yes_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200641 {"Perl_Gthr_key_ptr", (PERL_PROC*)&Perl_Gthr_key_ptr},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200642# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200643 {"boot_DynaLoader", (PERL_PROC*)&boot_DynaLoader},
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100644 {"Perl_hv_iternext_flags", (PERL_PROC*)&Perl_hv_iternext_flags},
645 {"Perl_hv_iterinit", (PERL_PROC*)&Perl_hv_iterinit},
646 {"Perl_hv_iterkey", (PERL_PROC*)&Perl_hv_iterkey},
647 {"Perl_hv_iterval", (PERL_PROC*)&Perl_hv_iterval},
648 {"Perl_av_fetch", (PERL_PROC*)&Perl_av_fetch},
649 {"Perl_av_len", (PERL_PROC*)&Perl_av_len},
650 {"Perl_sv_2nv_flags", (PERL_PROC*)&Perl_sv_2nv_flags},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200651# if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200652 {"PerlIOBase_pushed", (PERL_PROC*)&PerlIOBase_pushed},
653 {"PerlIO_define_layer", (PERL_PROC*)&PerlIO_define_layer},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200654# endif
655# if (PERL_REVISION == 5) && (PERL_VERSION >= 24)
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200656 {"Perl_savetmps", (PERL_PROC*)&Perl_savetmps},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200657# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 {"", NULL},
659};
660
Bram Moolenaar6b107212013-12-11 15:06:40 +0100661/* Work around for perl-5.18.
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200662 * For now, only the definitions of S_SvREFCNT_dec are needed in
663 * "perl\lib\CORE\inline.h". */
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200664# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200665static void
666S_SvREFCNT_dec(pTHX_ SV *sv)
667{
668 if (LIKELY(sv != NULL)) {
669 U32 rc = SvREFCNT(sv);
670 if (LIKELY(rc > 1))
671 SvREFCNT(sv) = rc - 1;
672 else
673 Perl_sv_free2(aTHX_ sv, rc);
674 }
675}
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200676# endif
Bram Moolenaar6b107212013-12-11 15:06:40 +0100677
Bram Moolenaarf5433fb2020-06-21 20:06:54 +0200678/* perl-5.32 needs Perl_SvREFCNT_dec */
679# if (PERL_REVISION == 5) && (PERL_VERSION >= 32)
680# define Perl_SvREFCNT_dec S_SvREFCNT_dec
681# endif
682
Bram Moolenaarfa4161c2017-06-10 15:46:23 +0200683/* perl-5.26 also needs S_TOPMARK and S_POPMARK. */
684# if (PERL_REVISION == 5) && (PERL_VERSION >= 26)
685PERL_STATIC_INLINE I32
686S_TOPMARK(pTHX)
687{
688 DEBUG_s(DEBUG_v(PerlIO_printf(Perl_debug_log,
689 "MARK top %p %" IVdf "\n",
690 PL_markstack_ptr,
691 (IV)*PL_markstack_ptr)));
692 return *PL_markstack_ptr;
693}
694
695PERL_STATIC_INLINE I32
696S_POPMARK(pTHX)
697{
698 DEBUG_s(DEBUG_v(PerlIO_printf(Perl_debug_log,
699 "MARK pop %p %" IVdf "\n",
700 (PL_markstack_ptr-1),
701 (IV)*(PL_markstack_ptr-1))));
702 assert((PL_markstack_ptr > PL_markstack) || !"MARK underflow");
703 return *PL_markstack_ptr--;
704}
705# endif
706
Bram Moolenaarf5433fb2020-06-21 20:06:54 +0200707/* perl-5.32 needs Perl_POPMARK */
708# if (PERL_REVISION == 5) && (PERL_VERSION >= 32)
709# define Perl_POPMARK S_POPMARK
ichizok54346712021-05-27 18:05:14 +0200710# endif
711
712/* perl-5.34 needs Perl_SvTRUE_common; used in SvTRUE_nomg_NN */
Philip H1d7caa52023-06-22 08:55:47 +0200713# if (PERL_REVISION == 5) && (PERL_VERSION == 34)
ichizok54346712021-05-27 18:05:14 +0200714PERL_STATIC_INLINE bool
715Perl_SvTRUE_common(pTHX_ SV * sv, const bool sv_2bool_is_fallback)
716{
717 if (UNLIKELY(SvIMMORTAL_INTERP(sv)))
718 return SvIMMORTAL_TRUE(sv);
719
720 if (! SvOK(sv))
721 return FALSE;
722
723 if (SvPOK(sv))
724 return SvPVXtrue(sv);
725
726 if (SvIOK(sv))
727 return SvIVX(sv) != 0; /* casts to bool */
728
729 if (SvROK(sv) && !(SvOBJECT(SvRV(sv)) && HvAMAGIC(SvSTASH(SvRV(sv)))))
730 return TRUE;
731
732 if (sv_2bool_is_fallback)
733 return sv_2bool_nomg(sv);
734
735 return isGV_with_GP(sv);
736}
737# endif
Bram Moolenaarf5433fb2020-06-21 20:06:54 +0200738
739/* perl-5.32 needs Perl_SvTRUE */
Philip H1d7caa52023-06-22 08:55:47 +0200740# if (PERL_REVISION == 5) && (PERL_VERSION == 32)
Bram Moolenaarf5433fb2020-06-21 20:06:54 +0200741PERL_STATIC_INLINE bool
742Perl_SvTRUE(pTHX_ SV *sv) {
743 if (!LIKELY(sv))
ichizok54346712021-05-27 18:05:14 +0200744 return FALSE;
Bram Moolenaarf5433fb2020-06-21 20:06:54 +0200745 SvGETMAGIC(sv);
746 return SvTRUE_nomg_NN(sv);
747}
748# endif
749
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750/*
751 * Make all runtime-links of perl.
752 *
Bram Moolenaar364ab2f2013-08-02 20:05:32 +0200753 * 1. Get module handle using dlopen() or vimLoadLib().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 * 2. Get pointer to perl function by GetProcAddress.
755 * 3. Repeat 2, until get all functions will be used.
756 *
757 * Parameter 'libname' provides name of DLL.
758 * Return OK or FAIL.
759 */
760 static int
761perl_runtime_link_init(char *libname, int verbose)
762{
763 int i;
764
765 if (hPerlLib != NULL)
766 return OK;
Bram Moolenaare06c1882010-07-21 22:05:20 +0200767 if ((hPerlLib = load_dll(libname)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 {
769 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100770 semsg(_("E370: Could not load library %s"), libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 return FAIL;
772 }
773 for (i = 0; perl_funcname_table[i].ptr; ++i)
774 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200775 if (!(*perl_funcname_table[i].ptr = symbol_from_dll(hPerlLib,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 perl_funcname_table[i].name)))
777 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200778 close_dll(hPerlLib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 hPerlLib = NULL;
780 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000781 semsg((const char *)_(e_could_not_load_library_function_str), perl_funcname_table[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 return FAIL;
783 }
784 }
785 return OK;
786}
787
788/*
789 * If runtime-link-perl(DLL) was loaded successfully, return TRUE.
790 * There were no DLL loaded, return FALSE.
791 */
792 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100793perl_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100795 return perl_runtime_link_init((char *)p_perldll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796}
797#endif /* DYNAMIC_PERL */
798
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200799#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
800static void vim_IOLayer_init(void);
801#endif
802
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803/*
804 * perl_init(): initialize perl interpreter
805 * We have to call perl_parse to initialize some structures,
806 * there's nothing to actually parse.
807 */
808 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100809perl_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810{
Bram Moolenaarc236c162008-07-13 17:41:49 +0000811 char *bootargs[] = { "VI", NULL };
812 int argc = 3;
813 static char *argv[] = { "", "-e", "" };
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814
Bram Moolenaarc236c162008-07-13 17:41:49 +0000815#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000816 Perl_sys_init(&argc, (char***)&argv);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 perl_interp = perl_alloc();
819 perl_construct(perl_interp);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000820 perl_parse(perl_interp, xs_init, argc, argv, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 perl_call_argv("VIM::bootstrap", (long)G_DISCARD, bootargs);
822 VIM_init();
823#ifdef USE_SFIO
824 sfdisc(PerlIO_stdout(), sfdcnewvim());
825 sfdisc(PerlIO_stderr(), sfdcnewvim());
826 sfsetbuf(PerlIO_stdout(), NULL, 0);
827 sfsetbuf(PerlIO_stderr(), NULL, 0);
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200828#elif defined(PERLIO_LAYERS)
829 vim_IOLayer_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830#endif
831}
832
833/*
Bram Moolenaar20279732020-03-29 20:51:07 +0200834 * Clean up after ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 */
836 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100837perl_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838{
839 if (perl_interp)
840 {
841 perl_run(perl_interp);
842 perl_destruct(perl_interp);
843 perl_free(perl_interp);
844 perl_interp = NULL;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000845#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100846 Perl_sys_term();
Bram Moolenaarc236c162008-07-13 17:41:49 +0000847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849}
850
851/*
852 * msg_split(): send a message to the message handling routines
853 * split at '\n' first though.
854 */
855 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100856msg_split(
857 char_u *s,
858 int attr) /* highlighting attributes */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859{
860 char *next;
861 char *token = (char *)s;
862
Bram Moolenaaraa8494a2007-10-09 08:47:27 +0000863 while ((next = strchr(token, '\n')) && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 {
865 *next++ = '\0'; /* replace \n with \0 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100866 msg_attr(token, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 token = next;
868 }
Bram Moolenaaraa8494a2007-10-09 08:47:27 +0000869 if (*token && !got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100870 msg_attr(token, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871}
872
873#ifndef FEAT_EVAL
874/*
875 * This stub is needed because an "#ifdef FEAT_EVAL" around Eval() doesn't
876 * work properly.
877 */
878 char_u *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100879eval_to_string(
880 char_u *arg UNUSED,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100881 int convert UNUSED,
882 int use_simple_function UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883{
884 return NULL;
885}
886#endif
887
888/*
889 * Create a new reference to an SV pointing to the SCR structure
Bram Moolenaare344bea2005-09-01 20:46:49 +0000890 * The b_perl_private/w_perl_private part of the SCR structure points to the
891 * SV, so there can only be one such SV for a particular SCR structure. When
892 * the last reference has gone (DESTROY is called),
893 * b_perl_private/w_perl_private is reset; When the screen goes away before
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 * all references are gone, the value of the SV is reset;
895 * any subsequent use of any of those reference will produce
896 * a warning. (see typemap)
897 */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000898
899 static SV *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100900newWINrv(SV *rv, win_T *ptr)
Bram Moolenaare344bea2005-09-01 20:46:49 +0000901{
902 sv_upgrade(rv, SVt_RV);
903 if (ptr->w_perl_private == NULL)
904 {
905 ptr->w_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +0100906 sv_setiv(ptr->w_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +0000907 }
Bram Moolenaar41c363a2018-08-02 21:46:51 +0200908 SvREFCNT_inc_void_NN(ptr->w_perl_private);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000909 SvRV(rv) = ptr->w_perl_private;
910 SvROK_on(rv);
911 return sv_bless(rv, gv_stashpv("VIWIN", TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912}
913
Bram Moolenaare344bea2005-09-01 20:46:49 +0000914 static SV *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100915newBUFrv(SV *rv, buf_T *ptr)
Bram Moolenaare344bea2005-09-01 20:46:49 +0000916{
917 sv_upgrade(rv, SVt_RV);
918 if (ptr->b_perl_private == NULL)
919 {
920 ptr->b_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +0100921 sv_setiv(ptr->b_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +0000922 }
Bram Moolenaar41c363a2018-08-02 21:46:51 +0200923 SvREFCNT_inc_void_NN(ptr->b_perl_private);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000924 SvRV(rv) = ptr->b_perl_private;
925 SvROK_on(rv);
926 return sv_bless(rv, gv_stashpv("VIBUF", TRUE));
927}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200929#if 0
930SV *__sv_save[1024];
931int __sv_save_ix;
932# define D_Save_Sv(sv) do { if (__sv_save_ix < 1024) __sv_save[__sv_save_ix++] = (sv); } while (0)
933#else
934# define D_Save_Sv(sv) NOOP
935#endif
936
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937/*
938 * perl_win_free
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200939 * Remove all references to the window to be destroyed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 */
941 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100942perl_win_free(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943{
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200944 if (wp->w_perl_private && perl_interp != NULL)
945 {
Bram Moolenaar578333b2018-07-22 07:31:09 +0200946 SV *sv = (SV*)wp->w_perl_private;
947 D_Save_Sv(sv);
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200948 sv_setiv(sv, 0);
949 SvREFCNT_dec(sv);
950 }
951 wp->w_perl_private = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952}
953
954 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100955perl_buf_free(buf_T *bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956{
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200957 if (bp->b_perl_private && perl_interp != NULL)
958 {
Bram Moolenaar578333b2018-07-22 07:31:09 +0200959 SV *sv = (SV *)bp->b_perl_private;
960 D_Save_Sv(sv);
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200961 sv_setiv(sv, 0);
962 SvREFCNT_dec(sv);
963 }
964 bp->b_perl_private = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965}
966
967#ifndef PROTO
968# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
969I32 cur_val(pTHX_ IV iv, SV *sv);
970# else
971I32 cur_val(IV iv, SV *sv);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200972# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973
974/*
975 * Handler for the magic variables $main::curwin and $main::curbuf.
976 * The handler is put into the magic vtbl for these variables.
977 * (This is effectively a C-level equivalent of a tied variable).
978 * There is no "set" function as the variables are read-only.
979 */
980# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
981I32 cur_val(pTHX_ IV iv, SV *sv)
982# else
983I32 cur_val(IV iv, SV *sv)
984# endif
985{
986 SV *rv;
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200987
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 if (iv == 0)
989 rv = newWINrv(newSV(0), curwin);
990 else
991 rv = newBUFrv(newSV(0), curbuf);
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200992
Bram Moolenaar41c363a2018-08-02 21:46:51 +0200993 if (SvRV(sv) != SvRV(rv))
994 // XXX: This magic variable is a bit confusing...
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100995 // Is currently refcounted ?
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200996 sv_setsv(sv, rv);
997
Bram Moolenaar41c363a2018-08-02 21:46:51 +0200998 SvREFCNT_dec(rv);
999
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 return 0;
1001}
1002#endif /* !PROTO */
1003
1004struct ufuncs cw_funcs = { cur_val, 0, 0 };
1005struct ufuncs cb_funcs = { cur_val, 0, 1 };
1006
1007/*
1008 * VIM_init(): Vim-specific initialisation.
1009 * Make the magical main::curwin and main::curbuf variables
1010 */
1011 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001012VIM_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013{
1014 static char cw[] = "main::curwin";
1015 static char cb[] = "main::curbuf";
1016 SV *sv;
1017
1018 sv = perl_get_sv(cw, TRUE);
1019 sv_magic(sv, NULL, 'U', (char *)&cw_funcs, sizeof(cw_funcs));
1020 SvREADONLY_on(sv);
1021
1022 sv = perl_get_sv(cb, TRUE);
1023 sv_magic(sv, NULL, 'U', (char *)&cb_funcs, sizeof(cb_funcs));
1024 SvREADONLY_on(sv);
1025
1026 /*
1027 * Setup the Safe compartment.
1028 * It shouldn't be a fatal error if the Safe module is missing.
1029 * XXX: Only shares the 'Msg' routine (which has to be called
1030 * like 'Msg(...)').
1031 */
1032 (void)perl_eval_pv( "if ( eval( 'require Safe' ) ) { $VIM::safe = Safe->new(); $VIM::safe->share_from( 'VIM', ['Msg'] ); }", G_DISCARD | G_VOID );
1033
1034}
1035
1036#ifdef DYNAMIC_PERL
1037static char *e_noperl = N_("Sorry, this command is disabled: the Perl library could not be loaded.");
1038#endif
1039
1040/*
1041 * ":perl"
1042 */
1043 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001044ex_perl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045{
1046 char *err;
1047 char *script;
1048 STRLEN length;
1049 SV *sv;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +02001050#ifdef HAVE_SANDBOX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 SV *safe;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +02001052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053
1054 script = (char *)script_get(eap, eap->arg);
1055 if (eap->skip)
1056 {
1057 vim_free(script);
1058 return;
1059 }
1060
1061 if (perl_interp == NULL)
1062 {
1063#ifdef DYNAMIC_PERL
1064 if (!perl_enabled(TRUE))
1065 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001066 emsg(_(e_noperl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 vim_free(script);
1068 return;
1069 }
1070#endif
1071 perl_init();
1072 }
1073
1074 {
1075 dSP;
1076 ENTER;
1077 SAVETMPS;
1078
1079 if (script == NULL)
1080 sv = newSVpv((char *)eap->arg, 0);
1081 else
1082 {
1083 sv = newSVpv(script, 0);
1084 vim_free(script);
1085 }
1086
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001087 if (sandbox || secure)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 {
Bram Moolenaara1711622011-07-27 14:15:46 +02001089 safe = perl_get_sv("VIM::safe", FALSE);
Bram Moolenaar3f947ea2009-07-14 14:04:54 +00001090# ifndef MAKE_TEST /* avoid a warning for unreachable code */
Bram Moolenaar954e8c52009-11-11 13:45:33 +00001091 if (safe == NULL || !SvTRUE(safe))
Bram Moolenaare96eea72022-01-28 21:00:51 +00001092 emsg(_(e_perl_evaluation_forbidden_in_sandbox_without_safe_module));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 else
Bram Moolenaar3f947ea2009-07-14 14:04:54 +00001094# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 {
1096 PUSHMARK(SP);
1097 XPUSHs(safe);
1098 XPUSHs(sv);
1099 PUTBACK;
1100 perl_call_method("reval", G_DISCARD);
1101 }
1102 }
1103 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
1105
1106 SvREFCNT_dec(sv);
1107
Bram Moolenaar7b61bf12016-06-26 17:16:51 +02001108 err = SvPV(GvSV(PL_errgv), length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109
1110 FREETMPS;
1111 LEAVE;
1112
1113 if (!length)
1114 return;
1115
1116 msg_split((char_u *)err, highlight_attr[HLF_E]);
1117 return;
1118 }
1119}
1120
1121 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001122replace_line(linenr_T *line, linenr_T *end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123{
1124 char *str;
1125
1126 if (SvOK(GvSV(PL_defgv)))
1127 {
1128 str = SvPV(GvSV(PL_defgv), PL_na);
1129 ml_replace(*line, (char_u *)str, 1);
1130 changed_bytes(*line, 0);
1131 }
1132 else
1133 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001134 ml_delete(*line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 deleted_lines_mark(*line, 1L);
1136 --(*end);
1137 --(*line);
1138 }
1139 return OK;
1140}
1141
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001142static struct ref_map_S {
1143 void *vim_ref;
1144 SV *perl_ref;
1145 struct ref_map_S *next;
1146} *ref_map = NULL;
1147
1148 static void
1149ref_map_free(void)
1150{
1151 struct ref_map_S *tofree;
1152 struct ref_map_S *refs = ref_map;
1153
1154 while (refs) {
1155 tofree = refs;
1156 refs = refs->next;
1157 vim_free(tofree);
1158 }
1159 ref_map = NULL;
1160}
1161
1162 static struct ref_map_S *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001163ref_map_find_SV(SV *const sv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001164{
1165 struct ref_map_S *refs = ref_map;
1166 int count = 350;
1167
1168 while (refs) {
1169 if (refs->perl_ref == sv)
1170 break;
1171 refs = refs->next;
1172 count--;
1173 }
1174
1175 if (!refs && count > 0) {
1176 refs = (struct ref_map_S *)alloc(sizeof(struct ref_map_S));
1177 if (!refs)
1178 return NULL;
1179 refs->perl_ref = sv;
1180 refs->vim_ref = NULL;
1181 refs->next = ref_map;
1182 ref_map = refs;
1183 }
1184
1185 return refs;
1186}
1187
1188 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001189perl_to_vim(SV *sv, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001190{
1191 if (SvROK(sv))
1192 sv = SvRV(sv);
1193
1194 switch (SvTYPE(sv)) {
1195 case SVt_NULL:
1196 break;
1197 case SVt_NV: /* float */
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001198 rettv->v_type = VAR_FLOAT;
1199 rettv->vval.v_float = SvNV(sv);
1200 break;
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001201 case SVt_IV: /* integer */
1202 if (!SvROK(sv)) { /* references should be string */
1203 rettv->vval.v_number = SvIV(sv);
1204 break;
1205 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001206 /* FALLTHROUGH */
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001207 case SVt_PV: /* string */
1208 {
1209 size_t len = 0;
1210 char * str_from = SvPV(sv, len);
Bram Moolenaar9b0ac222016-06-01 20:31:43 +02001211 char_u *str_to = (char_u*)alloc(
1212 (unsigned)(sizeof(char_u) * (len + 1)));
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001213
1214 if (str_to) {
1215 str_to[len] = '\0';
1216
1217 while (len--) {
1218 if (str_from[len] == '\0')
1219 str_to[len] = '\n';
1220 else
1221 str_to[len] = str_from[len];
1222 }
1223 }
1224
1225 rettv->v_type = VAR_STRING;
1226 rettv->vval.v_string = str_to;
1227 break;
1228 }
1229 case SVt_PVAV: /* list */
1230 {
1231 SSize_t size;
1232 listitem_T * item;
1233 SV ** item2;
1234 list_T * list;
1235 struct ref_map_S * refs;
1236
1237 if ((refs = ref_map_find_SV(sv)) == NULL)
1238 return FAIL;
1239
1240 if (refs->vim_ref)
1241 list = (list_T *) refs->vim_ref;
1242 else
1243 {
1244 if ((list = list_alloc()) == NULL)
1245 return FAIL;
1246 refs->vim_ref = list;
1247
1248 for (size = av_len((AV*)sv); size >= 0; size--)
1249 {
1250 if ((item = listitem_alloc()) == NULL)
1251 break;
1252
1253 item->li_tv.v_type = VAR_NUMBER;
1254 item->li_tv.v_lock = 0;
1255 item->li_tv.vval.v_number = 0;
1256 list_insert(list, item, list->lv_first);
1257
1258 item2 = av_fetch((AV *)sv, size, 0);
1259
1260 if (item2 == NULL || *item2 == NULL ||
Bram Moolenaard99df422016-01-29 23:20:40 +01001261 perl_to_vim(*item2, &item->li_tv) == FAIL)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001262 break;
1263 }
1264 }
1265
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001266 rettv_list_set(rettv, list);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001267 break;
1268 }
1269 case SVt_PVHV: /* dictionary */
1270 {
1271 HE * entry;
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001272 I32 key_len;
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001273 char * key;
1274 dictitem_T * item;
1275 SV * item2;
1276 dict_T * dict;
1277 struct ref_map_S * refs;
1278
1279 if ((refs = ref_map_find_SV(sv)) == NULL)
1280 return FAIL;
1281
1282 if (refs->vim_ref)
1283 dict = (dict_T *) refs->vim_ref;
1284 else
1285 {
1286
1287 if ((dict = dict_alloc()) == NULL)
1288 return FAIL;
1289 refs->vim_ref = dict;
1290
1291 hv_iterinit((HV *)sv);
1292
1293 for (entry = hv_iternext((HV *)sv); entry; entry = hv_iternext((HV *)sv))
1294 {
1295 key_len = 0;
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001296 key = hv_iterkey(entry, &key_len);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001297
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001298 if (!key || !key_len || strlen(key) < (size_t)key_len) {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001299 semsg("Malformed key Dictionary '%s'", key && *key ? key : "(empty)");
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001300 break;
1301 }
1302
1303 if ((item = dictitem_alloc((char_u *)key)) == NULL)
1304 break;
Bram Moolenaarc89d4b32018-07-08 17:19:02 +02001305 item->di_tv.v_type = VAR_NUMBER;
1306 item->di_tv.vval.v_number = 0;
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001307
1308 if (dict_add(dict, item) == FAIL) {
1309 dictitem_free(item);
1310 break;
1311 }
1312 item2 = hv_iterval((HV *)sv, entry);
1313 if (item2 == NULL || perl_to_vim(item2, &item->di_tv) == FAIL)
1314 break;
1315 }
1316 }
1317
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001318 rettv_dict_set(rettv, dict);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001319 break;
1320 }
1321 default: /* not convertible */
1322 {
1323 char *val = SvPV_nolen(sv);
1324 rettv->v_type = VAR_STRING;
1325 rettv->vval.v_string = val ? vim_strsave((char_u *)val) : NULL;
1326 break;
1327 }
1328 }
1329 return OK;
1330}
1331
1332/*
1333 * "perleval()"
1334 */
1335 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001336do_perleval(char_u *str, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001337{
1338 char *err = NULL;
1339 STRLEN err_len = 0;
1340 SV *sv = NULL;
1341#ifdef HAVE_SANDBOX
1342 SV *safe;
1343#endif
1344
1345 if (perl_interp == NULL)
1346 {
1347#ifdef DYNAMIC_PERL
1348 if (!perl_enabled(TRUE))
1349 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001350 emsg(_(e_noperl));
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001351 return;
1352 }
1353#endif
1354 perl_init();
1355 }
1356
1357 {
1358 dSP;
1359 ENTER;
1360 SAVETMPS;
1361
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001362 if (sandbox || secure)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001363 {
1364 safe = get_sv("VIM::safe", FALSE);
1365# ifndef MAKE_TEST /* avoid a warning for unreachable code */
1366 if (safe == NULL || !SvTRUE(safe))
Bram Moolenaare96eea72022-01-28 21:00:51 +00001367 emsg(_(e_perl_evaluation_forbidden_in_sandbox_without_safe_module));
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001368 else
1369# endif
1370 {
1371 sv = newSVpv((char *)str, 0);
1372 PUSHMARK(SP);
1373 XPUSHs(safe);
1374 XPUSHs(sv);
1375 PUTBACK;
1376 call_method("reval", G_SCALAR);
1377 SPAGAIN;
1378 SvREFCNT_dec(sv);
1379 sv = POPs;
Bram Moolenaar0f267622022-05-10 13:32:24 +01001380 PUTBACK;
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001381 }
1382 }
1383 else
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001384 sv = eval_pv((char *)str, 0);
1385
1386 if (sv) {
1387 perl_to_vim(sv, rettv);
1388 ref_map_free();
Bram Moolenaar7b61bf12016-06-26 17:16:51 +02001389 err = SvPV(GvSV(PL_errgv), err_len);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001390 }
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001391 FREETMPS;
1392 LEAVE;
1393 }
1394 if (err_len)
1395 msg_split((char_u *)err, highlight_attr[HLF_E]);
1396}
1397
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398/*
1399 * ":perldo".
1400 */
1401 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001402ex_perldo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403{
1404 STRLEN length;
1405 SV *sv;
1406 char *str;
1407 linenr_T i;
Bram Moolenaar85b57432017-01-29 22:59:12 +01001408 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001410 if (BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 return;
1412
1413 if (perl_interp == NULL)
1414 {
1415#ifdef DYNAMIC_PERL
1416 if (!perl_enabled(TRUE))
1417 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001418 emsg(_(e_noperl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 return;
1420 }
1421#endif
1422 perl_init();
1423 }
1424 {
1425 dSP;
1426 length = strlen((char *)eap->arg);
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001427 sv = newSV(length + sizeof("sub VIM::perldo {") - 1 + 1);
1428 sv_setpvn(sv, "sub VIM::perldo {", sizeof("sub VIM::perldo {") - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 sv_catpvn(sv, (char *)eap->arg, length);
1430 sv_catpvn(sv, "}", 1);
1431 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
1432 SvREFCNT_dec(sv);
Bram Moolenaar7b61bf12016-06-26 17:16:51 +02001433 str = SvPV(GvSV(PL_errgv), length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 if (length)
1435 goto err;
1436
1437 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
1438 return;
1439
1440 ENTER;
1441 SAVETMPS;
1442 for (i = eap->line1; i <= eap->line2; i++)
1443 {
Bram Moolenaar85b57432017-01-29 22:59:12 +01001444 /* Check the line number, the command my have deleted lines. */
1445 if (i > curbuf->b_ml.ml_line_count)
1446 break;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001447 sv_setpv(GvSV(PL_defgv), (char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 PUSHMARK(sp);
1449 perl_call_pv("VIM::perldo", G_SCALAR | G_EVAL);
Bram Moolenaar7b61bf12016-06-26 17:16:51 +02001450 str = SvPV(GvSV(PL_errgv), length);
Bram Moolenaar85b57432017-01-29 22:59:12 +01001451 if (length || curbuf != was_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 break;
1453 SPAGAIN;
1454 if (SvTRUEx(POPs))
1455 {
1456 if (replace_line(&i, &eap->line2) != OK)
1457 {
1458 PUTBACK;
1459 break;
1460 }
1461 }
1462 PUTBACK;
1463 }
1464 FREETMPS;
1465 LEAVE;
1466 check_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001467 update_screen(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 if (!length)
1469 return;
1470
1471err:
1472 msg_split((char_u *)str, highlight_attr[HLF_E]);
1473 return;
1474 }
1475}
1476
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001477#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
1478typedef struct {
1479 struct _PerlIO base;
1480 int attr;
1481} PerlIOVim;
1482
1483 static IV
1484PerlIOVim_pushed(pTHX_ PerlIO *f, const char *mode,
1485 SV *arg, PerlIO_funcs *tab)
1486{
1487 PerlIOVim *s = PerlIOSelf(f, PerlIOVim);
1488 s->attr = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001489 if (arg && SvPOK(arg))
1490 s->attr = syn_name2attr((char_u *)SvPV_nolen(arg));
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001491 return PerlIOBase_pushed(aTHX_ f, mode, (SV *)NULL, tab);
1492}
1493
1494 static SSize_t
1495PerlIOVim_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1496{
1497 char_u *str;
1498 PerlIOVim * s = PerlIOSelf(f, PerlIOVim);
1499
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001500 str = vim_strnsave((char_u *)vbuf, count);
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001501 if (str == NULL)
1502 return 0;
1503 msg_split((char_u *)str, s->attr);
1504 vim_free(str);
1505
Bram Moolenaar9b0ac222016-06-01 20:31:43 +02001506 return (SSize_t)count;
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001507}
1508
1509static PERLIO_FUNCS_DECL(PerlIO_Vim) = {
1510 sizeof(PerlIO_funcs),
1511 "Vim",
1512 sizeof(PerlIOVim),
1513 PERLIO_K_DUMMY, /* flags */
1514 PerlIOVim_pushed,
1515 NULL, /* popped */
1516 NULL, /* open */
1517 NULL, /* binmode */
1518 NULL, /* arg */
1519 NULL, /* fileno */
1520 NULL, /* dup */
1521 NULL, /* read */
1522 NULL, /* unread */
1523 PerlIOVim_write,
1524 NULL, /* seek */
1525 NULL, /* tell */
1526 NULL, /* close */
1527 NULL, /* flush */
1528 NULL, /* fill */
1529 NULL, /* eof */
1530 NULL, /* error */
1531 NULL, /* clearerr */
1532 NULL, /* setlinebuf */
1533 NULL, /* get_base */
1534 NULL, /* get_bufsiz */
1535 NULL, /* get_ptr */
1536 NULL, /* get_cnt */
1537 NULL /* set_ptrcnt */
1538};
1539
1540/* Use Vim routine for print operator */
1541 static void
1542vim_IOLayer_init(void)
1543{
1544 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_Vim));
1545 (void)eval_pv( "binmode(STDOUT, ':Vim')"
1546 " && binmode(STDERR, ':Vim(ErrorMsg)');", 0);
1547}
1548#endif /* PERLIO_LAYERS && !USE_SFIO */
1549
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550XS(boot_VIM);
1551
1552 static void
1553xs_init(pTHX)
1554{
1555 char *file = __FILE__;
1556
1557 /* DynaLoader is a special case */
1558 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1559 newXS("VIM::bootstrap", boot_VIM, file);
1560}
1561
1562typedef win_T * VIWIN;
1563typedef buf_T * VIBUF;
1564
1565MODULE = VIM PACKAGE = VIM
1566
1567void
1568Msg(text, hl=NULL)
1569 char *text;
1570 char *hl;
1571
1572 PREINIT:
1573 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574
1575 PPCODE:
1576 if (text != NULL)
1577 {
1578 attr = 0;
1579 if (hl != NULL)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001580 attr = syn_name2attr((char_u *)hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 msg_split((char_u *)text, attr);
1582 }
1583
1584void
1585SetOption(line)
1586 char *line;
1587
1588 PPCODE:
1589 if (line != NULL)
1590 do_set((char_u *)line, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001591 update_screen(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592
1593void
1594DoCommand(line)
1595 char *line;
1596
1597 PPCODE:
1598 if (line != NULL)
1599 do_cmdline_cmd((char_u *)line);
1600
1601void
1602Eval(str)
1603 char *str;
1604
1605 PREINIT:
1606 char_u *value;
1607 PPCODE:
Bram Moolenaara4e0b972022-10-01 19:43:52 +01001608 value = eval_to_string((char_u *)str, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 if (value == NULL)
1610 {
1611 XPUSHs(sv_2mortal(newSViv(0)));
1612 XPUSHs(sv_2mortal(newSVpv("", 0)));
1613 }
1614 else
1615 {
1616 XPUSHs(sv_2mortal(newSViv(1)));
1617 XPUSHs(sv_2mortal(newSVpv((char *)value, 0)));
1618 vim_free(value);
1619 }
1620
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001621SV*
1622Blob(SV* sv)
1623 PREINIT:
Bram Moolenaarb1443b42019-01-13 23:51:14 +01001624 STRLEN len;
1625 char *s;
1626 unsigned i;
1627 char buf[3];
1628 SV* newsv;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001629
1630 CODE:
1631 s = SvPVbyte(sv, len);
1632 newsv = newSVpv("0z", 2);
1633 for (i = 0; i < len; i++)
1634 {
Bram Moolenaar2472ae82019-02-23 15:04:17 +01001635 sprintf(buf, "%02X", (unsigned char)(s[i]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001636 sv_catpvn(newsv, buf, 2);
1637 }
1638 RETVAL = newsv;
1639 OUTPUT:
1640 RETVAL
1641
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642void
1643Buffers(...)
1644
1645 PREINIT:
1646 buf_T *vimbuf;
1647 int i, b;
1648
1649 PPCODE:
1650 if (items == 0)
1651 {
Philip H1d7caa52023-06-22 08:55:47 +02001652 if (GIMME_V == G_SCALAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 {
1654 i = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02001655 FOR_ALL_BUFFERS(vimbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 ++i;
1657
1658 XPUSHs(sv_2mortal(newSViv(i)));
1659 }
1660 else
1661 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001662 FOR_ALL_BUFFERS(vimbuf)
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +02001663 XPUSHs(sv_2mortal(newBUFrv(newSV(0), vimbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 }
1665 }
1666 else
1667 {
1668 for (i = 0; i < items; i++)
1669 {
1670 SV *sv = ST(i);
1671 if (SvIOK(sv))
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001672 b = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 else
1674 {
1675 char_u *pat;
1676 STRLEN len;
1677
1678 pat = (char_u *)SvPV(sv, len);
1679 ++emsg_off;
Bram Moolenaar16896a12018-03-06 12:25:56 +01001680 b = buflist_findpat(pat, pat + len, TRUE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 --emsg_off;
1682 }
1683
1684 if (b >= 0)
1685 {
1686 vimbuf = buflist_findnr(b);
1687 if (vimbuf)
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +02001688 XPUSHs(sv_2mortal(newBUFrv(newSV(0), vimbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 }
1690 }
1691 }
1692
1693void
1694Windows(...)
1695
1696 PREINIT:
1697 win_T *vimwin;
1698 int i, w;
1699
1700 PPCODE:
1701 if (items == 0)
1702 {
Philip H1d7caa52023-06-22 08:55:47 +02001703 if (GIMME_V == G_SCALAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 XPUSHs(sv_2mortal(newSViv(win_count())));
1705 else
1706 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001707 FOR_ALL_WINDOWS(vimwin)
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +02001708 XPUSHs(sv_2mortal(newWINrv(newSV(0), vimwin)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 }
1710 }
1711 else
1712 {
1713 for (i = 0; i < items; i++)
1714 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001715 w = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 vimwin = win_find_nr(w);
1717 if (vimwin)
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +02001718 XPUSHs(sv_2mortal(newWINrv(newSV(0), vimwin)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 }
1720 }
1721
1722MODULE = VIM PACKAGE = VIWIN
1723
1724void
1725DESTROY(win)
1726 VIWIN win
1727
1728 CODE:
1729 if (win_valid(win))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001730 win->w_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731
1732SV *
1733Buffer(win)
1734 VIWIN win
1735
1736 CODE:
1737 if (!win_valid(win))
1738 win = curwin;
1739 RETVAL = newBUFrv(newSV(0), win->w_buffer);
1740 OUTPUT:
1741 RETVAL
1742
1743void
1744SetHeight(win, height)
1745 VIWIN win
1746 int height;
1747
1748 PREINIT:
1749 win_T *savewin;
1750
1751 PPCODE:
1752 if (!win_valid(win))
1753 win = curwin;
1754 savewin = curwin;
1755 curwin = win;
1756 win_setheight(height);
1757 curwin = savewin;
1758
1759void
Bram Moolenaar864733a2016-04-02 14:18:01 +02001760Cursor(win, ...)
1761 VIWIN win
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762
1763 PPCODE:
Bram Moolenaara1711622011-07-27 14:15:46 +02001764 if (items == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 {
1766 EXTEND(sp, 2);
1767 if (!win_valid(win))
1768 win = curwin;
1769 PUSHs(sv_2mortal(newSViv(win->w_cursor.lnum)));
1770 PUSHs(sv_2mortal(newSViv(win->w_cursor.col)));
1771 }
Bram Moolenaara1711622011-07-27 14:15:46 +02001772 else if (items == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 {
1774 int lnum, col;
1775
1776 if (!win_valid(win))
1777 win = curwin;
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001778 lnum = (int) SvIV(ST(1));
1779 col = (int) SvIV(ST(2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 win->w_cursor.lnum = lnum;
1781 win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02001782 win->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 check_cursor(); /* put cursor on an existing line */
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001784 update_screen(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 }
1786
1787MODULE = VIM PACKAGE = VIBUF
1788
1789void
1790DESTROY(vimbuf)
1791 VIBUF vimbuf;
1792
1793 CODE:
1794 if (buf_valid(vimbuf))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001795 vimbuf->b_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796
1797void
1798Name(vimbuf)
1799 VIBUF vimbuf;
1800
1801 PPCODE:
1802 if (!buf_valid(vimbuf))
1803 vimbuf = curbuf;
1804 /* No file name returns an empty string */
1805 if (vimbuf->b_fname == NULL)
1806 XPUSHs(sv_2mortal(newSVpv("", 0)));
1807 else
1808 XPUSHs(sv_2mortal(newSVpv((char *)vimbuf->b_fname, 0)));
1809
1810void
1811Number(vimbuf)
1812 VIBUF vimbuf;
1813
1814 PPCODE:
1815 if (!buf_valid(vimbuf))
1816 vimbuf = curbuf;
1817 XPUSHs(sv_2mortal(newSViv(vimbuf->b_fnum)));
1818
1819void
1820Count(vimbuf)
1821 VIBUF vimbuf;
1822
1823 PPCODE:
1824 if (!buf_valid(vimbuf))
1825 vimbuf = curbuf;
1826 XPUSHs(sv_2mortal(newSViv(vimbuf->b_ml.ml_line_count)));
1827
1828void
1829Get(vimbuf, ...)
1830 VIBUF vimbuf;
1831
1832 PREINIT:
1833 char_u *line;
1834 int i;
1835 long lnum;
1836 PPCODE:
1837 if (buf_valid(vimbuf))
1838 {
1839 for (i = 1; i < items; i++)
1840 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001841 lnum = (long) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1843 {
1844 line = ml_get_buf(vimbuf, lnum, FALSE);
1845 XPUSHs(sv_2mortal(newSVpv((char *)line, 0)));
1846 }
1847 }
1848 }
1849
1850void
1851Set(vimbuf, ...)
1852 VIBUF vimbuf;
1853
1854 PREINIT:
1855 int i;
1856 long lnum;
1857 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 PPCODE:
1859 if (buf_valid(vimbuf))
1860 {
1861 if (items < 3)
1862 croak("Usage: VIBUF::Set(vimbuf, lnum, @lines)");
1863
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001864 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 for(i = 2; i < items; i++, lnum++)
1866 {
1867 line = SvPV(ST(i),PL_na);
1868 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1869 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001870 aco_save_T aco;
1871
Bram Moolenaare76062c2022-11-28 18:51:43 +00001872 /* Set curwin/curbuf for "vimbuf" and save some things. */
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001873 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00001874 if (curbuf == vimbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00001876 /* Only when a window was found. */
1877 if (u_savesub(lnum) == OK)
1878 {
1879 ml_replace(lnum, (char_u *)line, TRUE);
1880 changed_bytes(lnum, 0);
1881 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001882
Bram Moolenaare76062c2022-11-28 18:51:43 +00001883 /* restore curwin/curbuf and a few other things */
1884 aucmd_restbuf(&aco);
1885 /* Careful: autocommands may have made "vimbuf" invalid! */
1886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 }
1888 }
1889 }
1890
1891void
1892Delete(vimbuf, ...)
1893 VIBUF vimbuf;
1894
1895 PREINIT:
1896 long i, lnum = 0, count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 PPCODE:
1898 if (buf_valid(vimbuf))
1899 {
1900 if (items == 2)
1901 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001902 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 count = 1;
1904 }
1905 else if (items == 3)
1906 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001907 lnum = (long) SvIV(ST(1));
1908 count = (long) 1 + SvIV(ST(2)) - lnum;
Bram Moolenaara1711622011-07-27 14:15:46 +02001909 if (count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 count = 1;
Bram Moolenaara1711622011-07-27 14:15:46 +02001911 if (count < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 {
1913 lnum -= count;
1914 count = -count;
1915 }
1916 }
1917 if (items >= 2)
1918 {
1919 for (i = 0; i < count; i++)
1920 {
1921 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1922 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001923 aco_save_T aco;
1924
1925 /* set curwin/curbuf for "vimbuf" and save some things */
1926 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00001927 if (curbuf == vimbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00001929 /* Only when a window was found. */
1930 if (u_savedel(lnum, 1) == OK)
1931 {
1932 ml_delete(lnum);
1933 check_cursor();
1934 deleted_lines_mark(lnum, 1L);
1935 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001936
Bram Moolenaare76062c2022-11-28 18:51:43 +00001937 /* restore curwin/curbuf and a few other things */
1938 aucmd_restbuf(&aco);
1939 /* Careful: autocommands may have made "vimbuf"
1940 * invalid! */
1941 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001942
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001943 update_curbuf(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 }
1945 }
1946 }
1947 }
1948
1949void
1950Append(vimbuf, ...)
1951 VIBUF vimbuf;
1952
1953 PREINIT:
1954 int i;
1955 long lnum;
1956 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 PPCODE:
1958 if (buf_valid(vimbuf))
1959 {
1960 if (items < 3)
1961 croak("Usage: VIBUF::Append(vimbuf, lnum, @lines)");
1962
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001963 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 for (i = 2; i < items; i++, lnum++)
1965 {
1966 line = SvPV(ST(i),PL_na);
1967 if (lnum >= 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1968 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001969 aco_save_T aco;
1970
1971 /* set curwin/curbuf for "vimbuf" and save some things */
1972 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00001973 if (curbuf == vimbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00001975 /* Only when a window for "vimbuf" was found. */
1976 if (u_inssub(lnum + 1) == OK)
1977 {
1978 ml_append(lnum, (char_u *)line, (colnr_T)0, FALSE);
1979 appended_lines_mark(lnum, 1L);
1980 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001981
Bram Moolenaare76062c2022-11-28 18:51:43 +00001982 /* restore curwin/curbuf and a few other things */
1983 aucmd_restbuf(&aco);
1984 /* Careful: autocommands may have made "vimbuf" invalid! */
1985 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001986
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001987 update_curbuf(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 }
1989 }
1990 }
1991
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001992#ifdef __GNUC__
1993# pragma GCC diagnostic pop
1994#endif