blob: 590ef86ac07aa6dbce0e2709fdcb66b321ba56ab [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 Moolenaaraee1f4a2013-08-02 16:10:32 +020052#include <EXTERN.h>
53#include <perl.h>
54#include <XSUB.h>
55
Bram Moolenaar071d4272004-06-13 20:20:40 +000056
57/*
58 * Work around clashes between Perl and Vim namespace. proto.h doesn't
59 * include if_perl.pro and perlsfio.pro when IN_PERL_FILE is defined, because
60 * we need the CV typedef. proto.h can't be moved to after including
61 * if_perl.h, because we get all sorts of name clashes then.
62 */
63#ifndef PROTO
64#ifndef __MINGW32__
65# include "proto/if_perl.pro"
66# include "proto/if_perlsfio.pro"
67#endif
68#endif
69
70/* Perl compatibility stuff. This should ensure compatibility with older
71 * versions of Perl.
72 */
73
74#ifndef PERL_VERSION
75# include <patchlevel.h>
76# define PERL_REVISION 5
77# define PERL_VERSION PATCHLEVEL
78# define PERL_SUBVERSION SUBVERSION
79#endif
80
Bram Moolenaar700d1d72007-09-13 13:20:16 +000081/*
82 * Quoting Jan Dubois of Active State:
83 * ActivePerl build 822 still identifies itself as 5.8.8 but already
84 * contains many of the changes from the upcoming Perl 5.8.9 release.
85 *
86 * The changes include addition of two symbols (Perl_sv_2iv_flags,
87 * Perl_newXS_flags) not present in earlier releases.
88 *
Bram Moolenaar3b9b13e2007-09-15 12:49:35 +000089 * Jan Dubois suggested the following guarding scheme.
90 *
91 * Active State defined ACTIVEPERL_VERSION as a string in versions before
92 * 5.8.8; and so the comparison to 822 below needs to be guarded.
Bram Moolenaar700d1d72007-09-13 13:20:16 +000093 */
Bram Moolenaar3b9b13e2007-09-15 12:49:35 +000094#if (PERL_REVISION == 5) && (PERL_VERSION == 8) && (PERL_SUBVERSION >= 8)
95# if (ACTIVEPERL_VERSION >= 822) || (PERL_SUBVERSION >= 9)
96# define PERL589_OR_LATER
97# endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +000098#endif
99#if (PERL_REVISION == 5) && (PERL_VERSION >= 9)
100# define PERL589_OR_LATER
101#endif
102
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100103#if (PERL_REVISION == 5) && ((PERL_VERSION > 10) || \
104 (PERL_VERSION == 10) && (PERL_SUBVERSION >= 1))
105# define PERL5101_OR_LATER
106#endif
107
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#ifndef pTHX
109# define pTHX void
110# define pTHX_
111#endif
112
113#ifndef EXTERN_C
114# define EXTERN_C
115#endif
116
Bram Moolenaarc271c482012-08-08 13:17:31 +0200117#if (PERL_REVISION == 5) && (PERL_VERSION >= 14) && defined(_MSC_VER)
118/* Using PL_errgv to get the error message after perl_eval_sv() causes a crash
119 * with MSVC and Perl version 5.14. */
120# define AVOID_PL_ERRGV
121#endif
122
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123/* Compatibility hacks over */
124
125static PerlInterpreter *perl_interp = NULL;
126static void xs_init __ARGS((pTHX));
127static void VIM_init __ARGS((void));
128EXTERN_C void boot_DynaLoader __ARGS((pTHX_ CV*));
129
130/*
Bram Moolenaare06c1882010-07-21 22:05:20 +0200131 * For dynamic linked perl.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132 */
133#if defined(DYNAMIC_PERL) || defined(PROTO)
Bram Moolenaare06c1882010-07-21 22:05:20 +0200134
135#ifndef DYNAMIC_PERL /* just generating prototypes */
Bram Moolenaar766fb0d2010-07-22 11:34:16 +0200136#ifdef WIN3264
Bram Moolenaare06c1882010-07-21 22:05:20 +0200137typedef int HANDLE;
138#endif
139typedef int XSINIT_t;
140typedef int XSUBADDR_t;
Bram Moolenaard8619992014-03-12 17:08:05 +0100141#endif
142#ifndef USE_ITHREADS
Bram Moolenaare06c1882010-07-21 22:05:20 +0200143typedef int perl_key;
144#endif
145
Bram Moolenaar766fb0d2010-07-22 11:34:16 +0200146#ifndef WIN3264
Bram Moolenaare06c1882010-07-21 22:05:20 +0200147#include <dlfcn.h>
148#define HANDLE void*
149#define PERL_PROC void*
150#define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
151#define symbol_from_dll dlsym
152#define close_dll dlclose
153#else
154#define PERL_PROC FARPROC
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200155#define load_dll vimLoadLib
Bram Moolenaare06c1882010-07-21 22:05:20 +0200156#define symbol_from_dll GetProcAddress
157#define close_dll FreeLibrary
158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159/*
160 * Wrapper defines
161 */
162# define perl_alloc dll_perl_alloc
163# define perl_construct dll_perl_construct
164# define perl_parse dll_perl_parse
165# define perl_run dll_perl_run
166# define perl_destruct dll_perl_destruct
167# define perl_free dll_perl_free
168# define Perl_get_context dll_Perl_get_context
169# define Perl_croak dll_Perl_croak
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100170# ifdef PERL5101_OR_LATER
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100171# define Perl_croak_xs_usage dll_Perl_croak_xs_usage
172# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173# ifndef PROTO
174# define Perl_croak_nocontext dll_Perl_croak_nocontext
175# define Perl_call_argv dll_Perl_call_argv
176# define Perl_call_pv dll_Perl_call_pv
177# define Perl_eval_sv dll_Perl_eval_sv
178# define Perl_get_sv dll_Perl_get_sv
179# define Perl_eval_pv dll_Perl_eval_pv
180# define Perl_call_method dll_Perl_call_method
181# endif
182# define Perl_dowantarray dll_Perl_dowantarray
183# define Perl_free_tmps dll_Perl_free_tmps
184# define Perl_gv_stashpv dll_Perl_gv_stashpv
185# define Perl_markstack_grow dll_Perl_markstack_grow
186# define Perl_mg_find dll_Perl_mg_find
187# define Perl_newXS dll_Perl_newXS
188# define Perl_newSV dll_Perl_newSV
189# define Perl_newSViv dll_Perl_newSViv
190# define Perl_newSVpv dll_Perl_newSVpv
191# define Perl_pop_scope dll_Perl_pop_scope
192# define Perl_push_scope dll_Perl_push_scope
193# define Perl_save_int dll_Perl_save_int
194# define Perl_stack_grow dll_Perl_stack_grow
195# define Perl_set_context dll_Perl_set_context
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200196# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
197# define Perl_sv_2bool_flags dll_Perl_sv_2bool_flags
Bram Moolenaar01c10522012-09-21 12:50:51 +0200198# define Perl_xs_apiversion_bootcheck dll_Perl_xs_apiversion_bootcheck
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200199# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200# define Perl_sv_2bool dll_Perl_sv_2bool
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200201# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202# define Perl_sv_2iv dll_Perl_sv_2iv
203# define Perl_sv_2mortal dll_Perl_sv_2mortal
204# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
205# define Perl_sv_2pv_flags dll_Perl_sv_2pv_flags
206# define Perl_sv_2pv_nolen dll_Perl_sv_2pv_nolen
207# else
208# define Perl_sv_2pv dll_Perl_sv_2pv
209# endif
210# define Perl_sv_bless dll_Perl_sv_bless
211# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
212# define Perl_sv_catpvn_flags dll_Perl_sv_catpvn_flags
213# else
214# define Perl_sv_catpvn dll_Perl_sv_catpvn
215# endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000216#ifdef PERL589_OR_LATER
217# define Perl_sv_2iv_flags dll_Perl_sv_2iv_flags
218# define Perl_newXS_flags dll_Perl_newXS_flags
219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220# define Perl_sv_free dll_Perl_sv_free
Bram Moolenaarccf22172008-09-01 15:56:45 +0000221# if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
222# define Perl_sv_free2 dll_Perl_sv_free2
223# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224# define Perl_sv_isa dll_Perl_sv_isa
225# define Perl_sv_magic dll_Perl_sv_magic
226# define Perl_sv_setiv dll_Perl_sv_setiv
227# define Perl_sv_setpv dll_Perl_sv_setpv
228# define Perl_sv_setpvn dll_Perl_sv_setpvn
229# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
230# define Perl_sv_setsv_flags dll_Perl_sv_setsv_flags
231# else
232# define Perl_sv_setsv dll_Perl_sv_setsv
233# endif
234# define Perl_sv_upgrade dll_Perl_sv_upgrade
235# define Perl_Tstack_sp_ptr dll_Perl_Tstack_sp_ptr
236# define Perl_Top_ptr dll_Perl_Top_ptr
237# define Perl_Tstack_base_ptr dll_Perl_Tstack_base_ptr
238# define Perl_Tstack_max_ptr dll_Perl_Tstack_max_ptr
239# define Perl_Ttmps_ix_ptr dll_Perl_Ttmps_ix_ptr
240# define Perl_Ttmps_floor_ptr dll_Perl_Ttmps_floor_ptr
241# define Perl_Tmarkstack_ptr_ptr dll_Perl_Tmarkstack_ptr_ptr
242# define Perl_Tmarkstack_max_ptr dll_Perl_Tmarkstack_max_ptr
243# define Perl_TSv_ptr dll_Perl_TSv_ptr
244# define Perl_TXpv_ptr dll_Perl_TXpv_ptr
245# define Perl_Tna_ptr dll_Perl_Tna_ptr
246# define Perl_Idefgv_ptr dll_Perl_Idefgv_ptr
247# define Perl_Ierrgv_ptr dll_Perl_Ierrgv_ptr
248# define Perl_Isv_yes_ptr dll_Perl_Isv_yes_ptr
249# define boot_DynaLoader dll_boot_DynaLoader
Bram Moolenaare06c1882010-07-21 22:05:20 +0200250# define Perl_Gthr_key_ptr dll_Perl_Gthr_key_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000252# define Perl_sys_init dll_Perl_sys_init
Bram Moolenaarc236c162008-07-13 17:41:49 +0000253# define Perl_sys_term dll_Perl_sys_term
254# define Perl_ISv_ptr dll_Perl_ISv_ptr
255# define Perl_Istack_max_ptr dll_Perl_Istack_max_ptr
256# define Perl_Istack_base_ptr dll_Perl_Istack_base_ptr
257# define Perl_Itmps_ix_ptr dll_Perl_Itmps_ix_ptr
258# define Perl_Itmps_floor_ptr dll_Perl_Itmps_floor_ptr
259# define Perl_IXpv_ptr dll_Perl_IXpv_ptr
260# define Perl_Ina_ptr dll_Perl_Ina_ptr
261# define Perl_Imarkstack_ptr_ptr dll_Perl_Imarkstack_ptr_ptr
262# define Perl_Imarkstack_max_ptr dll_Perl_Imarkstack_max_ptr
263# define Perl_Istack_sp_ptr dll_Perl_Istack_sp_ptr
264# define Perl_Iop_ptr dll_Perl_Iop_ptr
265# define Perl_call_list dll_Perl_call_list
266# define Perl_Iscopestack_ix_ptr dll_Perl_Iscopestack_ix_ptr
267# define Perl_Iunitcheckav_ptr dll_Perl_Iunitcheckav_ptr
Bram Moolenaar01c10522012-09-21 12:50:51 +0200268# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100269# ifdef USE_ITHREADS
270# define PL_thr_key *dll_PL_thr_key
271# endif
Bram Moolenaar01c10522012-09-21 12:50:51 +0200272# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000273
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274/*
275 * Declare HANDLE for perl.dll and function pointers.
276 */
277static HANDLE hPerlLib = NULL;
278
279static PerlInterpreter* (*perl_alloc)();
280static void (*perl_construct)(PerlInterpreter*);
281static void (*perl_destruct)(PerlInterpreter*);
282static void (*perl_free)(PerlInterpreter*);
283static int (*perl_run)(PerlInterpreter*);
284static int (*perl_parse)(PerlInterpreter*, XSINIT_t, int, char**, char**);
285static void* (*Perl_get_context)(void);
Bram Moolenaara7ecc562006-08-16 16:17:39 +0000286static void (*Perl_croak)(pTHX_ const char*, ...);
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100287#ifdef PERL5101_OR_LATER
Bram Moolenaar6b107212013-12-11 15:06:40 +0100288/* Perl-5.18 has a different Perl_croak_xs_usage signature. */
289# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
290static void (*Perl_croak_xs_usage)(const CV *const, const char *const params);
291# else
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100292static void (*Perl_croak_xs_usage)(pTHX_ const CV *const, const char *const params);
Bram Moolenaar6b107212013-12-11 15:06:40 +0100293# endif
Bram Moolenaar9be6e212013-06-15 16:47:35 +0200294#endif
Bram Moolenaara7ecc562006-08-16 16:17:39 +0000295static void (*Perl_croak_nocontext)(const char*, ...);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296static I32 (*Perl_dowantarray)(pTHX);
297static void (*Perl_free_tmps)(pTHX);
298static HV* (*Perl_gv_stashpv)(pTHX_ const char*, I32);
299static void (*Perl_markstack_grow)(pTHX);
300static MAGIC* (*Perl_mg_find)(pTHX_ SV*, int);
301static CV* (*Perl_newXS)(pTHX_ char*, XSUBADDR_t, char*);
302static SV* (*Perl_newSV)(pTHX_ STRLEN);
303static SV* (*Perl_newSViv)(pTHX_ IV);
304static SV* (*Perl_newSVpv)(pTHX_ const char*, STRLEN);
305static I32 (*Perl_call_argv)(pTHX_ const char*, I32, char**);
306static I32 (*Perl_call_pv)(pTHX_ const char*, I32);
307static I32 (*Perl_eval_sv)(pTHX_ SV*, I32);
308static SV* (*Perl_get_sv)(pTHX_ const char*, I32);
309static SV* (*Perl_eval_pv)(pTHX_ const char*, I32);
310static SV* (*Perl_call_method)(pTHX_ const char*, I32);
311static void (*Perl_pop_scope)(pTHX);
312static void (*Perl_push_scope)(pTHX);
313static void (*Perl_save_int)(pTHX_ int*);
314static SV** (*Perl_stack_grow)(pTHX_ SV**, SV**p, int);
315static SV** (*Perl_set_context)(void*);
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200316#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
317static bool (*Perl_sv_2bool_flags)(pTHX_ SV*, I32);
318static void (*Perl_xs_apiversion_bootcheck)(pTHX_ SV *module, const char *api_p, STRLEN api_len);
319#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320static bool (*Perl_sv_2bool)(pTHX_ SV*);
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322static IV (*Perl_sv_2iv)(pTHX_ SV*);
323static SV* (*Perl_sv_2mortal)(pTHX_ SV*);
324#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
325static char* (*Perl_sv_2pv_flags)(pTHX_ SV*, STRLEN*, I32);
326static char* (*Perl_sv_2pv_nolen)(pTHX_ SV*);
327#else
328static char* (*Perl_sv_2pv)(pTHX_ SV*, STRLEN*);
329#endif
330static SV* (*Perl_sv_bless)(pTHX_ SV*, HV*);
331#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
332static void (*Perl_sv_catpvn_flags)(pTHX_ SV* , const char*, STRLEN, I32);
333#else
334static void (*Perl_sv_catpvn)(pTHX_ SV*, const char*, STRLEN);
335#endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000336#ifdef PERL589_OR_LATER
337static IV (*Perl_sv_2iv_flags)(pTHX_ SV* sv, I32 flags);
338static CV * (*Perl_newXS_flags)(pTHX_ const char *name, XSUBADDR_t subaddr, const char *const filename, const char *const proto, U32 flags);
339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340static void (*Perl_sv_free)(pTHX_ SV*);
341static int (*Perl_sv_isa)(pTHX_ SV*, const char*);
342static void (*Perl_sv_magic)(pTHX_ SV*, SV*, int, const char*, I32);
343static void (*Perl_sv_setiv)(pTHX_ SV*, IV);
344static void (*Perl_sv_setpv)(pTHX_ SV*, const char*);
345static void (*Perl_sv_setpvn)(pTHX_ SV*, const char*, STRLEN);
346#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
347static void (*Perl_sv_setsv_flags)(pTHX_ SV*, SV*, I32);
348#else
349static void (*Perl_sv_setsv)(pTHX_ SV*, SV*);
350#endif
351static bool (*Perl_sv_upgrade)(pTHX_ SV*, U32);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200352#if (PERL_REVISION == 5) && (PERL_VERSION < 10)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353static SV*** (*Perl_Tstack_sp_ptr)(register PerlInterpreter*);
354static OP** (*Perl_Top_ptr)(register PerlInterpreter*);
355static SV*** (*Perl_Tstack_base_ptr)(register PerlInterpreter*);
356static SV*** (*Perl_Tstack_max_ptr)(register PerlInterpreter*);
357static I32* (*Perl_Ttmps_ix_ptr)(register PerlInterpreter*);
358static I32* (*Perl_Ttmps_floor_ptr)(register PerlInterpreter*);
359static I32** (*Perl_Tmarkstack_ptr_ptr)(register PerlInterpreter*);
360static I32** (*Perl_Tmarkstack_max_ptr)(register PerlInterpreter*);
361static SV** (*Perl_TSv_ptr)(register PerlInterpreter*);
362static XPV** (*Perl_TXpv_ptr)(register PerlInterpreter*);
363static STRLEN* (*Perl_Tna_ptr)(register PerlInterpreter*);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200364#else
Bram Moolenaar6b107212013-12-11 15:06:40 +0100365/* Perl-5.18 has a different Perl_sv_free2 signature. */
366# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
367static void (*Perl_sv_free2)(pTHX_ SV*, const U32);
368# else
Bram Moolenaarccf22172008-09-01 15:56:45 +0000369static void (*Perl_sv_free2)(pTHX_ SV*);
Bram Moolenaar6b107212013-12-11 15:06:40 +0100370# endif
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000371static void (*Perl_sys_init)(int* argc, char*** argv);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000372static void (*Perl_sys_term)(void);
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200373static void (*Perl_call_list)(pTHX_ I32, AV*);
374# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
375# else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000376static SV** (*Perl_ISv_ptr)(register PerlInterpreter*);
377static SV*** (*Perl_Istack_max_ptr)(register PerlInterpreter*);
378static SV*** (*Perl_Istack_base_ptr)(register PerlInterpreter*);
379static XPV** (*Perl_IXpv_ptr)(register PerlInterpreter*);
380static I32* (*Perl_Itmps_ix_ptr)(register PerlInterpreter*);
381static I32* (*Perl_Itmps_floor_ptr)(register PerlInterpreter*);
382static STRLEN* (*Perl_Ina_ptr)(register PerlInterpreter*);
383static I32** (*Perl_Imarkstack_ptr_ptr)(register PerlInterpreter*);
384static I32** (*Perl_Imarkstack_max_ptr)(register PerlInterpreter*);
385static SV*** (*Perl_Istack_sp_ptr)(register PerlInterpreter*);
386static OP** (*Perl_Iop_ptr)(register PerlInterpreter*);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000387static I32* (*Perl_Iscopestack_ix_ptr)(register PerlInterpreter*);
388static AV** (*Perl_Iunitcheckav_ptr)(register PerlInterpreter*);
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200389# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200392#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100393# ifdef USE_ITHREADS
Bram Moolenaar01c10522012-09-21 12:50:51 +0200394static perl_key* dll_PL_thr_key;
Bram Moolenaard8619992014-03-12 17:08:05 +0100395# endif
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200396#else
Bram Moolenaare06c1882010-07-21 22:05:20 +0200397static GV** (*Perl_Idefgv_ptr)(register PerlInterpreter*);
398static GV** (*Perl_Ierrgv_ptr)(register PerlInterpreter*);
399static SV* (*Perl_Isv_yes_ptr)(register PerlInterpreter*);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200400static perl_key* (*Perl_Gthr_key_ptr)_((pTHX));
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200401#endif
402static void (*boot_DynaLoader)_((pTHX_ CV*));
Bram Moolenaare06c1882010-07-21 22:05:20 +0200403
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404/*
405 * Table of name to function pointer of perl.
406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407static struct {
408 char* name;
409 PERL_PROC* ptr;
410} perl_funcname_table[] = {
411 {"perl_alloc", (PERL_PROC*)&perl_alloc},
412 {"perl_construct", (PERL_PROC*)&perl_construct},
413 {"perl_destruct", (PERL_PROC*)&perl_destruct},
414 {"perl_free", (PERL_PROC*)&perl_free},
415 {"perl_run", (PERL_PROC*)&perl_run},
416 {"perl_parse", (PERL_PROC*)&perl_parse},
417 {"Perl_get_context", (PERL_PROC*)&Perl_get_context},
418 {"Perl_croak", (PERL_PROC*)&Perl_croak},
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100419#ifdef PERL5101_OR_LATER
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100420 {"Perl_croak_xs_usage", (PERL_PROC*)&Perl_croak_xs_usage},
421#endif
Bram Moolenaard8619992014-03-12 17:08:05 +0100422#ifdef PERL_IMPLICIT_CONTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 {"Perl_croak_nocontext", (PERL_PROC*)&Perl_croak_nocontext},
Bram Moolenaard8619992014-03-12 17:08:05 +0100424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 {"Perl_dowantarray", (PERL_PROC*)&Perl_dowantarray},
426 {"Perl_free_tmps", (PERL_PROC*)&Perl_free_tmps},
427 {"Perl_gv_stashpv", (PERL_PROC*)&Perl_gv_stashpv},
428 {"Perl_markstack_grow", (PERL_PROC*)&Perl_markstack_grow},
429 {"Perl_mg_find", (PERL_PROC*)&Perl_mg_find},
430 {"Perl_newXS", (PERL_PROC*)&Perl_newXS},
431 {"Perl_newSV", (PERL_PROC*)&Perl_newSV},
432 {"Perl_newSViv", (PERL_PROC*)&Perl_newSViv},
433 {"Perl_newSVpv", (PERL_PROC*)&Perl_newSVpv},
434 {"Perl_call_argv", (PERL_PROC*)&Perl_call_argv},
435 {"Perl_call_pv", (PERL_PROC*)&Perl_call_pv},
436 {"Perl_eval_sv", (PERL_PROC*)&Perl_eval_sv},
437 {"Perl_get_sv", (PERL_PROC*)&Perl_get_sv},
438 {"Perl_eval_pv", (PERL_PROC*)&Perl_eval_pv},
439 {"Perl_call_method", (PERL_PROC*)&Perl_call_method},
440 {"Perl_pop_scope", (PERL_PROC*)&Perl_pop_scope},
441 {"Perl_push_scope", (PERL_PROC*)&Perl_push_scope},
442 {"Perl_save_int", (PERL_PROC*)&Perl_save_int},
443 {"Perl_stack_grow", (PERL_PROC*)&Perl_stack_grow},
444 {"Perl_set_context", (PERL_PROC*)&Perl_set_context},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200445#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
446 {"Perl_sv_2bool_flags", (PERL_PROC*)&Perl_sv_2bool_flags},
447 {"Perl_xs_apiversion_bootcheck",(PERL_PROC*)&Perl_xs_apiversion_bootcheck},
448#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 {"Perl_sv_2bool", (PERL_PROC*)&Perl_sv_2bool},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 {"Perl_sv_2iv", (PERL_PROC*)&Perl_sv_2iv},
452 {"Perl_sv_2mortal", (PERL_PROC*)&Perl_sv_2mortal},
453#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
454 {"Perl_sv_2pv_flags", (PERL_PROC*)&Perl_sv_2pv_flags},
455 {"Perl_sv_2pv_nolen", (PERL_PROC*)&Perl_sv_2pv_nolen},
456#else
457 {"Perl_sv_2pv", (PERL_PROC*)&Perl_sv_2pv},
458#endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000459#ifdef PERL589_OR_LATER
460 {"Perl_sv_2iv_flags", (PERL_PROC*)&Perl_sv_2iv_flags},
461 {"Perl_newXS_flags", (PERL_PROC*)&Perl_newXS_flags},
462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 {"Perl_sv_bless", (PERL_PROC*)&Perl_sv_bless},
464#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
465 {"Perl_sv_catpvn_flags", (PERL_PROC*)&Perl_sv_catpvn_flags},
466#else
467 {"Perl_sv_catpvn", (PERL_PROC*)&Perl_sv_catpvn},
468#endif
469 {"Perl_sv_free", (PERL_PROC*)&Perl_sv_free},
470 {"Perl_sv_isa", (PERL_PROC*)&Perl_sv_isa},
471 {"Perl_sv_magic", (PERL_PROC*)&Perl_sv_magic},
472 {"Perl_sv_setiv", (PERL_PROC*)&Perl_sv_setiv},
473 {"Perl_sv_setpv", (PERL_PROC*)&Perl_sv_setpv},
474 {"Perl_sv_setpvn", (PERL_PROC*)&Perl_sv_setpvn},
475#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
476 {"Perl_sv_setsv_flags", (PERL_PROC*)&Perl_sv_setsv_flags},
477#else
478 {"Perl_sv_setsv", (PERL_PROC*)&Perl_sv_setsv},
479#endif
480 {"Perl_sv_upgrade", (PERL_PROC*)&Perl_sv_upgrade},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000481#if (PERL_REVISION == 5) && (PERL_VERSION < 10)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 {"Perl_Tstack_sp_ptr", (PERL_PROC*)&Perl_Tstack_sp_ptr},
483 {"Perl_Top_ptr", (PERL_PROC*)&Perl_Top_ptr},
484 {"Perl_Tstack_base_ptr", (PERL_PROC*)&Perl_Tstack_base_ptr},
485 {"Perl_Tstack_max_ptr", (PERL_PROC*)&Perl_Tstack_max_ptr},
486 {"Perl_Ttmps_ix_ptr", (PERL_PROC*)&Perl_Ttmps_ix_ptr},
487 {"Perl_Ttmps_floor_ptr", (PERL_PROC*)&Perl_Ttmps_floor_ptr},
488 {"Perl_Tmarkstack_ptr_ptr", (PERL_PROC*)&Perl_Tmarkstack_ptr_ptr},
489 {"Perl_Tmarkstack_max_ptr", (PERL_PROC*)&Perl_Tmarkstack_max_ptr},
490 {"Perl_TSv_ptr", (PERL_PROC*)&Perl_TSv_ptr},
491 {"Perl_TXpv_ptr", (PERL_PROC*)&Perl_TXpv_ptr},
492 {"Perl_Tna_ptr", (PERL_PROC*)&Perl_Tna_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000493#else
Bram Moolenaarccf22172008-09-01 15:56:45 +0000494 {"Perl_sv_free2", (PERL_PROC*)&Perl_sv_free2},
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000495 {"Perl_sys_init", (PERL_PROC*)&Perl_sys_init},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000496 {"Perl_sys_term", (PERL_PROC*)&Perl_sys_term},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200497 {"Perl_call_list", (PERL_PROC*)&Perl_call_list},
498# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
499# else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000500 {"Perl_ISv_ptr", (PERL_PROC*)&Perl_ISv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000501 {"Perl_Istack_max_ptr", (PERL_PROC*)&Perl_Istack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200502 {"Perl_Istack_base_ptr", (PERL_PROC*)&Perl_Istack_base_ptr},
503 {"Perl_IXpv_ptr", (PERL_PROC*)&Perl_IXpv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000504 {"Perl_Itmps_ix_ptr", (PERL_PROC*)&Perl_Itmps_ix_ptr},
505 {"Perl_Itmps_floor_ptr", (PERL_PROC*)&Perl_Itmps_floor_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200506 {"Perl_Ina_ptr", (PERL_PROC*)&Perl_Ina_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000507 {"Perl_Imarkstack_ptr_ptr", (PERL_PROC*)&Perl_Imarkstack_ptr_ptr},
508 {"Perl_Imarkstack_max_ptr", (PERL_PROC*)&Perl_Imarkstack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200509 {"Perl_Istack_sp_ptr", (PERL_PROC*)&Perl_Istack_sp_ptr},
510 {"Perl_Iop_ptr", (PERL_PROC*)&Perl_Iop_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000511 {"Perl_Iscopestack_ix_ptr", (PERL_PROC*)&Perl_Iscopestack_ix_ptr},
512 {"Perl_Iunitcheckav_ptr", (PERL_PROC*)&Perl_Iunitcheckav_ptr},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200513# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000514#endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200515#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100516# ifdef USE_ITHREADS
Bram Moolenaar01c10522012-09-21 12:50:51 +0200517 {"PL_thr_key", (PERL_PROC*)&dll_PL_thr_key},
Bram Moolenaard8619992014-03-12 17:08:05 +0100518# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200519#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 {"Perl_Idefgv_ptr", (PERL_PROC*)&Perl_Idefgv_ptr},
521 {"Perl_Ierrgv_ptr", (PERL_PROC*)&Perl_Ierrgv_ptr},
522 {"Perl_Isv_yes_ptr", (PERL_PROC*)&Perl_Isv_yes_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200523 {"Perl_Gthr_key_ptr", (PERL_PROC*)&Perl_Gthr_key_ptr},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200524#endif
525 {"boot_DynaLoader", (PERL_PROC*)&boot_DynaLoader},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 {"", NULL},
527};
528
Bram Moolenaar6b107212013-12-11 15:06:40 +0100529/* Work around for perl-5.18.
530 * The definitions of S_SvREFCNT_inc and S_SvREFCNT_dec are needed, so include
531 * "perl\lib\CORE\inline.h", after Perl_sv_free2 is defined.
532 * The linker won't complain about undefined __impl_Perl_sv_free2. */
533#if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
534# include <inline.h>
535#endif
536
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537/*
538 * Make all runtime-links of perl.
539 *
Bram Moolenaar364ab2f2013-08-02 20:05:32 +0200540 * 1. Get module handle using dlopen() or vimLoadLib().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 * 2. Get pointer to perl function by GetProcAddress.
542 * 3. Repeat 2, until get all functions will be used.
543 *
544 * Parameter 'libname' provides name of DLL.
545 * Return OK or FAIL.
546 */
547 static int
548perl_runtime_link_init(char *libname, int verbose)
549{
550 int i;
551
552 if (hPerlLib != NULL)
553 return OK;
Bram Moolenaare06c1882010-07-21 22:05:20 +0200554 if ((hPerlLib = load_dll(libname)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 {
556 if (verbose)
557 EMSG2(_("E370: Could not load library %s"), libname);
558 return FAIL;
559 }
560 for (i = 0; perl_funcname_table[i].ptr; ++i)
561 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200562 if (!(*perl_funcname_table[i].ptr = symbol_from_dll(hPerlLib,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 perl_funcname_table[i].name)))
564 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200565 close_dll(hPerlLib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 hPerlLib = NULL;
567 if (verbose)
568 EMSG2(_(e_loadfunc), perl_funcname_table[i].name);
569 return FAIL;
570 }
571 }
572 return OK;
573}
574
575/*
576 * If runtime-link-perl(DLL) was loaded successfully, return TRUE.
577 * There were no DLL loaded, return FALSE.
578 */
579 int
580perl_enabled(verbose)
581 int verbose;
582{
583 return perl_runtime_link_init(DYNAMIC_PERL_DLL, verbose) == OK;
584}
585#endif /* DYNAMIC_PERL */
586
587/*
588 * perl_init(): initialize perl interpreter
589 * We have to call perl_parse to initialize some structures,
590 * there's nothing to actually parse.
591 */
592 static void
593perl_init()
594{
Bram Moolenaarc236c162008-07-13 17:41:49 +0000595 char *bootargs[] = { "VI", NULL };
596 int argc = 3;
597 static char *argv[] = { "", "-e", "" };
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598
Bram Moolenaarc236c162008-07-13 17:41:49 +0000599#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000600 Perl_sys_init(&argc, (char***)&argv);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 perl_interp = perl_alloc();
603 perl_construct(perl_interp);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000604 perl_parse(perl_interp, xs_init, argc, argv, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 perl_call_argv("VIM::bootstrap", (long)G_DISCARD, bootargs);
606 VIM_init();
607#ifdef USE_SFIO
608 sfdisc(PerlIO_stdout(), sfdcnewvim());
609 sfdisc(PerlIO_stderr(), sfdcnewvim());
610 sfsetbuf(PerlIO_stdout(), NULL, 0);
611 sfsetbuf(PerlIO_stderr(), NULL, 0);
612#endif
613}
614
615/*
616 * perl_end(): clean up after ourselves
617 */
618 void
619perl_end()
620{
621 if (perl_interp)
622 {
623 perl_run(perl_interp);
624 perl_destruct(perl_interp);
625 perl_free(perl_interp);
626 perl_interp = NULL;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000627#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
628 Perl_sys_term();
629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 }
631#ifdef DYNAMIC_PERL
632 if (hPerlLib)
633 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200634 close_dll(hPerlLib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 hPerlLib = NULL;
636 }
637#endif
638}
639
640/*
641 * msg_split(): send a message to the message handling routines
642 * split at '\n' first though.
643 */
644 void
645msg_split(s, attr)
646 char_u *s;
647 int attr; /* highlighting attributes */
648{
649 char *next;
650 char *token = (char *)s;
651
Bram Moolenaaraa8494a2007-10-09 08:47:27 +0000652 while ((next = strchr(token, '\n')) && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 {
654 *next++ = '\0'; /* replace \n with \0 */
655 msg_attr((char_u *)token, attr);
656 token = next;
657 }
Bram Moolenaaraa8494a2007-10-09 08:47:27 +0000658 if (*token && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 msg_attr((char_u *)token, attr);
660}
661
662#ifndef FEAT_EVAL
663/*
664 * This stub is needed because an "#ifdef FEAT_EVAL" around Eval() doesn't
665 * work properly.
666 */
667 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000668eval_to_string(arg, nextcmd, dolist)
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100669 char_u *arg UNUSED;
670 char_u **nextcmd UNUSED;
671 int dolist UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672{
673 return NULL;
674}
675#endif
676
677/*
678 * Create a new reference to an SV pointing to the SCR structure
Bram Moolenaare344bea2005-09-01 20:46:49 +0000679 * The b_perl_private/w_perl_private part of the SCR structure points to the
680 * SV, so there can only be one such SV for a particular SCR structure. When
681 * the last reference has gone (DESTROY is called),
682 * b_perl_private/w_perl_private is reset; When the screen goes away before
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 * all references are gone, the value of the SV is reset;
684 * any subsequent use of any of those reference will produce
685 * a warning. (see typemap)
686 */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000687
688 static SV *
689newWINrv(rv, ptr)
690 SV *rv;
691 win_T *ptr;
692{
693 sv_upgrade(rv, SVt_RV);
694 if (ptr->w_perl_private == NULL)
695 {
696 ptr->w_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +0100697 sv_setiv(ptr->w_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +0000698 }
699 else
700 SvREFCNT_inc(ptr->w_perl_private);
701 SvRV(rv) = ptr->w_perl_private;
702 SvROK_on(rv);
703 return sv_bless(rv, gv_stashpv("VIWIN", TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704}
705
Bram Moolenaare344bea2005-09-01 20:46:49 +0000706 static SV *
707newBUFrv(rv, ptr)
708 SV *rv;
709 buf_T *ptr;
710{
711 sv_upgrade(rv, SVt_RV);
712 if (ptr->b_perl_private == NULL)
713 {
714 ptr->b_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +0100715 sv_setiv(ptr->b_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +0000716 }
717 else
718 SvREFCNT_inc(ptr->b_perl_private);
719 SvRV(rv) = ptr->b_perl_private;
720 SvROK_on(rv);
721 return sv_bless(rv, gv_stashpv("VIBUF", TRUE));
722}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723
724/*
725 * perl_win_free
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200726 * Remove all references to the window to be destroyed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 */
728 void
729perl_win_free(wp)
730 win_T *wp;
731{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000732 if (wp->w_perl_private)
733 sv_setiv((SV *)wp->w_perl_private, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 return;
735}
736
737 void
738perl_buf_free(bp)
739 buf_T *bp;
740{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000741 if (bp->b_perl_private)
742 sv_setiv((SV *)bp->b_perl_private, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 return;
744}
745
746#ifndef PROTO
747# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
748I32 cur_val(pTHX_ IV iv, SV *sv);
749# else
750I32 cur_val(IV iv, SV *sv);
751#endif
752
753/*
754 * Handler for the magic variables $main::curwin and $main::curbuf.
755 * The handler is put into the magic vtbl for these variables.
756 * (This is effectively a C-level equivalent of a tied variable).
757 * There is no "set" function as the variables are read-only.
758 */
759# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
760I32 cur_val(pTHX_ IV iv, SV *sv)
761# else
762I32 cur_val(IV iv, SV *sv)
763# endif
764{
765 SV *rv;
766 if (iv == 0)
767 rv = newWINrv(newSV(0), curwin);
768 else
769 rv = newBUFrv(newSV(0), curbuf);
770 sv_setsv(sv, rv);
771 return 0;
772}
773#endif /* !PROTO */
774
775struct ufuncs cw_funcs = { cur_val, 0, 0 };
776struct ufuncs cb_funcs = { cur_val, 0, 1 };
777
778/*
779 * VIM_init(): Vim-specific initialisation.
780 * Make the magical main::curwin and main::curbuf variables
781 */
782 static void
783VIM_init()
784{
785 static char cw[] = "main::curwin";
786 static char cb[] = "main::curbuf";
787 SV *sv;
788
789 sv = perl_get_sv(cw, TRUE);
790 sv_magic(sv, NULL, 'U', (char *)&cw_funcs, sizeof(cw_funcs));
791 SvREADONLY_on(sv);
792
793 sv = perl_get_sv(cb, TRUE);
794 sv_magic(sv, NULL, 'U', (char *)&cb_funcs, sizeof(cb_funcs));
795 SvREADONLY_on(sv);
796
797 /*
798 * Setup the Safe compartment.
799 * It shouldn't be a fatal error if the Safe module is missing.
800 * XXX: Only shares the 'Msg' routine (which has to be called
801 * like 'Msg(...)').
802 */
803 (void)perl_eval_pv( "if ( eval( 'require Safe' ) ) { $VIM::safe = Safe->new(); $VIM::safe->share_from( 'VIM', ['Msg'] ); }", G_DISCARD | G_VOID );
804
805}
806
807#ifdef DYNAMIC_PERL
808static char *e_noperl = N_("Sorry, this command is disabled: the Perl library could not be loaded.");
809#endif
810
811/*
812 * ":perl"
813 */
814 void
815ex_perl(eap)
816 exarg_T *eap;
817{
818 char *err;
819 char *script;
820 STRLEN length;
821 SV *sv;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200822#ifdef HAVE_SANDBOX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 SV *safe;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825
826 script = (char *)script_get(eap, eap->arg);
827 if (eap->skip)
828 {
829 vim_free(script);
830 return;
831 }
832
833 if (perl_interp == NULL)
834 {
835#ifdef DYNAMIC_PERL
836 if (!perl_enabled(TRUE))
837 {
838 EMSG(_(e_noperl));
839 vim_free(script);
840 return;
841 }
842#endif
843 perl_init();
844 }
845
846 {
847 dSP;
848 ENTER;
849 SAVETMPS;
850
851 if (script == NULL)
852 sv = newSVpv((char *)eap->arg, 0);
853 else
854 {
855 sv = newSVpv(script, 0);
856 vim_free(script);
857 }
858
859#ifdef HAVE_SANDBOX
860 if (sandbox)
861 {
Bram Moolenaara1711622011-07-27 14:15:46 +0200862 safe = perl_get_sv("VIM::safe", FALSE);
Bram Moolenaar3f947ea2009-07-14 14:04:54 +0000863# ifndef MAKE_TEST /* avoid a warning for unreachable code */
Bram Moolenaar954e8c52009-11-11 13:45:33 +0000864 if (safe == NULL || !SvTRUE(safe))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 EMSG(_("E299: Perl evaluation forbidden in sandbox without the Safe module"));
866 else
Bram Moolenaar3f947ea2009-07-14 14:04:54 +0000867# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 {
869 PUSHMARK(SP);
870 XPUSHs(safe);
871 XPUSHs(sv);
872 PUTBACK;
873 perl_call_method("reval", G_DISCARD);
874 }
875 }
876 else
877#endif
878 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
879
880 SvREFCNT_dec(sv);
881
Bram Moolenaarc271c482012-08-08 13:17:31 +0200882#ifdef AVOID_PL_ERRGV
883 err = SvPV(perl_get_sv("@", GV_ADD), length);
884#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 err = SvPV(GvSV(PL_errgv), length);
Bram Moolenaarc271c482012-08-08 13:17:31 +0200886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887
888 FREETMPS;
889 LEAVE;
890
891 if (!length)
892 return;
893
894 msg_split((char_u *)err, highlight_attr[HLF_E]);
895 return;
896 }
897}
898
899 static int
900replace_line(line, end)
901 linenr_T *line, *end;
902{
903 char *str;
904
905 if (SvOK(GvSV(PL_defgv)))
906 {
907 str = SvPV(GvSV(PL_defgv), PL_na);
908 ml_replace(*line, (char_u *)str, 1);
909 changed_bytes(*line, 0);
910 }
911 else
912 {
913 ml_delete(*line, FALSE);
914 deleted_lines_mark(*line, 1L);
915 --(*end);
916 --(*line);
917 }
918 return OK;
919}
920
921/*
922 * ":perldo".
923 */
924 void
925ex_perldo(eap)
926 exarg_T *eap;
927{
928 STRLEN length;
929 SV *sv;
930 char *str;
931 linenr_T i;
932
933 if (bufempty())
934 return;
935
936 if (perl_interp == NULL)
937 {
938#ifdef DYNAMIC_PERL
939 if (!perl_enabled(TRUE))
940 {
941 EMSG(_(e_noperl));
942 return;
943 }
944#endif
945 perl_init();
946 }
947 {
948 dSP;
949 length = strlen((char *)eap->arg);
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000950 sv = newSV(length + sizeof("sub VIM::perldo {") - 1 + 1);
951 sv_setpvn(sv, "sub VIM::perldo {", sizeof("sub VIM::perldo {") - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 sv_catpvn(sv, (char *)eap->arg, length);
953 sv_catpvn(sv, "}", 1);
954 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
955 SvREFCNT_dec(sv);
Bram Moolenaarc271c482012-08-08 13:17:31 +0200956#ifdef AVOID_PL_ERRGV
957 str = SvPV(perl_get_sv("@", GV_ADD), length);
958#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 str = SvPV(GvSV(PL_errgv), length);
Bram Moolenaarc271c482012-08-08 13:17:31 +0200960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 if (length)
962 goto err;
963
964 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
965 return;
966
967 ENTER;
968 SAVETMPS;
969 for (i = eap->line1; i <= eap->line2; i++)
970 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000971 sv_setpv(GvSV(PL_defgv), (char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 PUSHMARK(sp);
973 perl_call_pv("VIM::perldo", G_SCALAR | G_EVAL);
Bram Moolenaarc271c482012-08-08 13:17:31 +0200974#ifdef AVOID_PL_ERRGV
975 str = SvPV(perl_get_sv("@", GV_ADD), length);
976#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 str = SvPV(GvSV(PL_errgv), length);
Bram Moolenaarc271c482012-08-08 13:17:31 +0200978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 if (length)
980 break;
981 SPAGAIN;
982 if (SvTRUEx(POPs))
983 {
984 if (replace_line(&i, &eap->line2) != OK)
985 {
986 PUTBACK;
987 break;
988 }
989 }
990 PUTBACK;
991 }
992 FREETMPS;
993 LEAVE;
994 check_cursor();
995 update_screen(NOT_VALID);
996 if (!length)
997 return;
998
999err:
1000 msg_split((char_u *)str, highlight_attr[HLF_E]);
1001 return;
1002 }
1003}
1004
Bram Moolenaar01dd60c2008-07-24 14:24:48 +00001005#ifndef FEAT_WINDOWS
1006int win_valid(win_T *w) { return TRUE; }
1007int win_count() { return 1; }
1008win_T *win_find_nr(int n) { return curwin; }
1009#endif
1010
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011XS(boot_VIM);
1012
1013 static void
1014xs_init(pTHX)
1015{
1016 char *file = __FILE__;
1017
1018 /* DynaLoader is a special case */
1019 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1020 newXS("VIM::bootstrap", boot_VIM, file);
1021}
1022
1023typedef win_T * VIWIN;
1024typedef buf_T * VIBUF;
1025
1026MODULE = VIM PACKAGE = VIM
1027
1028void
1029Msg(text, hl=NULL)
1030 char *text;
1031 char *hl;
1032
1033 PREINIT:
1034 int attr;
1035 int id;
1036
1037 PPCODE:
1038 if (text != NULL)
1039 {
1040 attr = 0;
1041 if (hl != NULL)
1042 {
1043 id = syn_name2id((char_u *)hl);
1044 if (id != 0)
1045 attr = syn_id2attr(id);
1046 }
1047 msg_split((char_u *)text, attr);
1048 }
1049
1050void
1051SetOption(line)
1052 char *line;
1053
1054 PPCODE:
1055 if (line != NULL)
1056 do_set((char_u *)line, 0);
1057 update_screen(NOT_VALID);
1058
1059void
1060DoCommand(line)
1061 char *line;
1062
1063 PPCODE:
1064 if (line != NULL)
1065 do_cmdline_cmd((char_u *)line);
1066
1067void
1068Eval(str)
1069 char *str;
1070
1071 PREINIT:
1072 char_u *value;
1073 PPCODE:
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001074 value = eval_to_string((char_u *)str, (char_u **)0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 if (value == NULL)
1076 {
1077 XPUSHs(sv_2mortal(newSViv(0)));
1078 XPUSHs(sv_2mortal(newSVpv("", 0)));
1079 }
1080 else
1081 {
1082 XPUSHs(sv_2mortal(newSViv(1)));
1083 XPUSHs(sv_2mortal(newSVpv((char *)value, 0)));
1084 vim_free(value);
1085 }
1086
1087void
1088Buffers(...)
1089
1090 PREINIT:
1091 buf_T *vimbuf;
1092 int i, b;
1093
1094 PPCODE:
1095 if (items == 0)
1096 {
1097 if (GIMME == G_SCALAR)
1098 {
1099 i = 0;
1100 for (vimbuf = firstbuf; vimbuf; vimbuf = vimbuf->b_next)
1101 ++i;
1102
1103 XPUSHs(sv_2mortal(newSViv(i)));
1104 }
1105 else
1106 {
1107 for (vimbuf = firstbuf; vimbuf; vimbuf = vimbuf->b_next)
1108 XPUSHs(newBUFrv(newSV(0), vimbuf));
1109 }
1110 }
1111 else
1112 {
1113 for (i = 0; i < items; i++)
1114 {
1115 SV *sv = ST(i);
1116 if (SvIOK(sv))
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001117 b = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 else
1119 {
1120 char_u *pat;
1121 STRLEN len;
1122
1123 pat = (char_u *)SvPV(sv, len);
1124 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001125 b = buflist_findpat(pat, pat+len, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 --emsg_off;
1127 }
1128
1129 if (b >= 0)
1130 {
1131 vimbuf = buflist_findnr(b);
1132 if (vimbuf)
1133 XPUSHs(newBUFrv(newSV(0), vimbuf));
1134 }
1135 }
1136 }
1137
1138void
1139Windows(...)
1140
1141 PREINIT:
1142 win_T *vimwin;
1143 int i, w;
1144
1145 PPCODE:
1146 if (items == 0)
1147 {
1148 if (GIMME == G_SCALAR)
1149 XPUSHs(sv_2mortal(newSViv(win_count())));
1150 else
1151 {
1152 for (vimwin = firstwin; vimwin != NULL; vimwin = W_NEXT(vimwin))
1153 XPUSHs(newWINrv(newSV(0), vimwin));
1154 }
1155 }
1156 else
1157 {
1158 for (i = 0; i < items; i++)
1159 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001160 w = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 vimwin = win_find_nr(w);
1162 if (vimwin)
1163 XPUSHs(newWINrv(newSV(0), vimwin));
1164 }
1165 }
1166
1167MODULE = VIM PACKAGE = VIWIN
1168
1169void
1170DESTROY(win)
1171 VIWIN win
1172
1173 CODE:
1174 if (win_valid(win))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001175 win->w_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176
1177SV *
1178Buffer(win)
1179 VIWIN win
1180
1181 CODE:
1182 if (!win_valid(win))
1183 win = curwin;
1184 RETVAL = newBUFrv(newSV(0), win->w_buffer);
1185 OUTPUT:
1186 RETVAL
1187
1188void
1189SetHeight(win, height)
1190 VIWIN win
1191 int height;
1192
1193 PREINIT:
1194 win_T *savewin;
1195
1196 PPCODE:
1197 if (!win_valid(win))
1198 win = curwin;
1199 savewin = curwin;
1200 curwin = win;
1201 win_setheight(height);
1202 curwin = savewin;
1203
1204void
1205Cursor(win, ...)
1206 VIWIN win
1207
1208 PPCODE:
Bram Moolenaara1711622011-07-27 14:15:46 +02001209 if (items == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 {
1211 EXTEND(sp, 2);
1212 if (!win_valid(win))
1213 win = curwin;
1214 PUSHs(sv_2mortal(newSViv(win->w_cursor.lnum)));
1215 PUSHs(sv_2mortal(newSViv(win->w_cursor.col)));
1216 }
Bram Moolenaara1711622011-07-27 14:15:46 +02001217 else if (items == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 {
1219 int lnum, col;
1220
1221 if (!win_valid(win))
1222 win = curwin;
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001223 lnum = (int) SvIV(ST(1));
1224 col = (int) SvIV(ST(2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 win->w_cursor.lnum = lnum;
1226 win->w_cursor.col = col;
1227 check_cursor(); /* put cursor on an existing line */
1228 update_screen(NOT_VALID);
1229 }
1230
1231MODULE = VIM PACKAGE = VIBUF
1232
1233void
1234DESTROY(vimbuf)
1235 VIBUF vimbuf;
1236
1237 CODE:
1238 if (buf_valid(vimbuf))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001239 vimbuf->b_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240
1241void
1242Name(vimbuf)
1243 VIBUF vimbuf;
1244
1245 PPCODE:
1246 if (!buf_valid(vimbuf))
1247 vimbuf = curbuf;
1248 /* No file name returns an empty string */
1249 if (vimbuf->b_fname == NULL)
1250 XPUSHs(sv_2mortal(newSVpv("", 0)));
1251 else
1252 XPUSHs(sv_2mortal(newSVpv((char *)vimbuf->b_fname, 0)));
1253
1254void
1255Number(vimbuf)
1256 VIBUF vimbuf;
1257
1258 PPCODE:
1259 if (!buf_valid(vimbuf))
1260 vimbuf = curbuf;
1261 XPUSHs(sv_2mortal(newSViv(vimbuf->b_fnum)));
1262
1263void
1264Count(vimbuf)
1265 VIBUF vimbuf;
1266
1267 PPCODE:
1268 if (!buf_valid(vimbuf))
1269 vimbuf = curbuf;
1270 XPUSHs(sv_2mortal(newSViv(vimbuf->b_ml.ml_line_count)));
1271
1272void
1273Get(vimbuf, ...)
1274 VIBUF vimbuf;
1275
1276 PREINIT:
1277 char_u *line;
1278 int i;
1279 long lnum;
1280 PPCODE:
1281 if (buf_valid(vimbuf))
1282 {
1283 for (i = 1; i < items; i++)
1284 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001285 lnum = (long) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1287 {
1288 line = ml_get_buf(vimbuf, lnum, FALSE);
1289 XPUSHs(sv_2mortal(newSVpv((char *)line, 0)));
1290 }
1291 }
1292 }
1293
1294void
1295Set(vimbuf, ...)
1296 VIBUF vimbuf;
1297
1298 PREINIT:
1299 int i;
1300 long lnum;
1301 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 PPCODE:
1303 if (buf_valid(vimbuf))
1304 {
1305 if (items < 3)
1306 croak("Usage: VIBUF::Set(vimbuf, lnum, @lines)");
1307
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001308 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 for(i = 2; i < items; i++, lnum++)
1310 {
1311 line = SvPV(ST(i),PL_na);
1312 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1313 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001314 aco_save_T aco;
1315
1316 /* set curwin/curbuf for "vimbuf" and save some things */
1317 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001318
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 if (u_savesub(lnum) == OK)
1320 {
1321 ml_replace(lnum, (char_u *)line, TRUE);
1322 changed_bytes(lnum, 0);
1323 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001324
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001325 /* restore curwin/curbuf and a few other things */
1326 aucmd_restbuf(&aco);
1327 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 }
1329 }
1330 }
1331
1332void
1333Delete(vimbuf, ...)
1334 VIBUF vimbuf;
1335
1336 PREINIT:
1337 long i, lnum = 0, count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 PPCODE:
1339 if (buf_valid(vimbuf))
1340 {
1341 if (items == 2)
1342 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001343 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 count = 1;
1345 }
1346 else if (items == 3)
1347 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001348 lnum = (long) SvIV(ST(1));
1349 count = (long) 1 + SvIV(ST(2)) - lnum;
Bram Moolenaara1711622011-07-27 14:15:46 +02001350 if (count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 count = 1;
Bram Moolenaara1711622011-07-27 14:15:46 +02001352 if (count < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 {
1354 lnum -= count;
1355 count = -count;
1356 }
1357 }
1358 if (items >= 2)
1359 {
1360 for (i = 0; i < count; i++)
1361 {
1362 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1363 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001364 aco_save_T aco;
1365
1366 /* set curwin/curbuf for "vimbuf" and save some things */
1367 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001368
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 if (u_savedel(lnum, 1) == OK)
1370 {
1371 ml_delete(lnum, 0);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00001372 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 deleted_lines_mark(lnum, 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001375
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001376 /* restore curwin/curbuf and a few other things */
1377 aucmd_restbuf(&aco);
1378 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001379
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 update_curbuf(VALID);
1381 }
1382 }
1383 }
1384 }
1385
1386void
1387Append(vimbuf, ...)
1388 VIBUF vimbuf;
1389
1390 PREINIT:
1391 int i;
1392 long lnum;
1393 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 PPCODE:
1395 if (buf_valid(vimbuf))
1396 {
1397 if (items < 3)
1398 croak("Usage: VIBUF::Append(vimbuf, lnum, @lines)");
1399
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001400 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 for (i = 2; i < items; i++, lnum++)
1402 {
1403 line = SvPV(ST(i),PL_na);
1404 if (lnum >= 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1405 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001406 aco_save_T aco;
1407
1408 /* set curwin/curbuf for "vimbuf" and save some things */
1409 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001410
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 if (u_inssub(lnum + 1) == OK)
1412 {
1413 ml_append(lnum, (char_u *)line, (colnr_T)0, FALSE);
1414 appended_lines_mark(lnum, 1L);
1415 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001416
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001417 /* restore curwin/curbuf and a few other things */
1418 aucmd_restbuf(&aco);
1419 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001420
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 update_curbuf(VALID);
1422 }
1423 }
1424 }
1425