blob: aab7ade227093a23d8a03f9e29bd97c379588070 [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);
1078 char_u *str_to = (char_u*)alloc(sizeof(char_u) * (len + 1));
1079
1080 if (str_to) {
1081 str_to[len] = '\0';
1082
1083 while (len--) {
1084 if (str_from[len] == '\0')
1085 str_to[len] = '\n';
1086 else
1087 str_to[len] = str_from[len];
1088 }
1089 }
1090
1091 rettv->v_type = VAR_STRING;
1092 rettv->vval.v_string = str_to;
1093 break;
1094 }
1095 case SVt_PVAV: /* list */
1096 {
1097 SSize_t size;
1098 listitem_T * item;
1099 SV ** item2;
1100 list_T * list;
1101 struct ref_map_S * refs;
1102
1103 if ((refs = ref_map_find_SV(sv)) == NULL)
1104 return FAIL;
1105
1106 if (refs->vim_ref)
1107 list = (list_T *) refs->vim_ref;
1108 else
1109 {
1110 if ((list = list_alloc()) == NULL)
1111 return FAIL;
1112 refs->vim_ref = list;
1113
1114 for (size = av_len((AV*)sv); size >= 0; size--)
1115 {
1116 if ((item = listitem_alloc()) == NULL)
1117 break;
1118
1119 item->li_tv.v_type = VAR_NUMBER;
1120 item->li_tv.v_lock = 0;
1121 item->li_tv.vval.v_number = 0;
1122 list_insert(list, item, list->lv_first);
1123
1124 item2 = av_fetch((AV *)sv, size, 0);
1125
1126 if (item2 == NULL || *item2 == NULL ||
Bram Moolenaard99df422016-01-29 23:20:40 +01001127 perl_to_vim(*item2, &item->li_tv) == FAIL)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001128 break;
1129 }
1130 }
1131
1132 list->lv_refcount++;
1133 rettv->v_type = VAR_LIST;
1134 rettv->vval.v_list = list;
1135 break;
1136 }
1137 case SVt_PVHV: /* dictionary */
1138 {
1139 HE * entry;
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001140 I32 key_len;
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001141 char * key;
1142 dictitem_T * item;
1143 SV * item2;
1144 dict_T * dict;
1145 struct ref_map_S * refs;
1146
1147 if ((refs = ref_map_find_SV(sv)) == NULL)
1148 return FAIL;
1149
1150 if (refs->vim_ref)
1151 dict = (dict_T *) refs->vim_ref;
1152 else
1153 {
1154
1155 if ((dict = dict_alloc()) == NULL)
1156 return FAIL;
1157 refs->vim_ref = dict;
1158
1159 hv_iterinit((HV *)sv);
1160
1161 for (entry = hv_iternext((HV *)sv); entry; entry = hv_iternext((HV *)sv))
1162 {
1163 key_len = 0;
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001164 key = hv_iterkey(entry, &key_len);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001165
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001166 if (!key || !key_len || strlen(key) < (size_t)key_len) {
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001167 EMSG2("Malformed key Dictionary '%s'", key && *key ? key : "(empty)");
1168 break;
1169 }
1170
1171 if ((item = dictitem_alloc((char_u *)key)) == NULL)
1172 break;
1173
1174 item->di_tv.v_type = VAR_NUMBER;
1175 item->di_tv.v_lock = 0;
1176 item->di_tv.vval.v_number = 0;
1177
1178 if (dict_add(dict, item) == FAIL) {
1179 dictitem_free(item);
1180 break;
1181 }
1182 item2 = hv_iterval((HV *)sv, entry);
1183 if (item2 == NULL || perl_to_vim(item2, &item->di_tv) == FAIL)
1184 break;
1185 }
1186 }
1187
1188 dict->dv_refcount++;
1189 rettv->v_type = VAR_DICT;
1190 rettv->vval.v_dict = dict;
1191 break;
1192 }
1193 default: /* not convertible */
1194 {
1195 char *val = SvPV_nolen(sv);
1196 rettv->v_type = VAR_STRING;
1197 rettv->vval.v_string = val ? vim_strsave((char_u *)val) : NULL;
1198 break;
1199 }
1200 }
1201 return OK;
1202}
1203
1204/*
1205 * "perleval()"
1206 */
1207 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001208do_perleval(char_u *str, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001209{
1210 char *err = NULL;
1211 STRLEN err_len = 0;
1212 SV *sv = NULL;
1213#ifdef HAVE_SANDBOX
1214 SV *safe;
1215#endif
1216
1217 if (perl_interp == NULL)
1218 {
1219#ifdef DYNAMIC_PERL
1220 if (!perl_enabled(TRUE))
1221 {
1222 EMSG(_(e_noperl));
1223 return;
1224 }
1225#endif
1226 perl_init();
1227 }
1228
1229 {
1230 dSP;
1231 ENTER;
1232 SAVETMPS;
1233
1234#ifdef HAVE_SANDBOX
1235 if (sandbox)
1236 {
1237 safe = get_sv("VIM::safe", FALSE);
1238# ifndef MAKE_TEST /* avoid a warning for unreachable code */
1239 if (safe == NULL || !SvTRUE(safe))
1240 EMSG(_("E299: Perl evaluation forbidden in sandbox without the Safe module"));
1241 else
1242# endif
1243 {
1244 sv = newSVpv((char *)str, 0);
1245 PUSHMARK(SP);
1246 XPUSHs(safe);
1247 XPUSHs(sv);
1248 PUTBACK;
1249 call_method("reval", G_SCALAR);
1250 SPAGAIN;
1251 SvREFCNT_dec(sv);
1252 sv = POPs;
1253 }
1254 }
1255 else
1256#endif /* HAVE_SANDBOX */
1257 sv = eval_pv((char *)str, 0);
1258
1259 if (sv) {
1260 perl_to_vim(sv, rettv);
1261 ref_map_free();
1262 err = CHECK_EVAL_ERR(err_len);
1263 }
1264 PUTBACK;
1265 FREETMPS;
1266 LEAVE;
1267 }
1268 if (err_len)
1269 msg_split((char_u *)err, highlight_attr[HLF_E]);
1270}
1271
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272/*
1273 * ":perldo".
1274 */
1275 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001276ex_perldo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277{
1278 STRLEN length;
1279 SV *sv;
1280 char *str;
1281 linenr_T i;
1282
1283 if (bufempty())
1284 return;
1285
1286 if (perl_interp == NULL)
1287 {
1288#ifdef DYNAMIC_PERL
1289 if (!perl_enabled(TRUE))
1290 {
1291 EMSG(_(e_noperl));
1292 return;
1293 }
1294#endif
1295 perl_init();
1296 }
1297 {
1298 dSP;
1299 length = strlen((char *)eap->arg);
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001300 sv = newSV(length + sizeof("sub VIM::perldo {") - 1 + 1);
1301 sv_setpvn(sv, "sub VIM::perldo {", sizeof("sub VIM::perldo {") - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 sv_catpvn(sv, (char *)eap->arg, length);
1303 sv_catpvn(sv, "}", 1);
1304 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
1305 SvREFCNT_dec(sv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001306 str = CHECK_EVAL_ERR(length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 if (length)
1308 goto err;
1309
1310 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
1311 return;
1312
1313 ENTER;
1314 SAVETMPS;
1315 for (i = eap->line1; i <= eap->line2; i++)
1316 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001317 sv_setpv(GvSV(PL_defgv), (char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 PUSHMARK(sp);
1319 perl_call_pv("VIM::perldo", G_SCALAR | G_EVAL);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001320 str = CHECK_EVAL_ERR(length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 if (length)
1322 break;
1323 SPAGAIN;
1324 if (SvTRUEx(POPs))
1325 {
1326 if (replace_line(&i, &eap->line2) != OK)
1327 {
1328 PUTBACK;
1329 break;
1330 }
1331 }
1332 PUTBACK;
1333 }
1334 FREETMPS;
1335 LEAVE;
1336 check_cursor();
1337 update_screen(NOT_VALID);
1338 if (!length)
1339 return;
1340
1341err:
1342 msg_split((char_u *)str, highlight_attr[HLF_E]);
1343 return;
1344 }
1345}
1346
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001347#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
1348typedef struct {
1349 struct _PerlIO base;
1350 int attr;
1351} PerlIOVim;
1352
1353 static IV
1354PerlIOVim_pushed(pTHX_ PerlIO *f, const char *mode,
1355 SV *arg, PerlIO_funcs *tab)
1356{
1357 PerlIOVim *s = PerlIOSelf(f, PerlIOVim);
1358 s->attr = 0;
1359 if (arg && SvPOK(arg)) {
1360 int id = syn_name2id((char_u *)SvPV_nolen(arg));
1361 if (id != 0)
1362 s->attr = syn_id2attr(id);
1363 }
1364 return PerlIOBase_pushed(aTHX_ f, mode, (SV *)NULL, tab);
1365}
1366
1367 static SSize_t
1368PerlIOVim_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1369{
1370 char_u *str;
1371 PerlIOVim * s = PerlIOSelf(f, PerlIOVim);
1372
1373 str = vim_strnsave((char_u *)vbuf, count);
1374 if (str == NULL)
1375 return 0;
1376 msg_split((char_u *)str, s->attr);
1377 vim_free(str);
1378
1379 return count;
1380}
1381
1382static PERLIO_FUNCS_DECL(PerlIO_Vim) = {
1383 sizeof(PerlIO_funcs),
1384 "Vim",
1385 sizeof(PerlIOVim),
1386 PERLIO_K_DUMMY, /* flags */
1387 PerlIOVim_pushed,
1388 NULL, /* popped */
1389 NULL, /* open */
1390 NULL, /* binmode */
1391 NULL, /* arg */
1392 NULL, /* fileno */
1393 NULL, /* dup */
1394 NULL, /* read */
1395 NULL, /* unread */
1396 PerlIOVim_write,
1397 NULL, /* seek */
1398 NULL, /* tell */
1399 NULL, /* close */
1400 NULL, /* flush */
1401 NULL, /* fill */
1402 NULL, /* eof */
1403 NULL, /* error */
1404 NULL, /* clearerr */
1405 NULL, /* setlinebuf */
1406 NULL, /* get_base */
1407 NULL, /* get_bufsiz */
1408 NULL, /* get_ptr */
1409 NULL, /* get_cnt */
1410 NULL /* set_ptrcnt */
1411};
1412
1413/* Use Vim routine for print operator */
1414 static void
1415vim_IOLayer_init(void)
1416{
1417 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_Vim));
1418 (void)eval_pv( "binmode(STDOUT, ':Vim')"
1419 " && binmode(STDERR, ':Vim(ErrorMsg)');", 0);
1420}
1421#endif /* PERLIO_LAYERS && !USE_SFIO */
1422
Bram Moolenaar01dd60c2008-07-24 14:24:48 +00001423#ifndef FEAT_WINDOWS
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001424 int
1425win_valid(win_T *w)
1426{
1427 return TRUE;
1428}
1429 int
1430win_count(void)
1431{
1432 return 1;
1433}
1434 win_T *
1435win_find_nr(int n)
1436{
1437 return curwin;
1438}
Bram Moolenaar01dd60c2008-07-24 14:24:48 +00001439#endif
1440
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441XS(boot_VIM);
1442
1443 static void
1444xs_init(pTHX)
1445{
1446 char *file = __FILE__;
1447
1448 /* DynaLoader is a special case */
1449 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1450 newXS("VIM::bootstrap", boot_VIM, file);
1451}
1452
1453typedef win_T * VIWIN;
1454typedef buf_T * VIBUF;
1455
1456MODULE = VIM PACKAGE = VIM
1457
1458void
1459Msg(text, hl=NULL)
1460 char *text;
1461 char *hl;
1462
1463 PREINIT:
1464 int attr;
1465 int id;
1466
1467 PPCODE:
1468 if (text != NULL)
1469 {
1470 attr = 0;
1471 if (hl != NULL)
1472 {
1473 id = syn_name2id((char_u *)hl);
1474 if (id != 0)
1475 attr = syn_id2attr(id);
1476 }
1477 msg_split((char_u *)text, attr);
1478 }
1479
1480void
1481SetOption(line)
1482 char *line;
1483
1484 PPCODE:
1485 if (line != NULL)
1486 do_set((char_u *)line, 0);
1487 update_screen(NOT_VALID);
1488
1489void
1490DoCommand(line)
1491 char *line;
1492
1493 PPCODE:
1494 if (line != NULL)
1495 do_cmdline_cmd((char_u *)line);
1496
1497void
1498Eval(str)
1499 char *str;
1500
1501 PREINIT:
1502 char_u *value;
1503 PPCODE:
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001504 value = eval_to_string((char_u *)str, (char_u **)0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 if (value == NULL)
1506 {
1507 XPUSHs(sv_2mortal(newSViv(0)));
1508 XPUSHs(sv_2mortal(newSVpv("", 0)));
1509 }
1510 else
1511 {
1512 XPUSHs(sv_2mortal(newSViv(1)));
1513 XPUSHs(sv_2mortal(newSVpv((char *)value, 0)));
1514 vim_free(value);
1515 }
1516
1517void
1518Buffers(...)
1519
1520 PREINIT:
1521 buf_T *vimbuf;
1522 int i, b;
1523
1524 PPCODE:
1525 if (items == 0)
1526 {
1527 if (GIMME == G_SCALAR)
1528 {
1529 i = 0;
1530 for (vimbuf = firstbuf; vimbuf; vimbuf = vimbuf->b_next)
1531 ++i;
1532
1533 XPUSHs(sv_2mortal(newSViv(i)));
1534 }
1535 else
1536 {
1537 for (vimbuf = firstbuf; vimbuf; vimbuf = vimbuf->b_next)
1538 XPUSHs(newBUFrv(newSV(0), vimbuf));
1539 }
1540 }
1541 else
1542 {
1543 for (i = 0; i < items; i++)
1544 {
1545 SV *sv = ST(i);
1546 if (SvIOK(sv))
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001547 b = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 else
1549 {
1550 char_u *pat;
1551 STRLEN len;
1552
1553 pat = (char_u *)SvPV(sv, len);
1554 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001555 b = buflist_findpat(pat, pat+len, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 --emsg_off;
1557 }
1558
1559 if (b >= 0)
1560 {
1561 vimbuf = buflist_findnr(b);
1562 if (vimbuf)
1563 XPUSHs(newBUFrv(newSV(0), vimbuf));
1564 }
1565 }
1566 }
1567
1568void
1569Windows(...)
1570
1571 PREINIT:
1572 win_T *vimwin;
1573 int i, w;
1574
1575 PPCODE:
1576 if (items == 0)
1577 {
1578 if (GIMME == G_SCALAR)
1579 XPUSHs(sv_2mortal(newSViv(win_count())));
1580 else
1581 {
1582 for (vimwin = firstwin; vimwin != NULL; vimwin = W_NEXT(vimwin))
1583 XPUSHs(newWINrv(newSV(0), vimwin));
1584 }
1585 }
1586 else
1587 {
1588 for (i = 0; i < items; i++)
1589 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001590 w = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 vimwin = win_find_nr(w);
1592 if (vimwin)
1593 XPUSHs(newWINrv(newSV(0), vimwin));
1594 }
1595 }
1596
1597MODULE = VIM PACKAGE = VIWIN
1598
1599void
1600DESTROY(win)
1601 VIWIN win
1602
1603 CODE:
1604 if (win_valid(win))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001605 win->w_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606
1607SV *
1608Buffer(win)
1609 VIWIN win
1610
1611 CODE:
1612 if (!win_valid(win))
1613 win = curwin;
1614 RETVAL = newBUFrv(newSV(0), win->w_buffer);
1615 OUTPUT:
1616 RETVAL
1617
1618void
1619SetHeight(win, height)
1620 VIWIN win
1621 int height;
1622
1623 PREINIT:
1624 win_T *savewin;
1625
1626 PPCODE:
1627 if (!win_valid(win))
1628 win = curwin;
1629 savewin = curwin;
1630 curwin = win;
1631 win_setheight(height);
1632 curwin = savewin;
1633
1634void
Bram Moolenaar864733a2016-04-02 14:18:01 +02001635Cursor(win, ...)
1636 VIWIN win
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
1638 PPCODE:
Bram Moolenaara1711622011-07-27 14:15:46 +02001639 if (items == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 {
1641 EXTEND(sp, 2);
1642 if (!win_valid(win))
1643 win = curwin;
1644 PUSHs(sv_2mortal(newSViv(win->w_cursor.lnum)));
1645 PUSHs(sv_2mortal(newSViv(win->w_cursor.col)));
1646 }
Bram Moolenaara1711622011-07-27 14:15:46 +02001647 else if (items == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 {
1649 int lnum, col;
1650
1651 if (!win_valid(win))
1652 win = curwin;
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001653 lnum = (int) SvIV(ST(1));
1654 col = (int) SvIV(ST(2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 win->w_cursor.lnum = lnum;
1656 win->w_cursor.col = col;
1657 check_cursor(); /* put cursor on an existing line */
1658 update_screen(NOT_VALID);
1659 }
1660
1661MODULE = VIM PACKAGE = VIBUF
1662
1663void
1664DESTROY(vimbuf)
1665 VIBUF vimbuf;
1666
1667 CODE:
1668 if (buf_valid(vimbuf))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001669 vimbuf->b_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670
1671void
1672Name(vimbuf)
1673 VIBUF vimbuf;
1674
1675 PPCODE:
1676 if (!buf_valid(vimbuf))
1677 vimbuf = curbuf;
1678 /* No file name returns an empty string */
1679 if (vimbuf->b_fname == NULL)
1680 XPUSHs(sv_2mortal(newSVpv("", 0)));
1681 else
1682 XPUSHs(sv_2mortal(newSVpv((char *)vimbuf->b_fname, 0)));
1683
1684void
1685Number(vimbuf)
1686 VIBUF vimbuf;
1687
1688 PPCODE:
1689 if (!buf_valid(vimbuf))
1690 vimbuf = curbuf;
1691 XPUSHs(sv_2mortal(newSViv(vimbuf->b_fnum)));
1692
1693void
1694Count(vimbuf)
1695 VIBUF vimbuf;
1696
1697 PPCODE:
1698 if (!buf_valid(vimbuf))
1699 vimbuf = curbuf;
1700 XPUSHs(sv_2mortal(newSViv(vimbuf->b_ml.ml_line_count)));
1701
1702void
1703Get(vimbuf, ...)
1704 VIBUF vimbuf;
1705
1706 PREINIT:
1707 char_u *line;
1708 int i;
1709 long lnum;
1710 PPCODE:
1711 if (buf_valid(vimbuf))
1712 {
1713 for (i = 1; i < items; i++)
1714 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001715 lnum = (long) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1717 {
1718 line = ml_get_buf(vimbuf, lnum, FALSE);
1719 XPUSHs(sv_2mortal(newSVpv((char *)line, 0)));
1720 }
1721 }
1722 }
1723
1724void
1725Set(vimbuf, ...)
1726 VIBUF vimbuf;
1727
1728 PREINIT:
1729 int i;
1730 long lnum;
1731 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 PPCODE:
1733 if (buf_valid(vimbuf))
1734 {
1735 if (items < 3)
1736 croak("Usage: VIBUF::Set(vimbuf, lnum, @lines)");
1737
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001738 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 for(i = 2; i < items; i++, lnum++)
1740 {
1741 line = SvPV(ST(i),PL_na);
1742 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1743 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001744 aco_save_T aco;
1745
1746 /* set curwin/curbuf for "vimbuf" and save some things */
1747 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001748
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 if (u_savesub(lnum) == OK)
1750 {
1751 ml_replace(lnum, (char_u *)line, TRUE);
1752 changed_bytes(lnum, 0);
1753 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001754
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001755 /* restore curwin/curbuf and a few other things */
1756 aucmd_restbuf(&aco);
1757 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 }
1759 }
1760 }
1761
1762void
1763Delete(vimbuf, ...)
1764 VIBUF vimbuf;
1765
1766 PREINIT:
1767 long i, lnum = 0, count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 PPCODE:
1769 if (buf_valid(vimbuf))
1770 {
1771 if (items == 2)
1772 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001773 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 count = 1;
1775 }
1776 else if (items == 3)
1777 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001778 lnum = (long) SvIV(ST(1));
1779 count = (long) 1 + SvIV(ST(2)) - lnum;
Bram Moolenaara1711622011-07-27 14:15:46 +02001780 if (count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 count = 1;
Bram Moolenaara1711622011-07-27 14:15:46 +02001782 if (count < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 {
1784 lnum -= count;
1785 count = -count;
1786 }
1787 }
1788 if (items >= 2)
1789 {
1790 for (i = 0; i < count; i++)
1791 {
1792 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1793 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001794 aco_save_T aco;
1795
1796 /* set curwin/curbuf for "vimbuf" and save some things */
1797 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001798
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 if (u_savedel(lnum, 1) == OK)
1800 {
1801 ml_delete(lnum, 0);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00001802 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 deleted_lines_mark(lnum, 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001805
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001806 /* restore curwin/curbuf and a few other things */
1807 aucmd_restbuf(&aco);
1808 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001809
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 update_curbuf(VALID);
1811 }
1812 }
1813 }
1814 }
1815
1816void
1817Append(vimbuf, ...)
1818 VIBUF vimbuf;
1819
1820 PREINIT:
1821 int i;
1822 long lnum;
1823 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 PPCODE:
1825 if (buf_valid(vimbuf))
1826 {
1827 if (items < 3)
1828 croak("Usage: VIBUF::Append(vimbuf, lnum, @lines)");
1829
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001830 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 for (i = 2; i < items; i++, lnum++)
1832 {
1833 line = SvPV(ST(i),PL_na);
1834 if (lnum >= 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1835 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001836 aco_save_T aco;
1837
1838 /* set curwin/curbuf for "vimbuf" and save some things */
1839 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001840
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 if (u_inssub(lnum + 1) == OK)
1842 {
1843 ml_append(lnum, (char_u *)line, (colnr_T)0, FALSE);
1844 appended_lines_mark(lnum, 1L);
1845 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001846
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001847 /* restore curwin/curbuf and a few other things */
1848 aucmd_restbuf(&aco);
1849 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001850
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 update_curbuf(VALID);
1852 }
1853 }
1854 }
1855
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001856#ifdef __GNUC__
1857# pragma GCC diagnostic pop
1858#endif