blob: 627f437075a61fd4a2a027351cc032e5a6198026 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 */
8/*
9 * if_perl.xs: Main code for Perl interface support.
10 * Mostly written by Sven Verdoolaege.
11 */
12
13#define _memory_h /* avoid memset redeclaration */
14#define IN_PERL_FILE /* don't include if_perl.pro from proto.h */
15
Bram Moolenaaraee1f4a2013-08-02 16:10:32 +020016/*
Bram Moolenaar6b107212013-12-11 15:06:40 +010017 * Currently 32-bit version of ActivePerl is built with VC6 (or MinGW since
18 * ActivePerl 5.18).
Bram Moolenaaraee1f4a2013-08-02 16:10:32 +020019 * (http://community.activestate.com/faq/windows-compilers-perl-modules)
20 * It means that time_t should be 32-bit. However the default size of
21 * time_t is 64-bit since VC8. So we have to define _USE_32BIT_TIME_T.
22 */
23#if defined(WIN32) && !defined(_WIN64)
24# define _USE_32BIT_TIME_T
25#endif
26
Bram Moolenaar6b107212013-12-11 15:06:40 +010027/*
28 * Prevent including winsock.h. perl.h tries to detect whether winsock.h is
29 * already included before including winsock2.h, because winsock2.h isn't
30 * compatible with winsock.h. However the detection doesn't work with some
31 * versions of MinGW. If WIN32_LEAN_AND_MEAN is defined, windows.h will not
32 * include winsock.h.
33 */
34#ifdef WIN32
35# define WIN32_LEAN_AND_MEAN
36#endif
37
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#include "vim.h"
39
Bram Moolenaar7c0daf02013-12-14 11:46:08 +010040/* Work around for perl-5.18.
41 * Don't include "perl\lib\CORE\inline.h" for now,
42 * include it after Perl_sv_free2 is defined. */
43#ifdef DYNAMIC_PERL
44# define PERL_NO_INLINE_FUNCTIONS
45#endif
46
Bram Moolenaar207fd752013-12-14 11:50:35 +010047/* Work around for using MSVC and ActivePerl 5.18. */
48#ifdef _MSC_VER
49# define __inline__ __inline
50#endif
51
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010052#ifdef __GNUC__
53# pragma GCC diagnostic push
54# pragma GCC diagnostic ignored "-Wunused-variable"
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010055#endif
56
Bram Moolenaaraee1f4a2013-08-02 16:10:32 +020057#include <EXTERN.h>
58#include <perl.h>
59#include <XSUB.h>
Bram Moolenaar6244a0f2016-04-14 14:09:25 +020060#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
61# include <perliol.h>
62#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000063
Bram Moolenaarcf190c62016-06-02 11:54:06 +020064/* Workaround for perl < 5.8.7 */
65#ifndef PERLIO_FUNCS_DECL
66# ifdef PERLIO_FUNCS_CONST
67# define PERLIO_FUNCS_DECL(funcs) const PerlIO_funcs funcs
68# define PERLIO_FUNCS_CAST(funcs) (PerlIO_funcs*)(funcs)
69# else
70# define PERLIO_FUNCS_DECL(funcs) PerlIO_funcs funcs
71# define PERLIO_FUNCS_CAST(funcs) (funcs)
72# endif
73#endif
Bram Moolenaarc4bc0e62016-06-02 13:54:49 +020074#ifndef SvREFCNT_inc_void_NN
75# define SvREFCNT_inc_void_NN SvREFCNT_inc
76#endif
Bram Moolenaarcf190c62016-06-02 11:54:06 +020077
Bram Moolenaar071d4272004-06-13 20:20:40 +000078/*
79 * Work around clashes between Perl and Vim namespace. proto.h doesn't
80 * include if_perl.pro and perlsfio.pro when IN_PERL_FILE is defined, because
81 * we need the CV typedef. proto.h can't be moved to after including
82 * if_perl.h, because we get all sorts of name clashes then.
83 */
84#ifndef PROTO
Bram Moolenaareeb50ab2016-06-26 17:19:46 +020085# ifndef __MINGW32__
86# include "proto/if_perl.pro"
87# include "proto/if_perlsfio.pro"
88# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000089#endif
90
Bram Moolenaar1f402802018-09-21 14:01:27 +020091// Perl compatibility stuff. This should ensure compatibility with older
92// versions of Perl.
Bram Moolenaar071d4272004-06-13 20:20:40 +000093#ifndef PERL_VERSION
Bram Moolenaareeb50ab2016-06-26 17:19:46 +020094# include <patchlevel.h>
95# define PERL_REVISION 5
96# define PERL_VERSION PATCHLEVEL
97# define PERL_SUBVERSION SUBVERSION
Bram Moolenaar071d4272004-06-13 20:20:40 +000098#endif
99
Bram Moolenaar1f402802018-09-21 14:01:27 +0200100
101// Work around for ActivePerl 5.20.3+: Avoid generating (g)vim.lib.
102#if defined(ACTIVEPERL_VERSION) && (ACTIVEPERL_VERSION >= 2003) \
103 && defined(WIN32) && defined(USE_DYNAMIC_LOADING)
104# undef XS_EXTERNAL
105# define XS_EXTERNAL(name) XSPROTO(name)
106#endif
107
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000108/*
109 * Quoting Jan Dubois of Active State:
110 * ActivePerl build 822 still identifies itself as 5.8.8 but already
111 * contains many of the changes from the upcoming Perl 5.8.9 release.
112 *
113 * The changes include addition of two symbols (Perl_sv_2iv_flags,
114 * Perl_newXS_flags) not present in earlier releases.
115 *
Bram Moolenaar3b9b13e2007-09-15 12:49:35 +0000116 * Jan Dubois suggested the following guarding scheme.
117 *
118 * Active State defined ACTIVEPERL_VERSION as a string in versions before
119 * 5.8.8; and so the comparison to 822 below needs to be guarded.
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000120 */
Bram Moolenaar3b9b13e2007-09-15 12:49:35 +0000121#if (PERL_REVISION == 5) && (PERL_VERSION == 8) && (PERL_SUBVERSION >= 8)
122# if (ACTIVEPERL_VERSION >= 822) || (PERL_SUBVERSION >= 9)
123# define PERL589_OR_LATER
124# endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000125#endif
126#if (PERL_REVISION == 5) && (PERL_VERSION >= 9)
127# define PERL589_OR_LATER
128#endif
129
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100130#if (PERL_REVISION == 5) && ((PERL_VERSION > 10) || \
131 (PERL_VERSION == 10) && (PERL_SUBVERSION >= 1))
132# define PERL5101_OR_LATER
133#endif
134
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#ifndef pTHX
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200136# define pTHX void
137# define pTHX_
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#endif
139
140#ifndef EXTERN_C
141# define EXTERN_C
142#endif
143
144/* Compatibility hacks over */
145
146static PerlInterpreter *perl_interp = NULL;
Bram Moolenaard99df422016-01-29 23:20:40 +0100147static void xs_init(pTHX);
148static void VIM_init(void);
149EXTERN_C void boot_DynaLoader(pTHX_ CV*);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151/*
Bram Moolenaare06c1882010-07-21 22:05:20 +0200152 * For dynamic linked perl.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 */
154#if defined(DYNAMIC_PERL) || defined(PROTO)
Bram Moolenaare06c1882010-07-21 22:05:20 +0200155
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200156# ifndef DYNAMIC_PERL /* just generating prototypes */
157# ifdef WIN3264
Bram Moolenaare06c1882010-07-21 22:05:20 +0200158typedef int HANDLE;
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200159# endif
Bram Moolenaare06c1882010-07-21 22:05:20 +0200160typedef int XSINIT_t;
161typedef int XSUBADDR_t;
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200162# endif
163# ifndef USE_ITHREADS
Bram Moolenaare06c1882010-07-21 22:05:20 +0200164typedef int perl_key;
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200165# endif
Bram Moolenaare06c1882010-07-21 22:05:20 +0200166
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200167# ifndef WIN3264
168# include <dlfcn.h>
169# define HANDLE void*
170# define PERL_PROC void*
171# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
172# define symbol_from_dll dlsym
173# define close_dll dlclose
174# else
175# define PERL_PROC FARPROC
176# define load_dll vimLoadLib
177# define symbol_from_dll GetProcAddress
178# define close_dll FreeLibrary
179# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180/*
181 * Wrapper defines
182 */
183# define perl_alloc dll_perl_alloc
184# define perl_construct dll_perl_construct
185# define perl_parse dll_perl_parse
186# define perl_run dll_perl_run
187# define perl_destruct dll_perl_destruct
188# define perl_free dll_perl_free
189# define Perl_get_context dll_Perl_get_context
190# define Perl_croak dll_Perl_croak
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100191# ifdef PERL5101_OR_LATER
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100192# define Perl_croak_xs_usage dll_Perl_croak_xs_usage
193# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194# ifndef PROTO
195# define Perl_croak_nocontext dll_Perl_croak_nocontext
196# define Perl_call_argv dll_Perl_call_argv
197# define Perl_call_pv dll_Perl_call_pv
198# define Perl_eval_sv dll_Perl_eval_sv
199# define Perl_get_sv dll_Perl_get_sv
200# define Perl_eval_pv dll_Perl_eval_pv
201# define Perl_call_method dll_Perl_call_method
202# endif
203# define Perl_dowantarray dll_Perl_dowantarray
204# define Perl_free_tmps dll_Perl_free_tmps
205# define Perl_gv_stashpv dll_Perl_gv_stashpv
206# define Perl_markstack_grow dll_Perl_markstack_grow
207# define Perl_mg_find dll_Perl_mg_find
Bram Moolenaar578333b2018-07-22 07:31:09 +0200208# if (PERL_REVISION == 5) && (PERL_VERSION >= 28)
209# define Perl_mg_get dll_Perl_mg_get
210# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211# define Perl_newXS dll_Perl_newXS
212# define Perl_newSV dll_Perl_newSV
213# define Perl_newSViv dll_Perl_newSViv
214# define Perl_newSVpv dll_Perl_newSVpv
215# define Perl_pop_scope dll_Perl_pop_scope
216# define Perl_push_scope dll_Perl_push_scope
217# define Perl_save_int dll_Perl_save_int
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200218# if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
219# define Perl_save_strlen dll_Perl_save_strlen
220# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221# define Perl_stack_grow dll_Perl_stack_grow
222# define Perl_set_context dll_Perl_set_context
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200223# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200224# define Perl_sv_2bool_flags dll_Perl_sv_2bool_flags
225# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
226# define Perl_xs_apiversion_bootcheck dll_Perl_xs_apiversion_bootcheck
227# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200228# else
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200229# define Perl_sv_2bool dll_Perl_sv_2bool
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200230# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231# define Perl_sv_2iv dll_Perl_sv_2iv
232# define Perl_sv_2mortal dll_Perl_sv_2mortal
233# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
234# define Perl_sv_2pv_flags dll_Perl_sv_2pv_flags
235# define Perl_sv_2pv_nolen dll_Perl_sv_2pv_nolen
236# else
237# define Perl_sv_2pv dll_Perl_sv_2pv
238# endif
239# define Perl_sv_bless dll_Perl_sv_bless
240# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
241# define Perl_sv_catpvn_flags dll_Perl_sv_catpvn_flags
242# else
243# define Perl_sv_catpvn dll_Perl_sv_catpvn
244# endif
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200245# ifdef PERL589_OR_LATER
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000246# define Perl_sv_2iv_flags dll_Perl_sv_2iv_flags
247# define Perl_newXS_flags dll_Perl_newXS_flags
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200248# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249# define Perl_sv_free dll_Perl_sv_free
Bram Moolenaarccf22172008-09-01 15:56:45 +0000250# if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
251# define Perl_sv_free2 dll_Perl_sv_free2
252# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253# define Perl_sv_isa dll_Perl_sv_isa
254# define Perl_sv_magic dll_Perl_sv_magic
255# define Perl_sv_setiv dll_Perl_sv_setiv
256# define Perl_sv_setpv dll_Perl_sv_setpv
257# define Perl_sv_setpvn dll_Perl_sv_setpvn
258# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
259# define Perl_sv_setsv_flags dll_Perl_sv_setsv_flags
260# else
261# define Perl_sv_setsv dll_Perl_sv_setsv
262# endif
263# define Perl_sv_upgrade dll_Perl_sv_upgrade
264# define Perl_Tstack_sp_ptr dll_Perl_Tstack_sp_ptr
265# define Perl_Top_ptr dll_Perl_Top_ptr
266# define Perl_Tstack_base_ptr dll_Perl_Tstack_base_ptr
267# define Perl_Tstack_max_ptr dll_Perl_Tstack_max_ptr
268# define Perl_Ttmps_ix_ptr dll_Perl_Ttmps_ix_ptr
269# define Perl_Ttmps_floor_ptr dll_Perl_Ttmps_floor_ptr
270# define Perl_Tmarkstack_ptr_ptr dll_Perl_Tmarkstack_ptr_ptr
271# define Perl_Tmarkstack_max_ptr dll_Perl_Tmarkstack_max_ptr
272# define Perl_TSv_ptr dll_Perl_TSv_ptr
273# define Perl_TXpv_ptr dll_Perl_TXpv_ptr
274# define Perl_Tna_ptr dll_Perl_Tna_ptr
275# define Perl_Idefgv_ptr dll_Perl_Idefgv_ptr
276# define Perl_Ierrgv_ptr dll_Perl_Ierrgv_ptr
277# define Perl_Isv_yes_ptr dll_Perl_Isv_yes_ptr
278# define boot_DynaLoader dll_boot_DynaLoader
Bram Moolenaare06c1882010-07-21 22:05:20 +0200279# define Perl_Gthr_key_ptr dll_Perl_Gthr_key_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000281# define Perl_sys_init dll_Perl_sys_init
Bram Moolenaarc236c162008-07-13 17:41:49 +0000282# define Perl_sys_term dll_Perl_sys_term
283# define Perl_ISv_ptr dll_Perl_ISv_ptr
284# define Perl_Istack_max_ptr dll_Perl_Istack_max_ptr
285# define Perl_Istack_base_ptr dll_Perl_Istack_base_ptr
286# define Perl_Itmps_ix_ptr dll_Perl_Itmps_ix_ptr
287# define Perl_Itmps_floor_ptr dll_Perl_Itmps_floor_ptr
288# define Perl_IXpv_ptr dll_Perl_IXpv_ptr
289# define Perl_Ina_ptr dll_Perl_Ina_ptr
290# define Perl_Imarkstack_ptr_ptr dll_Perl_Imarkstack_ptr_ptr
291# define Perl_Imarkstack_max_ptr dll_Perl_Imarkstack_max_ptr
292# define Perl_Istack_sp_ptr dll_Perl_Istack_sp_ptr
293# define Perl_Iop_ptr dll_Perl_Iop_ptr
294# define Perl_call_list dll_Perl_call_list
295# define Perl_Iscopestack_ix_ptr dll_Perl_Iscopestack_ix_ptr
296# define Perl_Iunitcheckav_ptr dll_Perl_Iunitcheckav_ptr
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200297# if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
298# define Perl_xs_handshake dll_Perl_xs_handshake
299# define Perl_xs_boot_epilog dll_Perl_xs_boot_epilog
300# endif
Bram Moolenaar01c10522012-09-21 12:50:51 +0200301# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100302# ifdef USE_ITHREADS
303# define PL_thr_key *dll_PL_thr_key
304# endif
Bram Moolenaar01c10522012-09-21 12:50:51 +0200305# endif
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100306# define Perl_hv_iternext_flags dll_Perl_hv_iternext_flags
307# define Perl_hv_iterinit dll_Perl_hv_iterinit
308# define Perl_hv_iterkey dll_Perl_hv_iterkey
309# define Perl_hv_iterval dll_Perl_hv_iterval
310# define Perl_av_fetch dll_Perl_av_fetch
311# define Perl_av_len dll_Perl_av_len
312# define Perl_sv_2nv_flags dll_Perl_sv_2nv_flags
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200313# if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
314# define PerlIOBase_pushed dll_PerlIOBase_pushed
315# define PerlIO_define_layer dll_PerlIO_define_layer
316# endif
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200317# if (PERL_REVISION == 5) && (PERL_VERSION >= 24)
318# define Perl_savetmps dll_Perl_savetmps
319# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000320
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321/*
322 * Declare HANDLE for perl.dll and function pointers.
323 */
324static HANDLE hPerlLib = NULL;
325
326static PerlInterpreter* (*perl_alloc)();
327static void (*perl_construct)(PerlInterpreter*);
328static void (*perl_destruct)(PerlInterpreter*);
329static void (*perl_free)(PerlInterpreter*);
330static int (*perl_run)(PerlInterpreter*);
331static int (*perl_parse)(PerlInterpreter*, XSINIT_t, int, char**, char**);
332static void* (*Perl_get_context)(void);
Bram Moolenaar864733a2016-04-02 14:18:01 +0200333static void (*Perl_croak)(pTHX_ const char*, ...) __attribute__noreturn__;
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200334# ifdef PERL5101_OR_LATER
Bram Moolenaar6b107212013-12-11 15:06:40 +0100335/* Perl-5.18 has a different Perl_croak_xs_usage signature. */
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200336# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
Bram Moolenaar864733a2016-04-02 14:18:01 +0200337static void (*Perl_croak_xs_usage)(const CV *const, const char *const params)
338 __attribute__noreturn__;
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200339# else
Bram Moolenaar864733a2016-04-02 14:18:01 +0200340static void (*Perl_croak_xs_usage)(pTHX_ const CV *const, const char *const params)
341 __attribute__noreturn__;
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200342# endif
Bram Moolenaar6b107212013-12-11 15:06:40 +0100343# endif
Bram Moolenaar864733a2016-04-02 14:18:01 +0200344static void (*Perl_croak_nocontext)(const char*, ...) __attribute__noreturn__;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345static I32 (*Perl_dowantarray)(pTHX);
346static void (*Perl_free_tmps)(pTHX);
347static HV* (*Perl_gv_stashpv)(pTHX_ const char*, I32);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200348# if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200349static I32* (*Perl_markstack_grow)(pTHX);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200350# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351static void (*Perl_markstack_grow)(pTHX);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200352# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353static MAGIC* (*Perl_mg_find)(pTHX_ SV*, int);
Bram Moolenaar578333b2018-07-22 07:31:09 +0200354# if (PERL_REVISION == 5) && (PERL_VERSION >= 28)
355static int (*Perl_mg_get)(pTHX_ SV*);
356# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357static CV* (*Perl_newXS)(pTHX_ char*, XSUBADDR_t, char*);
358static SV* (*Perl_newSV)(pTHX_ STRLEN);
359static SV* (*Perl_newSViv)(pTHX_ IV);
360static SV* (*Perl_newSVpv)(pTHX_ const char*, STRLEN);
361static I32 (*Perl_call_argv)(pTHX_ const char*, I32, char**);
362static I32 (*Perl_call_pv)(pTHX_ const char*, I32);
363static I32 (*Perl_eval_sv)(pTHX_ SV*, I32);
364static SV* (*Perl_get_sv)(pTHX_ const char*, I32);
365static SV* (*Perl_eval_pv)(pTHX_ const char*, I32);
366static SV* (*Perl_call_method)(pTHX_ const char*, I32);
367static void (*Perl_pop_scope)(pTHX);
368static void (*Perl_push_scope)(pTHX);
369static void (*Perl_save_int)(pTHX_ int*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200370# if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200371static void (*Perl_save_strlen)(pTHX_ STRLEN* ptr);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200372# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373static SV** (*Perl_stack_grow)(pTHX_ SV**, SV**p, int);
374static SV** (*Perl_set_context)(void*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200375# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200376static bool (*Perl_sv_2bool_flags)(pTHX_ SV*, I32);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200377# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200378static void (*Perl_xs_apiversion_bootcheck)(pTHX_ SV *module, const char *api_p, STRLEN api_len);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200379# endif
380# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381static bool (*Perl_sv_2bool)(pTHX_ SV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200382# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383static IV (*Perl_sv_2iv)(pTHX_ SV*);
384static SV* (*Perl_sv_2mortal)(pTHX_ SV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200385# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386static char* (*Perl_sv_2pv_flags)(pTHX_ SV*, STRLEN*, I32);
387static char* (*Perl_sv_2pv_nolen)(pTHX_ SV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200388# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389static char* (*Perl_sv_2pv)(pTHX_ SV*, STRLEN*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200390# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391static SV* (*Perl_sv_bless)(pTHX_ SV*, HV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200392# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393static void (*Perl_sv_catpvn_flags)(pTHX_ SV* , const char*, STRLEN, I32);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200394# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395static void (*Perl_sv_catpvn)(pTHX_ SV*, const char*, STRLEN);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200396# endif
397# ifdef PERL589_OR_LATER
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000398static IV (*Perl_sv_2iv_flags)(pTHX_ SV* sv, I32 flags);
399static CV * (*Perl_newXS_flags)(pTHX_ const char *name, XSUBADDR_t subaddr, const char *const filename, const char *const proto, U32 flags);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200400# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401static void (*Perl_sv_free)(pTHX_ SV*);
402static int (*Perl_sv_isa)(pTHX_ SV*, const char*);
403static void (*Perl_sv_magic)(pTHX_ SV*, SV*, int, const char*, I32);
404static void (*Perl_sv_setiv)(pTHX_ SV*, IV);
405static void (*Perl_sv_setpv)(pTHX_ SV*, const char*);
406static void (*Perl_sv_setpvn)(pTHX_ SV*, const char*, STRLEN);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200407# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408static void (*Perl_sv_setsv_flags)(pTHX_ SV*, SV*, I32);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200409# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410static void (*Perl_sv_setsv)(pTHX_ SV*, SV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200411# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412static bool (*Perl_sv_upgrade)(pTHX_ SV*, U32);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200413# if (PERL_REVISION == 5) && (PERL_VERSION < 10)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414static SV*** (*Perl_Tstack_sp_ptr)(register PerlInterpreter*);
415static OP** (*Perl_Top_ptr)(register PerlInterpreter*);
416static SV*** (*Perl_Tstack_base_ptr)(register PerlInterpreter*);
417static SV*** (*Perl_Tstack_max_ptr)(register PerlInterpreter*);
418static I32* (*Perl_Ttmps_ix_ptr)(register PerlInterpreter*);
419static I32* (*Perl_Ttmps_floor_ptr)(register PerlInterpreter*);
420static I32** (*Perl_Tmarkstack_ptr_ptr)(register PerlInterpreter*);
421static I32** (*Perl_Tmarkstack_max_ptr)(register PerlInterpreter*);
422static SV** (*Perl_TSv_ptr)(register PerlInterpreter*);
423static XPV** (*Perl_TXpv_ptr)(register PerlInterpreter*);
424static STRLEN* (*Perl_Tna_ptr)(register PerlInterpreter*);
Bram Moolenaar6b107212013-12-11 15:06:40 +0100425# else
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200426/* Perl-5.18 has a different Perl_sv_free2 signature. */
427# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
428static void (*Perl_sv_free2)(pTHX_ SV*, const U32);
429# else
Bram Moolenaarccf22172008-09-01 15:56:45 +0000430static void (*Perl_sv_free2)(pTHX_ SV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200431# endif
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000432static void (*Perl_sys_init)(int* argc, char*** argv);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000433static void (*Perl_sys_term)(void);
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200434static void (*Perl_call_list)(pTHX_ I32, AV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200435# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
436# else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000437static SV** (*Perl_ISv_ptr)(register PerlInterpreter*);
438static SV*** (*Perl_Istack_max_ptr)(register PerlInterpreter*);
439static SV*** (*Perl_Istack_base_ptr)(register PerlInterpreter*);
440static XPV** (*Perl_IXpv_ptr)(register PerlInterpreter*);
441static I32* (*Perl_Itmps_ix_ptr)(register PerlInterpreter*);
442static I32* (*Perl_Itmps_floor_ptr)(register PerlInterpreter*);
443static STRLEN* (*Perl_Ina_ptr)(register PerlInterpreter*);
444static I32** (*Perl_Imarkstack_ptr_ptr)(register PerlInterpreter*);
445static I32** (*Perl_Imarkstack_max_ptr)(register PerlInterpreter*);
446static SV*** (*Perl_Istack_sp_ptr)(register PerlInterpreter*);
447static OP** (*Perl_Iop_ptr)(register PerlInterpreter*);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000448static I32* (*Perl_Iscopestack_ix_ptr)(register PerlInterpreter*);
449static AV** (*Perl_Iunitcheckav_ptr)(register PerlInterpreter*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200450# endif
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200451# endif
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200452# if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200453static I32 (*Perl_xs_handshake)(const U32, void *, const char *, ...);
454static void (*Perl_xs_boot_epilog)(pTHX_ const U32);
Bram Moolenaard8619992014-03-12 17:08:05 +0100455# endif
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200456
457# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
458# ifdef USE_ITHREADS
459static perl_key* dll_PL_thr_key;
460# endif
461# else
Bram Moolenaare06c1882010-07-21 22:05:20 +0200462static GV** (*Perl_Idefgv_ptr)(register PerlInterpreter*);
463static GV** (*Perl_Ierrgv_ptr)(register PerlInterpreter*);
464static SV* (*Perl_Isv_yes_ptr)(register PerlInterpreter*);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200465static perl_key* (*Perl_Gthr_key_ptr)_((pTHX));
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200466# endif
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200467static void (*boot_DynaLoader)_((pTHX_ CV*));
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100468static HE * (*Perl_hv_iternext_flags)(pTHX_ HV *, I32);
469static I32 (*Perl_hv_iterinit)(pTHX_ HV *);
470static char * (*Perl_hv_iterkey)(pTHX_ HE *, I32 *);
471static SV * (*Perl_hv_iterval)(pTHX_ HV *, HE *);
472static SV** (*Perl_av_fetch)(pTHX_ AV *, SSize_t, I32);
473static SSize_t (*Perl_av_len)(pTHX_ AV *);
474static NV (*Perl_sv_2nv_flags)(pTHX_ SV *const, const I32);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200475# if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200476static IV (*PerlIOBase_pushed)(pTHX_ PerlIO *, const char *, SV *, PerlIO_funcs *);
477static void (*PerlIO_define_layer)(pTHX_ PerlIO_funcs *);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200478# endif
479# if (PERL_REVISION == 5) && (PERL_VERSION >= 24)
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200480static void (*Perl_savetmps)(pTHX);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200481# endif
Bram Moolenaare06c1882010-07-21 22:05:20 +0200482
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483/*
484 * Table of name to function pointer of perl.
485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486static struct {
487 char* name;
488 PERL_PROC* ptr;
489} perl_funcname_table[] = {
490 {"perl_alloc", (PERL_PROC*)&perl_alloc},
491 {"perl_construct", (PERL_PROC*)&perl_construct},
492 {"perl_destruct", (PERL_PROC*)&perl_destruct},
493 {"perl_free", (PERL_PROC*)&perl_free},
494 {"perl_run", (PERL_PROC*)&perl_run},
495 {"perl_parse", (PERL_PROC*)&perl_parse},
496 {"Perl_get_context", (PERL_PROC*)&Perl_get_context},
497 {"Perl_croak", (PERL_PROC*)&Perl_croak},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200498# ifdef PERL5101_OR_LATER
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100499 {"Perl_croak_xs_usage", (PERL_PROC*)&Perl_croak_xs_usage},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200500# endif
501# ifdef PERL_IMPLICIT_CONTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 {"Perl_croak_nocontext", (PERL_PROC*)&Perl_croak_nocontext},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200503# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 {"Perl_dowantarray", (PERL_PROC*)&Perl_dowantarray},
505 {"Perl_free_tmps", (PERL_PROC*)&Perl_free_tmps},
506 {"Perl_gv_stashpv", (PERL_PROC*)&Perl_gv_stashpv},
507 {"Perl_markstack_grow", (PERL_PROC*)&Perl_markstack_grow},
508 {"Perl_mg_find", (PERL_PROC*)&Perl_mg_find},
Bram Moolenaar578333b2018-07-22 07:31:09 +0200509# if (PERL_REVISION == 5) && (PERL_VERSION >= 28)
510 {"Perl_mg_get", (PERL_PROC*)&Perl_mg_get},
511# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 {"Perl_newXS", (PERL_PROC*)&Perl_newXS},
513 {"Perl_newSV", (PERL_PROC*)&Perl_newSV},
514 {"Perl_newSViv", (PERL_PROC*)&Perl_newSViv},
515 {"Perl_newSVpv", (PERL_PROC*)&Perl_newSVpv},
516 {"Perl_call_argv", (PERL_PROC*)&Perl_call_argv},
517 {"Perl_call_pv", (PERL_PROC*)&Perl_call_pv},
518 {"Perl_eval_sv", (PERL_PROC*)&Perl_eval_sv},
519 {"Perl_get_sv", (PERL_PROC*)&Perl_get_sv},
520 {"Perl_eval_pv", (PERL_PROC*)&Perl_eval_pv},
521 {"Perl_call_method", (PERL_PROC*)&Perl_call_method},
522 {"Perl_pop_scope", (PERL_PROC*)&Perl_pop_scope},
523 {"Perl_push_scope", (PERL_PROC*)&Perl_push_scope},
524 {"Perl_save_int", (PERL_PROC*)&Perl_save_int},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200525# if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200526 {"Perl_save_strlen", (PERL_PROC*)&Perl_save_strlen},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200527# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 {"Perl_stack_grow", (PERL_PROC*)&Perl_stack_grow},
529 {"Perl_set_context", (PERL_PROC*)&Perl_set_context},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200530# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200531 {"Perl_sv_2bool_flags", (PERL_PROC*)&Perl_sv_2bool_flags},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200532# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200533 {"Perl_xs_apiversion_bootcheck",(PERL_PROC*)&Perl_xs_apiversion_bootcheck},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200534# endif
535# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 {"Perl_sv_2bool", (PERL_PROC*)&Perl_sv_2bool},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200537# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 {"Perl_sv_2iv", (PERL_PROC*)&Perl_sv_2iv},
539 {"Perl_sv_2mortal", (PERL_PROC*)&Perl_sv_2mortal},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200540# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 {"Perl_sv_2pv_flags", (PERL_PROC*)&Perl_sv_2pv_flags},
542 {"Perl_sv_2pv_nolen", (PERL_PROC*)&Perl_sv_2pv_nolen},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200543# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 {"Perl_sv_2pv", (PERL_PROC*)&Perl_sv_2pv},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200545# endif
546# ifdef PERL589_OR_LATER
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000547 {"Perl_sv_2iv_flags", (PERL_PROC*)&Perl_sv_2iv_flags},
548 {"Perl_newXS_flags", (PERL_PROC*)&Perl_newXS_flags},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200549# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 {"Perl_sv_bless", (PERL_PROC*)&Perl_sv_bless},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200551# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 {"Perl_sv_catpvn_flags", (PERL_PROC*)&Perl_sv_catpvn_flags},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200553# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 {"Perl_sv_catpvn", (PERL_PROC*)&Perl_sv_catpvn},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200555# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 {"Perl_sv_free", (PERL_PROC*)&Perl_sv_free},
557 {"Perl_sv_isa", (PERL_PROC*)&Perl_sv_isa},
558 {"Perl_sv_magic", (PERL_PROC*)&Perl_sv_magic},
559 {"Perl_sv_setiv", (PERL_PROC*)&Perl_sv_setiv},
560 {"Perl_sv_setpv", (PERL_PROC*)&Perl_sv_setpv},
561 {"Perl_sv_setpvn", (PERL_PROC*)&Perl_sv_setpvn},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200562# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 {"Perl_sv_setsv_flags", (PERL_PROC*)&Perl_sv_setsv_flags},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200564# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 {"Perl_sv_setsv", (PERL_PROC*)&Perl_sv_setsv},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200566# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 {"Perl_sv_upgrade", (PERL_PROC*)&Perl_sv_upgrade},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200568# if (PERL_REVISION == 5) && (PERL_VERSION < 10)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 {"Perl_Tstack_sp_ptr", (PERL_PROC*)&Perl_Tstack_sp_ptr},
570 {"Perl_Top_ptr", (PERL_PROC*)&Perl_Top_ptr},
571 {"Perl_Tstack_base_ptr", (PERL_PROC*)&Perl_Tstack_base_ptr},
572 {"Perl_Tstack_max_ptr", (PERL_PROC*)&Perl_Tstack_max_ptr},
573 {"Perl_Ttmps_ix_ptr", (PERL_PROC*)&Perl_Ttmps_ix_ptr},
574 {"Perl_Ttmps_floor_ptr", (PERL_PROC*)&Perl_Ttmps_floor_ptr},
575 {"Perl_Tmarkstack_ptr_ptr", (PERL_PROC*)&Perl_Tmarkstack_ptr_ptr},
576 {"Perl_Tmarkstack_max_ptr", (PERL_PROC*)&Perl_Tmarkstack_max_ptr},
577 {"Perl_TSv_ptr", (PERL_PROC*)&Perl_TSv_ptr},
578 {"Perl_TXpv_ptr", (PERL_PROC*)&Perl_TXpv_ptr},
579 {"Perl_Tna_ptr", (PERL_PROC*)&Perl_Tna_ptr},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200580# else
Bram Moolenaarccf22172008-09-01 15:56:45 +0000581 {"Perl_sv_free2", (PERL_PROC*)&Perl_sv_free2},
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000582 {"Perl_sys_init", (PERL_PROC*)&Perl_sys_init},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000583 {"Perl_sys_term", (PERL_PROC*)&Perl_sys_term},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200584 {"Perl_call_list", (PERL_PROC*)&Perl_call_list},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200585# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
586# else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000587 {"Perl_ISv_ptr", (PERL_PROC*)&Perl_ISv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000588 {"Perl_Istack_max_ptr", (PERL_PROC*)&Perl_Istack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200589 {"Perl_Istack_base_ptr", (PERL_PROC*)&Perl_Istack_base_ptr},
590 {"Perl_IXpv_ptr", (PERL_PROC*)&Perl_IXpv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000591 {"Perl_Itmps_ix_ptr", (PERL_PROC*)&Perl_Itmps_ix_ptr},
592 {"Perl_Itmps_floor_ptr", (PERL_PROC*)&Perl_Itmps_floor_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200593 {"Perl_Ina_ptr", (PERL_PROC*)&Perl_Ina_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000594 {"Perl_Imarkstack_ptr_ptr", (PERL_PROC*)&Perl_Imarkstack_ptr_ptr},
595 {"Perl_Imarkstack_max_ptr", (PERL_PROC*)&Perl_Imarkstack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200596 {"Perl_Istack_sp_ptr", (PERL_PROC*)&Perl_Istack_sp_ptr},
597 {"Perl_Iop_ptr", (PERL_PROC*)&Perl_Iop_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000598 {"Perl_Iscopestack_ix_ptr", (PERL_PROC*)&Perl_Iscopestack_ix_ptr},
599 {"Perl_Iunitcheckav_ptr", (PERL_PROC*)&Perl_Iunitcheckav_ptr},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200600# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200601# endif
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200602# if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200603 {"Perl_xs_handshake", (PERL_PROC*)&Perl_xs_handshake},
604 {"Perl_xs_boot_epilog", (PERL_PROC*)&Perl_xs_boot_epilog},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200605# endif
606# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100607# ifdef USE_ITHREADS
Bram Moolenaar01c10522012-09-21 12:50:51 +0200608 {"PL_thr_key", (PERL_PROC*)&dll_PL_thr_key},
Bram Moolenaard8619992014-03-12 17:08:05 +0100609# endif
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200610# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 {"Perl_Idefgv_ptr", (PERL_PROC*)&Perl_Idefgv_ptr},
612 {"Perl_Ierrgv_ptr", (PERL_PROC*)&Perl_Ierrgv_ptr},
613 {"Perl_Isv_yes_ptr", (PERL_PROC*)&Perl_Isv_yes_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200614 {"Perl_Gthr_key_ptr", (PERL_PROC*)&Perl_Gthr_key_ptr},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200615# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200616 {"boot_DynaLoader", (PERL_PROC*)&boot_DynaLoader},
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100617 {"Perl_hv_iternext_flags", (PERL_PROC*)&Perl_hv_iternext_flags},
618 {"Perl_hv_iterinit", (PERL_PROC*)&Perl_hv_iterinit},
619 {"Perl_hv_iterkey", (PERL_PROC*)&Perl_hv_iterkey},
620 {"Perl_hv_iterval", (PERL_PROC*)&Perl_hv_iterval},
621 {"Perl_av_fetch", (PERL_PROC*)&Perl_av_fetch},
622 {"Perl_av_len", (PERL_PROC*)&Perl_av_len},
623 {"Perl_sv_2nv_flags", (PERL_PROC*)&Perl_sv_2nv_flags},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200624# if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200625 {"PerlIOBase_pushed", (PERL_PROC*)&PerlIOBase_pushed},
626 {"PerlIO_define_layer", (PERL_PROC*)&PerlIO_define_layer},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200627# endif
628# if (PERL_REVISION == 5) && (PERL_VERSION >= 24)
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200629 {"Perl_savetmps", (PERL_PROC*)&Perl_savetmps},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200630# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 {"", NULL},
632};
633
Bram Moolenaar6b107212013-12-11 15:06:40 +0100634/* Work around for perl-5.18.
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200635 * For now, only the definitions of S_SvREFCNT_dec are needed in
636 * "perl\lib\CORE\inline.h". */
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200637# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200638static void
639S_SvREFCNT_dec(pTHX_ SV *sv)
640{
641 if (LIKELY(sv != NULL)) {
642 U32 rc = SvREFCNT(sv);
643 if (LIKELY(rc > 1))
644 SvREFCNT(sv) = rc - 1;
645 else
646 Perl_sv_free2(aTHX_ sv, rc);
647 }
648}
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200649# endif
Bram Moolenaar6b107212013-12-11 15:06:40 +0100650
Bram Moolenaarfa4161c2017-06-10 15:46:23 +0200651/* perl-5.26 also needs S_TOPMARK and S_POPMARK. */
652# if (PERL_REVISION == 5) && (PERL_VERSION >= 26)
653PERL_STATIC_INLINE I32
654S_TOPMARK(pTHX)
655{
656 DEBUG_s(DEBUG_v(PerlIO_printf(Perl_debug_log,
657 "MARK top %p %" IVdf "\n",
658 PL_markstack_ptr,
659 (IV)*PL_markstack_ptr)));
660 return *PL_markstack_ptr;
661}
662
663PERL_STATIC_INLINE I32
664S_POPMARK(pTHX)
665{
666 DEBUG_s(DEBUG_v(PerlIO_printf(Perl_debug_log,
667 "MARK pop %p %" IVdf "\n",
668 (PL_markstack_ptr-1),
669 (IV)*(PL_markstack_ptr-1))));
670 assert((PL_markstack_ptr > PL_markstack) || !"MARK underflow");
671 return *PL_markstack_ptr--;
672}
673# endif
674
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675/*
676 * Make all runtime-links of perl.
677 *
Bram Moolenaar364ab2f2013-08-02 20:05:32 +0200678 * 1. Get module handle using dlopen() or vimLoadLib().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 * 2. Get pointer to perl function by GetProcAddress.
680 * 3. Repeat 2, until get all functions will be used.
681 *
682 * Parameter 'libname' provides name of DLL.
683 * Return OK or FAIL.
684 */
685 static int
686perl_runtime_link_init(char *libname, int verbose)
687{
688 int i;
689
690 if (hPerlLib != NULL)
691 return OK;
Bram Moolenaare06c1882010-07-21 22:05:20 +0200692 if ((hPerlLib = load_dll(libname)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 {
694 if (verbose)
695 EMSG2(_("E370: Could not load library %s"), libname);
696 return FAIL;
697 }
698 for (i = 0; perl_funcname_table[i].ptr; ++i)
699 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200700 if (!(*perl_funcname_table[i].ptr = symbol_from_dll(hPerlLib,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 perl_funcname_table[i].name)))
702 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200703 close_dll(hPerlLib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 hPerlLib = NULL;
705 if (verbose)
706 EMSG2(_(e_loadfunc), perl_funcname_table[i].name);
707 return FAIL;
708 }
709 }
710 return OK;
711}
712
713/*
714 * If runtime-link-perl(DLL) was loaded successfully, return TRUE.
715 * There were no DLL loaded, return FALSE.
716 */
717 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100718perl_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100720 return perl_runtime_link_init((char *)p_perldll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721}
722#endif /* DYNAMIC_PERL */
723
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200724#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
725static void vim_IOLayer_init(void);
726#endif
727
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728/*
729 * perl_init(): initialize perl interpreter
730 * We have to call perl_parse to initialize some structures,
731 * there's nothing to actually parse.
732 */
733 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100734perl_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735{
Bram Moolenaarc236c162008-07-13 17:41:49 +0000736 char *bootargs[] = { "VI", NULL };
737 int argc = 3;
738 static char *argv[] = { "", "-e", "" };
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739
Bram Moolenaarc236c162008-07-13 17:41:49 +0000740#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000741 Perl_sys_init(&argc, (char***)&argv);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 perl_interp = perl_alloc();
744 perl_construct(perl_interp);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000745 perl_parse(perl_interp, xs_init, argc, argv, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 perl_call_argv("VIM::bootstrap", (long)G_DISCARD, bootargs);
747 VIM_init();
748#ifdef USE_SFIO
749 sfdisc(PerlIO_stdout(), sfdcnewvim());
750 sfdisc(PerlIO_stderr(), sfdcnewvim());
751 sfsetbuf(PerlIO_stdout(), NULL, 0);
752 sfsetbuf(PerlIO_stderr(), NULL, 0);
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200753#elif defined(PERLIO_LAYERS)
754 vim_IOLayer_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755#endif
756}
757
758/*
759 * perl_end(): clean up after ourselves
760 */
761 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100762perl_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763{
764 if (perl_interp)
765 {
766 perl_run(perl_interp);
767 perl_destruct(perl_interp);
768 perl_free(perl_interp);
769 perl_interp = NULL;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000770#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100771 Perl_sys_term();
Bram Moolenaarc236c162008-07-13 17:41:49 +0000772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 }
774#ifdef DYNAMIC_PERL
775 if (hPerlLib)
776 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200777 close_dll(hPerlLib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 hPerlLib = NULL;
779 }
780#endif
781}
782
783/*
784 * msg_split(): send a message to the message handling routines
785 * split at '\n' first though.
786 */
787 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100788msg_split(
789 char_u *s,
790 int attr) /* highlighting attributes */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791{
792 char *next;
793 char *token = (char *)s;
794
Bram Moolenaaraa8494a2007-10-09 08:47:27 +0000795 while ((next = strchr(token, '\n')) && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 {
797 *next++ = '\0'; /* replace \n with \0 */
798 msg_attr((char_u *)token, attr);
799 token = next;
800 }
Bram Moolenaaraa8494a2007-10-09 08:47:27 +0000801 if (*token && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 msg_attr((char_u *)token, attr);
803}
804
805#ifndef FEAT_EVAL
806/*
807 * This stub is needed because an "#ifdef FEAT_EVAL" around Eval() doesn't
808 * work properly.
809 */
810 char_u *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100811eval_to_string(
812 char_u *arg UNUSED,
813 char_u **nextcmd UNUSED,
814 int dolist UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815{
816 return NULL;
817}
818#endif
819
820/*
821 * Create a new reference to an SV pointing to the SCR structure
Bram Moolenaare344bea2005-09-01 20:46:49 +0000822 * The b_perl_private/w_perl_private part of the SCR structure points to the
823 * SV, so there can only be one such SV for a particular SCR structure. When
824 * the last reference has gone (DESTROY is called),
825 * b_perl_private/w_perl_private is reset; When the screen goes away before
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 * all references are gone, the value of the SV is reset;
827 * any subsequent use of any of those reference will produce
828 * a warning. (see typemap)
829 */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000830
831 static SV *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100832newWINrv(SV *rv, win_T *ptr)
Bram Moolenaare344bea2005-09-01 20:46:49 +0000833{
834 sv_upgrade(rv, SVt_RV);
835 if (ptr->w_perl_private == NULL)
836 {
837 ptr->w_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +0100838 sv_setiv(ptr->w_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +0000839 }
Bram Moolenaar41c363a2018-08-02 21:46:51 +0200840 SvREFCNT_inc_void_NN(ptr->w_perl_private);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000841 SvRV(rv) = ptr->w_perl_private;
842 SvROK_on(rv);
843 return sv_bless(rv, gv_stashpv("VIWIN", TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844}
845
Bram Moolenaare344bea2005-09-01 20:46:49 +0000846 static SV *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100847newBUFrv(SV *rv, buf_T *ptr)
Bram Moolenaare344bea2005-09-01 20:46:49 +0000848{
849 sv_upgrade(rv, SVt_RV);
850 if (ptr->b_perl_private == NULL)
851 {
852 ptr->b_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +0100853 sv_setiv(ptr->b_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +0000854 }
Bram Moolenaar41c363a2018-08-02 21:46:51 +0200855 SvREFCNT_inc_void_NN(ptr->b_perl_private);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000856 SvRV(rv) = ptr->b_perl_private;
857 SvROK_on(rv);
858 return sv_bless(rv, gv_stashpv("VIBUF", TRUE));
859}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200861#if 0
862SV *__sv_save[1024];
863int __sv_save_ix;
864# define D_Save_Sv(sv) do { if (__sv_save_ix < 1024) __sv_save[__sv_save_ix++] = (sv); } while (0)
865#else
866# define D_Save_Sv(sv) NOOP
867#endif
868
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869/*
870 * perl_win_free
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200871 * Remove all references to the window to be destroyed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 */
873 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100874perl_win_free(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875{
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200876 if (wp->w_perl_private && perl_interp != NULL)
877 {
Bram Moolenaar578333b2018-07-22 07:31:09 +0200878 SV *sv = (SV*)wp->w_perl_private;
879 D_Save_Sv(sv);
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200880 sv_setiv(sv, 0);
881 SvREFCNT_dec(sv);
882 }
883 wp->w_perl_private = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884}
885
886 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100887perl_buf_free(buf_T *bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888{
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200889 if (bp->b_perl_private && perl_interp != NULL)
890 {
Bram Moolenaar578333b2018-07-22 07:31:09 +0200891 SV *sv = (SV *)bp->b_perl_private;
892 D_Save_Sv(sv);
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200893 sv_setiv(sv, 0);
894 SvREFCNT_dec(sv);
895 }
896 bp->b_perl_private = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897}
898
899#ifndef PROTO
900# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
901I32 cur_val(pTHX_ IV iv, SV *sv);
902# else
903I32 cur_val(IV iv, SV *sv);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200904# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905
906/*
907 * Handler for the magic variables $main::curwin and $main::curbuf.
908 * The handler is put into the magic vtbl for these variables.
909 * (This is effectively a C-level equivalent of a tied variable).
910 * There is no "set" function as the variables are read-only.
911 */
912# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
913I32 cur_val(pTHX_ IV iv, SV *sv)
914# else
915I32 cur_val(IV iv, SV *sv)
916# endif
917{
918 SV *rv;
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200919
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 if (iv == 0)
921 rv = newWINrv(newSV(0), curwin);
922 else
923 rv = newBUFrv(newSV(0), curbuf);
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200924
Bram Moolenaar41c363a2018-08-02 21:46:51 +0200925 if (SvRV(sv) != SvRV(rv))
926 // XXX: This magic variable is a bit confusing...
927 // Is curently refcounted ?
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +0200928 sv_setsv(sv, rv);
929
Bram Moolenaar41c363a2018-08-02 21:46:51 +0200930 SvREFCNT_dec(rv);
931
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 return 0;
933}
934#endif /* !PROTO */
935
936struct ufuncs cw_funcs = { cur_val, 0, 0 };
937struct ufuncs cb_funcs = { cur_val, 0, 1 };
938
939/*
940 * VIM_init(): Vim-specific initialisation.
941 * Make the magical main::curwin and main::curbuf variables
942 */
943 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100944VIM_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945{
946 static char cw[] = "main::curwin";
947 static char cb[] = "main::curbuf";
948 SV *sv;
949
950 sv = perl_get_sv(cw, TRUE);
951 sv_magic(sv, NULL, 'U', (char *)&cw_funcs, sizeof(cw_funcs));
952 SvREADONLY_on(sv);
953
954 sv = perl_get_sv(cb, TRUE);
955 sv_magic(sv, NULL, 'U', (char *)&cb_funcs, sizeof(cb_funcs));
956 SvREADONLY_on(sv);
957
958 /*
959 * Setup the Safe compartment.
960 * It shouldn't be a fatal error if the Safe module is missing.
961 * XXX: Only shares the 'Msg' routine (which has to be called
962 * like 'Msg(...)').
963 */
964 (void)perl_eval_pv( "if ( eval( 'require Safe' ) ) { $VIM::safe = Safe->new(); $VIM::safe->share_from( 'VIM', ['Msg'] ); }", G_DISCARD | G_VOID );
965
966}
967
968#ifdef DYNAMIC_PERL
969static char *e_noperl = N_("Sorry, this command is disabled: the Perl library could not be loaded.");
970#endif
971
972/*
973 * ":perl"
974 */
975 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100976ex_perl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977{
978 char *err;
979 char *script;
980 STRLEN length;
981 SV *sv;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200982#ifdef HAVE_SANDBOX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 SV *safe;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985
986 script = (char *)script_get(eap, eap->arg);
987 if (eap->skip)
988 {
989 vim_free(script);
990 return;
991 }
992
993 if (perl_interp == NULL)
994 {
995#ifdef DYNAMIC_PERL
996 if (!perl_enabled(TRUE))
997 {
998 EMSG(_(e_noperl));
999 vim_free(script);
1000 return;
1001 }
1002#endif
1003 perl_init();
1004 }
1005
1006 {
1007 dSP;
1008 ENTER;
1009 SAVETMPS;
1010
1011 if (script == NULL)
1012 sv = newSVpv((char *)eap->arg, 0);
1013 else
1014 {
1015 sv = newSVpv(script, 0);
1016 vim_free(script);
1017 }
1018
1019#ifdef HAVE_SANDBOX
1020 if (sandbox)
1021 {
Bram Moolenaara1711622011-07-27 14:15:46 +02001022 safe = perl_get_sv("VIM::safe", FALSE);
Bram Moolenaar3f947ea2009-07-14 14:04:54 +00001023# ifndef MAKE_TEST /* avoid a warning for unreachable code */
Bram Moolenaar954e8c52009-11-11 13:45:33 +00001024 if (safe == NULL || !SvTRUE(safe))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 EMSG(_("E299: Perl evaluation forbidden in sandbox without the Safe module"));
1026 else
Bram Moolenaar3f947ea2009-07-14 14:04:54 +00001027# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 {
1029 PUSHMARK(SP);
1030 XPUSHs(safe);
1031 XPUSHs(sv);
1032 PUTBACK;
1033 perl_call_method("reval", G_DISCARD);
1034 }
1035 }
1036 else
1037#endif
1038 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
1039
1040 SvREFCNT_dec(sv);
1041
Bram Moolenaar7b61bf12016-06-26 17:16:51 +02001042 err = SvPV(GvSV(PL_errgv), length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043
1044 FREETMPS;
1045 LEAVE;
1046
1047 if (!length)
1048 return;
1049
1050 msg_split((char_u *)err, highlight_attr[HLF_E]);
1051 return;
1052 }
1053}
1054
1055 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001056replace_line(linenr_T *line, linenr_T *end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057{
1058 char *str;
1059
1060 if (SvOK(GvSV(PL_defgv)))
1061 {
1062 str = SvPV(GvSV(PL_defgv), PL_na);
1063 ml_replace(*line, (char_u *)str, 1);
1064 changed_bytes(*line, 0);
1065 }
1066 else
1067 {
1068 ml_delete(*line, FALSE);
1069 deleted_lines_mark(*line, 1L);
1070 --(*end);
1071 --(*line);
1072 }
1073 return OK;
1074}
1075
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001076static struct ref_map_S {
1077 void *vim_ref;
1078 SV *perl_ref;
1079 struct ref_map_S *next;
1080} *ref_map = NULL;
1081
1082 static void
1083ref_map_free(void)
1084{
1085 struct ref_map_S *tofree;
1086 struct ref_map_S *refs = ref_map;
1087
1088 while (refs) {
1089 tofree = refs;
1090 refs = refs->next;
1091 vim_free(tofree);
1092 }
1093 ref_map = NULL;
1094}
1095
1096 static struct ref_map_S *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001097ref_map_find_SV(SV *const sv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001098{
1099 struct ref_map_S *refs = ref_map;
1100 int count = 350;
1101
1102 while (refs) {
1103 if (refs->perl_ref == sv)
1104 break;
1105 refs = refs->next;
1106 count--;
1107 }
1108
1109 if (!refs && count > 0) {
1110 refs = (struct ref_map_S *)alloc(sizeof(struct ref_map_S));
1111 if (!refs)
1112 return NULL;
1113 refs->perl_ref = sv;
1114 refs->vim_ref = NULL;
1115 refs->next = ref_map;
1116 ref_map = refs;
1117 }
1118
1119 return refs;
1120}
1121
1122 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001123perl_to_vim(SV *sv, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001124{
1125 if (SvROK(sv))
1126 sv = SvRV(sv);
1127
1128 switch (SvTYPE(sv)) {
1129 case SVt_NULL:
1130 break;
1131 case SVt_NV: /* float */
1132#ifdef FEAT_FLOAT
1133 rettv->v_type = VAR_FLOAT;
1134 rettv->vval.v_float = SvNV(sv);
1135 break;
1136#endif
1137 case SVt_IV: /* integer */
1138 if (!SvROK(sv)) { /* references should be string */
1139 rettv->vval.v_number = SvIV(sv);
1140 break;
1141 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001142 /* FALLTHROUGH */
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001143 case SVt_PV: /* string */
1144 {
1145 size_t len = 0;
1146 char * str_from = SvPV(sv, len);
Bram Moolenaar9b0ac222016-06-01 20:31:43 +02001147 char_u *str_to = (char_u*)alloc(
1148 (unsigned)(sizeof(char_u) * (len + 1)));
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001149
1150 if (str_to) {
1151 str_to[len] = '\0';
1152
1153 while (len--) {
1154 if (str_from[len] == '\0')
1155 str_to[len] = '\n';
1156 else
1157 str_to[len] = str_from[len];
1158 }
1159 }
1160
1161 rettv->v_type = VAR_STRING;
1162 rettv->vval.v_string = str_to;
1163 break;
1164 }
1165 case SVt_PVAV: /* list */
1166 {
1167 SSize_t size;
1168 listitem_T * item;
1169 SV ** item2;
1170 list_T * list;
1171 struct ref_map_S * refs;
1172
1173 if ((refs = ref_map_find_SV(sv)) == NULL)
1174 return FAIL;
1175
1176 if (refs->vim_ref)
1177 list = (list_T *) refs->vim_ref;
1178 else
1179 {
1180 if ((list = list_alloc()) == NULL)
1181 return FAIL;
1182 refs->vim_ref = list;
1183
1184 for (size = av_len((AV*)sv); size >= 0; size--)
1185 {
1186 if ((item = listitem_alloc()) == NULL)
1187 break;
1188
1189 item->li_tv.v_type = VAR_NUMBER;
1190 item->li_tv.v_lock = 0;
1191 item->li_tv.vval.v_number = 0;
1192 list_insert(list, item, list->lv_first);
1193
1194 item2 = av_fetch((AV *)sv, size, 0);
1195
1196 if (item2 == NULL || *item2 == NULL ||
Bram Moolenaard99df422016-01-29 23:20:40 +01001197 perl_to_vim(*item2, &item->li_tv) == FAIL)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001198 break;
1199 }
1200 }
1201
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001202 rettv_list_set(rettv, list);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001203 break;
1204 }
1205 case SVt_PVHV: /* dictionary */
1206 {
1207 HE * entry;
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001208 I32 key_len;
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001209 char * key;
1210 dictitem_T * item;
1211 SV * item2;
1212 dict_T * dict;
1213 struct ref_map_S * refs;
1214
1215 if ((refs = ref_map_find_SV(sv)) == NULL)
1216 return FAIL;
1217
1218 if (refs->vim_ref)
1219 dict = (dict_T *) refs->vim_ref;
1220 else
1221 {
1222
1223 if ((dict = dict_alloc()) == NULL)
1224 return FAIL;
1225 refs->vim_ref = dict;
1226
1227 hv_iterinit((HV *)sv);
1228
1229 for (entry = hv_iternext((HV *)sv); entry; entry = hv_iternext((HV *)sv))
1230 {
1231 key_len = 0;
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001232 key = hv_iterkey(entry, &key_len);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001233
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001234 if (!key || !key_len || strlen(key) < (size_t)key_len) {
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001235 EMSG2("Malformed key Dictionary '%s'", key && *key ? key : "(empty)");
1236 break;
1237 }
1238
1239 if ((item = dictitem_alloc((char_u *)key)) == NULL)
1240 break;
Bram Moolenaarc89d4b32018-07-08 17:19:02 +02001241 item->di_tv.v_type = VAR_NUMBER;
1242 item->di_tv.vval.v_number = 0;
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001243
1244 if (dict_add(dict, item) == FAIL) {
1245 dictitem_free(item);
1246 break;
1247 }
1248 item2 = hv_iterval((HV *)sv, entry);
1249 if (item2 == NULL || perl_to_vim(item2, &item->di_tv) == FAIL)
1250 break;
1251 }
1252 }
1253
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001254 rettv_dict_set(rettv, dict);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001255 break;
1256 }
1257 default: /* not convertible */
1258 {
1259 char *val = SvPV_nolen(sv);
1260 rettv->v_type = VAR_STRING;
1261 rettv->vval.v_string = val ? vim_strsave((char_u *)val) : NULL;
1262 break;
1263 }
1264 }
1265 return OK;
1266}
1267
1268/*
1269 * "perleval()"
1270 */
1271 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001272do_perleval(char_u *str, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001273{
1274 char *err = NULL;
1275 STRLEN err_len = 0;
1276 SV *sv = NULL;
1277#ifdef HAVE_SANDBOX
1278 SV *safe;
1279#endif
1280
1281 if (perl_interp == NULL)
1282 {
1283#ifdef DYNAMIC_PERL
1284 if (!perl_enabled(TRUE))
1285 {
1286 EMSG(_(e_noperl));
1287 return;
1288 }
1289#endif
1290 perl_init();
1291 }
1292
1293 {
1294 dSP;
1295 ENTER;
1296 SAVETMPS;
1297
1298#ifdef HAVE_SANDBOX
1299 if (sandbox)
1300 {
1301 safe = get_sv("VIM::safe", FALSE);
1302# ifndef MAKE_TEST /* avoid a warning for unreachable code */
1303 if (safe == NULL || !SvTRUE(safe))
1304 EMSG(_("E299: Perl evaluation forbidden in sandbox without the Safe module"));
1305 else
1306# endif
1307 {
1308 sv = newSVpv((char *)str, 0);
1309 PUSHMARK(SP);
1310 XPUSHs(safe);
1311 XPUSHs(sv);
1312 PUTBACK;
1313 call_method("reval", G_SCALAR);
1314 SPAGAIN;
1315 SvREFCNT_dec(sv);
1316 sv = POPs;
1317 }
1318 }
1319 else
1320#endif /* HAVE_SANDBOX */
1321 sv = eval_pv((char *)str, 0);
1322
1323 if (sv) {
1324 perl_to_vim(sv, rettv);
1325 ref_map_free();
Bram Moolenaar7b61bf12016-06-26 17:16:51 +02001326 err = SvPV(GvSV(PL_errgv), err_len);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001327 }
1328 PUTBACK;
1329 FREETMPS;
1330 LEAVE;
1331 }
1332 if (err_len)
1333 msg_split((char_u *)err, highlight_attr[HLF_E]);
1334}
1335
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336/*
1337 * ":perldo".
1338 */
1339 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001340ex_perldo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341{
1342 STRLEN length;
1343 SV *sv;
1344 char *str;
1345 linenr_T i;
Bram Moolenaar85b57432017-01-29 22:59:12 +01001346 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001348 if (BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 return;
1350
1351 if (perl_interp == NULL)
1352 {
1353#ifdef DYNAMIC_PERL
1354 if (!perl_enabled(TRUE))
1355 {
1356 EMSG(_(e_noperl));
1357 return;
1358 }
1359#endif
1360 perl_init();
1361 }
1362 {
1363 dSP;
1364 length = strlen((char *)eap->arg);
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001365 sv = newSV(length + sizeof("sub VIM::perldo {") - 1 + 1);
1366 sv_setpvn(sv, "sub VIM::perldo {", sizeof("sub VIM::perldo {") - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 sv_catpvn(sv, (char *)eap->arg, length);
1368 sv_catpvn(sv, "}", 1);
1369 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
1370 SvREFCNT_dec(sv);
Bram Moolenaar7b61bf12016-06-26 17:16:51 +02001371 str = SvPV(GvSV(PL_errgv), length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 if (length)
1373 goto err;
1374
1375 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
1376 return;
1377
1378 ENTER;
1379 SAVETMPS;
1380 for (i = eap->line1; i <= eap->line2; i++)
1381 {
Bram Moolenaar85b57432017-01-29 22:59:12 +01001382 /* Check the line number, the command my have deleted lines. */
1383 if (i > curbuf->b_ml.ml_line_count)
1384 break;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001385 sv_setpv(GvSV(PL_defgv), (char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 PUSHMARK(sp);
1387 perl_call_pv("VIM::perldo", G_SCALAR | G_EVAL);
Bram Moolenaar7b61bf12016-06-26 17:16:51 +02001388 str = SvPV(GvSV(PL_errgv), length);
Bram Moolenaar85b57432017-01-29 22:59:12 +01001389 if (length || curbuf != was_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 break;
1391 SPAGAIN;
1392 if (SvTRUEx(POPs))
1393 {
1394 if (replace_line(&i, &eap->line2) != OK)
1395 {
1396 PUTBACK;
1397 break;
1398 }
1399 }
1400 PUTBACK;
1401 }
1402 FREETMPS;
1403 LEAVE;
1404 check_cursor();
1405 update_screen(NOT_VALID);
1406 if (!length)
1407 return;
1408
1409err:
1410 msg_split((char_u *)str, highlight_attr[HLF_E]);
1411 return;
1412 }
1413}
1414
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001415#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
1416typedef struct {
1417 struct _PerlIO base;
1418 int attr;
1419} PerlIOVim;
1420
1421 static IV
1422PerlIOVim_pushed(pTHX_ PerlIO *f, const char *mode,
1423 SV *arg, PerlIO_funcs *tab)
1424{
1425 PerlIOVim *s = PerlIOSelf(f, PerlIOVim);
1426 s->attr = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001427 if (arg && SvPOK(arg))
1428 s->attr = syn_name2attr((char_u *)SvPV_nolen(arg));
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001429 return PerlIOBase_pushed(aTHX_ f, mode, (SV *)NULL, tab);
1430}
1431
1432 static SSize_t
1433PerlIOVim_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1434{
1435 char_u *str;
1436 PerlIOVim * s = PerlIOSelf(f, PerlIOVim);
1437
Bram Moolenaar9b0ac222016-06-01 20:31:43 +02001438 str = vim_strnsave((char_u *)vbuf, (int)count);
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001439 if (str == NULL)
1440 return 0;
1441 msg_split((char_u *)str, s->attr);
1442 vim_free(str);
1443
Bram Moolenaar9b0ac222016-06-01 20:31:43 +02001444 return (SSize_t)count;
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001445}
1446
1447static PERLIO_FUNCS_DECL(PerlIO_Vim) = {
1448 sizeof(PerlIO_funcs),
1449 "Vim",
1450 sizeof(PerlIOVim),
1451 PERLIO_K_DUMMY, /* flags */
1452 PerlIOVim_pushed,
1453 NULL, /* popped */
1454 NULL, /* open */
1455 NULL, /* binmode */
1456 NULL, /* arg */
1457 NULL, /* fileno */
1458 NULL, /* dup */
1459 NULL, /* read */
1460 NULL, /* unread */
1461 PerlIOVim_write,
1462 NULL, /* seek */
1463 NULL, /* tell */
1464 NULL, /* close */
1465 NULL, /* flush */
1466 NULL, /* fill */
1467 NULL, /* eof */
1468 NULL, /* error */
1469 NULL, /* clearerr */
1470 NULL, /* setlinebuf */
1471 NULL, /* get_base */
1472 NULL, /* get_bufsiz */
1473 NULL, /* get_ptr */
1474 NULL, /* get_cnt */
1475 NULL /* set_ptrcnt */
1476};
1477
1478/* Use Vim routine for print operator */
1479 static void
1480vim_IOLayer_init(void)
1481{
1482 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_Vim));
1483 (void)eval_pv( "binmode(STDOUT, ':Vim')"
1484 " && binmode(STDERR, ':Vim(ErrorMsg)');", 0);
1485}
1486#endif /* PERLIO_LAYERS && !USE_SFIO */
1487
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488XS(boot_VIM);
1489
1490 static void
1491xs_init(pTHX)
1492{
1493 char *file = __FILE__;
1494
1495 /* DynaLoader is a special case */
1496 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1497 newXS("VIM::bootstrap", boot_VIM, file);
1498}
1499
1500typedef win_T * VIWIN;
1501typedef buf_T * VIBUF;
1502
1503MODULE = VIM PACKAGE = VIM
1504
1505void
1506Msg(text, hl=NULL)
1507 char *text;
1508 char *hl;
1509
1510 PREINIT:
1511 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512
1513 PPCODE:
1514 if (text != NULL)
1515 {
1516 attr = 0;
1517 if (hl != NULL)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001518 attr = syn_name2attr((char_u *)hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 msg_split((char_u *)text, attr);
1520 }
1521
1522void
1523SetOption(line)
1524 char *line;
1525
1526 PPCODE:
1527 if (line != NULL)
1528 do_set((char_u *)line, 0);
1529 update_screen(NOT_VALID);
1530
1531void
1532DoCommand(line)
1533 char *line;
1534
1535 PPCODE:
1536 if (line != NULL)
1537 do_cmdline_cmd((char_u *)line);
1538
1539void
1540Eval(str)
1541 char *str;
1542
1543 PREINIT:
1544 char_u *value;
1545 PPCODE:
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001546 value = eval_to_string((char_u *)str, (char_u **)0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 if (value == NULL)
1548 {
1549 XPUSHs(sv_2mortal(newSViv(0)));
1550 XPUSHs(sv_2mortal(newSVpv("", 0)));
1551 }
1552 else
1553 {
1554 XPUSHs(sv_2mortal(newSViv(1)));
1555 XPUSHs(sv_2mortal(newSVpv((char *)value, 0)));
1556 vim_free(value);
1557 }
1558
1559void
1560Buffers(...)
1561
1562 PREINIT:
1563 buf_T *vimbuf;
1564 int i, b;
1565
1566 PPCODE:
1567 if (items == 0)
1568 {
1569 if (GIMME == G_SCALAR)
1570 {
1571 i = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02001572 FOR_ALL_BUFFERS(vimbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 ++i;
1574
1575 XPUSHs(sv_2mortal(newSViv(i)));
1576 }
1577 else
1578 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001579 FOR_ALL_BUFFERS(vimbuf)
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +02001580 XPUSHs(sv_2mortal(newBUFrv(newSV(0), vimbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 }
1582 }
1583 else
1584 {
1585 for (i = 0; i < items; i++)
1586 {
1587 SV *sv = ST(i);
1588 if (SvIOK(sv))
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001589 b = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 else
1591 {
1592 char_u *pat;
1593 STRLEN len;
1594
1595 pat = (char_u *)SvPV(sv, len);
1596 ++emsg_off;
Bram Moolenaar16896a12018-03-06 12:25:56 +01001597 b = buflist_findpat(pat, pat + len, TRUE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 --emsg_off;
1599 }
1600
1601 if (b >= 0)
1602 {
1603 vimbuf = buflist_findnr(b);
1604 if (vimbuf)
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +02001605 XPUSHs(sv_2mortal(newBUFrv(newSV(0), vimbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 }
1607 }
1608 }
1609
1610void
1611Windows(...)
1612
1613 PREINIT:
1614 win_T *vimwin;
1615 int i, w;
1616
1617 PPCODE:
1618 if (items == 0)
1619 {
1620 if (GIMME == G_SCALAR)
1621 XPUSHs(sv_2mortal(newSViv(win_count())));
1622 else
1623 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001624 FOR_ALL_WINDOWS(vimwin)
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +02001625 XPUSHs(sv_2mortal(newWINrv(newSV(0), vimwin)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 }
1627 }
1628 else
1629 {
1630 for (i = 0; i < items; i++)
1631 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001632 w = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 vimwin = win_find_nr(w);
1634 if (vimwin)
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +02001635 XPUSHs(sv_2mortal(newWINrv(newSV(0), vimwin)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 }
1637 }
1638
1639MODULE = VIM PACKAGE = VIWIN
1640
1641void
1642DESTROY(win)
1643 VIWIN win
1644
1645 CODE:
1646 if (win_valid(win))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001647 win->w_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648
1649SV *
1650Buffer(win)
1651 VIWIN win
1652
1653 CODE:
1654 if (!win_valid(win))
1655 win = curwin;
1656 RETVAL = newBUFrv(newSV(0), win->w_buffer);
1657 OUTPUT:
1658 RETVAL
1659
1660void
1661SetHeight(win, height)
1662 VIWIN win
1663 int height;
1664
1665 PREINIT:
1666 win_T *savewin;
1667
1668 PPCODE:
1669 if (!win_valid(win))
1670 win = curwin;
1671 savewin = curwin;
1672 curwin = win;
1673 win_setheight(height);
1674 curwin = savewin;
1675
1676void
Bram Moolenaar864733a2016-04-02 14:18:01 +02001677Cursor(win, ...)
1678 VIWIN win
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679
1680 PPCODE:
Bram Moolenaara1711622011-07-27 14:15:46 +02001681 if (items == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 {
1683 EXTEND(sp, 2);
1684 if (!win_valid(win))
1685 win = curwin;
1686 PUSHs(sv_2mortal(newSViv(win->w_cursor.lnum)));
1687 PUSHs(sv_2mortal(newSViv(win->w_cursor.col)));
1688 }
Bram Moolenaara1711622011-07-27 14:15:46 +02001689 else if (items == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 {
1691 int lnum, col;
1692
1693 if (!win_valid(win))
1694 win = curwin;
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001695 lnum = (int) SvIV(ST(1));
1696 col = (int) SvIV(ST(2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 win->w_cursor.lnum = lnum;
1698 win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02001699 win->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 check_cursor(); /* put cursor on an existing line */
1701 update_screen(NOT_VALID);
1702 }
1703
1704MODULE = VIM PACKAGE = VIBUF
1705
1706void
1707DESTROY(vimbuf)
1708 VIBUF vimbuf;
1709
1710 CODE:
1711 if (buf_valid(vimbuf))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001712 vimbuf->b_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713
1714void
1715Name(vimbuf)
1716 VIBUF vimbuf;
1717
1718 PPCODE:
1719 if (!buf_valid(vimbuf))
1720 vimbuf = curbuf;
1721 /* No file name returns an empty string */
1722 if (vimbuf->b_fname == NULL)
1723 XPUSHs(sv_2mortal(newSVpv("", 0)));
1724 else
1725 XPUSHs(sv_2mortal(newSVpv((char *)vimbuf->b_fname, 0)));
1726
1727void
1728Number(vimbuf)
1729 VIBUF vimbuf;
1730
1731 PPCODE:
1732 if (!buf_valid(vimbuf))
1733 vimbuf = curbuf;
1734 XPUSHs(sv_2mortal(newSViv(vimbuf->b_fnum)));
1735
1736void
1737Count(vimbuf)
1738 VIBUF vimbuf;
1739
1740 PPCODE:
1741 if (!buf_valid(vimbuf))
1742 vimbuf = curbuf;
1743 XPUSHs(sv_2mortal(newSViv(vimbuf->b_ml.ml_line_count)));
1744
1745void
1746Get(vimbuf, ...)
1747 VIBUF vimbuf;
1748
1749 PREINIT:
1750 char_u *line;
1751 int i;
1752 long lnum;
1753 PPCODE:
1754 if (buf_valid(vimbuf))
1755 {
1756 for (i = 1; i < items; i++)
1757 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001758 lnum = (long) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1760 {
1761 line = ml_get_buf(vimbuf, lnum, FALSE);
1762 XPUSHs(sv_2mortal(newSVpv((char *)line, 0)));
1763 }
1764 }
1765 }
1766
1767void
1768Set(vimbuf, ...)
1769 VIBUF vimbuf;
1770
1771 PREINIT:
1772 int i;
1773 long lnum;
1774 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 PPCODE:
1776 if (buf_valid(vimbuf))
1777 {
1778 if (items < 3)
1779 croak("Usage: VIBUF::Set(vimbuf, lnum, @lines)");
1780
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001781 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 for(i = 2; i < items; i++, lnum++)
1783 {
1784 line = SvPV(ST(i),PL_na);
1785 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1786 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001787 aco_save_T aco;
1788
1789 /* set curwin/curbuf for "vimbuf" and save some things */
1790 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001791
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 if (u_savesub(lnum) == OK)
1793 {
1794 ml_replace(lnum, (char_u *)line, TRUE);
1795 changed_bytes(lnum, 0);
1796 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001797
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001798 /* restore curwin/curbuf and a few other things */
1799 aucmd_restbuf(&aco);
1800 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 }
1802 }
1803 }
1804
1805void
1806Delete(vimbuf, ...)
1807 VIBUF vimbuf;
1808
1809 PREINIT:
1810 long i, lnum = 0, count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 PPCODE:
1812 if (buf_valid(vimbuf))
1813 {
1814 if (items == 2)
1815 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001816 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 count = 1;
1818 }
1819 else if (items == 3)
1820 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001821 lnum = (long) SvIV(ST(1));
1822 count = (long) 1 + SvIV(ST(2)) - lnum;
Bram Moolenaara1711622011-07-27 14:15:46 +02001823 if (count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 count = 1;
Bram Moolenaara1711622011-07-27 14:15:46 +02001825 if (count < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 {
1827 lnum -= count;
1828 count = -count;
1829 }
1830 }
1831 if (items >= 2)
1832 {
1833 for (i = 0; i < count; i++)
1834 {
1835 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1836 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001837 aco_save_T aco;
1838
1839 /* set curwin/curbuf for "vimbuf" and save some things */
1840 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001841
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 if (u_savedel(lnum, 1) == OK)
1843 {
1844 ml_delete(lnum, 0);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00001845 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 deleted_lines_mark(lnum, 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001848
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001849 /* restore curwin/curbuf and a few other things */
1850 aucmd_restbuf(&aco);
1851 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001852
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 update_curbuf(VALID);
1854 }
1855 }
1856 }
1857 }
1858
1859void
1860Append(vimbuf, ...)
1861 VIBUF vimbuf;
1862
1863 PREINIT:
1864 int i;
1865 long lnum;
1866 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 PPCODE:
1868 if (buf_valid(vimbuf))
1869 {
1870 if (items < 3)
1871 croak("Usage: VIBUF::Append(vimbuf, lnum, @lines)");
1872
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001873 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 for (i = 2; i < items; i++, lnum++)
1875 {
1876 line = SvPV(ST(i),PL_na);
1877 if (lnum >= 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1878 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001879 aco_save_T aco;
1880
1881 /* set curwin/curbuf for "vimbuf" and save some things */
1882 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001883
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 if (u_inssub(lnum + 1) == OK)
1885 {
1886 ml_append(lnum, (char_u *)line, (colnr_T)0, FALSE);
1887 appended_lines_mark(lnum, 1L);
1888 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001889
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001890 /* restore curwin/curbuf and a few other things */
1891 aucmd_restbuf(&aco);
1892 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001893
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 update_curbuf(VALID);
1895 }
1896 }
1897 }
1898
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001899#ifdef __GNUC__
1900# pragma GCC diagnostic pop
1901#endif