blob: 180fce64c4395becb14475f4770e5bd95b15bbcf [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
Yee Cheng Chin7a9d1aa2023-09-01 18:46:17 +0200318# ifdef PERL_USE_THREAD_LOCAL
319# define PL_current_context *dll_PL_current_context
320# endif
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100321# define Perl_hv_iternext_flags dll_Perl_hv_iternext_flags
322# define Perl_hv_iterinit dll_Perl_hv_iterinit
323# define Perl_hv_iterkey dll_Perl_hv_iterkey
324# define Perl_hv_iterval dll_Perl_hv_iterval
325# define Perl_av_fetch dll_Perl_av_fetch
326# define Perl_av_len dll_Perl_av_len
327# define Perl_sv_2nv_flags dll_Perl_sv_2nv_flags
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200328# if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
329# define PerlIOBase_pushed dll_PerlIOBase_pushed
330# define PerlIO_define_layer dll_PerlIO_define_layer
331# endif
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200332# if (PERL_REVISION == 5) && (PERL_VERSION >= 24)
333# define Perl_savetmps dll_Perl_savetmps
334# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000335
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336/*
337 * Declare HANDLE for perl.dll and function pointers.
338 */
339static HANDLE hPerlLib = NULL;
340
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200341static PerlInterpreter* (*perl_alloc)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342static void (*perl_construct)(PerlInterpreter*);
343static void (*perl_destruct)(PerlInterpreter*);
344static void (*perl_free)(PerlInterpreter*);
345static int (*perl_run)(PerlInterpreter*);
346static int (*perl_parse)(PerlInterpreter*, XSINIT_t, int, char**, char**);
Christian Brabandt55460da2023-08-29 21:31:28 +0200347# if defined(WIN32) || ((PERL_REVISION == 5) && (PERL_VERSION < 38))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348static void* (*Perl_get_context)(void);
Christian Brabandt55460da2023-08-29 21:31:28 +0200349# endif
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
Yee Cheng Chin7a9d1aa2023-09-01 18:46:17 +0200490# ifdef PERL_USE_THREAD_LOCAL
491static void** dll_PL_current_context;
492# endif
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200493static void (*boot_DynaLoader)_((pTHX_ CV*));
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100494static HE * (*Perl_hv_iternext_flags)(pTHX_ HV *, I32);
495static I32 (*Perl_hv_iterinit)(pTHX_ HV *);
496static char * (*Perl_hv_iterkey)(pTHX_ HE *, I32 *);
497static SV * (*Perl_hv_iterval)(pTHX_ HV *, HE *);
498static SV** (*Perl_av_fetch)(pTHX_ AV *, SSize_t, I32);
499static SSize_t (*Perl_av_len)(pTHX_ AV *);
500static NV (*Perl_sv_2nv_flags)(pTHX_ SV *const, const I32);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200501# if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200502static IV (*PerlIOBase_pushed)(pTHX_ PerlIO *, const char *, SV *, PerlIO_funcs *);
503static void (*PerlIO_define_layer)(pTHX_ PerlIO_funcs *);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200504# endif
505# if (PERL_REVISION == 5) && (PERL_VERSION >= 24)
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200506static void (*Perl_savetmps)(pTHX);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200507# endif
Bram Moolenaare06c1882010-07-21 22:05:20 +0200508
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509/*
510 * Table of name to function pointer of perl.
511 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512static struct {
513 char* name;
514 PERL_PROC* ptr;
515} perl_funcname_table[] = {
516 {"perl_alloc", (PERL_PROC*)&perl_alloc},
517 {"perl_construct", (PERL_PROC*)&perl_construct},
518 {"perl_destruct", (PERL_PROC*)&perl_destruct},
519 {"perl_free", (PERL_PROC*)&perl_free},
520 {"perl_run", (PERL_PROC*)&perl_run},
521 {"perl_parse", (PERL_PROC*)&perl_parse},
Christian Brabandt55460da2023-08-29 21:31:28 +0200522# if defined(WIN32) || ((PERL_REVISION == 5) && (PERL_VERSION < 38))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 {"Perl_get_context", (PERL_PROC*)&Perl_get_context},
Christian Brabandt55460da2023-08-29 21:31:28 +0200524# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 {"Perl_croak", (PERL_PROC*)&Perl_croak},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200526# ifdef PERL5101_OR_LATER
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100527 {"Perl_croak_xs_usage", (PERL_PROC*)&Perl_croak_xs_usage},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200528# endif
529# ifdef PERL_IMPLICIT_CONTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 {"Perl_croak_nocontext", (PERL_PROC*)&Perl_croak_nocontext},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200531# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 {"Perl_dowantarray", (PERL_PROC*)&Perl_dowantarray},
533 {"Perl_free_tmps", (PERL_PROC*)&Perl_free_tmps},
534 {"Perl_gv_stashpv", (PERL_PROC*)&Perl_gv_stashpv},
535 {"Perl_markstack_grow", (PERL_PROC*)&Perl_markstack_grow},
536 {"Perl_mg_find", (PERL_PROC*)&Perl_mg_find},
Bram Moolenaar578333b2018-07-22 07:31:09 +0200537# if (PERL_REVISION == 5) && (PERL_VERSION >= 28)
538 {"Perl_mg_get", (PERL_PROC*)&Perl_mg_get},
539# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 {"Perl_newXS", (PERL_PROC*)&Perl_newXS},
541 {"Perl_newSV", (PERL_PROC*)&Perl_newSV},
542 {"Perl_newSViv", (PERL_PROC*)&Perl_newSViv},
543 {"Perl_newSVpv", (PERL_PROC*)&Perl_newSVpv},
544 {"Perl_call_argv", (PERL_PROC*)&Perl_call_argv},
545 {"Perl_call_pv", (PERL_PROC*)&Perl_call_pv},
546 {"Perl_eval_sv", (PERL_PROC*)&Perl_eval_sv},
547 {"Perl_get_sv", (PERL_PROC*)&Perl_get_sv},
548 {"Perl_eval_pv", (PERL_PROC*)&Perl_eval_pv},
549 {"Perl_call_method", (PERL_PROC*)&Perl_call_method},
550 {"Perl_pop_scope", (PERL_PROC*)&Perl_pop_scope},
551 {"Perl_push_scope", (PERL_PROC*)&Perl_push_scope},
552 {"Perl_save_int", (PERL_PROC*)&Perl_save_int},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200553# if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200554 {"Perl_save_strlen", (PERL_PROC*)&Perl_save_strlen},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200555# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 {"Perl_stack_grow", (PERL_PROC*)&Perl_stack_grow},
557 {"Perl_set_context", (PERL_PROC*)&Perl_set_context},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200558# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200559 {"Perl_sv_2bool_flags", (PERL_PROC*)&Perl_sv_2bool_flags},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200560# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200561 {"Perl_xs_apiversion_bootcheck",(PERL_PROC*)&Perl_xs_apiversion_bootcheck},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200562# endif
563# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 {"Perl_sv_2bool", (PERL_PROC*)&Perl_sv_2bool},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200565# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 {"Perl_sv_2iv", (PERL_PROC*)&Perl_sv_2iv},
567 {"Perl_sv_2mortal", (PERL_PROC*)&Perl_sv_2mortal},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200568# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 {"Perl_sv_2pv_flags", (PERL_PROC*)&Perl_sv_2pv_flags},
570 {"Perl_sv_2pv_nolen", (PERL_PROC*)&Perl_sv_2pv_nolen},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200571# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 {"Perl_sv_2pv", (PERL_PROC*)&Perl_sv_2pv},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200573# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100574 {"Perl_sv_2pvbyte", (PERL_PROC*)&Perl_sv_2pvbyte},
Bram Moolenaar895a7a42020-09-10 21:36:11 +0200575# if (PERL_REVISION == 5) && (PERL_VERSION >= 32)
576 {"Perl_sv_2pvbyte_flags", (PERL_PROC*)&Perl_sv_2pvbyte_flags},
577# endif
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200578# ifdef PERL589_OR_LATER
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000579 {"Perl_sv_2iv_flags", (PERL_PROC*)&Perl_sv_2iv_flags},
580 {"Perl_newXS_flags", (PERL_PROC*)&Perl_newXS_flags},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200581# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 {"Perl_sv_bless", (PERL_PROC*)&Perl_sv_bless},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200583# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 {"Perl_sv_catpvn_flags", (PERL_PROC*)&Perl_sv_catpvn_flags},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200585# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 {"Perl_sv_catpvn", (PERL_PROC*)&Perl_sv_catpvn},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200587# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 {"Perl_sv_free", (PERL_PROC*)&Perl_sv_free},
589 {"Perl_sv_isa", (PERL_PROC*)&Perl_sv_isa},
590 {"Perl_sv_magic", (PERL_PROC*)&Perl_sv_magic},
591 {"Perl_sv_setiv", (PERL_PROC*)&Perl_sv_setiv},
592 {"Perl_sv_setpv", (PERL_PROC*)&Perl_sv_setpv},
593 {"Perl_sv_setpvn", (PERL_PROC*)&Perl_sv_setpvn},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200594# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 {"Perl_sv_setsv_flags", (PERL_PROC*)&Perl_sv_setsv_flags},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200596# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 {"Perl_sv_setsv", (PERL_PROC*)&Perl_sv_setsv},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200598# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 {"Perl_sv_upgrade", (PERL_PROC*)&Perl_sv_upgrade},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200600# if (PERL_REVISION == 5) && (PERL_VERSION < 10)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 {"Perl_Tstack_sp_ptr", (PERL_PROC*)&Perl_Tstack_sp_ptr},
602 {"Perl_Top_ptr", (PERL_PROC*)&Perl_Top_ptr},
603 {"Perl_Tstack_base_ptr", (PERL_PROC*)&Perl_Tstack_base_ptr},
604 {"Perl_Tstack_max_ptr", (PERL_PROC*)&Perl_Tstack_max_ptr},
605 {"Perl_Ttmps_ix_ptr", (PERL_PROC*)&Perl_Ttmps_ix_ptr},
606 {"Perl_Ttmps_floor_ptr", (PERL_PROC*)&Perl_Ttmps_floor_ptr},
607 {"Perl_Tmarkstack_ptr_ptr", (PERL_PROC*)&Perl_Tmarkstack_ptr_ptr},
608 {"Perl_Tmarkstack_max_ptr", (PERL_PROC*)&Perl_Tmarkstack_max_ptr},
609 {"Perl_TSv_ptr", (PERL_PROC*)&Perl_TSv_ptr},
610 {"Perl_TXpv_ptr", (PERL_PROC*)&Perl_TXpv_ptr},
611 {"Perl_Tna_ptr", (PERL_PROC*)&Perl_Tna_ptr},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200612# else
Bram Moolenaarccf22172008-09-01 15:56:45 +0000613 {"Perl_sv_free2", (PERL_PROC*)&Perl_sv_free2},
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000614 {"Perl_sys_init", (PERL_PROC*)&Perl_sys_init},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000615 {"Perl_sys_term", (PERL_PROC*)&Perl_sys_term},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200616 {"Perl_call_list", (PERL_PROC*)&Perl_call_list},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200617# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
618# else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000619 {"Perl_ISv_ptr", (PERL_PROC*)&Perl_ISv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000620 {"Perl_Istack_max_ptr", (PERL_PROC*)&Perl_Istack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200621 {"Perl_Istack_base_ptr", (PERL_PROC*)&Perl_Istack_base_ptr},
622 {"Perl_IXpv_ptr", (PERL_PROC*)&Perl_IXpv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000623 {"Perl_Itmps_ix_ptr", (PERL_PROC*)&Perl_Itmps_ix_ptr},
624 {"Perl_Itmps_floor_ptr", (PERL_PROC*)&Perl_Itmps_floor_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200625 {"Perl_Ina_ptr", (PERL_PROC*)&Perl_Ina_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000626 {"Perl_Imarkstack_ptr_ptr", (PERL_PROC*)&Perl_Imarkstack_ptr_ptr},
627 {"Perl_Imarkstack_max_ptr", (PERL_PROC*)&Perl_Imarkstack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200628 {"Perl_Istack_sp_ptr", (PERL_PROC*)&Perl_Istack_sp_ptr},
629 {"Perl_Iop_ptr", (PERL_PROC*)&Perl_Iop_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000630 {"Perl_Iscopestack_ix_ptr", (PERL_PROC*)&Perl_Iscopestack_ix_ptr},
631 {"Perl_Iunitcheckav_ptr", (PERL_PROC*)&Perl_Iunitcheckav_ptr},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200632# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200633# endif
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200634# if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200635 {"Perl_xs_handshake", (PERL_PROC*)&Perl_xs_handshake},
636 {"Perl_xs_boot_epilog", (PERL_PROC*)&Perl_xs_boot_epilog},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200637# endif
638# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100639# ifdef USE_ITHREADS
Bram Moolenaar01c10522012-09-21 12:50:51 +0200640 {"PL_thr_key", (PERL_PROC*)&dll_PL_thr_key},
Bram Moolenaard8619992014-03-12 17:08:05 +0100641# endif
Yee Cheng Chin7a9d1aa2023-09-01 18:46:17 +0200642# ifdef PERL_USE_THREAD_LOCAL
643 {"PL_current_context", (PERL_PROC*)&dll_PL_current_context},
644# endif
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200645# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 {"Perl_Idefgv_ptr", (PERL_PROC*)&Perl_Idefgv_ptr},
647 {"Perl_Ierrgv_ptr", (PERL_PROC*)&Perl_Ierrgv_ptr},
648 {"Perl_Isv_yes_ptr", (PERL_PROC*)&Perl_Isv_yes_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200649 {"Perl_Gthr_key_ptr", (PERL_PROC*)&Perl_Gthr_key_ptr},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200650# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200651 {"boot_DynaLoader", (PERL_PROC*)&boot_DynaLoader},
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100652 {"Perl_hv_iternext_flags", (PERL_PROC*)&Perl_hv_iternext_flags},
653 {"Perl_hv_iterinit", (PERL_PROC*)&Perl_hv_iterinit},
654 {"Perl_hv_iterkey", (PERL_PROC*)&Perl_hv_iterkey},
655 {"Perl_hv_iterval", (PERL_PROC*)&Perl_hv_iterval},
656 {"Perl_av_fetch", (PERL_PROC*)&Perl_av_fetch},
657 {"Perl_av_len", (PERL_PROC*)&Perl_av_len},
658 {"Perl_sv_2nv_flags", (PERL_PROC*)&Perl_sv_2nv_flags},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200659# if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200660 {"PerlIOBase_pushed", (PERL_PROC*)&PerlIOBase_pushed},
661 {"PerlIO_define_layer", (PERL_PROC*)&PerlIO_define_layer},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200662# endif
663# if (PERL_REVISION == 5) && (PERL_VERSION >= 24)
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200664 {"Perl_savetmps", (PERL_PROC*)&Perl_savetmps},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200665# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 {"", NULL},
667};
668
Christian Brabandt55460da2023-08-29 21:31:28 +0200669# if (PERL_REVISION == 5) && (PERL_VERSION <= 30)
670// In 5.30, GIMME_V requires linking to Perl_block_gimme() instead of being
671// completely inline. Just use the deprecated GIMME for simplicity.
672# undef GIMME_V
673# define GIMME_V GIMME
Bram Moolenaarf5433fb2020-06-21 20:06:54 +0200674# endif
675
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676/*
677 * Make all runtime-links of perl.
678 *
Bram Moolenaar364ab2f2013-08-02 20:05:32 +0200679 * 1. Get module handle using dlopen() or vimLoadLib().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 * 2. Get pointer to perl function by GetProcAddress.
681 * 3. Repeat 2, until get all functions will be used.
682 *
683 * Parameter 'libname' provides name of DLL.
684 * Return OK or FAIL.
685 */
686 static int
687perl_runtime_link_init(char *libname, int verbose)
688{
689 int i;
690
691 if (hPerlLib != NULL)
692 return OK;
Bram Moolenaare06c1882010-07-21 22:05:20 +0200693 if ((hPerlLib = load_dll(libname)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 {
695 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100696 semsg(_("E370: Could not load library %s"), libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 return FAIL;
698 }
699 for (i = 0; perl_funcname_table[i].ptr; ++i)
700 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200701 if (!(*perl_funcname_table[i].ptr = symbol_from_dll(hPerlLib,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 perl_funcname_table[i].name)))
703 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200704 close_dll(hPerlLib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 hPerlLib = NULL;
706 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000707 semsg((const char *)_(e_could_not_load_library_function_str), perl_funcname_table[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 return FAIL;
709 }
710 }
711 return OK;
712}
713
714/*
715 * If runtime-link-perl(DLL) was loaded successfully, return TRUE.
716 * There were no DLL loaded, return FALSE.
717 */
718 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100719perl_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100721 return perl_runtime_link_init((char *)p_perldll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722}
723#endif /* DYNAMIC_PERL */
724
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200725#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
726static void vim_IOLayer_init(void);
727#endif
728
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729/*
730 * perl_init(): initialize perl interpreter
731 * We have to call perl_parse to initialize some structures,
732 * there's nothing to actually parse.
733 */
734 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100735perl_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736{
Bram Moolenaarc236c162008-07-13 17:41:49 +0000737 char *bootargs[] = { "VI", NULL };
738 int argc = 3;
739 static char *argv[] = { "", "-e", "" };
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740
Bram Moolenaarc236c162008-07-13 17:41:49 +0000741#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000742 Perl_sys_init(&argc, (char***)&argv);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 perl_interp = perl_alloc();
745 perl_construct(perl_interp);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000746 perl_parse(perl_interp, xs_init, argc, argv, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 perl_call_argv("VIM::bootstrap", (long)G_DISCARD, bootargs);
748 VIM_init();
749#ifdef USE_SFIO
750 sfdisc(PerlIO_stdout(), sfdcnewvim());
751 sfdisc(PerlIO_stderr(), sfdcnewvim());
752 sfsetbuf(PerlIO_stdout(), NULL, 0);
753 sfsetbuf(PerlIO_stderr(), NULL, 0);
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200754#elif defined(PERLIO_LAYERS)
755 vim_IOLayer_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756#endif
757}
758
759/*
Bram Moolenaar20279732020-03-29 20:51:07 +0200760 * Clean up after ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 */
762 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100763perl_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764{
765 if (perl_interp)
766 {
767 perl_run(perl_interp);
768 perl_destruct(perl_interp);
769 perl_free(perl_interp);
770 perl_interp = NULL;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000771#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100772 Perl_sys_term();
Bram Moolenaarc236c162008-07-13 17:41:49 +0000773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775}
776
777/*
778 * msg_split(): send a message to the message handling routines
779 * split at '\n' first though.
780 */
781 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100782msg_split(
783 char_u *s,
784 int attr) /* highlighting attributes */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785{
786 char *next;
787 char *token = (char *)s;
788
Bram Moolenaaraa8494a2007-10-09 08:47:27 +0000789 while ((next = strchr(token, '\n')) && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 {
791 *next++ = '\0'; /* replace \n with \0 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100792 msg_attr(token, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 token = next;
794 }
Bram Moolenaaraa8494a2007-10-09 08:47:27 +0000795 if (*token && !got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100796 msg_attr(token, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797}
798
799#ifndef FEAT_EVAL
800/*
801 * This stub is needed because an "#ifdef FEAT_EVAL" around Eval() doesn't
802 * work properly.
803 */
804 char_u *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100805eval_to_string(
806 char_u *arg UNUSED,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100807 int convert UNUSED,
808 int use_simple_function UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809{
810 return NULL;
811}
812#endif
813
814/*
815 * Create a new reference to an SV pointing to the SCR structure
Bram Moolenaare344bea2005-09-01 20:46:49 +0000816 * The b_perl_private/w_perl_private part of the SCR structure points to the
817 * SV, so there can only be one such SV for a particular SCR structure. When
818 * the last reference has gone (DESTROY is called),
819 * b_perl_private/w_perl_private is reset; When the screen goes away before
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 * all references are gone, the value of the SV is reset;
821 * any subsequent use of any of those reference will produce
822 * a warning. (see typemap)
823 */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000824
825 static SV *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100826newWINrv(SV *rv, win_T *ptr)
Bram Moolenaare344bea2005-09-01 20:46:49 +0000827{
828 sv_upgrade(rv, SVt_RV);
829 if (ptr->w_perl_private == NULL)
830 {
831 ptr->w_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +0100832 sv_setiv(ptr->w_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +0000833 }
Bram Moolenaar41c363a2018-08-02 21:46:51 +0200834 SvREFCNT_inc_void_NN(ptr->w_perl_private);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000835 SvRV(rv) = ptr->w_perl_private;
836 SvROK_on(rv);
837 return sv_bless(rv, gv_stashpv("VIWIN", TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838}
839
Bram Moolenaare344bea2005-09-01 20:46:49 +0000840 static SV *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100841newBUFrv(SV *rv, buf_T *ptr)
Bram Moolenaare344bea2005-09-01 20:46:49 +0000842{
843 sv_upgrade(rv, SVt_RV);
844 if (ptr->b_perl_private == NULL)
845 {
846 ptr->b_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +0100847 sv_setiv(ptr->b_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +0000848 }
Bram Moolenaar41c363a2018-08-02 21:46:51 +0200849 SvREFCNT_inc_void_NN(ptr->b_perl_private);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000850 SvRV(rv) = ptr->b_perl_private;
851 SvROK_on(rv);
852 return sv_bless(rv, gv_stashpv("VIBUF", TRUE));
853}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200855#if 0
856SV *__sv_save[1024];
857int __sv_save_ix;
858# define D_Save_Sv(sv) do { if (__sv_save_ix < 1024) __sv_save[__sv_save_ix++] = (sv); } while (0)
859#else
860# define D_Save_Sv(sv) NOOP
861#endif
862
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863/*
864 * perl_win_free
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200865 * Remove all references to the window to be destroyed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 */
867 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100868perl_win_free(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869{
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200870 if (wp->w_perl_private && perl_interp != NULL)
871 {
Bram Moolenaar578333b2018-07-22 07:31:09 +0200872 SV *sv = (SV*)wp->w_perl_private;
873 D_Save_Sv(sv);
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200874 sv_setiv(sv, 0);
875 SvREFCNT_dec(sv);
876 }
877 wp->w_perl_private = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878}
879
880 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100881perl_buf_free(buf_T *bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882{
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200883 if (bp->b_perl_private && perl_interp != NULL)
884 {
Bram Moolenaar578333b2018-07-22 07:31:09 +0200885 SV *sv = (SV *)bp->b_perl_private;
886 D_Save_Sv(sv);
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200887 sv_setiv(sv, 0);
888 SvREFCNT_dec(sv);
889 }
890 bp->b_perl_private = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891}
892
893#ifndef PROTO
894# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
895I32 cur_val(pTHX_ IV iv, SV *sv);
896# else
897I32 cur_val(IV iv, SV *sv);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200898# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899
900/*
901 * Handler for the magic variables $main::curwin and $main::curbuf.
902 * The handler is put into the magic vtbl for these variables.
903 * (This is effectively a C-level equivalent of a tied variable).
904 * There is no "set" function as the variables are read-only.
905 */
906# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
907I32 cur_val(pTHX_ IV iv, SV *sv)
908# else
909I32 cur_val(IV iv, SV *sv)
910# endif
911{
912 SV *rv;
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200913
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 if (iv == 0)
915 rv = newWINrv(newSV(0), curwin);
916 else
917 rv = newBUFrv(newSV(0), curbuf);
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200918
Bram Moolenaar41c363a2018-08-02 21:46:51 +0200919 if (SvRV(sv) != SvRV(rv))
920 // XXX: This magic variable is a bit confusing...
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100921 // Is currently refcounted ?
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200922 sv_setsv(sv, rv);
923
Bram Moolenaar41c363a2018-08-02 21:46:51 +0200924 SvREFCNT_dec(rv);
925
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 return 0;
927}
928#endif /* !PROTO */
929
930struct ufuncs cw_funcs = { cur_val, 0, 0 };
931struct ufuncs cb_funcs = { cur_val, 0, 1 };
932
933/*
934 * VIM_init(): Vim-specific initialisation.
935 * Make the magical main::curwin and main::curbuf variables
936 */
937 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100938VIM_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939{
940 static char cw[] = "main::curwin";
941 static char cb[] = "main::curbuf";
942 SV *sv;
943
944 sv = perl_get_sv(cw, TRUE);
945 sv_magic(sv, NULL, 'U', (char *)&cw_funcs, sizeof(cw_funcs));
946 SvREADONLY_on(sv);
947
948 sv = perl_get_sv(cb, TRUE);
949 sv_magic(sv, NULL, 'U', (char *)&cb_funcs, sizeof(cb_funcs));
950 SvREADONLY_on(sv);
951
952 /*
953 * Setup the Safe compartment.
954 * It shouldn't be a fatal error if the Safe module is missing.
955 * XXX: Only shares the 'Msg' routine (which has to be called
956 * like 'Msg(...)').
957 */
958 (void)perl_eval_pv( "if ( eval( 'require Safe' ) ) { $VIM::safe = Safe->new(); $VIM::safe->share_from( 'VIM', ['Msg'] ); }", G_DISCARD | G_VOID );
959
960}
961
962#ifdef DYNAMIC_PERL
963static char *e_noperl = N_("Sorry, this command is disabled: the Perl library could not be loaded.");
964#endif
965
966/*
967 * ":perl"
968 */
969 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100970ex_perl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971{
972 char *err;
973 char *script;
974 STRLEN length;
975 SV *sv;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200976#ifdef HAVE_SANDBOX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 SV *safe;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979
980 script = (char *)script_get(eap, eap->arg);
981 if (eap->skip)
982 {
983 vim_free(script);
984 return;
985 }
986
987 if (perl_interp == NULL)
988 {
989#ifdef DYNAMIC_PERL
990 if (!perl_enabled(TRUE))
991 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100992 emsg(_(e_noperl));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 vim_free(script);
994 return;
995 }
996#endif
997 perl_init();
998 }
999
1000 {
1001 dSP;
1002 ENTER;
1003 SAVETMPS;
1004
1005 if (script == NULL)
1006 sv = newSVpv((char *)eap->arg, 0);
1007 else
1008 {
1009 sv = newSVpv(script, 0);
1010 vim_free(script);
1011 }
1012
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001013 if (sandbox || secure)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 {
Bram Moolenaara1711622011-07-27 14:15:46 +02001015 safe = perl_get_sv("VIM::safe", FALSE);
Bram Moolenaar3f947ea2009-07-14 14:04:54 +00001016# ifndef MAKE_TEST /* avoid a warning for unreachable code */
Bram Moolenaar954e8c52009-11-11 13:45:33 +00001017 if (safe == NULL || !SvTRUE(safe))
Bram Moolenaare96eea72022-01-28 21:00:51 +00001018 emsg(_(e_perl_evaluation_forbidden_in_sandbox_without_safe_module));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 else
Bram Moolenaar3f947ea2009-07-14 14:04:54 +00001020# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 {
1022 PUSHMARK(SP);
1023 XPUSHs(safe);
1024 XPUSHs(sv);
1025 PUTBACK;
1026 perl_call_method("reval", G_DISCARD);
1027 }
1028 }
1029 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
1031
1032 SvREFCNT_dec(sv);
1033
Bram Moolenaar7b61bf12016-06-26 17:16:51 +02001034 err = SvPV(GvSV(PL_errgv), length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035
1036 FREETMPS;
1037 LEAVE;
1038
1039 if (!length)
1040 return;
1041
1042 msg_split((char_u *)err, highlight_attr[HLF_E]);
1043 return;
1044 }
1045}
1046
1047 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001048replace_line(linenr_T *line, linenr_T *end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049{
1050 char *str;
1051
1052 if (SvOK(GvSV(PL_defgv)))
1053 {
1054 str = SvPV(GvSV(PL_defgv), PL_na);
1055 ml_replace(*line, (char_u *)str, 1);
1056 changed_bytes(*line, 0);
1057 }
1058 else
1059 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001060 ml_delete(*line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 deleted_lines_mark(*line, 1L);
1062 --(*end);
1063 --(*line);
1064 }
1065 return OK;
1066}
1067
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001068static struct ref_map_S {
1069 void *vim_ref;
1070 SV *perl_ref;
1071 struct ref_map_S *next;
1072} *ref_map = NULL;
1073
1074 static void
1075ref_map_free(void)
1076{
1077 struct ref_map_S *tofree;
1078 struct ref_map_S *refs = ref_map;
1079
1080 while (refs) {
1081 tofree = refs;
1082 refs = refs->next;
1083 vim_free(tofree);
1084 }
1085 ref_map = NULL;
1086}
1087
1088 static struct ref_map_S *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001089ref_map_find_SV(SV *const sv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001090{
1091 struct ref_map_S *refs = ref_map;
1092 int count = 350;
1093
1094 while (refs) {
1095 if (refs->perl_ref == sv)
1096 break;
1097 refs = refs->next;
1098 count--;
1099 }
1100
1101 if (!refs && count > 0) {
1102 refs = (struct ref_map_S *)alloc(sizeof(struct ref_map_S));
1103 if (!refs)
1104 return NULL;
1105 refs->perl_ref = sv;
1106 refs->vim_ref = NULL;
1107 refs->next = ref_map;
1108 ref_map = refs;
1109 }
1110
1111 return refs;
1112}
1113
1114 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001115perl_to_vim(SV *sv, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001116{
1117 if (SvROK(sv))
1118 sv = SvRV(sv);
1119
1120 switch (SvTYPE(sv)) {
1121 case SVt_NULL:
1122 break;
1123 case SVt_NV: /* float */
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001124 rettv->v_type = VAR_FLOAT;
1125 rettv->vval.v_float = SvNV(sv);
1126 break;
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001127 case SVt_IV: /* integer */
1128 if (!SvROK(sv)) { /* references should be string */
1129 rettv->vval.v_number = SvIV(sv);
1130 break;
1131 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001132 /* FALLTHROUGH */
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001133 case SVt_PV: /* string */
1134 {
1135 size_t len = 0;
1136 char * str_from = SvPV(sv, len);
Bram Moolenaar9b0ac222016-06-01 20:31:43 +02001137 char_u *str_to = (char_u*)alloc(
1138 (unsigned)(sizeof(char_u) * (len + 1)));
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001139
1140 if (str_to) {
1141 str_to[len] = '\0';
1142
1143 while (len--) {
1144 if (str_from[len] == '\0')
1145 str_to[len] = '\n';
1146 else
1147 str_to[len] = str_from[len];
1148 }
1149 }
1150
1151 rettv->v_type = VAR_STRING;
1152 rettv->vval.v_string = str_to;
1153 break;
1154 }
1155 case SVt_PVAV: /* list */
1156 {
1157 SSize_t size;
1158 listitem_T * item;
1159 SV ** item2;
1160 list_T * list;
1161 struct ref_map_S * refs;
1162
1163 if ((refs = ref_map_find_SV(sv)) == NULL)
1164 return FAIL;
1165
1166 if (refs->vim_ref)
1167 list = (list_T *) refs->vim_ref;
1168 else
1169 {
1170 if ((list = list_alloc()) == NULL)
1171 return FAIL;
1172 refs->vim_ref = list;
1173
1174 for (size = av_len((AV*)sv); size >= 0; size--)
1175 {
1176 if ((item = listitem_alloc()) == NULL)
1177 break;
1178
1179 item->li_tv.v_type = VAR_NUMBER;
1180 item->li_tv.v_lock = 0;
1181 item->li_tv.vval.v_number = 0;
1182 list_insert(list, item, list->lv_first);
1183
1184 item2 = av_fetch((AV *)sv, size, 0);
1185
1186 if (item2 == NULL || *item2 == NULL ||
Bram Moolenaard99df422016-01-29 23:20:40 +01001187 perl_to_vim(*item2, &item->li_tv) == FAIL)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001188 break;
1189 }
1190 }
1191
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001192 rettv_list_set(rettv, list);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001193 break;
1194 }
1195 case SVt_PVHV: /* dictionary */
1196 {
1197 HE * entry;
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001198 I32 key_len;
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001199 char * key;
1200 dictitem_T * item;
1201 SV * item2;
1202 dict_T * dict;
1203 struct ref_map_S * refs;
1204
1205 if ((refs = ref_map_find_SV(sv)) == NULL)
1206 return FAIL;
1207
1208 if (refs->vim_ref)
1209 dict = (dict_T *) refs->vim_ref;
1210 else
1211 {
1212
1213 if ((dict = dict_alloc()) == NULL)
1214 return FAIL;
1215 refs->vim_ref = dict;
1216
1217 hv_iterinit((HV *)sv);
1218
1219 for (entry = hv_iternext((HV *)sv); entry; entry = hv_iternext((HV *)sv))
1220 {
1221 key_len = 0;
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001222 key = hv_iterkey(entry, &key_len);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001223
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001224 if (!key || !key_len || strlen(key) < (size_t)key_len) {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001225 semsg("Malformed key Dictionary '%s'", key && *key ? key : "(empty)");
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001226 break;
1227 }
1228
1229 if ((item = dictitem_alloc((char_u *)key)) == NULL)
1230 break;
Bram Moolenaarc89d4b32018-07-08 17:19:02 +02001231 item->di_tv.v_type = VAR_NUMBER;
1232 item->di_tv.vval.v_number = 0;
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001233
1234 if (dict_add(dict, item) == FAIL) {
1235 dictitem_free(item);
1236 break;
1237 }
1238 item2 = hv_iterval((HV *)sv, entry);
1239 if (item2 == NULL || perl_to_vim(item2, &item->di_tv) == FAIL)
1240 break;
1241 }
1242 }
1243
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001244 rettv_dict_set(rettv, dict);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001245 break;
1246 }
1247 default: /* not convertible */
1248 {
1249 char *val = SvPV_nolen(sv);
1250 rettv->v_type = VAR_STRING;
1251 rettv->vval.v_string = val ? vim_strsave((char_u *)val) : NULL;
1252 break;
1253 }
1254 }
1255 return OK;
1256}
1257
1258/*
1259 * "perleval()"
1260 */
1261 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001262do_perleval(char_u *str, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001263{
1264 char *err = NULL;
1265 STRLEN err_len = 0;
1266 SV *sv = NULL;
1267#ifdef HAVE_SANDBOX
1268 SV *safe;
1269#endif
1270
1271 if (perl_interp == NULL)
1272 {
1273#ifdef DYNAMIC_PERL
1274 if (!perl_enabled(TRUE))
1275 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001276 emsg(_(e_noperl));
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001277 return;
1278 }
1279#endif
1280 perl_init();
1281 }
1282
1283 {
1284 dSP;
1285 ENTER;
1286 SAVETMPS;
1287
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001288 if (sandbox || secure)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001289 {
1290 safe = get_sv("VIM::safe", FALSE);
1291# ifndef MAKE_TEST /* avoid a warning for unreachable code */
1292 if (safe == NULL || !SvTRUE(safe))
Bram Moolenaare96eea72022-01-28 21:00:51 +00001293 emsg(_(e_perl_evaluation_forbidden_in_sandbox_without_safe_module));
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001294 else
1295# endif
1296 {
1297 sv = newSVpv((char *)str, 0);
1298 PUSHMARK(SP);
1299 XPUSHs(safe);
1300 XPUSHs(sv);
1301 PUTBACK;
1302 call_method("reval", G_SCALAR);
1303 SPAGAIN;
1304 SvREFCNT_dec(sv);
1305 sv = POPs;
Bram Moolenaar0f267622022-05-10 13:32:24 +01001306 PUTBACK;
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001307 }
1308 }
1309 else
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001310 sv = eval_pv((char *)str, 0);
1311
1312 if (sv) {
1313 perl_to_vim(sv, rettv);
1314 ref_map_free();
Bram Moolenaar7b61bf12016-06-26 17:16:51 +02001315 err = SvPV(GvSV(PL_errgv), err_len);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001316 }
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001317 FREETMPS;
1318 LEAVE;
1319 }
1320 if (err_len)
1321 msg_split((char_u *)err, highlight_attr[HLF_E]);
1322}
1323
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324/*
1325 * ":perldo".
1326 */
1327 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001328ex_perldo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329{
1330 STRLEN length;
1331 SV *sv;
1332 char *str;
1333 linenr_T i;
Bram Moolenaar85b57432017-01-29 22:59:12 +01001334 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001336 if (BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 return;
1338
1339 if (perl_interp == NULL)
1340 {
1341#ifdef DYNAMIC_PERL
1342 if (!perl_enabled(TRUE))
1343 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001344 emsg(_(e_noperl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 return;
1346 }
1347#endif
1348 perl_init();
1349 }
1350 {
1351 dSP;
1352 length = strlen((char *)eap->arg);
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001353 sv = newSV(length + sizeof("sub VIM::perldo {") - 1 + 1);
1354 sv_setpvn(sv, "sub VIM::perldo {", sizeof("sub VIM::perldo {") - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 sv_catpvn(sv, (char *)eap->arg, length);
1356 sv_catpvn(sv, "}", 1);
1357 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
1358 SvREFCNT_dec(sv);
Bram Moolenaar7b61bf12016-06-26 17:16:51 +02001359 str = SvPV(GvSV(PL_errgv), length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 if (length)
1361 goto err;
1362
1363 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
1364 return;
1365
1366 ENTER;
1367 SAVETMPS;
1368 for (i = eap->line1; i <= eap->line2; i++)
1369 {
Bram Moolenaar85b57432017-01-29 22:59:12 +01001370 /* Check the line number, the command my have deleted lines. */
1371 if (i > curbuf->b_ml.ml_line_count)
1372 break;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001373 sv_setpv(GvSV(PL_defgv), (char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 PUSHMARK(sp);
1375 perl_call_pv("VIM::perldo", G_SCALAR | G_EVAL);
Bram Moolenaar7b61bf12016-06-26 17:16:51 +02001376 str = SvPV(GvSV(PL_errgv), length);
Bram Moolenaar85b57432017-01-29 22:59:12 +01001377 if (length || curbuf != was_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 break;
1379 SPAGAIN;
1380 if (SvTRUEx(POPs))
1381 {
1382 if (replace_line(&i, &eap->line2) != OK)
1383 {
1384 PUTBACK;
1385 break;
1386 }
1387 }
1388 PUTBACK;
1389 }
1390 FREETMPS;
1391 LEAVE;
1392 check_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001393 update_screen(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 if (!length)
1395 return;
1396
1397err:
1398 msg_split((char_u *)str, highlight_attr[HLF_E]);
1399 return;
1400 }
1401}
1402
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001403#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
1404typedef struct {
1405 struct _PerlIO base;
1406 int attr;
1407} PerlIOVim;
1408
1409 static IV
1410PerlIOVim_pushed(pTHX_ PerlIO *f, const char *mode,
1411 SV *arg, PerlIO_funcs *tab)
1412{
1413 PerlIOVim *s = PerlIOSelf(f, PerlIOVim);
1414 s->attr = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001415 if (arg && SvPOK(arg))
1416 s->attr = syn_name2attr((char_u *)SvPV_nolen(arg));
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001417 return PerlIOBase_pushed(aTHX_ f, mode, (SV *)NULL, tab);
1418}
1419
1420 static SSize_t
1421PerlIOVim_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1422{
1423 char_u *str;
1424 PerlIOVim * s = PerlIOSelf(f, PerlIOVim);
1425
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001426 str = vim_strnsave((char_u *)vbuf, count);
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001427 if (str == NULL)
1428 return 0;
1429 msg_split((char_u *)str, s->attr);
1430 vim_free(str);
1431
Bram Moolenaar9b0ac222016-06-01 20:31:43 +02001432 return (SSize_t)count;
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001433}
1434
1435static PERLIO_FUNCS_DECL(PerlIO_Vim) = {
1436 sizeof(PerlIO_funcs),
1437 "Vim",
1438 sizeof(PerlIOVim),
1439 PERLIO_K_DUMMY, /* flags */
1440 PerlIOVim_pushed,
1441 NULL, /* popped */
1442 NULL, /* open */
1443 NULL, /* binmode */
1444 NULL, /* arg */
1445 NULL, /* fileno */
1446 NULL, /* dup */
1447 NULL, /* read */
1448 NULL, /* unread */
1449 PerlIOVim_write,
1450 NULL, /* seek */
1451 NULL, /* tell */
1452 NULL, /* close */
1453 NULL, /* flush */
1454 NULL, /* fill */
1455 NULL, /* eof */
1456 NULL, /* error */
1457 NULL, /* clearerr */
1458 NULL, /* setlinebuf */
1459 NULL, /* get_base */
1460 NULL, /* get_bufsiz */
1461 NULL, /* get_ptr */
1462 NULL, /* get_cnt */
1463 NULL /* set_ptrcnt */
1464};
1465
1466/* Use Vim routine for print operator */
1467 static void
1468vim_IOLayer_init(void)
1469{
1470 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_Vim));
1471 (void)eval_pv( "binmode(STDOUT, ':Vim')"
1472 " && binmode(STDERR, ':Vim(ErrorMsg)');", 0);
1473}
1474#endif /* PERLIO_LAYERS && !USE_SFIO */
1475
Christian Brabandt55460da2023-08-29 21:31:28 +02001476#ifdef DYNAMIC_PERL
1477
1478// Certain functionality that we use like SvREFCNT_dec are inlined for
1479// performance reasons. They reference Perl APIs like Perl_sv_free2(), which
1480// would cause linking errors in dynamic builds as we don't link against Perl
1481// during build time. Manually fix it here by redirecting these functions
1482// towards the dynamically loaded version.
1483
1484# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
1485# undef Perl_sv_free2
1486void Perl_sv_free2(pTHX_ SV* sv, const U32 refcnt)
1487{
1488 (*dll_Perl_sv_free2)(aTHX_ sv, refcnt);
1489}
1490# else
1491# undef Perl_sv_free2
1492void Perl_sv_free2(pTHX_ SV* sv)
1493{
1494 (*dll_Perl_sv_free2)(aTHX_ sv);
1495}
1496# endif
1497
1498# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
1499# undef Perl_sv_2bool_flags
1500bool Perl_sv_2bool_flags(pTHX_ SV* sv, I32 flags)
1501{
1502 return (*dll_Perl_sv_2bool_flags)(aTHX_ sv, flags);
1503}
1504# endif
1505
1506# if (PERL_REVISION == 5) && (PERL_VERSION >= 28)
1507# undef Perl_mg_get
1508int Perl_mg_get(pTHX_ SV* sv)
1509{
1510 return (*dll_Perl_mg_get)(aTHX_ sv);
1511}
1512# endif
1513
1514# undef Perl_sv_2nv_flags
1515NV Perl_sv_2nv_flags(pTHX_ SV *const sv, const I32 flags)
1516{
1517 return (*dll_Perl_sv_2nv_flags)(aTHX_ sv, flags);
1518}
1519
1520# ifdef PERL589_OR_LATER
1521# undef Perl_sv_2iv_flags
1522IV Perl_sv_2iv_flags(pTHX_ SV* sv, I32 flags)
1523{
1524 return (*dll_Perl_sv_2iv_flags)(aTHX_ sv, flags);
1525}
1526# endif
1527
Christian Brabandt55460da2023-08-29 21:31:28 +02001528#endif // DYNAMIC_PERL
1529
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530XS(boot_VIM);
1531
1532 static void
1533xs_init(pTHX)
1534{
1535 char *file = __FILE__;
1536
1537 /* DynaLoader is a special case */
1538 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1539 newXS("VIM::bootstrap", boot_VIM, file);
1540}
1541
1542typedef win_T * VIWIN;
1543typedef buf_T * VIBUF;
1544
1545MODULE = VIM PACKAGE = VIM
1546
1547void
1548Msg(text, hl=NULL)
1549 char *text;
1550 char *hl;
1551
1552 PREINIT:
1553 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554
1555 PPCODE:
1556 if (text != NULL)
1557 {
1558 attr = 0;
1559 if (hl != NULL)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001560 attr = syn_name2attr((char_u *)hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 msg_split((char_u *)text, attr);
1562 }
1563
1564void
1565SetOption(line)
1566 char *line;
1567
1568 PPCODE:
1569 if (line != NULL)
1570 do_set((char_u *)line, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001571 update_screen(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572
1573void
1574DoCommand(line)
1575 char *line;
1576
1577 PPCODE:
1578 if (line != NULL)
1579 do_cmdline_cmd((char_u *)line);
1580
1581void
1582Eval(str)
1583 char *str;
1584
1585 PREINIT:
1586 char_u *value;
1587 PPCODE:
Bram Moolenaara4e0b972022-10-01 19:43:52 +01001588 value = eval_to_string((char_u *)str, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 if (value == NULL)
1590 {
1591 XPUSHs(sv_2mortal(newSViv(0)));
1592 XPUSHs(sv_2mortal(newSVpv("", 0)));
1593 }
1594 else
1595 {
1596 XPUSHs(sv_2mortal(newSViv(1)));
1597 XPUSHs(sv_2mortal(newSVpv((char *)value, 0)));
1598 vim_free(value);
1599 }
1600
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001601SV*
1602Blob(SV* sv)
1603 PREINIT:
Bram Moolenaarb1443b42019-01-13 23:51:14 +01001604 STRLEN len;
1605 char *s;
1606 unsigned i;
1607 char buf[3];
1608 SV* newsv;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001609
1610 CODE:
1611 s = SvPVbyte(sv, len);
1612 newsv = newSVpv("0z", 2);
1613 for (i = 0; i < len; i++)
1614 {
Bram Moolenaar2472ae82019-02-23 15:04:17 +01001615 sprintf(buf, "%02X", (unsigned char)(s[i]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001616 sv_catpvn(newsv, buf, 2);
1617 }
1618 RETVAL = newsv;
1619 OUTPUT:
1620 RETVAL
1621
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622void
1623Buffers(...)
1624
1625 PREINIT:
1626 buf_T *vimbuf;
1627 int i, b;
1628
1629 PPCODE:
1630 if (items == 0)
1631 {
Philip H1d7caa52023-06-22 08:55:47 +02001632 if (GIMME_V == G_SCALAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 {
1634 i = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02001635 FOR_ALL_BUFFERS(vimbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 ++i;
1637
1638 XPUSHs(sv_2mortal(newSViv(i)));
1639 }
1640 else
1641 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001642 FOR_ALL_BUFFERS(vimbuf)
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +02001643 XPUSHs(sv_2mortal(newBUFrv(newSV(0), vimbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 }
1645 }
1646 else
1647 {
1648 for (i = 0; i < items; i++)
1649 {
1650 SV *sv = ST(i);
1651 if (SvIOK(sv))
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001652 b = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 else
1654 {
1655 char_u *pat;
1656 STRLEN len;
1657
1658 pat = (char_u *)SvPV(sv, len);
1659 ++emsg_off;
Bram Moolenaar16896a12018-03-06 12:25:56 +01001660 b = buflist_findpat(pat, pat + len, TRUE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 --emsg_off;
1662 }
1663
1664 if (b >= 0)
1665 {
1666 vimbuf = buflist_findnr(b);
1667 if (vimbuf)
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +02001668 XPUSHs(sv_2mortal(newBUFrv(newSV(0), vimbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 }
1670 }
1671 }
1672
1673void
1674Windows(...)
1675
1676 PREINIT:
1677 win_T *vimwin;
1678 int i, w;
1679
1680 PPCODE:
1681 if (items == 0)
1682 {
Philip H1d7caa52023-06-22 08:55:47 +02001683 if (GIMME_V == G_SCALAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 XPUSHs(sv_2mortal(newSViv(win_count())));
1685 else
1686 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001687 FOR_ALL_WINDOWS(vimwin)
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +02001688 XPUSHs(sv_2mortal(newWINrv(newSV(0), vimwin)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 }
1690 }
1691 else
1692 {
1693 for (i = 0; i < items; i++)
1694 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001695 w = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 vimwin = win_find_nr(w);
1697 if (vimwin)
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +02001698 XPUSHs(sv_2mortal(newWINrv(newSV(0), vimwin)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 }
1700 }
1701
1702MODULE = VIM PACKAGE = VIWIN
1703
1704void
1705DESTROY(win)
1706 VIWIN win
1707
1708 CODE:
1709 if (win_valid(win))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001710 win->w_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711
1712SV *
1713Buffer(win)
1714 VIWIN win
1715
1716 CODE:
1717 if (!win_valid(win))
1718 win = curwin;
1719 RETVAL = newBUFrv(newSV(0), win->w_buffer);
1720 OUTPUT:
1721 RETVAL
1722
1723void
1724SetHeight(win, height)
1725 VIWIN win
1726 int height;
1727
1728 PREINIT:
1729 win_T *savewin;
1730
1731 PPCODE:
1732 if (!win_valid(win))
1733 win = curwin;
1734 savewin = curwin;
1735 curwin = win;
1736 win_setheight(height);
1737 curwin = savewin;
1738
1739void
Bram Moolenaar864733a2016-04-02 14:18:01 +02001740Cursor(win, ...)
1741 VIWIN win
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742
1743 PPCODE:
Bram Moolenaara1711622011-07-27 14:15:46 +02001744 if (items == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 {
1746 EXTEND(sp, 2);
1747 if (!win_valid(win))
1748 win = curwin;
1749 PUSHs(sv_2mortal(newSViv(win->w_cursor.lnum)));
1750 PUSHs(sv_2mortal(newSViv(win->w_cursor.col)));
1751 }
Bram Moolenaara1711622011-07-27 14:15:46 +02001752 else if (items == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 {
1754 int lnum, col;
1755
1756 if (!win_valid(win))
1757 win = curwin;
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001758 lnum = (int) SvIV(ST(1));
1759 col = (int) SvIV(ST(2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 win->w_cursor.lnum = lnum;
1761 win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02001762 win->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 check_cursor(); /* put cursor on an existing line */
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001764 update_screen(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 }
1766
1767MODULE = VIM PACKAGE = VIBUF
1768
1769void
1770DESTROY(vimbuf)
1771 VIBUF vimbuf;
1772
1773 CODE:
1774 if (buf_valid(vimbuf))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001775 vimbuf->b_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
1777void
1778Name(vimbuf)
1779 VIBUF vimbuf;
1780
1781 PPCODE:
1782 if (!buf_valid(vimbuf))
1783 vimbuf = curbuf;
1784 /* No file name returns an empty string */
1785 if (vimbuf->b_fname == NULL)
1786 XPUSHs(sv_2mortal(newSVpv("", 0)));
1787 else
1788 XPUSHs(sv_2mortal(newSVpv((char *)vimbuf->b_fname, 0)));
1789
1790void
1791Number(vimbuf)
1792 VIBUF vimbuf;
1793
1794 PPCODE:
1795 if (!buf_valid(vimbuf))
1796 vimbuf = curbuf;
1797 XPUSHs(sv_2mortal(newSViv(vimbuf->b_fnum)));
1798
1799void
1800Count(vimbuf)
1801 VIBUF vimbuf;
1802
1803 PPCODE:
1804 if (!buf_valid(vimbuf))
1805 vimbuf = curbuf;
1806 XPUSHs(sv_2mortal(newSViv(vimbuf->b_ml.ml_line_count)));
1807
1808void
1809Get(vimbuf, ...)
1810 VIBUF vimbuf;
1811
1812 PREINIT:
1813 char_u *line;
1814 int i;
1815 long lnum;
1816 PPCODE:
1817 if (buf_valid(vimbuf))
1818 {
1819 for (i = 1; i < items; i++)
1820 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001821 lnum = (long) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1823 {
1824 line = ml_get_buf(vimbuf, lnum, FALSE);
1825 XPUSHs(sv_2mortal(newSVpv((char *)line, 0)));
1826 }
1827 }
1828 }
1829
1830void
1831Set(vimbuf, ...)
1832 VIBUF vimbuf;
1833
1834 PREINIT:
1835 int i;
1836 long lnum;
1837 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 PPCODE:
1839 if (buf_valid(vimbuf))
1840 {
1841 if (items < 3)
1842 croak("Usage: VIBUF::Set(vimbuf, lnum, @lines)");
1843
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001844 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 for(i = 2; i < items; i++, lnum++)
1846 {
1847 line = SvPV(ST(i),PL_na);
1848 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1849 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001850 aco_save_T aco;
1851
Bram Moolenaare76062c2022-11-28 18:51:43 +00001852 /* Set curwin/curbuf for "vimbuf" and save some things. */
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001853 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00001854 if (curbuf == vimbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00001856 /* Only when a window was found. */
1857 if (u_savesub(lnum) == OK)
1858 {
1859 ml_replace(lnum, (char_u *)line, TRUE);
1860 changed_bytes(lnum, 0);
1861 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001862
Bram Moolenaare76062c2022-11-28 18:51:43 +00001863 /* restore curwin/curbuf and a few other things */
1864 aucmd_restbuf(&aco);
1865 /* Careful: autocommands may have made "vimbuf" invalid! */
1866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 }
1868 }
1869 }
1870
1871void
1872Delete(vimbuf, ...)
1873 VIBUF vimbuf;
1874
1875 PREINIT:
1876 long i, lnum = 0, count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 PPCODE:
1878 if (buf_valid(vimbuf))
1879 {
1880 if (items == 2)
1881 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001882 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 count = 1;
1884 }
1885 else if (items == 3)
1886 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001887 lnum = (long) SvIV(ST(1));
1888 count = (long) 1 + SvIV(ST(2)) - lnum;
Bram Moolenaara1711622011-07-27 14:15:46 +02001889 if (count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 count = 1;
Bram Moolenaara1711622011-07-27 14:15:46 +02001891 if (count < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 {
1893 lnum -= count;
1894 count = -count;
1895 }
1896 }
1897 if (items >= 2)
1898 {
1899 for (i = 0; i < count; i++)
1900 {
1901 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1902 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001903 aco_save_T aco;
1904
1905 /* set curwin/curbuf for "vimbuf" and save some things */
1906 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00001907 if (curbuf == vimbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00001909 /* Only when a window was found. */
1910 if (u_savedel(lnum, 1) == OK)
1911 {
1912 ml_delete(lnum);
1913 check_cursor();
1914 deleted_lines_mark(lnum, 1L);
1915 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001916
Bram Moolenaare76062c2022-11-28 18:51:43 +00001917 /* restore curwin/curbuf and a few other things */
1918 aucmd_restbuf(&aco);
1919 /* Careful: autocommands may have made "vimbuf"
1920 * invalid! */
1921 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001922
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001923 update_curbuf(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 }
1925 }
1926 }
1927 }
1928
1929void
1930Append(vimbuf, ...)
1931 VIBUF vimbuf;
1932
1933 PREINIT:
1934 int i;
1935 long lnum;
1936 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 PPCODE:
1938 if (buf_valid(vimbuf))
1939 {
1940 if (items < 3)
1941 croak("Usage: VIBUF::Append(vimbuf, lnum, @lines)");
1942
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001943 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 for (i = 2; i < items; i++, lnum++)
1945 {
1946 line = SvPV(ST(i),PL_na);
1947 if (lnum >= 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1948 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001949 aco_save_T aco;
1950
1951 /* set curwin/curbuf for "vimbuf" and save some things */
1952 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00001953 if (curbuf == vimbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00001955 /* Only when a window for "vimbuf" was found. */
1956 if (u_inssub(lnum + 1) == OK)
1957 {
1958 ml_append(lnum, (char_u *)line, (colnr_T)0, FALSE);
1959 appended_lines_mark(lnum, 1L);
1960 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001961
Bram Moolenaare76062c2022-11-28 18:51:43 +00001962 /* restore curwin/curbuf and a few other things */
1963 aucmd_restbuf(&aco);
1964 /* Careful: autocommands may have made "vimbuf" invalid! */
1965 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001966
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001967 update_curbuf(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 }
1969 }
1970 }
1971
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001972#ifdef __GNUC__
1973# pragma GCC diagnostic pop
1974#endif