blob: 57a3b8fd14742700ea82207064994423abc6ded2 [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;
141typedef int perl_key;
142#endif
143
Bram Moolenaar766fb0d2010-07-22 11:34:16 +0200144#ifndef WIN3264
Bram Moolenaare06c1882010-07-21 22:05:20 +0200145#include <dlfcn.h>
146#define HANDLE void*
147#define PERL_PROC void*
148#define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
149#define symbol_from_dll dlsym
150#define close_dll dlclose
151#else
152#define PERL_PROC FARPROC
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200153#define load_dll vimLoadLib
Bram Moolenaare06c1882010-07-21 22:05:20 +0200154#define symbol_from_dll GetProcAddress
155#define close_dll FreeLibrary
156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157/*
158 * Wrapper defines
159 */
160# define perl_alloc dll_perl_alloc
161# define perl_construct dll_perl_construct
162# define perl_parse dll_perl_parse
163# define perl_run dll_perl_run
164# define perl_destruct dll_perl_destruct
165# define perl_free dll_perl_free
166# define Perl_get_context dll_Perl_get_context
167# define Perl_croak dll_Perl_croak
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100168# ifdef PERL5101_OR_LATER
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100169# define Perl_croak_xs_usage dll_Perl_croak_xs_usage
170# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171# ifndef PROTO
172# define Perl_croak_nocontext dll_Perl_croak_nocontext
173# define Perl_call_argv dll_Perl_call_argv
174# define Perl_call_pv dll_Perl_call_pv
175# define Perl_eval_sv dll_Perl_eval_sv
176# define Perl_get_sv dll_Perl_get_sv
177# define Perl_eval_pv dll_Perl_eval_pv
178# define Perl_call_method dll_Perl_call_method
179# endif
180# define Perl_dowantarray dll_Perl_dowantarray
181# define Perl_free_tmps dll_Perl_free_tmps
182# define Perl_gv_stashpv dll_Perl_gv_stashpv
183# define Perl_markstack_grow dll_Perl_markstack_grow
184# define Perl_mg_find dll_Perl_mg_find
185# define Perl_newXS dll_Perl_newXS
186# define Perl_newSV dll_Perl_newSV
187# define Perl_newSViv dll_Perl_newSViv
188# define Perl_newSVpv dll_Perl_newSVpv
189# define Perl_pop_scope dll_Perl_pop_scope
190# define Perl_push_scope dll_Perl_push_scope
191# define Perl_save_int dll_Perl_save_int
192# define Perl_stack_grow dll_Perl_stack_grow
193# define Perl_set_context dll_Perl_set_context
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200194# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
195# define Perl_sv_2bool_flags dll_Perl_sv_2bool_flags
Bram Moolenaar01c10522012-09-21 12:50:51 +0200196# define Perl_xs_apiversion_bootcheck dll_Perl_xs_apiversion_bootcheck
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200197# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198# define Perl_sv_2bool dll_Perl_sv_2bool
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200199# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200# define Perl_sv_2iv dll_Perl_sv_2iv
201# define Perl_sv_2mortal dll_Perl_sv_2mortal
202# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
203# define Perl_sv_2pv_flags dll_Perl_sv_2pv_flags
204# define Perl_sv_2pv_nolen dll_Perl_sv_2pv_nolen
205# else
206# define Perl_sv_2pv dll_Perl_sv_2pv
207# endif
208# define Perl_sv_bless dll_Perl_sv_bless
209# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
210# define Perl_sv_catpvn_flags dll_Perl_sv_catpvn_flags
211# else
212# define Perl_sv_catpvn dll_Perl_sv_catpvn
213# endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000214#ifdef PERL589_OR_LATER
215# define Perl_sv_2iv_flags dll_Perl_sv_2iv_flags
216# define Perl_newXS_flags dll_Perl_newXS_flags
217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218# define Perl_sv_free dll_Perl_sv_free
Bram Moolenaarccf22172008-09-01 15:56:45 +0000219# if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
220# define Perl_sv_free2 dll_Perl_sv_free2
221# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222# define Perl_sv_isa dll_Perl_sv_isa
223# define Perl_sv_magic dll_Perl_sv_magic
224# define Perl_sv_setiv dll_Perl_sv_setiv
225# define Perl_sv_setpv dll_Perl_sv_setpv
226# define Perl_sv_setpvn dll_Perl_sv_setpvn
227# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
228# define Perl_sv_setsv_flags dll_Perl_sv_setsv_flags
229# else
230# define Perl_sv_setsv dll_Perl_sv_setsv
231# endif
232# define Perl_sv_upgrade dll_Perl_sv_upgrade
233# define Perl_Tstack_sp_ptr dll_Perl_Tstack_sp_ptr
234# define Perl_Top_ptr dll_Perl_Top_ptr
235# define Perl_Tstack_base_ptr dll_Perl_Tstack_base_ptr
236# define Perl_Tstack_max_ptr dll_Perl_Tstack_max_ptr
237# define Perl_Ttmps_ix_ptr dll_Perl_Ttmps_ix_ptr
238# define Perl_Ttmps_floor_ptr dll_Perl_Ttmps_floor_ptr
239# define Perl_Tmarkstack_ptr_ptr dll_Perl_Tmarkstack_ptr_ptr
240# define Perl_Tmarkstack_max_ptr dll_Perl_Tmarkstack_max_ptr
241# define Perl_TSv_ptr dll_Perl_TSv_ptr
242# define Perl_TXpv_ptr dll_Perl_TXpv_ptr
243# define Perl_Tna_ptr dll_Perl_Tna_ptr
244# define Perl_Idefgv_ptr dll_Perl_Idefgv_ptr
245# define Perl_Ierrgv_ptr dll_Perl_Ierrgv_ptr
246# define Perl_Isv_yes_ptr dll_Perl_Isv_yes_ptr
247# define boot_DynaLoader dll_boot_DynaLoader
Bram Moolenaare06c1882010-07-21 22:05:20 +0200248# define Perl_Gthr_key_ptr dll_Perl_Gthr_key_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000250# define Perl_sys_init dll_Perl_sys_init
Bram Moolenaarc236c162008-07-13 17:41:49 +0000251# define Perl_sys_term dll_Perl_sys_term
252# define Perl_ISv_ptr dll_Perl_ISv_ptr
253# define Perl_Istack_max_ptr dll_Perl_Istack_max_ptr
254# define Perl_Istack_base_ptr dll_Perl_Istack_base_ptr
255# define Perl_Itmps_ix_ptr dll_Perl_Itmps_ix_ptr
256# define Perl_Itmps_floor_ptr dll_Perl_Itmps_floor_ptr
257# define Perl_IXpv_ptr dll_Perl_IXpv_ptr
258# define Perl_Ina_ptr dll_Perl_Ina_ptr
259# define Perl_Imarkstack_ptr_ptr dll_Perl_Imarkstack_ptr_ptr
260# define Perl_Imarkstack_max_ptr dll_Perl_Imarkstack_max_ptr
261# define Perl_Istack_sp_ptr dll_Perl_Istack_sp_ptr
262# define Perl_Iop_ptr dll_Perl_Iop_ptr
263# define Perl_call_list dll_Perl_call_list
264# define Perl_Iscopestack_ix_ptr dll_Perl_Iscopestack_ix_ptr
265# define Perl_Iunitcheckav_ptr dll_Perl_Iunitcheckav_ptr
Bram Moolenaar01c10522012-09-21 12:50:51 +0200266# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
267# define PL_thr_key *dll_PL_thr_key
268# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000269
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270/*
271 * Declare HANDLE for perl.dll and function pointers.
272 */
273static HANDLE hPerlLib = NULL;
274
275static PerlInterpreter* (*perl_alloc)();
276static void (*perl_construct)(PerlInterpreter*);
277static void (*perl_destruct)(PerlInterpreter*);
278static void (*perl_free)(PerlInterpreter*);
279static int (*perl_run)(PerlInterpreter*);
280static int (*perl_parse)(PerlInterpreter*, XSINIT_t, int, char**, char**);
281static void* (*Perl_get_context)(void);
Bram Moolenaara7ecc562006-08-16 16:17:39 +0000282static void (*Perl_croak)(pTHX_ const char*, ...);
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100283#ifdef PERL5101_OR_LATER
Bram Moolenaar6b107212013-12-11 15:06:40 +0100284/* Perl-5.18 has a different Perl_croak_xs_usage signature. */
285# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
286static void (*Perl_croak_xs_usage)(const CV *const, const char *const params);
287# else
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100288static void (*Perl_croak_xs_usage)(pTHX_ const CV *const, const char *const params);
Bram Moolenaar6b107212013-12-11 15:06:40 +0100289# endif
Bram Moolenaar9be6e212013-06-15 16:47:35 +0200290#endif
Bram Moolenaara7ecc562006-08-16 16:17:39 +0000291static void (*Perl_croak_nocontext)(const char*, ...);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292static I32 (*Perl_dowantarray)(pTHX);
293static void (*Perl_free_tmps)(pTHX);
294static HV* (*Perl_gv_stashpv)(pTHX_ const char*, I32);
295static void (*Perl_markstack_grow)(pTHX);
296static MAGIC* (*Perl_mg_find)(pTHX_ SV*, int);
297static CV* (*Perl_newXS)(pTHX_ char*, XSUBADDR_t, char*);
298static SV* (*Perl_newSV)(pTHX_ STRLEN);
299static SV* (*Perl_newSViv)(pTHX_ IV);
300static SV* (*Perl_newSVpv)(pTHX_ const char*, STRLEN);
301static I32 (*Perl_call_argv)(pTHX_ const char*, I32, char**);
302static I32 (*Perl_call_pv)(pTHX_ const char*, I32);
303static I32 (*Perl_eval_sv)(pTHX_ SV*, I32);
304static SV* (*Perl_get_sv)(pTHX_ const char*, I32);
305static SV* (*Perl_eval_pv)(pTHX_ const char*, I32);
306static SV* (*Perl_call_method)(pTHX_ const char*, I32);
307static void (*Perl_pop_scope)(pTHX);
308static void (*Perl_push_scope)(pTHX);
309static void (*Perl_save_int)(pTHX_ int*);
310static SV** (*Perl_stack_grow)(pTHX_ SV**, SV**p, int);
311static SV** (*Perl_set_context)(void*);
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200312#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
313static bool (*Perl_sv_2bool_flags)(pTHX_ SV*, I32);
314static void (*Perl_xs_apiversion_bootcheck)(pTHX_ SV *module, const char *api_p, STRLEN api_len);
315#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316static bool (*Perl_sv_2bool)(pTHX_ SV*);
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318static IV (*Perl_sv_2iv)(pTHX_ SV*);
319static SV* (*Perl_sv_2mortal)(pTHX_ SV*);
320#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
321static char* (*Perl_sv_2pv_flags)(pTHX_ SV*, STRLEN*, I32);
322static char* (*Perl_sv_2pv_nolen)(pTHX_ SV*);
323#else
324static char* (*Perl_sv_2pv)(pTHX_ SV*, STRLEN*);
325#endif
326static SV* (*Perl_sv_bless)(pTHX_ SV*, HV*);
327#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
328static void (*Perl_sv_catpvn_flags)(pTHX_ SV* , const char*, STRLEN, I32);
329#else
330static void (*Perl_sv_catpvn)(pTHX_ SV*, const char*, STRLEN);
331#endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000332#ifdef PERL589_OR_LATER
333static IV (*Perl_sv_2iv_flags)(pTHX_ SV* sv, I32 flags);
334static CV * (*Perl_newXS_flags)(pTHX_ const char *name, XSUBADDR_t subaddr, const char *const filename, const char *const proto, U32 flags);
335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336static void (*Perl_sv_free)(pTHX_ SV*);
337static int (*Perl_sv_isa)(pTHX_ SV*, const char*);
338static void (*Perl_sv_magic)(pTHX_ SV*, SV*, int, const char*, I32);
339static void (*Perl_sv_setiv)(pTHX_ SV*, IV);
340static void (*Perl_sv_setpv)(pTHX_ SV*, const char*);
341static void (*Perl_sv_setpvn)(pTHX_ SV*, const char*, STRLEN);
342#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
343static void (*Perl_sv_setsv_flags)(pTHX_ SV*, SV*, I32);
344#else
345static void (*Perl_sv_setsv)(pTHX_ SV*, SV*);
346#endif
347static bool (*Perl_sv_upgrade)(pTHX_ SV*, U32);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200348#if (PERL_REVISION == 5) && (PERL_VERSION < 10)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349static SV*** (*Perl_Tstack_sp_ptr)(register PerlInterpreter*);
350static OP** (*Perl_Top_ptr)(register PerlInterpreter*);
351static SV*** (*Perl_Tstack_base_ptr)(register PerlInterpreter*);
352static SV*** (*Perl_Tstack_max_ptr)(register PerlInterpreter*);
353static I32* (*Perl_Ttmps_ix_ptr)(register PerlInterpreter*);
354static I32* (*Perl_Ttmps_floor_ptr)(register PerlInterpreter*);
355static I32** (*Perl_Tmarkstack_ptr_ptr)(register PerlInterpreter*);
356static I32** (*Perl_Tmarkstack_max_ptr)(register PerlInterpreter*);
357static SV** (*Perl_TSv_ptr)(register PerlInterpreter*);
358static XPV** (*Perl_TXpv_ptr)(register PerlInterpreter*);
359static STRLEN* (*Perl_Tna_ptr)(register PerlInterpreter*);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200360#else
Bram Moolenaar6b107212013-12-11 15:06:40 +0100361/* Perl-5.18 has a different Perl_sv_free2 signature. */
362# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
363static void (*Perl_sv_free2)(pTHX_ SV*, const U32);
364# else
Bram Moolenaarccf22172008-09-01 15:56:45 +0000365static void (*Perl_sv_free2)(pTHX_ SV*);
Bram Moolenaar6b107212013-12-11 15:06:40 +0100366# endif
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000367static void (*Perl_sys_init)(int* argc, char*** argv);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000368static void (*Perl_sys_term)(void);
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200369static void (*Perl_call_list)(pTHX_ I32, AV*);
370# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
371# else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000372static SV** (*Perl_ISv_ptr)(register PerlInterpreter*);
373static SV*** (*Perl_Istack_max_ptr)(register PerlInterpreter*);
374static SV*** (*Perl_Istack_base_ptr)(register PerlInterpreter*);
375static XPV** (*Perl_IXpv_ptr)(register PerlInterpreter*);
376static I32* (*Perl_Itmps_ix_ptr)(register PerlInterpreter*);
377static I32* (*Perl_Itmps_floor_ptr)(register PerlInterpreter*);
378static STRLEN* (*Perl_Ina_ptr)(register PerlInterpreter*);
379static I32** (*Perl_Imarkstack_ptr_ptr)(register PerlInterpreter*);
380static I32** (*Perl_Imarkstack_max_ptr)(register PerlInterpreter*);
381static SV*** (*Perl_Istack_sp_ptr)(register PerlInterpreter*);
382static OP** (*Perl_Iop_ptr)(register PerlInterpreter*);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000383static I32* (*Perl_Iscopestack_ix_ptr)(register PerlInterpreter*);
384static AV** (*Perl_Iunitcheckav_ptr)(register PerlInterpreter*);
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200385# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200388#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaar01c10522012-09-21 12:50:51 +0200389static perl_key* dll_PL_thr_key;
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200390#else
Bram Moolenaare06c1882010-07-21 22:05:20 +0200391static GV** (*Perl_Idefgv_ptr)(register PerlInterpreter*);
392static GV** (*Perl_Ierrgv_ptr)(register PerlInterpreter*);
393static SV* (*Perl_Isv_yes_ptr)(register PerlInterpreter*);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200394static perl_key* (*Perl_Gthr_key_ptr)_((pTHX));
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200395#endif
396static void (*boot_DynaLoader)_((pTHX_ CV*));
Bram Moolenaare06c1882010-07-21 22:05:20 +0200397
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398/*
399 * Table of name to function pointer of perl.
400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401static struct {
402 char* name;
403 PERL_PROC* ptr;
404} perl_funcname_table[] = {
405 {"perl_alloc", (PERL_PROC*)&perl_alloc},
406 {"perl_construct", (PERL_PROC*)&perl_construct},
407 {"perl_destruct", (PERL_PROC*)&perl_destruct},
408 {"perl_free", (PERL_PROC*)&perl_free},
409 {"perl_run", (PERL_PROC*)&perl_run},
410 {"perl_parse", (PERL_PROC*)&perl_parse},
411 {"Perl_get_context", (PERL_PROC*)&Perl_get_context},
412 {"Perl_croak", (PERL_PROC*)&Perl_croak},
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100413#ifdef PERL5101_OR_LATER
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100414 {"Perl_croak_xs_usage", (PERL_PROC*)&Perl_croak_xs_usage},
415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 {"Perl_croak_nocontext", (PERL_PROC*)&Perl_croak_nocontext},
417 {"Perl_dowantarray", (PERL_PROC*)&Perl_dowantarray},
418 {"Perl_free_tmps", (PERL_PROC*)&Perl_free_tmps},
419 {"Perl_gv_stashpv", (PERL_PROC*)&Perl_gv_stashpv},
420 {"Perl_markstack_grow", (PERL_PROC*)&Perl_markstack_grow},
421 {"Perl_mg_find", (PERL_PROC*)&Perl_mg_find},
422 {"Perl_newXS", (PERL_PROC*)&Perl_newXS},
423 {"Perl_newSV", (PERL_PROC*)&Perl_newSV},
424 {"Perl_newSViv", (PERL_PROC*)&Perl_newSViv},
425 {"Perl_newSVpv", (PERL_PROC*)&Perl_newSVpv},
426 {"Perl_call_argv", (PERL_PROC*)&Perl_call_argv},
427 {"Perl_call_pv", (PERL_PROC*)&Perl_call_pv},
428 {"Perl_eval_sv", (PERL_PROC*)&Perl_eval_sv},
429 {"Perl_get_sv", (PERL_PROC*)&Perl_get_sv},
430 {"Perl_eval_pv", (PERL_PROC*)&Perl_eval_pv},
431 {"Perl_call_method", (PERL_PROC*)&Perl_call_method},
432 {"Perl_pop_scope", (PERL_PROC*)&Perl_pop_scope},
433 {"Perl_push_scope", (PERL_PROC*)&Perl_push_scope},
434 {"Perl_save_int", (PERL_PROC*)&Perl_save_int},
435 {"Perl_stack_grow", (PERL_PROC*)&Perl_stack_grow},
436 {"Perl_set_context", (PERL_PROC*)&Perl_set_context},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200437#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
438 {"Perl_sv_2bool_flags", (PERL_PROC*)&Perl_sv_2bool_flags},
439 {"Perl_xs_apiversion_bootcheck",(PERL_PROC*)&Perl_xs_apiversion_bootcheck},
440#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 {"Perl_sv_2bool", (PERL_PROC*)&Perl_sv_2bool},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 {"Perl_sv_2iv", (PERL_PROC*)&Perl_sv_2iv},
444 {"Perl_sv_2mortal", (PERL_PROC*)&Perl_sv_2mortal},
445#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
446 {"Perl_sv_2pv_flags", (PERL_PROC*)&Perl_sv_2pv_flags},
447 {"Perl_sv_2pv_nolen", (PERL_PROC*)&Perl_sv_2pv_nolen},
448#else
449 {"Perl_sv_2pv", (PERL_PROC*)&Perl_sv_2pv},
450#endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000451#ifdef PERL589_OR_LATER
452 {"Perl_sv_2iv_flags", (PERL_PROC*)&Perl_sv_2iv_flags},
453 {"Perl_newXS_flags", (PERL_PROC*)&Perl_newXS_flags},
454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 {"Perl_sv_bless", (PERL_PROC*)&Perl_sv_bless},
456#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
457 {"Perl_sv_catpvn_flags", (PERL_PROC*)&Perl_sv_catpvn_flags},
458#else
459 {"Perl_sv_catpvn", (PERL_PROC*)&Perl_sv_catpvn},
460#endif
461 {"Perl_sv_free", (PERL_PROC*)&Perl_sv_free},
462 {"Perl_sv_isa", (PERL_PROC*)&Perl_sv_isa},
463 {"Perl_sv_magic", (PERL_PROC*)&Perl_sv_magic},
464 {"Perl_sv_setiv", (PERL_PROC*)&Perl_sv_setiv},
465 {"Perl_sv_setpv", (PERL_PROC*)&Perl_sv_setpv},
466 {"Perl_sv_setpvn", (PERL_PROC*)&Perl_sv_setpvn},
467#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
468 {"Perl_sv_setsv_flags", (PERL_PROC*)&Perl_sv_setsv_flags},
469#else
470 {"Perl_sv_setsv", (PERL_PROC*)&Perl_sv_setsv},
471#endif
472 {"Perl_sv_upgrade", (PERL_PROC*)&Perl_sv_upgrade},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000473#if (PERL_REVISION == 5) && (PERL_VERSION < 10)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 {"Perl_Tstack_sp_ptr", (PERL_PROC*)&Perl_Tstack_sp_ptr},
475 {"Perl_Top_ptr", (PERL_PROC*)&Perl_Top_ptr},
476 {"Perl_Tstack_base_ptr", (PERL_PROC*)&Perl_Tstack_base_ptr},
477 {"Perl_Tstack_max_ptr", (PERL_PROC*)&Perl_Tstack_max_ptr},
478 {"Perl_Ttmps_ix_ptr", (PERL_PROC*)&Perl_Ttmps_ix_ptr},
479 {"Perl_Ttmps_floor_ptr", (PERL_PROC*)&Perl_Ttmps_floor_ptr},
480 {"Perl_Tmarkstack_ptr_ptr", (PERL_PROC*)&Perl_Tmarkstack_ptr_ptr},
481 {"Perl_Tmarkstack_max_ptr", (PERL_PROC*)&Perl_Tmarkstack_max_ptr},
482 {"Perl_TSv_ptr", (PERL_PROC*)&Perl_TSv_ptr},
483 {"Perl_TXpv_ptr", (PERL_PROC*)&Perl_TXpv_ptr},
484 {"Perl_Tna_ptr", (PERL_PROC*)&Perl_Tna_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000485#else
Bram Moolenaarccf22172008-09-01 15:56:45 +0000486 {"Perl_sv_free2", (PERL_PROC*)&Perl_sv_free2},
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000487 {"Perl_sys_init", (PERL_PROC*)&Perl_sys_init},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000488 {"Perl_sys_term", (PERL_PROC*)&Perl_sys_term},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200489 {"Perl_call_list", (PERL_PROC*)&Perl_call_list},
490# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
491# else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000492 {"Perl_ISv_ptr", (PERL_PROC*)&Perl_ISv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000493 {"Perl_Istack_max_ptr", (PERL_PROC*)&Perl_Istack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200494 {"Perl_Istack_base_ptr", (PERL_PROC*)&Perl_Istack_base_ptr},
495 {"Perl_IXpv_ptr", (PERL_PROC*)&Perl_IXpv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000496 {"Perl_Itmps_ix_ptr", (PERL_PROC*)&Perl_Itmps_ix_ptr},
497 {"Perl_Itmps_floor_ptr", (PERL_PROC*)&Perl_Itmps_floor_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200498 {"Perl_Ina_ptr", (PERL_PROC*)&Perl_Ina_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000499 {"Perl_Imarkstack_ptr_ptr", (PERL_PROC*)&Perl_Imarkstack_ptr_ptr},
500 {"Perl_Imarkstack_max_ptr", (PERL_PROC*)&Perl_Imarkstack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200501 {"Perl_Istack_sp_ptr", (PERL_PROC*)&Perl_Istack_sp_ptr},
502 {"Perl_Iop_ptr", (PERL_PROC*)&Perl_Iop_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000503 {"Perl_Iscopestack_ix_ptr", (PERL_PROC*)&Perl_Iscopestack_ix_ptr},
504 {"Perl_Iunitcheckav_ptr", (PERL_PROC*)&Perl_Iunitcheckav_ptr},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200505# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000506#endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200507#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaar01c10522012-09-21 12:50:51 +0200508 {"PL_thr_key", (PERL_PROC*)&dll_PL_thr_key},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200509#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 {"Perl_Idefgv_ptr", (PERL_PROC*)&Perl_Idefgv_ptr},
511 {"Perl_Ierrgv_ptr", (PERL_PROC*)&Perl_Ierrgv_ptr},
512 {"Perl_Isv_yes_ptr", (PERL_PROC*)&Perl_Isv_yes_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200513 {"Perl_Gthr_key_ptr", (PERL_PROC*)&Perl_Gthr_key_ptr},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200514#endif
515 {"boot_DynaLoader", (PERL_PROC*)&boot_DynaLoader},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 {"", NULL},
517};
518
Bram Moolenaar6b107212013-12-11 15:06:40 +0100519/* Work around for perl-5.18.
520 * The definitions of S_SvREFCNT_inc and S_SvREFCNT_dec are needed, so include
521 * "perl\lib\CORE\inline.h", after Perl_sv_free2 is defined.
522 * The linker won't complain about undefined __impl_Perl_sv_free2. */
523#if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
524# include <inline.h>
525#endif
526
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527/*
528 * Make all runtime-links of perl.
529 *
Bram Moolenaar364ab2f2013-08-02 20:05:32 +0200530 * 1. Get module handle using dlopen() or vimLoadLib().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 * 2. Get pointer to perl function by GetProcAddress.
532 * 3. Repeat 2, until get all functions will be used.
533 *
534 * Parameter 'libname' provides name of DLL.
535 * Return OK or FAIL.
536 */
537 static int
538perl_runtime_link_init(char *libname, int verbose)
539{
540 int i;
541
542 if (hPerlLib != NULL)
543 return OK;
Bram Moolenaare06c1882010-07-21 22:05:20 +0200544 if ((hPerlLib = load_dll(libname)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 {
546 if (verbose)
547 EMSG2(_("E370: Could not load library %s"), libname);
548 return FAIL;
549 }
550 for (i = 0; perl_funcname_table[i].ptr; ++i)
551 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200552 if (!(*perl_funcname_table[i].ptr = symbol_from_dll(hPerlLib,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 perl_funcname_table[i].name)))
554 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200555 close_dll(hPerlLib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 hPerlLib = NULL;
557 if (verbose)
558 EMSG2(_(e_loadfunc), perl_funcname_table[i].name);
559 return FAIL;
560 }
561 }
562 return OK;
563}
564
565/*
566 * If runtime-link-perl(DLL) was loaded successfully, return TRUE.
567 * There were no DLL loaded, return FALSE.
568 */
569 int
570perl_enabled(verbose)
571 int verbose;
572{
573 return perl_runtime_link_init(DYNAMIC_PERL_DLL, verbose) == OK;
574}
575#endif /* DYNAMIC_PERL */
576
577/*
578 * perl_init(): initialize perl interpreter
579 * We have to call perl_parse to initialize some structures,
580 * there's nothing to actually parse.
581 */
582 static void
583perl_init()
584{
Bram Moolenaarc236c162008-07-13 17:41:49 +0000585 char *bootargs[] = { "VI", NULL };
586 int argc = 3;
587 static char *argv[] = { "", "-e", "" };
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588
Bram Moolenaarc236c162008-07-13 17:41:49 +0000589#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000590 Perl_sys_init(&argc, (char***)&argv);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 perl_interp = perl_alloc();
593 perl_construct(perl_interp);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000594 perl_parse(perl_interp, xs_init, argc, argv, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 perl_call_argv("VIM::bootstrap", (long)G_DISCARD, bootargs);
596 VIM_init();
597#ifdef USE_SFIO
598 sfdisc(PerlIO_stdout(), sfdcnewvim());
599 sfdisc(PerlIO_stderr(), sfdcnewvim());
600 sfsetbuf(PerlIO_stdout(), NULL, 0);
601 sfsetbuf(PerlIO_stderr(), NULL, 0);
602#endif
603}
604
605/*
606 * perl_end(): clean up after ourselves
607 */
608 void
609perl_end()
610{
611 if (perl_interp)
612 {
613 perl_run(perl_interp);
614 perl_destruct(perl_interp);
615 perl_free(perl_interp);
616 perl_interp = NULL;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000617#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
618 Perl_sys_term();
619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 }
621#ifdef DYNAMIC_PERL
622 if (hPerlLib)
623 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200624 close_dll(hPerlLib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 hPerlLib = NULL;
626 }
627#endif
628}
629
630/*
631 * msg_split(): send a message to the message handling routines
632 * split at '\n' first though.
633 */
634 void
635msg_split(s, attr)
636 char_u *s;
637 int attr; /* highlighting attributes */
638{
639 char *next;
640 char *token = (char *)s;
641
Bram Moolenaaraa8494a2007-10-09 08:47:27 +0000642 while ((next = strchr(token, '\n')) && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 {
644 *next++ = '\0'; /* replace \n with \0 */
645 msg_attr((char_u *)token, attr);
646 token = next;
647 }
Bram Moolenaaraa8494a2007-10-09 08:47:27 +0000648 if (*token && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 msg_attr((char_u *)token, attr);
650}
651
652#ifndef FEAT_EVAL
653/*
654 * This stub is needed because an "#ifdef FEAT_EVAL" around Eval() doesn't
655 * work properly.
656 */
657 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000658eval_to_string(arg, nextcmd, dolist)
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100659 char_u *arg UNUSED;
660 char_u **nextcmd UNUSED;
661 int dolist UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662{
663 return NULL;
664}
665#endif
666
667/*
668 * Create a new reference to an SV pointing to the SCR structure
Bram Moolenaare344bea2005-09-01 20:46:49 +0000669 * The b_perl_private/w_perl_private part of the SCR structure points to the
670 * SV, so there can only be one such SV for a particular SCR structure. When
671 * the last reference has gone (DESTROY is called),
672 * b_perl_private/w_perl_private is reset; When the screen goes away before
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 * all references are gone, the value of the SV is reset;
674 * any subsequent use of any of those reference will produce
675 * a warning. (see typemap)
676 */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000677
678 static SV *
679newWINrv(rv, ptr)
680 SV *rv;
681 win_T *ptr;
682{
683 sv_upgrade(rv, SVt_RV);
684 if (ptr->w_perl_private == NULL)
685 {
686 ptr->w_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +0100687 sv_setiv(ptr->w_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +0000688 }
689 else
690 SvREFCNT_inc(ptr->w_perl_private);
691 SvRV(rv) = ptr->w_perl_private;
692 SvROK_on(rv);
693 return sv_bless(rv, gv_stashpv("VIWIN", TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694}
695
Bram Moolenaare344bea2005-09-01 20:46:49 +0000696 static SV *
697newBUFrv(rv, ptr)
698 SV *rv;
699 buf_T *ptr;
700{
701 sv_upgrade(rv, SVt_RV);
702 if (ptr->b_perl_private == NULL)
703 {
704 ptr->b_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +0100705 sv_setiv(ptr->b_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +0000706 }
707 else
708 SvREFCNT_inc(ptr->b_perl_private);
709 SvRV(rv) = ptr->b_perl_private;
710 SvROK_on(rv);
711 return sv_bless(rv, gv_stashpv("VIBUF", TRUE));
712}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713
714/*
715 * perl_win_free
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200716 * Remove all references to the window to be destroyed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 */
718 void
719perl_win_free(wp)
720 win_T *wp;
721{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000722 if (wp->w_perl_private)
723 sv_setiv((SV *)wp->w_perl_private, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 return;
725}
726
727 void
728perl_buf_free(bp)
729 buf_T *bp;
730{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000731 if (bp->b_perl_private)
732 sv_setiv((SV *)bp->b_perl_private, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 return;
734}
735
736#ifndef PROTO
737# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
738I32 cur_val(pTHX_ IV iv, SV *sv);
739# else
740I32 cur_val(IV iv, SV *sv);
741#endif
742
743/*
744 * Handler for the magic variables $main::curwin and $main::curbuf.
745 * The handler is put into the magic vtbl for these variables.
746 * (This is effectively a C-level equivalent of a tied variable).
747 * There is no "set" function as the variables are read-only.
748 */
749# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
750I32 cur_val(pTHX_ IV iv, SV *sv)
751# else
752I32 cur_val(IV iv, SV *sv)
753# endif
754{
755 SV *rv;
756 if (iv == 0)
757 rv = newWINrv(newSV(0), curwin);
758 else
759 rv = newBUFrv(newSV(0), curbuf);
760 sv_setsv(sv, rv);
761 return 0;
762}
763#endif /* !PROTO */
764
765struct ufuncs cw_funcs = { cur_val, 0, 0 };
766struct ufuncs cb_funcs = { cur_val, 0, 1 };
767
768/*
769 * VIM_init(): Vim-specific initialisation.
770 * Make the magical main::curwin and main::curbuf variables
771 */
772 static void
773VIM_init()
774{
775 static char cw[] = "main::curwin";
776 static char cb[] = "main::curbuf";
777 SV *sv;
778
779 sv = perl_get_sv(cw, TRUE);
780 sv_magic(sv, NULL, 'U', (char *)&cw_funcs, sizeof(cw_funcs));
781 SvREADONLY_on(sv);
782
783 sv = perl_get_sv(cb, TRUE);
784 sv_magic(sv, NULL, 'U', (char *)&cb_funcs, sizeof(cb_funcs));
785 SvREADONLY_on(sv);
786
787 /*
788 * Setup the Safe compartment.
789 * It shouldn't be a fatal error if the Safe module is missing.
790 * XXX: Only shares the 'Msg' routine (which has to be called
791 * like 'Msg(...)').
792 */
793 (void)perl_eval_pv( "if ( eval( 'require Safe' ) ) { $VIM::safe = Safe->new(); $VIM::safe->share_from( 'VIM', ['Msg'] ); }", G_DISCARD | G_VOID );
794
795}
796
797#ifdef DYNAMIC_PERL
798static char *e_noperl = N_("Sorry, this command is disabled: the Perl library could not be loaded.");
799#endif
800
801/*
802 * ":perl"
803 */
804 void
805ex_perl(eap)
806 exarg_T *eap;
807{
808 char *err;
809 char *script;
810 STRLEN length;
811 SV *sv;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200812#ifdef HAVE_SANDBOX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 SV *safe;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815
816 script = (char *)script_get(eap, eap->arg);
817 if (eap->skip)
818 {
819 vim_free(script);
820 return;
821 }
822
823 if (perl_interp == NULL)
824 {
825#ifdef DYNAMIC_PERL
826 if (!perl_enabled(TRUE))
827 {
828 EMSG(_(e_noperl));
829 vim_free(script);
830 return;
831 }
832#endif
833 perl_init();
834 }
835
836 {
837 dSP;
838 ENTER;
839 SAVETMPS;
840
841 if (script == NULL)
842 sv = newSVpv((char *)eap->arg, 0);
843 else
844 {
845 sv = newSVpv(script, 0);
846 vim_free(script);
847 }
848
849#ifdef HAVE_SANDBOX
850 if (sandbox)
851 {
Bram Moolenaara1711622011-07-27 14:15:46 +0200852 safe = perl_get_sv("VIM::safe", FALSE);
Bram Moolenaar3f947ea2009-07-14 14:04:54 +0000853# ifndef MAKE_TEST /* avoid a warning for unreachable code */
Bram Moolenaar954e8c52009-11-11 13:45:33 +0000854 if (safe == NULL || !SvTRUE(safe))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 EMSG(_("E299: Perl evaluation forbidden in sandbox without the Safe module"));
856 else
Bram Moolenaar3f947ea2009-07-14 14:04:54 +0000857# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 {
859 PUSHMARK(SP);
860 XPUSHs(safe);
861 XPUSHs(sv);
862 PUTBACK;
863 perl_call_method("reval", G_DISCARD);
864 }
865 }
866 else
867#endif
868 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
869
870 SvREFCNT_dec(sv);
871
Bram Moolenaarc271c482012-08-08 13:17:31 +0200872#ifdef AVOID_PL_ERRGV
873 err = SvPV(perl_get_sv("@", GV_ADD), length);
874#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 err = SvPV(GvSV(PL_errgv), length);
Bram Moolenaarc271c482012-08-08 13:17:31 +0200876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877
878 FREETMPS;
879 LEAVE;
880
881 if (!length)
882 return;
883
884 msg_split((char_u *)err, highlight_attr[HLF_E]);
885 return;
886 }
887}
888
889 static int
890replace_line(line, end)
891 linenr_T *line, *end;
892{
893 char *str;
894
895 if (SvOK(GvSV(PL_defgv)))
896 {
897 str = SvPV(GvSV(PL_defgv), PL_na);
898 ml_replace(*line, (char_u *)str, 1);
899 changed_bytes(*line, 0);
900 }
901 else
902 {
903 ml_delete(*line, FALSE);
904 deleted_lines_mark(*line, 1L);
905 --(*end);
906 --(*line);
907 }
908 return OK;
909}
910
911/*
912 * ":perldo".
913 */
914 void
915ex_perldo(eap)
916 exarg_T *eap;
917{
918 STRLEN length;
919 SV *sv;
920 char *str;
921 linenr_T i;
922
923 if (bufempty())
924 return;
925
926 if (perl_interp == NULL)
927 {
928#ifdef DYNAMIC_PERL
929 if (!perl_enabled(TRUE))
930 {
931 EMSG(_(e_noperl));
932 return;
933 }
934#endif
935 perl_init();
936 }
937 {
938 dSP;
939 length = strlen((char *)eap->arg);
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000940 sv = newSV(length + sizeof("sub VIM::perldo {") - 1 + 1);
941 sv_setpvn(sv, "sub VIM::perldo {", sizeof("sub VIM::perldo {") - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 sv_catpvn(sv, (char *)eap->arg, length);
943 sv_catpvn(sv, "}", 1);
944 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
945 SvREFCNT_dec(sv);
Bram Moolenaarc271c482012-08-08 13:17:31 +0200946#ifdef AVOID_PL_ERRGV
947 str = SvPV(perl_get_sv("@", GV_ADD), length);
948#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 str = SvPV(GvSV(PL_errgv), length);
Bram Moolenaarc271c482012-08-08 13:17:31 +0200950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 if (length)
952 goto err;
953
954 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
955 return;
956
957 ENTER;
958 SAVETMPS;
959 for (i = eap->line1; i <= eap->line2; i++)
960 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000961 sv_setpv(GvSV(PL_defgv), (char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 PUSHMARK(sp);
963 perl_call_pv("VIM::perldo", G_SCALAR | G_EVAL);
Bram Moolenaarc271c482012-08-08 13:17:31 +0200964#ifdef AVOID_PL_ERRGV
965 str = SvPV(perl_get_sv("@", GV_ADD), length);
966#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 str = SvPV(GvSV(PL_errgv), length);
Bram Moolenaarc271c482012-08-08 13:17:31 +0200968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 if (length)
970 break;
971 SPAGAIN;
972 if (SvTRUEx(POPs))
973 {
974 if (replace_line(&i, &eap->line2) != OK)
975 {
976 PUTBACK;
977 break;
978 }
979 }
980 PUTBACK;
981 }
982 FREETMPS;
983 LEAVE;
984 check_cursor();
985 update_screen(NOT_VALID);
986 if (!length)
987 return;
988
989err:
990 msg_split((char_u *)str, highlight_attr[HLF_E]);
991 return;
992 }
993}
994
Bram Moolenaar01dd60c2008-07-24 14:24:48 +0000995#ifndef FEAT_WINDOWS
996int win_valid(win_T *w) { return TRUE; }
997int win_count() { return 1; }
998win_T *win_find_nr(int n) { return curwin; }
999#endif
1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001XS(boot_VIM);
1002
1003 static void
1004xs_init(pTHX)
1005{
1006 char *file = __FILE__;
1007
1008 /* DynaLoader is a special case */
1009 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1010 newXS("VIM::bootstrap", boot_VIM, file);
1011}
1012
1013typedef win_T * VIWIN;
1014typedef buf_T * VIBUF;
1015
1016MODULE = VIM PACKAGE = VIM
1017
1018void
1019Msg(text, hl=NULL)
1020 char *text;
1021 char *hl;
1022
1023 PREINIT:
1024 int attr;
1025 int id;
1026
1027 PPCODE:
1028 if (text != NULL)
1029 {
1030 attr = 0;
1031 if (hl != NULL)
1032 {
1033 id = syn_name2id((char_u *)hl);
1034 if (id != 0)
1035 attr = syn_id2attr(id);
1036 }
1037 msg_split((char_u *)text, attr);
1038 }
1039
1040void
1041SetOption(line)
1042 char *line;
1043
1044 PPCODE:
1045 if (line != NULL)
1046 do_set((char_u *)line, 0);
1047 update_screen(NOT_VALID);
1048
1049void
1050DoCommand(line)
1051 char *line;
1052
1053 PPCODE:
1054 if (line != NULL)
1055 do_cmdline_cmd((char_u *)line);
1056
1057void
1058Eval(str)
1059 char *str;
1060
1061 PREINIT:
1062 char_u *value;
1063 PPCODE:
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001064 value = eval_to_string((char_u *)str, (char_u **)0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 if (value == NULL)
1066 {
1067 XPUSHs(sv_2mortal(newSViv(0)));
1068 XPUSHs(sv_2mortal(newSVpv("", 0)));
1069 }
1070 else
1071 {
1072 XPUSHs(sv_2mortal(newSViv(1)));
1073 XPUSHs(sv_2mortal(newSVpv((char *)value, 0)));
1074 vim_free(value);
1075 }
1076
1077void
1078Buffers(...)
1079
1080 PREINIT:
1081 buf_T *vimbuf;
1082 int i, b;
1083
1084 PPCODE:
1085 if (items == 0)
1086 {
1087 if (GIMME == G_SCALAR)
1088 {
1089 i = 0;
1090 for (vimbuf = firstbuf; vimbuf; vimbuf = vimbuf->b_next)
1091 ++i;
1092
1093 XPUSHs(sv_2mortal(newSViv(i)));
1094 }
1095 else
1096 {
1097 for (vimbuf = firstbuf; vimbuf; vimbuf = vimbuf->b_next)
1098 XPUSHs(newBUFrv(newSV(0), vimbuf));
1099 }
1100 }
1101 else
1102 {
1103 for (i = 0; i < items; i++)
1104 {
1105 SV *sv = ST(i);
1106 if (SvIOK(sv))
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001107 b = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 else
1109 {
1110 char_u *pat;
1111 STRLEN len;
1112
1113 pat = (char_u *)SvPV(sv, len);
1114 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001115 b = buflist_findpat(pat, pat+len, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 --emsg_off;
1117 }
1118
1119 if (b >= 0)
1120 {
1121 vimbuf = buflist_findnr(b);
1122 if (vimbuf)
1123 XPUSHs(newBUFrv(newSV(0), vimbuf));
1124 }
1125 }
1126 }
1127
1128void
1129Windows(...)
1130
1131 PREINIT:
1132 win_T *vimwin;
1133 int i, w;
1134
1135 PPCODE:
1136 if (items == 0)
1137 {
1138 if (GIMME == G_SCALAR)
1139 XPUSHs(sv_2mortal(newSViv(win_count())));
1140 else
1141 {
1142 for (vimwin = firstwin; vimwin != NULL; vimwin = W_NEXT(vimwin))
1143 XPUSHs(newWINrv(newSV(0), vimwin));
1144 }
1145 }
1146 else
1147 {
1148 for (i = 0; i < items; i++)
1149 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001150 w = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 vimwin = win_find_nr(w);
1152 if (vimwin)
1153 XPUSHs(newWINrv(newSV(0), vimwin));
1154 }
1155 }
1156
1157MODULE = VIM PACKAGE = VIWIN
1158
1159void
1160DESTROY(win)
1161 VIWIN win
1162
1163 CODE:
1164 if (win_valid(win))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001165 win->w_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166
1167SV *
1168Buffer(win)
1169 VIWIN win
1170
1171 CODE:
1172 if (!win_valid(win))
1173 win = curwin;
1174 RETVAL = newBUFrv(newSV(0), win->w_buffer);
1175 OUTPUT:
1176 RETVAL
1177
1178void
1179SetHeight(win, height)
1180 VIWIN win
1181 int height;
1182
1183 PREINIT:
1184 win_T *savewin;
1185
1186 PPCODE:
1187 if (!win_valid(win))
1188 win = curwin;
1189 savewin = curwin;
1190 curwin = win;
1191 win_setheight(height);
1192 curwin = savewin;
1193
1194void
1195Cursor(win, ...)
1196 VIWIN win
1197
1198 PPCODE:
Bram Moolenaara1711622011-07-27 14:15:46 +02001199 if (items == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 {
1201 EXTEND(sp, 2);
1202 if (!win_valid(win))
1203 win = curwin;
1204 PUSHs(sv_2mortal(newSViv(win->w_cursor.lnum)));
1205 PUSHs(sv_2mortal(newSViv(win->w_cursor.col)));
1206 }
Bram Moolenaara1711622011-07-27 14:15:46 +02001207 else if (items == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 {
1209 int lnum, col;
1210
1211 if (!win_valid(win))
1212 win = curwin;
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001213 lnum = (int) SvIV(ST(1));
1214 col = (int) SvIV(ST(2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 win->w_cursor.lnum = lnum;
1216 win->w_cursor.col = col;
1217 check_cursor(); /* put cursor on an existing line */
1218 update_screen(NOT_VALID);
1219 }
1220
1221MODULE = VIM PACKAGE = VIBUF
1222
1223void
1224DESTROY(vimbuf)
1225 VIBUF vimbuf;
1226
1227 CODE:
1228 if (buf_valid(vimbuf))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001229 vimbuf->b_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230
1231void
1232Name(vimbuf)
1233 VIBUF vimbuf;
1234
1235 PPCODE:
1236 if (!buf_valid(vimbuf))
1237 vimbuf = curbuf;
1238 /* No file name returns an empty string */
1239 if (vimbuf->b_fname == NULL)
1240 XPUSHs(sv_2mortal(newSVpv("", 0)));
1241 else
1242 XPUSHs(sv_2mortal(newSVpv((char *)vimbuf->b_fname, 0)));
1243
1244void
1245Number(vimbuf)
1246 VIBUF vimbuf;
1247
1248 PPCODE:
1249 if (!buf_valid(vimbuf))
1250 vimbuf = curbuf;
1251 XPUSHs(sv_2mortal(newSViv(vimbuf->b_fnum)));
1252
1253void
1254Count(vimbuf)
1255 VIBUF vimbuf;
1256
1257 PPCODE:
1258 if (!buf_valid(vimbuf))
1259 vimbuf = curbuf;
1260 XPUSHs(sv_2mortal(newSViv(vimbuf->b_ml.ml_line_count)));
1261
1262void
1263Get(vimbuf, ...)
1264 VIBUF vimbuf;
1265
1266 PREINIT:
1267 char_u *line;
1268 int i;
1269 long lnum;
1270 PPCODE:
1271 if (buf_valid(vimbuf))
1272 {
1273 for (i = 1; i < items; i++)
1274 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001275 lnum = (long) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1277 {
1278 line = ml_get_buf(vimbuf, lnum, FALSE);
1279 XPUSHs(sv_2mortal(newSVpv((char *)line, 0)));
1280 }
1281 }
1282 }
1283
1284void
1285Set(vimbuf, ...)
1286 VIBUF vimbuf;
1287
1288 PREINIT:
1289 int i;
1290 long lnum;
1291 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 PPCODE:
1293 if (buf_valid(vimbuf))
1294 {
1295 if (items < 3)
1296 croak("Usage: VIBUF::Set(vimbuf, lnum, @lines)");
1297
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001298 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 for(i = 2; i < items; i++, lnum++)
1300 {
1301 line = SvPV(ST(i),PL_na);
1302 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1303 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001304 aco_save_T aco;
1305
1306 /* set curwin/curbuf for "vimbuf" and save some things */
1307 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001308
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 if (u_savesub(lnum) == OK)
1310 {
1311 ml_replace(lnum, (char_u *)line, TRUE);
1312 changed_bytes(lnum, 0);
1313 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001314
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001315 /* restore curwin/curbuf and a few other things */
1316 aucmd_restbuf(&aco);
1317 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 }
1319 }
1320 }
1321
1322void
1323Delete(vimbuf, ...)
1324 VIBUF vimbuf;
1325
1326 PREINIT:
1327 long i, lnum = 0, count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 PPCODE:
1329 if (buf_valid(vimbuf))
1330 {
1331 if (items == 2)
1332 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001333 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 count = 1;
1335 }
1336 else if (items == 3)
1337 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001338 lnum = (long) SvIV(ST(1));
1339 count = (long) 1 + SvIV(ST(2)) - lnum;
Bram Moolenaara1711622011-07-27 14:15:46 +02001340 if (count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 count = 1;
Bram Moolenaara1711622011-07-27 14:15:46 +02001342 if (count < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 {
1344 lnum -= count;
1345 count = -count;
1346 }
1347 }
1348 if (items >= 2)
1349 {
1350 for (i = 0; i < count; i++)
1351 {
1352 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1353 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001354 aco_save_T aco;
1355
1356 /* set curwin/curbuf for "vimbuf" and save some things */
1357 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001358
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 if (u_savedel(lnum, 1) == OK)
1360 {
1361 ml_delete(lnum, 0);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00001362 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 deleted_lines_mark(lnum, 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001365
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001366 /* restore curwin/curbuf and a few other things */
1367 aucmd_restbuf(&aco);
1368 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001369
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 update_curbuf(VALID);
1371 }
1372 }
1373 }
1374 }
1375
1376void
1377Append(vimbuf, ...)
1378 VIBUF vimbuf;
1379
1380 PREINIT:
1381 int i;
1382 long lnum;
1383 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 PPCODE:
1385 if (buf_valid(vimbuf))
1386 {
1387 if (items < 3)
1388 croak("Usage: VIBUF::Append(vimbuf, lnum, @lines)");
1389
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001390 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 for (i = 2; i < items; i++, lnum++)
1392 {
1393 line = SvPV(ST(i),PL_na);
1394 if (lnum >= 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1395 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001396 aco_save_T aco;
1397
1398 /* set curwin/curbuf for "vimbuf" and save some things */
1399 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001400
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 if (u_inssub(lnum + 1) == OK)
1402 {
1403 ml_append(lnum, (char_u *)line, (colnr_T)0, FALSE);
1404 appended_lines_mark(lnum, 1L);
1405 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001406
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001407 /* restore curwin/curbuf and a few other things */
1408 aucmd_restbuf(&aco);
1409 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001410
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 update_curbuf(VALID);
1412 }
1413 }
1414 }
1415