blob: a4899945fe5fc165b70201ed98b4229dac8fcaa5 [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
64/*
65 * Work around clashes between Perl and Vim namespace. proto.h doesn't
66 * include if_perl.pro and perlsfio.pro when IN_PERL_FILE is defined, because
67 * we need the CV typedef. proto.h can't be moved to after including
68 * if_perl.h, because we get all sorts of name clashes then.
69 */
70#ifndef PROTO
71#ifndef __MINGW32__
72# include "proto/if_perl.pro"
73# include "proto/if_perlsfio.pro"
74#endif
75#endif
76
77/* Perl compatibility stuff. This should ensure compatibility with older
78 * versions of Perl.
79 */
80
81#ifndef PERL_VERSION
82# include <patchlevel.h>
83# define PERL_REVISION 5
84# define PERL_VERSION PATCHLEVEL
85# define PERL_SUBVERSION SUBVERSION
86#endif
87
Bram Moolenaar700d1d72007-09-13 13:20:16 +000088/*
89 * Quoting Jan Dubois of Active State:
90 * ActivePerl build 822 still identifies itself as 5.8.8 but already
91 * contains many of the changes from the upcoming Perl 5.8.9 release.
92 *
93 * The changes include addition of two symbols (Perl_sv_2iv_flags,
94 * Perl_newXS_flags) not present in earlier releases.
95 *
Bram Moolenaar3b9b13e2007-09-15 12:49:35 +000096 * Jan Dubois suggested the following guarding scheme.
97 *
98 * Active State defined ACTIVEPERL_VERSION as a string in versions before
99 * 5.8.8; and so the comparison to 822 below needs to be guarded.
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000100 */
Bram Moolenaar3b9b13e2007-09-15 12:49:35 +0000101#if (PERL_REVISION == 5) && (PERL_VERSION == 8) && (PERL_SUBVERSION >= 8)
102# if (ACTIVEPERL_VERSION >= 822) || (PERL_SUBVERSION >= 9)
103# define PERL589_OR_LATER
104# endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000105#endif
106#if (PERL_REVISION == 5) && (PERL_VERSION >= 9)
107# define PERL589_OR_LATER
108#endif
109
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100110#if (PERL_REVISION == 5) && ((PERL_VERSION > 10) || \
111 (PERL_VERSION == 10) && (PERL_SUBVERSION >= 1))
112# define PERL5101_OR_LATER
113#endif
114
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#ifndef pTHX
116# define pTHX void
117# define pTHX_
118#endif
119
120#ifndef EXTERN_C
121# define EXTERN_C
122#endif
123
Bram Moolenaarc271c482012-08-08 13:17:31 +0200124#if (PERL_REVISION == 5) && (PERL_VERSION >= 14) && defined(_MSC_VER)
125/* Using PL_errgv to get the error message after perl_eval_sv() causes a crash
126 * with MSVC and Perl version 5.14. */
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100127# define CHECK_EVAL_ERR(len) SvPV(perl_get_sv("@", GV_ADD), (len));
128#else
129# define CHECK_EVAL_ERR(len) SvPV(GvSV(PL_errgv), (len));
Bram Moolenaarc271c482012-08-08 13:17:31 +0200130#endif
131
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132/* Compatibility hacks over */
133
134static PerlInterpreter *perl_interp = NULL;
Bram Moolenaard99df422016-01-29 23:20:40 +0100135static void xs_init(pTHX);
136static void VIM_init(void);
137EXTERN_C void boot_DynaLoader(pTHX_ CV*);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
139/*
Bram Moolenaare06c1882010-07-21 22:05:20 +0200140 * For dynamic linked perl.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 */
142#if defined(DYNAMIC_PERL) || defined(PROTO)
Bram Moolenaare06c1882010-07-21 22:05:20 +0200143
144#ifndef DYNAMIC_PERL /* just generating prototypes */
Bram Moolenaar766fb0d2010-07-22 11:34:16 +0200145#ifdef WIN3264
Bram Moolenaare06c1882010-07-21 22:05:20 +0200146typedef int HANDLE;
147#endif
148typedef int XSINIT_t;
149typedef int XSUBADDR_t;
Bram Moolenaard8619992014-03-12 17:08:05 +0100150#endif
151#ifndef USE_ITHREADS
Bram Moolenaare06c1882010-07-21 22:05:20 +0200152typedef int perl_key;
153#endif
154
Bram Moolenaar766fb0d2010-07-22 11:34:16 +0200155#ifndef WIN3264
Bram Moolenaare06c1882010-07-21 22:05:20 +0200156#include <dlfcn.h>
157#define HANDLE void*
158#define PERL_PROC void*
159#define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
160#define symbol_from_dll dlsym
161#define close_dll dlclose
162#else
163#define PERL_PROC FARPROC
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200164#define load_dll vimLoadLib
Bram Moolenaare06c1882010-07-21 22:05:20 +0200165#define symbol_from_dll GetProcAddress
166#define close_dll FreeLibrary
167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168/*
169 * Wrapper defines
170 */
171# define perl_alloc dll_perl_alloc
172# define perl_construct dll_perl_construct
173# define perl_parse dll_perl_parse
174# define perl_run dll_perl_run
175# define perl_destruct dll_perl_destruct
176# define perl_free dll_perl_free
177# define Perl_get_context dll_Perl_get_context
178# define Perl_croak dll_Perl_croak
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100179# ifdef PERL5101_OR_LATER
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100180# define Perl_croak_xs_usage dll_Perl_croak_xs_usage
181# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182# ifndef PROTO
183# define Perl_croak_nocontext dll_Perl_croak_nocontext
184# define Perl_call_argv dll_Perl_call_argv
185# define Perl_call_pv dll_Perl_call_pv
186# define Perl_eval_sv dll_Perl_eval_sv
187# define Perl_get_sv dll_Perl_get_sv
188# define Perl_eval_pv dll_Perl_eval_pv
189# define Perl_call_method dll_Perl_call_method
190# endif
191# define Perl_dowantarray dll_Perl_dowantarray
192# define Perl_free_tmps dll_Perl_free_tmps
193# define Perl_gv_stashpv dll_Perl_gv_stashpv
194# define Perl_markstack_grow dll_Perl_markstack_grow
195# define Perl_mg_find dll_Perl_mg_find
196# define Perl_newXS dll_Perl_newXS
197# define Perl_newSV dll_Perl_newSV
198# define Perl_newSViv dll_Perl_newSViv
199# define Perl_newSVpv dll_Perl_newSVpv
200# define Perl_pop_scope dll_Perl_pop_scope
201# define Perl_push_scope dll_Perl_push_scope
202# define Perl_save_int dll_Perl_save_int
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200203# if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
204# define Perl_save_strlen dll_Perl_save_strlen
205# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206# define Perl_stack_grow dll_Perl_stack_grow
207# define Perl_set_context dll_Perl_set_context
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200208# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200209# define Perl_sv_2bool_flags dll_Perl_sv_2bool_flags
210# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
211# define Perl_xs_apiversion_bootcheck dll_Perl_xs_apiversion_bootcheck
212# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200213# else
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200214# define Perl_sv_2bool dll_Perl_sv_2bool
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200215# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216# define Perl_sv_2iv dll_Perl_sv_2iv
217# define Perl_sv_2mortal dll_Perl_sv_2mortal
218# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
219# define Perl_sv_2pv_flags dll_Perl_sv_2pv_flags
220# define Perl_sv_2pv_nolen dll_Perl_sv_2pv_nolen
221# else
222# define Perl_sv_2pv dll_Perl_sv_2pv
223# endif
224# define Perl_sv_bless dll_Perl_sv_bless
225# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
226# define Perl_sv_catpvn_flags dll_Perl_sv_catpvn_flags
227# else
228# define Perl_sv_catpvn dll_Perl_sv_catpvn
229# endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000230#ifdef PERL589_OR_LATER
231# define Perl_sv_2iv_flags dll_Perl_sv_2iv_flags
232# define Perl_newXS_flags dll_Perl_newXS_flags
233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234# define Perl_sv_free dll_Perl_sv_free
Bram Moolenaarccf22172008-09-01 15:56:45 +0000235# if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
236# define Perl_sv_free2 dll_Perl_sv_free2
237# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238# define Perl_sv_isa dll_Perl_sv_isa
239# define Perl_sv_magic dll_Perl_sv_magic
240# define Perl_sv_setiv dll_Perl_sv_setiv
241# define Perl_sv_setpv dll_Perl_sv_setpv
242# define Perl_sv_setpvn dll_Perl_sv_setpvn
243# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
244# define Perl_sv_setsv_flags dll_Perl_sv_setsv_flags
245# else
246# define Perl_sv_setsv dll_Perl_sv_setsv
247# endif
248# define Perl_sv_upgrade dll_Perl_sv_upgrade
249# define Perl_Tstack_sp_ptr dll_Perl_Tstack_sp_ptr
250# define Perl_Top_ptr dll_Perl_Top_ptr
251# define Perl_Tstack_base_ptr dll_Perl_Tstack_base_ptr
252# define Perl_Tstack_max_ptr dll_Perl_Tstack_max_ptr
253# define Perl_Ttmps_ix_ptr dll_Perl_Ttmps_ix_ptr
254# define Perl_Ttmps_floor_ptr dll_Perl_Ttmps_floor_ptr
255# define Perl_Tmarkstack_ptr_ptr dll_Perl_Tmarkstack_ptr_ptr
256# define Perl_Tmarkstack_max_ptr dll_Perl_Tmarkstack_max_ptr
257# define Perl_TSv_ptr dll_Perl_TSv_ptr
258# define Perl_TXpv_ptr dll_Perl_TXpv_ptr
259# define Perl_Tna_ptr dll_Perl_Tna_ptr
260# define Perl_Idefgv_ptr dll_Perl_Idefgv_ptr
261# define Perl_Ierrgv_ptr dll_Perl_Ierrgv_ptr
262# define Perl_Isv_yes_ptr dll_Perl_Isv_yes_ptr
263# define boot_DynaLoader dll_boot_DynaLoader
Bram Moolenaare06c1882010-07-21 22:05:20 +0200264# define Perl_Gthr_key_ptr dll_Perl_Gthr_key_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000266# define Perl_sys_init dll_Perl_sys_init
Bram Moolenaarc236c162008-07-13 17:41:49 +0000267# define Perl_sys_term dll_Perl_sys_term
268# define Perl_ISv_ptr dll_Perl_ISv_ptr
269# define Perl_Istack_max_ptr dll_Perl_Istack_max_ptr
270# define Perl_Istack_base_ptr dll_Perl_Istack_base_ptr
271# define Perl_Itmps_ix_ptr dll_Perl_Itmps_ix_ptr
272# define Perl_Itmps_floor_ptr dll_Perl_Itmps_floor_ptr
273# define Perl_IXpv_ptr dll_Perl_IXpv_ptr
274# define Perl_Ina_ptr dll_Perl_Ina_ptr
275# define Perl_Imarkstack_ptr_ptr dll_Perl_Imarkstack_ptr_ptr
276# define Perl_Imarkstack_max_ptr dll_Perl_Imarkstack_max_ptr
277# define Perl_Istack_sp_ptr dll_Perl_Istack_sp_ptr
278# define Perl_Iop_ptr dll_Perl_Iop_ptr
279# define Perl_call_list dll_Perl_call_list
280# define Perl_Iscopestack_ix_ptr dll_Perl_Iscopestack_ix_ptr
281# define Perl_Iunitcheckav_ptr dll_Perl_Iunitcheckav_ptr
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200282# if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
283# define Perl_xs_handshake dll_Perl_xs_handshake
284# define Perl_xs_boot_epilog dll_Perl_xs_boot_epilog
285# endif
Bram Moolenaar01c10522012-09-21 12:50:51 +0200286# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100287# ifdef USE_ITHREADS
288# define PL_thr_key *dll_PL_thr_key
289# endif
Bram Moolenaar01c10522012-09-21 12:50:51 +0200290# endif
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100291# define Perl_hv_iternext_flags dll_Perl_hv_iternext_flags
292# define Perl_hv_iterinit dll_Perl_hv_iterinit
293# define Perl_hv_iterkey dll_Perl_hv_iterkey
294# define Perl_hv_iterval dll_Perl_hv_iterval
295# define Perl_av_fetch dll_Perl_av_fetch
296# define Perl_av_len dll_Perl_av_len
297# define Perl_sv_2nv_flags dll_Perl_sv_2nv_flags
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200298# if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
299# define PerlIOBase_pushed dll_PerlIOBase_pushed
300# define PerlIO_define_layer dll_PerlIO_define_layer
301# endif
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200302# if (PERL_REVISION == 5) && (PERL_VERSION >= 24)
303# define Perl_savetmps dll_Perl_savetmps
304# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000305
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306/*
307 * Declare HANDLE for perl.dll and function pointers.
308 */
309static HANDLE hPerlLib = NULL;
310
311static PerlInterpreter* (*perl_alloc)();
312static void (*perl_construct)(PerlInterpreter*);
313static void (*perl_destruct)(PerlInterpreter*);
314static void (*perl_free)(PerlInterpreter*);
315static int (*perl_run)(PerlInterpreter*);
316static int (*perl_parse)(PerlInterpreter*, XSINIT_t, int, char**, char**);
317static void* (*Perl_get_context)(void);
Bram Moolenaar864733a2016-04-02 14:18:01 +0200318static void (*Perl_croak)(pTHX_ const char*, ...) __attribute__noreturn__;
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100319#ifdef PERL5101_OR_LATER
Bram Moolenaar6b107212013-12-11 15:06:40 +0100320/* Perl-5.18 has a different Perl_croak_xs_usage signature. */
321# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
Bram Moolenaar864733a2016-04-02 14:18:01 +0200322static void (*Perl_croak_xs_usage)(const CV *const, const char *const params)
323 __attribute__noreturn__;
Bram Moolenaar6b107212013-12-11 15:06:40 +0100324# else
Bram Moolenaar864733a2016-04-02 14:18:01 +0200325static void (*Perl_croak_xs_usage)(pTHX_ const CV *const, const char *const params)
326 __attribute__noreturn__;
Bram Moolenaar6b107212013-12-11 15:06:40 +0100327# endif
Bram Moolenaar9be6e212013-06-15 16:47:35 +0200328#endif
Bram Moolenaar864733a2016-04-02 14:18:01 +0200329static void (*Perl_croak_nocontext)(const char*, ...) __attribute__noreturn__;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330static I32 (*Perl_dowantarray)(pTHX);
331static void (*Perl_free_tmps)(pTHX);
332static HV* (*Perl_gv_stashpv)(pTHX_ const char*, I32);
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200333#if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
334static I32* (*Perl_markstack_grow)(pTHX);
335#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336static void (*Perl_markstack_grow)(pTHX);
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338static MAGIC* (*Perl_mg_find)(pTHX_ SV*, int);
339static CV* (*Perl_newXS)(pTHX_ char*, XSUBADDR_t, char*);
340static SV* (*Perl_newSV)(pTHX_ STRLEN);
341static SV* (*Perl_newSViv)(pTHX_ IV);
342static SV* (*Perl_newSVpv)(pTHX_ const char*, STRLEN);
343static I32 (*Perl_call_argv)(pTHX_ const char*, I32, char**);
344static I32 (*Perl_call_pv)(pTHX_ const char*, I32);
345static I32 (*Perl_eval_sv)(pTHX_ SV*, I32);
346static SV* (*Perl_get_sv)(pTHX_ const char*, I32);
347static SV* (*Perl_eval_pv)(pTHX_ const char*, I32);
348static SV* (*Perl_call_method)(pTHX_ const char*, I32);
349static void (*Perl_pop_scope)(pTHX);
350static void (*Perl_push_scope)(pTHX);
351static void (*Perl_save_int)(pTHX_ int*);
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200352#if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
353static void (*Perl_save_strlen)(pTHX_ STRLEN* ptr);
354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355static SV** (*Perl_stack_grow)(pTHX_ SV**, SV**p, int);
356static SV** (*Perl_set_context)(void*);
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200357#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
358static bool (*Perl_sv_2bool_flags)(pTHX_ SV*, I32);
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200359# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200360static void (*Perl_xs_apiversion_bootcheck)(pTHX_ SV *module, const char *api_p, STRLEN api_len);
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200361# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200362#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363static bool (*Perl_sv_2bool)(pTHX_ SV*);
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365static IV (*Perl_sv_2iv)(pTHX_ SV*);
366static SV* (*Perl_sv_2mortal)(pTHX_ SV*);
367#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
368static char* (*Perl_sv_2pv_flags)(pTHX_ SV*, STRLEN*, I32);
369static char* (*Perl_sv_2pv_nolen)(pTHX_ SV*);
370#else
371static char* (*Perl_sv_2pv)(pTHX_ SV*, STRLEN*);
372#endif
373static SV* (*Perl_sv_bless)(pTHX_ SV*, HV*);
374#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
375static void (*Perl_sv_catpvn_flags)(pTHX_ SV* , const char*, STRLEN, I32);
376#else
377static void (*Perl_sv_catpvn)(pTHX_ SV*, const char*, STRLEN);
378#endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000379#ifdef PERL589_OR_LATER
380static IV (*Perl_sv_2iv_flags)(pTHX_ SV* sv, I32 flags);
381static CV * (*Perl_newXS_flags)(pTHX_ const char *name, XSUBADDR_t subaddr, const char *const filename, const char *const proto, U32 flags);
382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383static void (*Perl_sv_free)(pTHX_ SV*);
384static int (*Perl_sv_isa)(pTHX_ SV*, const char*);
385static void (*Perl_sv_magic)(pTHX_ SV*, SV*, int, const char*, I32);
386static void (*Perl_sv_setiv)(pTHX_ SV*, IV);
387static void (*Perl_sv_setpv)(pTHX_ SV*, const char*);
388static void (*Perl_sv_setpvn)(pTHX_ SV*, const char*, STRLEN);
389#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
390static void (*Perl_sv_setsv_flags)(pTHX_ SV*, SV*, I32);
391#else
392static void (*Perl_sv_setsv)(pTHX_ SV*, SV*);
393#endif
394static bool (*Perl_sv_upgrade)(pTHX_ SV*, U32);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200395#if (PERL_REVISION == 5) && (PERL_VERSION < 10)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396static SV*** (*Perl_Tstack_sp_ptr)(register PerlInterpreter*);
397static OP** (*Perl_Top_ptr)(register PerlInterpreter*);
398static SV*** (*Perl_Tstack_base_ptr)(register PerlInterpreter*);
399static SV*** (*Perl_Tstack_max_ptr)(register PerlInterpreter*);
400static I32* (*Perl_Ttmps_ix_ptr)(register PerlInterpreter*);
401static I32* (*Perl_Ttmps_floor_ptr)(register PerlInterpreter*);
402static I32** (*Perl_Tmarkstack_ptr_ptr)(register PerlInterpreter*);
403static I32** (*Perl_Tmarkstack_max_ptr)(register PerlInterpreter*);
404static SV** (*Perl_TSv_ptr)(register PerlInterpreter*);
405static XPV** (*Perl_TXpv_ptr)(register PerlInterpreter*);
406static STRLEN* (*Perl_Tna_ptr)(register PerlInterpreter*);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200407#else
Bram Moolenaar6b107212013-12-11 15:06:40 +0100408/* Perl-5.18 has a different Perl_sv_free2 signature. */
409# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
410static void (*Perl_sv_free2)(pTHX_ SV*, const U32);
411# else
Bram Moolenaarccf22172008-09-01 15:56:45 +0000412static void (*Perl_sv_free2)(pTHX_ SV*);
Bram Moolenaar6b107212013-12-11 15:06:40 +0100413# endif
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000414static void (*Perl_sys_init)(int* argc, char*** argv);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000415static void (*Perl_sys_term)(void);
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200416static void (*Perl_call_list)(pTHX_ I32, AV*);
417# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
418# else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000419static SV** (*Perl_ISv_ptr)(register PerlInterpreter*);
420static SV*** (*Perl_Istack_max_ptr)(register PerlInterpreter*);
421static SV*** (*Perl_Istack_base_ptr)(register PerlInterpreter*);
422static XPV** (*Perl_IXpv_ptr)(register PerlInterpreter*);
423static I32* (*Perl_Itmps_ix_ptr)(register PerlInterpreter*);
424static I32* (*Perl_Itmps_floor_ptr)(register PerlInterpreter*);
425static STRLEN* (*Perl_Ina_ptr)(register PerlInterpreter*);
426static I32** (*Perl_Imarkstack_ptr_ptr)(register PerlInterpreter*);
427static I32** (*Perl_Imarkstack_max_ptr)(register PerlInterpreter*);
428static SV*** (*Perl_Istack_sp_ptr)(register PerlInterpreter*);
429static OP** (*Perl_Iop_ptr)(register PerlInterpreter*);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000430static I32* (*Perl_Iscopestack_ix_ptr)(register PerlInterpreter*);
431static AV** (*Perl_Iunitcheckav_ptr)(register PerlInterpreter*);
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200432# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000433#endif
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200434#if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
435static I32 (*Perl_xs_handshake)(const U32, void *, const char *, ...);
436static void (*Perl_xs_boot_epilog)(pTHX_ const U32);
437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200439#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100440# ifdef USE_ITHREADS
Bram Moolenaar01c10522012-09-21 12:50:51 +0200441static perl_key* dll_PL_thr_key;
Bram Moolenaard8619992014-03-12 17:08:05 +0100442# endif
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200443#else
Bram Moolenaare06c1882010-07-21 22:05:20 +0200444static GV** (*Perl_Idefgv_ptr)(register PerlInterpreter*);
445static GV** (*Perl_Ierrgv_ptr)(register PerlInterpreter*);
446static SV* (*Perl_Isv_yes_ptr)(register PerlInterpreter*);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200447static perl_key* (*Perl_Gthr_key_ptr)_((pTHX));
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200448#endif
449static void (*boot_DynaLoader)_((pTHX_ CV*));
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100450static HE * (*Perl_hv_iternext_flags)(pTHX_ HV *, I32);
451static I32 (*Perl_hv_iterinit)(pTHX_ HV *);
452static char * (*Perl_hv_iterkey)(pTHX_ HE *, I32 *);
453static SV * (*Perl_hv_iterval)(pTHX_ HV *, HE *);
454static SV** (*Perl_av_fetch)(pTHX_ AV *, SSize_t, I32);
455static SSize_t (*Perl_av_len)(pTHX_ AV *);
456static NV (*Perl_sv_2nv_flags)(pTHX_ SV *const, const I32);
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200457#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
458static IV (*PerlIOBase_pushed)(pTHX_ PerlIO *, const char *, SV *, PerlIO_funcs *);
459static void (*PerlIO_define_layer)(pTHX_ PerlIO_funcs *);
460#endif
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200461#if (PERL_REVISION == 5) && (PERL_VERSION >= 24)
462static void (*Perl_savetmps)(pTHX);
463#endif
Bram Moolenaare06c1882010-07-21 22:05:20 +0200464
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465/*
466 * Table of name to function pointer of perl.
467 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468static struct {
469 char* name;
470 PERL_PROC* ptr;
471} perl_funcname_table[] = {
472 {"perl_alloc", (PERL_PROC*)&perl_alloc},
473 {"perl_construct", (PERL_PROC*)&perl_construct},
474 {"perl_destruct", (PERL_PROC*)&perl_destruct},
475 {"perl_free", (PERL_PROC*)&perl_free},
476 {"perl_run", (PERL_PROC*)&perl_run},
477 {"perl_parse", (PERL_PROC*)&perl_parse},
478 {"Perl_get_context", (PERL_PROC*)&Perl_get_context},
479 {"Perl_croak", (PERL_PROC*)&Perl_croak},
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100480#ifdef PERL5101_OR_LATER
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100481 {"Perl_croak_xs_usage", (PERL_PROC*)&Perl_croak_xs_usage},
482#endif
Bram Moolenaard8619992014-03-12 17:08:05 +0100483#ifdef PERL_IMPLICIT_CONTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 {"Perl_croak_nocontext", (PERL_PROC*)&Perl_croak_nocontext},
Bram Moolenaard8619992014-03-12 17:08:05 +0100485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 {"Perl_dowantarray", (PERL_PROC*)&Perl_dowantarray},
487 {"Perl_free_tmps", (PERL_PROC*)&Perl_free_tmps},
488 {"Perl_gv_stashpv", (PERL_PROC*)&Perl_gv_stashpv},
489 {"Perl_markstack_grow", (PERL_PROC*)&Perl_markstack_grow},
490 {"Perl_mg_find", (PERL_PROC*)&Perl_mg_find},
491 {"Perl_newXS", (PERL_PROC*)&Perl_newXS},
492 {"Perl_newSV", (PERL_PROC*)&Perl_newSV},
493 {"Perl_newSViv", (PERL_PROC*)&Perl_newSViv},
494 {"Perl_newSVpv", (PERL_PROC*)&Perl_newSVpv},
495 {"Perl_call_argv", (PERL_PROC*)&Perl_call_argv},
496 {"Perl_call_pv", (PERL_PROC*)&Perl_call_pv},
497 {"Perl_eval_sv", (PERL_PROC*)&Perl_eval_sv},
498 {"Perl_get_sv", (PERL_PROC*)&Perl_get_sv},
499 {"Perl_eval_pv", (PERL_PROC*)&Perl_eval_pv},
500 {"Perl_call_method", (PERL_PROC*)&Perl_call_method},
501 {"Perl_pop_scope", (PERL_PROC*)&Perl_pop_scope},
502 {"Perl_push_scope", (PERL_PROC*)&Perl_push_scope},
503 {"Perl_save_int", (PERL_PROC*)&Perl_save_int},
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200504#if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
505 {"Perl_save_strlen", (PERL_PROC*)&Perl_save_strlen},
506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 {"Perl_stack_grow", (PERL_PROC*)&Perl_stack_grow},
508 {"Perl_set_context", (PERL_PROC*)&Perl_set_context},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200509#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
510 {"Perl_sv_2bool_flags", (PERL_PROC*)&Perl_sv_2bool_flags},
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200511# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200512 {"Perl_xs_apiversion_bootcheck",(PERL_PROC*)&Perl_xs_apiversion_bootcheck},
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200513# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200514#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 {"Perl_sv_2bool", (PERL_PROC*)&Perl_sv_2bool},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 {"Perl_sv_2iv", (PERL_PROC*)&Perl_sv_2iv},
518 {"Perl_sv_2mortal", (PERL_PROC*)&Perl_sv_2mortal},
519#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
520 {"Perl_sv_2pv_flags", (PERL_PROC*)&Perl_sv_2pv_flags},
521 {"Perl_sv_2pv_nolen", (PERL_PROC*)&Perl_sv_2pv_nolen},
522#else
523 {"Perl_sv_2pv", (PERL_PROC*)&Perl_sv_2pv},
524#endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000525#ifdef PERL589_OR_LATER
526 {"Perl_sv_2iv_flags", (PERL_PROC*)&Perl_sv_2iv_flags},
527 {"Perl_newXS_flags", (PERL_PROC*)&Perl_newXS_flags},
528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {"Perl_sv_bless", (PERL_PROC*)&Perl_sv_bless},
530#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
531 {"Perl_sv_catpvn_flags", (PERL_PROC*)&Perl_sv_catpvn_flags},
532#else
533 {"Perl_sv_catpvn", (PERL_PROC*)&Perl_sv_catpvn},
534#endif
535 {"Perl_sv_free", (PERL_PROC*)&Perl_sv_free},
536 {"Perl_sv_isa", (PERL_PROC*)&Perl_sv_isa},
537 {"Perl_sv_magic", (PERL_PROC*)&Perl_sv_magic},
538 {"Perl_sv_setiv", (PERL_PROC*)&Perl_sv_setiv},
539 {"Perl_sv_setpv", (PERL_PROC*)&Perl_sv_setpv},
540 {"Perl_sv_setpvn", (PERL_PROC*)&Perl_sv_setpvn},
541#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
542 {"Perl_sv_setsv_flags", (PERL_PROC*)&Perl_sv_setsv_flags},
543#else
544 {"Perl_sv_setsv", (PERL_PROC*)&Perl_sv_setsv},
545#endif
546 {"Perl_sv_upgrade", (PERL_PROC*)&Perl_sv_upgrade},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000547#if (PERL_REVISION == 5) && (PERL_VERSION < 10)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 {"Perl_Tstack_sp_ptr", (PERL_PROC*)&Perl_Tstack_sp_ptr},
549 {"Perl_Top_ptr", (PERL_PROC*)&Perl_Top_ptr},
550 {"Perl_Tstack_base_ptr", (PERL_PROC*)&Perl_Tstack_base_ptr},
551 {"Perl_Tstack_max_ptr", (PERL_PROC*)&Perl_Tstack_max_ptr},
552 {"Perl_Ttmps_ix_ptr", (PERL_PROC*)&Perl_Ttmps_ix_ptr},
553 {"Perl_Ttmps_floor_ptr", (PERL_PROC*)&Perl_Ttmps_floor_ptr},
554 {"Perl_Tmarkstack_ptr_ptr", (PERL_PROC*)&Perl_Tmarkstack_ptr_ptr},
555 {"Perl_Tmarkstack_max_ptr", (PERL_PROC*)&Perl_Tmarkstack_max_ptr},
556 {"Perl_TSv_ptr", (PERL_PROC*)&Perl_TSv_ptr},
557 {"Perl_TXpv_ptr", (PERL_PROC*)&Perl_TXpv_ptr},
558 {"Perl_Tna_ptr", (PERL_PROC*)&Perl_Tna_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000559#else
Bram Moolenaarccf22172008-09-01 15:56:45 +0000560 {"Perl_sv_free2", (PERL_PROC*)&Perl_sv_free2},
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000561 {"Perl_sys_init", (PERL_PROC*)&Perl_sys_init},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000562 {"Perl_sys_term", (PERL_PROC*)&Perl_sys_term},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200563 {"Perl_call_list", (PERL_PROC*)&Perl_call_list},
564# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
565# else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000566 {"Perl_ISv_ptr", (PERL_PROC*)&Perl_ISv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000567 {"Perl_Istack_max_ptr", (PERL_PROC*)&Perl_Istack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200568 {"Perl_Istack_base_ptr", (PERL_PROC*)&Perl_Istack_base_ptr},
569 {"Perl_IXpv_ptr", (PERL_PROC*)&Perl_IXpv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000570 {"Perl_Itmps_ix_ptr", (PERL_PROC*)&Perl_Itmps_ix_ptr},
571 {"Perl_Itmps_floor_ptr", (PERL_PROC*)&Perl_Itmps_floor_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200572 {"Perl_Ina_ptr", (PERL_PROC*)&Perl_Ina_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000573 {"Perl_Imarkstack_ptr_ptr", (PERL_PROC*)&Perl_Imarkstack_ptr_ptr},
574 {"Perl_Imarkstack_max_ptr", (PERL_PROC*)&Perl_Imarkstack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200575 {"Perl_Istack_sp_ptr", (PERL_PROC*)&Perl_Istack_sp_ptr},
576 {"Perl_Iop_ptr", (PERL_PROC*)&Perl_Iop_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000577 {"Perl_Iscopestack_ix_ptr", (PERL_PROC*)&Perl_Iscopestack_ix_ptr},
578 {"Perl_Iunitcheckav_ptr", (PERL_PROC*)&Perl_Iunitcheckav_ptr},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200579# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000580#endif
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200581#if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
582 {"Perl_xs_handshake", (PERL_PROC*)&Perl_xs_handshake},
583 {"Perl_xs_boot_epilog", (PERL_PROC*)&Perl_xs_boot_epilog},
584#endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200585#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100586# ifdef USE_ITHREADS
Bram Moolenaar01c10522012-09-21 12:50:51 +0200587 {"PL_thr_key", (PERL_PROC*)&dll_PL_thr_key},
Bram Moolenaard8619992014-03-12 17:08:05 +0100588# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200589#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 {"Perl_Idefgv_ptr", (PERL_PROC*)&Perl_Idefgv_ptr},
591 {"Perl_Ierrgv_ptr", (PERL_PROC*)&Perl_Ierrgv_ptr},
592 {"Perl_Isv_yes_ptr", (PERL_PROC*)&Perl_Isv_yes_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200593 {"Perl_Gthr_key_ptr", (PERL_PROC*)&Perl_Gthr_key_ptr},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200594#endif
595 {"boot_DynaLoader", (PERL_PROC*)&boot_DynaLoader},
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100596 {"Perl_hv_iternext_flags", (PERL_PROC*)&Perl_hv_iternext_flags},
597 {"Perl_hv_iterinit", (PERL_PROC*)&Perl_hv_iterinit},
598 {"Perl_hv_iterkey", (PERL_PROC*)&Perl_hv_iterkey},
599 {"Perl_hv_iterval", (PERL_PROC*)&Perl_hv_iterval},
600 {"Perl_av_fetch", (PERL_PROC*)&Perl_av_fetch},
601 {"Perl_av_len", (PERL_PROC*)&Perl_av_len},
602 {"Perl_sv_2nv_flags", (PERL_PROC*)&Perl_sv_2nv_flags},
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200603#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
604 {"PerlIOBase_pushed", (PERL_PROC*)&PerlIOBase_pushed},
605 {"PerlIO_define_layer", (PERL_PROC*)&PerlIO_define_layer},
606#endif
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200607#if (PERL_REVISION == 5) && (PERL_VERSION >= 24)
608 {"Perl_savetmps", (PERL_PROC*)&Perl_savetmps},
609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 {"", NULL},
611};
612
Bram Moolenaar6b107212013-12-11 15:06:40 +0100613/* Work around for perl-5.18.
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200614 * For now, only the definitions of S_SvREFCNT_dec are needed in
615 * "perl\lib\CORE\inline.h". */
Bram Moolenaar6b107212013-12-11 15:06:40 +0100616#if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200617static void
618S_SvREFCNT_dec(pTHX_ SV *sv)
619{
620 if (LIKELY(sv != NULL)) {
621 U32 rc = SvREFCNT(sv);
622 if (LIKELY(rc > 1))
623 SvREFCNT(sv) = rc - 1;
624 else
625 Perl_sv_free2(aTHX_ sv, rc);
626 }
627}
Bram Moolenaar6b107212013-12-11 15:06:40 +0100628#endif
629
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630/*
631 * Make all runtime-links of perl.
632 *
Bram Moolenaar364ab2f2013-08-02 20:05:32 +0200633 * 1. Get module handle using dlopen() or vimLoadLib().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 * 2. Get pointer to perl function by GetProcAddress.
635 * 3. Repeat 2, until get all functions will be used.
636 *
637 * Parameter 'libname' provides name of DLL.
638 * Return OK or FAIL.
639 */
640 static int
641perl_runtime_link_init(char *libname, int verbose)
642{
643 int i;
644
645 if (hPerlLib != NULL)
646 return OK;
Bram Moolenaare06c1882010-07-21 22:05:20 +0200647 if ((hPerlLib = load_dll(libname)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 {
649 if (verbose)
650 EMSG2(_("E370: Could not load library %s"), libname);
651 return FAIL;
652 }
653 for (i = 0; perl_funcname_table[i].ptr; ++i)
654 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200655 if (!(*perl_funcname_table[i].ptr = symbol_from_dll(hPerlLib,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 perl_funcname_table[i].name)))
657 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200658 close_dll(hPerlLib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 hPerlLib = NULL;
660 if (verbose)
661 EMSG2(_(e_loadfunc), perl_funcname_table[i].name);
662 return FAIL;
663 }
664 }
665 return OK;
666}
667
668/*
669 * If runtime-link-perl(DLL) was loaded successfully, return TRUE.
670 * There were no DLL loaded, return FALSE.
671 */
672 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100673perl_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100675 return perl_runtime_link_init((char *)p_perldll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676}
677#endif /* DYNAMIC_PERL */
678
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200679#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
680static void vim_IOLayer_init(void);
681#endif
682
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683/*
684 * perl_init(): initialize perl interpreter
685 * We have to call perl_parse to initialize some structures,
686 * there's nothing to actually parse.
687 */
688 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100689perl_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690{
Bram Moolenaarc236c162008-07-13 17:41:49 +0000691 char *bootargs[] = { "VI", NULL };
692 int argc = 3;
693 static char *argv[] = { "", "-e", "" };
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694
Bram Moolenaarc236c162008-07-13 17:41:49 +0000695#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000696 Perl_sys_init(&argc, (char***)&argv);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 perl_interp = perl_alloc();
699 perl_construct(perl_interp);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000700 perl_parse(perl_interp, xs_init, argc, argv, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 perl_call_argv("VIM::bootstrap", (long)G_DISCARD, bootargs);
702 VIM_init();
703#ifdef USE_SFIO
704 sfdisc(PerlIO_stdout(), sfdcnewvim());
705 sfdisc(PerlIO_stderr(), sfdcnewvim());
706 sfsetbuf(PerlIO_stdout(), NULL, 0);
707 sfsetbuf(PerlIO_stderr(), NULL, 0);
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200708#elif defined(PERLIO_LAYERS)
709 vim_IOLayer_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710#endif
711}
712
713/*
714 * perl_end(): clean up after ourselves
715 */
716 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100717perl_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718{
719 if (perl_interp)
720 {
721 perl_run(perl_interp);
722 perl_destruct(perl_interp);
723 perl_free(perl_interp);
724 perl_interp = NULL;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000725#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100726 Perl_sys_term();
Bram Moolenaarc236c162008-07-13 17:41:49 +0000727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 }
729#ifdef DYNAMIC_PERL
730 if (hPerlLib)
731 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200732 close_dll(hPerlLib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 hPerlLib = NULL;
734 }
735#endif
736}
737
738/*
739 * msg_split(): send a message to the message handling routines
740 * split at '\n' first though.
741 */
742 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100743msg_split(
744 char_u *s,
745 int attr) /* highlighting attributes */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746{
747 char *next;
748 char *token = (char *)s;
749
Bram Moolenaaraa8494a2007-10-09 08:47:27 +0000750 while ((next = strchr(token, '\n')) && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 {
752 *next++ = '\0'; /* replace \n with \0 */
753 msg_attr((char_u *)token, attr);
754 token = next;
755 }
Bram Moolenaaraa8494a2007-10-09 08:47:27 +0000756 if (*token && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 msg_attr((char_u *)token, attr);
758}
759
760#ifndef FEAT_EVAL
761/*
762 * This stub is needed because an "#ifdef FEAT_EVAL" around Eval() doesn't
763 * work properly.
764 */
765 char_u *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100766eval_to_string(
767 char_u *arg UNUSED,
768 char_u **nextcmd UNUSED,
769 int dolist UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770{
771 return NULL;
772}
773#endif
774
775/*
776 * Create a new reference to an SV pointing to the SCR structure
Bram Moolenaare344bea2005-09-01 20:46:49 +0000777 * The b_perl_private/w_perl_private part of the SCR structure points to the
778 * SV, so there can only be one such SV for a particular SCR structure. When
779 * the last reference has gone (DESTROY is called),
780 * b_perl_private/w_perl_private is reset; When the screen goes away before
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 * all references are gone, the value of the SV is reset;
782 * any subsequent use of any of those reference will produce
783 * a warning. (see typemap)
784 */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000785
786 static SV *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100787newWINrv(SV *rv, win_T *ptr)
Bram Moolenaare344bea2005-09-01 20:46:49 +0000788{
789 sv_upgrade(rv, SVt_RV);
790 if (ptr->w_perl_private == NULL)
791 {
792 ptr->w_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +0100793 sv_setiv(ptr->w_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +0000794 }
795 else
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200796 SvREFCNT_inc_void_NN(ptr->w_perl_private);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000797 SvRV(rv) = ptr->w_perl_private;
798 SvROK_on(rv);
799 return sv_bless(rv, gv_stashpv("VIWIN", TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800}
801
Bram Moolenaare344bea2005-09-01 20:46:49 +0000802 static SV *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100803newBUFrv(SV *rv, buf_T *ptr)
Bram Moolenaare344bea2005-09-01 20:46:49 +0000804{
805 sv_upgrade(rv, SVt_RV);
806 if (ptr->b_perl_private == NULL)
807 {
808 ptr->b_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +0100809 sv_setiv(ptr->b_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +0000810 }
811 else
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200812 SvREFCNT_inc_void_NN(ptr->b_perl_private);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000813 SvRV(rv) = ptr->b_perl_private;
814 SvROK_on(rv);
815 return sv_bless(rv, gv_stashpv("VIBUF", TRUE));
816}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817
818/*
819 * perl_win_free
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200820 * Remove all references to the window to be destroyed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 */
822 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100823perl_win_free(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000825 if (wp->w_perl_private)
826 sv_setiv((SV *)wp->w_perl_private, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 return;
828}
829
830 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100831perl_buf_free(buf_T *bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000833 if (bp->b_perl_private)
834 sv_setiv((SV *)bp->b_perl_private, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 return;
836}
837
838#ifndef PROTO
839# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
840I32 cur_val(pTHX_ IV iv, SV *sv);
841# else
842I32 cur_val(IV iv, SV *sv);
843#endif
844
845/*
846 * Handler for the magic variables $main::curwin and $main::curbuf.
847 * The handler is put into the magic vtbl for these variables.
848 * (This is effectively a C-level equivalent of a tied variable).
849 * There is no "set" function as the variables are read-only.
850 */
851# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
852I32 cur_val(pTHX_ IV iv, SV *sv)
853# else
854I32 cur_val(IV iv, SV *sv)
855# endif
856{
857 SV *rv;
858 if (iv == 0)
859 rv = newWINrv(newSV(0), curwin);
860 else
861 rv = newBUFrv(newSV(0), curbuf);
862 sv_setsv(sv, rv);
Bram Moolenaar95509e12016-04-15 21:16:11 +0200863 SvREFCNT_dec(SvRV(rv));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 return 0;
865}
866#endif /* !PROTO */
867
868struct ufuncs cw_funcs = { cur_val, 0, 0 };
869struct ufuncs cb_funcs = { cur_val, 0, 1 };
870
871/*
872 * VIM_init(): Vim-specific initialisation.
873 * Make the magical main::curwin and main::curbuf variables
874 */
875 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100876VIM_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877{
878 static char cw[] = "main::curwin";
879 static char cb[] = "main::curbuf";
880 SV *sv;
881
882 sv = perl_get_sv(cw, TRUE);
883 sv_magic(sv, NULL, 'U', (char *)&cw_funcs, sizeof(cw_funcs));
884 SvREADONLY_on(sv);
885
886 sv = perl_get_sv(cb, TRUE);
887 sv_magic(sv, NULL, 'U', (char *)&cb_funcs, sizeof(cb_funcs));
888 SvREADONLY_on(sv);
889
890 /*
891 * Setup the Safe compartment.
892 * It shouldn't be a fatal error if the Safe module is missing.
893 * XXX: Only shares the 'Msg' routine (which has to be called
894 * like 'Msg(...)').
895 */
896 (void)perl_eval_pv( "if ( eval( 'require Safe' ) ) { $VIM::safe = Safe->new(); $VIM::safe->share_from( 'VIM', ['Msg'] ); }", G_DISCARD | G_VOID );
897
898}
899
900#ifdef DYNAMIC_PERL
901static char *e_noperl = N_("Sorry, this command is disabled: the Perl library could not be loaded.");
902#endif
903
904/*
905 * ":perl"
906 */
907 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100908ex_perl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909{
910 char *err;
911 char *script;
912 STRLEN length;
913 SV *sv;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200914#ifdef HAVE_SANDBOX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 SV *safe;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917
918 script = (char *)script_get(eap, eap->arg);
919 if (eap->skip)
920 {
921 vim_free(script);
922 return;
923 }
924
925 if (perl_interp == NULL)
926 {
927#ifdef DYNAMIC_PERL
928 if (!perl_enabled(TRUE))
929 {
930 EMSG(_(e_noperl));
931 vim_free(script);
932 return;
933 }
934#endif
935 perl_init();
936 }
937
938 {
939 dSP;
940 ENTER;
941 SAVETMPS;
942
943 if (script == NULL)
944 sv = newSVpv((char *)eap->arg, 0);
945 else
946 {
947 sv = newSVpv(script, 0);
948 vim_free(script);
949 }
950
951#ifdef HAVE_SANDBOX
952 if (sandbox)
953 {
Bram Moolenaara1711622011-07-27 14:15:46 +0200954 safe = perl_get_sv("VIM::safe", FALSE);
Bram Moolenaar3f947ea2009-07-14 14:04:54 +0000955# ifndef MAKE_TEST /* avoid a warning for unreachable code */
Bram Moolenaar954e8c52009-11-11 13:45:33 +0000956 if (safe == NULL || !SvTRUE(safe))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 EMSG(_("E299: Perl evaluation forbidden in sandbox without the Safe module"));
958 else
Bram Moolenaar3f947ea2009-07-14 14:04:54 +0000959# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 {
961 PUSHMARK(SP);
962 XPUSHs(safe);
963 XPUSHs(sv);
964 PUTBACK;
965 perl_call_method("reval", G_DISCARD);
966 }
967 }
968 else
969#endif
970 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
971
972 SvREFCNT_dec(sv);
973
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100974 err = CHECK_EVAL_ERR(length);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975
976 FREETMPS;
977 LEAVE;
978
979 if (!length)
980 return;
981
982 msg_split((char_u *)err, highlight_attr[HLF_E]);
983 return;
984 }
985}
986
987 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100988replace_line(linenr_T *line, linenr_T *end)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989{
990 char *str;
991
992 if (SvOK(GvSV(PL_defgv)))
993 {
994 str = SvPV(GvSV(PL_defgv), PL_na);
995 ml_replace(*line, (char_u *)str, 1);
996 changed_bytes(*line, 0);
997 }
998 else
999 {
1000 ml_delete(*line, FALSE);
1001 deleted_lines_mark(*line, 1L);
1002 --(*end);
1003 --(*line);
1004 }
1005 return OK;
1006}
1007
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001008static struct ref_map_S {
1009 void *vim_ref;
1010 SV *perl_ref;
1011 struct ref_map_S *next;
1012} *ref_map = NULL;
1013
1014 static void
1015ref_map_free(void)
1016{
1017 struct ref_map_S *tofree;
1018 struct ref_map_S *refs = ref_map;
1019
1020 while (refs) {
1021 tofree = refs;
1022 refs = refs->next;
1023 vim_free(tofree);
1024 }
1025 ref_map = NULL;
1026}
1027
1028 static struct ref_map_S *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001029ref_map_find_SV(SV *const sv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001030{
1031 struct ref_map_S *refs = ref_map;
1032 int count = 350;
1033
1034 while (refs) {
1035 if (refs->perl_ref == sv)
1036 break;
1037 refs = refs->next;
1038 count--;
1039 }
1040
1041 if (!refs && count > 0) {
1042 refs = (struct ref_map_S *)alloc(sizeof(struct ref_map_S));
1043 if (!refs)
1044 return NULL;
1045 refs->perl_ref = sv;
1046 refs->vim_ref = NULL;
1047 refs->next = ref_map;
1048 ref_map = refs;
1049 }
1050
1051 return refs;
1052}
1053
1054 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001055perl_to_vim(SV *sv, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001056{
1057 if (SvROK(sv))
1058 sv = SvRV(sv);
1059
1060 switch (SvTYPE(sv)) {
1061 case SVt_NULL:
1062 break;
1063 case SVt_NV: /* float */
1064#ifdef FEAT_FLOAT
1065 rettv->v_type = VAR_FLOAT;
1066 rettv->vval.v_float = SvNV(sv);
1067 break;
1068#endif
1069 case SVt_IV: /* integer */
1070 if (!SvROK(sv)) { /* references should be string */
1071 rettv->vval.v_number = SvIV(sv);
1072 break;
1073 }
1074 case SVt_PV: /* string */
1075 {
1076 size_t len = 0;
1077 char * str_from = SvPV(sv, len);
Bram Moolenaar9b0ac222016-06-01 20:31:43 +02001078 char_u *str_to = (char_u*)alloc(
1079 (unsigned)(sizeof(char_u) * (len + 1)));
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001080
1081 if (str_to) {
1082 str_to[len] = '\0';
1083
1084 while (len--) {
1085 if (str_from[len] == '\0')
1086 str_to[len] = '\n';
1087 else
1088 str_to[len] = str_from[len];
1089 }
1090 }
1091
1092 rettv->v_type = VAR_STRING;
1093 rettv->vval.v_string = str_to;
1094 break;
1095 }
1096 case SVt_PVAV: /* list */
1097 {
1098 SSize_t size;
1099 listitem_T * item;
1100 SV ** item2;
1101 list_T * list;
1102 struct ref_map_S * refs;
1103
1104 if ((refs = ref_map_find_SV(sv)) == NULL)
1105 return FAIL;
1106
1107 if (refs->vim_ref)
1108 list = (list_T *) refs->vim_ref;
1109 else
1110 {
1111 if ((list = list_alloc()) == NULL)
1112 return FAIL;
1113 refs->vim_ref = list;
1114
1115 for (size = av_len((AV*)sv); size >= 0; size--)
1116 {
1117 if ((item = listitem_alloc()) == NULL)
1118 break;
1119
1120 item->li_tv.v_type = VAR_NUMBER;
1121 item->li_tv.v_lock = 0;
1122 item->li_tv.vval.v_number = 0;
1123 list_insert(list, item, list->lv_first);
1124
1125 item2 = av_fetch((AV *)sv, size, 0);
1126
1127 if (item2 == NULL || *item2 == NULL ||
Bram Moolenaard99df422016-01-29 23:20:40 +01001128 perl_to_vim(*item2, &item->li_tv) == FAIL)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001129 break;
1130 }
1131 }
1132
1133 list->lv_refcount++;
1134 rettv->v_type = VAR_LIST;
1135 rettv->vval.v_list = list;
1136 break;
1137 }
1138 case SVt_PVHV: /* dictionary */
1139 {
1140 HE * entry;
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001141 I32 key_len;
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001142 char * key;
1143 dictitem_T * item;
1144 SV * item2;
1145 dict_T * dict;
1146 struct ref_map_S * refs;
1147
1148 if ((refs = ref_map_find_SV(sv)) == NULL)
1149 return FAIL;
1150
1151 if (refs->vim_ref)
1152 dict = (dict_T *) refs->vim_ref;
1153 else
1154 {
1155
1156 if ((dict = dict_alloc()) == NULL)
1157 return FAIL;
1158 refs->vim_ref = dict;
1159
1160 hv_iterinit((HV *)sv);
1161
1162 for (entry = hv_iternext((HV *)sv); entry; entry = hv_iternext((HV *)sv))
1163 {
1164 key_len = 0;
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001165 key = hv_iterkey(entry, &key_len);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001166
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001167 if (!key || !key_len || strlen(key) < (size_t)key_len) {
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001168 EMSG2("Malformed key Dictionary '%s'", key && *key ? key : "(empty)");
1169 break;
1170 }
1171
1172 if ((item = dictitem_alloc((char_u *)key)) == NULL)
1173 break;
1174
1175 item->di_tv.v_type = VAR_NUMBER;
1176 item->di_tv.v_lock = 0;
1177 item->di_tv.vval.v_number = 0;
1178
1179 if (dict_add(dict, item) == FAIL) {
1180 dictitem_free(item);
1181 break;
1182 }
1183 item2 = hv_iterval((HV *)sv, entry);
1184 if (item2 == NULL || perl_to_vim(item2, &item->di_tv) == FAIL)
1185 break;
1186 }
1187 }
1188
1189 dict->dv_refcount++;
1190 rettv->v_type = VAR_DICT;
1191 rettv->vval.v_dict = dict;
1192 break;
1193 }
1194 default: /* not convertible */
1195 {
1196 char *val = SvPV_nolen(sv);
1197 rettv->v_type = VAR_STRING;
1198 rettv->vval.v_string = val ? vim_strsave((char_u *)val) : NULL;
1199 break;
1200 }
1201 }
1202 return OK;
1203}
1204
1205/*
1206 * "perleval()"
1207 */
1208 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001209do_perleval(char_u *str, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001210{
1211 char *err = NULL;
1212 STRLEN err_len = 0;
1213 SV *sv = NULL;
1214#ifdef HAVE_SANDBOX
1215 SV *safe;
1216#endif
1217
1218 if (perl_interp == NULL)
1219 {
1220#ifdef DYNAMIC_PERL
1221 if (!perl_enabled(TRUE))
1222 {
1223 EMSG(_(e_noperl));
1224 return;
1225 }
1226#endif
1227 perl_init();
1228 }
1229
1230 {
1231 dSP;
1232 ENTER;
1233 SAVETMPS;
1234
1235#ifdef HAVE_SANDBOX
1236 if (sandbox)
1237 {
1238 safe = get_sv("VIM::safe", FALSE);
1239# ifndef MAKE_TEST /* avoid a warning for unreachable code */
1240 if (safe == NULL || !SvTRUE(safe))
1241 EMSG(_("E299: Perl evaluation forbidden in sandbox without the Safe module"));
1242 else
1243# endif
1244 {
1245 sv = newSVpv((char *)str, 0);
1246 PUSHMARK(SP);
1247 XPUSHs(safe);
1248 XPUSHs(sv);
1249 PUTBACK;
1250 call_method("reval", G_SCALAR);
1251 SPAGAIN;
1252 SvREFCNT_dec(sv);
1253 sv = POPs;
1254 }
1255 }
1256 else
1257#endif /* HAVE_SANDBOX */
1258 sv = eval_pv((char *)str, 0);
1259
1260 if (sv) {
1261 perl_to_vim(sv, rettv);
1262 ref_map_free();
1263 err = CHECK_EVAL_ERR(err_len);
1264 }
1265 PUTBACK;
1266 FREETMPS;
1267 LEAVE;
1268 }
1269 if (err_len)
1270 msg_split((char_u *)err, highlight_attr[HLF_E]);
1271}
1272
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273/*
1274 * ":perldo".
1275 */
1276 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001277ex_perldo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278{
1279 STRLEN length;
1280 SV *sv;
1281 char *str;
1282 linenr_T i;
1283
1284 if (bufempty())
1285 return;
1286
1287 if (perl_interp == NULL)
1288 {
1289#ifdef DYNAMIC_PERL
1290 if (!perl_enabled(TRUE))
1291 {
1292 EMSG(_(e_noperl));
1293 return;
1294 }
1295#endif
1296 perl_init();
1297 }
1298 {
1299 dSP;
1300 length = strlen((char *)eap->arg);
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001301 sv = newSV(length + sizeof("sub VIM::perldo {") - 1 + 1);
1302 sv_setpvn(sv, "sub VIM::perldo {", sizeof("sub VIM::perldo {") - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 sv_catpvn(sv, (char *)eap->arg, length);
1304 sv_catpvn(sv, "}", 1);
1305 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
1306 SvREFCNT_dec(sv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001307 str = CHECK_EVAL_ERR(length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 if (length)
1309 goto err;
1310
1311 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
1312 return;
1313
1314 ENTER;
1315 SAVETMPS;
1316 for (i = eap->line1; i <= eap->line2; i++)
1317 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001318 sv_setpv(GvSV(PL_defgv), (char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 PUSHMARK(sp);
1320 perl_call_pv("VIM::perldo", G_SCALAR | G_EVAL);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001321 str = CHECK_EVAL_ERR(length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 if (length)
1323 break;
1324 SPAGAIN;
1325 if (SvTRUEx(POPs))
1326 {
1327 if (replace_line(&i, &eap->line2) != OK)
1328 {
1329 PUTBACK;
1330 break;
1331 }
1332 }
1333 PUTBACK;
1334 }
1335 FREETMPS;
1336 LEAVE;
1337 check_cursor();
1338 update_screen(NOT_VALID);
1339 if (!length)
1340 return;
1341
1342err:
1343 msg_split((char_u *)str, highlight_attr[HLF_E]);
1344 return;
1345 }
1346}
1347
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001348#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
1349typedef struct {
1350 struct _PerlIO base;
1351 int attr;
1352} PerlIOVim;
1353
1354 static IV
1355PerlIOVim_pushed(pTHX_ PerlIO *f, const char *mode,
1356 SV *arg, PerlIO_funcs *tab)
1357{
1358 PerlIOVim *s = PerlIOSelf(f, PerlIOVim);
1359 s->attr = 0;
1360 if (arg && SvPOK(arg)) {
1361 int id = syn_name2id((char_u *)SvPV_nolen(arg));
1362 if (id != 0)
1363 s->attr = syn_id2attr(id);
1364 }
1365 return PerlIOBase_pushed(aTHX_ f, mode, (SV *)NULL, tab);
1366}
1367
1368 static SSize_t
1369PerlIOVim_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1370{
1371 char_u *str;
1372 PerlIOVim * s = PerlIOSelf(f, PerlIOVim);
1373
Bram Moolenaar9b0ac222016-06-01 20:31:43 +02001374 str = vim_strnsave((char_u *)vbuf, (int)count);
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001375 if (str == NULL)
1376 return 0;
1377 msg_split((char_u *)str, s->attr);
1378 vim_free(str);
1379
Bram Moolenaar9b0ac222016-06-01 20:31:43 +02001380 return (SSize_t)count;
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001381}
1382
1383static PERLIO_FUNCS_DECL(PerlIO_Vim) = {
1384 sizeof(PerlIO_funcs),
1385 "Vim",
1386 sizeof(PerlIOVim),
1387 PERLIO_K_DUMMY, /* flags */
1388 PerlIOVim_pushed,
1389 NULL, /* popped */
1390 NULL, /* open */
1391 NULL, /* binmode */
1392 NULL, /* arg */
1393 NULL, /* fileno */
1394 NULL, /* dup */
1395 NULL, /* read */
1396 NULL, /* unread */
1397 PerlIOVim_write,
1398 NULL, /* seek */
1399 NULL, /* tell */
1400 NULL, /* close */
1401 NULL, /* flush */
1402 NULL, /* fill */
1403 NULL, /* eof */
1404 NULL, /* error */
1405 NULL, /* clearerr */
1406 NULL, /* setlinebuf */
1407 NULL, /* get_base */
1408 NULL, /* get_bufsiz */
1409 NULL, /* get_ptr */
1410 NULL, /* get_cnt */
1411 NULL /* set_ptrcnt */
1412};
1413
1414/* Use Vim routine for print operator */
1415 static void
1416vim_IOLayer_init(void)
1417{
1418 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_Vim));
1419 (void)eval_pv( "binmode(STDOUT, ':Vim')"
1420 " && binmode(STDERR, ':Vim(ErrorMsg)');", 0);
1421}
1422#endif /* PERLIO_LAYERS && !USE_SFIO */
1423
Bram Moolenaar01dd60c2008-07-24 14:24:48 +00001424#ifndef FEAT_WINDOWS
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001425 int
1426win_valid(win_T *w)
1427{
1428 return TRUE;
1429}
1430 int
1431win_count(void)
1432{
1433 return 1;
1434}
1435 win_T *
1436win_find_nr(int n)
1437{
1438 return curwin;
1439}
Bram Moolenaar01dd60c2008-07-24 14:24:48 +00001440#endif
1441
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442XS(boot_VIM);
1443
1444 static void
1445xs_init(pTHX)
1446{
1447 char *file = __FILE__;
1448
1449 /* DynaLoader is a special case */
1450 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1451 newXS("VIM::bootstrap", boot_VIM, file);
1452}
1453
1454typedef win_T * VIWIN;
1455typedef buf_T * VIBUF;
1456
1457MODULE = VIM PACKAGE = VIM
1458
1459void
1460Msg(text, hl=NULL)
1461 char *text;
1462 char *hl;
1463
1464 PREINIT:
1465 int attr;
1466 int id;
1467
1468 PPCODE:
1469 if (text != NULL)
1470 {
1471 attr = 0;
1472 if (hl != NULL)
1473 {
1474 id = syn_name2id((char_u *)hl);
1475 if (id != 0)
1476 attr = syn_id2attr(id);
1477 }
1478 msg_split((char_u *)text, attr);
1479 }
1480
1481void
1482SetOption(line)
1483 char *line;
1484
1485 PPCODE:
1486 if (line != NULL)
1487 do_set((char_u *)line, 0);
1488 update_screen(NOT_VALID);
1489
1490void
1491DoCommand(line)
1492 char *line;
1493
1494 PPCODE:
1495 if (line != NULL)
1496 do_cmdline_cmd((char_u *)line);
1497
1498void
1499Eval(str)
1500 char *str;
1501
1502 PREINIT:
1503 char_u *value;
1504 PPCODE:
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001505 value = eval_to_string((char_u *)str, (char_u **)0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 if (value == NULL)
1507 {
1508 XPUSHs(sv_2mortal(newSViv(0)));
1509 XPUSHs(sv_2mortal(newSVpv("", 0)));
1510 }
1511 else
1512 {
1513 XPUSHs(sv_2mortal(newSViv(1)));
1514 XPUSHs(sv_2mortal(newSVpv((char *)value, 0)));
1515 vim_free(value);
1516 }
1517
1518void
1519Buffers(...)
1520
1521 PREINIT:
1522 buf_T *vimbuf;
1523 int i, b;
1524
1525 PPCODE:
1526 if (items == 0)
1527 {
1528 if (GIMME == G_SCALAR)
1529 {
1530 i = 0;
1531 for (vimbuf = firstbuf; vimbuf; vimbuf = vimbuf->b_next)
1532 ++i;
1533
1534 XPUSHs(sv_2mortal(newSViv(i)));
1535 }
1536 else
1537 {
1538 for (vimbuf = firstbuf; vimbuf; vimbuf = vimbuf->b_next)
1539 XPUSHs(newBUFrv(newSV(0), vimbuf));
1540 }
1541 }
1542 else
1543 {
1544 for (i = 0; i < items; i++)
1545 {
1546 SV *sv = ST(i);
1547 if (SvIOK(sv))
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001548 b = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 else
1550 {
1551 char_u *pat;
1552 STRLEN len;
1553
1554 pat = (char_u *)SvPV(sv, len);
1555 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001556 b = buflist_findpat(pat, pat+len, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 --emsg_off;
1558 }
1559
1560 if (b >= 0)
1561 {
1562 vimbuf = buflist_findnr(b);
1563 if (vimbuf)
1564 XPUSHs(newBUFrv(newSV(0), vimbuf));
1565 }
1566 }
1567 }
1568
1569void
1570Windows(...)
1571
1572 PREINIT:
1573 win_T *vimwin;
1574 int i, w;
1575
1576 PPCODE:
1577 if (items == 0)
1578 {
1579 if (GIMME == G_SCALAR)
1580 XPUSHs(sv_2mortal(newSViv(win_count())));
1581 else
1582 {
1583 for (vimwin = firstwin; vimwin != NULL; vimwin = W_NEXT(vimwin))
1584 XPUSHs(newWINrv(newSV(0), vimwin));
1585 }
1586 }
1587 else
1588 {
1589 for (i = 0; i < items; i++)
1590 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001591 w = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 vimwin = win_find_nr(w);
1593 if (vimwin)
1594 XPUSHs(newWINrv(newSV(0), vimwin));
1595 }
1596 }
1597
1598MODULE = VIM PACKAGE = VIWIN
1599
1600void
1601DESTROY(win)
1602 VIWIN win
1603
1604 CODE:
1605 if (win_valid(win))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001606 win->w_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607
1608SV *
1609Buffer(win)
1610 VIWIN win
1611
1612 CODE:
1613 if (!win_valid(win))
1614 win = curwin;
1615 RETVAL = newBUFrv(newSV(0), win->w_buffer);
1616 OUTPUT:
1617 RETVAL
1618
1619void
1620SetHeight(win, height)
1621 VIWIN win
1622 int height;
1623
1624 PREINIT:
1625 win_T *savewin;
1626
1627 PPCODE:
1628 if (!win_valid(win))
1629 win = curwin;
1630 savewin = curwin;
1631 curwin = win;
1632 win_setheight(height);
1633 curwin = savewin;
1634
1635void
Bram Moolenaar864733a2016-04-02 14:18:01 +02001636Cursor(win, ...)
1637 VIWIN win
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638
1639 PPCODE:
Bram Moolenaara1711622011-07-27 14:15:46 +02001640 if (items == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 {
1642 EXTEND(sp, 2);
1643 if (!win_valid(win))
1644 win = curwin;
1645 PUSHs(sv_2mortal(newSViv(win->w_cursor.lnum)));
1646 PUSHs(sv_2mortal(newSViv(win->w_cursor.col)));
1647 }
Bram Moolenaara1711622011-07-27 14:15:46 +02001648 else if (items == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 {
1650 int lnum, col;
1651
1652 if (!win_valid(win))
1653 win = curwin;
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001654 lnum = (int) SvIV(ST(1));
1655 col = (int) SvIV(ST(2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 win->w_cursor.lnum = lnum;
1657 win->w_cursor.col = col;
1658 check_cursor(); /* put cursor on an existing line */
1659 update_screen(NOT_VALID);
1660 }
1661
1662MODULE = VIM PACKAGE = VIBUF
1663
1664void
1665DESTROY(vimbuf)
1666 VIBUF vimbuf;
1667
1668 CODE:
1669 if (buf_valid(vimbuf))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001670 vimbuf->b_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671
1672void
1673Name(vimbuf)
1674 VIBUF vimbuf;
1675
1676 PPCODE:
1677 if (!buf_valid(vimbuf))
1678 vimbuf = curbuf;
1679 /* No file name returns an empty string */
1680 if (vimbuf->b_fname == NULL)
1681 XPUSHs(sv_2mortal(newSVpv("", 0)));
1682 else
1683 XPUSHs(sv_2mortal(newSVpv((char *)vimbuf->b_fname, 0)));
1684
1685void
1686Number(vimbuf)
1687 VIBUF vimbuf;
1688
1689 PPCODE:
1690 if (!buf_valid(vimbuf))
1691 vimbuf = curbuf;
1692 XPUSHs(sv_2mortal(newSViv(vimbuf->b_fnum)));
1693
1694void
1695Count(vimbuf)
1696 VIBUF vimbuf;
1697
1698 PPCODE:
1699 if (!buf_valid(vimbuf))
1700 vimbuf = curbuf;
1701 XPUSHs(sv_2mortal(newSViv(vimbuf->b_ml.ml_line_count)));
1702
1703void
1704Get(vimbuf, ...)
1705 VIBUF vimbuf;
1706
1707 PREINIT:
1708 char_u *line;
1709 int i;
1710 long lnum;
1711 PPCODE:
1712 if (buf_valid(vimbuf))
1713 {
1714 for (i = 1; i < items; i++)
1715 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001716 lnum = (long) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1718 {
1719 line = ml_get_buf(vimbuf, lnum, FALSE);
1720 XPUSHs(sv_2mortal(newSVpv((char *)line, 0)));
1721 }
1722 }
1723 }
1724
1725void
1726Set(vimbuf, ...)
1727 VIBUF vimbuf;
1728
1729 PREINIT:
1730 int i;
1731 long lnum;
1732 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 PPCODE:
1734 if (buf_valid(vimbuf))
1735 {
1736 if (items < 3)
1737 croak("Usage: VIBUF::Set(vimbuf, lnum, @lines)");
1738
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001739 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 for(i = 2; i < items; i++, lnum++)
1741 {
1742 line = SvPV(ST(i),PL_na);
1743 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1744 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001745 aco_save_T aco;
1746
1747 /* set curwin/curbuf for "vimbuf" and save some things */
1748 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001749
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 if (u_savesub(lnum) == OK)
1751 {
1752 ml_replace(lnum, (char_u *)line, TRUE);
1753 changed_bytes(lnum, 0);
1754 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001755
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001756 /* restore curwin/curbuf and a few other things */
1757 aucmd_restbuf(&aco);
1758 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 }
1760 }
1761 }
1762
1763void
1764Delete(vimbuf, ...)
1765 VIBUF vimbuf;
1766
1767 PREINIT:
1768 long i, lnum = 0, count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 PPCODE:
1770 if (buf_valid(vimbuf))
1771 {
1772 if (items == 2)
1773 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001774 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 count = 1;
1776 }
1777 else if (items == 3)
1778 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001779 lnum = (long) SvIV(ST(1));
1780 count = (long) 1 + SvIV(ST(2)) - lnum;
Bram Moolenaara1711622011-07-27 14:15:46 +02001781 if (count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 count = 1;
Bram Moolenaara1711622011-07-27 14:15:46 +02001783 if (count < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 {
1785 lnum -= count;
1786 count = -count;
1787 }
1788 }
1789 if (items >= 2)
1790 {
1791 for (i = 0; i < count; i++)
1792 {
1793 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1794 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001795 aco_save_T aco;
1796
1797 /* set curwin/curbuf for "vimbuf" and save some things */
1798 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001799
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 if (u_savedel(lnum, 1) == OK)
1801 {
1802 ml_delete(lnum, 0);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00001803 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 deleted_lines_mark(lnum, 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001806
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001807 /* restore curwin/curbuf and a few other things */
1808 aucmd_restbuf(&aco);
1809 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001810
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 update_curbuf(VALID);
1812 }
1813 }
1814 }
1815 }
1816
1817void
1818Append(vimbuf, ...)
1819 VIBUF vimbuf;
1820
1821 PREINIT:
1822 int i;
1823 long lnum;
1824 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 PPCODE:
1826 if (buf_valid(vimbuf))
1827 {
1828 if (items < 3)
1829 croak("Usage: VIBUF::Append(vimbuf, lnum, @lines)");
1830
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001831 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 for (i = 2; i < items; i++, lnum++)
1833 {
1834 line = SvPV(ST(i),PL_na);
1835 if (lnum >= 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1836 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001837 aco_save_T aco;
1838
1839 /* set curwin/curbuf for "vimbuf" and save some things */
1840 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001841
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 if (u_inssub(lnum + 1) == OK)
1843 {
1844 ml_append(lnum, (char_u *)line, (colnr_T)0, FALSE);
1845 appended_lines_mark(lnum, 1L);
1846 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001847
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001848 /* restore curwin/curbuf and a few other things */
1849 aucmd_restbuf(&aco);
1850 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001851
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 update_curbuf(VALID);
1853 }
1854 }
1855 }
1856
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001857#ifdef __GNUC__
1858# pragma GCC diagnostic pop
1859#endif