blob: 4fbc13e3a5cd34c3dde842a2a736e75f4189829e [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>
60
Bram Moolenaar071d4272004-06-13 20:20:40 +000061
62/*
63 * Work around clashes between Perl and Vim namespace. proto.h doesn't
64 * include if_perl.pro and perlsfio.pro when IN_PERL_FILE is defined, because
65 * we need the CV typedef. proto.h can't be moved to after including
66 * if_perl.h, because we get all sorts of name clashes then.
67 */
68#ifndef PROTO
69#ifndef __MINGW32__
70# include "proto/if_perl.pro"
71# include "proto/if_perlsfio.pro"
72#endif
73#endif
74
75/* Perl compatibility stuff. This should ensure compatibility with older
76 * versions of Perl.
77 */
78
79#ifndef PERL_VERSION
80# include <patchlevel.h>
81# define PERL_REVISION 5
82# define PERL_VERSION PATCHLEVEL
83# define PERL_SUBVERSION SUBVERSION
84#endif
85
Bram Moolenaar700d1d72007-09-13 13:20:16 +000086/*
87 * Quoting Jan Dubois of Active State:
88 * ActivePerl build 822 still identifies itself as 5.8.8 but already
89 * contains many of the changes from the upcoming Perl 5.8.9 release.
90 *
91 * The changes include addition of two symbols (Perl_sv_2iv_flags,
92 * Perl_newXS_flags) not present in earlier releases.
93 *
Bram Moolenaar3b9b13e2007-09-15 12:49:35 +000094 * Jan Dubois suggested the following guarding scheme.
95 *
96 * Active State defined ACTIVEPERL_VERSION as a string in versions before
97 * 5.8.8; and so the comparison to 822 below needs to be guarded.
Bram Moolenaar700d1d72007-09-13 13:20:16 +000098 */
Bram Moolenaar3b9b13e2007-09-15 12:49:35 +000099#if (PERL_REVISION == 5) && (PERL_VERSION == 8) && (PERL_SUBVERSION >= 8)
100# if (ACTIVEPERL_VERSION >= 822) || (PERL_SUBVERSION >= 9)
101# define PERL589_OR_LATER
102# endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000103#endif
104#if (PERL_REVISION == 5) && (PERL_VERSION >= 9)
105# define PERL589_OR_LATER
106#endif
107
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100108#if (PERL_REVISION == 5) && ((PERL_VERSION > 10) || \
109 (PERL_VERSION == 10) && (PERL_SUBVERSION >= 1))
110# define PERL5101_OR_LATER
111#endif
112
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#ifndef pTHX
114# define pTHX void
115# define pTHX_
116#endif
117
118#ifndef EXTERN_C
119# define EXTERN_C
120#endif
121
Bram Moolenaarc271c482012-08-08 13:17:31 +0200122#if (PERL_REVISION == 5) && (PERL_VERSION >= 14) && defined(_MSC_VER)
123/* Using PL_errgv to get the error message after perl_eval_sv() causes a crash
124 * with MSVC and Perl version 5.14. */
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100125# define CHECK_EVAL_ERR(len) SvPV(perl_get_sv("@", GV_ADD), (len));
126#else
127# define CHECK_EVAL_ERR(len) SvPV(GvSV(PL_errgv), (len));
Bram Moolenaarc271c482012-08-08 13:17:31 +0200128#endif
129
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130/* Compatibility hacks over */
131
132static PerlInterpreter *perl_interp = NULL;
Bram Moolenaard99df422016-01-29 23:20:40 +0100133static void xs_init(pTHX);
134static void VIM_init(void);
135EXTERN_C void boot_DynaLoader(pTHX_ CV*);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
137/*
Bram Moolenaare06c1882010-07-21 22:05:20 +0200138 * For dynamic linked perl.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139 */
140#if defined(DYNAMIC_PERL) || defined(PROTO)
Bram Moolenaare06c1882010-07-21 22:05:20 +0200141
142#ifndef DYNAMIC_PERL /* just generating prototypes */
Bram Moolenaar766fb0d2010-07-22 11:34:16 +0200143#ifdef WIN3264
Bram Moolenaare06c1882010-07-21 22:05:20 +0200144typedef int HANDLE;
145#endif
146typedef int XSINIT_t;
147typedef int XSUBADDR_t;
Bram Moolenaard8619992014-03-12 17:08:05 +0100148#endif
149#ifndef USE_ITHREADS
Bram Moolenaare06c1882010-07-21 22:05:20 +0200150typedef int perl_key;
151#endif
152
Bram Moolenaar766fb0d2010-07-22 11:34:16 +0200153#ifndef WIN3264
Bram Moolenaare06c1882010-07-21 22:05:20 +0200154#include <dlfcn.h>
155#define HANDLE void*
156#define PERL_PROC void*
157#define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
158#define symbol_from_dll dlsym
159#define close_dll dlclose
160#else
161#define PERL_PROC FARPROC
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200162#define load_dll vimLoadLib
Bram Moolenaare06c1882010-07-21 22:05:20 +0200163#define symbol_from_dll GetProcAddress
164#define close_dll FreeLibrary
165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166/*
167 * Wrapper defines
168 */
169# define perl_alloc dll_perl_alloc
170# define perl_construct dll_perl_construct
171# define perl_parse dll_perl_parse
172# define perl_run dll_perl_run
173# define perl_destruct dll_perl_destruct
174# define perl_free dll_perl_free
175# define Perl_get_context dll_Perl_get_context
176# define Perl_croak dll_Perl_croak
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100177# ifdef PERL5101_OR_LATER
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100178# define Perl_croak_xs_usage dll_Perl_croak_xs_usage
179# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180# ifndef PROTO
181# define Perl_croak_nocontext dll_Perl_croak_nocontext
182# define Perl_call_argv dll_Perl_call_argv
183# define Perl_call_pv dll_Perl_call_pv
184# define Perl_eval_sv dll_Perl_eval_sv
185# define Perl_get_sv dll_Perl_get_sv
186# define Perl_eval_pv dll_Perl_eval_pv
187# define Perl_call_method dll_Perl_call_method
188# endif
189# define Perl_dowantarray dll_Perl_dowantarray
190# define Perl_free_tmps dll_Perl_free_tmps
191# define Perl_gv_stashpv dll_Perl_gv_stashpv
192# define Perl_markstack_grow dll_Perl_markstack_grow
193# define Perl_mg_find dll_Perl_mg_find
194# define Perl_newXS dll_Perl_newXS
195# define Perl_newSV dll_Perl_newSV
196# define Perl_newSViv dll_Perl_newSViv
197# define Perl_newSVpv dll_Perl_newSVpv
198# define Perl_pop_scope dll_Perl_pop_scope
199# define Perl_push_scope dll_Perl_push_scope
200# define Perl_save_int dll_Perl_save_int
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200201# if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
202# define Perl_save_strlen dll_Perl_save_strlen
203# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204# define Perl_stack_grow dll_Perl_stack_grow
205# define Perl_set_context dll_Perl_set_context
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200206# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200207# define Perl_sv_2bool_flags dll_Perl_sv_2bool_flags
208# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
209# define Perl_xs_apiversion_bootcheck dll_Perl_xs_apiversion_bootcheck
210# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200211# else
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200212# define Perl_sv_2bool dll_Perl_sv_2bool
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200213# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214# define Perl_sv_2iv dll_Perl_sv_2iv
215# define Perl_sv_2mortal dll_Perl_sv_2mortal
216# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
217# define Perl_sv_2pv_flags dll_Perl_sv_2pv_flags
218# define Perl_sv_2pv_nolen dll_Perl_sv_2pv_nolen
219# else
220# define Perl_sv_2pv dll_Perl_sv_2pv
221# endif
222# define Perl_sv_bless dll_Perl_sv_bless
223# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
224# define Perl_sv_catpvn_flags dll_Perl_sv_catpvn_flags
225# else
226# define Perl_sv_catpvn dll_Perl_sv_catpvn
227# endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000228#ifdef PERL589_OR_LATER
229# define Perl_sv_2iv_flags dll_Perl_sv_2iv_flags
230# define Perl_newXS_flags dll_Perl_newXS_flags
231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232# define Perl_sv_free dll_Perl_sv_free
Bram Moolenaarccf22172008-09-01 15:56:45 +0000233# if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
234# define Perl_sv_free2 dll_Perl_sv_free2
235# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236# define Perl_sv_isa dll_Perl_sv_isa
237# define Perl_sv_magic dll_Perl_sv_magic
238# define Perl_sv_setiv dll_Perl_sv_setiv
239# define Perl_sv_setpv dll_Perl_sv_setpv
240# define Perl_sv_setpvn dll_Perl_sv_setpvn
241# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
242# define Perl_sv_setsv_flags dll_Perl_sv_setsv_flags
243# else
244# define Perl_sv_setsv dll_Perl_sv_setsv
245# endif
246# define Perl_sv_upgrade dll_Perl_sv_upgrade
247# define Perl_Tstack_sp_ptr dll_Perl_Tstack_sp_ptr
248# define Perl_Top_ptr dll_Perl_Top_ptr
249# define Perl_Tstack_base_ptr dll_Perl_Tstack_base_ptr
250# define Perl_Tstack_max_ptr dll_Perl_Tstack_max_ptr
251# define Perl_Ttmps_ix_ptr dll_Perl_Ttmps_ix_ptr
252# define Perl_Ttmps_floor_ptr dll_Perl_Ttmps_floor_ptr
253# define Perl_Tmarkstack_ptr_ptr dll_Perl_Tmarkstack_ptr_ptr
254# define Perl_Tmarkstack_max_ptr dll_Perl_Tmarkstack_max_ptr
255# define Perl_TSv_ptr dll_Perl_TSv_ptr
256# define Perl_TXpv_ptr dll_Perl_TXpv_ptr
257# define Perl_Tna_ptr dll_Perl_Tna_ptr
258# define Perl_Idefgv_ptr dll_Perl_Idefgv_ptr
259# define Perl_Ierrgv_ptr dll_Perl_Ierrgv_ptr
260# define Perl_Isv_yes_ptr dll_Perl_Isv_yes_ptr
261# define boot_DynaLoader dll_boot_DynaLoader
Bram Moolenaare06c1882010-07-21 22:05:20 +0200262# define Perl_Gthr_key_ptr dll_Perl_Gthr_key_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000264# define Perl_sys_init dll_Perl_sys_init
Bram Moolenaarc236c162008-07-13 17:41:49 +0000265# define Perl_sys_term dll_Perl_sys_term
266# define Perl_ISv_ptr dll_Perl_ISv_ptr
267# define Perl_Istack_max_ptr dll_Perl_Istack_max_ptr
268# define Perl_Istack_base_ptr dll_Perl_Istack_base_ptr
269# define Perl_Itmps_ix_ptr dll_Perl_Itmps_ix_ptr
270# define Perl_Itmps_floor_ptr dll_Perl_Itmps_floor_ptr
271# define Perl_IXpv_ptr dll_Perl_IXpv_ptr
272# define Perl_Ina_ptr dll_Perl_Ina_ptr
273# define Perl_Imarkstack_ptr_ptr dll_Perl_Imarkstack_ptr_ptr
274# define Perl_Imarkstack_max_ptr dll_Perl_Imarkstack_max_ptr
275# define Perl_Istack_sp_ptr dll_Perl_Istack_sp_ptr
276# define Perl_Iop_ptr dll_Perl_Iop_ptr
277# define Perl_call_list dll_Perl_call_list
278# define Perl_Iscopestack_ix_ptr dll_Perl_Iscopestack_ix_ptr
279# define Perl_Iunitcheckav_ptr dll_Perl_Iunitcheckav_ptr
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200280# if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
281# define Perl_xs_handshake dll_Perl_xs_handshake
282# define Perl_xs_boot_epilog dll_Perl_xs_boot_epilog
283# endif
Bram Moolenaar01c10522012-09-21 12:50:51 +0200284# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100285# ifdef USE_ITHREADS
286# define PL_thr_key *dll_PL_thr_key
287# endif
Bram Moolenaar01c10522012-09-21 12:50:51 +0200288# endif
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100289# define Perl_hv_iternext_flags dll_Perl_hv_iternext_flags
290# define Perl_hv_iterinit dll_Perl_hv_iterinit
291# define Perl_hv_iterkey dll_Perl_hv_iterkey
292# define Perl_hv_iterval dll_Perl_hv_iterval
293# define Perl_av_fetch dll_Perl_av_fetch
294# define Perl_av_len dll_Perl_av_len
295# define Perl_sv_2nv_flags dll_Perl_sv_2nv_flags
Bram Moolenaarc236c162008-07-13 17:41:49 +0000296
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297/*
298 * Declare HANDLE for perl.dll and function pointers.
299 */
300static HANDLE hPerlLib = NULL;
301
302static PerlInterpreter* (*perl_alloc)();
303static void (*perl_construct)(PerlInterpreter*);
304static void (*perl_destruct)(PerlInterpreter*);
305static void (*perl_free)(PerlInterpreter*);
306static int (*perl_run)(PerlInterpreter*);
307static int (*perl_parse)(PerlInterpreter*, XSINIT_t, int, char**, char**);
308static void* (*Perl_get_context)(void);
Bram Moolenaar864733a2016-04-02 14:18:01 +0200309static void (*Perl_croak)(pTHX_ const char*, ...) __attribute__noreturn__;
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100310#ifdef PERL5101_OR_LATER
Bram Moolenaar6b107212013-12-11 15:06:40 +0100311/* Perl-5.18 has a different Perl_croak_xs_usage signature. */
312# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
Bram Moolenaar864733a2016-04-02 14:18:01 +0200313static void (*Perl_croak_xs_usage)(const CV *const, const char *const params)
314 __attribute__noreturn__;
Bram Moolenaar6b107212013-12-11 15:06:40 +0100315# else
Bram Moolenaar864733a2016-04-02 14:18:01 +0200316static void (*Perl_croak_xs_usage)(pTHX_ const CV *const, const char *const params)
317 __attribute__noreturn__;
Bram Moolenaar6b107212013-12-11 15:06:40 +0100318# endif
Bram Moolenaar9be6e212013-06-15 16:47:35 +0200319#endif
Bram Moolenaar864733a2016-04-02 14:18:01 +0200320static void (*Perl_croak_nocontext)(const char*, ...) __attribute__noreturn__;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321static I32 (*Perl_dowantarray)(pTHX);
322static void (*Perl_free_tmps)(pTHX);
323static HV* (*Perl_gv_stashpv)(pTHX_ const char*, I32);
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200324#if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
325static I32* (*Perl_markstack_grow)(pTHX);
326#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327static void (*Perl_markstack_grow)(pTHX);
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329static MAGIC* (*Perl_mg_find)(pTHX_ SV*, int);
330static CV* (*Perl_newXS)(pTHX_ char*, XSUBADDR_t, char*);
331static SV* (*Perl_newSV)(pTHX_ STRLEN);
332static SV* (*Perl_newSViv)(pTHX_ IV);
333static SV* (*Perl_newSVpv)(pTHX_ const char*, STRLEN);
334static I32 (*Perl_call_argv)(pTHX_ const char*, I32, char**);
335static I32 (*Perl_call_pv)(pTHX_ const char*, I32);
336static I32 (*Perl_eval_sv)(pTHX_ SV*, I32);
337static SV* (*Perl_get_sv)(pTHX_ const char*, I32);
338static SV* (*Perl_eval_pv)(pTHX_ const char*, I32);
339static SV* (*Perl_call_method)(pTHX_ const char*, I32);
340static void (*Perl_pop_scope)(pTHX);
341static void (*Perl_push_scope)(pTHX);
342static void (*Perl_save_int)(pTHX_ int*);
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200343#if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
344static void (*Perl_save_strlen)(pTHX_ STRLEN* ptr);
345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346static SV** (*Perl_stack_grow)(pTHX_ SV**, SV**p, int);
347static SV** (*Perl_set_context)(void*);
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200348#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
349static bool (*Perl_sv_2bool_flags)(pTHX_ SV*, I32);
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200350# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200351static void (*Perl_xs_apiversion_bootcheck)(pTHX_ SV *module, const char *api_p, STRLEN api_len);
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200352# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200353#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354static bool (*Perl_sv_2bool)(pTHX_ SV*);
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356static IV (*Perl_sv_2iv)(pTHX_ SV*);
357static SV* (*Perl_sv_2mortal)(pTHX_ SV*);
358#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
359static char* (*Perl_sv_2pv_flags)(pTHX_ SV*, STRLEN*, I32);
360static char* (*Perl_sv_2pv_nolen)(pTHX_ SV*);
361#else
362static char* (*Perl_sv_2pv)(pTHX_ SV*, STRLEN*);
363#endif
364static SV* (*Perl_sv_bless)(pTHX_ SV*, HV*);
365#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
366static void (*Perl_sv_catpvn_flags)(pTHX_ SV* , const char*, STRLEN, I32);
367#else
368static void (*Perl_sv_catpvn)(pTHX_ SV*, const char*, STRLEN);
369#endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000370#ifdef PERL589_OR_LATER
371static IV (*Perl_sv_2iv_flags)(pTHX_ SV* sv, I32 flags);
372static CV * (*Perl_newXS_flags)(pTHX_ const char *name, XSUBADDR_t subaddr, const char *const filename, const char *const proto, U32 flags);
373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374static void (*Perl_sv_free)(pTHX_ SV*);
375static int (*Perl_sv_isa)(pTHX_ SV*, const char*);
376static void (*Perl_sv_magic)(pTHX_ SV*, SV*, int, const char*, I32);
377static void (*Perl_sv_setiv)(pTHX_ SV*, IV);
378static void (*Perl_sv_setpv)(pTHX_ SV*, const char*);
379static void (*Perl_sv_setpvn)(pTHX_ SV*, const char*, STRLEN);
380#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
381static void (*Perl_sv_setsv_flags)(pTHX_ SV*, SV*, I32);
382#else
383static void (*Perl_sv_setsv)(pTHX_ SV*, SV*);
384#endif
385static bool (*Perl_sv_upgrade)(pTHX_ SV*, U32);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200386#if (PERL_REVISION == 5) && (PERL_VERSION < 10)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387static SV*** (*Perl_Tstack_sp_ptr)(register PerlInterpreter*);
388static OP** (*Perl_Top_ptr)(register PerlInterpreter*);
389static SV*** (*Perl_Tstack_base_ptr)(register PerlInterpreter*);
390static SV*** (*Perl_Tstack_max_ptr)(register PerlInterpreter*);
391static I32* (*Perl_Ttmps_ix_ptr)(register PerlInterpreter*);
392static I32* (*Perl_Ttmps_floor_ptr)(register PerlInterpreter*);
393static I32** (*Perl_Tmarkstack_ptr_ptr)(register PerlInterpreter*);
394static I32** (*Perl_Tmarkstack_max_ptr)(register PerlInterpreter*);
395static SV** (*Perl_TSv_ptr)(register PerlInterpreter*);
396static XPV** (*Perl_TXpv_ptr)(register PerlInterpreter*);
397static STRLEN* (*Perl_Tna_ptr)(register PerlInterpreter*);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200398#else
Bram Moolenaar6b107212013-12-11 15:06:40 +0100399/* Perl-5.18 has a different Perl_sv_free2 signature. */
400# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
401static void (*Perl_sv_free2)(pTHX_ SV*, const U32);
402# else
Bram Moolenaarccf22172008-09-01 15:56:45 +0000403static void (*Perl_sv_free2)(pTHX_ SV*);
Bram Moolenaar6b107212013-12-11 15:06:40 +0100404# endif
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000405static void (*Perl_sys_init)(int* argc, char*** argv);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000406static void (*Perl_sys_term)(void);
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200407static void (*Perl_call_list)(pTHX_ I32, AV*);
408# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
409# else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000410static SV** (*Perl_ISv_ptr)(register PerlInterpreter*);
411static SV*** (*Perl_Istack_max_ptr)(register PerlInterpreter*);
412static SV*** (*Perl_Istack_base_ptr)(register PerlInterpreter*);
413static XPV** (*Perl_IXpv_ptr)(register PerlInterpreter*);
414static I32* (*Perl_Itmps_ix_ptr)(register PerlInterpreter*);
415static I32* (*Perl_Itmps_floor_ptr)(register PerlInterpreter*);
416static STRLEN* (*Perl_Ina_ptr)(register PerlInterpreter*);
417static I32** (*Perl_Imarkstack_ptr_ptr)(register PerlInterpreter*);
418static I32** (*Perl_Imarkstack_max_ptr)(register PerlInterpreter*);
419static SV*** (*Perl_Istack_sp_ptr)(register PerlInterpreter*);
420static OP** (*Perl_Iop_ptr)(register PerlInterpreter*);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000421static I32* (*Perl_Iscopestack_ix_ptr)(register PerlInterpreter*);
422static AV** (*Perl_Iunitcheckav_ptr)(register PerlInterpreter*);
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200423# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000424#endif
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200425#if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
426static I32 (*Perl_xs_handshake)(const U32, void *, const char *, ...);
427static void (*Perl_xs_boot_epilog)(pTHX_ const U32);
428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200430#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100431# ifdef USE_ITHREADS
Bram Moolenaar01c10522012-09-21 12:50:51 +0200432static perl_key* dll_PL_thr_key;
Bram Moolenaard8619992014-03-12 17:08:05 +0100433# endif
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200434#else
Bram Moolenaare06c1882010-07-21 22:05:20 +0200435static GV** (*Perl_Idefgv_ptr)(register PerlInterpreter*);
436static GV** (*Perl_Ierrgv_ptr)(register PerlInterpreter*);
437static SV* (*Perl_Isv_yes_ptr)(register PerlInterpreter*);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200438static perl_key* (*Perl_Gthr_key_ptr)_((pTHX));
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200439#endif
440static void (*boot_DynaLoader)_((pTHX_ CV*));
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100441static HE * (*Perl_hv_iternext_flags)(pTHX_ HV *, I32);
442static I32 (*Perl_hv_iterinit)(pTHX_ HV *);
443static char * (*Perl_hv_iterkey)(pTHX_ HE *, I32 *);
444static SV * (*Perl_hv_iterval)(pTHX_ HV *, HE *);
445static SV** (*Perl_av_fetch)(pTHX_ AV *, SSize_t, I32);
446static SSize_t (*Perl_av_len)(pTHX_ AV *);
447static NV (*Perl_sv_2nv_flags)(pTHX_ SV *const, const I32);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200448
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449/*
450 * Table of name to function pointer of perl.
451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452static struct {
453 char* name;
454 PERL_PROC* ptr;
455} perl_funcname_table[] = {
456 {"perl_alloc", (PERL_PROC*)&perl_alloc},
457 {"perl_construct", (PERL_PROC*)&perl_construct},
458 {"perl_destruct", (PERL_PROC*)&perl_destruct},
459 {"perl_free", (PERL_PROC*)&perl_free},
460 {"perl_run", (PERL_PROC*)&perl_run},
461 {"perl_parse", (PERL_PROC*)&perl_parse},
462 {"Perl_get_context", (PERL_PROC*)&Perl_get_context},
463 {"Perl_croak", (PERL_PROC*)&Perl_croak},
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100464#ifdef PERL5101_OR_LATER
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100465 {"Perl_croak_xs_usage", (PERL_PROC*)&Perl_croak_xs_usage},
466#endif
Bram Moolenaard8619992014-03-12 17:08:05 +0100467#ifdef PERL_IMPLICIT_CONTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 {"Perl_croak_nocontext", (PERL_PROC*)&Perl_croak_nocontext},
Bram Moolenaard8619992014-03-12 17:08:05 +0100469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 {"Perl_dowantarray", (PERL_PROC*)&Perl_dowantarray},
471 {"Perl_free_tmps", (PERL_PROC*)&Perl_free_tmps},
472 {"Perl_gv_stashpv", (PERL_PROC*)&Perl_gv_stashpv},
473 {"Perl_markstack_grow", (PERL_PROC*)&Perl_markstack_grow},
474 {"Perl_mg_find", (PERL_PROC*)&Perl_mg_find},
475 {"Perl_newXS", (PERL_PROC*)&Perl_newXS},
476 {"Perl_newSV", (PERL_PROC*)&Perl_newSV},
477 {"Perl_newSViv", (PERL_PROC*)&Perl_newSViv},
478 {"Perl_newSVpv", (PERL_PROC*)&Perl_newSVpv},
479 {"Perl_call_argv", (PERL_PROC*)&Perl_call_argv},
480 {"Perl_call_pv", (PERL_PROC*)&Perl_call_pv},
481 {"Perl_eval_sv", (PERL_PROC*)&Perl_eval_sv},
482 {"Perl_get_sv", (PERL_PROC*)&Perl_get_sv},
483 {"Perl_eval_pv", (PERL_PROC*)&Perl_eval_pv},
484 {"Perl_call_method", (PERL_PROC*)&Perl_call_method},
485 {"Perl_pop_scope", (PERL_PROC*)&Perl_pop_scope},
486 {"Perl_push_scope", (PERL_PROC*)&Perl_push_scope},
487 {"Perl_save_int", (PERL_PROC*)&Perl_save_int},
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200488#if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
489 {"Perl_save_strlen", (PERL_PROC*)&Perl_save_strlen},
490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 {"Perl_stack_grow", (PERL_PROC*)&Perl_stack_grow},
492 {"Perl_set_context", (PERL_PROC*)&Perl_set_context},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200493#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
494 {"Perl_sv_2bool_flags", (PERL_PROC*)&Perl_sv_2bool_flags},
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200495# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200496 {"Perl_xs_apiversion_bootcheck",(PERL_PROC*)&Perl_xs_apiversion_bootcheck},
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200497# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200498#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 {"Perl_sv_2bool", (PERL_PROC*)&Perl_sv_2bool},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 {"Perl_sv_2iv", (PERL_PROC*)&Perl_sv_2iv},
502 {"Perl_sv_2mortal", (PERL_PROC*)&Perl_sv_2mortal},
503#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
504 {"Perl_sv_2pv_flags", (PERL_PROC*)&Perl_sv_2pv_flags},
505 {"Perl_sv_2pv_nolen", (PERL_PROC*)&Perl_sv_2pv_nolen},
506#else
507 {"Perl_sv_2pv", (PERL_PROC*)&Perl_sv_2pv},
508#endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000509#ifdef PERL589_OR_LATER
510 {"Perl_sv_2iv_flags", (PERL_PROC*)&Perl_sv_2iv_flags},
511 {"Perl_newXS_flags", (PERL_PROC*)&Perl_newXS_flags},
512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 {"Perl_sv_bless", (PERL_PROC*)&Perl_sv_bless},
514#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
515 {"Perl_sv_catpvn_flags", (PERL_PROC*)&Perl_sv_catpvn_flags},
516#else
517 {"Perl_sv_catpvn", (PERL_PROC*)&Perl_sv_catpvn},
518#endif
519 {"Perl_sv_free", (PERL_PROC*)&Perl_sv_free},
520 {"Perl_sv_isa", (PERL_PROC*)&Perl_sv_isa},
521 {"Perl_sv_magic", (PERL_PROC*)&Perl_sv_magic},
522 {"Perl_sv_setiv", (PERL_PROC*)&Perl_sv_setiv},
523 {"Perl_sv_setpv", (PERL_PROC*)&Perl_sv_setpv},
524 {"Perl_sv_setpvn", (PERL_PROC*)&Perl_sv_setpvn},
525#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
526 {"Perl_sv_setsv_flags", (PERL_PROC*)&Perl_sv_setsv_flags},
527#else
528 {"Perl_sv_setsv", (PERL_PROC*)&Perl_sv_setsv},
529#endif
530 {"Perl_sv_upgrade", (PERL_PROC*)&Perl_sv_upgrade},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000531#if (PERL_REVISION == 5) && (PERL_VERSION < 10)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 {"Perl_Tstack_sp_ptr", (PERL_PROC*)&Perl_Tstack_sp_ptr},
533 {"Perl_Top_ptr", (PERL_PROC*)&Perl_Top_ptr},
534 {"Perl_Tstack_base_ptr", (PERL_PROC*)&Perl_Tstack_base_ptr},
535 {"Perl_Tstack_max_ptr", (PERL_PROC*)&Perl_Tstack_max_ptr},
536 {"Perl_Ttmps_ix_ptr", (PERL_PROC*)&Perl_Ttmps_ix_ptr},
537 {"Perl_Ttmps_floor_ptr", (PERL_PROC*)&Perl_Ttmps_floor_ptr},
538 {"Perl_Tmarkstack_ptr_ptr", (PERL_PROC*)&Perl_Tmarkstack_ptr_ptr},
539 {"Perl_Tmarkstack_max_ptr", (PERL_PROC*)&Perl_Tmarkstack_max_ptr},
540 {"Perl_TSv_ptr", (PERL_PROC*)&Perl_TSv_ptr},
541 {"Perl_TXpv_ptr", (PERL_PROC*)&Perl_TXpv_ptr},
542 {"Perl_Tna_ptr", (PERL_PROC*)&Perl_Tna_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000543#else
Bram Moolenaarccf22172008-09-01 15:56:45 +0000544 {"Perl_sv_free2", (PERL_PROC*)&Perl_sv_free2},
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000545 {"Perl_sys_init", (PERL_PROC*)&Perl_sys_init},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000546 {"Perl_sys_term", (PERL_PROC*)&Perl_sys_term},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200547 {"Perl_call_list", (PERL_PROC*)&Perl_call_list},
548# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
549# else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000550 {"Perl_ISv_ptr", (PERL_PROC*)&Perl_ISv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000551 {"Perl_Istack_max_ptr", (PERL_PROC*)&Perl_Istack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200552 {"Perl_Istack_base_ptr", (PERL_PROC*)&Perl_Istack_base_ptr},
553 {"Perl_IXpv_ptr", (PERL_PROC*)&Perl_IXpv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000554 {"Perl_Itmps_ix_ptr", (PERL_PROC*)&Perl_Itmps_ix_ptr},
555 {"Perl_Itmps_floor_ptr", (PERL_PROC*)&Perl_Itmps_floor_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200556 {"Perl_Ina_ptr", (PERL_PROC*)&Perl_Ina_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000557 {"Perl_Imarkstack_ptr_ptr", (PERL_PROC*)&Perl_Imarkstack_ptr_ptr},
558 {"Perl_Imarkstack_max_ptr", (PERL_PROC*)&Perl_Imarkstack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200559 {"Perl_Istack_sp_ptr", (PERL_PROC*)&Perl_Istack_sp_ptr},
560 {"Perl_Iop_ptr", (PERL_PROC*)&Perl_Iop_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000561 {"Perl_Iscopestack_ix_ptr", (PERL_PROC*)&Perl_Iscopestack_ix_ptr},
562 {"Perl_Iunitcheckav_ptr", (PERL_PROC*)&Perl_Iunitcheckav_ptr},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200563# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000564#endif
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200565#if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
566 {"Perl_xs_handshake", (PERL_PROC*)&Perl_xs_handshake},
567 {"Perl_xs_boot_epilog", (PERL_PROC*)&Perl_xs_boot_epilog},
568#endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200569#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100570# ifdef USE_ITHREADS
Bram Moolenaar01c10522012-09-21 12:50:51 +0200571 {"PL_thr_key", (PERL_PROC*)&dll_PL_thr_key},
Bram Moolenaard8619992014-03-12 17:08:05 +0100572# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200573#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 {"Perl_Idefgv_ptr", (PERL_PROC*)&Perl_Idefgv_ptr},
575 {"Perl_Ierrgv_ptr", (PERL_PROC*)&Perl_Ierrgv_ptr},
576 {"Perl_Isv_yes_ptr", (PERL_PROC*)&Perl_Isv_yes_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200577 {"Perl_Gthr_key_ptr", (PERL_PROC*)&Perl_Gthr_key_ptr},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200578#endif
579 {"boot_DynaLoader", (PERL_PROC*)&boot_DynaLoader},
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100580 {"Perl_hv_iternext_flags", (PERL_PROC*)&Perl_hv_iternext_flags},
581 {"Perl_hv_iterinit", (PERL_PROC*)&Perl_hv_iterinit},
582 {"Perl_hv_iterkey", (PERL_PROC*)&Perl_hv_iterkey},
583 {"Perl_hv_iterval", (PERL_PROC*)&Perl_hv_iterval},
584 {"Perl_av_fetch", (PERL_PROC*)&Perl_av_fetch},
585 {"Perl_av_len", (PERL_PROC*)&Perl_av_len},
586 {"Perl_sv_2nv_flags", (PERL_PROC*)&Perl_sv_2nv_flags},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 {"", NULL},
588};
589
Bram Moolenaar6b107212013-12-11 15:06:40 +0100590/* Work around for perl-5.18.
591 * The definitions of S_SvREFCNT_inc and S_SvREFCNT_dec are needed, so include
592 * "perl\lib\CORE\inline.h", after Perl_sv_free2 is defined.
593 * The linker won't complain about undefined __impl_Perl_sv_free2. */
594#if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
Bram Moolenaar864733a2016-04-02 14:18:01 +0200595# define PL_memory_wrap "panic: memory wrap" /* Dummy */
Bram Moolenaar6b107212013-12-11 15:06:40 +0100596# include <inline.h>
Bram Moolenaar864733a2016-04-02 14:18:01 +0200597# undef PL_memory_wrap
Bram Moolenaar6b107212013-12-11 15:06:40 +0100598#endif
599
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600/*
601 * Make all runtime-links of perl.
602 *
Bram Moolenaar364ab2f2013-08-02 20:05:32 +0200603 * 1. Get module handle using dlopen() or vimLoadLib().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 * 2. Get pointer to perl function by GetProcAddress.
605 * 3. Repeat 2, until get all functions will be used.
606 *
607 * Parameter 'libname' provides name of DLL.
608 * Return OK or FAIL.
609 */
610 static int
611perl_runtime_link_init(char *libname, int verbose)
612{
613 int i;
614
615 if (hPerlLib != NULL)
616 return OK;
Bram Moolenaare06c1882010-07-21 22:05:20 +0200617 if ((hPerlLib = load_dll(libname)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 {
619 if (verbose)
620 EMSG2(_("E370: Could not load library %s"), libname);
621 return FAIL;
622 }
623 for (i = 0; perl_funcname_table[i].ptr; ++i)
624 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200625 if (!(*perl_funcname_table[i].ptr = symbol_from_dll(hPerlLib,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 perl_funcname_table[i].name)))
627 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200628 close_dll(hPerlLib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 hPerlLib = NULL;
630 if (verbose)
631 EMSG2(_(e_loadfunc), perl_funcname_table[i].name);
632 return FAIL;
633 }
634 }
635 return OK;
636}
637
638/*
639 * If runtime-link-perl(DLL) was loaded successfully, return TRUE.
640 * There were no DLL loaded, return FALSE.
641 */
642 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100643perl_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100645 return perl_runtime_link_init((char *)p_perldll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646}
647#endif /* DYNAMIC_PERL */
648
649/*
650 * perl_init(): initialize perl interpreter
651 * We have to call perl_parse to initialize some structures,
652 * there's nothing to actually parse.
653 */
654 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100655perl_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656{
Bram Moolenaarc236c162008-07-13 17:41:49 +0000657 char *bootargs[] = { "VI", NULL };
658 int argc = 3;
659 static char *argv[] = { "", "-e", "" };
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660
Bram Moolenaarc236c162008-07-13 17:41:49 +0000661#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000662 Perl_sys_init(&argc, (char***)&argv);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 perl_interp = perl_alloc();
665 perl_construct(perl_interp);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000666 perl_parse(perl_interp, xs_init, argc, argv, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 perl_call_argv("VIM::bootstrap", (long)G_DISCARD, bootargs);
668 VIM_init();
669#ifdef USE_SFIO
670 sfdisc(PerlIO_stdout(), sfdcnewvim());
671 sfdisc(PerlIO_stderr(), sfdcnewvim());
672 sfsetbuf(PerlIO_stdout(), NULL, 0);
673 sfsetbuf(PerlIO_stderr(), NULL, 0);
674#endif
675}
676
677/*
678 * perl_end(): clean up after ourselves
679 */
680 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100681perl_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682{
683 if (perl_interp)
684 {
685 perl_run(perl_interp);
686 perl_destruct(perl_interp);
687 perl_free(perl_interp);
688 perl_interp = NULL;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000689#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100690 Perl_sys_term();
Bram Moolenaarc236c162008-07-13 17:41:49 +0000691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 }
693#ifdef DYNAMIC_PERL
694 if (hPerlLib)
695 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200696 close_dll(hPerlLib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 hPerlLib = NULL;
698 }
699#endif
700}
701
702/*
703 * msg_split(): send a message to the message handling routines
704 * split at '\n' first though.
705 */
706 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100707msg_split(
708 char_u *s,
709 int attr) /* highlighting attributes */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710{
711 char *next;
712 char *token = (char *)s;
713
Bram Moolenaaraa8494a2007-10-09 08:47:27 +0000714 while ((next = strchr(token, '\n')) && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 {
716 *next++ = '\0'; /* replace \n with \0 */
717 msg_attr((char_u *)token, attr);
718 token = next;
719 }
Bram Moolenaaraa8494a2007-10-09 08:47:27 +0000720 if (*token && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 msg_attr((char_u *)token, attr);
722}
723
724#ifndef FEAT_EVAL
725/*
726 * This stub is needed because an "#ifdef FEAT_EVAL" around Eval() doesn't
727 * work properly.
728 */
729 char_u *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100730eval_to_string(
731 char_u *arg UNUSED,
732 char_u **nextcmd UNUSED,
733 int dolist UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734{
735 return NULL;
736}
737#endif
738
739/*
740 * Create a new reference to an SV pointing to the SCR structure
Bram Moolenaare344bea2005-09-01 20:46:49 +0000741 * The b_perl_private/w_perl_private part of the SCR structure points to the
742 * SV, so there can only be one such SV for a particular SCR structure. When
743 * the last reference has gone (DESTROY is called),
744 * b_perl_private/w_perl_private is reset; When the screen goes away before
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 * all references are gone, the value of the SV is reset;
746 * any subsequent use of any of those reference will produce
747 * a warning. (see typemap)
748 */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000749
750 static SV *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100751newWINrv(SV *rv, win_T *ptr)
Bram Moolenaare344bea2005-09-01 20:46:49 +0000752{
753 sv_upgrade(rv, SVt_RV);
754 if (ptr->w_perl_private == NULL)
755 {
756 ptr->w_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +0100757 sv_setiv(ptr->w_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +0000758 }
759 else
760 SvREFCNT_inc(ptr->w_perl_private);
761 SvRV(rv) = ptr->w_perl_private;
762 SvROK_on(rv);
763 return sv_bless(rv, gv_stashpv("VIWIN", TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764}
765
Bram Moolenaare344bea2005-09-01 20:46:49 +0000766 static SV *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100767newBUFrv(SV *rv, buf_T *ptr)
Bram Moolenaare344bea2005-09-01 20:46:49 +0000768{
769 sv_upgrade(rv, SVt_RV);
770 if (ptr->b_perl_private == NULL)
771 {
772 ptr->b_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +0100773 sv_setiv(ptr->b_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +0000774 }
775 else
776 SvREFCNT_inc(ptr->b_perl_private);
777 SvRV(rv) = ptr->b_perl_private;
778 SvROK_on(rv);
779 return sv_bless(rv, gv_stashpv("VIBUF", TRUE));
780}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781
782/*
783 * perl_win_free
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200784 * Remove all references to the window to be destroyed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 */
786 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100787perl_win_free(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000789 if (wp->w_perl_private)
790 sv_setiv((SV *)wp->w_perl_private, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 return;
792}
793
794 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100795perl_buf_free(buf_T *bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000797 if (bp->b_perl_private)
798 sv_setiv((SV *)bp->b_perl_private, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 return;
800}
801
802#ifndef PROTO
803# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
804I32 cur_val(pTHX_ IV iv, SV *sv);
805# else
806I32 cur_val(IV iv, SV *sv);
807#endif
808
809/*
810 * Handler for the magic variables $main::curwin and $main::curbuf.
811 * The handler is put into the magic vtbl for these variables.
812 * (This is effectively a C-level equivalent of a tied variable).
813 * There is no "set" function as the variables are read-only.
814 */
815# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
816I32 cur_val(pTHX_ IV iv, SV *sv)
817# else
818I32 cur_val(IV iv, SV *sv)
819# endif
820{
821 SV *rv;
822 if (iv == 0)
823 rv = newWINrv(newSV(0), curwin);
824 else
825 rv = newBUFrv(newSV(0), curbuf);
826 sv_setsv(sv, rv);
827 return 0;
828}
829#endif /* !PROTO */
830
831struct ufuncs cw_funcs = { cur_val, 0, 0 };
832struct ufuncs cb_funcs = { cur_val, 0, 1 };
833
834/*
835 * VIM_init(): Vim-specific initialisation.
836 * Make the magical main::curwin and main::curbuf variables
837 */
838 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100839VIM_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
841 static char cw[] = "main::curwin";
842 static char cb[] = "main::curbuf";
843 SV *sv;
844
845 sv = perl_get_sv(cw, TRUE);
846 sv_magic(sv, NULL, 'U', (char *)&cw_funcs, sizeof(cw_funcs));
847 SvREADONLY_on(sv);
848
849 sv = perl_get_sv(cb, TRUE);
850 sv_magic(sv, NULL, 'U', (char *)&cb_funcs, sizeof(cb_funcs));
851 SvREADONLY_on(sv);
852
853 /*
854 * Setup the Safe compartment.
855 * It shouldn't be a fatal error if the Safe module is missing.
856 * XXX: Only shares the 'Msg' routine (which has to be called
857 * like 'Msg(...)').
858 */
859 (void)perl_eval_pv( "if ( eval( 'require Safe' ) ) { $VIM::safe = Safe->new(); $VIM::safe->share_from( 'VIM', ['Msg'] ); }", G_DISCARD | G_VOID );
860
861}
862
863#ifdef DYNAMIC_PERL
864static char *e_noperl = N_("Sorry, this command is disabled: the Perl library could not be loaded.");
865#endif
866
867/*
868 * ":perl"
869 */
870 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100871ex_perl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872{
873 char *err;
874 char *script;
875 STRLEN length;
876 SV *sv;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200877#ifdef HAVE_SANDBOX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 SV *safe;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880
881 script = (char *)script_get(eap, eap->arg);
882 if (eap->skip)
883 {
884 vim_free(script);
885 return;
886 }
887
888 if (perl_interp == NULL)
889 {
890#ifdef DYNAMIC_PERL
891 if (!perl_enabled(TRUE))
892 {
893 EMSG(_(e_noperl));
894 vim_free(script);
895 return;
896 }
897#endif
898 perl_init();
899 }
900
901 {
902 dSP;
903 ENTER;
904 SAVETMPS;
905
906 if (script == NULL)
907 sv = newSVpv((char *)eap->arg, 0);
908 else
909 {
910 sv = newSVpv(script, 0);
911 vim_free(script);
912 }
913
914#ifdef HAVE_SANDBOX
915 if (sandbox)
916 {
Bram Moolenaara1711622011-07-27 14:15:46 +0200917 safe = perl_get_sv("VIM::safe", FALSE);
Bram Moolenaar3f947ea2009-07-14 14:04:54 +0000918# ifndef MAKE_TEST /* avoid a warning for unreachable code */
Bram Moolenaar954e8c52009-11-11 13:45:33 +0000919 if (safe == NULL || !SvTRUE(safe))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 EMSG(_("E299: Perl evaluation forbidden in sandbox without the Safe module"));
921 else
Bram Moolenaar3f947ea2009-07-14 14:04:54 +0000922# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 {
924 PUSHMARK(SP);
925 XPUSHs(safe);
926 XPUSHs(sv);
927 PUTBACK;
928 perl_call_method("reval", G_DISCARD);
929 }
930 }
931 else
932#endif
933 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
934
935 SvREFCNT_dec(sv);
936
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100937 err = CHECK_EVAL_ERR(length);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938
939 FREETMPS;
940 LEAVE;
941
942 if (!length)
943 return;
944
945 msg_split((char_u *)err, highlight_attr[HLF_E]);
946 return;
947 }
948}
949
950 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100951replace_line(linenr_T *line, linenr_T *end)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952{
953 char *str;
954
955 if (SvOK(GvSV(PL_defgv)))
956 {
957 str = SvPV(GvSV(PL_defgv), PL_na);
958 ml_replace(*line, (char_u *)str, 1);
959 changed_bytes(*line, 0);
960 }
961 else
962 {
963 ml_delete(*line, FALSE);
964 deleted_lines_mark(*line, 1L);
965 --(*end);
966 --(*line);
967 }
968 return OK;
969}
970
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100971static struct ref_map_S {
972 void *vim_ref;
973 SV *perl_ref;
974 struct ref_map_S *next;
975} *ref_map = NULL;
976
977 static void
978ref_map_free(void)
979{
980 struct ref_map_S *tofree;
981 struct ref_map_S *refs = ref_map;
982
983 while (refs) {
984 tofree = refs;
985 refs = refs->next;
986 vim_free(tofree);
987 }
988 ref_map = NULL;
989}
990
991 static struct ref_map_S *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100992ref_map_find_SV(SV *const sv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100993{
994 struct ref_map_S *refs = ref_map;
995 int count = 350;
996
997 while (refs) {
998 if (refs->perl_ref == sv)
999 break;
1000 refs = refs->next;
1001 count--;
1002 }
1003
1004 if (!refs && count > 0) {
1005 refs = (struct ref_map_S *)alloc(sizeof(struct ref_map_S));
1006 if (!refs)
1007 return NULL;
1008 refs->perl_ref = sv;
1009 refs->vim_ref = NULL;
1010 refs->next = ref_map;
1011 ref_map = refs;
1012 }
1013
1014 return refs;
1015}
1016
1017 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001018perl_to_vim(SV *sv, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001019{
1020 if (SvROK(sv))
1021 sv = SvRV(sv);
1022
1023 switch (SvTYPE(sv)) {
1024 case SVt_NULL:
1025 break;
1026 case SVt_NV: /* float */
1027#ifdef FEAT_FLOAT
1028 rettv->v_type = VAR_FLOAT;
1029 rettv->vval.v_float = SvNV(sv);
1030 break;
1031#endif
1032 case SVt_IV: /* integer */
1033 if (!SvROK(sv)) { /* references should be string */
1034 rettv->vval.v_number = SvIV(sv);
1035 break;
1036 }
1037 case SVt_PV: /* string */
1038 {
1039 size_t len = 0;
1040 char * str_from = SvPV(sv, len);
1041 char_u *str_to = (char_u*)alloc(sizeof(char_u) * (len + 1));
1042
1043 if (str_to) {
1044 str_to[len] = '\0';
1045
1046 while (len--) {
1047 if (str_from[len] == '\0')
1048 str_to[len] = '\n';
1049 else
1050 str_to[len] = str_from[len];
1051 }
1052 }
1053
1054 rettv->v_type = VAR_STRING;
1055 rettv->vval.v_string = str_to;
1056 break;
1057 }
1058 case SVt_PVAV: /* list */
1059 {
1060 SSize_t size;
1061 listitem_T * item;
1062 SV ** item2;
1063 list_T * list;
1064 struct ref_map_S * refs;
1065
1066 if ((refs = ref_map_find_SV(sv)) == NULL)
1067 return FAIL;
1068
1069 if (refs->vim_ref)
1070 list = (list_T *) refs->vim_ref;
1071 else
1072 {
1073 if ((list = list_alloc()) == NULL)
1074 return FAIL;
1075 refs->vim_ref = list;
1076
1077 for (size = av_len((AV*)sv); size >= 0; size--)
1078 {
1079 if ((item = listitem_alloc()) == NULL)
1080 break;
1081
1082 item->li_tv.v_type = VAR_NUMBER;
1083 item->li_tv.v_lock = 0;
1084 item->li_tv.vval.v_number = 0;
1085 list_insert(list, item, list->lv_first);
1086
1087 item2 = av_fetch((AV *)sv, size, 0);
1088
1089 if (item2 == NULL || *item2 == NULL ||
Bram Moolenaard99df422016-01-29 23:20:40 +01001090 perl_to_vim(*item2, &item->li_tv) == FAIL)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001091 break;
1092 }
1093 }
1094
1095 list->lv_refcount++;
1096 rettv->v_type = VAR_LIST;
1097 rettv->vval.v_list = list;
1098 break;
1099 }
1100 case SVt_PVHV: /* dictionary */
1101 {
1102 HE * entry;
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001103 I32 key_len;
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001104 char * key;
1105 dictitem_T * item;
1106 SV * item2;
1107 dict_T * dict;
1108 struct ref_map_S * refs;
1109
1110 if ((refs = ref_map_find_SV(sv)) == NULL)
1111 return FAIL;
1112
1113 if (refs->vim_ref)
1114 dict = (dict_T *) refs->vim_ref;
1115 else
1116 {
1117
1118 if ((dict = dict_alloc()) == NULL)
1119 return FAIL;
1120 refs->vim_ref = dict;
1121
1122 hv_iterinit((HV *)sv);
1123
1124 for (entry = hv_iternext((HV *)sv); entry; entry = hv_iternext((HV *)sv))
1125 {
1126 key_len = 0;
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001127 key = hv_iterkey(entry, &key_len);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001128
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001129 if (!key || !key_len || strlen(key) < (size_t)key_len) {
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001130 EMSG2("Malformed key Dictionary '%s'", key && *key ? key : "(empty)");
1131 break;
1132 }
1133
1134 if ((item = dictitem_alloc((char_u *)key)) == NULL)
1135 break;
1136
1137 item->di_tv.v_type = VAR_NUMBER;
1138 item->di_tv.v_lock = 0;
1139 item->di_tv.vval.v_number = 0;
1140
1141 if (dict_add(dict, item) == FAIL) {
1142 dictitem_free(item);
1143 break;
1144 }
1145 item2 = hv_iterval((HV *)sv, entry);
1146 if (item2 == NULL || perl_to_vim(item2, &item->di_tv) == FAIL)
1147 break;
1148 }
1149 }
1150
1151 dict->dv_refcount++;
1152 rettv->v_type = VAR_DICT;
1153 rettv->vval.v_dict = dict;
1154 break;
1155 }
1156 default: /* not convertible */
1157 {
1158 char *val = SvPV_nolen(sv);
1159 rettv->v_type = VAR_STRING;
1160 rettv->vval.v_string = val ? vim_strsave((char_u *)val) : NULL;
1161 break;
1162 }
1163 }
1164 return OK;
1165}
1166
1167/*
1168 * "perleval()"
1169 */
1170 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001171do_perleval(char_u *str, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001172{
1173 char *err = NULL;
1174 STRLEN err_len = 0;
1175 SV *sv = NULL;
1176#ifdef HAVE_SANDBOX
1177 SV *safe;
1178#endif
1179
1180 if (perl_interp == NULL)
1181 {
1182#ifdef DYNAMIC_PERL
1183 if (!perl_enabled(TRUE))
1184 {
1185 EMSG(_(e_noperl));
1186 return;
1187 }
1188#endif
1189 perl_init();
1190 }
1191
1192 {
1193 dSP;
1194 ENTER;
1195 SAVETMPS;
1196
1197#ifdef HAVE_SANDBOX
1198 if (sandbox)
1199 {
1200 safe = get_sv("VIM::safe", FALSE);
1201# ifndef MAKE_TEST /* avoid a warning for unreachable code */
1202 if (safe == NULL || !SvTRUE(safe))
1203 EMSG(_("E299: Perl evaluation forbidden in sandbox without the Safe module"));
1204 else
1205# endif
1206 {
1207 sv = newSVpv((char *)str, 0);
1208 PUSHMARK(SP);
1209 XPUSHs(safe);
1210 XPUSHs(sv);
1211 PUTBACK;
1212 call_method("reval", G_SCALAR);
1213 SPAGAIN;
1214 SvREFCNT_dec(sv);
1215 sv = POPs;
1216 }
1217 }
1218 else
1219#endif /* HAVE_SANDBOX */
1220 sv = eval_pv((char *)str, 0);
1221
1222 if (sv) {
1223 perl_to_vim(sv, rettv);
1224 ref_map_free();
1225 err = CHECK_EVAL_ERR(err_len);
1226 }
1227 PUTBACK;
1228 FREETMPS;
1229 LEAVE;
1230 }
1231 if (err_len)
1232 msg_split((char_u *)err, highlight_attr[HLF_E]);
1233}
1234
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235/*
1236 * ":perldo".
1237 */
1238 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001239ex_perldo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240{
1241 STRLEN length;
1242 SV *sv;
1243 char *str;
1244 linenr_T i;
1245
1246 if (bufempty())
1247 return;
1248
1249 if (perl_interp == NULL)
1250 {
1251#ifdef DYNAMIC_PERL
1252 if (!perl_enabled(TRUE))
1253 {
1254 EMSG(_(e_noperl));
1255 return;
1256 }
1257#endif
1258 perl_init();
1259 }
1260 {
1261 dSP;
1262 length = strlen((char *)eap->arg);
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001263 sv = newSV(length + sizeof("sub VIM::perldo {") - 1 + 1);
1264 sv_setpvn(sv, "sub VIM::perldo {", sizeof("sub VIM::perldo {") - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 sv_catpvn(sv, (char *)eap->arg, length);
1266 sv_catpvn(sv, "}", 1);
1267 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
1268 SvREFCNT_dec(sv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001269 str = CHECK_EVAL_ERR(length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 if (length)
1271 goto err;
1272
1273 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
1274 return;
1275
1276 ENTER;
1277 SAVETMPS;
1278 for (i = eap->line1; i <= eap->line2; i++)
1279 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001280 sv_setpv(GvSV(PL_defgv), (char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 PUSHMARK(sp);
1282 perl_call_pv("VIM::perldo", G_SCALAR | G_EVAL);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001283 str = CHECK_EVAL_ERR(length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 if (length)
1285 break;
1286 SPAGAIN;
1287 if (SvTRUEx(POPs))
1288 {
1289 if (replace_line(&i, &eap->line2) != OK)
1290 {
1291 PUTBACK;
1292 break;
1293 }
1294 }
1295 PUTBACK;
1296 }
1297 FREETMPS;
1298 LEAVE;
1299 check_cursor();
1300 update_screen(NOT_VALID);
1301 if (!length)
1302 return;
1303
1304err:
1305 msg_split((char_u *)str, highlight_attr[HLF_E]);
1306 return;
1307 }
1308}
1309
Bram Moolenaar01dd60c2008-07-24 14:24:48 +00001310#ifndef FEAT_WINDOWS
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001311 int
1312win_valid(win_T *w)
1313{
1314 return TRUE;
1315}
1316 int
1317win_count(void)
1318{
1319 return 1;
1320}
1321 win_T *
1322win_find_nr(int n)
1323{
1324 return curwin;
1325}
Bram Moolenaar01dd60c2008-07-24 14:24:48 +00001326#endif
1327
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328XS(boot_VIM);
1329
1330 static void
1331xs_init(pTHX)
1332{
1333 char *file = __FILE__;
1334
1335 /* DynaLoader is a special case */
1336 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1337 newXS("VIM::bootstrap", boot_VIM, file);
1338}
1339
1340typedef win_T * VIWIN;
1341typedef buf_T * VIBUF;
1342
1343MODULE = VIM PACKAGE = VIM
1344
1345void
1346Msg(text, hl=NULL)
1347 char *text;
1348 char *hl;
1349
1350 PREINIT:
1351 int attr;
1352 int id;
1353
1354 PPCODE:
1355 if (text != NULL)
1356 {
1357 attr = 0;
1358 if (hl != NULL)
1359 {
1360 id = syn_name2id((char_u *)hl);
1361 if (id != 0)
1362 attr = syn_id2attr(id);
1363 }
1364 msg_split((char_u *)text, attr);
1365 }
1366
1367void
1368SetOption(line)
1369 char *line;
1370
1371 PPCODE:
1372 if (line != NULL)
1373 do_set((char_u *)line, 0);
1374 update_screen(NOT_VALID);
1375
1376void
1377DoCommand(line)
1378 char *line;
1379
1380 PPCODE:
1381 if (line != NULL)
1382 do_cmdline_cmd((char_u *)line);
1383
1384void
1385Eval(str)
1386 char *str;
1387
1388 PREINIT:
1389 char_u *value;
1390 PPCODE:
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001391 value = eval_to_string((char_u *)str, (char_u **)0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 if (value == NULL)
1393 {
1394 XPUSHs(sv_2mortal(newSViv(0)));
1395 XPUSHs(sv_2mortal(newSVpv("", 0)));
1396 }
1397 else
1398 {
1399 XPUSHs(sv_2mortal(newSViv(1)));
1400 XPUSHs(sv_2mortal(newSVpv((char *)value, 0)));
1401 vim_free(value);
1402 }
1403
1404void
1405Buffers(...)
1406
1407 PREINIT:
1408 buf_T *vimbuf;
1409 int i, b;
1410
1411 PPCODE:
1412 if (items == 0)
1413 {
1414 if (GIMME == G_SCALAR)
1415 {
1416 i = 0;
1417 for (vimbuf = firstbuf; vimbuf; vimbuf = vimbuf->b_next)
1418 ++i;
1419
1420 XPUSHs(sv_2mortal(newSViv(i)));
1421 }
1422 else
1423 {
1424 for (vimbuf = firstbuf; vimbuf; vimbuf = vimbuf->b_next)
1425 XPUSHs(newBUFrv(newSV(0), vimbuf));
1426 }
1427 }
1428 else
1429 {
1430 for (i = 0; i < items; i++)
1431 {
1432 SV *sv = ST(i);
1433 if (SvIOK(sv))
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001434 b = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 else
1436 {
1437 char_u *pat;
1438 STRLEN len;
1439
1440 pat = (char_u *)SvPV(sv, len);
1441 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001442 b = buflist_findpat(pat, pat+len, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 --emsg_off;
1444 }
1445
1446 if (b >= 0)
1447 {
1448 vimbuf = buflist_findnr(b);
1449 if (vimbuf)
1450 XPUSHs(newBUFrv(newSV(0), vimbuf));
1451 }
1452 }
1453 }
1454
1455void
1456Windows(...)
1457
1458 PREINIT:
1459 win_T *vimwin;
1460 int i, w;
1461
1462 PPCODE:
1463 if (items == 0)
1464 {
1465 if (GIMME == G_SCALAR)
1466 XPUSHs(sv_2mortal(newSViv(win_count())));
1467 else
1468 {
1469 for (vimwin = firstwin; vimwin != NULL; vimwin = W_NEXT(vimwin))
1470 XPUSHs(newWINrv(newSV(0), vimwin));
1471 }
1472 }
1473 else
1474 {
1475 for (i = 0; i < items; i++)
1476 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001477 w = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 vimwin = win_find_nr(w);
1479 if (vimwin)
1480 XPUSHs(newWINrv(newSV(0), vimwin));
1481 }
1482 }
1483
1484MODULE = VIM PACKAGE = VIWIN
1485
1486void
1487DESTROY(win)
1488 VIWIN win
1489
1490 CODE:
1491 if (win_valid(win))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001492 win->w_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493
1494SV *
1495Buffer(win)
1496 VIWIN win
1497
1498 CODE:
1499 if (!win_valid(win))
1500 win = curwin;
1501 RETVAL = newBUFrv(newSV(0), win->w_buffer);
1502 OUTPUT:
1503 RETVAL
1504
1505void
1506SetHeight(win, height)
1507 VIWIN win
1508 int height;
1509
1510 PREINIT:
1511 win_T *savewin;
1512
1513 PPCODE:
1514 if (!win_valid(win))
1515 win = curwin;
1516 savewin = curwin;
1517 curwin = win;
1518 win_setheight(height);
1519 curwin = savewin;
1520
1521void
Bram Moolenaar864733a2016-04-02 14:18:01 +02001522Cursor(win, ...)
1523 VIWIN win
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524
1525 PPCODE:
Bram Moolenaara1711622011-07-27 14:15:46 +02001526 if (items == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 {
1528 EXTEND(sp, 2);
1529 if (!win_valid(win))
1530 win = curwin;
1531 PUSHs(sv_2mortal(newSViv(win->w_cursor.lnum)));
1532 PUSHs(sv_2mortal(newSViv(win->w_cursor.col)));
1533 }
Bram Moolenaara1711622011-07-27 14:15:46 +02001534 else if (items == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 {
1536 int lnum, col;
1537
1538 if (!win_valid(win))
1539 win = curwin;
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001540 lnum = (int) SvIV(ST(1));
1541 col = (int) SvIV(ST(2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 win->w_cursor.lnum = lnum;
1543 win->w_cursor.col = col;
1544 check_cursor(); /* put cursor on an existing line */
1545 update_screen(NOT_VALID);
1546 }
1547
1548MODULE = VIM PACKAGE = VIBUF
1549
1550void
1551DESTROY(vimbuf)
1552 VIBUF vimbuf;
1553
1554 CODE:
1555 if (buf_valid(vimbuf))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001556 vimbuf->b_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557
1558void
1559Name(vimbuf)
1560 VIBUF vimbuf;
1561
1562 PPCODE:
1563 if (!buf_valid(vimbuf))
1564 vimbuf = curbuf;
1565 /* No file name returns an empty string */
1566 if (vimbuf->b_fname == NULL)
1567 XPUSHs(sv_2mortal(newSVpv("", 0)));
1568 else
1569 XPUSHs(sv_2mortal(newSVpv((char *)vimbuf->b_fname, 0)));
1570
1571void
1572Number(vimbuf)
1573 VIBUF vimbuf;
1574
1575 PPCODE:
1576 if (!buf_valid(vimbuf))
1577 vimbuf = curbuf;
1578 XPUSHs(sv_2mortal(newSViv(vimbuf->b_fnum)));
1579
1580void
1581Count(vimbuf)
1582 VIBUF vimbuf;
1583
1584 PPCODE:
1585 if (!buf_valid(vimbuf))
1586 vimbuf = curbuf;
1587 XPUSHs(sv_2mortal(newSViv(vimbuf->b_ml.ml_line_count)));
1588
1589void
1590Get(vimbuf, ...)
1591 VIBUF vimbuf;
1592
1593 PREINIT:
1594 char_u *line;
1595 int i;
1596 long lnum;
1597 PPCODE:
1598 if (buf_valid(vimbuf))
1599 {
1600 for (i = 1; i < items; i++)
1601 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001602 lnum = (long) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1604 {
1605 line = ml_get_buf(vimbuf, lnum, FALSE);
1606 XPUSHs(sv_2mortal(newSVpv((char *)line, 0)));
1607 }
1608 }
1609 }
1610
1611void
1612Set(vimbuf, ...)
1613 VIBUF vimbuf;
1614
1615 PREINIT:
1616 int i;
1617 long lnum;
1618 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 PPCODE:
1620 if (buf_valid(vimbuf))
1621 {
1622 if (items < 3)
1623 croak("Usage: VIBUF::Set(vimbuf, lnum, @lines)");
1624
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001625 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 for(i = 2; i < items; i++, lnum++)
1627 {
1628 line = SvPV(ST(i),PL_na);
1629 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1630 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001631 aco_save_T aco;
1632
1633 /* set curwin/curbuf for "vimbuf" and save some things */
1634 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001635
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 if (u_savesub(lnum) == OK)
1637 {
1638 ml_replace(lnum, (char_u *)line, TRUE);
1639 changed_bytes(lnum, 0);
1640 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001641
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001642 /* restore curwin/curbuf and a few other things */
1643 aucmd_restbuf(&aco);
1644 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 }
1646 }
1647 }
1648
1649void
1650Delete(vimbuf, ...)
1651 VIBUF vimbuf;
1652
1653 PREINIT:
1654 long i, lnum = 0, count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 PPCODE:
1656 if (buf_valid(vimbuf))
1657 {
1658 if (items == 2)
1659 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001660 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 count = 1;
1662 }
1663 else if (items == 3)
1664 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001665 lnum = (long) SvIV(ST(1));
1666 count = (long) 1 + SvIV(ST(2)) - lnum;
Bram Moolenaara1711622011-07-27 14:15:46 +02001667 if (count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 count = 1;
Bram Moolenaara1711622011-07-27 14:15:46 +02001669 if (count < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 {
1671 lnum -= count;
1672 count = -count;
1673 }
1674 }
1675 if (items >= 2)
1676 {
1677 for (i = 0; i < count; i++)
1678 {
1679 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1680 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001681 aco_save_T aco;
1682
1683 /* set curwin/curbuf for "vimbuf" and save some things */
1684 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001685
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 if (u_savedel(lnum, 1) == OK)
1687 {
1688 ml_delete(lnum, 0);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00001689 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 deleted_lines_mark(lnum, 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001692
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001693 /* restore curwin/curbuf and a few other things */
1694 aucmd_restbuf(&aco);
1695 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001696
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 update_curbuf(VALID);
1698 }
1699 }
1700 }
1701 }
1702
1703void
1704Append(vimbuf, ...)
1705 VIBUF vimbuf;
1706
1707 PREINIT:
1708 int i;
1709 long lnum;
1710 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 PPCODE:
1712 if (buf_valid(vimbuf))
1713 {
1714 if (items < 3)
1715 croak("Usage: VIBUF::Append(vimbuf, lnum, @lines)");
1716
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001717 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 for (i = 2; i < items; i++, lnum++)
1719 {
1720 line = SvPV(ST(i),PL_na);
1721 if (lnum >= 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1722 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001723 aco_save_T aco;
1724
1725 /* set curwin/curbuf for "vimbuf" and save some things */
1726 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001727
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 if (u_inssub(lnum + 1) == OK)
1729 {
1730 ml_append(lnum, (char_u *)line, (colnr_T)0, FALSE);
1731 appended_lines_mark(lnum, 1L);
1732 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001733
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001734 /* restore curwin/curbuf and a few other things */
1735 aucmd_restbuf(&aco);
1736 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001737
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 update_curbuf(VALID);
1739 }
1740 }
1741 }
1742
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001743#ifdef __GNUC__
1744# pragma GCC diagnostic pop
1745#endif