blob: b723dcbe5b319fb84a48c7f9d4db616227157570 [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. */
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100120# define CHECK_EVAL_ERR(len) SvPV(perl_get_sv("@", GV_ADD), (len));
121#else
122# define CHECK_EVAL_ERR(len) SvPV(GvSV(PL_errgv), (len));
Bram Moolenaarc271c482012-08-08 13:17:31 +0200123#endif
124
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125/* Compatibility hacks over */
126
127static PerlInterpreter *perl_interp = NULL;
Bram Moolenaard99df422016-01-29 23:20:40 +0100128static void xs_init(pTHX);
129static void VIM_init(void);
130EXTERN_C void boot_DynaLoader(pTHX_ CV*);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131
132/*
Bram Moolenaare06c1882010-07-21 22:05:20 +0200133 * For dynamic linked perl.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 */
135#if defined(DYNAMIC_PERL) || defined(PROTO)
Bram Moolenaare06c1882010-07-21 22:05:20 +0200136
137#ifndef DYNAMIC_PERL /* just generating prototypes */
Bram Moolenaar766fb0d2010-07-22 11:34:16 +0200138#ifdef WIN3264
Bram Moolenaare06c1882010-07-21 22:05:20 +0200139typedef int HANDLE;
140#endif
141typedef int XSINIT_t;
142typedef int XSUBADDR_t;
Bram Moolenaard8619992014-03-12 17:08:05 +0100143#endif
144#ifndef USE_ITHREADS
Bram Moolenaare06c1882010-07-21 22:05:20 +0200145typedef int perl_key;
146#endif
147
Bram Moolenaar766fb0d2010-07-22 11:34:16 +0200148#ifndef WIN3264
Bram Moolenaare06c1882010-07-21 22:05:20 +0200149#include <dlfcn.h>
150#define HANDLE void*
151#define PERL_PROC void*
152#define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
153#define symbol_from_dll dlsym
154#define close_dll dlclose
155#else
156#define PERL_PROC FARPROC
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200157#define load_dll vimLoadLib
Bram Moolenaare06c1882010-07-21 22:05:20 +0200158#define symbol_from_dll GetProcAddress
159#define close_dll FreeLibrary
160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161/*
162 * Wrapper defines
163 */
164# define perl_alloc dll_perl_alloc
165# define perl_construct dll_perl_construct
166# define perl_parse dll_perl_parse
167# define perl_run dll_perl_run
168# define perl_destruct dll_perl_destruct
169# define perl_free dll_perl_free
170# define Perl_get_context dll_Perl_get_context
171# define Perl_croak dll_Perl_croak
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100172# ifdef PERL5101_OR_LATER
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100173# define Perl_croak_xs_usage dll_Perl_croak_xs_usage
174# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175# ifndef PROTO
176# define Perl_croak_nocontext dll_Perl_croak_nocontext
177# define Perl_call_argv dll_Perl_call_argv
178# define Perl_call_pv dll_Perl_call_pv
179# define Perl_eval_sv dll_Perl_eval_sv
180# define Perl_get_sv dll_Perl_get_sv
181# define Perl_eval_pv dll_Perl_eval_pv
182# define Perl_call_method dll_Perl_call_method
183# endif
184# define Perl_dowantarray dll_Perl_dowantarray
185# define Perl_free_tmps dll_Perl_free_tmps
186# define Perl_gv_stashpv dll_Perl_gv_stashpv
187# define Perl_markstack_grow dll_Perl_markstack_grow
188# define Perl_mg_find dll_Perl_mg_find
189# define Perl_newXS dll_Perl_newXS
190# define Perl_newSV dll_Perl_newSV
191# define Perl_newSViv dll_Perl_newSViv
192# define Perl_newSVpv dll_Perl_newSVpv
193# define Perl_pop_scope dll_Perl_pop_scope
194# define Perl_push_scope dll_Perl_push_scope
195# define Perl_save_int dll_Perl_save_int
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200196# if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
197# define Perl_save_strlen dll_Perl_save_strlen
198# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199# define Perl_stack_grow dll_Perl_stack_grow
200# define Perl_set_context dll_Perl_set_context
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200201# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200202# define Perl_sv_2bool_flags dll_Perl_sv_2bool_flags
203# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
204# define Perl_xs_apiversion_bootcheck dll_Perl_xs_apiversion_bootcheck
205# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200206# else
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200207# define Perl_sv_2bool dll_Perl_sv_2bool
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200208# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209# define Perl_sv_2iv dll_Perl_sv_2iv
210# define Perl_sv_2mortal dll_Perl_sv_2mortal
211# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
212# define Perl_sv_2pv_flags dll_Perl_sv_2pv_flags
213# define Perl_sv_2pv_nolen dll_Perl_sv_2pv_nolen
214# else
215# define Perl_sv_2pv dll_Perl_sv_2pv
216# endif
217# define Perl_sv_bless dll_Perl_sv_bless
218# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
219# define Perl_sv_catpvn_flags dll_Perl_sv_catpvn_flags
220# else
221# define Perl_sv_catpvn dll_Perl_sv_catpvn
222# endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000223#ifdef PERL589_OR_LATER
224# define Perl_sv_2iv_flags dll_Perl_sv_2iv_flags
225# define Perl_newXS_flags dll_Perl_newXS_flags
226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227# define Perl_sv_free dll_Perl_sv_free
Bram Moolenaarccf22172008-09-01 15:56:45 +0000228# if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
229# define Perl_sv_free2 dll_Perl_sv_free2
230# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231# define Perl_sv_isa dll_Perl_sv_isa
232# define Perl_sv_magic dll_Perl_sv_magic
233# define Perl_sv_setiv dll_Perl_sv_setiv
234# define Perl_sv_setpv dll_Perl_sv_setpv
235# define Perl_sv_setpvn dll_Perl_sv_setpvn
236# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
237# define Perl_sv_setsv_flags dll_Perl_sv_setsv_flags
238# else
239# define Perl_sv_setsv dll_Perl_sv_setsv
240# endif
241# define Perl_sv_upgrade dll_Perl_sv_upgrade
242# define Perl_Tstack_sp_ptr dll_Perl_Tstack_sp_ptr
243# define Perl_Top_ptr dll_Perl_Top_ptr
244# define Perl_Tstack_base_ptr dll_Perl_Tstack_base_ptr
245# define Perl_Tstack_max_ptr dll_Perl_Tstack_max_ptr
246# define Perl_Ttmps_ix_ptr dll_Perl_Ttmps_ix_ptr
247# define Perl_Ttmps_floor_ptr dll_Perl_Ttmps_floor_ptr
248# define Perl_Tmarkstack_ptr_ptr dll_Perl_Tmarkstack_ptr_ptr
249# define Perl_Tmarkstack_max_ptr dll_Perl_Tmarkstack_max_ptr
250# define Perl_TSv_ptr dll_Perl_TSv_ptr
251# define Perl_TXpv_ptr dll_Perl_TXpv_ptr
252# define Perl_Tna_ptr dll_Perl_Tna_ptr
253# define Perl_Idefgv_ptr dll_Perl_Idefgv_ptr
254# define Perl_Ierrgv_ptr dll_Perl_Ierrgv_ptr
255# define Perl_Isv_yes_ptr dll_Perl_Isv_yes_ptr
256# define boot_DynaLoader dll_boot_DynaLoader
Bram Moolenaare06c1882010-07-21 22:05:20 +0200257# define Perl_Gthr_key_ptr dll_Perl_Gthr_key_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000259# define Perl_sys_init dll_Perl_sys_init
Bram Moolenaarc236c162008-07-13 17:41:49 +0000260# define Perl_sys_term dll_Perl_sys_term
261# define Perl_ISv_ptr dll_Perl_ISv_ptr
262# define Perl_Istack_max_ptr dll_Perl_Istack_max_ptr
263# define Perl_Istack_base_ptr dll_Perl_Istack_base_ptr
264# define Perl_Itmps_ix_ptr dll_Perl_Itmps_ix_ptr
265# define Perl_Itmps_floor_ptr dll_Perl_Itmps_floor_ptr
266# define Perl_IXpv_ptr dll_Perl_IXpv_ptr
267# define Perl_Ina_ptr dll_Perl_Ina_ptr
268# define Perl_Imarkstack_ptr_ptr dll_Perl_Imarkstack_ptr_ptr
269# define Perl_Imarkstack_max_ptr dll_Perl_Imarkstack_max_ptr
270# define Perl_Istack_sp_ptr dll_Perl_Istack_sp_ptr
271# define Perl_Iop_ptr dll_Perl_Iop_ptr
272# define Perl_call_list dll_Perl_call_list
273# define Perl_Iscopestack_ix_ptr dll_Perl_Iscopestack_ix_ptr
274# define Perl_Iunitcheckav_ptr dll_Perl_Iunitcheckav_ptr
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200275# if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
276# define Perl_xs_handshake dll_Perl_xs_handshake
277# define Perl_xs_boot_epilog dll_Perl_xs_boot_epilog
278# endif
Bram Moolenaar01c10522012-09-21 12:50:51 +0200279# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100280# ifdef USE_ITHREADS
281# define PL_thr_key *dll_PL_thr_key
282# endif
Bram Moolenaar01c10522012-09-21 12:50:51 +0200283# endif
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100284# define Perl_hv_iternext_flags dll_Perl_hv_iternext_flags
285# define Perl_hv_iterinit dll_Perl_hv_iterinit
286# define Perl_hv_iterkey dll_Perl_hv_iterkey
287# define Perl_hv_iterval dll_Perl_hv_iterval
288# define Perl_av_fetch dll_Perl_av_fetch
289# define Perl_av_len dll_Perl_av_len
290# define Perl_sv_2nv_flags dll_Perl_sv_2nv_flags
Bram Moolenaarc236c162008-07-13 17:41:49 +0000291
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292/*
293 * Declare HANDLE for perl.dll and function pointers.
294 */
295static HANDLE hPerlLib = NULL;
296
297static PerlInterpreter* (*perl_alloc)();
298static void (*perl_construct)(PerlInterpreter*);
299static void (*perl_destruct)(PerlInterpreter*);
300static void (*perl_free)(PerlInterpreter*);
301static int (*perl_run)(PerlInterpreter*);
302static int (*perl_parse)(PerlInterpreter*, XSINIT_t, int, char**, char**);
303static void* (*Perl_get_context)(void);
Bram Moolenaara7ecc562006-08-16 16:17:39 +0000304static void (*Perl_croak)(pTHX_ const char*, ...);
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100305#ifdef PERL5101_OR_LATER
Bram Moolenaar6b107212013-12-11 15:06:40 +0100306/* Perl-5.18 has a different Perl_croak_xs_usage signature. */
307# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
308static void (*Perl_croak_xs_usage)(const CV *const, const char *const params);
309# else
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100310static void (*Perl_croak_xs_usage)(pTHX_ const CV *const, const char *const params);
Bram Moolenaar6b107212013-12-11 15:06:40 +0100311# endif
Bram Moolenaar9be6e212013-06-15 16:47:35 +0200312#endif
Bram Moolenaara7ecc562006-08-16 16:17:39 +0000313static void (*Perl_croak_nocontext)(const char*, ...);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314static I32 (*Perl_dowantarray)(pTHX);
315static void (*Perl_free_tmps)(pTHX);
316static HV* (*Perl_gv_stashpv)(pTHX_ const char*, I32);
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200317#if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
318static I32* (*Perl_markstack_grow)(pTHX);
319#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320static void (*Perl_markstack_grow)(pTHX);
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322static MAGIC* (*Perl_mg_find)(pTHX_ SV*, int);
323static CV* (*Perl_newXS)(pTHX_ char*, XSUBADDR_t, char*);
324static SV* (*Perl_newSV)(pTHX_ STRLEN);
325static SV* (*Perl_newSViv)(pTHX_ IV);
326static SV* (*Perl_newSVpv)(pTHX_ const char*, STRLEN);
327static I32 (*Perl_call_argv)(pTHX_ const char*, I32, char**);
328static I32 (*Perl_call_pv)(pTHX_ const char*, I32);
329static I32 (*Perl_eval_sv)(pTHX_ SV*, I32);
330static SV* (*Perl_get_sv)(pTHX_ const char*, I32);
331static SV* (*Perl_eval_pv)(pTHX_ const char*, I32);
332static SV* (*Perl_call_method)(pTHX_ const char*, I32);
333static void (*Perl_pop_scope)(pTHX);
334static void (*Perl_push_scope)(pTHX);
335static void (*Perl_save_int)(pTHX_ int*);
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200336#if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
337static void (*Perl_save_strlen)(pTHX_ STRLEN* ptr);
338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339static SV** (*Perl_stack_grow)(pTHX_ SV**, SV**p, int);
340static SV** (*Perl_set_context)(void*);
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200341#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
342static bool (*Perl_sv_2bool_flags)(pTHX_ SV*, I32);
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200343# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200344static void (*Perl_xs_apiversion_bootcheck)(pTHX_ SV *module, const char *api_p, STRLEN api_len);
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200345# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200346#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347static bool (*Perl_sv_2bool)(pTHX_ SV*);
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349static IV (*Perl_sv_2iv)(pTHX_ SV*);
350static SV* (*Perl_sv_2mortal)(pTHX_ SV*);
351#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
352static char* (*Perl_sv_2pv_flags)(pTHX_ SV*, STRLEN*, I32);
353static char* (*Perl_sv_2pv_nolen)(pTHX_ SV*);
354#else
355static char* (*Perl_sv_2pv)(pTHX_ SV*, STRLEN*);
356#endif
357static SV* (*Perl_sv_bless)(pTHX_ SV*, HV*);
358#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
359static void (*Perl_sv_catpvn_flags)(pTHX_ SV* , const char*, STRLEN, I32);
360#else
361static void (*Perl_sv_catpvn)(pTHX_ SV*, const char*, STRLEN);
362#endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000363#ifdef PERL589_OR_LATER
364static IV (*Perl_sv_2iv_flags)(pTHX_ SV* sv, I32 flags);
365static CV * (*Perl_newXS_flags)(pTHX_ const char *name, XSUBADDR_t subaddr, const char *const filename, const char *const proto, U32 flags);
366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367static void (*Perl_sv_free)(pTHX_ SV*);
368static int (*Perl_sv_isa)(pTHX_ SV*, const char*);
369static void (*Perl_sv_magic)(pTHX_ SV*, SV*, int, const char*, I32);
370static void (*Perl_sv_setiv)(pTHX_ SV*, IV);
371static void (*Perl_sv_setpv)(pTHX_ SV*, const char*);
372static void (*Perl_sv_setpvn)(pTHX_ SV*, const char*, STRLEN);
373#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
374static void (*Perl_sv_setsv_flags)(pTHX_ SV*, SV*, I32);
375#else
376static void (*Perl_sv_setsv)(pTHX_ SV*, SV*);
377#endif
378static bool (*Perl_sv_upgrade)(pTHX_ SV*, U32);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200379#if (PERL_REVISION == 5) && (PERL_VERSION < 10)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380static SV*** (*Perl_Tstack_sp_ptr)(register PerlInterpreter*);
381static OP** (*Perl_Top_ptr)(register PerlInterpreter*);
382static SV*** (*Perl_Tstack_base_ptr)(register PerlInterpreter*);
383static SV*** (*Perl_Tstack_max_ptr)(register PerlInterpreter*);
384static I32* (*Perl_Ttmps_ix_ptr)(register PerlInterpreter*);
385static I32* (*Perl_Ttmps_floor_ptr)(register PerlInterpreter*);
386static I32** (*Perl_Tmarkstack_ptr_ptr)(register PerlInterpreter*);
387static I32** (*Perl_Tmarkstack_max_ptr)(register PerlInterpreter*);
388static SV** (*Perl_TSv_ptr)(register PerlInterpreter*);
389static XPV** (*Perl_TXpv_ptr)(register PerlInterpreter*);
390static STRLEN* (*Perl_Tna_ptr)(register PerlInterpreter*);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200391#else
Bram Moolenaar6b107212013-12-11 15:06:40 +0100392/* Perl-5.18 has a different Perl_sv_free2 signature. */
393# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
394static void (*Perl_sv_free2)(pTHX_ SV*, const U32);
395# else
Bram Moolenaarccf22172008-09-01 15:56:45 +0000396static void (*Perl_sv_free2)(pTHX_ SV*);
Bram Moolenaar6b107212013-12-11 15:06:40 +0100397# endif
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000398static void (*Perl_sys_init)(int* argc, char*** argv);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000399static void (*Perl_sys_term)(void);
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200400static void (*Perl_call_list)(pTHX_ I32, AV*);
401# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
402# else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000403static SV** (*Perl_ISv_ptr)(register PerlInterpreter*);
404static SV*** (*Perl_Istack_max_ptr)(register PerlInterpreter*);
405static SV*** (*Perl_Istack_base_ptr)(register PerlInterpreter*);
406static XPV** (*Perl_IXpv_ptr)(register PerlInterpreter*);
407static I32* (*Perl_Itmps_ix_ptr)(register PerlInterpreter*);
408static I32* (*Perl_Itmps_floor_ptr)(register PerlInterpreter*);
409static STRLEN* (*Perl_Ina_ptr)(register PerlInterpreter*);
410static I32** (*Perl_Imarkstack_ptr_ptr)(register PerlInterpreter*);
411static I32** (*Perl_Imarkstack_max_ptr)(register PerlInterpreter*);
412static SV*** (*Perl_Istack_sp_ptr)(register PerlInterpreter*);
413static OP** (*Perl_Iop_ptr)(register PerlInterpreter*);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000414static I32* (*Perl_Iscopestack_ix_ptr)(register PerlInterpreter*);
415static AV** (*Perl_Iunitcheckav_ptr)(register PerlInterpreter*);
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200416# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000417#endif
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200418#if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
419static I32 (*Perl_xs_handshake)(const U32, void *, const char *, ...);
420static void (*Perl_xs_boot_epilog)(pTHX_ const U32);
421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200423#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100424# ifdef USE_ITHREADS
Bram Moolenaar01c10522012-09-21 12:50:51 +0200425static perl_key* dll_PL_thr_key;
Bram Moolenaard8619992014-03-12 17:08:05 +0100426# endif
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200427#else
Bram Moolenaare06c1882010-07-21 22:05:20 +0200428static GV** (*Perl_Idefgv_ptr)(register PerlInterpreter*);
429static GV** (*Perl_Ierrgv_ptr)(register PerlInterpreter*);
430static SV* (*Perl_Isv_yes_ptr)(register PerlInterpreter*);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200431static perl_key* (*Perl_Gthr_key_ptr)_((pTHX));
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200432#endif
433static void (*boot_DynaLoader)_((pTHX_ CV*));
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100434static HE * (*Perl_hv_iternext_flags)(pTHX_ HV *, I32);
435static I32 (*Perl_hv_iterinit)(pTHX_ HV *);
436static char * (*Perl_hv_iterkey)(pTHX_ HE *, I32 *);
437static SV * (*Perl_hv_iterval)(pTHX_ HV *, HE *);
438static SV** (*Perl_av_fetch)(pTHX_ AV *, SSize_t, I32);
439static SSize_t (*Perl_av_len)(pTHX_ AV *);
440static NV (*Perl_sv_2nv_flags)(pTHX_ SV *const, const I32);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200441
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442/*
443 * Table of name to function pointer of perl.
444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445static struct {
446 char* name;
447 PERL_PROC* ptr;
448} perl_funcname_table[] = {
449 {"perl_alloc", (PERL_PROC*)&perl_alloc},
450 {"perl_construct", (PERL_PROC*)&perl_construct},
451 {"perl_destruct", (PERL_PROC*)&perl_destruct},
452 {"perl_free", (PERL_PROC*)&perl_free},
453 {"perl_run", (PERL_PROC*)&perl_run},
454 {"perl_parse", (PERL_PROC*)&perl_parse},
455 {"Perl_get_context", (PERL_PROC*)&Perl_get_context},
456 {"Perl_croak", (PERL_PROC*)&Perl_croak},
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100457#ifdef PERL5101_OR_LATER
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100458 {"Perl_croak_xs_usage", (PERL_PROC*)&Perl_croak_xs_usage},
459#endif
Bram Moolenaard8619992014-03-12 17:08:05 +0100460#ifdef PERL_IMPLICIT_CONTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 {"Perl_croak_nocontext", (PERL_PROC*)&Perl_croak_nocontext},
Bram Moolenaard8619992014-03-12 17:08:05 +0100462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 {"Perl_dowantarray", (PERL_PROC*)&Perl_dowantarray},
464 {"Perl_free_tmps", (PERL_PROC*)&Perl_free_tmps},
465 {"Perl_gv_stashpv", (PERL_PROC*)&Perl_gv_stashpv},
466 {"Perl_markstack_grow", (PERL_PROC*)&Perl_markstack_grow},
467 {"Perl_mg_find", (PERL_PROC*)&Perl_mg_find},
468 {"Perl_newXS", (PERL_PROC*)&Perl_newXS},
469 {"Perl_newSV", (PERL_PROC*)&Perl_newSV},
470 {"Perl_newSViv", (PERL_PROC*)&Perl_newSViv},
471 {"Perl_newSVpv", (PERL_PROC*)&Perl_newSVpv},
472 {"Perl_call_argv", (PERL_PROC*)&Perl_call_argv},
473 {"Perl_call_pv", (PERL_PROC*)&Perl_call_pv},
474 {"Perl_eval_sv", (PERL_PROC*)&Perl_eval_sv},
475 {"Perl_get_sv", (PERL_PROC*)&Perl_get_sv},
476 {"Perl_eval_pv", (PERL_PROC*)&Perl_eval_pv},
477 {"Perl_call_method", (PERL_PROC*)&Perl_call_method},
478 {"Perl_pop_scope", (PERL_PROC*)&Perl_pop_scope},
479 {"Perl_push_scope", (PERL_PROC*)&Perl_push_scope},
480 {"Perl_save_int", (PERL_PROC*)&Perl_save_int},
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200481#if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
482 {"Perl_save_strlen", (PERL_PROC*)&Perl_save_strlen},
483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 {"Perl_stack_grow", (PERL_PROC*)&Perl_stack_grow},
485 {"Perl_set_context", (PERL_PROC*)&Perl_set_context},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200486#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
487 {"Perl_sv_2bool_flags", (PERL_PROC*)&Perl_sv_2bool_flags},
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200488# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200489 {"Perl_xs_apiversion_bootcheck",(PERL_PROC*)&Perl_xs_apiversion_bootcheck},
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200490# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200491#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 {"Perl_sv_2bool", (PERL_PROC*)&Perl_sv_2bool},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 {"Perl_sv_2iv", (PERL_PROC*)&Perl_sv_2iv},
495 {"Perl_sv_2mortal", (PERL_PROC*)&Perl_sv_2mortal},
496#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
497 {"Perl_sv_2pv_flags", (PERL_PROC*)&Perl_sv_2pv_flags},
498 {"Perl_sv_2pv_nolen", (PERL_PROC*)&Perl_sv_2pv_nolen},
499#else
500 {"Perl_sv_2pv", (PERL_PROC*)&Perl_sv_2pv},
501#endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000502#ifdef PERL589_OR_LATER
503 {"Perl_sv_2iv_flags", (PERL_PROC*)&Perl_sv_2iv_flags},
504 {"Perl_newXS_flags", (PERL_PROC*)&Perl_newXS_flags},
505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 {"Perl_sv_bless", (PERL_PROC*)&Perl_sv_bless},
507#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
508 {"Perl_sv_catpvn_flags", (PERL_PROC*)&Perl_sv_catpvn_flags},
509#else
510 {"Perl_sv_catpvn", (PERL_PROC*)&Perl_sv_catpvn},
511#endif
512 {"Perl_sv_free", (PERL_PROC*)&Perl_sv_free},
513 {"Perl_sv_isa", (PERL_PROC*)&Perl_sv_isa},
514 {"Perl_sv_magic", (PERL_PROC*)&Perl_sv_magic},
515 {"Perl_sv_setiv", (PERL_PROC*)&Perl_sv_setiv},
516 {"Perl_sv_setpv", (PERL_PROC*)&Perl_sv_setpv},
517 {"Perl_sv_setpvn", (PERL_PROC*)&Perl_sv_setpvn},
518#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
519 {"Perl_sv_setsv_flags", (PERL_PROC*)&Perl_sv_setsv_flags},
520#else
521 {"Perl_sv_setsv", (PERL_PROC*)&Perl_sv_setsv},
522#endif
523 {"Perl_sv_upgrade", (PERL_PROC*)&Perl_sv_upgrade},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000524#if (PERL_REVISION == 5) && (PERL_VERSION < 10)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 {"Perl_Tstack_sp_ptr", (PERL_PROC*)&Perl_Tstack_sp_ptr},
526 {"Perl_Top_ptr", (PERL_PROC*)&Perl_Top_ptr},
527 {"Perl_Tstack_base_ptr", (PERL_PROC*)&Perl_Tstack_base_ptr},
528 {"Perl_Tstack_max_ptr", (PERL_PROC*)&Perl_Tstack_max_ptr},
529 {"Perl_Ttmps_ix_ptr", (PERL_PROC*)&Perl_Ttmps_ix_ptr},
530 {"Perl_Ttmps_floor_ptr", (PERL_PROC*)&Perl_Ttmps_floor_ptr},
531 {"Perl_Tmarkstack_ptr_ptr", (PERL_PROC*)&Perl_Tmarkstack_ptr_ptr},
532 {"Perl_Tmarkstack_max_ptr", (PERL_PROC*)&Perl_Tmarkstack_max_ptr},
533 {"Perl_TSv_ptr", (PERL_PROC*)&Perl_TSv_ptr},
534 {"Perl_TXpv_ptr", (PERL_PROC*)&Perl_TXpv_ptr},
535 {"Perl_Tna_ptr", (PERL_PROC*)&Perl_Tna_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000536#else
Bram Moolenaarccf22172008-09-01 15:56:45 +0000537 {"Perl_sv_free2", (PERL_PROC*)&Perl_sv_free2},
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000538 {"Perl_sys_init", (PERL_PROC*)&Perl_sys_init},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000539 {"Perl_sys_term", (PERL_PROC*)&Perl_sys_term},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200540 {"Perl_call_list", (PERL_PROC*)&Perl_call_list},
541# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
542# else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000543 {"Perl_ISv_ptr", (PERL_PROC*)&Perl_ISv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000544 {"Perl_Istack_max_ptr", (PERL_PROC*)&Perl_Istack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200545 {"Perl_Istack_base_ptr", (PERL_PROC*)&Perl_Istack_base_ptr},
546 {"Perl_IXpv_ptr", (PERL_PROC*)&Perl_IXpv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000547 {"Perl_Itmps_ix_ptr", (PERL_PROC*)&Perl_Itmps_ix_ptr},
548 {"Perl_Itmps_floor_ptr", (PERL_PROC*)&Perl_Itmps_floor_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200549 {"Perl_Ina_ptr", (PERL_PROC*)&Perl_Ina_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000550 {"Perl_Imarkstack_ptr_ptr", (PERL_PROC*)&Perl_Imarkstack_ptr_ptr},
551 {"Perl_Imarkstack_max_ptr", (PERL_PROC*)&Perl_Imarkstack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200552 {"Perl_Istack_sp_ptr", (PERL_PROC*)&Perl_Istack_sp_ptr},
553 {"Perl_Iop_ptr", (PERL_PROC*)&Perl_Iop_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000554 {"Perl_Iscopestack_ix_ptr", (PERL_PROC*)&Perl_Iscopestack_ix_ptr},
555 {"Perl_Iunitcheckav_ptr", (PERL_PROC*)&Perl_Iunitcheckav_ptr},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200556# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000557#endif
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200558#if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
559 {"Perl_xs_handshake", (PERL_PROC*)&Perl_xs_handshake},
560 {"Perl_xs_boot_epilog", (PERL_PROC*)&Perl_xs_boot_epilog},
561#endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200562#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100563# ifdef USE_ITHREADS
Bram Moolenaar01c10522012-09-21 12:50:51 +0200564 {"PL_thr_key", (PERL_PROC*)&dll_PL_thr_key},
Bram Moolenaard8619992014-03-12 17:08:05 +0100565# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200566#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 {"Perl_Idefgv_ptr", (PERL_PROC*)&Perl_Idefgv_ptr},
568 {"Perl_Ierrgv_ptr", (PERL_PROC*)&Perl_Ierrgv_ptr},
569 {"Perl_Isv_yes_ptr", (PERL_PROC*)&Perl_Isv_yes_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200570 {"Perl_Gthr_key_ptr", (PERL_PROC*)&Perl_Gthr_key_ptr},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200571#endif
572 {"boot_DynaLoader", (PERL_PROC*)&boot_DynaLoader},
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100573 {"Perl_hv_iternext_flags", (PERL_PROC*)&Perl_hv_iternext_flags},
574 {"Perl_hv_iterinit", (PERL_PROC*)&Perl_hv_iterinit},
575 {"Perl_hv_iterkey", (PERL_PROC*)&Perl_hv_iterkey},
576 {"Perl_hv_iterval", (PERL_PROC*)&Perl_hv_iterval},
577 {"Perl_av_fetch", (PERL_PROC*)&Perl_av_fetch},
578 {"Perl_av_len", (PERL_PROC*)&Perl_av_len},
579 {"Perl_sv_2nv_flags", (PERL_PROC*)&Perl_sv_2nv_flags},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 {"", NULL},
581};
582
Bram Moolenaar6b107212013-12-11 15:06:40 +0100583/* Work around for perl-5.18.
584 * The definitions of S_SvREFCNT_inc and S_SvREFCNT_dec are needed, so include
585 * "perl\lib\CORE\inline.h", after Perl_sv_free2 is defined.
586 * The linker won't complain about undefined __impl_Perl_sv_free2. */
587#if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
588# include <inline.h>
589#endif
590
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591/*
592 * Make all runtime-links of perl.
593 *
Bram Moolenaar364ab2f2013-08-02 20:05:32 +0200594 * 1. Get module handle using dlopen() or vimLoadLib().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 * 2. Get pointer to perl function by GetProcAddress.
596 * 3. Repeat 2, until get all functions will be used.
597 *
598 * Parameter 'libname' provides name of DLL.
599 * Return OK or FAIL.
600 */
601 static int
602perl_runtime_link_init(char *libname, int verbose)
603{
604 int i;
605
606 if (hPerlLib != NULL)
607 return OK;
Bram Moolenaare06c1882010-07-21 22:05:20 +0200608 if ((hPerlLib = load_dll(libname)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 {
610 if (verbose)
611 EMSG2(_("E370: Could not load library %s"), libname);
612 return FAIL;
613 }
614 for (i = 0; perl_funcname_table[i].ptr; ++i)
615 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200616 if (!(*perl_funcname_table[i].ptr = symbol_from_dll(hPerlLib,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 perl_funcname_table[i].name)))
618 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200619 close_dll(hPerlLib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 hPerlLib = NULL;
621 if (verbose)
622 EMSG2(_(e_loadfunc), perl_funcname_table[i].name);
623 return FAIL;
624 }
625 }
626 return OK;
627}
628
629/*
630 * If runtime-link-perl(DLL) was loaded successfully, return TRUE.
631 * There were no DLL loaded, return FALSE.
632 */
633 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100634perl_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100636 return perl_runtime_link_init((char *)p_perldll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637}
638#endif /* DYNAMIC_PERL */
639
640/*
641 * perl_init(): initialize perl interpreter
642 * We have to call perl_parse to initialize some structures,
643 * there's nothing to actually parse.
644 */
645 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100646perl_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647{
Bram Moolenaarc236c162008-07-13 17:41:49 +0000648 char *bootargs[] = { "VI", NULL };
649 int argc = 3;
650 static char *argv[] = { "", "-e", "" };
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651
Bram Moolenaarc236c162008-07-13 17:41:49 +0000652#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000653 Perl_sys_init(&argc, (char***)&argv);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 perl_interp = perl_alloc();
656 perl_construct(perl_interp);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000657 perl_parse(perl_interp, xs_init, argc, argv, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 perl_call_argv("VIM::bootstrap", (long)G_DISCARD, bootargs);
659 VIM_init();
660#ifdef USE_SFIO
661 sfdisc(PerlIO_stdout(), sfdcnewvim());
662 sfdisc(PerlIO_stderr(), sfdcnewvim());
663 sfsetbuf(PerlIO_stdout(), NULL, 0);
664 sfsetbuf(PerlIO_stderr(), NULL, 0);
665#endif
666}
667
668/*
669 * perl_end(): clean up after ourselves
670 */
671 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100672perl_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673{
674 if (perl_interp)
675 {
676 perl_run(perl_interp);
677 perl_destruct(perl_interp);
678 perl_free(perl_interp);
679 perl_interp = NULL;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000680#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100681 Perl_sys_term();
Bram Moolenaarc236c162008-07-13 17:41:49 +0000682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 }
684#ifdef DYNAMIC_PERL
685 if (hPerlLib)
686 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200687 close_dll(hPerlLib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 hPerlLib = NULL;
689 }
690#endif
691}
692
693/*
694 * msg_split(): send a message to the message handling routines
695 * split at '\n' first though.
696 */
697 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100698msg_split(
699 char_u *s,
700 int attr) /* highlighting attributes */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701{
702 char *next;
703 char *token = (char *)s;
704
Bram Moolenaaraa8494a2007-10-09 08:47:27 +0000705 while ((next = strchr(token, '\n')) && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 {
707 *next++ = '\0'; /* replace \n with \0 */
708 msg_attr((char_u *)token, attr);
709 token = next;
710 }
Bram Moolenaaraa8494a2007-10-09 08:47:27 +0000711 if (*token && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 msg_attr((char_u *)token, attr);
713}
714
715#ifndef FEAT_EVAL
716/*
717 * This stub is needed because an "#ifdef FEAT_EVAL" around Eval() doesn't
718 * work properly.
719 */
720 char_u *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100721eval_to_string(
722 char_u *arg UNUSED,
723 char_u **nextcmd UNUSED,
724 int dolist UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725{
726 return NULL;
727}
728#endif
729
730/*
731 * Create a new reference to an SV pointing to the SCR structure
Bram Moolenaare344bea2005-09-01 20:46:49 +0000732 * The b_perl_private/w_perl_private part of the SCR structure points to the
733 * SV, so there can only be one such SV for a particular SCR structure. When
734 * the last reference has gone (DESTROY is called),
735 * b_perl_private/w_perl_private is reset; When the screen goes away before
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 * all references are gone, the value of the SV is reset;
737 * any subsequent use of any of those reference will produce
738 * a warning. (see typemap)
739 */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000740
741 static SV *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100742newWINrv(SV *rv, win_T *ptr)
Bram Moolenaare344bea2005-09-01 20:46:49 +0000743{
744 sv_upgrade(rv, SVt_RV);
745 if (ptr->w_perl_private == NULL)
746 {
747 ptr->w_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +0100748 sv_setiv(ptr->w_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +0000749 }
750 else
751 SvREFCNT_inc(ptr->w_perl_private);
752 SvRV(rv) = ptr->w_perl_private;
753 SvROK_on(rv);
754 return sv_bless(rv, gv_stashpv("VIWIN", TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755}
756
Bram Moolenaare344bea2005-09-01 20:46:49 +0000757 static SV *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100758newBUFrv(SV *rv, buf_T *ptr)
Bram Moolenaare344bea2005-09-01 20:46:49 +0000759{
760 sv_upgrade(rv, SVt_RV);
761 if (ptr->b_perl_private == NULL)
762 {
763 ptr->b_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +0100764 sv_setiv(ptr->b_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +0000765 }
766 else
767 SvREFCNT_inc(ptr->b_perl_private);
768 SvRV(rv) = ptr->b_perl_private;
769 SvROK_on(rv);
770 return sv_bless(rv, gv_stashpv("VIBUF", TRUE));
771}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772
773/*
774 * perl_win_free
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200775 * Remove all references to the window to be destroyed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 */
777 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100778perl_win_free(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000780 if (wp->w_perl_private)
781 sv_setiv((SV *)wp->w_perl_private, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 return;
783}
784
785 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100786perl_buf_free(buf_T *bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000788 if (bp->b_perl_private)
789 sv_setiv((SV *)bp->b_perl_private, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 return;
791}
792
793#ifndef PROTO
794# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
795I32 cur_val(pTHX_ IV iv, SV *sv);
796# else
797I32 cur_val(IV iv, SV *sv);
798#endif
799
800/*
801 * Handler for the magic variables $main::curwin and $main::curbuf.
802 * The handler is put into the magic vtbl for these variables.
803 * (This is effectively a C-level equivalent of a tied variable).
804 * There is no "set" function as the variables are read-only.
805 */
806# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
807I32 cur_val(pTHX_ IV iv, SV *sv)
808# else
809I32 cur_val(IV iv, SV *sv)
810# endif
811{
812 SV *rv;
813 if (iv == 0)
814 rv = newWINrv(newSV(0), curwin);
815 else
816 rv = newBUFrv(newSV(0), curbuf);
817 sv_setsv(sv, rv);
818 return 0;
819}
820#endif /* !PROTO */
821
822struct ufuncs cw_funcs = { cur_val, 0, 0 };
823struct ufuncs cb_funcs = { cur_val, 0, 1 };
824
825/*
826 * VIM_init(): Vim-specific initialisation.
827 * Make the magical main::curwin and main::curbuf variables
828 */
829 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100830VIM_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831{
832 static char cw[] = "main::curwin";
833 static char cb[] = "main::curbuf";
834 SV *sv;
835
836 sv = perl_get_sv(cw, TRUE);
837 sv_magic(sv, NULL, 'U', (char *)&cw_funcs, sizeof(cw_funcs));
838 SvREADONLY_on(sv);
839
840 sv = perl_get_sv(cb, TRUE);
841 sv_magic(sv, NULL, 'U', (char *)&cb_funcs, sizeof(cb_funcs));
842 SvREADONLY_on(sv);
843
844 /*
845 * Setup the Safe compartment.
846 * It shouldn't be a fatal error if the Safe module is missing.
847 * XXX: Only shares the 'Msg' routine (which has to be called
848 * like 'Msg(...)').
849 */
850 (void)perl_eval_pv( "if ( eval( 'require Safe' ) ) { $VIM::safe = Safe->new(); $VIM::safe->share_from( 'VIM', ['Msg'] ); }", G_DISCARD | G_VOID );
851
852}
853
854#ifdef DYNAMIC_PERL
855static char *e_noperl = N_("Sorry, this command is disabled: the Perl library could not be loaded.");
856#endif
857
858/*
859 * ":perl"
860 */
861 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100862ex_perl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863{
864 char *err;
865 char *script;
866 STRLEN length;
867 SV *sv;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200868#ifdef HAVE_SANDBOX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 SV *safe;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871
872 script = (char *)script_get(eap, eap->arg);
873 if (eap->skip)
874 {
875 vim_free(script);
876 return;
877 }
878
879 if (perl_interp == NULL)
880 {
881#ifdef DYNAMIC_PERL
882 if (!perl_enabled(TRUE))
883 {
884 EMSG(_(e_noperl));
885 vim_free(script);
886 return;
887 }
888#endif
889 perl_init();
890 }
891
892 {
893 dSP;
894 ENTER;
895 SAVETMPS;
896
897 if (script == NULL)
898 sv = newSVpv((char *)eap->arg, 0);
899 else
900 {
901 sv = newSVpv(script, 0);
902 vim_free(script);
903 }
904
905#ifdef HAVE_SANDBOX
906 if (sandbox)
907 {
Bram Moolenaara1711622011-07-27 14:15:46 +0200908 safe = perl_get_sv("VIM::safe", FALSE);
Bram Moolenaar3f947ea2009-07-14 14:04:54 +0000909# ifndef MAKE_TEST /* avoid a warning for unreachable code */
Bram Moolenaar954e8c52009-11-11 13:45:33 +0000910 if (safe == NULL || !SvTRUE(safe))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 EMSG(_("E299: Perl evaluation forbidden in sandbox without the Safe module"));
912 else
Bram Moolenaar3f947ea2009-07-14 14:04:54 +0000913# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 {
915 PUSHMARK(SP);
916 XPUSHs(safe);
917 XPUSHs(sv);
918 PUTBACK;
919 perl_call_method("reval", G_DISCARD);
920 }
921 }
922 else
923#endif
924 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
925
926 SvREFCNT_dec(sv);
927
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100928 err = CHECK_EVAL_ERR(length);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929
930 FREETMPS;
931 LEAVE;
932
933 if (!length)
934 return;
935
936 msg_split((char_u *)err, highlight_attr[HLF_E]);
937 return;
938 }
939}
940
941 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100942replace_line(linenr_T *line, linenr_T *end)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943{
944 char *str;
945
946 if (SvOK(GvSV(PL_defgv)))
947 {
948 str = SvPV(GvSV(PL_defgv), PL_na);
949 ml_replace(*line, (char_u *)str, 1);
950 changed_bytes(*line, 0);
951 }
952 else
953 {
954 ml_delete(*line, FALSE);
955 deleted_lines_mark(*line, 1L);
956 --(*end);
957 --(*line);
958 }
959 return OK;
960}
961
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100962static struct ref_map_S {
963 void *vim_ref;
964 SV *perl_ref;
965 struct ref_map_S *next;
966} *ref_map = NULL;
967
968 static void
969ref_map_free(void)
970{
971 struct ref_map_S *tofree;
972 struct ref_map_S *refs = ref_map;
973
974 while (refs) {
975 tofree = refs;
976 refs = refs->next;
977 vim_free(tofree);
978 }
979 ref_map = NULL;
980}
981
982 static struct ref_map_S *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100983ref_map_find_SV(SV *const sv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100984{
985 struct ref_map_S *refs = ref_map;
986 int count = 350;
987
988 while (refs) {
989 if (refs->perl_ref == sv)
990 break;
991 refs = refs->next;
992 count--;
993 }
994
995 if (!refs && count > 0) {
996 refs = (struct ref_map_S *)alloc(sizeof(struct ref_map_S));
997 if (!refs)
998 return NULL;
999 refs->perl_ref = sv;
1000 refs->vim_ref = NULL;
1001 refs->next = ref_map;
1002 ref_map = refs;
1003 }
1004
1005 return refs;
1006}
1007
1008 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001009perl_to_vim(SV *sv, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001010{
1011 if (SvROK(sv))
1012 sv = SvRV(sv);
1013
1014 switch (SvTYPE(sv)) {
1015 case SVt_NULL:
1016 break;
1017 case SVt_NV: /* float */
1018#ifdef FEAT_FLOAT
1019 rettv->v_type = VAR_FLOAT;
1020 rettv->vval.v_float = SvNV(sv);
1021 break;
1022#endif
1023 case SVt_IV: /* integer */
1024 if (!SvROK(sv)) { /* references should be string */
1025 rettv->vval.v_number = SvIV(sv);
1026 break;
1027 }
1028 case SVt_PV: /* string */
1029 {
1030 size_t len = 0;
1031 char * str_from = SvPV(sv, len);
1032 char_u *str_to = (char_u*)alloc(sizeof(char_u) * (len + 1));
1033
1034 if (str_to) {
1035 str_to[len] = '\0';
1036
1037 while (len--) {
1038 if (str_from[len] == '\0')
1039 str_to[len] = '\n';
1040 else
1041 str_to[len] = str_from[len];
1042 }
1043 }
1044
1045 rettv->v_type = VAR_STRING;
1046 rettv->vval.v_string = str_to;
1047 break;
1048 }
1049 case SVt_PVAV: /* list */
1050 {
1051 SSize_t size;
1052 listitem_T * item;
1053 SV ** item2;
1054 list_T * list;
1055 struct ref_map_S * refs;
1056
1057 if ((refs = ref_map_find_SV(sv)) == NULL)
1058 return FAIL;
1059
1060 if (refs->vim_ref)
1061 list = (list_T *) refs->vim_ref;
1062 else
1063 {
1064 if ((list = list_alloc()) == NULL)
1065 return FAIL;
1066 refs->vim_ref = list;
1067
1068 for (size = av_len((AV*)sv); size >= 0; size--)
1069 {
1070 if ((item = listitem_alloc()) == NULL)
1071 break;
1072
1073 item->li_tv.v_type = VAR_NUMBER;
1074 item->li_tv.v_lock = 0;
1075 item->li_tv.vval.v_number = 0;
1076 list_insert(list, item, list->lv_first);
1077
1078 item2 = av_fetch((AV *)sv, size, 0);
1079
1080 if (item2 == NULL || *item2 == NULL ||
Bram Moolenaard99df422016-01-29 23:20:40 +01001081 perl_to_vim(*item2, &item->li_tv) == FAIL)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001082 break;
1083 }
1084 }
1085
1086 list->lv_refcount++;
1087 rettv->v_type = VAR_LIST;
1088 rettv->vval.v_list = list;
1089 break;
1090 }
1091 case SVt_PVHV: /* dictionary */
1092 {
1093 HE * entry;
1094 size_t key_len;
1095 char * key;
1096 dictitem_T * item;
1097 SV * item2;
1098 dict_T * dict;
1099 struct ref_map_S * refs;
1100
1101 if ((refs = ref_map_find_SV(sv)) == NULL)
1102 return FAIL;
1103
1104 if (refs->vim_ref)
1105 dict = (dict_T *) refs->vim_ref;
1106 else
1107 {
1108
1109 if ((dict = dict_alloc()) == NULL)
1110 return FAIL;
1111 refs->vim_ref = dict;
1112
1113 hv_iterinit((HV *)sv);
1114
1115 for (entry = hv_iternext((HV *)sv); entry; entry = hv_iternext((HV *)sv))
1116 {
1117 key_len = 0;
1118 key = hv_iterkey(entry, (I32 *)&key_len);
1119
1120 if (!key || !key_len || strlen(key) < key_len) {
1121 EMSG2("Malformed key Dictionary '%s'", key && *key ? key : "(empty)");
1122 break;
1123 }
1124
1125 if ((item = dictitem_alloc((char_u *)key)) == NULL)
1126 break;
1127
1128 item->di_tv.v_type = VAR_NUMBER;
1129 item->di_tv.v_lock = 0;
1130 item->di_tv.vval.v_number = 0;
1131
1132 if (dict_add(dict, item) == FAIL) {
1133 dictitem_free(item);
1134 break;
1135 }
1136 item2 = hv_iterval((HV *)sv, entry);
1137 if (item2 == NULL || perl_to_vim(item2, &item->di_tv) == FAIL)
1138 break;
1139 }
1140 }
1141
1142 dict->dv_refcount++;
1143 rettv->v_type = VAR_DICT;
1144 rettv->vval.v_dict = dict;
1145 break;
1146 }
1147 default: /* not convertible */
1148 {
1149 char *val = SvPV_nolen(sv);
1150 rettv->v_type = VAR_STRING;
1151 rettv->vval.v_string = val ? vim_strsave((char_u *)val) : NULL;
1152 break;
1153 }
1154 }
1155 return OK;
1156}
1157
1158/*
1159 * "perleval()"
1160 */
1161 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001162do_perleval(char_u *str, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001163{
1164 char *err = NULL;
1165 STRLEN err_len = 0;
1166 SV *sv = NULL;
1167#ifdef HAVE_SANDBOX
1168 SV *safe;
1169#endif
1170
1171 if (perl_interp == NULL)
1172 {
1173#ifdef DYNAMIC_PERL
1174 if (!perl_enabled(TRUE))
1175 {
1176 EMSG(_(e_noperl));
1177 return;
1178 }
1179#endif
1180 perl_init();
1181 }
1182
1183 {
1184 dSP;
1185 ENTER;
1186 SAVETMPS;
1187
1188#ifdef HAVE_SANDBOX
1189 if (sandbox)
1190 {
1191 safe = get_sv("VIM::safe", FALSE);
1192# ifndef MAKE_TEST /* avoid a warning for unreachable code */
1193 if (safe == NULL || !SvTRUE(safe))
1194 EMSG(_("E299: Perl evaluation forbidden in sandbox without the Safe module"));
1195 else
1196# endif
1197 {
1198 sv = newSVpv((char *)str, 0);
1199 PUSHMARK(SP);
1200 XPUSHs(safe);
1201 XPUSHs(sv);
1202 PUTBACK;
1203 call_method("reval", G_SCALAR);
1204 SPAGAIN;
1205 SvREFCNT_dec(sv);
1206 sv = POPs;
1207 }
1208 }
1209 else
1210#endif /* HAVE_SANDBOX */
1211 sv = eval_pv((char *)str, 0);
1212
1213 if (sv) {
1214 perl_to_vim(sv, rettv);
1215 ref_map_free();
1216 err = CHECK_EVAL_ERR(err_len);
1217 }
1218 PUTBACK;
1219 FREETMPS;
1220 LEAVE;
1221 }
1222 if (err_len)
1223 msg_split((char_u *)err, highlight_attr[HLF_E]);
1224}
1225
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226/*
1227 * ":perldo".
1228 */
1229 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001230ex_perldo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231{
1232 STRLEN length;
1233 SV *sv;
1234 char *str;
1235 linenr_T i;
1236
1237 if (bufempty())
1238 return;
1239
1240 if (perl_interp == NULL)
1241 {
1242#ifdef DYNAMIC_PERL
1243 if (!perl_enabled(TRUE))
1244 {
1245 EMSG(_(e_noperl));
1246 return;
1247 }
1248#endif
1249 perl_init();
1250 }
1251 {
1252 dSP;
1253 length = strlen((char *)eap->arg);
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001254 sv = newSV(length + sizeof("sub VIM::perldo {") - 1 + 1);
1255 sv_setpvn(sv, "sub VIM::perldo {", sizeof("sub VIM::perldo {") - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 sv_catpvn(sv, (char *)eap->arg, length);
1257 sv_catpvn(sv, "}", 1);
1258 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
1259 SvREFCNT_dec(sv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001260 str = CHECK_EVAL_ERR(length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 if (length)
1262 goto err;
1263
1264 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
1265 return;
1266
1267 ENTER;
1268 SAVETMPS;
1269 for (i = eap->line1; i <= eap->line2; i++)
1270 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001271 sv_setpv(GvSV(PL_defgv), (char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 PUSHMARK(sp);
1273 perl_call_pv("VIM::perldo", G_SCALAR | G_EVAL);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001274 str = CHECK_EVAL_ERR(length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 if (length)
1276 break;
1277 SPAGAIN;
1278 if (SvTRUEx(POPs))
1279 {
1280 if (replace_line(&i, &eap->line2) != OK)
1281 {
1282 PUTBACK;
1283 break;
1284 }
1285 }
1286 PUTBACK;
1287 }
1288 FREETMPS;
1289 LEAVE;
1290 check_cursor();
1291 update_screen(NOT_VALID);
1292 if (!length)
1293 return;
1294
1295err:
1296 msg_split((char_u *)str, highlight_attr[HLF_E]);
1297 return;
1298 }
1299}
1300
Bram Moolenaar01dd60c2008-07-24 14:24:48 +00001301#ifndef FEAT_WINDOWS
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001302 int
1303win_valid(win_T *w)
1304{
1305 return TRUE;
1306}
1307 int
1308win_count(void)
1309{
1310 return 1;
1311}
1312 win_T *
1313win_find_nr(int n)
1314{
1315 return curwin;
1316}
Bram Moolenaar01dd60c2008-07-24 14:24:48 +00001317#endif
1318
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319XS(boot_VIM);
1320
1321 static void
1322xs_init(pTHX)
1323{
1324 char *file = __FILE__;
1325
1326 /* DynaLoader is a special case */
1327 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1328 newXS("VIM::bootstrap", boot_VIM, file);
1329}
1330
1331typedef win_T * VIWIN;
1332typedef buf_T * VIBUF;
1333
1334MODULE = VIM PACKAGE = VIM
1335
1336void
1337Msg(text, hl=NULL)
1338 char *text;
1339 char *hl;
1340
1341 PREINIT:
1342 int attr;
1343 int id;
1344
1345 PPCODE:
1346 if (text != NULL)
1347 {
1348 attr = 0;
1349 if (hl != NULL)
1350 {
1351 id = syn_name2id((char_u *)hl);
1352 if (id != 0)
1353 attr = syn_id2attr(id);
1354 }
1355 msg_split((char_u *)text, attr);
1356 }
1357
1358void
1359SetOption(line)
1360 char *line;
1361
1362 PPCODE:
1363 if (line != NULL)
1364 do_set((char_u *)line, 0);
1365 update_screen(NOT_VALID);
1366
1367void
1368DoCommand(line)
1369 char *line;
1370
1371 PPCODE:
1372 if (line != NULL)
1373 do_cmdline_cmd((char_u *)line);
1374
1375void
1376Eval(str)
1377 char *str;
1378
1379 PREINIT:
1380 char_u *value;
1381 PPCODE:
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001382 value = eval_to_string((char_u *)str, (char_u **)0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 if (value == NULL)
1384 {
1385 XPUSHs(sv_2mortal(newSViv(0)));
1386 XPUSHs(sv_2mortal(newSVpv("", 0)));
1387 }
1388 else
1389 {
1390 XPUSHs(sv_2mortal(newSViv(1)));
1391 XPUSHs(sv_2mortal(newSVpv((char *)value, 0)));
1392 vim_free(value);
1393 }
1394
1395void
1396Buffers(...)
1397
1398 PREINIT:
1399 buf_T *vimbuf;
1400 int i, b;
1401
1402 PPCODE:
1403 if (items == 0)
1404 {
1405 if (GIMME == G_SCALAR)
1406 {
1407 i = 0;
1408 for (vimbuf = firstbuf; vimbuf; vimbuf = vimbuf->b_next)
1409 ++i;
1410
1411 XPUSHs(sv_2mortal(newSViv(i)));
1412 }
1413 else
1414 {
1415 for (vimbuf = firstbuf; vimbuf; vimbuf = vimbuf->b_next)
1416 XPUSHs(newBUFrv(newSV(0), vimbuf));
1417 }
1418 }
1419 else
1420 {
1421 for (i = 0; i < items; i++)
1422 {
1423 SV *sv = ST(i);
1424 if (SvIOK(sv))
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001425 b = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 else
1427 {
1428 char_u *pat;
1429 STRLEN len;
1430
1431 pat = (char_u *)SvPV(sv, len);
1432 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001433 b = buflist_findpat(pat, pat+len, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 --emsg_off;
1435 }
1436
1437 if (b >= 0)
1438 {
1439 vimbuf = buflist_findnr(b);
1440 if (vimbuf)
1441 XPUSHs(newBUFrv(newSV(0), vimbuf));
1442 }
1443 }
1444 }
1445
1446void
1447Windows(...)
1448
1449 PREINIT:
1450 win_T *vimwin;
1451 int i, w;
1452
1453 PPCODE:
1454 if (items == 0)
1455 {
1456 if (GIMME == G_SCALAR)
1457 XPUSHs(sv_2mortal(newSViv(win_count())));
1458 else
1459 {
1460 for (vimwin = firstwin; vimwin != NULL; vimwin = W_NEXT(vimwin))
1461 XPUSHs(newWINrv(newSV(0), vimwin));
1462 }
1463 }
1464 else
1465 {
1466 for (i = 0; i < items; i++)
1467 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001468 w = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 vimwin = win_find_nr(w);
1470 if (vimwin)
1471 XPUSHs(newWINrv(newSV(0), vimwin));
1472 }
1473 }
1474
1475MODULE = VIM PACKAGE = VIWIN
1476
1477void
1478DESTROY(win)
1479 VIWIN win
1480
1481 CODE:
1482 if (win_valid(win))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001483 win->w_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484
1485SV *
1486Buffer(win)
1487 VIWIN win
1488
1489 CODE:
1490 if (!win_valid(win))
1491 win = curwin;
1492 RETVAL = newBUFrv(newSV(0), win->w_buffer);
1493 OUTPUT:
1494 RETVAL
1495
1496void
1497SetHeight(win, height)
1498 VIWIN win
1499 int height;
1500
1501 PREINIT:
1502 win_T *savewin;
1503
1504 PPCODE:
1505 if (!win_valid(win))
1506 win = curwin;
1507 savewin = curwin;
1508 curwin = win;
1509 win_setheight(height);
1510 curwin = savewin;
1511
1512void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001513Cursor(VIWIN win, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514
1515 PPCODE:
Bram Moolenaara1711622011-07-27 14:15:46 +02001516 if (items == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 {
1518 EXTEND(sp, 2);
1519 if (!win_valid(win))
1520 win = curwin;
1521 PUSHs(sv_2mortal(newSViv(win->w_cursor.lnum)));
1522 PUSHs(sv_2mortal(newSViv(win->w_cursor.col)));
1523 }
Bram Moolenaara1711622011-07-27 14:15:46 +02001524 else if (items == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 {
1526 int lnum, col;
1527
1528 if (!win_valid(win))
1529 win = curwin;
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001530 lnum = (int) SvIV(ST(1));
1531 col = (int) SvIV(ST(2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 win->w_cursor.lnum = lnum;
1533 win->w_cursor.col = col;
1534 check_cursor(); /* put cursor on an existing line */
1535 update_screen(NOT_VALID);
1536 }
1537
1538MODULE = VIM PACKAGE = VIBUF
1539
1540void
1541DESTROY(vimbuf)
1542 VIBUF vimbuf;
1543
1544 CODE:
1545 if (buf_valid(vimbuf))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001546 vimbuf->b_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547
1548void
1549Name(vimbuf)
1550 VIBUF vimbuf;
1551
1552 PPCODE:
1553 if (!buf_valid(vimbuf))
1554 vimbuf = curbuf;
1555 /* No file name returns an empty string */
1556 if (vimbuf->b_fname == NULL)
1557 XPUSHs(sv_2mortal(newSVpv("", 0)));
1558 else
1559 XPUSHs(sv_2mortal(newSVpv((char *)vimbuf->b_fname, 0)));
1560
1561void
1562Number(vimbuf)
1563 VIBUF vimbuf;
1564
1565 PPCODE:
1566 if (!buf_valid(vimbuf))
1567 vimbuf = curbuf;
1568 XPUSHs(sv_2mortal(newSViv(vimbuf->b_fnum)));
1569
1570void
1571Count(vimbuf)
1572 VIBUF vimbuf;
1573
1574 PPCODE:
1575 if (!buf_valid(vimbuf))
1576 vimbuf = curbuf;
1577 XPUSHs(sv_2mortal(newSViv(vimbuf->b_ml.ml_line_count)));
1578
1579void
1580Get(vimbuf, ...)
1581 VIBUF vimbuf;
1582
1583 PREINIT:
1584 char_u *line;
1585 int i;
1586 long lnum;
1587 PPCODE:
1588 if (buf_valid(vimbuf))
1589 {
1590 for (i = 1; i < items; i++)
1591 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001592 lnum = (long) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1594 {
1595 line = ml_get_buf(vimbuf, lnum, FALSE);
1596 XPUSHs(sv_2mortal(newSVpv((char *)line, 0)));
1597 }
1598 }
1599 }
1600
1601void
1602Set(vimbuf, ...)
1603 VIBUF vimbuf;
1604
1605 PREINIT:
1606 int i;
1607 long lnum;
1608 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 PPCODE:
1610 if (buf_valid(vimbuf))
1611 {
1612 if (items < 3)
1613 croak("Usage: VIBUF::Set(vimbuf, lnum, @lines)");
1614
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001615 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 for(i = 2; i < items; i++, lnum++)
1617 {
1618 line = SvPV(ST(i),PL_na);
1619 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1620 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001621 aco_save_T aco;
1622
1623 /* set curwin/curbuf for "vimbuf" and save some things */
1624 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001625
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 if (u_savesub(lnum) == OK)
1627 {
1628 ml_replace(lnum, (char_u *)line, TRUE);
1629 changed_bytes(lnum, 0);
1630 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001631
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001632 /* restore curwin/curbuf and a few other things */
1633 aucmd_restbuf(&aco);
1634 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 }
1636 }
1637 }
1638
1639void
1640Delete(vimbuf, ...)
1641 VIBUF vimbuf;
1642
1643 PREINIT:
1644 long i, lnum = 0, count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 PPCODE:
1646 if (buf_valid(vimbuf))
1647 {
1648 if (items == 2)
1649 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001650 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 count = 1;
1652 }
1653 else if (items == 3)
1654 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001655 lnum = (long) SvIV(ST(1));
1656 count = (long) 1 + SvIV(ST(2)) - lnum;
Bram Moolenaara1711622011-07-27 14:15:46 +02001657 if (count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 count = 1;
Bram Moolenaara1711622011-07-27 14:15:46 +02001659 if (count < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 {
1661 lnum -= count;
1662 count = -count;
1663 }
1664 }
1665 if (items >= 2)
1666 {
1667 for (i = 0; i < count; i++)
1668 {
1669 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1670 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001671 aco_save_T aco;
1672
1673 /* set curwin/curbuf for "vimbuf" and save some things */
1674 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001675
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 if (u_savedel(lnum, 1) == OK)
1677 {
1678 ml_delete(lnum, 0);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00001679 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 deleted_lines_mark(lnum, 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001682
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001683 /* restore curwin/curbuf and a few other things */
1684 aucmd_restbuf(&aco);
1685 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001686
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 update_curbuf(VALID);
1688 }
1689 }
1690 }
1691 }
1692
1693void
1694Append(vimbuf, ...)
1695 VIBUF vimbuf;
1696
1697 PREINIT:
1698 int i;
1699 long lnum;
1700 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 PPCODE:
1702 if (buf_valid(vimbuf))
1703 {
1704 if (items < 3)
1705 croak("Usage: VIBUF::Append(vimbuf, lnum, @lines)");
1706
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001707 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 for (i = 2; i < items; i++, lnum++)
1709 {
1710 line = SvPV(ST(i),PL_na);
1711 if (lnum >= 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1712 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001713 aco_save_T aco;
1714
1715 /* set curwin/curbuf for "vimbuf" and save some things */
1716 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001717
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 if (u_inssub(lnum + 1) == OK)
1719 {
1720 ml_append(lnum, (char_u *)line, (colnr_T)0, FALSE);
1721 appended_lines_mark(lnum, 1L);
1722 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001723
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001724 /* restore curwin/curbuf and a few other things */
1725 aucmd_restbuf(&aco);
1726 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001727
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 update_curbuf(VALID);
1729 }
1730 }
1731 }
1732