blob: 89aa2db124d71e3dd544331b83be49b6e9dfb88a [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 */
8/*
9 * if_perl.xs: Main code for Perl interface support.
10 * Mostly written by Sven Verdoolaege.
11 */
12
13#define _memory_h /* avoid memset redeclaration */
14#define IN_PERL_FILE /* don't include if_perl.pro from proto.h */
15
Bram Moolenaaraee1f4a2013-08-02 16:10:32 +020016/*
Bram Moolenaar6b107212013-12-11 15:06:40 +010017 * Currently 32-bit version of ActivePerl is built with VC6 (or MinGW since
18 * ActivePerl 5.18).
Bram Moolenaaraee1f4a2013-08-02 16:10:32 +020019 * (http://community.activestate.com/faq/windows-compilers-perl-modules)
20 * It means that time_t should be 32-bit. However the default size of
21 * time_t is 64-bit since VC8. So we have to define _USE_32BIT_TIME_T.
22 */
23#if defined(WIN32) && !defined(_WIN64)
24# define _USE_32BIT_TIME_T
25#endif
26
Bram Moolenaar6b107212013-12-11 15:06:40 +010027/*
28 * Prevent including winsock.h. perl.h tries to detect whether winsock.h is
29 * already included before including winsock2.h, because winsock2.h isn't
30 * compatible with winsock.h. However the detection doesn't work with some
31 * versions of MinGW. If WIN32_LEAN_AND_MEAN is defined, windows.h will not
32 * include winsock.h.
33 */
34#ifdef WIN32
35# define WIN32_LEAN_AND_MEAN
36#endif
37
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#include "vim.h"
39
Bram Moolenaar7c0daf02013-12-14 11:46:08 +010040/* Work around for perl-5.18.
41 * Don't include "perl\lib\CORE\inline.h" for now,
42 * include it after Perl_sv_free2 is defined. */
43#ifdef DYNAMIC_PERL
44# define PERL_NO_INLINE_FUNCTIONS
45#endif
46
Bram Moolenaar207fd752013-12-14 11:50:35 +010047/* Work around for using MSVC and ActivePerl 5.18. */
48#ifdef _MSC_VER
49# define __inline__ __inline
50#endif
51
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010052#ifdef __GNUC__
53# pragma GCC diagnostic push
54# pragma GCC diagnostic ignored "-Wunused-variable"
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010055#endif
56
Bram Moolenaaraee1f4a2013-08-02 16:10:32 +020057#include <EXTERN.h>
58#include <perl.h>
59#include <XSUB.h>
Bram Moolenaar6244a0f2016-04-14 14:09:25 +020060#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
61# include <perliol.h>
62#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000063
Bram Moolenaarcf190c62016-06-02 11:54:06 +020064/* Workaround for perl < 5.8.7 */
65#ifndef PERLIO_FUNCS_DECL
66# ifdef PERLIO_FUNCS_CONST
67# define PERLIO_FUNCS_DECL(funcs) const PerlIO_funcs funcs
68# define PERLIO_FUNCS_CAST(funcs) (PerlIO_funcs*)(funcs)
69# else
70# define PERLIO_FUNCS_DECL(funcs) PerlIO_funcs funcs
71# define PERLIO_FUNCS_CAST(funcs) (funcs)
72# endif
73#endif
Bram Moolenaarc4bc0e62016-06-02 13:54:49 +020074#ifndef SvREFCNT_inc_void_NN
75# define SvREFCNT_inc_void_NN SvREFCNT_inc
76#endif
Bram Moolenaarcf190c62016-06-02 11:54:06 +020077
Bram Moolenaar071d4272004-06-13 20:20:40 +000078/*
79 * Work around clashes between Perl and Vim namespace. proto.h doesn't
80 * include if_perl.pro and perlsfio.pro when IN_PERL_FILE is defined, because
81 * we need the CV typedef. proto.h can't be moved to after including
82 * if_perl.h, because we get all sorts of name clashes then.
83 */
84#ifndef PROTO
85#ifndef __MINGW32__
86# include "proto/if_perl.pro"
87# include "proto/if_perlsfio.pro"
88#endif
89#endif
90
91/* Perl compatibility stuff. This should ensure compatibility with older
92 * versions of Perl.
93 */
94
95#ifndef PERL_VERSION
96# include <patchlevel.h>
97# define PERL_REVISION 5
98# define PERL_VERSION PATCHLEVEL
99# define PERL_SUBVERSION SUBVERSION
100#endif
101
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000102/*
103 * Quoting Jan Dubois of Active State:
104 * ActivePerl build 822 still identifies itself as 5.8.8 but already
105 * contains many of the changes from the upcoming Perl 5.8.9 release.
106 *
107 * The changes include addition of two symbols (Perl_sv_2iv_flags,
108 * Perl_newXS_flags) not present in earlier releases.
109 *
Bram Moolenaar3b9b13e2007-09-15 12:49:35 +0000110 * Jan Dubois suggested the following guarding scheme.
111 *
112 * Active State defined ACTIVEPERL_VERSION as a string in versions before
113 * 5.8.8; and so the comparison to 822 below needs to be guarded.
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000114 */
Bram Moolenaar3b9b13e2007-09-15 12:49:35 +0000115#if (PERL_REVISION == 5) && (PERL_VERSION == 8) && (PERL_SUBVERSION >= 8)
116# if (ACTIVEPERL_VERSION >= 822) || (PERL_SUBVERSION >= 9)
117# define PERL589_OR_LATER
118# endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000119#endif
120#if (PERL_REVISION == 5) && (PERL_VERSION >= 9)
121# define PERL589_OR_LATER
122#endif
123
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100124#if (PERL_REVISION == 5) && ((PERL_VERSION > 10) || \
125 (PERL_VERSION == 10) && (PERL_SUBVERSION >= 1))
126# define PERL5101_OR_LATER
127#endif
128
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#ifndef pTHX
130# define pTHX void
131# define pTHX_
132#endif
133
134#ifndef EXTERN_C
135# define EXTERN_C
136#endif
137
Bram Moolenaarc271c482012-08-08 13:17:31 +0200138#if (PERL_REVISION == 5) && (PERL_VERSION >= 14) && defined(_MSC_VER)
139/* Using PL_errgv to get the error message after perl_eval_sv() causes a crash
140 * with MSVC and Perl version 5.14. */
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100141# define CHECK_EVAL_ERR(len) SvPV(perl_get_sv("@", GV_ADD), (len));
142#else
143# define CHECK_EVAL_ERR(len) SvPV(GvSV(PL_errgv), (len));
Bram Moolenaarc271c482012-08-08 13:17:31 +0200144#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
158#ifndef DYNAMIC_PERL /* just generating prototypes */
Bram Moolenaar766fb0d2010-07-22 11:34:16 +0200159#ifdef WIN3264
Bram Moolenaare06c1882010-07-21 22:05:20 +0200160typedef int HANDLE;
161#endif
162typedef int XSINIT_t;
163typedef int XSUBADDR_t;
Bram Moolenaard8619992014-03-12 17:08:05 +0100164#endif
165#ifndef USE_ITHREADS
Bram Moolenaare06c1882010-07-21 22:05:20 +0200166typedef int perl_key;
167#endif
168
Bram Moolenaar766fb0d2010-07-22 11:34:16 +0200169#ifndef WIN3264
Bram Moolenaare06c1882010-07-21 22:05:20 +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
176#else
177#define PERL_PROC FARPROC
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200178#define load_dll vimLoadLib
Bram Moolenaare06c1882010-07-21 22:05:20 +0200179#define symbol_from_dll GetProcAddress
180#define close_dll FreeLibrary
181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182/*
183 * Wrapper defines
184 */
185# define perl_alloc dll_perl_alloc
186# define perl_construct dll_perl_construct
187# define perl_parse dll_perl_parse
188# define perl_run dll_perl_run
189# define perl_destruct dll_perl_destruct
190# define perl_free dll_perl_free
191# define Perl_get_context dll_Perl_get_context
192# define Perl_croak dll_Perl_croak
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100193# ifdef PERL5101_OR_LATER
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100194# define Perl_croak_xs_usage dll_Perl_croak_xs_usage
195# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196# ifndef PROTO
197# define Perl_croak_nocontext dll_Perl_croak_nocontext
198# define Perl_call_argv dll_Perl_call_argv
199# define Perl_call_pv dll_Perl_call_pv
200# define Perl_eval_sv dll_Perl_eval_sv
201# define Perl_get_sv dll_Perl_get_sv
202# define Perl_eval_pv dll_Perl_eval_pv
203# define Perl_call_method dll_Perl_call_method
204# endif
205# define Perl_dowantarray dll_Perl_dowantarray
206# define Perl_free_tmps dll_Perl_free_tmps
207# define Perl_gv_stashpv dll_Perl_gv_stashpv
208# define Perl_markstack_grow dll_Perl_markstack_grow
209# define Perl_mg_find dll_Perl_mg_find
210# define Perl_newXS dll_Perl_newXS
211# define Perl_newSV dll_Perl_newSV
212# define Perl_newSViv dll_Perl_newSViv
213# define Perl_newSVpv dll_Perl_newSVpv
214# define Perl_pop_scope dll_Perl_pop_scope
215# define Perl_push_scope dll_Perl_push_scope
216# define Perl_save_int dll_Perl_save_int
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200217# if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
218# define Perl_save_strlen dll_Perl_save_strlen
219# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220# define Perl_stack_grow dll_Perl_stack_grow
221# define Perl_set_context dll_Perl_set_context
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200222# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200223# define Perl_sv_2bool_flags dll_Perl_sv_2bool_flags
224# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
225# define Perl_xs_apiversion_bootcheck dll_Perl_xs_apiversion_bootcheck
226# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200227# else
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200228# define Perl_sv_2bool dll_Perl_sv_2bool
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200229# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230# define Perl_sv_2iv dll_Perl_sv_2iv
231# define Perl_sv_2mortal dll_Perl_sv_2mortal
232# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
233# define Perl_sv_2pv_flags dll_Perl_sv_2pv_flags
234# define Perl_sv_2pv_nolen dll_Perl_sv_2pv_nolen
235# else
236# define Perl_sv_2pv dll_Perl_sv_2pv
237# endif
238# define Perl_sv_bless dll_Perl_sv_bless
239# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
240# define Perl_sv_catpvn_flags dll_Perl_sv_catpvn_flags
241# else
242# define Perl_sv_catpvn dll_Perl_sv_catpvn
243# endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000244#ifdef PERL589_OR_LATER
245# define Perl_sv_2iv_flags dll_Perl_sv_2iv_flags
246# define Perl_newXS_flags dll_Perl_newXS_flags
247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248# define Perl_sv_free dll_Perl_sv_free
Bram Moolenaarccf22172008-09-01 15:56:45 +0000249# if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
250# define Perl_sv_free2 dll_Perl_sv_free2
251# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252# define Perl_sv_isa dll_Perl_sv_isa
253# define Perl_sv_magic dll_Perl_sv_magic
254# define Perl_sv_setiv dll_Perl_sv_setiv
255# define Perl_sv_setpv dll_Perl_sv_setpv
256# define Perl_sv_setpvn dll_Perl_sv_setpvn
257# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
258# define Perl_sv_setsv_flags dll_Perl_sv_setsv_flags
259# else
260# define Perl_sv_setsv dll_Perl_sv_setsv
261# endif
262# define Perl_sv_upgrade dll_Perl_sv_upgrade
263# define Perl_Tstack_sp_ptr dll_Perl_Tstack_sp_ptr
264# define Perl_Top_ptr dll_Perl_Top_ptr
265# define Perl_Tstack_base_ptr dll_Perl_Tstack_base_ptr
266# define Perl_Tstack_max_ptr dll_Perl_Tstack_max_ptr
267# define Perl_Ttmps_ix_ptr dll_Perl_Ttmps_ix_ptr
268# define Perl_Ttmps_floor_ptr dll_Perl_Ttmps_floor_ptr
269# define Perl_Tmarkstack_ptr_ptr dll_Perl_Tmarkstack_ptr_ptr
270# define Perl_Tmarkstack_max_ptr dll_Perl_Tmarkstack_max_ptr
271# define Perl_TSv_ptr dll_Perl_TSv_ptr
272# define Perl_TXpv_ptr dll_Perl_TXpv_ptr
273# define Perl_Tna_ptr dll_Perl_Tna_ptr
274# define Perl_Idefgv_ptr dll_Perl_Idefgv_ptr
275# define Perl_Ierrgv_ptr dll_Perl_Ierrgv_ptr
276# define Perl_Isv_yes_ptr dll_Perl_Isv_yes_ptr
277# define boot_DynaLoader dll_boot_DynaLoader
Bram Moolenaare06c1882010-07-21 22:05:20 +0200278# define Perl_Gthr_key_ptr dll_Perl_Gthr_key_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000280# define Perl_sys_init dll_Perl_sys_init
Bram Moolenaarc236c162008-07-13 17:41:49 +0000281# define Perl_sys_term dll_Perl_sys_term
282# define Perl_ISv_ptr dll_Perl_ISv_ptr
283# define Perl_Istack_max_ptr dll_Perl_Istack_max_ptr
284# define Perl_Istack_base_ptr dll_Perl_Istack_base_ptr
285# define Perl_Itmps_ix_ptr dll_Perl_Itmps_ix_ptr
286# define Perl_Itmps_floor_ptr dll_Perl_Itmps_floor_ptr
287# define Perl_IXpv_ptr dll_Perl_IXpv_ptr
288# define Perl_Ina_ptr dll_Perl_Ina_ptr
289# define Perl_Imarkstack_ptr_ptr dll_Perl_Imarkstack_ptr_ptr
290# define Perl_Imarkstack_max_ptr dll_Perl_Imarkstack_max_ptr
291# define Perl_Istack_sp_ptr dll_Perl_Istack_sp_ptr
292# define Perl_Iop_ptr dll_Perl_Iop_ptr
293# define Perl_call_list dll_Perl_call_list
294# define Perl_Iscopestack_ix_ptr dll_Perl_Iscopestack_ix_ptr
295# define Perl_Iunitcheckav_ptr dll_Perl_Iunitcheckav_ptr
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200296# if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
297# define Perl_xs_handshake dll_Perl_xs_handshake
298# define Perl_xs_boot_epilog dll_Perl_xs_boot_epilog
299# endif
Bram Moolenaar01c10522012-09-21 12:50:51 +0200300# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100301# ifdef USE_ITHREADS
302# define PL_thr_key *dll_PL_thr_key
303# endif
Bram Moolenaar01c10522012-09-21 12:50:51 +0200304# endif
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100305# define Perl_hv_iternext_flags dll_Perl_hv_iternext_flags
306# define Perl_hv_iterinit dll_Perl_hv_iterinit
307# define Perl_hv_iterkey dll_Perl_hv_iterkey
308# define Perl_hv_iterval dll_Perl_hv_iterval
309# define Perl_av_fetch dll_Perl_av_fetch
310# define Perl_av_len dll_Perl_av_len
311# define Perl_sv_2nv_flags dll_Perl_sv_2nv_flags
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200312# if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
313# define PerlIOBase_pushed dll_PerlIOBase_pushed
314# define PerlIO_define_layer dll_PerlIO_define_layer
315# endif
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200316# if (PERL_REVISION == 5) && (PERL_VERSION >= 24)
317# define Perl_savetmps dll_Perl_savetmps
318# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000319
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320/*
321 * Declare HANDLE for perl.dll and function pointers.
322 */
323static HANDLE hPerlLib = NULL;
324
325static PerlInterpreter* (*perl_alloc)();
326static void (*perl_construct)(PerlInterpreter*);
327static void (*perl_destruct)(PerlInterpreter*);
328static void (*perl_free)(PerlInterpreter*);
329static int (*perl_run)(PerlInterpreter*);
330static int (*perl_parse)(PerlInterpreter*, XSINIT_t, int, char**, char**);
331static void* (*Perl_get_context)(void);
Bram Moolenaar864733a2016-04-02 14:18:01 +0200332static void (*Perl_croak)(pTHX_ const char*, ...) __attribute__noreturn__;
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100333#ifdef PERL5101_OR_LATER
Bram Moolenaar6b107212013-12-11 15:06:40 +0100334/* Perl-5.18 has a different Perl_croak_xs_usage signature. */
335# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
Bram Moolenaar864733a2016-04-02 14:18:01 +0200336static void (*Perl_croak_xs_usage)(const CV *const, const char *const params)
337 __attribute__noreturn__;
Bram Moolenaar6b107212013-12-11 15:06:40 +0100338# else
Bram Moolenaar864733a2016-04-02 14:18:01 +0200339static void (*Perl_croak_xs_usage)(pTHX_ const CV *const, const char *const params)
340 __attribute__noreturn__;
Bram Moolenaar6b107212013-12-11 15:06:40 +0100341# endif
Bram Moolenaar9be6e212013-06-15 16:47:35 +0200342#endif
Bram Moolenaar864733a2016-04-02 14:18:01 +0200343static void (*Perl_croak_nocontext)(const char*, ...) __attribute__noreturn__;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344static I32 (*Perl_dowantarray)(pTHX);
345static void (*Perl_free_tmps)(pTHX);
346static HV* (*Perl_gv_stashpv)(pTHX_ const char*, I32);
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200347#if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
348static I32* (*Perl_markstack_grow)(pTHX);
349#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350static void (*Perl_markstack_grow)(pTHX);
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352static MAGIC* (*Perl_mg_find)(pTHX_ SV*, int);
353static CV* (*Perl_newXS)(pTHX_ char*, XSUBADDR_t, char*);
354static SV* (*Perl_newSV)(pTHX_ STRLEN);
355static SV* (*Perl_newSViv)(pTHX_ IV);
356static SV* (*Perl_newSVpv)(pTHX_ const char*, STRLEN);
357static I32 (*Perl_call_argv)(pTHX_ const char*, I32, char**);
358static I32 (*Perl_call_pv)(pTHX_ const char*, I32);
359static I32 (*Perl_eval_sv)(pTHX_ SV*, I32);
360static SV* (*Perl_get_sv)(pTHX_ const char*, I32);
361static SV* (*Perl_eval_pv)(pTHX_ const char*, I32);
362static SV* (*Perl_call_method)(pTHX_ const char*, I32);
363static void (*Perl_pop_scope)(pTHX);
364static void (*Perl_push_scope)(pTHX);
365static void (*Perl_save_int)(pTHX_ int*);
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200366#if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
367static void (*Perl_save_strlen)(pTHX_ STRLEN* ptr);
368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369static SV** (*Perl_stack_grow)(pTHX_ SV**, SV**p, int);
370static SV** (*Perl_set_context)(void*);
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200371#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
372static bool (*Perl_sv_2bool_flags)(pTHX_ SV*, I32);
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200373# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200374static void (*Perl_xs_apiversion_bootcheck)(pTHX_ SV *module, const char *api_p, STRLEN api_len);
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200375# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200376#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377static bool (*Perl_sv_2bool)(pTHX_ SV*);
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379static IV (*Perl_sv_2iv)(pTHX_ SV*);
380static SV* (*Perl_sv_2mortal)(pTHX_ SV*);
381#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
382static char* (*Perl_sv_2pv_flags)(pTHX_ SV*, STRLEN*, I32);
383static char* (*Perl_sv_2pv_nolen)(pTHX_ SV*);
384#else
385static char* (*Perl_sv_2pv)(pTHX_ SV*, STRLEN*);
386#endif
387static SV* (*Perl_sv_bless)(pTHX_ SV*, HV*);
388#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
389static void (*Perl_sv_catpvn_flags)(pTHX_ SV* , const char*, STRLEN, I32);
390#else
391static void (*Perl_sv_catpvn)(pTHX_ SV*, const char*, STRLEN);
392#endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000393#ifdef PERL589_OR_LATER
394static IV (*Perl_sv_2iv_flags)(pTHX_ SV* sv, I32 flags);
395static CV * (*Perl_newXS_flags)(pTHX_ const char *name, XSUBADDR_t subaddr, const char *const filename, const char *const proto, U32 flags);
396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397static void (*Perl_sv_free)(pTHX_ SV*);
398static int (*Perl_sv_isa)(pTHX_ SV*, const char*);
399static void (*Perl_sv_magic)(pTHX_ SV*, SV*, int, const char*, I32);
400static void (*Perl_sv_setiv)(pTHX_ SV*, IV);
401static void (*Perl_sv_setpv)(pTHX_ SV*, const char*);
402static void (*Perl_sv_setpvn)(pTHX_ SV*, const char*, STRLEN);
403#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
404static void (*Perl_sv_setsv_flags)(pTHX_ SV*, SV*, I32);
405#else
406static void (*Perl_sv_setsv)(pTHX_ SV*, SV*);
407#endif
408static bool (*Perl_sv_upgrade)(pTHX_ SV*, U32);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200409#if (PERL_REVISION == 5) && (PERL_VERSION < 10)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410static SV*** (*Perl_Tstack_sp_ptr)(register PerlInterpreter*);
411static OP** (*Perl_Top_ptr)(register PerlInterpreter*);
412static SV*** (*Perl_Tstack_base_ptr)(register PerlInterpreter*);
413static SV*** (*Perl_Tstack_max_ptr)(register PerlInterpreter*);
414static I32* (*Perl_Ttmps_ix_ptr)(register PerlInterpreter*);
415static I32* (*Perl_Ttmps_floor_ptr)(register PerlInterpreter*);
416static I32** (*Perl_Tmarkstack_ptr_ptr)(register PerlInterpreter*);
417static I32** (*Perl_Tmarkstack_max_ptr)(register PerlInterpreter*);
418static SV** (*Perl_TSv_ptr)(register PerlInterpreter*);
419static XPV** (*Perl_TXpv_ptr)(register PerlInterpreter*);
420static STRLEN* (*Perl_Tna_ptr)(register PerlInterpreter*);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200421#else
Bram Moolenaar6b107212013-12-11 15:06:40 +0100422/* Perl-5.18 has a different Perl_sv_free2 signature. */
423# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
424static void (*Perl_sv_free2)(pTHX_ SV*, const U32);
425# else
Bram Moolenaarccf22172008-09-01 15:56:45 +0000426static void (*Perl_sv_free2)(pTHX_ SV*);
Bram Moolenaar6b107212013-12-11 15:06:40 +0100427# endif
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000428static void (*Perl_sys_init)(int* argc, char*** argv);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000429static void (*Perl_sys_term)(void);
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200430static void (*Perl_call_list)(pTHX_ I32, AV*);
431# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
432# else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000433static SV** (*Perl_ISv_ptr)(register PerlInterpreter*);
434static SV*** (*Perl_Istack_max_ptr)(register PerlInterpreter*);
435static SV*** (*Perl_Istack_base_ptr)(register PerlInterpreter*);
436static XPV** (*Perl_IXpv_ptr)(register PerlInterpreter*);
437static I32* (*Perl_Itmps_ix_ptr)(register PerlInterpreter*);
438static I32* (*Perl_Itmps_floor_ptr)(register PerlInterpreter*);
439static STRLEN* (*Perl_Ina_ptr)(register PerlInterpreter*);
440static I32** (*Perl_Imarkstack_ptr_ptr)(register PerlInterpreter*);
441static I32** (*Perl_Imarkstack_max_ptr)(register PerlInterpreter*);
442static SV*** (*Perl_Istack_sp_ptr)(register PerlInterpreter*);
443static OP** (*Perl_Iop_ptr)(register PerlInterpreter*);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000444static I32* (*Perl_Iscopestack_ix_ptr)(register PerlInterpreter*);
445static AV** (*Perl_Iunitcheckav_ptr)(register PerlInterpreter*);
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200446# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000447#endif
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200448#if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
449static I32 (*Perl_xs_handshake)(const U32, void *, const char *, ...);
450static void (*Perl_xs_boot_epilog)(pTHX_ const U32);
451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200453#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100454# ifdef USE_ITHREADS
Bram Moolenaar01c10522012-09-21 12:50:51 +0200455static perl_key* dll_PL_thr_key;
Bram Moolenaard8619992014-03-12 17:08:05 +0100456# endif
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200457#else
Bram Moolenaare06c1882010-07-21 22:05:20 +0200458static GV** (*Perl_Idefgv_ptr)(register PerlInterpreter*);
459static GV** (*Perl_Ierrgv_ptr)(register PerlInterpreter*);
460static SV* (*Perl_Isv_yes_ptr)(register PerlInterpreter*);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200461static perl_key* (*Perl_Gthr_key_ptr)_((pTHX));
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200462#endif
463static void (*boot_DynaLoader)_((pTHX_ CV*));
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100464static HE * (*Perl_hv_iternext_flags)(pTHX_ HV *, I32);
465static I32 (*Perl_hv_iterinit)(pTHX_ HV *);
466static char * (*Perl_hv_iterkey)(pTHX_ HE *, I32 *);
467static SV * (*Perl_hv_iterval)(pTHX_ HV *, HE *);
468static SV** (*Perl_av_fetch)(pTHX_ AV *, SSize_t, I32);
469static SSize_t (*Perl_av_len)(pTHX_ AV *);
470static NV (*Perl_sv_2nv_flags)(pTHX_ SV *const, const I32);
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200471#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
472static IV (*PerlIOBase_pushed)(pTHX_ PerlIO *, const char *, SV *, PerlIO_funcs *);
473static void (*PerlIO_define_layer)(pTHX_ PerlIO_funcs *);
474#endif
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200475#if (PERL_REVISION == 5) && (PERL_VERSION >= 24)
476static void (*Perl_savetmps)(pTHX);
477#endif
Bram Moolenaare06c1882010-07-21 22:05:20 +0200478
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479/*
480 * Table of name to function pointer of perl.
481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482static struct {
483 char* name;
484 PERL_PROC* ptr;
485} perl_funcname_table[] = {
486 {"perl_alloc", (PERL_PROC*)&perl_alloc},
487 {"perl_construct", (PERL_PROC*)&perl_construct},
488 {"perl_destruct", (PERL_PROC*)&perl_destruct},
489 {"perl_free", (PERL_PROC*)&perl_free},
490 {"perl_run", (PERL_PROC*)&perl_run},
491 {"perl_parse", (PERL_PROC*)&perl_parse},
492 {"Perl_get_context", (PERL_PROC*)&Perl_get_context},
493 {"Perl_croak", (PERL_PROC*)&Perl_croak},
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100494#ifdef PERL5101_OR_LATER
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100495 {"Perl_croak_xs_usage", (PERL_PROC*)&Perl_croak_xs_usage},
496#endif
Bram Moolenaard8619992014-03-12 17:08:05 +0100497#ifdef PERL_IMPLICIT_CONTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 {"Perl_croak_nocontext", (PERL_PROC*)&Perl_croak_nocontext},
Bram Moolenaard8619992014-03-12 17:08:05 +0100499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 {"Perl_dowantarray", (PERL_PROC*)&Perl_dowantarray},
501 {"Perl_free_tmps", (PERL_PROC*)&Perl_free_tmps},
502 {"Perl_gv_stashpv", (PERL_PROC*)&Perl_gv_stashpv},
503 {"Perl_markstack_grow", (PERL_PROC*)&Perl_markstack_grow},
504 {"Perl_mg_find", (PERL_PROC*)&Perl_mg_find},
505 {"Perl_newXS", (PERL_PROC*)&Perl_newXS},
506 {"Perl_newSV", (PERL_PROC*)&Perl_newSV},
507 {"Perl_newSViv", (PERL_PROC*)&Perl_newSViv},
508 {"Perl_newSVpv", (PERL_PROC*)&Perl_newSVpv},
509 {"Perl_call_argv", (PERL_PROC*)&Perl_call_argv},
510 {"Perl_call_pv", (PERL_PROC*)&Perl_call_pv},
511 {"Perl_eval_sv", (PERL_PROC*)&Perl_eval_sv},
512 {"Perl_get_sv", (PERL_PROC*)&Perl_get_sv},
513 {"Perl_eval_pv", (PERL_PROC*)&Perl_eval_pv},
514 {"Perl_call_method", (PERL_PROC*)&Perl_call_method},
515 {"Perl_pop_scope", (PERL_PROC*)&Perl_pop_scope},
516 {"Perl_push_scope", (PERL_PROC*)&Perl_push_scope},
517 {"Perl_save_int", (PERL_PROC*)&Perl_save_int},
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200518#if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
519 {"Perl_save_strlen", (PERL_PROC*)&Perl_save_strlen},
520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 {"Perl_stack_grow", (PERL_PROC*)&Perl_stack_grow},
522 {"Perl_set_context", (PERL_PROC*)&Perl_set_context},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200523#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
524 {"Perl_sv_2bool_flags", (PERL_PROC*)&Perl_sv_2bool_flags},
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200525# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200526 {"Perl_xs_apiversion_bootcheck",(PERL_PROC*)&Perl_xs_apiversion_bootcheck},
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200527# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200528#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {"Perl_sv_2bool", (PERL_PROC*)&Perl_sv_2bool},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 {"Perl_sv_2iv", (PERL_PROC*)&Perl_sv_2iv},
532 {"Perl_sv_2mortal", (PERL_PROC*)&Perl_sv_2mortal},
533#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
534 {"Perl_sv_2pv_flags", (PERL_PROC*)&Perl_sv_2pv_flags},
535 {"Perl_sv_2pv_nolen", (PERL_PROC*)&Perl_sv_2pv_nolen},
536#else
537 {"Perl_sv_2pv", (PERL_PROC*)&Perl_sv_2pv},
538#endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000539#ifdef PERL589_OR_LATER
540 {"Perl_sv_2iv_flags", (PERL_PROC*)&Perl_sv_2iv_flags},
541 {"Perl_newXS_flags", (PERL_PROC*)&Perl_newXS_flags},
542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 {"Perl_sv_bless", (PERL_PROC*)&Perl_sv_bless},
544#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
545 {"Perl_sv_catpvn_flags", (PERL_PROC*)&Perl_sv_catpvn_flags},
546#else
547 {"Perl_sv_catpvn", (PERL_PROC*)&Perl_sv_catpvn},
548#endif
549 {"Perl_sv_free", (PERL_PROC*)&Perl_sv_free},
550 {"Perl_sv_isa", (PERL_PROC*)&Perl_sv_isa},
551 {"Perl_sv_magic", (PERL_PROC*)&Perl_sv_magic},
552 {"Perl_sv_setiv", (PERL_PROC*)&Perl_sv_setiv},
553 {"Perl_sv_setpv", (PERL_PROC*)&Perl_sv_setpv},
554 {"Perl_sv_setpvn", (PERL_PROC*)&Perl_sv_setpvn},
555#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
556 {"Perl_sv_setsv_flags", (PERL_PROC*)&Perl_sv_setsv_flags},
557#else
558 {"Perl_sv_setsv", (PERL_PROC*)&Perl_sv_setsv},
559#endif
560 {"Perl_sv_upgrade", (PERL_PROC*)&Perl_sv_upgrade},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000561#if (PERL_REVISION == 5) && (PERL_VERSION < 10)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 {"Perl_Tstack_sp_ptr", (PERL_PROC*)&Perl_Tstack_sp_ptr},
563 {"Perl_Top_ptr", (PERL_PROC*)&Perl_Top_ptr},
564 {"Perl_Tstack_base_ptr", (PERL_PROC*)&Perl_Tstack_base_ptr},
565 {"Perl_Tstack_max_ptr", (PERL_PROC*)&Perl_Tstack_max_ptr},
566 {"Perl_Ttmps_ix_ptr", (PERL_PROC*)&Perl_Ttmps_ix_ptr},
567 {"Perl_Ttmps_floor_ptr", (PERL_PROC*)&Perl_Ttmps_floor_ptr},
568 {"Perl_Tmarkstack_ptr_ptr", (PERL_PROC*)&Perl_Tmarkstack_ptr_ptr},
569 {"Perl_Tmarkstack_max_ptr", (PERL_PROC*)&Perl_Tmarkstack_max_ptr},
570 {"Perl_TSv_ptr", (PERL_PROC*)&Perl_TSv_ptr},
571 {"Perl_TXpv_ptr", (PERL_PROC*)&Perl_TXpv_ptr},
572 {"Perl_Tna_ptr", (PERL_PROC*)&Perl_Tna_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000573#else
Bram Moolenaarccf22172008-09-01 15:56:45 +0000574 {"Perl_sv_free2", (PERL_PROC*)&Perl_sv_free2},
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000575 {"Perl_sys_init", (PERL_PROC*)&Perl_sys_init},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000576 {"Perl_sys_term", (PERL_PROC*)&Perl_sys_term},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200577 {"Perl_call_list", (PERL_PROC*)&Perl_call_list},
578# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
579# else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000580 {"Perl_ISv_ptr", (PERL_PROC*)&Perl_ISv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000581 {"Perl_Istack_max_ptr", (PERL_PROC*)&Perl_Istack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200582 {"Perl_Istack_base_ptr", (PERL_PROC*)&Perl_Istack_base_ptr},
583 {"Perl_IXpv_ptr", (PERL_PROC*)&Perl_IXpv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000584 {"Perl_Itmps_ix_ptr", (PERL_PROC*)&Perl_Itmps_ix_ptr},
585 {"Perl_Itmps_floor_ptr", (PERL_PROC*)&Perl_Itmps_floor_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200586 {"Perl_Ina_ptr", (PERL_PROC*)&Perl_Ina_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000587 {"Perl_Imarkstack_ptr_ptr", (PERL_PROC*)&Perl_Imarkstack_ptr_ptr},
588 {"Perl_Imarkstack_max_ptr", (PERL_PROC*)&Perl_Imarkstack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200589 {"Perl_Istack_sp_ptr", (PERL_PROC*)&Perl_Istack_sp_ptr},
590 {"Perl_Iop_ptr", (PERL_PROC*)&Perl_Iop_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000591 {"Perl_Iscopestack_ix_ptr", (PERL_PROC*)&Perl_Iscopestack_ix_ptr},
592 {"Perl_Iunitcheckav_ptr", (PERL_PROC*)&Perl_Iunitcheckav_ptr},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200593# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000594#endif
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200595#if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
596 {"Perl_xs_handshake", (PERL_PROC*)&Perl_xs_handshake},
597 {"Perl_xs_boot_epilog", (PERL_PROC*)&Perl_xs_boot_epilog},
598#endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200599#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100600# ifdef USE_ITHREADS
Bram Moolenaar01c10522012-09-21 12:50:51 +0200601 {"PL_thr_key", (PERL_PROC*)&dll_PL_thr_key},
Bram Moolenaard8619992014-03-12 17:08:05 +0100602# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200603#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 {"Perl_Idefgv_ptr", (PERL_PROC*)&Perl_Idefgv_ptr},
605 {"Perl_Ierrgv_ptr", (PERL_PROC*)&Perl_Ierrgv_ptr},
606 {"Perl_Isv_yes_ptr", (PERL_PROC*)&Perl_Isv_yes_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200607 {"Perl_Gthr_key_ptr", (PERL_PROC*)&Perl_Gthr_key_ptr},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200608#endif
609 {"boot_DynaLoader", (PERL_PROC*)&boot_DynaLoader},
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100610 {"Perl_hv_iternext_flags", (PERL_PROC*)&Perl_hv_iternext_flags},
611 {"Perl_hv_iterinit", (PERL_PROC*)&Perl_hv_iterinit},
612 {"Perl_hv_iterkey", (PERL_PROC*)&Perl_hv_iterkey},
613 {"Perl_hv_iterval", (PERL_PROC*)&Perl_hv_iterval},
614 {"Perl_av_fetch", (PERL_PROC*)&Perl_av_fetch},
615 {"Perl_av_len", (PERL_PROC*)&Perl_av_len},
616 {"Perl_sv_2nv_flags", (PERL_PROC*)&Perl_sv_2nv_flags},
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200617#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
618 {"PerlIOBase_pushed", (PERL_PROC*)&PerlIOBase_pushed},
619 {"PerlIO_define_layer", (PERL_PROC*)&PerlIO_define_layer},
620#endif
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200621#if (PERL_REVISION == 5) && (PERL_VERSION >= 24)
622 {"Perl_savetmps", (PERL_PROC*)&Perl_savetmps},
623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 {"", NULL},
625};
626
Bram Moolenaar6b107212013-12-11 15:06:40 +0100627/* Work around for perl-5.18.
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200628 * For now, only the definitions of S_SvREFCNT_dec are needed in
629 * "perl\lib\CORE\inline.h". */
Bram Moolenaar6b107212013-12-11 15:06:40 +0100630#if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200631static void
632S_SvREFCNT_dec(pTHX_ SV *sv)
633{
634 if (LIKELY(sv != NULL)) {
635 U32 rc = SvREFCNT(sv);
636 if (LIKELY(rc > 1))
637 SvREFCNT(sv) = rc - 1;
638 else
639 Perl_sv_free2(aTHX_ sv, rc);
640 }
641}
Bram Moolenaar6b107212013-12-11 15:06:40 +0100642#endif
643
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644/*
645 * Make all runtime-links of perl.
646 *
Bram Moolenaar364ab2f2013-08-02 20:05:32 +0200647 * 1. Get module handle using dlopen() or vimLoadLib().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 * 2. Get pointer to perl function by GetProcAddress.
649 * 3. Repeat 2, until get all functions will be used.
650 *
651 * Parameter 'libname' provides name of DLL.
652 * Return OK or FAIL.
653 */
654 static int
655perl_runtime_link_init(char *libname, int verbose)
656{
657 int i;
658
659 if (hPerlLib != NULL)
660 return OK;
Bram Moolenaare06c1882010-07-21 22:05:20 +0200661 if ((hPerlLib = load_dll(libname)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 {
663 if (verbose)
664 EMSG2(_("E370: Could not load library %s"), libname);
665 return FAIL;
666 }
667 for (i = 0; perl_funcname_table[i].ptr; ++i)
668 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200669 if (!(*perl_funcname_table[i].ptr = symbol_from_dll(hPerlLib,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 perl_funcname_table[i].name)))
671 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200672 close_dll(hPerlLib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 hPerlLib = NULL;
674 if (verbose)
675 EMSG2(_(e_loadfunc), perl_funcname_table[i].name);
676 return FAIL;
677 }
678 }
679 return OK;
680}
681
682/*
683 * If runtime-link-perl(DLL) was loaded successfully, return TRUE.
684 * There were no DLL loaded, return FALSE.
685 */
686 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100687perl_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100689 return perl_runtime_link_init((char *)p_perldll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690}
691#endif /* DYNAMIC_PERL */
692
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200693#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
694static void vim_IOLayer_init(void);
695#endif
696
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697/*
698 * perl_init(): initialize perl interpreter
699 * We have to call perl_parse to initialize some structures,
700 * there's nothing to actually parse.
701 */
702 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100703perl_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704{
Bram Moolenaarc236c162008-07-13 17:41:49 +0000705 char *bootargs[] = { "VI", NULL };
706 int argc = 3;
707 static char *argv[] = { "", "-e", "" };
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708
Bram Moolenaarc236c162008-07-13 17:41:49 +0000709#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000710 Perl_sys_init(&argc, (char***)&argv);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 perl_interp = perl_alloc();
713 perl_construct(perl_interp);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000714 perl_parse(perl_interp, xs_init, argc, argv, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 perl_call_argv("VIM::bootstrap", (long)G_DISCARD, bootargs);
716 VIM_init();
717#ifdef USE_SFIO
718 sfdisc(PerlIO_stdout(), sfdcnewvim());
719 sfdisc(PerlIO_stderr(), sfdcnewvim());
720 sfsetbuf(PerlIO_stdout(), NULL, 0);
721 sfsetbuf(PerlIO_stderr(), NULL, 0);
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200722#elif defined(PERLIO_LAYERS)
723 vim_IOLayer_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724#endif
725}
726
727/*
728 * perl_end(): clean up after ourselves
729 */
730 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100731perl_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732{
733 if (perl_interp)
734 {
735 perl_run(perl_interp);
736 perl_destruct(perl_interp);
737 perl_free(perl_interp);
738 perl_interp = NULL;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000739#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100740 Perl_sys_term();
Bram Moolenaarc236c162008-07-13 17:41:49 +0000741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 }
743#ifdef DYNAMIC_PERL
744 if (hPerlLib)
745 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200746 close_dll(hPerlLib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 hPerlLib = NULL;
748 }
749#endif
750}
751
752/*
753 * msg_split(): send a message to the message handling routines
754 * split at '\n' first though.
755 */
756 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100757msg_split(
758 char_u *s,
759 int attr) /* highlighting attributes */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760{
761 char *next;
762 char *token = (char *)s;
763
Bram Moolenaaraa8494a2007-10-09 08:47:27 +0000764 while ((next = strchr(token, '\n')) && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 {
766 *next++ = '\0'; /* replace \n with \0 */
767 msg_attr((char_u *)token, attr);
768 token = next;
769 }
Bram Moolenaaraa8494a2007-10-09 08:47:27 +0000770 if (*token && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 msg_attr((char_u *)token, attr);
772}
773
774#ifndef FEAT_EVAL
775/*
776 * This stub is needed because an "#ifdef FEAT_EVAL" around Eval() doesn't
777 * work properly.
778 */
779 char_u *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100780eval_to_string(
781 char_u *arg UNUSED,
782 char_u **nextcmd UNUSED,
783 int dolist UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784{
785 return NULL;
786}
787#endif
788
789/*
790 * Create a new reference to an SV pointing to the SCR structure
Bram Moolenaare344bea2005-09-01 20:46:49 +0000791 * The b_perl_private/w_perl_private part of the SCR structure points to the
792 * SV, so there can only be one such SV for a particular SCR structure. When
793 * the last reference has gone (DESTROY is called),
794 * b_perl_private/w_perl_private is reset; When the screen goes away before
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 * all references are gone, the value of the SV is reset;
796 * any subsequent use of any of those reference will produce
797 * a warning. (see typemap)
798 */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000799
800 static SV *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100801newWINrv(SV *rv, win_T *ptr)
Bram Moolenaare344bea2005-09-01 20:46:49 +0000802{
803 sv_upgrade(rv, SVt_RV);
804 if (ptr->w_perl_private == NULL)
805 {
806 ptr->w_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +0100807 sv_setiv(ptr->w_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +0000808 }
809 else
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200810 SvREFCNT_inc_void_NN(ptr->w_perl_private);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000811 SvRV(rv) = ptr->w_perl_private;
812 SvROK_on(rv);
813 return sv_bless(rv, gv_stashpv("VIWIN", TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814}
815
Bram Moolenaare344bea2005-09-01 20:46:49 +0000816 static SV *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100817newBUFrv(SV *rv, buf_T *ptr)
Bram Moolenaare344bea2005-09-01 20:46:49 +0000818{
819 sv_upgrade(rv, SVt_RV);
820 if (ptr->b_perl_private == NULL)
821 {
822 ptr->b_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +0100823 sv_setiv(ptr->b_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +0000824 }
825 else
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200826 SvREFCNT_inc_void_NN(ptr->b_perl_private);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000827 SvRV(rv) = ptr->b_perl_private;
828 SvROK_on(rv);
829 return sv_bless(rv, gv_stashpv("VIBUF", TRUE));
830}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831
832/*
833 * perl_win_free
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200834 * Remove all references to the window to be destroyed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 */
836 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100837perl_win_free(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000839 if (wp->w_perl_private)
840 sv_setiv((SV *)wp->w_perl_private, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 return;
842}
843
844 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100845perl_buf_free(buf_T *bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000847 if (bp->b_perl_private)
848 sv_setiv((SV *)bp->b_perl_private, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 return;
850}
851
852#ifndef PROTO
853# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
854I32 cur_val(pTHX_ IV iv, SV *sv);
855# else
856I32 cur_val(IV iv, SV *sv);
857#endif
858
859/*
860 * Handler for the magic variables $main::curwin and $main::curbuf.
861 * The handler is put into the magic vtbl for these variables.
862 * (This is effectively a C-level equivalent of a tied variable).
863 * There is no "set" function as the variables are read-only.
864 */
865# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
866I32 cur_val(pTHX_ IV iv, SV *sv)
867# else
868I32 cur_val(IV iv, SV *sv)
869# endif
870{
871 SV *rv;
872 if (iv == 0)
873 rv = newWINrv(newSV(0), curwin);
874 else
875 rv = newBUFrv(newSV(0), curbuf);
876 sv_setsv(sv, rv);
Bram Moolenaar95509e12016-04-15 21:16:11 +0200877 SvREFCNT_dec(SvRV(rv));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 return 0;
879}
880#endif /* !PROTO */
881
882struct ufuncs cw_funcs = { cur_val, 0, 0 };
883struct ufuncs cb_funcs = { cur_val, 0, 1 };
884
885/*
886 * VIM_init(): Vim-specific initialisation.
887 * Make the magical main::curwin and main::curbuf variables
888 */
889 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100890VIM_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891{
892 static char cw[] = "main::curwin";
893 static char cb[] = "main::curbuf";
894 SV *sv;
895
896 sv = perl_get_sv(cw, TRUE);
897 sv_magic(sv, NULL, 'U', (char *)&cw_funcs, sizeof(cw_funcs));
898 SvREADONLY_on(sv);
899
900 sv = perl_get_sv(cb, TRUE);
901 sv_magic(sv, NULL, 'U', (char *)&cb_funcs, sizeof(cb_funcs));
902 SvREADONLY_on(sv);
903
904 /*
905 * Setup the Safe compartment.
906 * It shouldn't be a fatal error if the Safe module is missing.
907 * XXX: Only shares the 'Msg' routine (which has to be called
908 * like 'Msg(...)').
909 */
910 (void)perl_eval_pv( "if ( eval( 'require Safe' ) ) { $VIM::safe = Safe->new(); $VIM::safe->share_from( 'VIM', ['Msg'] ); }", G_DISCARD | G_VOID );
911
912}
913
914#ifdef DYNAMIC_PERL
915static char *e_noperl = N_("Sorry, this command is disabled: the Perl library could not be loaded.");
916#endif
917
918/*
919 * ":perl"
920 */
921 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100922ex_perl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923{
924 char *err;
925 char *script;
926 STRLEN length;
927 SV *sv;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200928#ifdef HAVE_SANDBOX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 SV *safe;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931
932 script = (char *)script_get(eap, eap->arg);
933 if (eap->skip)
934 {
935 vim_free(script);
936 return;
937 }
938
939 if (perl_interp == NULL)
940 {
941#ifdef DYNAMIC_PERL
942 if (!perl_enabled(TRUE))
943 {
944 EMSG(_(e_noperl));
945 vim_free(script);
946 return;
947 }
948#endif
949 perl_init();
950 }
951
952 {
953 dSP;
954 ENTER;
955 SAVETMPS;
956
957 if (script == NULL)
958 sv = newSVpv((char *)eap->arg, 0);
959 else
960 {
961 sv = newSVpv(script, 0);
962 vim_free(script);
963 }
964
965#ifdef HAVE_SANDBOX
966 if (sandbox)
967 {
Bram Moolenaara1711622011-07-27 14:15:46 +0200968 safe = perl_get_sv("VIM::safe", FALSE);
Bram Moolenaar3f947ea2009-07-14 14:04:54 +0000969# ifndef MAKE_TEST /* avoid a warning for unreachable code */
Bram Moolenaar954e8c52009-11-11 13:45:33 +0000970 if (safe == NULL || !SvTRUE(safe))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 EMSG(_("E299: Perl evaluation forbidden in sandbox without the Safe module"));
972 else
Bram Moolenaar3f947ea2009-07-14 14:04:54 +0000973# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 {
975 PUSHMARK(SP);
976 XPUSHs(safe);
977 XPUSHs(sv);
978 PUTBACK;
979 perl_call_method("reval", G_DISCARD);
980 }
981 }
982 else
983#endif
984 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
985
986 SvREFCNT_dec(sv);
987
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100988 err = CHECK_EVAL_ERR(length);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989
990 FREETMPS;
991 LEAVE;
992
993 if (!length)
994 return;
995
996 msg_split((char_u *)err, highlight_attr[HLF_E]);
997 return;
998 }
999}
1000
1001 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001002replace_line(linenr_T *line, linenr_T *end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003{
1004 char *str;
1005
1006 if (SvOK(GvSV(PL_defgv)))
1007 {
1008 str = SvPV(GvSV(PL_defgv), PL_na);
1009 ml_replace(*line, (char_u *)str, 1);
1010 changed_bytes(*line, 0);
1011 }
1012 else
1013 {
1014 ml_delete(*line, FALSE);
1015 deleted_lines_mark(*line, 1L);
1016 --(*end);
1017 --(*line);
1018 }
1019 return OK;
1020}
1021
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001022static struct ref_map_S {
1023 void *vim_ref;
1024 SV *perl_ref;
1025 struct ref_map_S *next;
1026} *ref_map = NULL;
1027
1028 static void
1029ref_map_free(void)
1030{
1031 struct ref_map_S *tofree;
1032 struct ref_map_S *refs = ref_map;
1033
1034 while (refs) {
1035 tofree = refs;
1036 refs = refs->next;
1037 vim_free(tofree);
1038 }
1039 ref_map = NULL;
1040}
1041
1042 static struct ref_map_S *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001043ref_map_find_SV(SV *const sv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001044{
1045 struct ref_map_S *refs = ref_map;
1046 int count = 350;
1047
1048 while (refs) {
1049 if (refs->perl_ref == sv)
1050 break;
1051 refs = refs->next;
1052 count--;
1053 }
1054
1055 if (!refs && count > 0) {
1056 refs = (struct ref_map_S *)alloc(sizeof(struct ref_map_S));
1057 if (!refs)
1058 return NULL;
1059 refs->perl_ref = sv;
1060 refs->vim_ref = NULL;
1061 refs->next = ref_map;
1062 ref_map = refs;
1063 }
1064
1065 return refs;
1066}
1067
1068 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001069perl_to_vim(SV *sv, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001070{
1071 if (SvROK(sv))
1072 sv = SvRV(sv);
1073
1074 switch (SvTYPE(sv)) {
1075 case SVt_NULL:
1076 break;
1077 case SVt_NV: /* float */
1078#ifdef FEAT_FLOAT
1079 rettv->v_type = VAR_FLOAT;
1080 rettv->vval.v_float = SvNV(sv);
1081 break;
1082#endif
1083 case SVt_IV: /* integer */
1084 if (!SvROK(sv)) { /* references should be string */
1085 rettv->vval.v_number = SvIV(sv);
1086 break;
1087 }
1088 case SVt_PV: /* string */
1089 {
1090 size_t len = 0;
1091 char * str_from = SvPV(sv, len);
Bram Moolenaar9b0ac222016-06-01 20:31:43 +02001092 char_u *str_to = (char_u*)alloc(
1093 (unsigned)(sizeof(char_u) * (len + 1)));
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001094
1095 if (str_to) {
1096 str_to[len] = '\0';
1097
1098 while (len--) {
1099 if (str_from[len] == '\0')
1100 str_to[len] = '\n';
1101 else
1102 str_to[len] = str_from[len];
1103 }
1104 }
1105
1106 rettv->v_type = VAR_STRING;
1107 rettv->vval.v_string = str_to;
1108 break;
1109 }
1110 case SVt_PVAV: /* list */
1111 {
1112 SSize_t size;
1113 listitem_T * item;
1114 SV ** item2;
1115 list_T * list;
1116 struct ref_map_S * refs;
1117
1118 if ((refs = ref_map_find_SV(sv)) == NULL)
1119 return FAIL;
1120
1121 if (refs->vim_ref)
1122 list = (list_T *) refs->vim_ref;
1123 else
1124 {
1125 if ((list = list_alloc()) == NULL)
1126 return FAIL;
1127 refs->vim_ref = list;
1128
1129 for (size = av_len((AV*)sv); size >= 0; size--)
1130 {
1131 if ((item = listitem_alloc()) == NULL)
1132 break;
1133
1134 item->li_tv.v_type = VAR_NUMBER;
1135 item->li_tv.v_lock = 0;
1136 item->li_tv.vval.v_number = 0;
1137 list_insert(list, item, list->lv_first);
1138
1139 item2 = av_fetch((AV *)sv, size, 0);
1140
1141 if (item2 == NULL || *item2 == NULL ||
Bram Moolenaard99df422016-01-29 23:20:40 +01001142 perl_to_vim(*item2, &item->li_tv) == FAIL)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001143 break;
1144 }
1145 }
1146
1147 list->lv_refcount++;
1148 rettv->v_type = VAR_LIST;
1149 rettv->vval.v_list = list;
1150 break;
1151 }
1152 case SVt_PVHV: /* dictionary */
1153 {
1154 HE * entry;
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001155 I32 key_len;
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001156 char * key;
1157 dictitem_T * item;
1158 SV * item2;
1159 dict_T * dict;
1160 struct ref_map_S * refs;
1161
1162 if ((refs = ref_map_find_SV(sv)) == NULL)
1163 return FAIL;
1164
1165 if (refs->vim_ref)
1166 dict = (dict_T *) refs->vim_ref;
1167 else
1168 {
1169
1170 if ((dict = dict_alloc()) == NULL)
1171 return FAIL;
1172 refs->vim_ref = dict;
1173
1174 hv_iterinit((HV *)sv);
1175
1176 for (entry = hv_iternext((HV *)sv); entry; entry = hv_iternext((HV *)sv))
1177 {
1178 key_len = 0;
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001179 key = hv_iterkey(entry, &key_len);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001180
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001181 if (!key || !key_len || strlen(key) < (size_t)key_len) {
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001182 EMSG2("Malformed key Dictionary '%s'", key && *key ? key : "(empty)");
1183 break;
1184 }
1185
1186 if ((item = dictitem_alloc((char_u *)key)) == NULL)
1187 break;
1188
1189 item->di_tv.v_type = VAR_NUMBER;
1190 item->di_tv.v_lock = 0;
1191 item->di_tv.vval.v_number = 0;
1192
1193 if (dict_add(dict, item) == FAIL) {
1194 dictitem_free(item);
1195 break;
1196 }
1197 item2 = hv_iterval((HV *)sv, entry);
1198 if (item2 == NULL || perl_to_vim(item2, &item->di_tv) == FAIL)
1199 break;
1200 }
1201 }
1202
1203 dict->dv_refcount++;
1204 rettv->v_type = VAR_DICT;
1205 rettv->vval.v_dict = dict;
1206 break;
1207 }
1208 default: /* not convertible */
1209 {
1210 char *val = SvPV_nolen(sv);
1211 rettv->v_type = VAR_STRING;
1212 rettv->vval.v_string = val ? vim_strsave((char_u *)val) : NULL;
1213 break;
1214 }
1215 }
1216 return OK;
1217}
1218
1219/*
1220 * "perleval()"
1221 */
1222 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001223do_perleval(char_u *str, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001224{
1225 char *err = NULL;
1226 STRLEN err_len = 0;
1227 SV *sv = NULL;
1228#ifdef HAVE_SANDBOX
1229 SV *safe;
1230#endif
1231
1232 if (perl_interp == NULL)
1233 {
1234#ifdef DYNAMIC_PERL
1235 if (!perl_enabled(TRUE))
1236 {
1237 EMSG(_(e_noperl));
1238 return;
1239 }
1240#endif
1241 perl_init();
1242 }
1243
1244 {
1245 dSP;
1246 ENTER;
1247 SAVETMPS;
1248
1249#ifdef HAVE_SANDBOX
1250 if (sandbox)
1251 {
1252 safe = get_sv("VIM::safe", FALSE);
1253# ifndef MAKE_TEST /* avoid a warning for unreachable code */
1254 if (safe == NULL || !SvTRUE(safe))
1255 EMSG(_("E299: Perl evaluation forbidden in sandbox without the Safe module"));
1256 else
1257# endif
1258 {
1259 sv = newSVpv((char *)str, 0);
1260 PUSHMARK(SP);
1261 XPUSHs(safe);
1262 XPUSHs(sv);
1263 PUTBACK;
1264 call_method("reval", G_SCALAR);
1265 SPAGAIN;
1266 SvREFCNT_dec(sv);
1267 sv = POPs;
1268 }
1269 }
1270 else
1271#endif /* HAVE_SANDBOX */
1272 sv = eval_pv((char *)str, 0);
1273
1274 if (sv) {
1275 perl_to_vim(sv, rettv);
1276 ref_map_free();
1277 err = CHECK_EVAL_ERR(err_len);
1278 }
1279 PUTBACK;
1280 FREETMPS;
1281 LEAVE;
1282 }
1283 if (err_len)
1284 msg_split((char_u *)err, highlight_attr[HLF_E]);
1285}
1286
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287/*
1288 * ":perldo".
1289 */
1290 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001291ex_perldo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292{
1293 STRLEN length;
1294 SV *sv;
1295 char *str;
1296 linenr_T i;
1297
1298 if (bufempty())
1299 return;
1300
1301 if (perl_interp == NULL)
1302 {
1303#ifdef DYNAMIC_PERL
1304 if (!perl_enabled(TRUE))
1305 {
1306 EMSG(_(e_noperl));
1307 return;
1308 }
1309#endif
1310 perl_init();
1311 }
1312 {
1313 dSP;
1314 length = strlen((char *)eap->arg);
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001315 sv = newSV(length + sizeof("sub VIM::perldo {") - 1 + 1);
1316 sv_setpvn(sv, "sub VIM::perldo {", sizeof("sub VIM::perldo {") - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 sv_catpvn(sv, (char *)eap->arg, length);
1318 sv_catpvn(sv, "}", 1);
1319 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
1320 SvREFCNT_dec(sv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001321 str = CHECK_EVAL_ERR(length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 if (length)
1323 goto err;
1324
1325 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
1326 return;
1327
1328 ENTER;
1329 SAVETMPS;
1330 for (i = eap->line1; i <= eap->line2; i++)
1331 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001332 sv_setpv(GvSV(PL_defgv), (char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 PUSHMARK(sp);
1334 perl_call_pv("VIM::perldo", G_SCALAR | G_EVAL);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001335 str = CHECK_EVAL_ERR(length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 if (length)
1337 break;
1338 SPAGAIN;
1339 if (SvTRUEx(POPs))
1340 {
1341 if (replace_line(&i, &eap->line2) != OK)
1342 {
1343 PUTBACK;
1344 break;
1345 }
1346 }
1347 PUTBACK;
1348 }
1349 FREETMPS;
1350 LEAVE;
1351 check_cursor();
1352 update_screen(NOT_VALID);
1353 if (!length)
1354 return;
1355
1356err:
1357 msg_split((char_u *)str, highlight_attr[HLF_E]);
1358 return;
1359 }
1360}
1361
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001362#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
1363typedef struct {
1364 struct _PerlIO base;
1365 int attr;
1366} PerlIOVim;
1367
1368 static IV
1369PerlIOVim_pushed(pTHX_ PerlIO *f, const char *mode,
1370 SV *arg, PerlIO_funcs *tab)
1371{
1372 PerlIOVim *s = PerlIOSelf(f, PerlIOVim);
1373 s->attr = 0;
1374 if (arg && SvPOK(arg)) {
1375 int id = syn_name2id((char_u *)SvPV_nolen(arg));
1376 if (id != 0)
1377 s->attr = syn_id2attr(id);
1378 }
1379 return PerlIOBase_pushed(aTHX_ f, mode, (SV *)NULL, tab);
1380}
1381
1382 static SSize_t
1383PerlIOVim_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1384{
1385 char_u *str;
1386 PerlIOVim * s = PerlIOSelf(f, PerlIOVim);
1387
Bram Moolenaar9b0ac222016-06-01 20:31:43 +02001388 str = vim_strnsave((char_u *)vbuf, (int)count);
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001389 if (str == NULL)
1390 return 0;
1391 msg_split((char_u *)str, s->attr);
1392 vim_free(str);
1393
Bram Moolenaar9b0ac222016-06-01 20:31:43 +02001394 return (SSize_t)count;
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001395}
1396
1397static PERLIO_FUNCS_DECL(PerlIO_Vim) = {
1398 sizeof(PerlIO_funcs),
1399 "Vim",
1400 sizeof(PerlIOVim),
1401 PERLIO_K_DUMMY, /* flags */
1402 PerlIOVim_pushed,
1403 NULL, /* popped */
1404 NULL, /* open */
1405 NULL, /* binmode */
1406 NULL, /* arg */
1407 NULL, /* fileno */
1408 NULL, /* dup */
1409 NULL, /* read */
1410 NULL, /* unread */
1411 PerlIOVim_write,
1412 NULL, /* seek */
1413 NULL, /* tell */
1414 NULL, /* close */
1415 NULL, /* flush */
1416 NULL, /* fill */
1417 NULL, /* eof */
1418 NULL, /* error */
1419 NULL, /* clearerr */
1420 NULL, /* setlinebuf */
1421 NULL, /* get_base */
1422 NULL, /* get_bufsiz */
1423 NULL, /* get_ptr */
1424 NULL, /* get_cnt */
1425 NULL /* set_ptrcnt */
1426};
1427
1428/* Use Vim routine for print operator */
1429 static void
1430vim_IOLayer_init(void)
1431{
1432 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_Vim));
1433 (void)eval_pv( "binmode(STDOUT, ':Vim')"
1434 " && binmode(STDERR, ':Vim(ErrorMsg)');", 0);
1435}
1436#endif /* PERLIO_LAYERS && !USE_SFIO */
1437
Bram Moolenaar01dd60c2008-07-24 14:24:48 +00001438#ifndef FEAT_WINDOWS
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001439 int
1440win_valid(win_T *w)
1441{
1442 return TRUE;
1443}
1444 int
1445win_count(void)
1446{
1447 return 1;
1448}
1449 win_T *
1450win_find_nr(int n)
1451{
1452 return curwin;
1453}
Bram Moolenaar01dd60c2008-07-24 14:24:48 +00001454#endif
1455
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456XS(boot_VIM);
1457
1458 static void
1459xs_init(pTHX)
1460{
1461 char *file = __FILE__;
1462
1463 /* DynaLoader is a special case */
1464 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1465 newXS("VIM::bootstrap", boot_VIM, file);
1466}
1467
1468typedef win_T * VIWIN;
1469typedef buf_T * VIBUF;
1470
1471MODULE = VIM PACKAGE = VIM
1472
1473void
1474Msg(text, hl=NULL)
1475 char *text;
1476 char *hl;
1477
1478 PREINIT:
1479 int attr;
1480 int id;
1481
1482 PPCODE:
1483 if (text != NULL)
1484 {
1485 attr = 0;
1486 if (hl != NULL)
1487 {
1488 id = syn_name2id((char_u *)hl);
1489 if (id != 0)
1490 attr = syn_id2attr(id);
1491 }
1492 msg_split((char_u *)text, attr);
1493 }
1494
1495void
1496SetOption(line)
1497 char *line;
1498
1499 PPCODE:
1500 if (line != NULL)
1501 do_set((char_u *)line, 0);
1502 update_screen(NOT_VALID);
1503
1504void
1505DoCommand(line)
1506 char *line;
1507
1508 PPCODE:
1509 if (line != NULL)
1510 do_cmdline_cmd((char_u *)line);
1511
1512void
1513Eval(str)
1514 char *str;
1515
1516 PREINIT:
1517 char_u *value;
1518 PPCODE:
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001519 value = eval_to_string((char_u *)str, (char_u **)0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 if (value == NULL)
1521 {
1522 XPUSHs(sv_2mortal(newSViv(0)));
1523 XPUSHs(sv_2mortal(newSVpv("", 0)));
1524 }
1525 else
1526 {
1527 XPUSHs(sv_2mortal(newSViv(1)));
1528 XPUSHs(sv_2mortal(newSVpv((char *)value, 0)));
1529 vim_free(value);
1530 }
1531
1532void
1533Buffers(...)
1534
1535 PREINIT:
1536 buf_T *vimbuf;
1537 int i, b;
1538
1539 PPCODE:
1540 if (items == 0)
1541 {
1542 if (GIMME == G_SCALAR)
1543 {
1544 i = 0;
1545 for (vimbuf = firstbuf; vimbuf; vimbuf = vimbuf->b_next)
1546 ++i;
1547
1548 XPUSHs(sv_2mortal(newSViv(i)));
1549 }
1550 else
1551 {
1552 for (vimbuf = firstbuf; vimbuf; vimbuf = vimbuf->b_next)
1553 XPUSHs(newBUFrv(newSV(0), vimbuf));
1554 }
1555 }
1556 else
1557 {
1558 for (i = 0; i < items; i++)
1559 {
1560 SV *sv = ST(i);
1561 if (SvIOK(sv))
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001562 b = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 else
1564 {
1565 char_u *pat;
1566 STRLEN len;
1567
1568 pat = (char_u *)SvPV(sv, len);
1569 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001570 b = buflist_findpat(pat, pat+len, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 --emsg_off;
1572 }
1573
1574 if (b >= 0)
1575 {
1576 vimbuf = buflist_findnr(b);
1577 if (vimbuf)
1578 XPUSHs(newBUFrv(newSV(0), vimbuf));
1579 }
1580 }
1581 }
1582
1583void
1584Windows(...)
1585
1586 PREINIT:
1587 win_T *vimwin;
1588 int i, w;
1589
1590 PPCODE:
1591 if (items == 0)
1592 {
1593 if (GIMME == G_SCALAR)
1594 XPUSHs(sv_2mortal(newSViv(win_count())));
1595 else
1596 {
1597 for (vimwin = firstwin; vimwin != NULL; vimwin = W_NEXT(vimwin))
1598 XPUSHs(newWINrv(newSV(0), vimwin));
1599 }
1600 }
1601 else
1602 {
1603 for (i = 0; i < items; i++)
1604 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001605 w = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 vimwin = win_find_nr(w);
1607 if (vimwin)
1608 XPUSHs(newWINrv(newSV(0), vimwin));
1609 }
1610 }
1611
1612MODULE = VIM PACKAGE = VIWIN
1613
1614void
1615DESTROY(win)
1616 VIWIN win
1617
1618 CODE:
1619 if (win_valid(win))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001620 win->w_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621
1622SV *
1623Buffer(win)
1624 VIWIN win
1625
1626 CODE:
1627 if (!win_valid(win))
1628 win = curwin;
1629 RETVAL = newBUFrv(newSV(0), win->w_buffer);
1630 OUTPUT:
1631 RETVAL
1632
1633void
1634SetHeight(win, height)
1635 VIWIN win
1636 int height;
1637
1638 PREINIT:
1639 win_T *savewin;
1640
1641 PPCODE:
1642 if (!win_valid(win))
1643 win = curwin;
1644 savewin = curwin;
1645 curwin = win;
1646 win_setheight(height);
1647 curwin = savewin;
1648
1649void
Bram Moolenaar864733a2016-04-02 14:18:01 +02001650Cursor(win, ...)
1651 VIWIN win
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652
1653 PPCODE:
Bram Moolenaara1711622011-07-27 14:15:46 +02001654 if (items == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 {
1656 EXTEND(sp, 2);
1657 if (!win_valid(win))
1658 win = curwin;
1659 PUSHs(sv_2mortal(newSViv(win->w_cursor.lnum)));
1660 PUSHs(sv_2mortal(newSViv(win->w_cursor.col)));
1661 }
Bram Moolenaara1711622011-07-27 14:15:46 +02001662 else if (items == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 {
1664 int lnum, col;
1665
1666 if (!win_valid(win))
1667 win = curwin;
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001668 lnum = (int) SvIV(ST(1));
1669 col = (int) SvIV(ST(2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 win->w_cursor.lnum = lnum;
1671 win->w_cursor.col = col;
1672 check_cursor(); /* put cursor on an existing line */
1673 update_screen(NOT_VALID);
1674 }
1675
1676MODULE = VIM PACKAGE = VIBUF
1677
1678void
1679DESTROY(vimbuf)
1680 VIBUF vimbuf;
1681
1682 CODE:
1683 if (buf_valid(vimbuf))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001684 vimbuf->b_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685
1686void
1687Name(vimbuf)
1688 VIBUF vimbuf;
1689
1690 PPCODE:
1691 if (!buf_valid(vimbuf))
1692 vimbuf = curbuf;
1693 /* No file name returns an empty string */
1694 if (vimbuf->b_fname == NULL)
1695 XPUSHs(sv_2mortal(newSVpv("", 0)));
1696 else
1697 XPUSHs(sv_2mortal(newSVpv((char *)vimbuf->b_fname, 0)));
1698
1699void
1700Number(vimbuf)
1701 VIBUF vimbuf;
1702
1703 PPCODE:
1704 if (!buf_valid(vimbuf))
1705 vimbuf = curbuf;
1706 XPUSHs(sv_2mortal(newSViv(vimbuf->b_fnum)));
1707
1708void
1709Count(vimbuf)
1710 VIBUF vimbuf;
1711
1712 PPCODE:
1713 if (!buf_valid(vimbuf))
1714 vimbuf = curbuf;
1715 XPUSHs(sv_2mortal(newSViv(vimbuf->b_ml.ml_line_count)));
1716
1717void
1718Get(vimbuf, ...)
1719 VIBUF vimbuf;
1720
1721 PREINIT:
1722 char_u *line;
1723 int i;
1724 long lnum;
1725 PPCODE:
1726 if (buf_valid(vimbuf))
1727 {
1728 for (i = 1; i < items; i++)
1729 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001730 lnum = (long) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1732 {
1733 line = ml_get_buf(vimbuf, lnum, FALSE);
1734 XPUSHs(sv_2mortal(newSVpv((char *)line, 0)));
1735 }
1736 }
1737 }
1738
1739void
1740Set(vimbuf, ...)
1741 VIBUF vimbuf;
1742
1743 PREINIT:
1744 int i;
1745 long lnum;
1746 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 PPCODE:
1748 if (buf_valid(vimbuf))
1749 {
1750 if (items < 3)
1751 croak("Usage: VIBUF::Set(vimbuf, lnum, @lines)");
1752
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001753 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 for(i = 2; i < items; i++, lnum++)
1755 {
1756 line = SvPV(ST(i),PL_na);
1757 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1758 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001759 aco_save_T aco;
1760
1761 /* set curwin/curbuf for "vimbuf" and save some things */
1762 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001763
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 if (u_savesub(lnum) == OK)
1765 {
1766 ml_replace(lnum, (char_u *)line, TRUE);
1767 changed_bytes(lnum, 0);
1768 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001769
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001770 /* restore curwin/curbuf and a few other things */
1771 aucmd_restbuf(&aco);
1772 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 }
1774 }
1775 }
1776
1777void
1778Delete(vimbuf, ...)
1779 VIBUF vimbuf;
1780
1781 PREINIT:
1782 long i, lnum = 0, count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 PPCODE:
1784 if (buf_valid(vimbuf))
1785 {
1786 if (items == 2)
1787 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001788 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 count = 1;
1790 }
1791 else if (items == 3)
1792 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001793 lnum = (long) SvIV(ST(1));
1794 count = (long) 1 + SvIV(ST(2)) - lnum;
Bram Moolenaara1711622011-07-27 14:15:46 +02001795 if (count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 count = 1;
Bram Moolenaara1711622011-07-27 14:15:46 +02001797 if (count < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 {
1799 lnum -= count;
1800 count = -count;
1801 }
1802 }
1803 if (items >= 2)
1804 {
1805 for (i = 0; i < count; i++)
1806 {
1807 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1808 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001809 aco_save_T aco;
1810
1811 /* set curwin/curbuf for "vimbuf" and save some things */
1812 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001813
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 if (u_savedel(lnum, 1) == OK)
1815 {
1816 ml_delete(lnum, 0);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00001817 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 deleted_lines_mark(lnum, 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001820
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001821 /* restore curwin/curbuf and a few other things */
1822 aucmd_restbuf(&aco);
1823 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001824
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 update_curbuf(VALID);
1826 }
1827 }
1828 }
1829 }
1830
1831void
1832Append(vimbuf, ...)
1833 VIBUF vimbuf;
1834
1835 PREINIT:
1836 int i;
1837 long lnum;
1838 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 PPCODE:
1840 if (buf_valid(vimbuf))
1841 {
1842 if (items < 3)
1843 croak("Usage: VIBUF::Append(vimbuf, lnum, @lines)");
1844
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001845 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 for (i = 2; i < items; i++, lnum++)
1847 {
1848 line = SvPV(ST(i),PL_na);
1849 if (lnum >= 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1850 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001851 aco_save_T aco;
1852
1853 /* set curwin/curbuf for "vimbuf" and save some things */
1854 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001855
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 if (u_inssub(lnum + 1) == OK)
1857 {
1858 ml_append(lnum, (char_u *)line, (colnr_T)0, FALSE);
1859 appended_lines_mark(lnum, 1L);
1860 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001861
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001862 /* restore curwin/curbuf and a few other things */
1863 aucmd_restbuf(&aco);
1864 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001865
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 update_curbuf(VALID);
1867 }
1868 }
1869 }
1870
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001871#ifdef __GNUC__
1872# pragma GCC diagnostic pop
1873#endif