blob: 879ee2d646144caf51f52d7884413a200db30aa5 [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 Moolenaar207fd752013-12-14 11:50:35 +010040#ifdef _MSC_VER
Bram Moolenaar02890652020-10-31 13:05:11 +010041// Work around for using MSVC and ActivePerl 5.18.
Bram Moolenaar207fd752013-12-14 11:50:35 +010042# define __inline__ __inline
Bram Moolenaare73b38f2020-01-06 21:22:09 +010043// Work around for using MSVC and Strawberry Perl 5.30.
44# define __builtin_expect(expr, val) (expr)
Bram Moolenaar02890652020-10-31 13:05:11 +010045// Work around for using MSVC and Strawberry Perl 5.32.
46# define NO_THREAD_SAFE_LOCALE
Bram Moolenaar207fd752013-12-14 11:50:35 +010047#endif
48
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010049#ifdef __GNUC__
50# pragma GCC diagnostic push
51# pragma GCC diagnostic ignored "-Wunused-variable"
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010052#endif
53
Bram Moolenaaraee1f4a2013-08-02 16:10:32 +020054#include <EXTERN.h>
55#include <perl.h>
56#include <XSUB.h>
Bram Moolenaar6244a0f2016-04-14 14:09:25 +020057#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
58# include <perliol.h>
59#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000060
Bram Moolenaarcf190c62016-06-02 11:54:06 +020061/* Workaround for perl < 5.8.7 */
62#ifndef PERLIO_FUNCS_DECL
63# ifdef PERLIO_FUNCS_CONST
64# define PERLIO_FUNCS_DECL(funcs) const PerlIO_funcs funcs
65# define PERLIO_FUNCS_CAST(funcs) (PerlIO_funcs*)(funcs)
66# else
67# define PERLIO_FUNCS_DECL(funcs) PerlIO_funcs funcs
68# define PERLIO_FUNCS_CAST(funcs) (funcs)
69# endif
70#endif
Bram Moolenaarc4bc0e62016-06-02 13:54:49 +020071#ifndef SvREFCNT_inc_void_NN
72# define SvREFCNT_inc_void_NN SvREFCNT_inc
73#endif
Bram Moolenaarcf190c62016-06-02 11:54:06 +020074
Bram Moolenaar071d4272004-06-13 20:20:40 +000075/*
76 * Work around clashes between Perl and Vim namespace. proto.h doesn't
77 * include if_perl.pro and perlsfio.pro when IN_PERL_FILE is defined, because
78 * we need the CV typedef. proto.h can't be moved to after including
79 * if_perl.h, because we get all sorts of name clashes then.
80 */
81#ifndef PROTO
Bram Moolenaareeb50ab2016-06-26 17:19:46 +020082# ifndef __MINGW32__
83# include "proto/if_perl.pro"
84# include "proto/if_perlsfio.pro"
85# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000086#endif
87
Bram Moolenaar1f402802018-09-21 14:01:27 +020088// Perl compatibility stuff. This should ensure compatibility with older
89// versions of Perl.
Bram Moolenaar071d4272004-06-13 20:20:40 +000090#ifndef PERL_VERSION
Bram Moolenaareeb50ab2016-06-26 17:19:46 +020091# include <patchlevel.h>
92# define PERL_REVISION 5
93# define PERL_VERSION PATCHLEVEL
94# define PERL_SUBVERSION SUBVERSION
Bram Moolenaar071d4272004-06-13 20:20:40 +000095#endif
96
Bram Moolenaar1f402802018-09-21 14:01:27 +020097
98// Work around for ActivePerl 5.20.3+: Avoid generating (g)vim.lib.
99#if defined(ACTIVEPERL_VERSION) && (ACTIVEPERL_VERSION >= 2003) \
Bram Moolenaar4f974752019-02-17 17:44:42 +0100100 && defined(MSWIN) && defined(USE_DYNAMIC_LOADING)
Bram Moolenaar1f402802018-09-21 14:01:27 +0200101# undef XS_EXTERNAL
102# define XS_EXTERNAL(name) XSPROTO(name)
103#endif
104
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000105/*
106 * Quoting Jan Dubois of Active State:
107 * ActivePerl build 822 still identifies itself as 5.8.8 but already
108 * contains many of the changes from the upcoming Perl 5.8.9 release.
109 *
110 * The changes include addition of two symbols (Perl_sv_2iv_flags,
111 * Perl_newXS_flags) not present in earlier releases.
112 *
Bram Moolenaar3b9b13e2007-09-15 12:49:35 +0000113 * Jan Dubois suggested the following guarding scheme.
114 *
115 * Active State defined ACTIVEPERL_VERSION as a string in versions before
116 * 5.8.8; and so the comparison to 822 below needs to be guarded.
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000117 */
Bram Moolenaar3b9b13e2007-09-15 12:49:35 +0000118#if (PERL_REVISION == 5) && (PERL_VERSION == 8) && (PERL_SUBVERSION >= 8)
119# if (ACTIVEPERL_VERSION >= 822) || (PERL_SUBVERSION >= 9)
120# define PERL589_OR_LATER
121# endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000122#endif
123#if (PERL_REVISION == 5) && (PERL_VERSION >= 9)
124# define PERL589_OR_LATER
125#endif
126
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100127#if (PERL_REVISION == 5) && ((PERL_VERSION > 10) || \
128 (PERL_VERSION == 10) && (PERL_SUBVERSION >= 1))
129# define PERL5101_OR_LATER
130#endif
131
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#ifndef pTHX
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200133# define pTHX void
134# define pTHX_
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#endif
136
137#ifndef EXTERN_C
138# define EXTERN_C
139#endif
140
Philip H53752052022-11-04 22:32:21 +0000141// Suppress Infinite warnings when compiling XS modules under macOS 12 Monterey.
142#if defined(__clang__) && defined(__clang_major__) && __clang_major__ > 11
K.Takata161b6ac2022-11-14 15:31:07 +0000143# pragma clang diagnostic ignored "-Wcompound-token-split-by-macro"
Philip H53752052022-11-04 22:32:21 +0000144#endif
145
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146/* Compatibility hacks over */
147
148static PerlInterpreter *perl_interp = NULL;
Bram Moolenaard99df422016-01-29 23:20:40 +0100149static void xs_init(pTHX);
150static void VIM_init(void);
151EXTERN_C void boot_DynaLoader(pTHX_ CV*);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
153/*
Bram Moolenaare06c1882010-07-21 22:05:20 +0200154 * For dynamic linked perl.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 */
156#if defined(DYNAMIC_PERL) || defined(PROTO)
Bram Moolenaare06c1882010-07-21 22:05:20 +0200157
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200158# ifndef DYNAMIC_PERL /* just generating prototypes */
Bram Moolenaar4f974752019-02-17 17:44:42 +0100159# ifdef MSWIN
Bram Moolenaare06c1882010-07-21 22:05:20 +0200160typedef int HANDLE;
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200161# endif
Bram Moolenaare06c1882010-07-21 22:05:20 +0200162typedef int XSINIT_t;
163typedef int XSUBADDR_t;
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200164# endif
165# ifndef USE_ITHREADS
Bram Moolenaare06c1882010-07-21 22:05:20 +0200166typedef int perl_key;
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200167# endif
Bram Moolenaare06c1882010-07-21 22:05:20 +0200168
Bram Moolenaar4f974752019-02-17 17:44:42 +0100169# ifndef MSWIN
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200170# include <dlfcn.h>
171# define HANDLE void*
172# define PERL_PROC void*
173# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
174# define symbol_from_dll dlsym
175# define close_dll dlclose
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200176# define load_dll_error dlerror
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200177# else
178# define PERL_PROC FARPROC
179# define load_dll vimLoadLib
180# define symbol_from_dll GetProcAddress
181# define close_dll FreeLibrary
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200182# define load_dll_error GetWin32Error
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200183# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184/*
185 * Wrapper defines
186 */
187# define perl_alloc dll_perl_alloc
188# define perl_construct dll_perl_construct
189# define perl_parse dll_perl_parse
190# define perl_run dll_perl_run
191# define perl_destruct dll_perl_destruct
192# define perl_free dll_perl_free
Christian Brabandt55460da2023-08-29 21:31:28 +0200193# if defined(WIN32) || ((PERL_REVISION == 5) && (PERL_VERSION < 38))
194# define Perl_get_context dll_Perl_get_context
195# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196# define Perl_croak dll_Perl_croak
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100197# ifdef PERL5101_OR_LATER
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100198# define Perl_croak_xs_usage dll_Perl_croak_xs_usage
199# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200# ifndef PROTO
Bram Moolenaar81ea1df2020-04-11 18:01:41 +0200201# ifdef PERL_IMPLICIT_CONTEXT
202# define Perl_croak_nocontext dll_Perl_croak_nocontext
203# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204# define Perl_call_argv dll_Perl_call_argv
205# define Perl_call_pv dll_Perl_call_pv
206# define Perl_eval_sv dll_Perl_eval_sv
207# define Perl_get_sv dll_Perl_get_sv
208# define Perl_eval_pv dll_Perl_eval_pv
209# define Perl_call_method dll_Perl_call_method
210# endif
211# define Perl_dowantarray dll_Perl_dowantarray
212# define Perl_free_tmps dll_Perl_free_tmps
213# define Perl_gv_stashpv dll_Perl_gv_stashpv
214# define Perl_markstack_grow dll_Perl_markstack_grow
215# define Perl_mg_find dll_Perl_mg_find
Bram Moolenaar578333b2018-07-22 07:31:09 +0200216# if (PERL_REVISION == 5) && (PERL_VERSION >= 28)
217# define Perl_mg_get dll_Perl_mg_get
218# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219# define Perl_newXS dll_Perl_newXS
220# define Perl_newSV dll_Perl_newSV
221# define Perl_newSViv dll_Perl_newSViv
222# define Perl_newSVpv dll_Perl_newSVpv
223# define Perl_pop_scope dll_Perl_pop_scope
224# define Perl_push_scope dll_Perl_push_scope
225# define Perl_save_int dll_Perl_save_int
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200226# if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
227# define Perl_save_strlen dll_Perl_save_strlen
228# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229# define Perl_stack_grow dll_Perl_stack_grow
230# define Perl_set_context dll_Perl_set_context
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200231# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200232# define Perl_sv_2bool_flags dll_Perl_sv_2bool_flags
233# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
234# define Perl_xs_apiversion_bootcheck dll_Perl_xs_apiversion_bootcheck
235# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200236# else
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200237# define Perl_sv_2bool dll_Perl_sv_2bool
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200238# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239# define Perl_sv_2iv dll_Perl_sv_2iv
240# define Perl_sv_2mortal dll_Perl_sv_2mortal
241# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
242# define Perl_sv_2pv_flags dll_Perl_sv_2pv_flags
243# define Perl_sv_2pv_nolen dll_Perl_sv_2pv_nolen
244# else
245# define Perl_sv_2pv dll_Perl_sv_2pv
246# endif
Bram Moolenaar895a7a42020-09-10 21:36:11 +0200247# if (PERL_REVISION == 5) && (PERL_VERSION >= 32)
248# define Perl_sv_2pvbyte_flags dll_Perl_sv_2pvbyte_flags
249# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100250# define Perl_sv_2pvbyte dll_Perl_sv_2pvbyte
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251# define Perl_sv_bless dll_Perl_sv_bless
252# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
253# define Perl_sv_catpvn_flags dll_Perl_sv_catpvn_flags
254# else
255# define Perl_sv_catpvn dll_Perl_sv_catpvn
256# endif
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200257# ifdef PERL589_OR_LATER
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000258# define Perl_sv_2iv_flags dll_Perl_sv_2iv_flags
259# define Perl_newXS_flags dll_Perl_newXS_flags
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200260# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261# define Perl_sv_free dll_Perl_sv_free
Bram Moolenaarccf22172008-09-01 15:56:45 +0000262# if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
263# define Perl_sv_free2 dll_Perl_sv_free2
264# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265# define Perl_sv_isa dll_Perl_sv_isa
266# define Perl_sv_magic dll_Perl_sv_magic
267# define Perl_sv_setiv dll_Perl_sv_setiv
268# define Perl_sv_setpv dll_Perl_sv_setpv
269# define Perl_sv_setpvn dll_Perl_sv_setpvn
270# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
271# define Perl_sv_setsv_flags dll_Perl_sv_setsv_flags
272# else
273# define Perl_sv_setsv dll_Perl_sv_setsv
274# endif
275# define Perl_sv_upgrade dll_Perl_sv_upgrade
276# define Perl_Tstack_sp_ptr dll_Perl_Tstack_sp_ptr
277# define Perl_Top_ptr dll_Perl_Top_ptr
278# define Perl_Tstack_base_ptr dll_Perl_Tstack_base_ptr
279# define Perl_Tstack_max_ptr dll_Perl_Tstack_max_ptr
280# define Perl_Ttmps_ix_ptr dll_Perl_Ttmps_ix_ptr
281# define Perl_Ttmps_floor_ptr dll_Perl_Ttmps_floor_ptr
282# define Perl_Tmarkstack_ptr_ptr dll_Perl_Tmarkstack_ptr_ptr
283# define Perl_Tmarkstack_max_ptr dll_Perl_Tmarkstack_max_ptr
284# define Perl_TSv_ptr dll_Perl_TSv_ptr
285# define Perl_TXpv_ptr dll_Perl_TXpv_ptr
286# define Perl_Tna_ptr dll_Perl_Tna_ptr
287# define Perl_Idefgv_ptr dll_Perl_Idefgv_ptr
288# define Perl_Ierrgv_ptr dll_Perl_Ierrgv_ptr
289# define Perl_Isv_yes_ptr dll_Perl_Isv_yes_ptr
290# define boot_DynaLoader dll_boot_DynaLoader
Bram Moolenaare06c1882010-07-21 22:05:20 +0200291# define Perl_Gthr_key_ptr dll_Perl_Gthr_key_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000293# define Perl_sys_init dll_Perl_sys_init
Bram Moolenaarc236c162008-07-13 17:41:49 +0000294# define Perl_sys_term dll_Perl_sys_term
295# define Perl_ISv_ptr dll_Perl_ISv_ptr
296# define Perl_Istack_max_ptr dll_Perl_Istack_max_ptr
297# define Perl_Istack_base_ptr dll_Perl_Istack_base_ptr
298# define Perl_Itmps_ix_ptr dll_Perl_Itmps_ix_ptr
299# define Perl_Itmps_floor_ptr dll_Perl_Itmps_floor_ptr
300# define Perl_IXpv_ptr dll_Perl_IXpv_ptr
301# define Perl_Ina_ptr dll_Perl_Ina_ptr
302# define Perl_Imarkstack_ptr_ptr dll_Perl_Imarkstack_ptr_ptr
303# define Perl_Imarkstack_max_ptr dll_Perl_Imarkstack_max_ptr
304# define Perl_Istack_sp_ptr dll_Perl_Istack_sp_ptr
305# define Perl_Iop_ptr dll_Perl_Iop_ptr
306# define Perl_call_list dll_Perl_call_list
307# define Perl_Iscopestack_ix_ptr dll_Perl_Iscopestack_ix_ptr
308# define Perl_Iunitcheckav_ptr dll_Perl_Iunitcheckav_ptr
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200309# if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
310# define Perl_xs_handshake dll_Perl_xs_handshake
311# define Perl_xs_boot_epilog dll_Perl_xs_boot_epilog
312# endif
Bram Moolenaar01c10522012-09-21 12:50:51 +0200313# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100314# ifdef USE_ITHREADS
315# define PL_thr_key *dll_PL_thr_key
316# endif
Bram Moolenaar01c10522012-09-21 12:50:51 +0200317# endif
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100318# define Perl_hv_iternext_flags dll_Perl_hv_iternext_flags
319# define Perl_hv_iterinit dll_Perl_hv_iterinit
320# define Perl_hv_iterkey dll_Perl_hv_iterkey
321# define Perl_hv_iterval dll_Perl_hv_iterval
322# define Perl_av_fetch dll_Perl_av_fetch
323# define Perl_av_len dll_Perl_av_len
324# define Perl_sv_2nv_flags dll_Perl_sv_2nv_flags
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200325# if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
326# define PerlIOBase_pushed dll_PerlIOBase_pushed
327# define PerlIO_define_layer dll_PerlIO_define_layer
328# endif
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200329# if (PERL_REVISION == 5) && (PERL_VERSION >= 24)
330# define Perl_savetmps dll_Perl_savetmps
331# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000332
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333/*
334 * Declare HANDLE for perl.dll and function pointers.
335 */
336static HANDLE hPerlLib = NULL;
337
338static PerlInterpreter* (*perl_alloc)();
339static void (*perl_construct)(PerlInterpreter*);
340static void (*perl_destruct)(PerlInterpreter*);
341static void (*perl_free)(PerlInterpreter*);
342static int (*perl_run)(PerlInterpreter*);
343static int (*perl_parse)(PerlInterpreter*, XSINIT_t, int, char**, char**);
Christian Brabandt55460da2023-08-29 21:31:28 +0200344# if defined(WIN32) || ((PERL_REVISION == 5) && (PERL_VERSION < 38))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345static void* (*Perl_get_context)(void);
Christian Brabandt55460da2023-08-29 21:31:28 +0200346# endif
Bram Moolenaar864733a2016-04-02 14:18:01 +0200347static void (*Perl_croak)(pTHX_ const char*, ...) __attribute__noreturn__;
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200348# ifdef PERL5101_OR_LATER
Bram Moolenaar6b107212013-12-11 15:06:40 +0100349/* Perl-5.18 has a different Perl_croak_xs_usage signature. */
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200350# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
Bram Moolenaar864733a2016-04-02 14:18:01 +0200351static void (*Perl_croak_xs_usage)(const CV *const, const char *const params)
352 __attribute__noreturn__;
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200353# else
Bram Moolenaar864733a2016-04-02 14:18:01 +0200354static void (*Perl_croak_xs_usage)(pTHX_ const CV *const, const char *const params)
355 __attribute__noreturn__;
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200356# endif
Bram Moolenaar6b107212013-12-11 15:06:40 +0100357# endif
Bram Moolenaar81ea1df2020-04-11 18:01:41 +0200358# ifdef PERL_IMPLICIT_CONTEXT
Bram Moolenaar864733a2016-04-02 14:18:01 +0200359static void (*Perl_croak_nocontext)(const char*, ...) __attribute__noreturn__;
Bram Moolenaar81ea1df2020-04-11 18:01:41 +0200360# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361static I32 (*Perl_dowantarray)(pTHX);
362static void (*Perl_free_tmps)(pTHX);
363static HV* (*Perl_gv_stashpv)(pTHX_ const char*, I32);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200364# if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200365static I32* (*Perl_markstack_grow)(pTHX);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200366# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367static void (*Perl_markstack_grow)(pTHX);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200368# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369static MAGIC* (*Perl_mg_find)(pTHX_ SV*, int);
Bram Moolenaar578333b2018-07-22 07:31:09 +0200370# if (PERL_REVISION == 5) && (PERL_VERSION >= 28)
371static int (*Perl_mg_get)(pTHX_ SV*);
372# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373static CV* (*Perl_newXS)(pTHX_ char*, XSUBADDR_t, char*);
374static SV* (*Perl_newSV)(pTHX_ STRLEN);
375static SV* (*Perl_newSViv)(pTHX_ IV);
376static SV* (*Perl_newSVpv)(pTHX_ const char*, STRLEN);
377static I32 (*Perl_call_argv)(pTHX_ const char*, I32, char**);
378static I32 (*Perl_call_pv)(pTHX_ const char*, I32);
379static I32 (*Perl_eval_sv)(pTHX_ SV*, I32);
380static SV* (*Perl_get_sv)(pTHX_ const char*, I32);
381static SV* (*Perl_eval_pv)(pTHX_ const char*, I32);
382static SV* (*Perl_call_method)(pTHX_ const char*, I32);
383static void (*Perl_pop_scope)(pTHX);
384static void (*Perl_push_scope)(pTHX);
385static void (*Perl_save_int)(pTHX_ int*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200386# if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200387static void (*Perl_save_strlen)(pTHX_ STRLEN* ptr);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200388# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389static SV** (*Perl_stack_grow)(pTHX_ SV**, SV**p, int);
390static SV** (*Perl_set_context)(void*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200391# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200392static bool (*Perl_sv_2bool_flags)(pTHX_ SV*, I32);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200393# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200394static void (*Perl_xs_apiversion_bootcheck)(pTHX_ SV *module, const char *api_p, STRLEN api_len);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200395# endif
396# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397static bool (*Perl_sv_2bool)(pTHX_ SV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200398# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399static IV (*Perl_sv_2iv)(pTHX_ SV*);
400static SV* (*Perl_sv_2mortal)(pTHX_ SV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200401# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
Philip H1d7caa52023-06-22 08:55:47 +0200402static char* (*Perl_sv_2pv_flags)(pTHX_ SV*, STRLEN* const, const U32);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403static char* (*Perl_sv_2pv_nolen)(pTHX_ SV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200404# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405static char* (*Perl_sv_2pv)(pTHX_ SV*, STRLEN*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200406# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100407static char* (*Perl_sv_2pvbyte)(pTHX_ SV*, STRLEN*);
Bram Moolenaar895a7a42020-09-10 21:36:11 +0200408# if (PERL_REVISION == 5) && (PERL_VERSION >= 32)
Philip H1d7caa52023-06-22 08:55:47 +0200409static char* (*Perl_sv_2pvbyte_flags)(pTHX_ SV*, STRLEN* const, const U32);
Bram Moolenaar895a7a42020-09-10 21:36:11 +0200410# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411static SV* (*Perl_sv_bless)(pTHX_ SV*, HV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200412# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413static void (*Perl_sv_catpvn_flags)(pTHX_ SV* , const char*, STRLEN, I32);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200414# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415static void (*Perl_sv_catpvn)(pTHX_ SV*, const char*, STRLEN);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200416# endif
417# ifdef PERL589_OR_LATER
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000418static IV (*Perl_sv_2iv_flags)(pTHX_ SV* sv, I32 flags);
419static 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 +0200420# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421static void (*Perl_sv_free)(pTHX_ SV*);
422static int (*Perl_sv_isa)(pTHX_ SV*, const char*);
423static void (*Perl_sv_magic)(pTHX_ SV*, SV*, int, const char*, I32);
424static void (*Perl_sv_setiv)(pTHX_ SV*, IV);
425static void (*Perl_sv_setpv)(pTHX_ SV*, const char*);
426static void (*Perl_sv_setpvn)(pTHX_ SV*, const char*, STRLEN);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200427# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428static void (*Perl_sv_setsv_flags)(pTHX_ SV*, SV*, I32);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200429# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430static void (*Perl_sv_setsv)(pTHX_ SV*, SV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200431# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432static bool (*Perl_sv_upgrade)(pTHX_ SV*, U32);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200433# if (PERL_REVISION == 5) && (PERL_VERSION < 10)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434static SV*** (*Perl_Tstack_sp_ptr)(register PerlInterpreter*);
435static OP** (*Perl_Top_ptr)(register PerlInterpreter*);
436static SV*** (*Perl_Tstack_base_ptr)(register PerlInterpreter*);
437static SV*** (*Perl_Tstack_max_ptr)(register PerlInterpreter*);
438static I32* (*Perl_Ttmps_ix_ptr)(register PerlInterpreter*);
439static I32* (*Perl_Ttmps_floor_ptr)(register PerlInterpreter*);
440static I32** (*Perl_Tmarkstack_ptr_ptr)(register PerlInterpreter*);
441static I32** (*Perl_Tmarkstack_max_ptr)(register PerlInterpreter*);
442static SV** (*Perl_TSv_ptr)(register PerlInterpreter*);
443static XPV** (*Perl_TXpv_ptr)(register PerlInterpreter*);
444static STRLEN* (*Perl_Tna_ptr)(register PerlInterpreter*);
Bram Moolenaar6b107212013-12-11 15:06:40 +0100445# else
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200446/* Perl-5.18 has a different Perl_sv_free2 signature. */
447# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
448static void (*Perl_sv_free2)(pTHX_ SV*, const U32);
449# else
Bram Moolenaarccf22172008-09-01 15:56:45 +0000450static void (*Perl_sv_free2)(pTHX_ SV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200451# endif
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000452static void (*Perl_sys_init)(int* argc, char*** argv);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000453static void (*Perl_sys_term)(void);
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200454static void (*Perl_call_list)(pTHX_ I32, AV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200455# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
456# else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000457static SV** (*Perl_ISv_ptr)(register PerlInterpreter*);
458static SV*** (*Perl_Istack_max_ptr)(register PerlInterpreter*);
459static SV*** (*Perl_Istack_base_ptr)(register PerlInterpreter*);
460static XPV** (*Perl_IXpv_ptr)(register PerlInterpreter*);
461static I32* (*Perl_Itmps_ix_ptr)(register PerlInterpreter*);
462static I32* (*Perl_Itmps_floor_ptr)(register PerlInterpreter*);
463static STRLEN* (*Perl_Ina_ptr)(register PerlInterpreter*);
464static I32** (*Perl_Imarkstack_ptr_ptr)(register PerlInterpreter*);
465static I32** (*Perl_Imarkstack_max_ptr)(register PerlInterpreter*);
466static SV*** (*Perl_Istack_sp_ptr)(register PerlInterpreter*);
467static OP** (*Perl_Iop_ptr)(register PerlInterpreter*);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000468static I32* (*Perl_Iscopestack_ix_ptr)(register PerlInterpreter*);
469static AV** (*Perl_Iunitcheckav_ptr)(register PerlInterpreter*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200470# endif
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200471# endif
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200472# if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200473static I32 (*Perl_xs_handshake)(const U32, void *, const char *, ...);
474static void (*Perl_xs_boot_epilog)(pTHX_ const U32);
Bram Moolenaard8619992014-03-12 17:08:05 +0100475# endif
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200476
477# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
478# ifdef USE_ITHREADS
479static perl_key* dll_PL_thr_key;
480# endif
481# else
Bram Moolenaare06c1882010-07-21 22:05:20 +0200482static GV** (*Perl_Idefgv_ptr)(register PerlInterpreter*);
483static GV** (*Perl_Ierrgv_ptr)(register PerlInterpreter*);
484static SV* (*Perl_Isv_yes_ptr)(register PerlInterpreter*);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200485static perl_key* (*Perl_Gthr_key_ptr)_((pTHX));
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200486# endif
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200487static void (*boot_DynaLoader)_((pTHX_ CV*));
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100488static HE * (*Perl_hv_iternext_flags)(pTHX_ HV *, I32);
489static I32 (*Perl_hv_iterinit)(pTHX_ HV *);
490static char * (*Perl_hv_iterkey)(pTHX_ HE *, I32 *);
491static SV * (*Perl_hv_iterval)(pTHX_ HV *, HE *);
492static SV** (*Perl_av_fetch)(pTHX_ AV *, SSize_t, I32);
493static SSize_t (*Perl_av_len)(pTHX_ AV *);
494static NV (*Perl_sv_2nv_flags)(pTHX_ SV *const, const I32);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200495# if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200496static IV (*PerlIOBase_pushed)(pTHX_ PerlIO *, const char *, SV *, PerlIO_funcs *);
497static void (*PerlIO_define_layer)(pTHX_ PerlIO_funcs *);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200498# endif
499# if (PERL_REVISION == 5) && (PERL_VERSION >= 24)
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200500static void (*Perl_savetmps)(pTHX);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200501# endif
Bram Moolenaare06c1882010-07-21 22:05:20 +0200502
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503/*
504 * Table of name to function pointer of perl.
505 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506static struct {
507 char* name;
508 PERL_PROC* ptr;
509} perl_funcname_table[] = {
510 {"perl_alloc", (PERL_PROC*)&perl_alloc},
511 {"perl_construct", (PERL_PROC*)&perl_construct},
512 {"perl_destruct", (PERL_PROC*)&perl_destruct},
513 {"perl_free", (PERL_PROC*)&perl_free},
514 {"perl_run", (PERL_PROC*)&perl_run},
515 {"perl_parse", (PERL_PROC*)&perl_parse},
Christian Brabandt55460da2023-08-29 21:31:28 +0200516# if defined(WIN32) || ((PERL_REVISION == 5) && (PERL_VERSION < 38))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 {"Perl_get_context", (PERL_PROC*)&Perl_get_context},
Christian Brabandt55460da2023-08-29 21:31:28 +0200518# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 {"Perl_croak", (PERL_PROC*)&Perl_croak},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200520# ifdef PERL5101_OR_LATER
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100521 {"Perl_croak_xs_usage", (PERL_PROC*)&Perl_croak_xs_usage},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200522# endif
523# ifdef PERL_IMPLICIT_CONTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 {"Perl_croak_nocontext", (PERL_PROC*)&Perl_croak_nocontext},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200525# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 {"Perl_dowantarray", (PERL_PROC*)&Perl_dowantarray},
527 {"Perl_free_tmps", (PERL_PROC*)&Perl_free_tmps},
528 {"Perl_gv_stashpv", (PERL_PROC*)&Perl_gv_stashpv},
529 {"Perl_markstack_grow", (PERL_PROC*)&Perl_markstack_grow},
530 {"Perl_mg_find", (PERL_PROC*)&Perl_mg_find},
Bram Moolenaar578333b2018-07-22 07:31:09 +0200531# if (PERL_REVISION == 5) && (PERL_VERSION >= 28)
532 {"Perl_mg_get", (PERL_PROC*)&Perl_mg_get},
533# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 {"Perl_newXS", (PERL_PROC*)&Perl_newXS},
535 {"Perl_newSV", (PERL_PROC*)&Perl_newSV},
536 {"Perl_newSViv", (PERL_PROC*)&Perl_newSViv},
537 {"Perl_newSVpv", (PERL_PROC*)&Perl_newSVpv},
538 {"Perl_call_argv", (PERL_PROC*)&Perl_call_argv},
539 {"Perl_call_pv", (PERL_PROC*)&Perl_call_pv},
540 {"Perl_eval_sv", (PERL_PROC*)&Perl_eval_sv},
541 {"Perl_get_sv", (PERL_PROC*)&Perl_get_sv},
542 {"Perl_eval_pv", (PERL_PROC*)&Perl_eval_pv},
543 {"Perl_call_method", (PERL_PROC*)&Perl_call_method},
544 {"Perl_pop_scope", (PERL_PROC*)&Perl_pop_scope},
545 {"Perl_push_scope", (PERL_PROC*)&Perl_push_scope},
546 {"Perl_save_int", (PERL_PROC*)&Perl_save_int},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200547# if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200548 {"Perl_save_strlen", (PERL_PROC*)&Perl_save_strlen},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200549# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 {"Perl_stack_grow", (PERL_PROC*)&Perl_stack_grow},
551 {"Perl_set_context", (PERL_PROC*)&Perl_set_context},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200552# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200553 {"Perl_sv_2bool_flags", (PERL_PROC*)&Perl_sv_2bool_flags},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200554# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200555 {"Perl_xs_apiversion_bootcheck",(PERL_PROC*)&Perl_xs_apiversion_bootcheck},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200556# endif
557# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 {"Perl_sv_2bool", (PERL_PROC*)&Perl_sv_2bool},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200559# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 {"Perl_sv_2iv", (PERL_PROC*)&Perl_sv_2iv},
561 {"Perl_sv_2mortal", (PERL_PROC*)&Perl_sv_2mortal},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200562# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 {"Perl_sv_2pv_flags", (PERL_PROC*)&Perl_sv_2pv_flags},
564 {"Perl_sv_2pv_nolen", (PERL_PROC*)&Perl_sv_2pv_nolen},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200565# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 {"Perl_sv_2pv", (PERL_PROC*)&Perl_sv_2pv},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200567# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100568 {"Perl_sv_2pvbyte", (PERL_PROC*)&Perl_sv_2pvbyte},
Bram Moolenaar895a7a42020-09-10 21:36:11 +0200569# if (PERL_REVISION == 5) && (PERL_VERSION >= 32)
570 {"Perl_sv_2pvbyte_flags", (PERL_PROC*)&Perl_sv_2pvbyte_flags},
571# endif
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200572# ifdef PERL589_OR_LATER
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000573 {"Perl_sv_2iv_flags", (PERL_PROC*)&Perl_sv_2iv_flags},
574 {"Perl_newXS_flags", (PERL_PROC*)&Perl_newXS_flags},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200575# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 {"Perl_sv_bless", (PERL_PROC*)&Perl_sv_bless},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200577# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 {"Perl_sv_catpvn_flags", (PERL_PROC*)&Perl_sv_catpvn_flags},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200579# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 {"Perl_sv_catpvn", (PERL_PROC*)&Perl_sv_catpvn},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200581# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 {"Perl_sv_free", (PERL_PROC*)&Perl_sv_free},
583 {"Perl_sv_isa", (PERL_PROC*)&Perl_sv_isa},
584 {"Perl_sv_magic", (PERL_PROC*)&Perl_sv_magic},
585 {"Perl_sv_setiv", (PERL_PROC*)&Perl_sv_setiv},
586 {"Perl_sv_setpv", (PERL_PROC*)&Perl_sv_setpv},
587 {"Perl_sv_setpvn", (PERL_PROC*)&Perl_sv_setpvn},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200588# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 {"Perl_sv_setsv_flags", (PERL_PROC*)&Perl_sv_setsv_flags},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200590# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 {"Perl_sv_setsv", (PERL_PROC*)&Perl_sv_setsv},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200592# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 {"Perl_sv_upgrade", (PERL_PROC*)&Perl_sv_upgrade},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200594# if (PERL_REVISION == 5) && (PERL_VERSION < 10)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 {"Perl_Tstack_sp_ptr", (PERL_PROC*)&Perl_Tstack_sp_ptr},
596 {"Perl_Top_ptr", (PERL_PROC*)&Perl_Top_ptr},
597 {"Perl_Tstack_base_ptr", (PERL_PROC*)&Perl_Tstack_base_ptr},
598 {"Perl_Tstack_max_ptr", (PERL_PROC*)&Perl_Tstack_max_ptr},
599 {"Perl_Ttmps_ix_ptr", (PERL_PROC*)&Perl_Ttmps_ix_ptr},
600 {"Perl_Ttmps_floor_ptr", (PERL_PROC*)&Perl_Ttmps_floor_ptr},
601 {"Perl_Tmarkstack_ptr_ptr", (PERL_PROC*)&Perl_Tmarkstack_ptr_ptr},
602 {"Perl_Tmarkstack_max_ptr", (PERL_PROC*)&Perl_Tmarkstack_max_ptr},
603 {"Perl_TSv_ptr", (PERL_PROC*)&Perl_TSv_ptr},
604 {"Perl_TXpv_ptr", (PERL_PROC*)&Perl_TXpv_ptr},
605 {"Perl_Tna_ptr", (PERL_PROC*)&Perl_Tna_ptr},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200606# else
Bram Moolenaarccf22172008-09-01 15:56:45 +0000607 {"Perl_sv_free2", (PERL_PROC*)&Perl_sv_free2},
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000608 {"Perl_sys_init", (PERL_PROC*)&Perl_sys_init},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000609 {"Perl_sys_term", (PERL_PROC*)&Perl_sys_term},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200610 {"Perl_call_list", (PERL_PROC*)&Perl_call_list},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200611# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
612# else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000613 {"Perl_ISv_ptr", (PERL_PROC*)&Perl_ISv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000614 {"Perl_Istack_max_ptr", (PERL_PROC*)&Perl_Istack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200615 {"Perl_Istack_base_ptr", (PERL_PROC*)&Perl_Istack_base_ptr},
616 {"Perl_IXpv_ptr", (PERL_PROC*)&Perl_IXpv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000617 {"Perl_Itmps_ix_ptr", (PERL_PROC*)&Perl_Itmps_ix_ptr},
618 {"Perl_Itmps_floor_ptr", (PERL_PROC*)&Perl_Itmps_floor_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200619 {"Perl_Ina_ptr", (PERL_PROC*)&Perl_Ina_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000620 {"Perl_Imarkstack_ptr_ptr", (PERL_PROC*)&Perl_Imarkstack_ptr_ptr},
621 {"Perl_Imarkstack_max_ptr", (PERL_PROC*)&Perl_Imarkstack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200622 {"Perl_Istack_sp_ptr", (PERL_PROC*)&Perl_Istack_sp_ptr},
623 {"Perl_Iop_ptr", (PERL_PROC*)&Perl_Iop_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000624 {"Perl_Iscopestack_ix_ptr", (PERL_PROC*)&Perl_Iscopestack_ix_ptr},
625 {"Perl_Iunitcheckav_ptr", (PERL_PROC*)&Perl_Iunitcheckav_ptr},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200626# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200627# endif
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200628# if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200629 {"Perl_xs_handshake", (PERL_PROC*)&Perl_xs_handshake},
630 {"Perl_xs_boot_epilog", (PERL_PROC*)&Perl_xs_boot_epilog},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200631# endif
632# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100633# ifdef USE_ITHREADS
Bram Moolenaar01c10522012-09-21 12:50:51 +0200634 {"PL_thr_key", (PERL_PROC*)&dll_PL_thr_key},
Bram Moolenaard8619992014-03-12 17:08:05 +0100635# endif
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200636# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 {"Perl_Idefgv_ptr", (PERL_PROC*)&Perl_Idefgv_ptr},
638 {"Perl_Ierrgv_ptr", (PERL_PROC*)&Perl_Ierrgv_ptr},
639 {"Perl_Isv_yes_ptr", (PERL_PROC*)&Perl_Isv_yes_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200640 {"Perl_Gthr_key_ptr", (PERL_PROC*)&Perl_Gthr_key_ptr},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200641# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200642 {"boot_DynaLoader", (PERL_PROC*)&boot_DynaLoader},
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100643 {"Perl_hv_iternext_flags", (PERL_PROC*)&Perl_hv_iternext_flags},
644 {"Perl_hv_iterinit", (PERL_PROC*)&Perl_hv_iterinit},
645 {"Perl_hv_iterkey", (PERL_PROC*)&Perl_hv_iterkey},
646 {"Perl_hv_iterval", (PERL_PROC*)&Perl_hv_iterval},
647 {"Perl_av_fetch", (PERL_PROC*)&Perl_av_fetch},
648 {"Perl_av_len", (PERL_PROC*)&Perl_av_len},
649 {"Perl_sv_2nv_flags", (PERL_PROC*)&Perl_sv_2nv_flags},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200650# if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200651 {"PerlIOBase_pushed", (PERL_PROC*)&PerlIOBase_pushed},
652 {"PerlIO_define_layer", (PERL_PROC*)&PerlIO_define_layer},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200653# endif
654# if (PERL_REVISION == 5) && (PERL_VERSION >= 24)
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200655 {"Perl_savetmps", (PERL_PROC*)&Perl_savetmps},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200656# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 {"", NULL},
658};
659
Christian Brabandt55460da2023-08-29 21:31:28 +0200660# if (PERL_REVISION == 5) && (PERL_VERSION <= 30)
661// In 5.30, GIMME_V requires linking to Perl_block_gimme() instead of being
662// completely inline. Just use the deprecated GIMME for simplicity.
663# undef GIMME_V
664# define GIMME_V GIMME
Bram Moolenaarf5433fb2020-06-21 20:06:54 +0200665# endif
666
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667/*
668 * Make all runtime-links of perl.
669 *
Bram Moolenaar364ab2f2013-08-02 20:05:32 +0200670 * 1. Get module handle using dlopen() or vimLoadLib().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 * 2. Get pointer to perl function by GetProcAddress.
672 * 3. Repeat 2, until get all functions will be used.
673 *
674 * Parameter 'libname' provides name of DLL.
675 * Return OK or FAIL.
676 */
677 static int
678perl_runtime_link_init(char *libname, int verbose)
679{
680 int i;
681
682 if (hPerlLib != NULL)
683 return OK;
Bram Moolenaare06c1882010-07-21 22:05:20 +0200684 if ((hPerlLib = load_dll(libname)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 {
686 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100687 semsg(_("E370: Could not load library %s"), libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 return FAIL;
689 }
690 for (i = 0; perl_funcname_table[i].ptr; ++i)
691 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200692 if (!(*perl_funcname_table[i].ptr = symbol_from_dll(hPerlLib,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 perl_funcname_table[i].name)))
694 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200695 close_dll(hPerlLib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 hPerlLib = NULL;
697 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000698 semsg((const char *)_(e_could_not_load_library_function_str), perl_funcname_table[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 return FAIL;
700 }
701 }
702 return OK;
703}
704
705/*
706 * If runtime-link-perl(DLL) was loaded successfully, return TRUE.
707 * There were no DLL loaded, return FALSE.
708 */
709 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100710perl_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100712 return perl_runtime_link_init((char *)p_perldll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713}
714#endif /* DYNAMIC_PERL */
715
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200716#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
717static void vim_IOLayer_init(void);
718#endif
719
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720/*
721 * perl_init(): initialize perl interpreter
722 * We have to call perl_parse to initialize some structures,
723 * there's nothing to actually parse.
724 */
725 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100726perl_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727{
Bram Moolenaarc236c162008-07-13 17:41:49 +0000728 char *bootargs[] = { "VI", NULL };
729 int argc = 3;
730 static char *argv[] = { "", "-e", "" };
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731
Bram Moolenaarc236c162008-07-13 17:41:49 +0000732#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000733 Perl_sys_init(&argc, (char***)&argv);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 perl_interp = perl_alloc();
736 perl_construct(perl_interp);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000737 perl_parse(perl_interp, xs_init, argc, argv, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 perl_call_argv("VIM::bootstrap", (long)G_DISCARD, bootargs);
739 VIM_init();
740#ifdef USE_SFIO
741 sfdisc(PerlIO_stdout(), sfdcnewvim());
742 sfdisc(PerlIO_stderr(), sfdcnewvim());
743 sfsetbuf(PerlIO_stdout(), NULL, 0);
744 sfsetbuf(PerlIO_stderr(), NULL, 0);
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200745#elif defined(PERLIO_LAYERS)
746 vim_IOLayer_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747#endif
748}
749
750/*
Bram Moolenaar20279732020-03-29 20:51:07 +0200751 * Clean up after ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 */
753 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100754perl_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755{
756 if (perl_interp)
757 {
758 perl_run(perl_interp);
759 perl_destruct(perl_interp);
760 perl_free(perl_interp);
761 perl_interp = NULL;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000762#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100763 Perl_sys_term();
Bram Moolenaarc236c162008-07-13 17:41:49 +0000764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766}
767
768/*
769 * msg_split(): send a message to the message handling routines
770 * split at '\n' first though.
771 */
772 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100773msg_split(
774 char_u *s,
775 int attr) /* highlighting attributes */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776{
777 char *next;
778 char *token = (char *)s;
779
Bram Moolenaaraa8494a2007-10-09 08:47:27 +0000780 while ((next = strchr(token, '\n')) && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 {
782 *next++ = '\0'; /* replace \n with \0 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100783 msg_attr(token, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 token = next;
785 }
Bram Moolenaaraa8494a2007-10-09 08:47:27 +0000786 if (*token && !got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100787 msg_attr(token, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788}
789
790#ifndef FEAT_EVAL
791/*
792 * This stub is needed because an "#ifdef FEAT_EVAL" around Eval() doesn't
793 * work properly.
794 */
795 char_u *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100796eval_to_string(
797 char_u *arg UNUSED,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100798 int convert UNUSED,
799 int use_simple_function UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800{
801 return NULL;
802}
803#endif
804
805/*
806 * Create a new reference to an SV pointing to the SCR structure
Bram Moolenaare344bea2005-09-01 20:46:49 +0000807 * The b_perl_private/w_perl_private part of the SCR structure points to the
808 * SV, so there can only be one such SV for a particular SCR structure. When
809 * the last reference has gone (DESTROY is called),
810 * b_perl_private/w_perl_private is reset; When the screen goes away before
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 * all references are gone, the value of the SV is reset;
812 * any subsequent use of any of those reference will produce
813 * a warning. (see typemap)
814 */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000815
816 static SV *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100817newWINrv(SV *rv, win_T *ptr)
Bram Moolenaare344bea2005-09-01 20:46:49 +0000818{
819 sv_upgrade(rv, SVt_RV);
820 if (ptr->w_perl_private == NULL)
821 {
822 ptr->w_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +0100823 sv_setiv(ptr->w_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +0000824 }
Bram Moolenaar41c363a2018-08-02 21:46:51 +0200825 SvREFCNT_inc_void_NN(ptr->w_perl_private);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000826 SvRV(rv) = ptr->w_perl_private;
827 SvROK_on(rv);
828 return sv_bless(rv, gv_stashpv("VIWIN", TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829}
830
Bram Moolenaare344bea2005-09-01 20:46:49 +0000831 static SV *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100832newBUFrv(SV *rv, buf_T *ptr)
Bram Moolenaare344bea2005-09-01 20:46:49 +0000833{
834 sv_upgrade(rv, SVt_RV);
835 if (ptr->b_perl_private == NULL)
836 {
837 ptr->b_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +0100838 sv_setiv(ptr->b_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +0000839 }
Bram Moolenaar41c363a2018-08-02 21:46:51 +0200840 SvREFCNT_inc_void_NN(ptr->b_perl_private);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000841 SvRV(rv) = ptr->b_perl_private;
842 SvROK_on(rv);
843 return sv_bless(rv, gv_stashpv("VIBUF", TRUE));
844}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200846#if 0
847SV *__sv_save[1024];
848int __sv_save_ix;
849# define D_Save_Sv(sv) do { if (__sv_save_ix < 1024) __sv_save[__sv_save_ix++] = (sv); } while (0)
850#else
851# define D_Save_Sv(sv) NOOP
852#endif
853
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854/*
855 * perl_win_free
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200856 * Remove all references to the window to be destroyed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 */
858 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100859perl_win_free(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860{
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200861 if (wp->w_perl_private && perl_interp != NULL)
862 {
Bram Moolenaar578333b2018-07-22 07:31:09 +0200863 SV *sv = (SV*)wp->w_perl_private;
864 D_Save_Sv(sv);
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200865 sv_setiv(sv, 0);
866 SvREFCNT_dec(sv);
867 }
868 wp->w_perl_private = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869}
870
871 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100872perl_buf_free(buf_T *bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873{
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200874 if (bp->b_perl_private && perl_interp != NULL)
875 {
Bram Moolenaar578333b2018-07-22 07:31:09 +0200876 SV *sv = (SV *)bp->b_perl_private;
877 D_Save_Sv(sv);
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200878 sv_setiv(sv, 0);
879 SvREFCNT_dec(sv);
880 }
881 bp->b_perl_private = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882}
883
884#ifndef PROTO
885# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
886I32 cur_val(pTHX_ IV iv, SV *sv);
887# else
888I32 cur_val(IV iv, SV *sv);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200889# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890
891/*
892 * Handler for the magic variables $main::curwin and $main::curbuf.
893 * The handler is put into the magic vtbl for these variables.
894 * (This is effectively a C-level equivalent of a tied variable).
895 * There is no "set" function as the variables are read-only.
896 */
897# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
898I32 cur_val(pTHX_ IV iv, SV *sv)
899# else
900I32 cur_val(IV iv, SV *sv)
901# endif
902{
903 SV *rv;
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200904
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 if (iv == 0)
906 rv = newWINrv(newSV(0), curwin);
907 else
908 rv = newBUFrv(newSV(0), curbuf);
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200909
Bram Moolenaar41c363a2018-08-02 21:46:51 +0200910 if (SvRV(sv) != SvRV(rv))
911 // XXX: This magic variable is a bit confusing...
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100912 // Is currently refcounted ?
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200913 sv_setsv(sv, rv);
914
Bram Moolenaar41c363a2018-08-02 21:46:51 +0200915 SvREFCNT_dec(rv);
916
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 return 0;
918}
919#endif /* !PROTO */
920
921struct ufuncs cw_funcs = { cur_val, 0, 0 };
922struct ufuncs cb_funcs = { cur_val, 0, 1 };
923
924/*
925 * VIM_init(): Vim-specific initialisation.
926 * Make the magical main::curwin and main::curbuf variables
927 */
928 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100929VIM_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930{
931 static char cw[] = "main::curwin";
932 static char cb[] = "main::curbuf";
933 SV *sv;
934
935 sv = perl_get_sv(cw, TRUE);
936 sv_magic(sv, NULL, 'U', (char *)&cw_funcs, sizeof(cw_funcs));
937 SvREADONLY_on(sv);
938
939 sv = perl_get_sv(cb, TRUE);
940 sv_magic(sv, NULL, 'U', (char *)&cb_funcs, sizeof(cb_funcs));
941 SvREADONLY_on(sv);
942
943 /*
944 * Setup the Safe compartment.
945 * It shouldn't be a fatal error if the Safe module is missing.
946 * XXX: Only shares the 'Msg' routine (which has to be called
947 * like 'Msg(...)').
948 */
949 (void)perl_eval_pv( "if ( eval( 'require Safe' ) ) { $VIM::safe = Safe->new(); $VIM::safe->share_from( 'VIM', ['Msg'] ); }", G_DISCARD | G_VOID );
950
951}
952
953#ifdef DYNAMIC_PERL
954static char *e_noperl = N_("Sorry, this command is disabled: the Perl library could not be loaded.");
955#endif
956
957/*
958 * ":perl"
959 */
960 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100961ex_perl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962{
963 char *err;
964 char *script;
965 STRLEN length;
966 SV *sv;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200967#ifdef HAVE_SANDBOX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 SV *safe;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970
971 script = (char *)script_get(eap, eap->arg);
972 if (eap->skip)
973 {
974 vim_free(script);
975 return;
976 }
977
978 if (perl_interp == NULL)
979 {
980#ifdef DYNAMIC_PERL
981 if (!perl_enabled(TRUE))
982 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100983 emsg(_(e_noperl));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 vim_free(script);
985 return;
986 }
987#endif
988 perl_init();
989 }
990
991 {
992 dSP;
993 ENTER;
994 SAVETMPS;
995
996 if (script == NULL)
997 sv = newSVpv((char *)eap->arg, 0);
998 else
999 {
1000 sv = newSVpv(script, 0);
1001 vim_free(script);
1002 }
1003
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001004 if (sandbox || secure)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 {
Bram Moolenaara1711622011-07-27 14:15:46 +02001006 safe = perl_get_sv("VIM::safe", FALSE);
Bram Moolenaar3f947ea2009-07-14 14:04:54 +00001007# ifndef MAKE_TEST /* avoid a warning for unreachable code */
Bram Moolenaar954e8c52009-11-11 13:45:33 +00001008 if (safe == NULL || !SvTRUE(safe))
Bram Moolenaare96eea72022-01-28 21:00:51 +00001009 emsg(_(e_perl_evaluation_forbidden_in_sandbox_without_safe_module));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 else
Bram Moolenaar3f947ea2009-07-14 14:04:54 +00001011# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 {
1013 PUSHMARK(SP);
1014 XPUSHs(safe);
1015 XPUSHs(sv);
1016 PUTBACK;
1017 perl_call_method("reval", G_DISCARD);
1018 }
1019 }
1020 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
1022
1023 SvREFCNT_dec(sv);
1024
Bram Moolenaar7b61bf12016-06-26 17:16:51 +02001025 err = SvPV(GvSV(PL_errgv), length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026
1027 FREETMPS;
1028 LEAVE;
1029
1030 if (!length)
1031 return;
1032
1033 msg_split((char_u *)err, highlight_attr[HLF_E]);
1034 return;
1035 }
1036}
1037
1038 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001039replace_line(linenr_T *line, linenr_T *end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040{
1041 char *str;
1042
1043 if (SvOK(GvSV(PL_defgv)))
1044 {
1045 str = SvPV(GvSV(PL_defgv), PL_na);
1046 ml_replace(*line, (char_u *)str, 1);
1047 changed_bytes(*line, 0);
1048 }
1049 else
1050 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001051 ml_delete(*line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 deleted_lines_mark(*line, 1L);
1053 --(*end);
1054 --(*line);
1055 }
1056 return OK;
1057}
1058
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001059static struct ref_map_S {
1060 void *vim_ref;
1061 SV *perl_ref;
1062 struct ref_map_S *next;
1063} *ref_map = NULL;
1064
1065 static void
1066ref_map_free(void)
1067{
1068 struct ref_map_S *tofree;
1069 struct ref_map_S *refs = ref_map;
1070
1071 while (refs) {
1072 tofree = refs;
1073 refs = refs->next;
1074 vim_free(tofree);
1075 }
1076 ref_map = NULL;
1077}
1078
1079 static struct ref_map_S *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001080ref_map_find_SV(SV *const sv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001081{
1082 struct ref_map_S *refs = ref_map;
1083 int count = 350;
1084
1085 while (refs) {
1086 if (refs->perl_ref == sv)
1087 break;
1088 refs = refs->next;
1089 count--;
1090 }
1091
1092 if (!refs && count > 0) {
1093 refs = (struct ref_map_S *)alloc(sizeof(struct ref_map_S));
1094 if (!refs)
1095 return NULL;
1096 refs->perl_ref = sv;
1097 refs->vim_ref = NULL;
1098 refs->next = ref_map;
1099 ref_map = refs;
1100 }
1101
1102 return refs;
1103}
1104
1105 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001106perl_to_vim(SV *sv, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001107{
1108 if (SvROK(sv))
1109 sv = SvRV(sv);
1110
1111 switch (SvTYPE(sv)) {
1112 case SVt_NULL:
1113 break;
1114 case SVt_NV: /* float */
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001115 rettv->v_type = VAR_FLOAT;
1116 rettv->vval.v_float = SvNV(sv);
1117 break;
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001118 case SVt_IV: /* integer */
1119 if (!SvROK(sv)) { /* references should be string */
1120 rettv->vval.v_number = SvIV(sv);
1121 break;
1122 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001123 /* FALLTHROUGH */
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001124 case SVt_PV: /* string */
1125 {
1126 size_t len = 0;
1127 char * str_from = SvPV(sv, len);
Bram Moolenaar9b0ac222016-06-01 20:31:43 +02001128 char_u *str_to = (char_u*)alloc(
1129 (unsigned)(sizeof(char_u) * (len + 1)));
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001130
1131 if (str_to) {
1132 str_to[len] = '\0';
1133
1134 while (len--) {
1135 if (str_from[len] == '\0')
1136 str_to[len] = '\n';
1137 else
1138 str_to[len] = str_from[len];
1139 }
1140 }
1141
1142 rettv->v_type = VAR_STRING;
1143 rettv->vval.v_string = str_to;
1144 break;
1145 }
1146 case SVt_PVAV: /* list */
1147 {
1148 SSize_t size;
1149 listitem_T * item;
1150 SV ** item2;
1151 list_T * list;
1152 struct ref_map_S * refs;
1153
1154 if ((refs = ref_map_find_SV(sv)) == NULL)
1155 return FAIL;
1156
1157 if (refs->vim_ref)
1158 list = (list_T *) refs->vim_ref;
1159 else
1160 {
1161 if ((list = list_alloc()) == NULL)
1162 return FAIL;
1163 refs->vim_ref = list;
1164
1165 for (size = av_len((AV*)sv); size >= 0; size--)
1166 {
1167 if ((item = listitem_alloc()) == NULL)
1168 break;
1169
1170 item->li_tv.v_type = VAR_NUMBER;
1171 item->li_tv.v_lock = 0;
1172 item->li_tv.vval.v_number = 0;
1173 list_insert(list, item, list->lv_first);
1174
1175 item2 = av_fetch((AV *)sv, size, 0);
1176
1177 if (item2 == NULL || *item2 == NULL ||
Bram Moolenaard99df422016-01-29 23:20:40 +01001178 perl_to_vim(*item2, &item->li_tv) == FAIL)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001179 break;
1180 }
1181 }
1182
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001183 rettv_list_set(rettv, list);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001184 break;
1185 }
1186 case SVt_PVHV: /* dictionary */
1187 {
1188 HE * entry;
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001189 I32 key_len;
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001190 char * key;
1191 dictitem_T * item;
1192 SV * item2;
1193 dict_T * dict;
1194 struct ref_map_S * refs;
1195
1196 if ((refs = ref_map_find_SV(sv)) == NULL)
1197 return FAIL;
1198
1199 if (refs->vim_ref)
1200 dict = (dict_T *) refs->vim_ref;
1201 else
1202 {
1203
1204 if ((dict = dict_alloc()) == NULL)
1205 return FAIL;
1206 refs->vim_ref = dict;
1207
1208 hv_iterinit((HV *)sv);
1209
1210 for (entry = hv_iternext((HV *)sv); entry; entry = hv_iternext((HV *)sv))
1211 {
1212 key_len = 0;
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001213 key = hv_iterkey(entry, &key_len);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001214
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001215 if (!key || !key_len || strlen(key) < (size_t)key_len) {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001216 semsg("Malformed key Dictionary '%s'", key && *key ? key : "(empty)");
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001217 break;
1218 }
1219
1220 if ((item = dictitem_alloc((char_u *)key)) == NULL)
1221 break;
Bram Moolenaarc89d4b32018-07-08 17:19:02 +02001222 item->di_tv.v_type = VAR_NUMBER;
1223 item->di_tv.vval.v_number = 0;
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001224
1225 if (dict_add(dict, item) == FAIL) {
1226 dictitem_free(item);
1227 break;
1228 }
1229 item2 = hv_iterval((HV *)sv, entry);
1230 if (item2 == NULL || perl_to_vim(item2, &item->di_tv) == FAIL)
1231 break;
1232 }
1233 }
1234
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001235 rettv_dict_set(rettv, dict);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001236 break;
1237 }
1238 default: /* not convertible */
1239 {
1240 char *val = SvPV_nolen(sv);
1241 rettv->v_type = VAR_STRING;
1242 rettv->vval.v_string = val ? vim_strsave((char_u *)val) : NULL;
1243 break;
1244 }
1245 }
1246 return OK;
1247}
1248
1249/*
1250 * "perleval()"
1251 */
1252 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001253do_perleval(char_u *str, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001254{
1255 char *err = NULL;
1256 STRLEN err_len = 0;
1257 SV *sv = NULL;
1258#ifdef HAVE_SANDBOX
1259 SV *safe;
1260#endif
1261
1262 if (perl_interp == NULL)
1263 {
1264#ifdef DYNAMIC_PERL
1265 if (!perl_enabled(TRUE))
1266 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001267 emsg(_(e_noperl));
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001268 return;
1269 }
1270#endif
1271 perl_init();
1272 }
1273
1274 {
1275 dSP;
1276 ENTER;
1277 SAVETMPS;
1278
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001279 if (sandbox || secure)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001280 {
1281 safe = get_sv("VIM::safe", FALSE);
1282# ifndef MAKE_TEST /* avoid a warning for unreachable code */
1283 if (safe == NULL || !SvTRUE(safe))
Bram Moolenaare96eea72022-01-28 21:00:51 +00001284 emsg(_(e_perl_evaluation_forbidden_in_sandbox_without_safe_module));
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001285 else
1286# endif
1287 {
1288 sv = newSVpv((char *)str, 0);
1289 PUSHMARK(SP);
1290 XPUSHs(safe);
1291 XPUSHs(sv);
1292 PUTBACK;
1293 call_method("reval", G_SCALAR);
1294 SPAGAIN;
1295 SvREFCNT_dec(sv);
1296 sv = POPs;
Bram Moolenaar0f267622022-05-10 13:32:24 +01001297 PUTBACK;
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001298 }
1299 }
1300 else
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001301 sv = eval_pv((char *)str, 0);
1302
1303 if (sv) {
1304 perl_to_vim(sv, rettv);
1305 ref_map_free();
Bram Moolenaar7b61bf12016-06-26 17:16:51 +02001306 err = SvPV(GvSV(PL_errgv), err_len);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001307 }
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001308 FREETMPS;
1309 LEAVE;
1310 }
1311 if (err_len)
1312 msg_split((char_u *)err, highlight_attr[HLF_E]);
1313}
1314
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315/*
1316 * ":perldo".
1317 */
1318 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001319ex_perldo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320{
1321 STRLEN length;
1322 SV *sv;
1323 char *str;
1324 linenr_T i;
Bram Moolenaar85b57432017-01-29 22:59:12 +01001325 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001327 if (BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 return;
1329
1330 if (perl_interp == NULL)
1331 {
1332#ifdef DYNAMIC_PERL
1333 if (!perl_enabled(TRUE))
1334 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001335 emsg(_(e_noperl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 return;
1337 }
1338#endif
1339 perl_init();
1340 }
1341 {
1342 dSP;
1343 length = strlen((char *)eap->arg);
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001344 sv = newSV(length + sizeof("sub VIM::perldo {") - 1 + 1);
1345 sv_setpvn(sv, "sub VIM::perldo {", sizeof("sub VIM::perldo {") - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 sv_catpvn(sv, (char *)eap->arg, length);
1347 sv_catpvn(sv, "}", 1);
1348 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
1349 SvREFCNT_dec(sv);
Bram Moolenaar7b61bf12016-06-26 17:16:51 +02001350 str = SvPV(GvSV(PL_errgv), length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 if (length)
1352 goto err;
1353
1354 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
1355 return;
1356
1357 ENTER;
1358 SAVETMPS;
1359 for (i = eap->line1; i <= eap->line2; i++)
1360 {
Bram Moolenaar85b57432017-01-29 22:59:12 +01001361 /* Check the line number, the command my have deleted lines. */
1362 if (i > curbuf->b_ml.ml_line_count)
1363 break;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001364 sv_setpv(GvSV(PL_defgv), (char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 PUSHMARK(sp);
1366 perl_call_pv("VIM::perldo", G_SCALAR | G_EVAL);
Bram Moolenaar7b61bf12016-06-26 17:16:51 +02001367 str = SvPV(GvSV(PL_errgv), length);
Bram Moolenaar85b57432017-01-29 22:59:12 +01001368 if (length || curbuf != was_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 break;
1370 SPAGAIN;
1371 if (SvTRUEx(POPs))
1372 {
1373 if (replace_line(&i, &eap->line2) != OK)
1374 {
1375 PUTBACK;
1376 break;
1377 }
1378 }
1379 PUTBACK;
1380 }
1381 FREETMPS;
1382 LEAVE;
1383 check_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001384 update_screen(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 if (!length)
1386 return;
1387
1388err:
1389 msg_split((char_u *)str, highlight_attr[HLF_E]);
1390 return;
1391 }
1392}
1393
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001394#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
1395typedef struct {
1396 struct _PerlIO base;
1397 int attr;
1398} PerlIOVim;
1399
1400 static IV
1401PerlIOVim_pushed(pTHX_ PerlIO *f, const char *mode,
1402 SV *arg, PerlIO_funcs *tab)
1403{
1404 PerlIOVim *s = PerlIOSelf(f, PerlIOVim);
1405 s->attr = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001406 if (arg && SvPOK(arg))
1407 s->attr = syn_name2attr((char_u *)SvPV_nolen(arg));
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001408 return PerlIOBase_pushed(aTHX_ f, mode, (SV *)NULL, tab);
1409}
1410
1411 static SSize_t
1412PerlIOVim_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1413{
1414 char_u *str;
1415 PerlIOVim * s = PerlIOSelf(f, PerlIOVim);
1416
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001417 str = vim_strnsave((char_u *)vbuf, count);
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001418 if (str == NULL)
1419 return 0;
1420 msg_split((char_u *)str, s->attr);
1421 vim_free(str);
1422
Bram Moolenaar9b0ac222016-06-01 20:31:43 +02001423 return (SSize_t)count;
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001424}
1425
1426static PERLIO_FUNCS_DECL(PerlIO_Vim) = {
1427 sizeof(PerlIO_funcs),
1428 "Vim",
1429 sizeof(PerlIOVim),
1430 PERLIO_K_DUMMY, /* flags */
1431 PerlIOVim_pushed,
1432 NULL, /* popped */
1433 NULL, /* open */
1434 NULL, /* binmode */
1435 NULL, /* arg */
1436 NULL, /* fileno */
1437 NULL, /* dup */
1438 NULL, /* read */
1439 NULL, /* unread */
1440 PerlIOVim_write,
1441 NULL, /* seek */
1442 NULL, /* tell */
1443 NULL, /* close */
1444 NULL, /* flush */
1445 NULL, /* fill */
1446 NULL, /* eof */
1447 NULL, /* error */
1448 NULL, /* clearerr */
1449 NULL, /* setlinebuf */
1450 NULL, /* get_base */
1451 NULL, /* get_bufsiz */
1452 NULL, /* get_ptr */
1453 NULL, /* get_cnt */
1454 NULL /* set_ptrcnt */
1455};
1456
1457/* Use Vim routine for print operator */
1458 static void
1459vim_IOLayer_init(void)
1460{
1461 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_Vim));
1462 (void)eval_pv( "binmode(STDOUT, ':Vim')"
1463 " && binmode(STDERR, ':Vim(ErrorMsg)');", 0);
1464}
1465#endif /* PERLIO_LAYERS && !USE_SFIO */
1466
Christian Brabandt55460da2023-08-29 21:31:28 +02001467#ifdef DYNAMIC_PERL
1468
1469// Certain functionality that we use like SvREFCNT_dec are inlined for
1470// performance reasons. They reference Perl APIs like Perl_sv_free2(), which
1471// would cause linking errors in dynamic builds as we don't link against Perl
1472// during build time. Manually fix it here by redirecting these functions
1473// towards the dynamically loaded version.
1474
1475# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
1476# undef Perl_sv_free2
1477void Perl_sv_free2(pTHX_ SV* sv, const U32 refcnt)
1478{
1479 (*dll_Perl_sv_free2)(aTHX_ sv, refcnt);
1480}
1481# else
1482# undef Perl_sv_free2
1483void Perl_sv_free2(pTHX_ SV* sv)
1484{
1485 (*dll_Perl_sv_free2)(aTHX_ sv);
1486}
1487# endif
1488
1489# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
1490# undef Perl_sv_2bool_flags
1491bool Perl_sv_2bool_flags(pTHX_ SV* sv, I32 flags)
1492{
1493 return (*dll_Perl_sv_2bool_flags)(aTHX_ sv, flags);
1494}
1495# endif
1496
1497# if (PERL_REVISION == 5) && (PERL_VERSION >= 28)
1498# undef Perl_mg_get
1499int Perl_mg_get(pTHX_ SV* sv)
1500{
1501 return (*dll_Perl_mg_get)(aTHX_ sv);
1502}
1503# endif
1504
1505# undef Perl_sv_2nv_flags
1506NV Perl_sv_2nv_flags(pTHX_ SV *const sv, const I32 flags)
1507{
1508 return (*dll_Perl_sv_2nv_flags)(aTHX_ sv, flags);
1509}
1510
1511# ifdef PERL589_OR_LATER
1512# undef Perl_sv_2iv_flags
1513IV Perl_sv_2iv_flags(pTHX_ SV* sv, I32 flags)
1514{
1515 return (*dll_Perl_sv_2iv_flags)(aTHX_ sv, flags);
1516}
1517# endif
1518
1519# ifdef PERL_USE_THREAD_LOCAL
1520PERL_THREAD_LOCAL void *PL_current_context;
1521# endif
1522
1523#endif // DYNAMIC_PERL
1524
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525XS(boot_VIM);
1526
1527 static void
1528xs_init(pTHX)
1529{
1530 char *file = __FILE__;
1531
1532 /* DynaLoader is a special case */
1533 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1534 newXS("VIM::bootstrap", boot_VIM, file);
1535}
1536
1537typedef win_T * VIWIN;
1538typedef buf_T * VIBUF;
1539
1540MODULE = VIM PACKAGE = VIM
1541
1542void
1543Msg(text, hl=NULL)
1544 char *text;
1545 char *hl;
1546
1547 PREINIT:
1548 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549
1550 PPCODE:
1551 if (text != NULL)
1552 {
1553 attr = 0;
1554 if (hl != NULL)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001555 attr = syn_name2attr((char_u *)hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 msg_split((char_u *)text, attr);
1557 }
1558
1559void
1560SetOption(line)
1561 char *line;
1562
1563 PPCODE:
1564 if (line != NULL)
1565 do_set((char_u *)line, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001566 update_screen(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567
1568void
1569DoCommand(line)
1570 char *line;
1571
1572 PPCODE:
1573 if (line != NULL)
1574 do_cmdline_cmd((char_u *)line);
1575
1576void
1577Eval(str)
1578 char *str;
1579
1580 PREINIT:
1581 char_u *value;
1582 PPCODE:
Bram Moolenaara4e0b972022-10-01 19:43:52 +01001583 value = eval_to_string((char_u *)str, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 if (value == NULL)
1585 {
1586 XPUSHs(sv_2mortal(newSViv(0)));
1587 XPUSHs(sv_2mortal(newSVpv("", 0)));
1588 }
1589 else
1590 {
1591 XPUSHs(sv_2mortal(newSViv(1)));
1592 XPUSHs(sv_2mortal(newSVpv((char *)value, 0)));
1593 vim_free(value);
1594 }
1595
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001596SV*
1597Blob(SV* sv)
1598 PREINIT:
Bram Moolenaarb1443b42019-01-13 23:51:14 +01001599 STRLEN len;
1600 char *s;
1601 unsigned i;
1602 char buf[3];
1603 SV* newsv;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001604
1605 CODE:
1606 s = SvPVbyte(sv, len);
1607 newsv = newSVpv("0z", 2);
1608 for (i = 0; i < len; i++)
1609 {
Bram Moolenaar2472ae82019-02-23 15:04:17 +01001610 sprintf(buf, "%02X", (unsigned char)(s[i]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001611 sv_catpvn(newsv, buf, 2);
1612 }
1613 RETVAL = newsv;
1614 OUTPUT:
1615 RETVAL
1616
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617void
1618Buffers(...)
1619
1620 PREINIT:
1621 buf_T *vimbuf;
1622 int i, b;
1623
1624 PPCODE:
1625 if (items == 0)
1626 {
Philip H1d7caa52023-06-22 08:55:47 +02001627 if (GIMME_V == G_SCALAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 {
1629 i = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02001630 FOR_ALL_BUFFERS(vimbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 ++i;
1632
1633 XPUSHs(sv_2mortal(newSViv(i)));
1634 }
1635 else
1636 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001637 FOR_ALL_BUFFERS(vimbuf)
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +02001638 XPUSHs(sv_2mortal(newBUFrv(newSV(0), vimbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 }
1640 }
1641 else
1642 {
1643 for (i = 0; i < items; i++)
1644 {
1645 SV *sv = ST(i);
1646 if (SvIOK(sv))
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001647 b = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 else
1649 {
1650 char_u *pat;
1651 STRLEN len;
1652
1653 pat = (char_u *)SvPV(sv, len);
1654 ++emsg_off;
Bram Moolenaar16896a12018-03-06 12:25:56 +01001655 b = buflist_findpat(pat, pat + len, TRUE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 --emsg_off;
1657 }
1658
1659 if (b >= 0)
1660 {
1661 vimbuf = buflist_findnr(b);
1662 if (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 }
1667
1668void
1669Windows(...)
1670
1671 PREINIT:
1672 win_T *vimwin;
1673 int i, w;
1674
1675 PPCODE:
1676 if (items == 0)
1677 {
Philip H1d7caa52023-06-22 08:55:47 +02001678 if (GIMME_V == G_SCALAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 XPUSHs(sv_2mortal(newSViv(win_count())));
1680 else
1681 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001682 FOR_ALL_WINDOWS(vimwin)
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +02001683 XPUSHs(sv_2mortal(newWINrv(newSV(0), vimwin)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 }
1685 }
1686 else
1687 {
1688 for (i = 0; i < items; i++)
1689 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001690 w = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 vimwin = win_find_nr(w);
1692 if (vimwin)
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +02001693 XPUSHs(sv_2mortal(newWINrv(newSV(0), vimwin)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 }
1695 }
1696
1697MODULE = VIM PACKAGE = VIWIN
1698
1699void
1700DESTROY(win)
1701 VIWIN win
1702
1703 CODE:
1704 if (win_valid(win))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001705 win->w_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706
1707SV *
1708Buffer(win)
1709 VIWIN win
1710
1711 CODE:
1712 if (!win_valid(win))
1713 win = curwin;
1714 RETVAL = newBUFrv(newSV(0), win->w_buffer);
1715 OUTPUT:
1716 RETVAL
1717
1718void
1719SetHeight(win, height)
1720 VIWIN win
1721 int height;
1722
1723 PREINIT:
1724 win_T *savewin;
1725
1726 PPCODE:
1727 if (!win_valid(win))
1728 win = curwin;
1729 savewin = curwin;
1730 curwin = win;
1731 win_setheight(height);
1732 curwin = savewin;
1733
1734void
Bram Moolenaar864733a2016-04-02 14:18:01 +02001735Cursor(win, ...)
1736 VIWIN win
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737
1738 PPCODE:
Bram Moolenaara1711622011-07-27 14:15:46 +02001739 if (items == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 {
1741 EXTEND(sp, 2);
1742 if (!win_valid(win))
1743 win = curwin;
1744 PUSHs(sv_2mortal(newSViv(win->w_cursor.lnum)));
1745 PUSHs(sv_2mortal(newSViv(win->w_cursor.col)));
1746 }
Bram Moolenaara1711622011-07-27 14:15:46 +02001747 else if (items == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 {
1749 int lnum, col;
1750
1751 if (!win_valid(win))
1752 win = curwin;
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001753 lnum = (int) SvIV(ST(1));
1754 col = (int) SvIV(ST(2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 win->w_cursor.lnum = lnum;
1756 win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02001757 win->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 check_cursor(); /* put cursor on an existing line */
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001759 update_screen(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 }
1761
1762MODULE = VIM PACKAGE = VIBUF
1763
1764void
1765DESTROY(vimbuf)
1766 VIBUF vimbuf;
1767
1768 CODE:
1769 if (buf_valid(vimbuf))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001770 vimbuf->b_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771
1772void
1773Name(vimbuf)
1774 VIBUF vimbuf;
1775
1776 PPCODE:
1777 if (!buf_valid(vimbuf))
1778 vimbuf = curbuf;
1779 /* No file name returns an empty string */
1780 if (vimbuf->b_fname == NULL)
1781 XPUSHs(sv_2mortal(newSVpv("", 0)));
1782 else
1783 XPUSHs(sv_2mortal(newSVpv((char *)vimbuf->b_fname, 0)));
1784
1785void
1786Number(vimbuf)
1787 VIBUF vimbuf;
1788
1789 PPCODE:
1790 if (!buf_valid(vimbuf))
1791 vimbuf = curbuf;
1792 XPUSHs(sv_2mortal(newSViv(vimbuf->b_fnum)));
1793
1794void
1795Count(vimbuf)
1796 VIBUF vimbuf;
1797
1798 PPCODE:
1799 if (!buf_valid(vimbuf))
1800 vimbuf = curbuf;
1801 XPUSHs(sv_2mortal(newSViv(vimbuf->b_ml.ml_line_count)));
1802
1803void
1804Get(vimbuf, ...)
1805 VIBUF vimbuf;
1806
1807 PREINIT:
1808 char_u *line;
1809 int i;
1810 long lnum;
1811 PPCODE:
1812 if (buf_valid(vimbuf))
1813 {
1814 for (i = 1; i < items; i++)
1815 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001816 lnum = (long) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1818 {
1819 line = ml_get_buf(vimbuf, lnum, FALSE);
1820 XPUSHs(sv_2mortal(newSVpv((char *)line, 0)));
1821 }
1822 }
1823 }
1824
1825void
1826Set(vimbuf, ...)
1827 VIBUF vimbuf;
1828
1829 PREINIT:
1830 int i;
1831 long lnum;
1832 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 PPCODE:
1834 if (buf_valid(vimbuf))
1835 {
1836 if (items < 3)
1837 croak("Usage: VIBUF::Set(vimbuf, lnum, @lines)");
1838
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001839 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 for(i = 2; i < items; i++, lnum++)
1841 {
1842 line = SvPV(ST(i),PL_na);
1843 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1844 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001845 aco_save_T aco;
1846
Bram Moolenaare76062c2022-11-28 18:51:43 +00001847 /* Set curwin/curbuf for "vimbuf" and save some things. */
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001848 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00001849 if (curbuf == vimbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00001851 /* Only when a window was found. */
1852 if (u_savesub(lnum) == OK)
1853 {
1854 ml_replace(lnum, (char_u *)line, TRUE);
1855 changed_bytes(lnum, 0);
1856 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001857
Bram Moolenaare76062c2022-11-28 18:51:43 +00001858 /* restore curwin/curbuf and a few other things */
1859 aucmd_restbuf(&aco);
1860 /* Careful: autocommands may have made "vimbuf" invalid! */
1861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 }
1863 }
1864 }
1865
1866void
1867Delete(vimbuf, ...)
1868 VIBUF vimbuf;
1869
1870 PREINIT:
1871 long i, lnum = 0, count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 PPCODE:
1873 if (buf_valid(vimbuf))
1874 {
1875 if (items == 2)
1876 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001877 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 count = 1;
1879 }
1880 else if (items == 3)
1881 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001882 lnum = (long) SvIV(ST(1));
1883 count = (long) 1 + SvIV(ST(2)) - lnum;
Bram Moolenaara1711622011-07-27 14:15:46 +02001884 if (count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 count = 1;
Bram Moolenaara1711622011-07-27 14:15:46 +02001886 if (count < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 {
1888 lnum -= count;
1889 count = -count;
1890 }
1891 }
1892 if (items >= 2)
1893 {
1894 for (i = 0; i < count; i++)
1895 {
1896 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1897 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001898 aco_save_T aco;
1899
1900 /* set curwin/curbuf for "vimbuf" and save some things */
1901 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00001902 if (curbuf == vimbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00001904 /* Only when a window was found. */
1905 if (u_savedel(lnum, 1) == OK)
1906 {
1907 ml_delete(lnum);
1908 check_cursor();
1909 deleted_lines_mark(lnum, 1L);
1910 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001911
Bram Moolenaare76062c2022-11-28 18:51:43 +00001912 /* restore curwin/curbuf and a few other things */
1913 aucmd_restbuf(&aco);
1914 /* Careful: autocommands may have made "vimbuf"
1915 * invalid! */
1916 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001917
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001918 update_curbuf(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 }
1920 }
1921 }
1922 }
1923
1924void
1925Append(vimbuf, ...)
1926 VIBUF vimbuf;
1927
1928 PREINIT:
1929 int i;
1930 long lnum;
1931 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 PPCODE:
1933 if (buf_valid(vimbuf))
1934 {
1935 if (items < 3)
1936 croak("Usage: VIBUF::Append(vimbuf, lnum, @lines)");
1937
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001938 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 for (i = 2; i < items; i++, lnum++)
1940 {
1941 line = SvPV(ST(i),PL_na);
1942 if (lnum >= 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1943 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001944 aco_save_T aco;
1945
1946 /* set curwin/curbuf for "vimbuf" and save some things */
1947 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00001948 if (curbuf == vimbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00001950 /* Only when a window for "vimbuf" was found. */
1951 if (u_inssub(lnum + 1) == OK)
1952 {
1953 ml_append(lnum, (char_u *)line, (colnr_T)0, FALSE);
1954 appended_lines_mark(lnum, 1L);
1955 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001956
Bram Moolenaare76062c2022-11-28 18:51:43 +00001957 /* restore curwin/curbuf and a few other things */
1958 aucmd_restbuf(&aco);
1959 /* Careful: autocommands may have made "vimbuf" invalid! */
1960 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001961
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001962 update_curbuf(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 }
1964 }
1965 }
1966
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001967#ifdef __GNUC__
1968# pragma GCC diagnostic pop
1969#endif