blob: 5a7514798b399484c2cbb669a412df00b973ccf2 [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 Moolenaaraee1f4a2013-08-02 16:10:32 +020047#include <EXTERN.h>
48#include <perl.h>
49#include <XSUB.h>
50
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
52/*
53 * Work around clashes between Perl and Vim namespace. proto.h doesn't
54 * include if_perl.pro and perlsfio.pro when IN_PERL_FILE is defined, because
55 * we need the CV typedef. proto.h can't be moved to after including
56 * if_perl.h, because we get all sorts of name clashes then.
57 */
58#ifndef PROTO
59#ifndef __MINGW32__
60# include "proto/if_perl.pro"
61# include "proto/if_perlsfio.pro"
62#endif
63#endif
64
65/* Perl compatibility stuff. This should ensure compatibility with older
66 * versions of Perl.
67 */
68
69#ifndef PERL_VERSION
70# include <patchlevel.h>
71# define PERL_REVISION 5
72# define PERL_VERSION PATCHLEVEL
73# define PERL_SUBVERSION SUBVERSION
74#endif
75
Bram Moolenaar700d1d72007-09-13 13:20:16 +000076/*
77 * Quoting Jan Dubois of Active State:
78 * ActivePerl build 822 still identifies itself as 5.8.8 but already
79 * contains many of the changes from the upcoming Perl 5.8.9 release.
80 *
81 * The changes include addition of two symbols (Perl_sv_2iv_flags,
82 * Perl_newXS_flags) not present in earlier releases.
83 *
Bram Moolenaar3b9b13e2007-09-15 12:49:35 +000084 * Jan Dubois suggested the following guarding scheme.
85 *
86 * Active State defined ACTIVEPERL_VERSION as a string in versions before
87 * 5.8.8; and so the comparison to 822 below needs to be guarded.
Bram Moolenaar700d1d72007-09-13 13:20:16 +000088 */
Bram Moolenaar3b9b13e2007-09-15 12:49:35 +000089#if (PERL_REVISION == 5) && (PERL_VERSION == 8) && (PERL_SUBVERSION >= 8)
90# if (ACTIVEPERL_VERSION >= 822) || (PERL_SUBVERSION >= 9)
91# define PERL589_OR_LATER
92# endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +000093#endif
94#if (PERL_REVISION == 5) && (PERL_VERSION >= 9)
95# define PERL589_OR_LATER
96#endif
97
Bram Moolenaar58cb0892010-03-02 15:14:33 +010098#if (PERL_REVISION == 5) && ((PERL_VERSION > 10) || \
99 (PERL_VERSION == 10) && (PERL_SUBVERSION >= 1))
100# define PERL5101_OR_LATER
101#endif
102
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103#ifndef pTHX
104# define pTHX void
105# define pTHX_
106#endif
107
108#ifndef EXTERN_C
109# define EXTERN_C
110#endif
111
Bram Moolenaarc271c482012-08-08 13:17:31 +0200112#if (PERL_REVISION == 5) && (PERL_VERSION >= 14) && defined(_MSC_VER)
113/* Using PL_errgv to get the error message after perl_eval_sv() causes a crash
114 * with MSVC and Perl version 5.14. */
115# define AVOID_PL_ERRGV
116#endif
117
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118/* Compatibility hacks over */
119
120static PerlInterpreter *perl_interp = NULL;
121static void xs_init __ARGS((pTHX));
122static void VIM_init __ARGS((void));
123EXTERN_C void boot_DynaLoader __ARGS((pTHX_ CV*));
124
125/*
Bram Moolenaare06c1882010-07-21 22:05:20 +0200126 * For dynamic linked perl.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127 */
128#if defined(DYNAMIC_PERL) || defined(PROTO)
Bram Moolenaare06c1882010-07-21 22:05:20 +0200129
130#ifndef DYNAMIC_PERL /* just generating prototypes */
Bram Moolenaar766fb0d2010-07-22 11:34:16 +0200131#ifdef WIN3264
Bram Moolenaare06c1882010-07-21 22:05:20 +0200132typedef int HANDLE;
133#endif
134typedef int XSINIT_t;
135typedef int XSUBADDR_t;
136typedef int perl_key;
137#endif
138
Bram Moolenaar766fb0d2010-07-22 11:34:16 +0200139#ifndef WIN3264
Bram Moolenaare06c1882010-07-21 22:05:20 +0200140#include <dlfcn.h>
141#define HANDLE void*
142#define PERL_PROC void*
143#define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
144#define symbol_from_dll dlsym
145#define close_dll dlclose
146#else
147#define PERL_PROC FARPROC
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200148#define load_dll vimLoadLib
Bram Moolenaare06c1882010-07-21 22:05:20 +0200149#define symbol_from_dll GetProcAddress
150#define close_dll FreeLibrary
151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152/*
153 * Wrapper defines
154 */
155# define perl_alloc dll_perl_alloc
156# define perl_construct dll_perl_construct
157# define perl_parse dll_perl_parse
158# define perl_run dll_perl_run
159# define perl_destruct dll_perl_destruct
160# define perl_free dll_perl_free
161# define Perl_get_context dll_Perl_get_context
162# define Perl_croak dll_Perl_croak
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100163# ifdef PERL5101_OR_LATER
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100164# define Perl_croak_xs_usage dll_Perl_croak_xs_usage
165# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166# ifndef PROTO
167# define Perl_croak_nocontext dll_Perl_croak_nocontext
168# define Perl_call_argv dll_Perl_call_argv
169# define Perl_call_pv dll_Perl_call_pv
170# define Perl_eval_sv dll_Perl_eval_sv
171# define Perl_get_sv dll_Perl_get_sv
172# define Perl_eval_pv dll_Perl_eval_pv
173# define Perl_call_method dll_Perl_call_method
174# endif
175# define Perl_dowantarray dll_Perl_dowantarray
176# define Perl_free_tmps dll_Perl_free_tmps
177# define Perl_gv_stashpv dll_Perl_gv_stashpv
178# define Perl_markstack_grow dll_Perl_markstack_grow
179# define Perl_mg_find dll_Perl_mg_find
180# define Perl_newXS dll_Perl_newXS
181# define Perl_newSV dll_Perl_newSV
182# define Perl_newSViv dll_Perl_newSViv
183# define Perl_newSVpv dll_Perl_newSVpv
184# define Perl_pop_scope dll_Perl_pop_scope
185# define Perl_push_scope dll_Perl_push_scope
186# define Perl_save_int dll_Perl_save_int
187# define Perl_stack_grow dll_Perl_stack_grow
188# define Perl_set_context dll_Perl_set_context
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200189# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
190# define Perl_sv_2bool_flags dll_Perl_sv_2bool_flags
Bram Moolenaar01c10522012-09-21 12:50:51 +0200191# define Perl_xs_apiversion_bootcheck dll_Perl_xs_apiversion_bootcheck
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200192# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193# define Perl_sv_2bool dll_Perl_sv_2bool
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200194# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195# define Perl_sv_2iv dll_Perl_sv_2iv
196# define Perl_sv_2mortal dll_Perl_sv_2mortal
197# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
198# define Perl_sv_2pv_flags dll_Perl_sv_2pv_flags
199# define Perl_sv_2pv_nolen dll_Perl_sv_2pv_nolen
200# else
201# define Perl_sv_2pv dll_Perl_sv_2pv
202# endif
203# define Perl_sv_bless dll_Perl_sv_bless
204# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
205# define Perl_sv_catpvn_flags dll_Perl_sv_catpvn_flags
206# else
207# define Perl_sv_catpvn dll_Perl_sv_catpvn
208# endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000209#ifdef PERL589_OR_LATER
210# define Perl_sv_2iv_flags dll_Perl_sv_2iv_flags
211# define Perl_newXS_flags dll_Perl_newXS_flags
212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213# define Perl_sv_free dll_Perl_sv_free
Bram Moolenaarccf22172008-09-01 15:56:45 +0000214# if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
215# define Perl_sv_free2 dll_Perl_sv_free2
216# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217# define Perl_sv_isa dll_Perl_sv_isa
218# define Perl_sv_magic dll_Perl_sv_magic
219# define Perl_sv_setiv dll_Perl_sv_setiv
220# define Perl_sv_setpv dll_Perl_sv_setpv
221# define Perl_sv_setpvn dll_Perl_sv_setpvn
222# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
223# define Perl_sv_setsv_flags dll_Perl_sv_setsv_flags
224# else
225# define Perl_sv_setsv dll_Perl_sv_setsv
226# endif
227# define Perl_sv_upgrade dll_Perl_sv_upgrade
228# define Perl_Tstack_sp_ptr dll_Perl_Tstack_sp_ptr
229# define Perl_Top_ptr dll_Perl_Top_ptr
230# define Perl_Tstack_base_ptr dll_Perl_Tstack_base_ptr
231# define Perl_Tstack_max_ptr dll_Perl_Tstack_max_ptr
232# define Perl_Ttmps_ix_ptr dll_Perl_Ttmps_ix_ptr
233# define Perl_Ttmps_floor_ptr dll_Perl_Ttmps_floor_ptr
234# define Perl_Tmarkstack_ptr_ptr dll_Perl_Tmarkstack_ptr_ptr
235# define Perl_Tmarkstack_max_ptr dll_Perl_Tmarkstack_max_ptr
236# define Perl_TSv_ptr dll_Perl_TSv_ptr
237# define Perl_TXpv_ptr dll_Perl_TXpv_ptr
238# define Perl_Tna_ptr dll_Perl_Tna_ptr
239# define Perl_Idefgv_ptr dll_Perl_Idefgv_ptr
240# define Perl_Ierrgv_ptr dll_Perl_Ierrgv_ptr
241# define Perl_Isv_yes_ptr dll_Perl_Isv_yes_ptr
242# define boot_DynaLoader dll_boot_DynaLoader
Bram Moolenaare06c1882010-07-21 22:05:20 +0200243# define Perl_Gthr_key_ptr dll_Perl_Gthr_key_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000245# define Perl_sys_init dll_Perl_sys_init
Bram Moolenaarc236c162008-07-13 17:41:49 +0000246# define Perl_sys_term dll_Perl_sys_term
247# define Perl_ISv_ptr dll_Perl_ISv_ptr
248# define Perl_Istack_max_ptr dll_Perl_Istack_max_ptr
249# define Perl_Istack_base_ptr dll_Perl_Istack_base_ptr
250# define Perl_Itmps_ix_ptr dll_Perl_Itmps_ix_ptr
251# define Perl_Itmps_floor_ptr dll_Perl_Itmps_floor_ptr
252# define Perl_IXpv_ptr dll_Perl_IXpv_ptr
253# define Perl_Ina_ptr dll_Perl_Ina_ptr
254# define Perl_Imarkstack_ptr_ptr dll_Perl_Imarkstack_ptr_ptr
255# define Perl_Imarkstack_max_ptr dll_Perl_Imarkstack_max_ptr
256# define Perl_Istack_sp_ptr dll_Perl_Istack_sp_ptr
257# define Perl_Iop_ptr dll_Perl_Iop_ptr
258# define Perl_call_list dll_Perl_call_list
259# define Perl_Iscopestack_ix_ptr dll_Perl_Iscopestack_ix_ptr
260# define Perl_Iunitcheckav_ptr dll_Perl_Iunitcheckav_ptr
Bram Moolenaar01c10522012-09-21 12:50:51 +0200261# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
262# define PL_thr_key *dll_PL_thr_key
263# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000264
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265/*
266 * Declare HANDLE for perl.dll and function pointers.
267 */
268static HANDLE hPerlLib = NULL;
269
270static PerlInterpreter* (*perl_alloc)();
271static void (*perl_construct)(PerlInterpreter*);
272static void (*perl_destruct)(PerlInterpreter*);
273static void (*perl_free)(PerlInterpreter*);
274static int (*perl_run)(PerlInterpreter*);
275static int (*perl_parse)(PerlInterpreter*, XSINIT_t, int, char**, char**);
276static void* (*Perl_get_context)(void);
Bram Moolenaara7ecc562006-08-16 16:17:39 +0000277static void (*Perl_croak)(pTHX_ const char*, ...);
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100278#ifdef PERL5101_OR_LATER
Bram Moolenaar6b107212013-12-11 15:06:40 +0100279/* Perl-5.18 has a different Perl_croak_xs_usage signature. */
280# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
281static void (*Perl_croak_xs_usage)(const CV *const, const char *const params);
282# else
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100283static void (*Perl_croak_xs_usage)(pTHX_ const CV *const, const char *const params);
Bram Moolenaar6b107212013-12-11 15:06:40 +0100284# endif
Bram Moolenaar9be6e212013-06-15 16:47:35 +0200285#endif
Bram Moolenaara7ecc562006-08-16 16:17:39 +0000286static void (*Perl_croak_nocontext)(const char*, ...);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287static I32 (*Perl_dowantarray)(pTHX);
288static void (*Perl_free_tmps)(pTHX);
289static HV* (*Perl_gv_stashpv)(pTHX_ const char*, I32);
290static void (*Perl_markstack_grow)(pTHX);
291static MAGIC* (*Perl_mg_find)(pTHX_ SV*, int);
292static CV* (*Perl_newXS)(pTHX_ char*, XSUBADDR_t, char*);
293static SV* (*Perl_newSV)(pTHX_ STRLEN);
294static SV* (*Perl_newSViv)(pTHX_ IV);
295static SV* (*Perl_newSVpv)(pTHX_ const char*, STRLEN);
296static I32 (*Perl_call_argv)(pTHX_ const char*, I32, char**);
297static I32 (*Perl_call_pv)(pTHX_ const char*, I32);
298static I32 (*Perl_eval_sv)(pTHX_ SV*, I32);
299static SV* (*Perl_get_sv)(pTHX_ const char*, I32);
300static SV* (*Perl_eval_pv)(pTHX_ const char*, I32);
301static SV* (*Perl_call_method)(pTHX_ const char*, I32);
302static void (*Perl_pop_scope)(pTHX);
303static void (*Perl_push_scope)(pTHX);
304static void (*Perl_save_int)(pTHX_ int*);
305static SV** (*Perl_stack_grow)(pTHX_ SV**, SV**p, int);
306static SV** (*Perl_set_context)(void*);
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200307#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
308static bool (*Perl_sv_2bool_flags)(pTHX_ SV*, I32);
309static void (*Perl_xs_apiversion_bootcheck)(pTHX_ SV *module, const char *api_p, STRLEN api_len);
310#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311static bool (*Perl_sv_2bool)(pTHX_ SV*);
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313static IV (*Perl_sv_2iv)(pTHX_ SV*);
314static SV* (*Perl_sv_2mortal)(pTHX_ SV*);
315#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
316static char* (*Perl_sv_2pv_flags)(pTHX_ SV*, STRLEN*, I32);
317static char* (*Perl_sv_2pv_nolen)(pTHX_ SV*);
318#else
319static char* (*Perl_sv_2pv)(pTHX_ SV*, STRLEN*);
320#endif
321static SV* (*Perl_sv_bless)(pTHX_ SV*, HV*);
322#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
323static void (*Perl_sv_catpvn_flags)(pTHX_ SV* , const char*, STRLEN, I32);
324#else
325static void (*Perl_sv_catpvn)(pTHX_ SV*, const char*, STRLEN);
326#endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000327#ifdef PERL589_OR_LATER
328static IV (*Perl_sv_2iv_flags)(pTHX_ SV* sv, I32 flags);
329static CV * (*Perl_newXS_flags)(pTHX_ const char *name, XSUBADDR_t subaddr, const char *const filename, const char *const proto, U32 flags);
330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331static void (*Perl_sv_free)(pTHX_ SV*);
332static int (*Perl_sv_isa)(pTHX_ SV*, const char*);
333static void (*Perl_sv_magic)(pTHX_ SV*, SV*, int, const char*, I32);
334static void (*Perl_sv_setiv)(pTHX_ SV*, IV);
335static void (*Perl_sv_setpv)(pTHX_ SV*, const char*);
336static void (*Perl_sv_setpvn)(pTHX_ SV*, const char*, STRLEN);
337#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
338static void (*Perl_sv_setsv_flags)(pTHX_ SV*, SV*, I32);
339#else
340static void (*Perl_sv_setsv)(pTHX_ SV*, SV*);
341#endif
342static bool (*Perl_sv_upgrade)(pTHX_ SV*, U32);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200343#if (PERL_REVISION == 5) && (PERL_VERSION < 10)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344static SV*** (*Perl_Tstack_sp_ptr)(register PerlInterpreter*);
345static OP** (*Perl_Top_ptr)(register PerlInterpreter*);
346static SV*** (*Perl_Tstack_base_ptr)(register PerlInterpreter*);
347static SV*** (*Perl_Tstack_max_ptr)(register PerlInterpreter*);
348static I32* (*Perl_Ttmps_ix_ptr)(register PerlInterpreter*);
349static I32* (*Perl_Ttmps_floor_ptr)(register PerlInterpreter*);
350static I32** (*Perl_Tmarkstack_ptr_ptr)(register PerlInterpreter*);
351static I32** (*Perl_Tmarkstack_max_ptr)(register PerlInterpreter*);
352static SV** (*Perl_TSv_ptr)(register PerlInterpreter*);
353static XPV** (*Perl_TXpv_ptr)(register PerlInterpreter*);
354static STRLEN* (*Perl_Tna_ptr)(register PerlInterpreter*);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200355#else
Bram Moolenaar6b107212013-12-11 15:06:40 +0100356/* Perl-5.18 has a different Perl_sv_free2 signature. */
357# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
358static void (*Perl_sv_free2)(pTHX_ SV*, const U32);
359# else
Bram Moolenaarccf22172008-09-01 15:56:45 +0000360static void (*Perl_sv_free2)(pTHX_ SV*);
Bram Moolenaar6b107212013-12-11 15:06:40 +0100361# endif
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000362static void (*Perl_sys_init)(int* argc, char*** argv);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000363static void (*Perl_sys_term)(void);
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200364static void (*Perl_call_list)(pTHX_ I32, AV*);
365# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
366# else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000367static SV** (*Perl_ISv_ptr)(register PerlInterpreter*);
368static SV*** (*Perl_Istack_max_ptr)(register PerlInterpreter*);
369static SV*** (*Perl_Istack_base_ptr)(register PerlInterpreter*);
370static XPV** (*Perl_IXpv_ptr)(register PerlInterpreter*);
371static I32* (*Perl_Itmps_ix_ptr)(register PerlInterpreter*);
372static I32* (*Perl_Itmps_floor_ptr)(register PerlInterpreter*);
373static STRLEN* (*Perl_Ina_ptr)(register PerlInterpreter*);
374static I32** (*Perl_Imarkstack_ptr_ptr)(register PerlInterpreter*);
375static I32** (*Perl_Imarkstack_max_ptr)(register PerlInterpreter*);
376static SV*** (*Perl_Istack_sp_ptr)(register PerlInterpreter*);
377static OP** (*Perl_Iop_ptr)(register PerlInterpreter*);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000378static I32* (*Perl_Iscopestack_ix_ptr)(register PerlInterpreter*);
379static AV** (*Perl_Iunitcheckav_ptr)(register PerlInterpreter*);
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200380# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200383#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaar01c10522012-09-21 12:50:51 +0200384static perl_key* dll_PL_thr_key;
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200385#else
Bram Moolenaare06c1882010-07-21 22:05:20 +0200386static GV** (*Perl_Idefgv_ptr)(register PerlInterpreter*);
387static GV** (*Perl_Ierrgv_ptr)(register PerlInterpreter*);
388static SV* (*Perl_Isv_yes_ptr)(register PerlInterpreter*);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200389static perl_key* (*Perl_Gthr_key_ptr)_((pTHX));
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200390#endif
391static void (*boot_DynaLoader)_((pTHX_ CV*));
Bram Moolenaare06c1882010-07-21 22:05:20 +0200392
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393/*
394 * Table of name to function pointer of perl.
395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396static struct {
397 char* name;
398 PERL_PROC* ptr;
399} perl_funcname_table[] = {
400 {"perl_alloc", (PERL_PROC*)&perl_alloc},
401 {"perl_construct", (PERL_PROC*)&perl_construct},
402 {"perl_destruct", (PERL_PROC*)&perl_destruct},
403 {"perl_free", (PERL_PROC*)&perl_free},
404 {"perl_run", (PERL_PROC*)&perl_run},
405 {"perl_parse", (PERL_PROC*)&perl_parse},
406 {"Perl_get_context", (PERL_PROC*)&Perl_get_context},
407 {"Perl_croak", (PERL_PROC*)&Perl_croak},
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100408#ifdef PERL5101_OR_LATER
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100409 {"Perl_croak_xs_usage", (PERL_PROC*)&Perl_croak_xs_usage},
410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 {"Perl_croak_nocontext", (PERL_PROC*)&Perl_croak_nocontext},
412 {"Perl_dowantarray", (PERL_PROC*)&Perl_dowantarray},
413 {"Perl_free_tmps", (PERL_PROC*)&Perl_free_tmps},
414 {"Perl_gv_stashpv", (PERL_PROC*)&Perl_gv_stashpv},
415 {"Perl_markstack_grow", (PERL_PROC*)&Perl_markstack_grow},
416 {"Perl_mg_find", (PERL_PROC*)&Perl_mg_find},
417 {"Perl_newXS", (PERL_PROC*)&Perl_newXS},
418 {"Perl_newSV", (PERL_PROC*)&Perl_newSV},
419 {"Perl_newSViv", (PERL_PROC*)&Perl_newSViv},
420 {"Perl_newSVpv", (PERL_PROC*)&Perl_newSVpv},
421 {"Perl_call_argv", (PERL_PROC*)&Perl_call_argv},
422 {"Perl_call_pv", (PERL_PROC*)&Perl_call_pv},
423 {"Perl_eval_sv", (PERL_PROC*)&Perl_eval_sv},
424 {"Perl_get_sv", (PERL_PROC*)&Perl_get_sv},
425 {"Perl_eval_pv", (PERL_PROC*)&Perl_eval_pv},
426 {"Perl_call_method", (PERL_PROC*)&Perl_call_method},
427 {"Perl_pop_scope", (PERL_PROC*)&Perl_pop_scope},
428 {"Perl_push_scope", (PERL_PROC*)&Perl_push_scope},
429 {"Perl_save_int", (PERL_PROC*)&Perl_save_int},
430 {"Perl_stack_grow", (PERL_PROC*)&Perl_stack_grow},
431 {"Perl_set_context", (PERL_PROC*)&Perl_set_context},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200432#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
433 {"Perl_sv_2bool_flags", (PERL_PROC*)&Perl_sv_2bool_flags},
434 {"Perl_xs_apiversion_bootcheck",(PERL_PROC*)&Perl_xs_apiversion_bootcheck},
435#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 {"Perl_sv_2bool", (PERL_PROC*)&Perl_sv_2bool},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 {"Perl_sv_2iv", (PERL_PROC*)&Perl_sv_2iv},
439 {"Perl_sv_2mortal", (PERL_PROC*)&Perl_sv_2mortal},
440#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
441 {"Perl_sv_2pv_flags", (PERL_PROC*)&Perl_sv_2pv_flags},
442 {"Perl_sv_2pv_nolen", (PERL_PROC*)&Perl_sv_2pv_nolen},
443#else
444 {"Perl_sv_2pv", (PERL_PROC*)&Perl_sv_2pv},
445#endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000446#ifdef PERL589_OR_LATER
447 {"Perl_sv_2iv_flags", (PERL_PROC*)&Perl_sv_2iv_flags},
448 {"Perl_newXS_flags", (PERL_PROC*)&Perl_newXS_flags},
449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 {"Perl_sv_bless", (PERL_PROC*)&Perl_sv_bless},
451#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
452 {"Perl_sv_catpvn_flags", (PERL_PROC*)&Perl_sv_catpvn_flags},
453#else
454 {"Perl_sv_catpvn", (PERL_PROC*)&Perl_sv_catpvn},
455#endif
456 {"Perl_sv_free", (PERL_PROC*)&Perl_sv_free},
457 {"Perl_sv_isa", (PERL_PROC*)&Perl_sv_isa},
458 {"Perl_sv_magic", (PERL_PROC*)&Perl_sv_magic},
459 {"Perl_sv_setiv", (PERL_PROC*)&Perl_sv_setiv},
460 {"Perl_sv_setpv", (PERL_PROC*)&Perl_sv_setpv},
461 {"Perl_sv_setpvn", (PERL_PROC*)&Perl_sv_setpvn},
462#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
463 {"Perl_sv_setsv_flags", (PERL_PROC*)&Perl_sv_setsv_flags},
464#else
465 {"Perl_sv_setsv", (PERL_PROC*)&Perl_sv_setsv},
466#endif
467 {"Perl_sv_upgrade", (PERL_PROC*)&Perl_sv_upgrade},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000468#if (PERL_REVISION == 5) && (PERL_VERSION < 10)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 {"Perl_Tstack_sp_ptr", (PERL_PROC*)&Perl_Tstack_sp_ptr},
470 {"Perl_Top_ptr", (PERL_PROC*)&Perl_Top_ptr},
471 {"Perl_Tstack_base_ptr", (PERL_PROC*)&Perl_Tstack_base_ptr},
472 {"Perl_Tstack_max_ptr", (PERL_PROC*)&Perl_Tstack_max_ptr},
473 {"Perl_Ttmps_ix_ptr", (PERL_PROC*)&Perl_Ttmps_ix_ptr},
474 {"Perl_Ttmps_floor_ptr", (PERL_PROC*)&Perl_Ttmps_floor_ptr},
475 {"Perl_Tmarkstack_ptr_ptr", (PERL_PROC*)&Perl_Tmarkstack_ptr_ptr},
476 {"Perl_Tmarkstack_max_ptr", (PERL_PROC*)&Perl_Tmarkstack_max_ptr},
477 {"Perl_TSv_ptr", (PERL_PROC*)&Perl_TSv_ptr},
478 {"Perl_TXpv_ptr", (PERL_PROC*)&Perl_TXpv_ptr},
479 {"Perl_Tna_ptr", (PERL_PROC*)&Perl_Tna_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000480#else
Bram Moolenaarccf22172008-09-01 15:56:45 +0000481 {"Perl_sv_free2", (PERL_PROC*)&Perl_sv_free2},
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000482 {"Perl_sys_init", (PERL_PROC*)&Perl_sys_init},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000483 {"Perl_sys_term", (PERL_PROC*)&Perl_sys_term},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200484 {"Perl_call_list", (PERL_PROC*)&Perl_call_list},
485# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
486# else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000487 {"Perl_ISv_ptr", (PERL_PROC*)&Perl_ISv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000488 {"Perl_Istack_max_ptr", (PERL_PROC*)&Perl_Istack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200489 {"Perl_Istack_base_ptr", (PERL_PROC*)&Perl_Istack_base_ptr},
490 {"Perl_IXpv_ptr", (PERL_PROC*)&Perl_IXpv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000491 {"Perl_Itmps_ix_ptr", (PERL_PROC*)&Perl_Itmps_ix_ptr},
492 {"Perl_Itmps_floor_ptr", (PERL_PROC*)&Perl_Itmps_floor_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200493 {"Perl_Ina_ptr", (PERL_PROC*)&Perl_Ina_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000494 {"Perl_Imarkstack_ptr_ptr", (PERL_PROC*)&Perl_Imarkstack_ptr_ptr},
495 {"Perl_Imarkstack_max_ptr", (PERL_PROC*)&Perl_Imarkstack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200496 {"Perl_Istack_sp_ptr", (PERL_PROC*)&Perl_Istack_sp_ptr},
497 {"Perl_Iop_ptr", (PERL_PROC*)&Perl_Iop_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000498 {"Perl_Iscopestack_ix_ptr", (PERL_PROC*)&Perl_Iscopestack_ix_ptr},
499 {"Perl_Iunitcheckav_ptr", (PERL_PROC*)&Perl_Iunitcheckav_ptr},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200500# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000501#endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200502#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaar01c10522012-09-21 12:50:51 +0200503 {"PL_thr_key", (PERL_PROC*)&dll_PL_thr_key},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200504#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 {"Perl_Idefgv_ptr", (PERL_PROC*)&Perl_Idefgv_ptr},
506 {"Perl_Ierrgv_ptr", (PERL_PROC*)&Perl_Ierrgv_ptr},
507 {"Perl_Isv_yes_ptr", (PERL_PROC*)&Perl_Isv_yes_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200508 {"Perl_Gthr_key_ptr", (PERL_PROC*)&Perl_Gthr_key_ptr},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200509#endif
510 {"boot_DynaLoader", (PERL_PROC*)&boot_DynaLoader},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 {"", NULL},
512};
513
Bram Moolenaar6b107212013-12-11 15:06:40 +0100514/* Work around for perl-5.18.
515 * The definitions of S_SvREFCNT_inc and S_SvREFCNT_dec are needed, so include
516 * "perl\lib\CORE\inline.h", after Perl_sv_free2 is defined.
517 * The linker won't complain about undefined __impl_Perl_sv_free2. */
518#if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
519# include <inline.h>
520#endif
521
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522/*
523 * Make all runtime-links of perl.
524 *
Bram Moolenaar364ab2f2013-08-02 20:05:32 +0200525 * 1. Get module handle using dlopen() or vimLoadLib().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 * 2. Get pointer to perl function by GetProcAddress.
527 * 3. Repeat 2, until get all functions will be used.
528 *
529 * Parameter 'libname' provides name of DLL.
530 * Return OK or FAIL.
531 */
532 static int
533perl_runtime_link_init(char *libname, int verbose)
534{
535 int i;
536
537 if (hPerlLib != NULL)
538 return OK;
Bram Moolenaare06c1882010-07-21 22:05:20 +0200539 if ((hPerlLib = load_dll(libname)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 {
541 if (verbose)
542 EMSG2(_("E370: Could not load library %s"), libname);
543 return FAIL;
544 }
545 for (i = 0; perl_funcname_table[i].ptr; ++i)
546 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200547 if (!(*perl_funcname_table[i].ptr = symbol_from_dll(hPerlLib,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 perl_funcname_table[i].name)))
549 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200550 close_dll(hPerlLib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 hPerlLib = NULL;
552 if (verbose)
553 EMSG2(_(e_loadfunc), perl_funcname_table[i].name);
554 return FAIL;
555 }
556 }
557 return OK;
558}
559
560/*
561 * If runtime-link-perl(DLL) was loaded successfully, return TRUE.
562 * There were no DLL loaded, return FALSE.
563 */
564 int
565perl_enabled(verbose)
566 int verbose;
567{
568 return perl_runtime_link_init(DYNAMIC_PERL_DLL, verbose) == OK;
569}
570#endif /* DYNAMIC_PERL */
571
572/*
573 * perl_init(): initialize perl interpreter
574 * We have to call perl_parse to initialize some structures,
575 * there's nothing to actually parse.
576 */
577 static void
578perl_init()
579{
Bram Moolenaarc236c162008-07-13 17:41:49 +0000580 char *bootargs[] = { "VI", NULL };
581 int argc = 3;
582 static char *argv[] = { "", "-e", "" };
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583
Bram Moolenaarc236c162008-07-13 17:41:49 +0000584#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000585 Perl_sys_init(&argc, (char***)&argv);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 perl_interp = perl_alloc();
588 perl_construct(perl_interp);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000589 perl_parse(perl_interp, xs_init, argc, argv, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 perl_call_argv("VIM::bootstrap", (long)G_DISCARD, bootargs);
591 VIM_init();
592#ifdef USE_SFIO
593 sfdisc(PerlIO_stdout(), sfdcnewvim());
594 sfdisc(PerlIO_stderr(), sfdcnewvim());
595 sfsetbuf(PerlIO_stdout(), NULL, 0);
596 sfsetbuf(PerlIO_stderr(), NULL, 0);
597#endif
598}
599
600/*
601 * perl_end(): clean up after ourselves
602 */
603 void
604perl_end()
605{
606 if (perl_interp)
607 {
608 perl_run(perl_interp);
609 perl_destruct(perl_interp);
610 perl_free(perl_interp);
611 perl_interp = NULL;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000612#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
613 Perl_sys_term();
614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 }
616#ifdef DYNAMIC_PERL
617 if (hPerlLib)
618 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200619 close_dll(hPerlLib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 hPerlLib = NULL;
621 }
622#endif
623}
624
625/*
626 * msg_split(): send a message to the message handling routines
627 * split at '\n' first though.
628 */
629 void
630msg_split(s, attr)
631 char_u *s;
632 int attr; /* highlighting attributes */
633{
634 char *next;
635 char *token = (char *)s;
636
Bram Moolenaaraa8494a2007-10-09 08:47:27 +0000637 while ((next = strchr(token, '\n')) && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 {
639 *next++ = '\0'; /* replace \n with \0 */
640 msg_attr((char_u *)token, attr);
641 token = next;
642 }
Bram Moolenaaraa8494a2007-10-09 08:47:27 +0000643 if (*token && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 msg_attr((char_u *)token, attr);
645}
646
647#ifndef FEAT_EVAL
648/*
649 * This stub is needed because an "#ifdef FEAT_EVAL" around Eval() doesn't
650 * work properly.
651 */
652 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000653eval_to_string(arg, nextcmd, dolist)
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100654 char_u *arg UNUSED;
655 char_u **nextcmd UNUSED;
656 int dolist UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657{
658 return NULL;
659}
660#endif
661
662/*
663 * Create a new reference to an SV pointing to the SCR structure
Bram Moolenaare344bea2005-09-01 20:46:49 +0000664 * The b_perl_private/w_perl_private part of the SCR structure points to the
665 * SV, so there can only be one such SV for a particular SCR structure. When
666 * the last reference has gone (DESTROY is called),
667 * b_perl_private/w_perl_private is reset; When the screen goes away before
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 * all references are gone, the value of the SV is reset;
669 * any subsequent use of any of those reference will produce
670 * a warning. (see typemap)
671 */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000672
673 static SV *
674newWINrv(rv, ptr)
675 SV *rv;
676 win_T *ptr;
677{
678 sv_upgrade(rv, SVt_RV);
679 if (ptr->w_perl_private == NULL)
680 {
681 ptr->w_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +0100682 sv_setiv(ptr->w_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +0000683 }
684 else
685 SvREFCNT_inc(ptr->w_perl_private);
686 SvRV(rv) = ptr->w_perl_private;
687 SvROK_on(rv);
688 return sv_bless(rv, gv_stashpv("VIWIN", TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689}
690
Bram Moolenaare344bea2005-09-01 20:46:49 +0000691 static SV *
692newBUFrv(rv, ptr)
693 SV *rv;
694 buf_T *ptr;
695{
696 sv_upgrade(rv, SVt_RV);
697 if (ptr->b_perl_private == NULL)
698 {
699 ptr->b_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +0100700 sv_setiv(ptr->b_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +0000701 }
702 else
703 SvREFCNT_inc(ptr->b_perl_private);
704 SvRV(rv) = ptr->b_perl_private;
705 SvROK_on(rv);
706 return sv_bless(rv, gv_stashpv("VIBUF", TRUE));
707}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708
709/*
710 * perl_win_free
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200711 * Remove all references to the window to be destroyed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 */
713 void
714perl_win_free(wp)
715 win_T *wp;
716{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000717 if (wp->w_perl_private)
718 sv_setiv((SV *)wp->w_perl_private, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 return;
720}
721
722 void
723perl_buf_free(bp)
724 buf_T *bp;
725{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000726 if (bp->b_perl_private)
727 sv_setiv((SV *)bp->b_perl_private, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 return;
729}
730
731#ifndef PROTO
732# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
733I32 cur_val(pTHX_ IV iv, SV *sv);
734# else
735I32 cur_val(IV iv, SV *sv);
736#endif
737
738/*
739 * Handler for the magic variables $main::curwin and $main::curbuf.
740 * The handler is put into the magic vtbl for these variables.
741 * (This is effectively a C-level equivalent of a tied variable).
742 * There is no "set" function as the variables are read-only.
743 */
744# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
745I32 cur_val(pTHX_ IV iv, SV *sv)
746# else
747I32 cur_val(IV iv, SV *sv)
748# endif
749{
750 SV *rv;
751 if (iv == 0)
752 rv = newWINrv(newSV(0), curwin);
753 else
754 rv = newBUFrv(newSV(0), curbuf);
755 sv_setsv(sv, rv);
756 return 0;
757}
758#endif /* !PROTO */
759
760struct ufuncs cw_funcs = { cur_val, 0, 0 };
761struct ufuncs cb_funcs = { cur_val, 0, 1 };
762
763/*
764 * VIM_init(): Vim-specific initialisation.
765 * Make the magical main::curwin and main::curbuf variables
766 */
767 static void
768VIM_init()
769{
770 static char cw[] = "main::curwin";
771 static char cb[] = "main::curbuf";
772 SV *sv;
773
774 sv = perl_get_sv(cw, TRUE);
775 sv_magic(sv, NULL, 'U', (char *)&cw_funcs, sizeof(cw_funcs));
776 SvREADONLY_on(sv);
777
778 sv = perl_get_sv(cb, TRUE);
779 sv_magic(sv, NULL, 'U', (char *)&cb_funcs, sizeof(cb_funcs));
780 SvREADONLY_on(sv);
781
782 /*
783 * Setup the Safe compartment.
784 * It shouldn't be a fatal error if the Safe module is missing.
785 * XXX: Only shares the 'Msg' routine (which has to be called
786 * like 'Msg(...)').
787 */
788 (void)perl_eval_pv( "if ( eval( 'require Safe' ) ) { $VIM::safe = Safe->new(); $VIM::safe->share_from( 'VIM', ['Msg'] ); }", G_DISCARD | G_VOID );
789
790}
791
792#ifdef DYNAMIC_PERL
793static char *e_noperl = N_("Sorry, this command is disabled: the Perl library could not be loaded.");
794#endif
795
796/*
797 * ":perl"
798 */
799 void
800ex_perl(eap)
801 exarg_T *eap;
802{
803 char *err;
804 char *script;
805 STRLEN length;
806 SV *sv;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200807#ifdef HAVE_SANDBOX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 SV *safe;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810
811 script = (char *)script_get(eap, eap->arg);
812 if (eap->skip)
813 {
814 vim_free(script);
815 return;
816 }
817
818 if (perl_interp == NULL)
819 {
820#ifdef DYNAMIC_PERL
821 if (!perl_enabled(TRUE))
822 {
823 EMSG(_(e_noperl));
824 vim_free(script);
825 return;
826 }
827#endif
828 perl_init();
829 }
830
831 {
832 dSP;
833 ENTER;
834 SAVETMPS;
835
836 if (script == NULL)
837 sv = newSVpv((char *)eap->arg, 0);
838 else
839 {
840 sv = newSVpv(script, 0);
841 vim_free(script);
842 }
843
844#ifdef HAVE_SANDBOX
845 if (sandbox)
846 {
Bram Moolenaara1711622011-07-27 14:15:46 +0200847 safe = perl_get_sv("VIM::safe", FALSE);
Bram Moolenaar3f947ea2009-07-14 14:04:54 +0000848# ifndef MAKE_TEST /* avoid a warning for unreachable code */
Bram Moolenaar954e8c52009-11-11 13:45:33 +0000849 if (safe == NULL || !SvTRUE(safe))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 EMSG(_("E299: Perl evaluation forbidden in sandbox without the Safe module"));
851 else
Bram Moolenaar3f947ea2009-07-14 14:04:54 +0000852# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 {
854 PUSHMARK(SP);
855 XPUSHs(safe);
856 XPUSHs(sv);
857 PUTBACK;
858 perl_call_method("reval", G_DISCARD);
859 }
860 }
861 else
862#endif
863 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
864
865 SvREFCNT_dec(sv);
866
Bram Moolenaarc271c482012-08-08 13:17:31 +0200867#ifdef AVOID_PL_ERRGV
868 err = SvPV(perl_get_sv("@", GV_ADD), length);
869#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 err = SvPV(GvSV(PL_errgv), length);
Bram Moolenaarc271c482012-08-08 13:17:31 +0200871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872
873 FREETMPS;
874 LEAVE;
875
876 if (!length)
877 return;
878
879 msg_split((char_u *)err, highlight_attr[HLF_E]);
880 return;
881 }
882}
883
884 static int
885replace_line(line, end)
886 linenr_T *line, *end;
887{
888 char *str;
889
890 if (SvOK(GvSV(PL_defgv)))
891 {
892 str = SvPV(GvSV(PL_defgv), PL_na);
893 ml_replace(*line, (char_u *)str, 1);
894 changed_bytes(*line, 0);
895 }
896 else
897 {
898 ml_delete(*line, FALSE);
899 deleted_lines_mark(*line, 1L);
900 --(*end);
901 --(*line);
902 }
903 return OK;
904}
905
906/*
907 * ":perldo".
908 */
909 void
910ex_perldo(eap)
911 exarg_T *eap;
912{
913 STRLEN length;
914 SV *sv;
915 char *str;
916 linenr_T i;
917
918 if (bufempty())
919 return;
920
921 if (perl_interp == NULL)
922 {
923#ifdef DYNAMIC_PERL
924 if (!perl_enabled(TRUE))
925 {
926 EMSG(_(e_noperl));
927 return;
928 }
929#endif
930 perl_init();
931 }
932 {
933 dSP;
934 length = strlen((char *)eap->arg);
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000935 sv = newSV(length + sizeof("sub VIM::perldo {") - 1 + 1);
936 sv_setpvn(sv, "sub VIM::perldo {", sizeof("sub VIM::perldo {") - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 sv_catpvn(sv, (char *)eap->arg, length);
938 sv_catpvn(sv, "}", 1);
939 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
940 SvREFCNT_dec(sv);
Bram Moolenaarc271c482012-08-08 13:17:31 +0200941#ifdef AVOID_PL_ERRGV
942 str = SvPV(perl_get_sv("@", GV_ADD), length);
943#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 str = SvPV(GvSV(PL_errgv), length);
Bram Moolenaarc271c482012-08-08 13:17:31 +0200945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 if (length)
947 goto err;
948
949 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
950 return;
951
952 ENTER;
953 SAVETMPS;
954 for (i = eap->line1; i <= eap->line2; i++)
955 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000956 sv_setpv(GvSV(PL_defgv), (char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 PUSHMARK(sp);
958 perl_call_pv("VIM::perldo", G_SCALAR | G_EVAL);
Bram Moolenaarc271c482012-08-08 13:17:31 +0200959#ifdef AVOID_PL_ERRGV
960 str = SvPV(perl_get_sv("@", GV_ADD), length);
961#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 str = SvPV(GvSV(PL_errgv), length);
Bram Moolenaarc271c482012-08-08 13:17:31 +0200963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 if (length)
965 break;
966 SPAGAIN;
967 if (SvTRUEx(POPs))
968 {
969 if (replace_line(&i, &eap->line2) != OK)
970 {
971 PUTBACK;
972 break;
973 }
974 }
975 PUTBACK;
976 }
977 FREETMPS;
978 LEAVE;
979 check_cursor();
980 update_screen(NOT_VALID);
981 if (!length)
982 return;
983
984err:
985 msg_split((char_u *)str, highlight_attr[HLF_E]);
986 return;
987 }
988}
989
Bram Moolenaar01dd60c2008-07-24 14:24:48 +0000990#ifndef FEAT_WINDOWS
991int win_valid(win_T *w) { return TRUE; }
992int win_count() { return 1; }
993win_T *win_find_nr(int n) { return curwin; }
994#endif
995
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996XS(boot_VIM);
997
998 static void
999xs_init(pTHX)
1000{
1001 char *file = __FILE__;
1002
1003 /* DynaLoader is a special case */
1004 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1005 newXS("VIM::bootstrap", boot_VIM, file);
1006}
1007
1008typedef win_T * VIWIN;
1009typedef buf_T * VIBUF;
1010
1011MODULE = VIM PACKAGE = VIM
1012
1013void
1014Msg(text, hl=NULL)
1015 char *text;
1016 char *hl;
1017
1018 PREINIT:
1019 int attr;
1020 int id;
1021
1022 PPCODE:
1023 if (text != NULL)
1024 {
1025 attr = 0;
1026 if (hl != NULL)
1027 {
1028 id = syn_name2id((char_u *)hl);
1029 if (id != 0)
1030 attr = syn_id2attr(id);
1031 }
1032 msg_split((char_u *)text, attr);
1033 }
1034
1035void
1036SetOption(line)
1037 char *line;
1038
1039 PPCODE:
1040 if (line != NULL)
1041 do_set((char_u *)line, 0);
1042 update_screen(NOT_VALID);
1043
1044void
1045DoCommand(line)
1046 char *line;
1047
1048 PPCODE:
1049 if (line != NULL)
1050 do_cmdline_cmd((char_u *)line);
1051
1052void
1053Eval(str)
1054 char *str;
1055
1056 PREINIT:
1057 char_u *value;
1058 PPCODE:
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001059 value = eval_to_string((char_u *)str, (char_u **)0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 if (value == NULL)
1061 {
1062 XPUSHs(sv_2mortal(newSViv(0)));
1063 XPUSHs(sv_2mortal(newSVpv("", 0)));
1064 }
1065 else
1066 {
1067 XPUSHs(sv_2mortal(newSViv(1)));
1068 XPUSHs(sv_2mortal(newSVpv((char *)value, 0)));
1069 vim_free(value);
1070 }
1071
1072void
1073Buffers(...)
1074
1075 PREINIT:
1076 buf_T *vimbuf;
1077 int i, b;
1078
1079 PPCODE:
1080 if (items == 0)
1081 {
1082 if (GIMME == G_SCALAR)
1083 {
1084 i = 0;
1085 for (vimbuf = firstbuf; vimbuf; vimbuf = vimbuf->b_next)
1086 ++i;
1087
1088 XPUSHs(sv_2mortal(newSViv(i)));
1089 }
1090 else
1091 {
1092 for (vimbuf = firstbuf; vimbuf; vimbuf = vimbuf->b_next)
1093 XPUSHs(newBUFrv(newSV(0), vimbuf));
1094 }
1095 }
1096 else
1097 {
1098 for (i = 0; i < items; i++)
1099 {
1100 SV *sv = ST(i);
1101 if (SvIOK(sv))
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001102 b = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 else
1104 {
1105 char_u *pat;
1106 STRLEN len;
1107
1108 pat = (char_u *)SvPV(sv, len);
1109 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001110 b = buflist_findpat(pat, pat+len, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 --emsg_off;
1112 }
1113
1114 if (b >= 0)
1115 {
1116 vimbuf = buflist_findnr(b);
1117 if (vimbuf)
1118 XPUSHs(newBUFrv(newSV(0), vimbuf));
1119 }
1120 }
1121 }
1122
1123void
1124Windows(...)
1125
1126 PREINIT:
1127 win_T *vimwin;
1128 int i, w;
1129
1130 PPCODE:
1131 if (items == 0)
1132 {
1133 if (GIMME == G_SCALAR)
1134 XPUSHs(sv_2mortal(newSViv(win_count())));
1135 else
1136 {
1137 for (vimwin = firstwin; vimwin != NULL; vimwin = W_NEXT(vimwin))
1138 XPUSHs(newWINrv(newSV(0), vimwin));
1139 }
1140 }
1141 else
1142 {
1143 for (i = 0; i < items; i++)
1144 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001145 w = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 vimwin = win_find_nr(w);
1147 if (vimwin)
1148 XPUSHs(newWINrv(newSV(0), vimwin));
1149 }
1150 }
1151
1152MODULE = VIM PACKAGE = VIWIN
1153
1154void
1155DESTROY(win)
1156 VIWIN win
1157
1158 CODE:
1159 if (win_valid(win))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001160 win->w_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161
1162SV *
1163Buffer(win)
1164 VIWIN win
1165
1166 CODE:
1167 if (!win_valid(win))
1168 win = curwin;
1169 RETVAL = newBUFrv(newSV(0), win->w_buffer);
1170 OUTPUT:
1171 RETVAL
1172
1173void
1174SetHeight(win, height)
1175 VIWIN win
1176 int height;
1177
1178 PREINIT:
1179 win_T *savewin;
1180
1181 PPCODE:
1182 if (!win_valid(win))
1183 win = curwin;
1184 savewin = curwin;
1185 curwin = win;
1186 win_setheight(height);
1187 curwin = savewin;
1188
1189void
1190Cursor(win, ...)
1191 VIWIN win
1192
1193 PPCODE:
Bram Moolenaara1711622011-07-27 14:15:46 +02001194 if (items == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 {
1196 EXTEND(sp, 2);
1197 if (!win_valid(win))
1198 win = curwin;
1199 PUSHs(sv_2mortal(newSViv(win->w_cursor.lnum)));
1200 PUSHs(sv_2mortal(newSViv(win->w_cursor.col)));
1201 }
Bram Moolenaara1711622011-07-27 14:15:46 +02001202 else if (items == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 {
1204 int lnum, col;
1205
1206 if (!win_valid(win))
1207 win = curwin;
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001208 lnum = (int) SvIV(ST(1));
1209 col = (int) SvIV(ST(2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 win->w_cursor.lnum = lnum;
1211 win->w_cursor.col = col;
1212 check_cursor(); /* put cursor on an existing line */
1213 update_screen(NOT_VALID);
1214 }
1215
1216MODULE = VIM PACKAGE = VIBUF
1217
1218void
1219DESTROY(vimbuf)
1220 VIBUF vimbuf;
1221
1222 CODE:
1223 if (buf_valid(vimbuf))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001224 vimbuf->b_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225
1226void
1227Name(vimbuf)
1228 VIBUF vimbuf;
1229
1230 PPCODE:
1231 if (!buf_valid(vimbuf))
1232 vimbuf = curbuf;
1233 /* No file name returns an empty string */
1234 if (vimbuf->b_fname == NULL)
1235 XPUSHs(sv_2mortal(newSVpv("", 0)));
1236 else
1237 XPUSHs(sv_2mortal(newSVpv((char *)vimbuf->b_fname, 0)));
1238
1239void
1240Number(vimbuf)
1241 VIBUF vimbuf;
1242
1243 PPCODE:
1244 if (!buf_valid(vimbuf))
1245 vimbuf = curbuf;
1246 XPUSHs(sv_2mortal(newSViv(vimbuf->b_fnum)));
1247
1248void
1249Count(vimbuf)
1250 VIBUF vimbuf;
1251
1252 PPCODE:
1253 if (!buf_valid(vimbuf))
1254 vimbuf = curbuf;
1255 XPUSHs(sv_2mortal(newSViv(vimbuf->b_ml.ml_line_count)));
1256
1257void
1258Get(vimbuf, ...)
1259 VIBUF vimbuf;
1260
1261 PREINIT:
1262 char_u *line;
1263 int i;
1264 long lnum;
1265 PPCODE:
1266 if (buf_valid(vimbuf))
1267 {
1268 for (i = 1; i < items; i++)
1269 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001270 lnum = (long) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1272 {
1273 line = ml_get_buf(vimbuf, lnum, FALSE);
1274 XPUSHs(sv_2mortal(newSVpv((char *)line, 0)));
1275 }
1276 }
1277 }
1278
1279void
1280Set(vimbuf, ...)
1281 VIBUF vimbuf;
1282
1283 PREINIT:
1284 int i;
1285 long lnum;
1286 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 PPCODE:
1288 if (buf_valid(vimbuf))
1289 {
1290 if (items < 3)
1291 croak("Usage: VIBUF::Set(vimbuf, lnum, @lines)");
1292
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001293 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 for(i = 2; i < items; i++, lnum++)
1295 {
1296 line = SvPV(ST(i),PL_na);
1297 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1298 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001299 aco_save_T aco;
1300
1301 /* set curwin/curbuf for "vimbuf" and save some things */
1302 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001303
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 if (u_savesub(lnum) == OK)
1305 {
1306 ml_replace(lnum, (char_u *)line, TRUE);
1307 changed_bytes(lnum, 0);
1308 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001309
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001310 /* restore curwin/curbuf and a few other things */
1311 aucmd_restbuf(&aco);
1312 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 }
1314 }
1315 }
1316
1317void
1318Delete(vimbuf, ...)
1319 VIBUF vimbuf;
1320
1321 PREINIT:
1322 long i, lnum = 0, count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 PPCODE:
1324 if (buf_valid(vimbuf))
1325 {
1326 if (items == 2)
1327 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001328 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 count = 1;
1330 }
1331 else if (items == 3)
1332 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001333 lnum = (long) SvIV(ST(1));
1334 count = (long) 1 + SvIV(ST(2)) - lnum;
Bram Moolenaara1711622011-07-27 14:15:46 +02001335 if (count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 count = 1;
Bram Moolenaara1711622011-07-27 14:15:46 +02001337 if (count < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 {
1339 lnum -= count;
1340 count = -count;
1341 }
1342 }
1343 if (items >= 2)
1344 {
1345 for (i = 0; i < count; i++)
1346 {
1347 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1348 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001349 aco_save_T aco;
1350
1351 /* set curwin/curbuf for "vimbuf" and save some things */
1352 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001353
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 if (u_savedel(lnum, 1) == OK)
1355 {
1356 ml_delete(lnum, 0);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00001357 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 deleted_lines_mark(lnum, 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001360
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001361 /* restore curwin/curbuf and a few other things */
1362 aucmd_restbuf(&aco);
1363 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001364
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 update_curbuf(VALID);
1366 }
1367 }
1368 }
1369 }
1370
1371void
1372Append(vimbuf, ...)
1373 VIBUF vimbuf;
1374
1375 PREINIT:
1376 int i;
1377 long lnum;
1378 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 PPCODE:
1380 if (buf_valid(vimbuf))
1381 {
1382 if (items < 3)
1383 croak("Usage: VIBUF::Append(vimbuf, lnum, @lines)");
1384
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001385 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 for (i = 2; i < items; i++, lnum++)
1387 {
1388 line = SvPV(ST(i),PL_na);
1389 if (lnum >= 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1390 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001391 aco_save_T aco;
1392
1393 /* set curwin/curbuf for "vimbuf" and save some things */
1394 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001395
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 if (u_inssub(lnum + 1) == OK)
1397 {
1398 ml_append(lnum, (char_u *)line, (colnr_T)0, FALSE);
1399 appended_lines_mark(lnum, 1L);
1400 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001401
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001402 /* restore curwin/curbuf and a few other things */
1403 aucmd_restbuf(&aco);
1404 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001405
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 update_curbuf(VALID);
1407 }
1408 }
1409 }
1410