blob: b091bf7cab7977c1f77b57134258f81e45d65187 [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 Moolenaarc236c162008-07-13 17:41:49 +0000302
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303/*
304 * Declare HANDLE for perl.dll and function pointers.
305 */
306static HANDLE hPerlLib = NULL;
307
308static PerlInterpreter* (*perl_alloc)();
309static void (*perl_construct)(PerlInterpreter*);
310static void (*perl_destruct)(PerlInterpreter*);
311static void (*perl_free)(PerlInterpreter*);
312static int (*perl_run)(PerlInterpreter*);
313static int (*perl_parse)(PerlInterpreter*, XSINIT_t, int, char**, char**);
314static void* (*Perl_get_context)(void);
Bram Moolenaar864733a2016-04-02 14:18:01 +0200315static void (*Perl_croak)(pTHX_ const char*, ...) __attribute__noreturn__;
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100316#ifdef PERL5101_OR_LATER
Bram Moolenaar6b107212013-12-11 15:06:40 +0100317/* Perl-5.18 has a different Perl_croak_xs_usage signature. */
318# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
Bram Moolenaar864733a2016-04-02 14:18:01 +0200319static void (*Perl_croak_xs_usage)(const CV *const, const char *const params)
320 __attribute__noreturn__;
Bram Moolenaar6b107212013-12-11 15:06:40 +0100321# else
Bram Moolenaar864733a2016-04-02 14:18:01 +0200322static void (*Perl_croak_xs_usage)(pTHX_ const CV *const, const char *const params)
323 __attribute__noreturn__;
Bram Moolenaar6b107212013-12-11 15:06:40 +0100324# endif
Bram Moolenaar9be6e212013-06-15 16:47:35 +0200325#endif
Bram Moolenaar864733a2016-04-02 14:18:01 +0200326static void (*Perl_croak_nocontext)(const char*, ...) __attribute__noreturn__;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327static I32 (*Perl_dowantarray)(pTHX);
328static void (*Perl_free_tmps)(pTHX);
329static HV* (*Perl_gv_stashpv)(pTHX_ const char*, I32);
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200330#if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
331static I32* (*Perl_markstack_grow)(pTHX);
332#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333static void (*Perl_markstack_grow)(pTHX);
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335static MAGIC* (*Perl_mg_find)(pTHX_ SV*, int);
336static CV* (*Perl_newXS)(pTHX_ char*, XSUBADDR_t, char*);
337static SV* (*Perl_newSV)(pTHX_ STRLEN);
338static SV* (*Perl_newSViv)(pTHX_ IV);
339static SV* (*Perl_newSVpv)(pTHX_ const char*, STRLEN);
340static I32 (*Perl_call_argv)(pTHX_ const char*, I32, char**);
341static I32 (*Perl_call_pv)(pTHX_ const char*, I32);
342static I32 (*Perl_eval_sv)(pTHX_ SV*, I32);
343static SV* (*Perl_get_sv)(pTHX_ const char*, I32);
344static SV* (*Perl_eval_pv)(pTHX_ const char*, I32);
345static SV* (*Perl_call_method)(pTHX_ const char*, I32);
346static void (*Perl_pop_scope)(pTHX);
347static void (*Perl_push_scope)(pTHX);
348static void (*Perl_save_int)(pTHX_ int*);
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200349#if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
350static void (*Perl_save_strlen)(pTHX_ STRLEN* ptr);
351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352static SV** (*Perl_stack_grow)(pTHX_ SV**, SV**p, int);
353static SV** (*Perl_set_context)(void*);
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200354#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
355static bool (*Perl_sv_2bool_flags)(pTHX_ SV*, I32);
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200356# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200357static void (*Perl_xs_apiversion_bootcheck)(pTHX_ SV *module, const char *api_p, STRLEN api_len);
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200358# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200359#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360static bool (*Perl_sv_2bool)(pTHX_ SV*);
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362static IV (*Perl_sv_2iv)(pTHX_ SV*);
363static SV* (*Perl_sv_2mortal)(pTHX_ SV*);
364#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
365static char* (*Perl_sv_2pv_flags)(pTHX_ SV*, STRLEN*, I32);
366static char* (*Perl_sv_2pv_nolen)(pTHX_ SV*);
367#else
368static char* (*Perl_sv_2pv)(pTHX_ SV*, STRLEN*);
369#endif
370static SV* (*Perl_sv_bless)(pTHX_ SV*, HV*);
371#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
372static void (*Perl_sv_catpvn_flags)(pTHX_ SV* , const char*, STRLEN, I32);
373#else
374static void (*Perl_sv_catpvn)(pTHX_ SV*, const char*, STRLEN);
375#endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000376#ifdef PERL589_OR_LATER
377static IV (*Perl_sv_2iv_flags)(pTHX_ SV* sv, I32 flags);
378static CV * (*Perl_newXS_flags)(pTHX_ const char *name, XSUBADDR_t subaddr, const char *const filename, const char *const proto, U32 flags);
379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380static void (*Perl_sv_free)(pTHX_ SV*);
381static int (*Perl_sv_isa)(pTHX_ SV*, const char*);
382static void (*Perl_sv_magic)(pTHX_ SV*, SV*, int, const char*, I32);
383static void (*Perl_sv_setiv)(pTHX_ SV*, IV);
384static void (*Perl_sv_setpv)(pTHX_ SV*, const char*);
385static void (*Perl_sv_setpvn)(pTHX_ SV*, const char*, STRLEN);
386#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
387static void (*Perl_sv_setsv_flags)(pTHX_ SV*, SV*, I32);
388#else
389static void (*Perl_sv_setsv)(pTHX_ SV*, SV*);
390#endif
391static bool (*Perl_sv_upgrade)(pTHX_ SV*, U32);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200392#if (PERL_REVISION == 5) && (PERL_VERSION < 10)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393static SV*** (*Perl_Tstack_sp_ptr)(register PerlInterpreter*);
394static OP** (*Perl_Top_ptr)(register PerlInterpreter*);
395static SV*** (*Perl_Tstack_base_ptr)(register PerlInterpreter*);
396static SV*** (*Perl_Tstack_max_ptr)(register PerlInterpreter*);
397static I32* (*Perl_Ttmps_ix_ptr)(register PerlInterpreter*);
398static I32* (*Perl_Ttmps_floor_ptr)(register PerlInterpreter*);
399static I32** (*Perl_Tmarkstack_ptr_ptr)(register PerlInterpreter*);
400static I32** (*Perl_Tmarkstack_max_ptr)(register PerlInterpreter*);
401static SV** (*Perl_TSv_ptr)(register PerlInterpreter*);
402static XPV** (*Perl_TXpv_ptr)(register PerlInterpreter*);
403static STRLEN* (*Perl_Tna_ptr)(register PerlInterpreter*);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200404#else
Bram Moolenaar6b107212013-12-11 15:06:40 +0100405/* Perl-5.18 has a different Perl_sv_free2 signature. */
406# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
407static void (*Perl_sv_free2)(pTHX_ SV*, const U32);
408# else
Bram Moolenaarccf22172008-09-01 15:56:45 +0000409static void (*Perl_sv_free2)(pTHX_ SV*);
Bram Moolenaar6b107212013-12-11 15:06:40 +0100410# endif
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000411static void (*Perl_sys_init)(int* argc, char*** argv);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000412static void (*Perl_sys_term)(void);
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200413static void (*Perl_call_list)(pTHX_ I32, AV*);
414# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
415# else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000416static SV** (*Perl_ISv_ptr)(register PerlInterpreter*);
417static SV*** (*Perl_Istack_max_ptr)(register PerlInterpreter*);
418static SV*** (*Perl_Istack_base_ptr)(register PerlInterpreter*);
419static XPV** (*Perl_IXpv_ptr)(register PerlInterpreter*);
420static I32* (*Perl_Itmps_ix_ptr)(register PerlInterpreter*);
421static I32* (*Perl_Itmps_floor_ptr)(register PerlInterpreter*);
422static STRLEN* (*Perl_Ina_ptr)(register PerlInterpreter*);
423static I32** (*Perl_Imarkstack_ptr_ptr)(register PerlInterpreter*);
424static I32** (*Perl_Imarkstack_max_ptr)(register PerlInterpreter*);
425static SV*** (*Perl_Istack_sp_ptr)(register PerlInterpreter*);
426static OP** (*Perl_Iop_ptr)(register PerlInterpreter*);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000427static I32* (*Perl_Iscopestack_ix_ptr)(register PerlInterpreter*);
428static AV** (*Perl_Iunitcheckav_ptr)(register PerlInterpreter*);
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200429# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000430#endif
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200431#if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
432static I32 (*Perl_xs_handshake)(const U32, void *, const char *, ...);
433static void (*Perl_xs_boot_epilog)(pTHX_ const U32);
434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200436#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100437# ifdef USE_ITHREADS
Bram Moolenaar01c10522012-09-21 12:50:51 +0200438static perl_key* dll_PL_thr_key;
Bram Moolenaard8619992014-03-12 17:08:05 +0100439# endif
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200440#else
Bram Moolenaare06c1882010-07-21 22:05:20 +0200441static GV** (*Perl_Idefgv_ptr)(register PerlInterpreter*);
442static GV** (*Perl_Ierrgv_ptr)(register PerlInterpreter*);
443static SV* (*Perl_Isv_yes_ptr)(register PerlInterpreter*);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200444static perl_key* (*Perl_Gthr_key_ptr)_((pTHX));
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200445#endif
446static void (*boot_DynaLoader)_((pTHX_ CV*));
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100447static HE * (*Perl_hv_iternext_flags)(pTHX_ HV *, I32);
448static I32 (*Perl_hv_iterinit)(pTHX_ HV *);
449static char * (*Perl_hv_iterkey)(pTHX_ HE *, I32 *);
450static SV * (*Perl_hv_iterval)(pTHX_ HV *, HE *);
451static SV** (*Perl_av_fetch)(pTHX_ AV *, SSize_t, I32);
452static SSize_t (*Perl_av_len)(pTHX_ AV *);
453static NV (*Perl_sv_2nv_flags)(pTHX_ SV *const, const I32);
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200454#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
455static IV (*PerlIOBase_pushed)(pTHX_ PerlIO *, const char *, SV *, PerlIO_funcs *);
456static void (*PerlIO_define_layer)(pTHX_ PerlIO_funcs *);
457#endif
Bram Moolenaare06c1882010-07-21 22:05:20 +0200458
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459/*
460 * Table of name to function pointer of perl.
461 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462static struct {
463 char* name;
464 PERL_PROC* ptr;
465} perl_funcname_table[] = {
466 {"perl_alloc", (PERL_PROC*)&perl_alloc},
467 {"perl_construct", (PERL_PROC*)&perl_construct},
468 {"perl_destruct", (PERL_PROC*)&perl_destruct},
469 {"perl_free", (PERL_PROC*)&perl_free},
470 {"perl_run", (PERL_PROC*)&perl_run},
471 {"perl_parse", (PERL_PROC*)&perl_parse},
472 {"Perl_get_context", (PERL_PROC*)&Perl_get_context},
473 {"Perl_croak", (PERL_PROC*)&Perl_croak},
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100474#ifdef PERL5101_OR_LATER
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100475 {"Perl_croak_xs_usage", (PERL_PROC*)&Perl_croak_xs_usage},
476#endif
Bram Moolenaard8619992014-03-12 17:08:05 +0100477#ifdef PERL_IMPLICIT_CONTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 {"Perl_croak_nocontext", (PERL_PROC*)&Perl_croak_nocontext},
Bram Moolenaard8619992014-03-12 17:08:05 +0100479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 {"Perl_dowantarray", (PERL_PROC*)&Perl_dowantarray},
481 {"Perl_free_tmps", (PERL_PROC*)&Perl_free_tmps},
482 {"Perl_gv_stashpv", (PERL_PROC*)&Perl_gv_stashpv},
483 {"Perl_markstack_grow", (PERL_PROC*)&Perl_markstack_grow},
484 {"Perl_mg_find", (PERL_PROC*)&Perl_mg_find},
485 {"Perl_newXS", (PERL_PROC*)&Perl_newXS},
486 {"Perl_newSV", (PERL_PROC*)&Perl_newSV},
487 {"Perl_newSViv", (PERL_PROC*)&Perl_newSViv},
488 {"Perl_newSVpv", (PERL_PROC*)&Perl_newSVpv},
489 {"Perl_call_argv", (PERL_PROC*)&Perl_call_argv},
490 {"Perl_call_pv", (PERL_PROC*)&Perl_call_pv},
491 {"Perl_eval_sv", (PERL_PROC*)&Perl_eval_sv},
492 {"Perl_get_sv", (PERL_PROC*)&Perl_get_sv},
493 {"Perl_eval_pv", (PERL_PROC*)&Perl_eval_pv},
494 {"Perl_call_method", (PERL_PROC*)&Perl_call_method},
495 {"Perl_pop_scope", (PERL_PROC*)&Perl_pop_scope},
496 {"Perl_push_scope", (PERL_PROC*)&Perl_push_scope},
497 {"Perl_save_int", (PERL_PROC*)&Perl_save_int},
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200498#if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
499 {"Perl_save_strlen", (PERL_PROC*)&Perl_save_strlen},
500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 {"Perl_stack_grow", (PERL_PROC*)&Perl_stack_grow},
502 {"Perl_set_context", (PERL_PROC*)&Perl_set_context},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200503#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
504 {"Perl_sv_2bool_flags", (PERL_PROC*)&Perl_sv_2bool_flags},
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200505# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200506 {"Perl_xs_apiversion_bootcheck",(PERL_PROC*)&Perl_xs_apiversion_bootcheck},
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200507# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200508#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 {"Perl_sv_2bool", (PERL_PROC*)&Perl_sv_2bool},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 {"Perl_sv_2iv", (PERL_PROC*)&Perl_sv_2iv},
512 {"Perl_sv_2mortal", (PERL_PROC*)&Perl_sv_2mortal},
513#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
514 {"Perl_sv_2pv_flags", (PERL_PROC*)&Perl_sv_2pv_flags},
515 {"Perl_sv_2pv_nolen", (PERL_PROC*)&Perl_sv_2pv_nolen},
516#else
517 {"Perl_sv_2pv", (PERL_PROC*)&Perl_sv_2pv},
518#endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000519#ifdef PERL589_OR_LATER
520 {"Perl_sv_2iv_flags", (PERL_PROC*)&Perl_sv_2iv_flags},
521 {"Perl_newXS_flags", (PERL_PROC*)&Perl_newXS_flags},
522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 {"Perl_sv_bless", (PERL_PROC*)&Perl_sv_bless},
524#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
525 {"Perl_sv_catpvn_flags", (PERL_PROC*)&Perl_sv_catpvn_flags},
526#else
527 {"Perl_sv_catpvn", (PERL_PROC*)&Perl_sv_catpvn},
528#endif
529 {"Perl_sv_free", (PERL_PROC*)&Perl_sv_free},
530 {"Perl_sv_isa", (PERL_PROC*)&Perl_sv_isa},
531 {"Perl_sv_magic", (PERL_PROC*)&Perl_sv_magic},
532 {"Perl_sv_setiv", (PERL_PROC*)&Perl_sv_setiv},
533 {"Perl_sv_setpv", (PERL_PROC*)&Perl_sv_setpv},
534 {"Perl_sv_setpvn", (PERL_PROC*)&Perl_sv_setpvn},
535#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
536 {"Perl_sv_setsv_flags", (PERL_PROC*)&Perl_sv_setsv_flags},
537#else
538 {"Perl_sv_setsv", (PERL_PROC*)&Perl_sv_setsv},
539#endif
540 {"Perl_sv_upgrade", (PERL_PROC*)&Perl_sv_upgrade},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000541#if (PERL_REVISION == 5) && (PERL_VERSION < 10)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 {"Perl_Tstack_sp_ptr", (PERL_PROC*)&Perl_Tstack_sp_ptr},
543 {"Perl_Top_ptr", (PERL_PROC*)&Perl_Top_ptr},
544 {"Perl_Tstack_base_ptr", (PERL_PROC*)&Perl_Tstack_base_ptr},
545 {"Perl_Tstack_max_ptr", (PERL_PROC*)&Perl_Tstack_max_ptr},
546 {"Perl_Ttmps_ix_ptr", (PERL_PROC*)&Perl_Ttmps_ix_ptr},
547 {"Perl_Ttmps_floor_ptr", (PERL_PROC*)&Perl_Ttmps_floor_ptr},
548 {"Perl_Tmarkstack_ptr_ptr", (PERL_PROC*)&Perl_Tmarkstack_ptr_ptr},
549 {"Perl_Tmarkstack_max_ptr", (PERL_PROC*)&Perl_Tmarkstack_max_ptr},
550 {"Perl_TSv_ptr", (PERL_PROC*)&Perl_TSv_ptr},
551 {"Perl_TXpv_ptr", (PERL_PROC*)&Perl_TXpv_ptr},
552 {"Perl_Tna_ptr", (PERL_PROC*)&Perl_Tna_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000553#else
Bram Moolenaarccf22172008-09-01 15:56:45 +0000554 {"Perl_sv_free2", (PERL_PROC*)&Perl_sv_free2},
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000555 {"Perl_sys_init", (PERL_PROC*)&Perl_sys_init},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000556 {"Perl_sys_term", (PERL_PROC*)&Perl_sys_term},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200557 {"Perl_call_list", (PERL_PROC*)&Perl_call_list},
558# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
559# else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000560 {"Perl_ISv_ptr", (PERL_PROC*)&Perl_ISv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000561 {"Perl_Istack_max_ptr", (PERL_PROC*)&Perl_Istack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200562 {"Perl_Istack_base_ptr", (PERL_PROC*)&Perl_Istack_base_ptr},
563 {"Perl_IXpv_ptr", (PERL_PROC*)&Perl_IXpv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000564 {"Perl_Itmps_ix_ptr", (PERL_PROC*)&Perl_Itmps_ix_ptr},
565 {"Perl_Itmps_floor_ptr", (PERL_PROC*)&Perl_Itmps_floor_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200566 {"Perl_Ina_ptr", (PERL_PROC*)&Perl_Ina_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000567 {"Perl_Imarkstack_ptr_ptr", (PERL_PROC*)&Perl_Imarkstack_ptr_ptr},
568 {"Perl_Imarkstack_max_ptr", (PERL_PROC*)&Perl_Imarkstack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200569 {"Perl_Istack_sp_ptr", (PERL_PROC*)&Perl_Istack_sp_ptr},
570 {"Perl_Iop_ptr", (PERL_PROC*)&Perl_Iop_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000571 {"Perl_Iscopestack_ix_ptr", (PERL_PROC*)&Perl_Iscopestack_ix_ptr},
572 {"Perl_Iunitcheckav_ptr", (PERL_PROC*)&Perl_Iunitcheckav_ptr},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200573# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000574#endif
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200575#if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
576 {"Perl_xs_handshake", (PERL_PROC*)&Perl_xs_handshake},
577 {"Perl_xs_boot_epilog", (PERL_PROC*)&Perl_xs_boot_epilog},
578#endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200579#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100580# ifdef USE_ITHREADS
Bram Moolenaar01c10522012-09-21 12:50:51 +0200581 {"PL_thr_key", (PERL_PROC*)&dll_PL_thr_key},
Bram Moolenaard8619992014-03-12 17:08:05 +0100582# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200583#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 {"Perl_Idefgv_ptr", (PERL_PROC*)&Perl_Idefgv_ptr},
585 {"Perl_Ierrgv_ptr", (PERL_PROC*)&Perl_Ierrgv_ptr},
586 {"Perl_Isv_yes_ptr", (PERL_PROC*)&Perl_Isv_yes_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200587 {"Perl_Gthr_key_ptr", (PERL_PROC*)&Perl_Gthr_key_ptr},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200588#endif
589 {"boot_DynaLoader", (PERL_PROC*)&boot_DynaLoader},
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100590 {"Perl_hv_iternext_flags", (PERL_PROC*)&Perl_hv_iternext_flags},
591 {"Perl_hv_iterinit", (PERL_PROC*)&Perl_hv_iterinit},
592 {"Perl_hv_iterkey", (PERL_PROC*)&Perl_hv_iterkey},
593 {"Perl_hv_iterval", (PERL_PROC*)&Perl_hv_iterval},
594 {"Perl_av_fetch", (PERL_PROC*)&Perl_av_fetch},
595 {"Perl_av_len", (PERL_PROC*)&Perl_av_len},
596 {"Perl_sv_2nv_flags", (PERL_PROC*)&Perl_sv_2nv_flags},
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200597#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
598 {"PerlIOBase_pushed", (PERL_PROC*)&PerlIOBase_pushed},
599 {"PerlIO_define_layer", (PERL_PROC*)&PerlIO_define_layer},
600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 {"", NULL},
602};
603
Bram Moolenaar6b107212013-12-11 15:06:40 +0100604/* Work around for perl-5.18.
605 * The definitions of S_SvREFCNT_inc and S_SvREFCNT_dec are needed, so include
606 * "perl\lib\CORE\inline.h", after Perl_sv_free2 is defined.
607 * The linker won't complain about undefined __impl_Perl_sv_free2. */
608#if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
Bram Moolenaar864733a2016-04-02 14:18:01 +0200609# define PL_memory_wrap "panic: memory wrap" /* Dummy */
Bram Moolenaar6b107212013-12-11 15:06:40 +0100610# include <inline.h>
Bram Moolenaar864733a2016-04-02 14:18:01 +0200611# undef PL_memory_wrap
Bram Moolenaar6b107212013-12-11 15:06:40 +0100612#endif
613
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614/*
615 * Make all runtime-links of perl.
616 *
Bram Moolenaar364ab2f2013-08-02 20:05:32 +0200617 * 1. Get module handle using dlopen() or vimLoadLib().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 * 2. Get pointer to perl function by GetProcAddress.
619 * 3. Repeat 2, until get all functions will be used.
620 *
621 * Parameter 'libname' provides name of DLL.
622 * Return OK or FAIL.
623 */
624 static int
625perl_runtime_link_init(char *libname, int verbose)
626{
627 int i;
628
629 if (hPerlLib != NULL)
630 return OK;
Bram Moolenaare06c1882010-07-21 22:05:20 +0200631 if ((hPerlLib = load_dll(libname)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 {
633 if (verbose)
634 EMSG2(_("E370: Could not load library %s"), libname);
635 return FAIL;
636 }
637 for (i = 0; perl_funcname_table[i].ptr; ++i)
638 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200639 if (!(*perl_funcname_table[i].ptr = symbol_from_dll(hPerlLib,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 perl_funcname_table[i].name)))
641 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200642 close_dll(hPerlLib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 hPerlLib = NULL;
644 if (verbose)
645 EMSG2(_(e_loadfunc), perl_funcname_table[i].name);
646 return FAIL;
647 }
648 }
649 return OK;
650}
651
652/*
653 * If runtime-link-perl(DLL) was loaded successfully, return TRUE.
654 * There were no DLL loaded, return FALSE.
655 */
656 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100657perl_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100659 return perl_runtime_link_init((char *)p_perldll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660}
661#endif /* DYNAMIC_PERL */
662
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200663#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
664static void vim_IOLayer_init(void);
665#endif
666
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667/*
668 * perl_init(): initialize perl interpreter
669 * We have to call perl_parse to initialize some structures,
670 * there's nothing to actually parse.
671 */
672 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100673perl_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674{
Bram Moolenaarc236c162008-07-13 17:41:49 +0000675 char *bootargs[] = { "VI", NULL };
676 int argc = 3;
677 static char *argv[] = { "", "-e", "" };
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678
Bram Moolenaarc236c162008-07-13 17:41:49 +0000679#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000680 Perl_sys_init(&argc, (char***)&argv);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 perl_interp = perl_alloc();
683 perl_construct(perl_interp);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000684 perl_parse(perl_interp, xs_init, argc, argv, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 perl_call_argv("VIM::bootstrap", (long)G_DISCARD, bootargs);
686 VIM_init();
687#ifdef USE_SFIO
688 sfdisc(PerlIO_stdout(), sfdcnewvim());
689 sfdisc(PerlIO_stderr(), sfdcnewvim());
690 sfsetbuf(PerlIO_stdout(), NULL, 0);
691 sfsetbuf(PerlIO_stderr(), NULL, 0);
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200692#elif defined(PERLIO_LAYERS)
693 vim_IOLayer_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694#endif
695}
696
697/*
698 * perl_end(): clean up after ourselves
699 */
700 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100701perl_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702{
703 if (perl_interp)
704 {
705 perl_run(perl_interp);
706 perl_destruct(perl_interp);
707 perl_free(perl_interp);
708 perl_interp = NULL;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000709#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100710 Perl_sys_term();
Bram Moolenaarc236c162008-07-13 17:41:49 +0000711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 }
713#ifdef DYNAMIC_PERL
714 if (hPerlLib)
715 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200716 close_dll(hPerlLib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 hPerlLib = NULL;
718 }
719#endif
720}
721
722/*
723 * msg_split(): send a message to the message handling routines
724 * split at '\n' first though.
725 */
726 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100727msg_split(
728 char_u *s,
729 int attr) /* highlighting attributes */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730{
731 char *next;
732 char *token = (char *)s;
733
Bram Moolenaaraa8494a2007-10-09 08:47:27 +0000734 while ((next = strchr(token, '\n')) && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 {
736 *next++ = '\0'; /* replace \n with \0 */
737 msg_attr((char_u *)token, attr);
738 token = next;
739 }
Bram Moolenaaraa8494a2007-10-09 08:47:27 +0000740 if (*token && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 msg_attr((char_u *)token, attr);
742}
743
744#ifndef FEAT_EVAL
745/*
746 * This stub is needed because an "#ifdef FEAT_EVAL" around Eval() doesn't
747 * work properly.
748 */
749 char_u *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100750eval_to_string(
751 char_u *arg UNUSED,
752 char_u **nextcmd UNUSED,
753 int dolist UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754{
755 return NULL;
756}
757#endif
758
759/*
760 * Create a new reference to an SV pointing to the SCR structure
Bram Moolenaare344bea2005-09-01 20:46:49 +0000761 * The b_perl_private/w_perl_private part of the SCR structure points to the
762 * SV, so there can only be one such SV for a particular SCR structure. When
763 * the last reference has gone (DESTROY is called),
764 * b_perl_private/w_perl_private is reset; When the screen goes away before
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 * all references are gone, the value of the SV is reset;
766 * any subsequent use of any of those reference will produce
767 * a warning. (see typemap)
768 */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000769
770 static SV *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100771newWINrv(SV *rv, win_T *ptr)
Bram Moolenaare344bea2005-09-01 20:46:49 +0000772{
773 sv_upgrade(rv, SVt_RV);
774 if (ptr->w_perl_private == NULL)
775 {
776 ptr->w_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +0100777 sv_setiv(ptr->w_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +0000778 }
779 else
780 SvREFCNT_inc(ptr->w_perl_private);
781 SvRV(rv) = ptr->w_perl_private;
782 SvROK_on(rv);
783 return sv_bless(rv, gv_stashpv("VIWIN", TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784}
785
Bram Moolenaare344bea2005-09-01 20:46:49 +0000786 static SV *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100787newBUFrv(SV *rv, buf_T *ptr)
Bram Moolenaare344bea2005-09-01 20:46:49 +0000788{
789 sv_upgrade(rv, SVt_RV);
790 if (ptr->b_perl_private == NULL)
791 {
792 ptr->b_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +0100793 sv_setiv(ptr->b_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +0000794 }
795 else
796 SvREFCNT_inc(ptr->b_perl_private);
797 SvRV(rv) = ptr->b_perl_private;
798 SvROK_on(rv);
799 return sv_bless(rv, gv_stashpv("VIBUF", TRUE));
800}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801
802/*
803 * perl_win_free
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200804 * Remove all references to the window to be destroyed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 */
806 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100807perl_win_free(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000809 if (wp->w_perl_private)
810 sv_setiv((SV *)wp->w_perl_private, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 return;
812}
813
814 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100815perl_buf_free(buf_T *bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000817 if (bp->b_perl_private)
818 sv_setiv((SV *)bp->b_perl_private, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 return;
820}
821
822#ifndef PROTO
823# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
824I32 cur_val(pTHX_ IV iv, SV *sv);
825# else
826I32 cur_val(IV iv, SV *sv);
827#endif
828
829/*
830 * Handler for the magic variables $main::curwin and $main::curbuf.
831 * The handler is put into the magic vtbl for these variables.
832 * (This is effectively a C-level equivalent of a tied variable).
833 * There is no "set" function as the variables are read-only.
834 */
835# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
836I32 cur_val(pTHX_ IV iv, SV *sv)
837# else
838I32 cur_val(IV iv, SV *sv)
839# endif
840{
841 SV *rv;
842 if (iv == 0)
843 rv = newWINrv(newSV(0), curwin);
844 else
845 rv = newBUFrv(newSV(0), curbuf);
846 sv_setsv(sv, rv);
847 return 0;
848}
849#endif /* !PROTO */
850
851struct ufuncs cw_funcs = { cur_val, 0, 0 };
852struct ufuncs cb_funcs = { cur_val, 0, 1 };
853
854/*
855 * VIM_init(): Vim-specific initialisation.
856 * Make the magical main::curwin and main::curbuf variables
857 */
858 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100859VIM_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860{
861 static char cw[] = "main::curwin";
862 static char cb[] = "main::curbuf";
863 SV *sv;
864
865 sv = perl_get_sv(cw, TRUE);
866 sv_magic(sv, NULL, 'U', (char *)&cw_funcs, sizeof(cw_funcs));
867 SvREADONLY_on(sv);
868
869 sv = perl_get_sv(cb, TRUE);
870 sv_magic(sv, NULL, 'U', (char *)&cb_funcs, sizeof(cb_funcs));
871 SvREADONLY_on(sv);
872
873 /*
874 * Setup the Safe compartment.
875 * It shouldn't be a fatal error if the Safe module is missing.
876 * XXX: Only shares the 'Msg' routine (which has to be called
877 * like 'Msg(...)').
878 */
879 (void)perl_eval_pv( "if ( eval( 'require Safe' ) ) { $VIM::safe = Safe->new(); $VIM::safe->share_from( 'VIM', ['Msg'] ); }", G_DISCARD | G_VOID );
880
881}
882
883#ifdef DYNAMIC_PERL
884static char *e_noperl = N_("Sorry, this command is disabled: the Perl library could not be loaded.");
885#endif
886
887/*
888 * ":perl"
889 */
890 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100891ex_perl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892{
893 char *err;
894 char *script;
895 STRLEN length;
896 SV *sv;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200897#ifdef HAVE_SANDBOX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 SV *safe;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900
901 script = (char *)script_get(eap, eap->arg);
902 if (eap->skip)
903 {
904 vim_free(script);
905 return;
906 }
907
908 if (perl_interp == NULL)
909 {
910#ifdef DYNAMIC_PERL
911 if (!perl_enabled(TRUE))
912 {
913 EMSG(_(e_noperl));
914 vim_free(script);
915 return;
916 }
917#endif
918 perl_init();
919 }
920
921 {
922 dSP;
923 ENTER;
924 SAVETMPS;
925
926 if (script == NULL)
927 sv = newSVpv((char *)eap->arg, 0);
928 else
929 {
930 sv = newSVpv(script, 0);
931 vim_free(script);
932 }
933
934#ifdef HAVE_SANDBOX
935 if (sandbox)
936 {
Bram Moolenaara1711622011-07-27 14:15:46 +0200937 safe = perl_get_sv("VIM::safe", FALSE);
Bram Moolenaar3f947ea2009-07-14 14:04:54 +0000938# ifndef MAKE_TEST /* avoid a warning for unreachable code */
Bram Moolenaar954e8c52009-11-11 13:45:33 +0000939 if (safe == NULL || !SvTRUE(safe))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 EMSG(_("E299: Perl evaluation forbidden in sandbox without the Safe module"));
941 else
Bram Moolenaar3f947ea2009-07-14 14:04:54 +0000942# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 {
944 PUSHMARK(SP);
945 XPUSHs(safe);
946 XPUSHs(sv);
947 PUTBACK;
948 perl_call_method("reval", G_DISCARD);
949 }
950 }
951 else
952#endif
953 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
954
955 SvREFCNT_dec(sv);
956
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100957 err = CHECK_EVAL_ERR(length);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958
959 FREETMPS;
960 LEAVE;
961
962 if (!length)
963 return;
964
965 msg_split((char_u *)err, highlight_attr[HLF_E]);
966 return;
967 }
968}
969
970 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100971replace_line(linenr_T *line, linenr_T *end)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972{
973 char *str;
974
975 if (SvOK(GvSV(PL_defgv)))
976 {
977 str = SvPV(GvSV(PL_defgv), PL_na);
978 ml_replace(*line, (char_u *)str, 1);
979 changed_bytes(*line, 0);
980 }
981 else
982 {
983 ml_delete(*line, FALSE);
984 deleted_lines_mark(*line, 1L);
985 --(*end);
986 --(*line);
987 }
988 return OK;
989}
990
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100991static struct ref_map_S {
992 void *vim_ref;
993 SV *perl_ref;
994 struct ref_map_S *next;
995} *ref_map = NULL;
996
997 static void
998ref_map_free(void)
999{
1000 struct ref_map_S *tofree;
1001 struct ref_map_S *refs = ref_map;
1002
1003 while (refs) {
1004 tofree = refs;
1005 refs = refs->next;
1006 vim_free(tofree);
1007 }
1008 ref_map = NULL;
1009}
1010
1011 static struct ref_map_S *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001012ref_map_find_SV(SV *const sv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001013{
1014 struct ref_map_S *refs = ref_map;
1015 int count = 350;
1016
1017 while (refs) {
1018 if (refs->perl_ref == sv)
1019 break;
1020 refs = refs->next;
1021 count--;
1022 }
1023
1024 if (!refs && count > 0) {
1025 refs = (struct ref_map_S *)alloc(sizeof(struct ref_map_S));
1026 if (!refs)
1027 return NULL;
1028 refs->perl_ref = sv;
1029 refs->vim_ref = NULL;
1030 refs->next = ref_map;
1031 ref_map = refs;
1032 }
1033
1034 return refs;
1035}
1036
1037 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001038perl_to_vim(SV *sv, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001039{
1040 if (SvROK(sv))
1041 sv = SvRV(sv);
1042
1043 switch (SvTYPE(sv)) {
1044 case SVt_NULL:
1045 break;
1046 case SVt_NV: /* float */
1047#ifdef FEAT_FLOAT
1048 rettv->v_type = VAR_FLOAT;
1049 rettv->vval.v_float = SvNV(sv);
1050 break;
1051#endif
1052 case SVt_IV: /* integer */
1053 if (!SvROK(sv)) { /* references should be string */
1054 rettv->vval.v_number = SvIV(sv);
1055 break;
1056 }
1057 case SVt_PV: /* string */
1058 {
1059 size_t len = 0;
1060 char * str_from = SvPV(sv, len);
1061 char_u *str_to = (char_u*)alloc(sizeof(char_u) * (len + 1));
1062
1063 if (str_to) {
1064 str_to[len] = '\0';
1065
1066 while (len--) {
1067 if (str_from[len] == '\0')
1068 str_to[len] = '\n';
1069 else
1070 str_to[len] = str_from[len];
1071 }
1072 }
1073
1074 rettv->v_type = VAR_STRING;
1075 rettv->vval.v_string = str_to;
1076 break;
1077 }
1078 case SVt_PVAV: /* list */
1079 {
1080 SSize_t size;
1081 listitem_T * item;
1082 SV ** item2;
1083 list_T * list;
1084 struct ref_map_S * refs;
1085
1086 if ((refs = ref_map_find_SV(sv)) == NULL)
1087 return FAIL;
1088
1089 if (refs->vim_ref)
1090 list = (list_T *) refs->vim_ref;
1091 else
1092 {
1093 if ((list = list_alloc()) == NULL)
1094 return FAIL;
1095 refs->vim_ref = list;
1096
1097 for (size = av_len((AV*)sv); size >= 0; size--)
1098 {
1099 if ((item = listitem_alloc()) == NULL)
1100 break;
1101
1102 item->li_tv.v_type = VAR_NUMBER;
1103 item->li_tv.v_lock = 0;
1104 item->li_tv.vval.v_number = 0;
1105 list_insert(list, item, list->lv_first);
1106
1107 item2 = av_fetch((AV *)sv, size, 0);
1108
1109 if (item2 == NULL || *item2 == NULL ||
Bram Moolenaard99df422016-01-29 23:20:40 +01001110 perl_to_vim(*item2, &item->li_tv) == FAIL)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001111 break;
1112 }
1113 }
1114
1115 list->lv_refcount++;
1116 rettv->v_type = VAR_LIST;
1117 rettv->vval.v_list = list;
1118 break;
1119 }
1120 case SVt_PVHV: /* dictionary */
1121 {
1122 HE * entry;
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001123 I32 key_len;
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001124 char * key;
1125 dictitem_T * item;
1126 SV * item2;
1127 dict_T * dict;
1128 struct ref_map_S * refs;
1129
1130 if ((refs = ref_map_find_SV(sv)) == NULL)
1131 return FAIL;
1132
1133 if (refs->vim_ref)
1134 dict = (dict_T *) refs->vim_ref;
1135 else
1136 {
1137
1138 if ((dict = dict_alloc()) == NULL)
1139 return FAIL;
1140 refs->vim_ref = dict;
1141
1142 hv_iterinit((HV *)sv);
1143
1144 for (entry = hv_iternext((HV *)sv); entry; entry = hv_iternext((HV *)sv))
1145 {
1146 key_len = 0;
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001147 key = hv_iterkey(entry, &key_len);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001148
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001149 if (!key || !key_len || strlen(key) < (size_t)key_len) {
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001150 EMSG2("Malformed key Dictionary '%s'", key && *key ? key : "(empty)");
1151 break;
1152 }
1153
1154 if ((item = dictitem_alloc((char_u *)key)) == NULL)
1155 break;
1156
1157 item->di_tv.v_type = VAR_NUMBER;
1158 item->di_tv.v_lock = 0;
1159 item->di_tv.vval.v_number = 0;
1160
1161 if (dict_add(dict, item) == FAIL) {
1162 dictitem_free(item);
1163 break;
1164 }
1165 item2 = hv_iterval((HV *)sv, entry);
1166 if (item2 == NULL || perl_to_vim(item2, &item->di_tv) == FAIL)
1167 break;
1168 }
1169 }
1170
1171 dict->dv_refcount++;
1172 rettv->v_type = VAR_DICT;
1173 rettv->vval.v_dict = dict;
1174 break;
1175 }
1176 default: /* not convertible */
1177 {
1178 char *val = SvPV_nolen(sv);
1179 rettv->v_type = VAR_STRING;
1180 rettv->vval.v_string = val ? vim_strsave((char_u *)val) : NULL;
1181 break;
1182 }
1183 }
1184 return OK;
1185}
1186
1187/*
1188 * "perleval()"
1189 */
1190 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001191do_perleval(char_u *str, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001192{
1193 char *err = NULL;
1194 STRLEN err_len = 0;
1195 SV *sv = NULL;
1196#ifdef HAVE_SANDBOX
1197 SV *safe;
1198#endif
1199
1200 if (perl_interp == NULL)
1201 {
1202#ifdef DYNAMIC_PERL
1203 if (!perl_enabled(TRUE))
1204 {
1205 EMSG(_(e_noperl));
1206 return;
1207 }
1208#endif
1209 perl_init();
1210 }
1211
1212 {
1213 dSP;
1214 ENTER;
1215 SAVETMPS;
1216
1217#ifdef HAVE_SANDBOX
1218 if (sandbox)
1219 {
1220 safe = get_sv("VIM::safe", FALSE);
1221# ifndef MAKE_TEST /* avoid a warning for unreachable code */
1222 if (safe == NULL || !SvTRUE(safe))
1223 EMSG(_("E299: Perl evaluation forbidden in sandbox without the Safe module"));
1224 else
1225# endif
1226 {
1227 sv = newSVpv((char *)str, 0);
1228 PUSHMARK(SP);
1229 XPUSHs(safe);
1230 XPUSHs(sv);
1231 PUTBACK;
1232 call_method("reval", G_SCALAR);
1233 SPAGAIN;
1234 SvREFCNT_dec(sv);
1235 sv = POPs;
1236 }
1237 }
1238 else
1239#endif /* HAVE_SANDBOX */
1240 sv = eval_pv((char *)str, 0);
1241
1242 if (sv) {
1243 perl_to_vim(sv, rettv);
1244 ref_map_free();
1245 err = CHECK_EVAL_ERR(err_len);
1246 }
1247 PUTBACK;
1248 FREETMPS;
1249 LEAVE;
1250 }
1251 if (err_len)
1252 msg_split((char_u *)err, highlight_attr[HLF_E]);
1253}
1254
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255/*
1256 * ":perldo".
1257 */
1258 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001259ex_perldo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260{
1261 STRLEN length;
1262 SV *sv;
1263 char *str;
1264 linenr_T i;
1265
1266 if (bufempty())
1267 return;
1268
1269 if (perl_interp == NULL)
1270 {
1271#ifdef DYNAMIC_PERL
1272 if (!perl_enabled(TRUE))
1273 {
1274 EMSG(_(e_noperl));
1275 return;
1276 }
1277#endif
1278 perl_init();
1279 }
1280 {
1281 dSP;
1282 length = strlen((char *)eap->arg);
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001283 sv = newSV(length + sizeof("sub VIM::perldo {") - 1 + 1);
1284 sv_setpvn(sv, "sub VIM::perldo {", sizeof("sub VIM::perldo {") - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 sv_catpvn(sv, (char *)eap->arg, length);
1286 sv_catpvn(sv, "}", 1);
1287 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
1288 SvREFCNT_dec(sv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001289 str = CHECK_EVAL_ERR(length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 if (length)
1291 goto err;
1292
1293 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
1294 return;
1295
1296 ENTER;
1297 SAVETMPS;
1298 for (i = eap->line1; i <= eap->line2; i++)
1299 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001300 sv_setpv(GvSV(PL_defgv), (char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 PUSHMARK(sp);
1302 perl_call_pv("VIM::perldo", G_SCALAR | G_EVAL);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001303 str = CHECK_EVAL_ERR(length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 if (length)
1305 break;
1306 SPAGAIN;
1307 if (SvTRUEx(POPs))
1308 {
1309 if (replace_line(&i, &eap->line2) != OK)
1310 {
1311 PUTBACK;
1312 break;
1313 }
1314 }
1315 PUTBACK;
1316 }
1317 FREETMPS;
1318 LEAVE;
1319 check_cursor();
1320 update_screen(NOT_VALID);
1321 if (!length)
1322 return;
1323
1324err:
1325 msg_split((char_u *)str, highlight_attr[HLF_E]);
1326 return;
1327 }
1328}
1329
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001330#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
1331typedef struct {
1332 struct _PerlIO base;
1333 int attr;
1334} PerlIOVim;
1335
1336 static IV
1337PerlIOVim_pushed(pTHX_ PerlIO *f, const char *mode,
1338 SV *arg, PerlIO_funcs *tab)
1339{
1340 PerlIOVim *s = PerlIOSelf(f, PerlIOVim);
1341 s->attr = 0;
1342 if (arg && SvPOK(arg)) {
1343 int id = syn_name2id((char_u *)SvPV_nolen(arg));
1344 if (id != 0)
1345 s->attr = syn_id2attr(id);
1346 }
1347 return PerlIOBase_pushed(aTHX_ f, mode, (SV *)NULL, tab);
1348}
1349
1350 static SSize_t
1351PerlIOVim_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1352{
1353 char_u *str;
1354 PerlIOVim * s = PerlIOSelf(f, PerlIOVim);
1355
1356 str = vim_strnsave((char_u *)vbuf, count);
1357 if (str == NULL)
1358 return 0;
1359 msg_split((char_u *)str, s->attr);
1360 vim_free(str);
1361
1362 return count;
1363}
1364
1365static PERLIO_FUNCS_DECL(PerlIO_Vim) = {
1366 sizeof(PerlIO_funcs),
1367 "Vim",
1368 sizeof(PerlIOVim),
1369 PERLIO_K_DUMMY, /* flags */
1370 PerlIOVim_pushed,
1371 NULL, /* popped */
1372 NULL, /* open */
1373 NULL, /* binmode */
1374 NULL, /* arg */
1375 NULL, /* fileno */
1376 NULL, /* dup */
1377 NULL, /* read */
1378 NULL, /* unread */
1379 PerlIOVim_write,
1380 NULL, /* seek */
1381 NULL, /* tell */
1382 NULL, /* close */
1383 NULL, /* flush */
1384 NULL, /* fill */
1385 NULL, /* eof */
1386 NULL, /* error */
1387 NULL, /* clearerr */
1388 NULL, /* setlinebuf */
1389 NULL, /* get_base */
1390 NULL, /* get_bufsiz */
1391 NULL, /* get_ptr */
1392 NULL, /* get_cnt */
1393 NULL /* set_ptrcnt */
1394};
1395
1396/* Use Vim routine for print operator */
1397 static void
1398vim_IOLayer_init(void)
1399{
1400 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_Vim));
1401 (void)eval_pv( "binmode(STDOUT, ':Vim')"
1402 " && binmode(STDERR, ':Vim(ErrorMsg)');", 0);
1403}
1404#endif /* PERLIO_LAYERS && !USE_SFIO */
1405
Bram Moolenaar01dd60c2008-07-24 14:24:48 +00001406#ifndef FEAT_WINDOWS
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001407 int
1408win_valid(win_T *w)
1409{
1410 return TRUE;
1411}
1412 int
1413win_count(void)
1414{
1415 return 1;
1416}
1417 win_T *
1418win_find_nr(int n)
1419{
1420 return curwin;
1421}
Bram Moolenaar01dd60c2008-07-24 14:24:48 +00001422#endif
1423
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424XS(boot_VIM);
1425
1426 static void
1427xs_init(pTHX)
1428{
1429 char *file = __FILE__;
1430
1431 /* DynaLoader is a special case */
1432 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1433 newXS("VIM::bootstrap", boot_VIM, file);
1434}
1435
1436typedef win_T * VIWIN;
1437typedef buf_T * VIBUF;
1438
1439MODULE = VIM PACKAGE = VIM
1440
1441void
1442Msg(text, hl=NULL)
1443 char *text;
1444 char *hl;
1445
1446 PREINIT:
1447 int attr;
1448 int id;
1449
1450 PPCODE:
1451 if (text != NULL)
1452 {
1453 attr = 0;
1454 if (hl != NULL)
1455 {
1456 id = syn_name2id((char_u *)hl);
1457 if (id != 0)
1458 attr = syn_id2attr(id);
1459 }
1460 msg_split((char_u *)text, attr);
1461 }
1462
1463void
1464SetOption(line)
1465 char *line;
1466
1467 PPCODE:
1468 if (line != NULL)
1469 do_set((char_u *)line, 0);
1470 update_screen(NOT_VALID);
1471
1472void
1473DoCommand(line)
1474 char *line;
1475
1476 PPCODE:
1477 if (line != NULL)
1478 do_cmdline_cmd((char_u *)line);
1479
1480void
1481Eval(str)
1482 char *str;
1483
1484 PREINIT:
1485 char_u *value;
1486 PPCODE:
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001487 value = eval_to_string((char_u *)str, (char_u **)0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 if (value == NULL)
1489 {
1490 XPUSHs(sv_2mortal(newSViv(0)));
1491 XPUSHs(sv_2mortal(newSVpv("", 0)));
1492 }
1493 else
1494 {
1495 XPUSHs(sv_2mortal(newSViv(1)));
1496 XPUSHs(sv_2mortal(newSVpv((char *)value, 0)));
1497 vim_free(value);
1498 }
1499
1500void
1501Buffers(...)
1502
1503 PREINIT:
1504 buf_T *vimbuf;
1505 int i, b;
1506
1507 PPCODE:
1508 if (items == 0)
1509 {
1510 if (GIMME == G_SCALAR)
1511 {
1512 i = 0;
1513 for (vimbuf = firstbuf; vimbuf; vimbuf = vimbuf->b_next)
1514 ++i;
1515
1516 XPUSHs(sv_2mortal(newSViv(i)));
1517 }
1518 else
1519 {
1520 for (vimbuf = firstbuf; vimbuf; vimbuf = vimbuf->b_next)
1521 XPUSHs(newBUFrv(newSV(0), vimbuf));
1522 }
1523 }
1524 else
1525 {
1526 for (i = 0; i < items; i++)
1527 {
1528 SV *sv = ST(i);
1529 if (SvIOK(sv))
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001530 b = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 else
1532 {
1533 char_u *pat;
1534 STRLEN len;
1535
1536 pat = (char_u *)SvPV(sv, len);
1537 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001538 b = buflist_findpat(pat, pat+len, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 --emsg_off;
1540 }
1541
1542 if (b >= 0)
1543 {
1544 vimbuf = buflist_findnr(b);
1545 if (vimbuf)
1546 XPUSHs(newBUFrv(newSV(0), vimbuf));
1547 }
1548 }
1549 }
1550
1551void
1552Windows(...)
1553
1554 PREINIT:
1555 win_T *vimwin;
1556 int i, w;
1557
1558 PPCODE:
1559 if (items == 0)
1560 {
1561 if (GIMME == G_SCALAR)
1562 XPUSHs(sv_2mortal(newSViv(win_count())));
1563 else
1564 {
1565 for (vimwin = firstwin; vimwin != NULL; vimwin = W_NEXT(vimwin))
1566 XPUSHs(newWINrv(newSV(0), vimwin));
1567 }
1568 }
1569 else
1570 {
1571 for (i = 0; i < items; i++)
1572 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001573 w = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 vimwin = win_find_nr(w);
1575 if (vimwin)
1576 XPUSHs(newWINrv(newSV(0), vimwin));
1577 }
1578 }
1579
1580MODULE = VIM PACKAGE = VIWIN
1581
1582void
1583DESTROY(win)
1584 VIWIN win
1585
1586 CODE:
1587 if (win_valid(win))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001588 win->w_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589
1590SV *
1591Buffer(win)
1592 VIWIN win
1593
1594 CODE:
1595 if (!win_valid(win))
1596 win = curwin;
1597 RETVAL = newBUFrv(newSV(0), win->w_buffer);
1598 OUTPUT:
1599 RETVAL
1600
1601void
1602SetHeight(win, height)
1603 VIWIN win
1604 int height;
1605
1606 PREINIT:
1607 win_T *savewin;
1608
1609 PPCODE:
1610 if (!win_valid(win))
1611 win = curwin;
1612 savewin = curwin;
1613 curwin = win;
1614 win_setheight(height);
1615 curwin = savewin;
1616
1617void
Bram Moolenaar864733a2016-04-02 14:18:01 +02001618Cursor(win, ...)
1619 VIWIN win
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620
1621 PPCODE:
Bram Moolenaara1711622011-07-27 14:15:46 +02001622 if (items == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 {
1624 EXTEND(sp, 2);
1625 if (!win_valid(win))
1626 win = curwin;
1627 PUSHs(sv_2mortal(newSViv(win->w_cursor.lnum)));
1628 PUSHs(sv_2mortal(newSViv(win->w_cursor.col)));
1629 }
Bram Moolenaara1711622011-07-27 14:15:46 +02001630 else if (items == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 {
1632 int lnum, col;
1633
1634 if (!win_valid(win))
1635 win = curwin;
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001636 lnum = (int) SvIV(ST(1));
1637 col = (int) SvIV(ST(2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 win->w_cursor.lnum = lnum;
1639 win->w_cursor.col = col;
1640 check_cursor(); /* put cursor on an existing line */
1641 update_screen(NOT_VALID);
1642 }
1643
1644MODULE = VIM PACKAGE = VIBUF
1645
1646void
1647DESTROY(vimbuf)
1648 VIBUF vimbuf;
1649
1650 CODE:
1651 if (buf_valid(vimbuf))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001652 vimbuf->b_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653
1654void
1655Name(vimbuf)
1656 VIBUF vimbuf;
1657
1658 PPCODE:
1659 if (!buf_valid(vimbuf))
1660 vimbuf = curbuf;
1661 /* No file name returns an empty string */
1662 if (vimbuf->b_fname == NULL)
1663 XPUSHs(sv_2mortal(newSVpv("", 0)));
1664 else
1665 XPUSHs(sv_2mortal(newSVpv((char *)vimbuf->b_fname, 0)));
1666
1667void
1668Number(vimbuf)
1669 VIBUF vimbuf;
1670
1671 PPCODE:
1672 if (!buf_valid(vimbuf))
1673 vimbuf = curbuf;
1674 XPUSHs(sv_2mortal(newSViv(vimbuf->b_fnum)));
1675
1676void
1677Count(vimbuf)
1678 VIBUF vimbuf;
1679
1680 PPCODE:
1681 if (!buf_valid(vimbuf))
1682 vimbuf = curbuf;
1683 XPUSHs(sv_2mortal(newSViv(vimbuf->b_ml.ml_line_count)));
1684
1685void
1686Get(vimbuf, ...)
1687 VIBUF vimbuf;
1688
1689 PREINIT:
1690 char_u *line;
1691 int i;
1692 long lnum;
1693 PPCODE:
1694 if (buf_valid(vimbuf))
1695 {
1696 for (i = 1; i < items; i++)
1697 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001698 lnum = (long) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1700 {
1701 line = ml_get_buf(vimbuf, lnum, FALSE);
1702 XPUSHs(sv_2mortal(newSVpv((char *)line, 0)));
1703 }
1704 }
1705 }
1706
1707void
1708Set(vimbuf, ...)
1709 VIBUF vimbuf;
1710
1711 PREINIT:
1712 int i;
1713 long lnum;
1714 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 PPCODE:
1716 if (buf_valid(vimbuf))
1717 {
1718 if (items < 3)
1719 croak("Usage: VIBUF::Set(vimbuf, lnum, @lines)");
1720
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001721 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 for(i = 2; i < items; i++, lnum++)
1723 {
1724 line = SvPV(ST(i),PL_na);
1725 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1726 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001727 aco_save_T aco;
1728
1729 /* set curwin/curbuf for "vimbuf" and save some things */
1730 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001731
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 if (u_savesub(lnum) == OK)
1733 {
1734 ml_replace(lnum, (char_u *)line, TRUE);
1735 changed_bytes(lnum, 0);
1736 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001737
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001738 /* restore curwin/curbuf and a few other things */
1739 aucmd_restbuf(&aco);
1740 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 }
1742 }
1743 }
1744
1745void
1746Delete(vimbuf, ...)
1747 VIBUF vimbuf;
1748
1749 PREINIT:
1750 long i, lnum = 0, count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 PPCODE:
1752 if (buf_valid(vimbuf))
1753 {
1754 if (items == 2)
1755 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001756 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 count = 1;
1758 }
1759 else if (items == 3)
1760 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001761 lnum = (long) SvIV(ST(1));
1762 count = (long) 1 + SvIV(ST(2)) - lnum;
Bram Moolenaara1711622011-07-27 14:15:46 +02001763 if (count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 count = 1;
Bram Moolenaara1711622011-07-27 14:15:46 +02001765 if (count < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 {
1767 lnum -= count;
1768 count = -count;
1769 }
1770 }
1771 if (items >= 2)
1772 {
1773 for (i = 0; i < count; i++)
1774 {
1775 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1776 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001777 aco_save_T aco;
1778
1779 /* set curwin/curbuf for "vimbuf" and save some things */
1780 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001781
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 if (u_savedel(lnum, 1) == OK)
1783 {
1784 ml_delete(lnum, 0);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00001785 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 deleted_lines_mark(lnum, 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001788
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001789 /* restore curwin/curbuf and a few other things */
1790 aucmd_restbuf(&aco);
1791 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001792
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 update_curbuf(VALID);
1794 }
1795 }
1796 }
1797 }
1798
1799void
1800Append(vimbuf, ...)
1801 VIBUF vimbuf;
1802
1803 PREINIT:
1804 int i;
1805 long lnum;
1806 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 PPCODE:
1808 if (buf_valid(vimbuf))
1809 {
1810 if (items < 3)
1811 croak("Usage: VIBUF::Append(vimbuf, lnum, @lines)");
1812
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001813 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 for (i = 2; i < items; i++, lnum++)
1815 {
1816 line = SvPV(ST(i),PL_na);
1817 if (lnum >= 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1818 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001819 aco_save_T aco;
1820
1821 /* set curwin/curbuf for "vimbuf" and save some things */
1822 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001823
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 if (u_inssub(lnum + 1) == OK)
1825 {
1826 ml_append(lnum, (char_u *)line, (colnr_T)0, FALSE);
1827 appended_lines_mark(lnum, 1L);
1828 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001829
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001830 /* restore curwin/curbuf and a few other things */
1831 aucmd_restbuf(&aco);
1832 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001833
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 update_curbuf(VALID);
1835 }
1836 }
1837 }
1838
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001839#ifdef __GNUC__
1840# pragma GCC diagnostic pop
1841#endif