blob: 47c944039c2d2e822d99c1adbea0d38ebea35dca [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 Moolenaar6aa2cd42016-02-16 15:06:59 +010052#ifdef __GNUC__
53# pragma GCC diagnostic push
54# pragma GCC diagnostic ignored "-Wunused-variable"
55# pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
56#endif
57
Bram Moolenaaraee1f4a2013-08-02 16:10:32 +020058#include <EXTERN.h>
59#include <perl.h>
60#include <XSUB.h>
61
Bram Moolenaar071d4272004-06-13 20:20:40 +000062
63/*
64 * Work around clashes between Perl and Vim namespace. proto.h doesn't
65 * include if_perl.pro and perlsfio.pro when IN_PERL_FILE is defined, because
66 * we need the CV typedef. proto.h can't be moved to after including
67 * if_perl.h, because we get all sorts of name clashes then.
68 */
69#ifndef PROTO
70#ifndef __MINGW32__
71# include "proto/if_perl.pro"
72# include "proto/if_perlsfio.pro"
73#endif
74#endif
75
76/* Perl compatibility stuff. This should ensure compatibility with older
77 * versions of Perl.
78 */
79
80#ifndef PERL_VERSION
81# include <patchlevel.h>
82# define PERL_REVISION 5
83# define PERL_VERSION PATCHLEVEL
84# define PERL_SUBVERSION SUBVERSION
85#endif
86
Bram Moolenaar700d1d72007-09-13 13:20:16 +000087/*
88 * Quoting Jan Dubois of Active State:
89 * ActivePerl build 822 still identifies itself as 5.8.8 but already
90 * contains many of the changes from the upcoming Perl 5.8.9 release.
91 *
92 * The changes include addition of two symbols (Perl_sv_2iv_flags,
93 * Perl_newXS_flags) not present in earlier releases.
94 *
Bram Moolenaar3b9b13e2007-09-15 12:49:35 +000095 * Jan Dubois suggested the following guarding scheme.
96 *
97 * Active State defined ACTIVEPERL_VERSION as a string in versions before
98 * 5.8.8; and so the comparison to 822 below needs to be guarded.
Bram Moolenaar700d1d72007-09-13 13:20:16 +000099 */
Bram Moolenaar3b9b13e2007-09-15 12:49:35 +0000100#if (PERL_REVISION == 5) && (PERL_VERSION == 8) && (PERL_SUBVERSION >= 8)
101# if (ACTIVEPERL_VERSION >= 822) || (PERL_SUBVERSION >= 9)
102# define PERL589_OR_LATER
103# endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000104#endif
105#if (PERL_REVISION == 5) && (PERL_VERSION >= 9)
106# define PERL589_OR_LATER
107#endif
108
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100109#if (PERL_REVISION == 5) && ((PERL_VERSION > 10) || \
110 (PERL_VERSION == 10) && (PERL_SUBVERSION >= 1))
111# define PERL5101_OR_LATER
112#endif
113
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#ifndef pTHX
115# define pTHX void
116# define pTHX_
117#endif
118
119#ifndef EXTERN_C
120# define EXTERN_C
121#endif
122
Bram Moolenaarc271c482012-08-08 13:17:31 +0200123#if (PERL_REVISION == 5) && (PERL_VERSION >= 14) && defined(_MSC_VER)
124/* Using PL_errgv to get the error message after perl_eval_sv() causes a crash
125 * with MSVC and Perl version 5.14. */
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100126# define CHECK_EVAL_ERR(len) SvPV(perl_get_sv("@", GV_ADD), (len));
127#else
128# define CHECK_EVAL_ERR(len) SvPV(GvSV(PL_errgv), (len));
Bram Moolenaarc271c482012-08-08 13:17:31 +0200129#endif
130
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131/* Compatibility hacks over */
132
133static PerlInterpreter *perl_interp = NULL;
Bram Moolenaard99df422016-01-29 23:20:40 +0100134static void xs_init(pTHX);
135static void VIM_init(void);
136EXTERN_C void boot_DynaLoader(pTHX_ CV*);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137
138/*
Bram Moolenaare06c1882010-07-21 22:05:20 +0200139 * For dynamic linked perl.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 */
141#if defined(DYNAMIC_PERL) || defined(PROTO)
Bram Moolenaare06c1882010-07-21 22:05:20 +0200142
143#ifndef DYNAMIC_PERL /* just generating prototypes */
Bram Moolenaar766fb0d2010-07-22 11:34:16 +0200144#ifdef WIN3264
Bram Moolenaare06c1882010-07-21 22:05:20 +0200145typedef int HANDLE;
146#endif
147typedef int XSINIT_t;
148typedef int XSUBADDR_t;
Bram Moolenaard8619992014-03-12 17:08:05 +0100149#endif
150#ifndef USE_ITHREADS
Bram Moolenaare06c1882010-07-21 22:05:20 +0200151typedef int perl_key;
152#endif
153
Bram Moolenaar766fb0d2010-07-22 11:34:16 +0200154#ifndef WIN3264
Bram Moolenaare06c1882010-07-21 22:05:20 +0200155#include <dlfcn.h>
156#define HANDLE void*
157#define PERL_PROC void*
158#define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
159#define symbol_from_dll dlsym
160#define close_dll dlclose
161#else
162#define PERL_PROC FARPROC
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200163#define load_dll vimLoadLib
Bram Moolenaare06c1882010-07-21 22:05:20 +0200164#define symbol_from_dll GetProcAddress
165#define close_dll FreeLibrary
166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167/*
168 * Wrapper defines
169 */
170# define perl_alloc dll_perl_alloc
171# define perl_construct dll_perl_construct
172# define perl_parse dll_perl_parse
173# define perl_run dll_perl_run
174# define perl_destruct dll_perl_destruct
175# define perl_free dll_perl_free
176# define Perl_get_context dll_Perl_get_context
177# define Perl_croak dll_Perl_croak
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100178# ifdef PERL5101_OR_LATER
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100179# define Perl_croak_xs_usage dll_Perl_croak_xs_usage
180# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181# ifndef PROTO
182# define Perl_croak_nocontext dll_Perl_croak_nocontext
183# define Perl_call_argv dll_Perl_call_argv
184# define Perl_call_pv dll_Perl_call_pv
185# define Perl_eval_sv dll_Perl_eval_sv
186# define Perl_get_sv dll_Perl_get_sv
187# define Perl_eval_pv dll_Perl_eval_pv
188# define Perl_call_method dll_Perl_call_method
189# endif
190# define Perl_dowantarray dll_Perl_dowantarray
191# define Perl_free_tmps dll_Perl_free_tmps
192# define Perl_gv_stashpv dll_Perl_gv_stashpv
193# define Perl_markstack_grow dll_Perl_markstack_grow
194# define Perl_mg_find dll_Perl_mg_find
195# define Perl_newXS dll_Perl_newXS
196# define Perl_newSV dll_Perl_newSV
197# define Perl_newSViv dll_Perl_newSViv
198# define Perl_newSVpv dll_Perl_newSVpv
199# define Perl_pop_scope dll_Perl_pop_scope
200# define Perl_push_scope dll_Perl_push_scope
201# define Perl_save_int dll_Perl_save_int
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200202# if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
203# define Perl_save_strlen dll_Perl_save_strlen
204# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205# define Perl_stack_grow dll_Perl_stack_grow
206# define Perl_set_context dll_Perl_set_context
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200207# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200208# define Perl_sv_2bool_flags dll_Perl_sv_2bool_flags
209# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
210# define Perl_xs_apiversion_bootcheck dll_Perl_xs_apiversion_bootcheck
211# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200212# else
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200213# define Perl_sv_2bool dll_Perl_sv_2bool
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200214# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215# define Perl_sv_2iv dll_Perl_sv_2iv
216# define Perl_sv_2mortal dll_Perl_sv_2mortal
217# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
218# define Perl_sv_2pv_flags dll_Perl_sv_2pv_flags
219# define Perl_sv_2pv_nolen dll_Perl_sv_2pv_nolen
220# else
221# define Perl_sv_2pv dll_Perl_sv_2pv
222# endif
223# define Perl_sv_bless dll_Perl_sv_bless
224# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
225# define Perl_sv_catpvn_flags dll_Perl_sv_catpvn_flags
226# else
227# define Perl_sv_catpvn dll_Perl_sv_catpvn
228# endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000229#ifdef PERL589_OR_LATER
230# define Perl_sv_2iv_flags dll_Perl_sv_2iv_flags
231# define Perl_newXS_flags dll_Perl_newXS_flags
232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233# define Perl_sv_free dll_Perl_sv_free
Bram Moolenaarccf22172008-09-01 15:56:45 +0000234# if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
235# define Perl_sv_free2 dll_Perl_sv_free2
236# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237# define Perl_sv_isa dll_Perl_sv_isa
238# define Perl_sv_magic dll_Perl_sv_magic
239# define Perl_sv_setiv dll_Perl_sv_setiv
240# define Perl_sv_setpv dll_Perl_sv_setpv
241# define Perl_sv_setpvn dll_Perl_sv_setpvn
242# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
243# define Perl_sv_setsv_flags dll_Perl_sv_setsv_flags
244# else
245# define Perl_sv_setsv dll_Perl_sv_setsv
246# endif
247# define Perl_sv_upgrade dll_Perl_sv_upgrade
248# define Perl_Tstack_sp_ptr dll_Perl_Tstack_sp_ptr
249# define Perl_Top_ptr dll_Perl_Top_ptr
250# define Perl_Tstack_base_ptr dll_Perl_Tstack_base_ptr
251# define Perl_Tstack_max_ptr dll_Perl_Tstack_max_ptr
252# define Perl_Ttmps_ix_ptr dll_Perl_Ttmps_ix_ptr
253# define Perl_Ttmps_floor_ptr dll_Perl_Ttmps_floor_ptr
254# define Perl_Tmarkstack_ptr_ptr dll_Perl_Tmarkstack_ptr_ptr
255# define Perl_Tmarkstack_max_ptr dll_Perl_Tmarkstack_max_ptr
256# define Perl_TSv_ptr dll_Perl_TSv_ptr
257# define Perl_TXpv_ptr dll_Perl_TXpv_ptr
258# define Perl_Tna_ptr dll_Perl_Tna_ptr
259# define Perl_Idefgv_ptr dll_Perl_Idefgv_ptr
260# define Perl_Ierrgv_ptr dll_Perl_Ierrgv_ptr
261# define Perl_Isv_yes_ptr dll_Perl_Isv_yes_ptr
262# define boot_DynaLoader dll_boot_DynaLoader
Bram Moolenaare06c1882010-07-21 22:05:20 +0200263# define Perl_Gthr_key_ptr dll_Perl_Gthr_key_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000265# define Perl_sys_init dll_Perl_sys_init
Bram Moolenaarc236c162008-07-13 17:41:49 +0000266# define Perl_sys_term dll_Perl_sys_term
267# define Perl_ISv_ptr dll_Perl_ISv_ptr
268# define Perl_Istack_max_ptr dll_Perl_Istack_max_ptr
269# define Perl_Istack_base_ptr dll_Perl_Istack_base_ptr
270# define Perl_Itmps_ix_ptr dll_Perl_Itmps_ix_ptr
271# define Perl_Itmps_floor_ptr dll_Perl_Itmps_floor_ptr
272# define Perl_IXpv_ptr dll_Perl_IXpv_ptr
273# define Perl_Ina_ptr dll_Perl_Ina_ptr
274# define Perl_Imarkstack_ptr_ptr dll_Perl_Imarkstack_ptr_ptr
275# define Perl_Imarkstack_max_ptr dll_Perl_Imarkstack_max_ptr
276# define Perl_Istack_sp_ptr dll_Perl_Istack_sp_ptr
277# define Perl_Iop_ptr dll_Perl_Iop_ptr
278# define Perl_call_list dll_Perl_call_list
279# define Perl_Iscopestack_ix_ptr dll_Perl_Iscopestack_ix_ptr
280# define Perl_Iunitcheckav_ptr dll_Perl_Iunitcheckav_ptr
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200281# if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
282# define Perl_xs_handshake dll_Perl_xs_handshake
283# define Perl_xs_boot_epilog dll_Perl_xs_boot_epilog
284# endif
Bram Moolenaar01c10522012-09-21 12:50:51 +0200285# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100286# ifdef USE_ITHREADS
287# define PL_thr_key *dll_PL_thr_key
288# endif
Bram Moolenaar01c10522012-09-21 12:50:51 +0200289# endif
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100290# define Perl_hv_iternext_flags dll_Perl_hv_iternext_flags
291# define Perl_hv_iterinit dll_Perl_hv_iterinit
292# define Perl_hv_iterkey dll_Perl_hv_iterkey
293# define Perl_hv_iterval dll_Perl_hv_iterval
294# define Perl_av_fetch dll_Perl_av_fetch
295# define Perl_av_len dll_Perl_av_len
296# define Perl_sv_2nv_flags dll_Perl_sv_2nv_flags
Bram Moolenaarc236c162008-07-13 17:41:49 +0000297
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298/*
299 * Declare HANDLE for perl.dll and function pointers.
300 */
301static HANDLE hPerlLib = NULL;
302
303static PerlInterpreter* (*perl_alloc)();
304static void (*perl_construct)(PerlInterpreter*);
305static void (*perl_destruct)(PerlInterpreter*);
306static void (*perl_free)(PerlInterpreter*);
307static int (*perl_run)(PerlInterpreter*);
308static int (*perl_parse)(PerlInterpreter*, XSINIT_t, int, char**, char**);
309static void* (*Perl_get_context)(void);
Bram Moolenaara7ecc562006-08-16 16:17:39 +0000310static void (*Perl_croak)(pTHX_ const char*, ...);
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100311#ifdef PERL5101_OR_LATER
Bram Moolenaar6b107212013-12-11 15:06:40 +0100312/* Perl-5.18 has a different Perl_croak_xs_usage signature. */
313# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
314static void (*Perl_croak_xs_usage)(const CV *const, const char *const params);
315# else
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100316static void (*Perl_croak_xs_usage)(pTHX_ const CV *const, const char *const params);
Bram Moolenaar6b107212013-12-11 15:06:40 +0100317# endif
Bram Moolenaar9be6e212013-06-15 16:47:35 +0200318#endif
Bram Moolenaara7ecc562006-08-16 16:17:39 +0000319static void (*Perl_croak_nocontext)(const char*, ...);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320static I32 (*Perl_dowantarray)(pTHX);
321static void (*Perl_free_tmps)(pTHX);
322static HV* (*Perl_gv_stashpv)(pTHX_ const char*, I32);
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200323#if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
324static I32* (*Perl_markstack_grow)(pTHX);
325#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326static void (*Perl_markstack_grow)(pTHX);
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328static MAGIC* (*Perl_mg_find)(pTHX_ SV*, int);
329static CV* (*Perl_newXS)(pTHX_ char*, XSUBADDR_t, char*);
330static SV* (*Perl_newSV)(pTHX_ STRLEN);
331static SV* (*Perl_newSViv)(pTHX_ IV);
332static SV* (*Perl_newSVpv)(pTHX_ const char*, STRLEN);
333static I32 (*Perl_call_argv)(pTHX_ const char*, I32, char**);
334static I32 (*Perl_call_pv)(pTHX_ const char*, I32);
335static I32 (*Perl_eval_sv)(pTHX_ SV*, I32);
336static SV* (*Perl_get_sv)(pTHX_ const char*, I32);
337static SV* (*Perl_eval_pv)(pTHX_ const char*, I32);
338static SV* (*Perl_call_method)(pTHX_ const char*, I32);
339static void (*Perl_pop_scope)(pTHX);
340static void (*Perl_push_scope)(pTHX);
341static void (*Perl_save_int)(pTHX_ int*);
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200342#if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
343static void (*Perl_save_strlen)(pTHX_ STRLEN* ptr);
344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345static SV** (*Perl_stack_grow)(pTHX_ SV**, SV**p, int);
346static SV** (*Perl_set_context)(void*);
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200347#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
348static bool (*Perl_sv_2bool_flags)(pTHX_ SV*, I32);
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200349# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200350static void (*Perl_xs_apiversion_bootcheck)(pTHX_ SV *module, const char *api_p, STRLEN api_len);
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200351# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200352#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353static bool (*Perl_sv_2bool)(pTHX_ SV*);
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355static IV (*Perl_sv_2iv)(pTHX_ SV*);
356static SV* (*Perl_sv_2mortal)(pTHX_ SV*);
357#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
358static char* (*Perl_sv_2pv_flags)(pTHX_ SV*, STRLEN*, I32);
359static char* (*Perl_sv_2pv_nolen)(pTHX_ SV*);
360#else
361static char* (*Perl_sv_2pv)(pTHX_ SV*, STRLEN*);
362#endif
363static SV* (*Perl_sv_bless)(pTHX_ SV*, HV*);
364#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
365static void (*Perl_sv_catpvn_flags)(pTHX_ SV* , const char*, STRLEN, I32);
366#else
367static void (*Perl_sv_catpvn)(pTHX_ SV*, const char*, STRLEN);
368#endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000369#ifdef PERL589_OR_LATER
370static IV (*Perl_sv_2iv_flags)(pTHX_ SV* sv, I32 flags);
371static CV * (*Perl_newXS_flags)(pTHX_ const char *name, XSUBADDR_t subaddr, const char *const filename, const char *const proto, U32 flags);
372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373static void (*Perl_sv_free)(pTHX_ SV*);
374static int (*Perl_sv_isa)(pTHX_ SV*, const char*);
375static void (*Perl_sv_magic)(pTHX_ SV*, SV*, int, const char*, I32);
376static void (*Perl_sv_setiv)(pTHX_ SV*, IV);
377static void (*Perl_sv_setpv)(pTHX_ SV*, const char*);
378static void (*Perl_sv_setpvn)(pTHX_ SV*, const char*, STRLEN);
379#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
380static void (*Perl_sv_setsv_flags)(pTHX_ SV*, SV*, I32);
381#else
382static void (*Perl_sv_setsv)(pTHX_ SV*, SV*);
383#endif
384static bool (*Perl_sv_upgrade)(pTHX_ SV*, U32);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200385#if (PERL_REVISION == 5) && (PERL_VERSION < 10)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386static SV*** (*Perl_Tstack_sp_ptr)(register PerlInterpreter*);
387static OP** (*Perl_Top_ptr)(register PerlInterpreter*);
388static SV*** (*Perl_Tstack_base_ptr)(register PerlInterpreter*);
389static SV*** (*Perl_Tstack_max_ptr)(register PerlInterpreter*);
390static I32* (*Perl_Ttmps_ix_ptr)(register PerlInterpreter*);
391static I32* (*Perl_Ttmps_floor_ptr)(register PerlInterpreter*);
392static I32** (*Perl_Tmarkstack_ptr_ptr)(register PerlInterpreter*);
393static I32** (*Perl_Tmarkstack_max_ptr)(register PerlInterpreter*);
394static SV** (*Perl_TSv_ptr)(register PerlInterpreter*);
395static XPV** (*Perl_TXpv_ptr)(register PerlInterpreter*);
396static STRLEN* (*Perl_Tna_ptr)(register PerlInterpreter*);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200397#else
Bram Moolenaar6b107212013-12-11 15:06:40 +0100398/* Perl-5.18 has a different Perl_sv_free2 signature. */
399# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
400static void (*Perl_sv_free2)(pTHX_ SV*, const U32);
401# else
Bram Moolenaarccf22172008-09-01 15:56:45 +0000402static void (*Perl_sv_free2)(pTHX_ SV*);
Bram Moolenaar6b107212013-12-11 15:06:40 +0100403# endif
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000404static void (*Perl_sys_init)(int* argc, char*** argv);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000405static void (*Perl_sys_term)(void);
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200406static void (*Perl_call_list)(pTHX_ I32, AV*);
407# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
408# else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000409static SV** (*Perl_ISv_ptr)(register PerlInterpreter*);
410static SV*** (*Perl_Istack_max_ptr)(register PerlInterpreter*);
411static SV*** (*Perl_Istack_base_ptr)(register PerlInterpreter*);
412static XPV** (*Perl_IXpv_ptr)(register PerlInterpreter*);
413static I32* (*Perl_Itmps_ix_ptr)(register PerlInterpreter*);
414static I32* (*Perl_Itmps_floor_ptr)(register PerlInterpreter*);
415static STRLEN* (*Perl_Ina_ptr)(register PerlInterpreter*);
416static I32** (*Perl_Imarkstack_ptr_ptr)(register PerlInterpreter*);
417static I32** (*Perl_Imarkstack_max_ptr)(register PerlInterpreter*);
418static SV*** (*Perl_Istack_sp_ptr)(register PerlInterpreter*);
419static OP** (*Perl_Iop_ptr)(register PerlInterpreter*);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000420static I32* (*Perl_Iscopestack_ix_ptr)(register PerlInterpreter*);
421static AV** (*Perl_Iunitcheckav_ptr)(register PerlInterpreter*);
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200422# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000423#endif
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200424#if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
425static I32 (*Perl_xs_handshake)(const U32, void *, const char *, ...);
426static void (*Perl_xs_boot_epilog)(pTHX_ const U32);
427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200429#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100430# ifdef USE_ITHREADS
Bram Moolenaar01c10522012-09-21 12:50:51 +0200431static perl_key* dll_PL_thr_key;
Bram Moolenaard8619992014-03-12 17:08:05 +0100432# endif
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200433#else
Bram Moolenaare06c1882010-07-21 22:05:20 +0200434static GV** (*Perl_Idefgv_ptr)(register PerlInterpreter*);
435static GV** (*Perl_Ierrgv_ptr)(register PerlInterpreter*);
436static SV* (*Perl_Isv_yes_ptr)(register PerlInterpreter*);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200437static perl_key* (*Perl_Gthr_key_ptr)_((pTHX));
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200438#endif
439static void (*boot_DynaLoader)_((pTHX_ CV*));
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100440static HE * (*Perl_hv_iternext_flags)(pTHX_ HV *, I32);
441static I32 (*Perl_hv_iterinit)(pTHX_ HV *);
442static char * (*Perl_hv_iterkey)(pTHX_ HE *, I32 *);
443static SV * (*Perl_hv_iterval)(pTHX_ HV *, HE *);
444static SV** (*Perl_av_fetch)(pTHX_ AV *, SSize_t, I32);
445static SSize_t (*Perl_av_len)(pTHX_ AV *);
446static NV (*Perl_sv_2nv_flags)(pTHX_ SV *const, const I32);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200447
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448/*
449 * Table of name to function pointer of perl.
450 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451static struct {
452 char* name;
453 PERL_PROC* ptr;
454} perl_funcname_table[] = {
455 {"perl_alloc", (PERL_PROC*)&perl_alloc},
456 {"perl_construct", (PERL_PROC*)&perl_construct},
457 {"perl_destruct", (PERL_PROC*)&perl_destruct},
458 {"perl_free", (PERL_PROC*)&perl_free},
459 {"perl_run", (PERL_PROC*)&perl_run},
460 {"perl_parse", (PERL_PROC*)&perl_parse},
461 {"Perl_get_context", (PERL_PROC*)&Perl_get_context},
462 {"Perl_croak", (PERL_PROC*)&Perl_croak},
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100463#ifdef PERL5101_OR_LATER
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100464 {"Perl_croak_xs_usage", (PERL_PROC*)&Perl_croak_xs_usage},
465#endif
Bram Moolenaard8619992014-03-12 17:08:05 +0100466#ifdef PERL_IMPLICIT_CONTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 {"Perl_croak_nocontext", (PERL_PROC*)&Perl_croak_nocontext},
Bram Moolenaard8619992014-03-12 17:08:05 +0100468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 {"Perl_dowantarray", (PERL_PROC*)&Perl_dowantarray},
470 {"Perl_free_tmps", (PERL_PROC*)&Perl_free_tmps},
471 {"Perl_gv_stashpv", (PERL_PROC*)&Perl_gv_stashpv},
472 {"Perl_markstack_grow", (PERL_PROC*)&Perl_markstack_grow},
473 {"Perl_mg_find", (PERL_PROC*)&Perl_mg_find},
474 {"Perl_newXS", (PERL_PROC*)&Perl_newXS},
475 {"Perl_newSV", (PERL_PROC*)&Perl_newSV},
476 {"Perl_newSViv", (PERL_PROC*)&Perl_newSViv},
477 {"Perl_newSVpv", (PERL_PROC*)&Perl_newSVpv},
478 {"Perl_call_argv", (PERL_PROC*)&Perl_call_argv},
479 {"Perl_call_pv", (PERL_PROC*)&Perl_call_pv},
480 {"Perl_eval_sv", (PERL_PROC*)&Perl_eval_sv},
481 {"Perl_get_sv", (PERL_PROC*)&Perl_get_sv},
482 {"Perl_eval_pv", (PERL_PROC*)&Perl_eval_pv},
483 {"Perl_call_method", (PERL_PROC*)&Perl_call_method},
484 {"Perl_pop_scope", (PERL_PROC*)&Perl_pop_scope},
485 {"Perl_push_scope", (PERL_PROC*)&Perl_push_scope},
486 {"Perl_save_int", (PERL_PROC*)&Perl_save_int},
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200487#if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
488 {"Perl_save_strlen", (PERL_PROC*)&Perl_save_strlen},
489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 {"Perl_stack_grow", (PERL_PROC*)&Perl_stack_grow},
491 {"Perl_set_context", (PERL_PROC*)&Perl_set_context},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200492#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
493 {"Perl_sv_2bool_flags", (PERL_PROC*)&Perl_sv_2bool_flags},
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200494# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200495 {"Perl_xs_apiversion_bootcheck",(PERL_PROC*)&Perl_xs_apiversion_bootcheck},
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200496# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200497#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 {"Perl_sv_2bool", (PERL_PROC*)&Perl_sv_2bool},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 {"Perl_sv_2iv", (PERL_PROC*)&Perl_sv_2iv},
501 {"Perl_sv_2mortal", (PERL_PROC*)&Perl_sv_2mortal},
502#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
503 {"Perl_sv_2pv_flags", (PERL_PROC*)&Perl_sv_2pv_flags},
504 {"Perl_sv_2pv_nolen", (PERL_PROC*)&Perl_sv_2pv_nolen},
505#else
506 {"Perl_sv_2pv", (PERL_PROC*)&Perl_sv_2pv},
507#endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000508#ifdef PERL589_OR_LATER
509 {"Perl_sv_2iv_flags", (PERL_PROC*)&Perl_sv_2iv_flags},
510 {"Perl_newXS_flags", (PERL_PROC*)&Perl_newXS_flags},
511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 {"Perl_sv_bless", (PERL_PROC*)&Perl_sv_bless},
513#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
514 {"Perl_sv_catpvn_flags", (PERL_PROC*)&Perl_sv_catpvn_flags},
515#else
516 {"Perl_sv_catpvn", (PERL_PROC*)&Perl_sv_catpvn},
517#endif
518 {"Perl_sv_free", (PERL_PROC*)&Perl_sv_free},
519 {"Perl_sv_isa", (PERL_PROC*)&Perl_sv_isa},
520 {"Perl_sv_magic", (PERL_PROC*)&Perl_sv_magic},
521 {"Perl_sv_setiv", (PERL_PROC*)&Perl_sv_setiv},
522 {"Perl_sv_setpv", (PERL_PROC*)&Perl_sv_setpv},
523 {"Perl_sv_setpvn", (PERL_PROC*)&Perl_sv_setpvn},
524#if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
525 {"Perl_sv_setsv_flags", (PERL_PROC*)&Perl_sv_setsv_flags},
526#else
527 {"Perl_sv_setsv", (PERL_PROC*)&Perl_sv_setsv},
528#endif
529 {"Perl_sv_upgrade", (PERL_PROC*)&Perl_sv_upgrade},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000530#if (PERL_REVISION == 5) && (PERL_VERSION < 10)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 {"Perl_Tstack_sp_ptr", (PERL_PROC*)&Perl_Tstack_sp_ptr},
532 {"Perl_Top_ptr", (PERL_PROC*)&Perl_Top_ptr},
533 {"Perl_Tstack_base_ptr", (PERL_PROC*)&Perl_Tstack_base_ptr},
534 {"Perl_Tstack_max_ptr", (PERL_PROC*)&Perl_Tstack_max_ptr},
535 {"Perl_Ttmps_ix_ptr", (PERL_PROC*)&Perl_Ttmps_ix_ptr},
536 {"Perl_Ttmps_floor_ptr", (PERL_PROC*)&Perl_Ttmps_floor_ptr},
537 {"Perl_Tmarkstack_ptr_ptr", (PERL_PROC*)&Perl_Tmarkstack_ptr_ptr},
538 {"Perl_Tmarkstack_max_ptr", (PERL_PROC*)&Perl_Tmarkstack_max_ptr},
539 {"Perl_TSv_ptr", (PERL_PROC*)&Perl_TSv_ptr},
540 {"Perl_TXpv_ptr", (PERL_PROC*)&Perl_TXpv_ptr},
541 {"Perl_Tna_ptr", (PERL_PROC*)&Perl_Tna_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000542#else
Bram Moolenaarccf22172008-09-01 15:56:45 +0000543 {"Perl_sv_free2", (PERL_PROC*)&Perl_sv_free2},
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000544 {"Perl_sys_init", (PERL_PROC*)&Perl_sys_init},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000545 {"Perl_sys_term", (PERL_PROC*)&Perl_sys_term},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200546 {"Perl_call_list", (PERL_PROC*)&Perl_call_list},
547# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
548# else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000549 {"Perl_ISv_ptr", (PERL_PROC*)&Perl_ISv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000550 {"Perl_Istack_max_ptr", (PERL_PROC*)&Perl_Istack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200551 {"Perl_Istack_base_ptr", (PERL_PROC*)&Perl_Istack_base_ptr},
552 {"Perl_IXpv_ptr", (PERL_PROC*)&Perl_IXpv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000553 {"Perl_Itmps_ix_ptr", (PERL_PROC*)&Perl_Itmps_ix_ptr},
554 {"Perl_Itmps_floor_ptr", (PERL_PROC*)&Perl_Itmps_floor_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200555 {"Perl_Ina_ptr", (PERL_PROC*)&Perl_Ina_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000556 {"Perl_Imarkstack_ptr_ptr", (PERL_PROC*)&Perl_Imarkstack_ptr_ptr},
557 {"Perl_Imarkstack_max_ptr", (PERL_PROC*)&Perl_Imarkstack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200558 {"Perl_Istack_sp_ptr", (PERL_PROC*)&Perl_Istack_sp_ptr},
559 {"Perl_Iop_ptr", (PERL_PROC*)&Perl_Iop_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000560 {"Perl_Iscopestack_ix_ptr", (PERL_PROC*)&Perl_Iscopestack_ix_ptr},
561 {"Perl_Iunitcheckav_ptr", (PERL_PROC*)&Perl_Iunitcheckav_ptr},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200562# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000563#endif
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200564#if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
565 {"Perl_xs_handshake", (PERL_PROC*)&Perl_xs_handshake},
566 {"Perl_xs_boot_epilog", (PERL_PROC*)&Perl_xs_boot_epilog},
567#endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200568#if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100569# ifdef USE_ITHREADS
Bram Moolenaar01c10522012-09-21 12:50:51 +0200570 {"PL_thr_key", (PERL_PROC*)&dll_PL_thr_key},
Bram Moolenaard8619992014-03-12 17:08:05 +0100571# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200572#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 {"Perl_Idefgv_ptr", (PERL_PROC*)&Perl_Idefgv_ptr},
574 {"Perl_Ierrgv_ptr", (PERL_PROC*)&Perl_Ierrgv_ptr},
575 {"Perl_Isv_yes_ptr", (PERL_PROC*)&Perl_Isv_yes_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200576 {"Perl_Gthr_key_ptr", (PERL_PROC*)&Perl_Gthr_key_ptr},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200577#endif
578 {"boot_DynaLoader", (PERL_PROC*)&boot_DynaLoader},
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100579 {"Perl_hv_iternext_flags", (PERL_PROC*)&Perl_hv_iternext_flags},
580 {"Perl_hv_iterinit", (PERL_PROC*)&Perl_hv_iterinit},
581 {"Perl_hv_iterkey", (PERL_PROC*)&Perl_hv_iterkey},
582 {"Perl_hv_iterval", (PERL_PROC*)&Perl_hv_iterval},
583 {"Perl_av_fetch", (PERL_PROC*)&Perl_av_fetch},
584 {"Perl_av_len", (PERL_PROC*)&Perl_av_len},
585 {"Perl_sv_2nv_flags", (PERL_PROC*)&Perl_sv_2nv_flags},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 {"", NULL},
587};
588
Bram Moolenaar6b107212013-12-11 15:06:40 +0100589/* Work around for perl-5.18.
590 * The definitions of S_SvREFCNT_inc and S_SvREFCNT_dec are needed, so include
591 * "perl\lib\CORE\inline.h", after Perl_sv_free2 is defined.
592 * The linker won't complain about undefined __impl_Perl_sv_free2. */
593#if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
594# include <inline.h>
595#endif
596
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597/*
598 * Make all runtime-links of perl.
599 *
Bram Moolenaar364ab2f2013-08-02 20:05:32 +0200600 * 1. Get module handle using dlopen() or vimLoadLib().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 * 2. Get pointer to perl function by GetProcAddress.
602 * 3. Repeat 2, until get all functions will be used.
603 *
604 * Parameter 'libname' provides name of DLL.
605 * Return OK or FAIL.
606 */
607 static int
608perl_runtime_link_init(char *libname, int verbose)
609{
610 int i;
611
612 if (hPerlLib != NULL)
613 return OK;
Bram Moolenaare06c1882010-07-21 22:05:20 +0200614 if ((hPerlLib = load_dll(libname)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 {
616 if (verbose)
617 EMSG2(_("E370: Could not load library %s"), libname);
618 return FAIL;
619 }
620 for (i = 0; perl_funcname_table[i].ptr; ++i)
621 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200622 if (!(*perl_funcname_table[i].ptr = symbol_from_dll(hPerlLib,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 perl_funcname_table[i].name)))
624 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200625 close_dll(hPerlLib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 hPerlLib = NULL;
627 if (verbose)
628 EMSG2(_(e_loadfunc), perl_funcname_table[i].name);
629 return FAIL;
630 }
631 }
632 return OK;
633}
634
635/*
636 * If runtime-link-perl(DLL) was loaded successfully, return TRUE.
637 * There were no DLL loaded, return FALSE.
638 */
639 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100640perl_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100642 return perl_runtime_link_init((char *)p_perldll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643}
644#endif /* DYNAMIC_PERL */
645
646/*
647 * perl_init(): initialize perl interpreter
648 * We have to call perl_parse to initialize some structures,
649 * there's nothing to actually parse.
650 */
651 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100652perl_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653{
Bram Moolenaarc236c162008-07-13 17:41:49 +0000654 char *bootargs[] = { "VI", NULL };
655 int argc = 3;
656 static char *argv[] = { "", "-e", "" };
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657
Bram Moolenaarc236c162008-07-13 17:41:49 +0000658#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000659 Perl_sys_init(&argc, (char***)&argv);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 perl_interp = perl_alloc();
662 perl_construct(perl_interp);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000663 perl_parse(perl_interp, xs_init, argc, argv, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 perl_call_argv("VIM::bootstrap", (long)G_DISCARD, bootargs);
665 VIM_init();
666#ifdef USE_SFIO
667 sfdisc(PerlIO_stdout(), sfdcnewvim());
668 sfdisc(PerlIO_stderr(), sfdcnewvim());
669 sfsetbuf(PerlIO_stdout(), NULL, 0);
670 sfsetbuf(PerlIO_stderr(), NULL, 0);
671#endif
672}
673
674/*
675 * perl_end(): clean up after ourselves
676 */
677 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100678perl_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679{
680 if (perl_interp)
681 {
682 perl_run(perl_interp);
683 perl_destruct(perl_interp);
684 perl_free(perl_interp);
685 perl_interp = NULL;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000686#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100687 Perl_sys_term();
Bram Moolenaarc236c162008-07-13 17:41:49 +0000688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 }
690#ifdef DYNAMIC_PERL
691 if (hPerlLib)
692 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200693 close_dll(hPerlLib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 hPerlLib = NULL;
695 }
696#endif
697}
698
699/*
700 * msg_split(): send a message to the message handling routines
701 * split at '\n' first though.
702 */
703 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100704msg_split(
705 char_u *s,
706 int attr) /* highlighting attributes */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707{
708 char *next;
709 char *token = (char *)s;
710
Bram Moolenaaraa8494a2007-10-09 08:47:27 +0000711 while ((next = strchr(token, '\n')) && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 {
713 *next++ = '\0'; /* replace \n with \0 */
714 msg_attr((char_u *)token, attr);
715 token = next;
716 }
Bram Moolenaaraa8494a2007-10-09 08:47:27 +0000717 if (*token && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 msg_attr((char_u *)token, attr);
719}
720
721#ifndef FEAT_EVAL
722/*
723 * This stub is needed because an "#ifdef FEAT_EVAL" around Eval() doesn't
724 * work properly.
725 */
726 char_u *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100727eval_to_string(
728 char_u *arg UNUSED,
729 char_u **nextcmd UNUSED,
730 int dolist UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731{
732 return NULL;
733}
734#endif
735
736/*
737 * Create a new reference to an SV pointing to the SCR structure
Bram Moolenaare344bea2005-09-01 20:46:49 +0000738 * The b_perl_private/w_perl_private part of the SCR structure points to the
739 * SV, so there can only be one such SV for a particular SCR structure. When
740 * the last reference has gone (DESTROY is called),
741 * b_perl_private/w_perl_private is reset; When the screen goes away before
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 * all references are gone, the value of the SV is reset;
743 * any subsequent use of any of those reference will produce
744 * a warning. (see typemap)
745 */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000746
747 static SV *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100748newWINrv(SV *rv, win_T *ptr)
Bram Moolenaare344bea2005-09-01 20:46:49 +0000749{
750 sv_upgrade(rv, SVt_RV);
751 if (ptr->w_perl_private == NULL)
752 {
753 ptr->w_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +0100754 sv_setiv(ptr->w_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +0000755 }
756 else
757 SvREFCNT_inc(ptr->w_perl_private);
758 SvRV(rv) = ptr->w_perl_private;
759 SvROK_on(rv);
760 return sv_bless(rv, gv_stashpv("VIWIN", TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761}
762
Bram Moolenaare344bea2005-09-01 20:46:49 +0000763 static SV *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100764newBUFrv(SV *rv, buf_T *ptr)
Bram Moolenaare344bea2005-09-01 20:46:49 +0000765{
766 sv_upgrade(rv, SVt_RV);
767 if (ptr->b_perl_private == NULL)
768 {
769 ptr->b_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +0100770 sv_setiv(ptr->b_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +0000771 }
772 else
773 SvREFCNT_inc(ptr->b_perl_private);
774 SvRV(rv) = ptr->b_perl_private;
775 SvROK_on(rv);
776 return sv_bless(rv, gv_stashpv("VIBUF", TRUE));
777}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778
779/*
780 * perl_win_free
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200781 * Remove all references to the window to be destroyed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 */
783 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100784perl_win_free(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000786 if (wp->w_perl_private)
787 sv_setiv((SV *)wp->w_perl_private, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 return;
789}
790
791 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100792perl_buf_free(buf_T *bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000794 if (bp->b_perl_private)
795 sv_setiv((SV *)bp->b_perl_private, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 return;
797}
798
799#ifndef PROTO
800# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
801I32 cur_val(pTHX_ IV iv, SV *sv);
802# else
803I32 cur_val(IV iv, SV *sv);
804#endif
805
806/*
807 * Handler for the magic variables $main::curwin and $main::curbuf.
808 * The handler is put into the magic vtbl for these variables.
809 * (This is effectively a C-level equivalent of a tied variable).
810 * There is no "set" function as the variables are read-only.
811 */
812# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
813I32 cur_val(pTHX_ IV iv, SV *sv)
814# else
815I32 cur_val(IV iv, SV *sv)
816# endif
817{
818 SV *rv;
819 if (iv == 0)
820 rv = newWINrv(newSV(0), curwin);
821 else
822 rv = newBUFrv(newSV(0), curbuf);
823 sv_setsv(sv, rv);
824 return 0;
825}
826#endif /* !PROTO */
827
828struct ufuncs cw_funcs = { cur_val, 0, 0 };
829struct ufuncs cb_funcs = { cur_val, 0, 1 };
830
831/*
832 * VIM_init(): Vim-specific initialisation.
833 * Make the magical main::curwin and main::curbuf variables
834 */
835 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100836VIM_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837{
838 static char cw[] = "main::curwin";
839 static char cb[] = "main::curbuf";
840 SV *sv;
841
842 sv = perl_get_sv(cw, TRUE);
843 sv_magic(sv, NULL, 'U', (char *)&cw_funcs, sizeof(cw_funcs));
844 SvREADONLY_on(sv);
845
846 sv = perl_get_sv(cb, TRUE);
847 sv_magic(sv, NULL, 'U', (char *)&cb_funcs, sizeof(cb_funcs));
848 SvREADONLY_on(sv);
849
850 /*
851 * Setup the Safe compartment.
852 * It shouldn't be a fatal error if the Safe module is missing.
853 * XXX: Only shares the 'Msg' routine (which has to be called
854 * like 'Msg(...)').
855 */
856 (void)perl_eval_pv( "if ( eval( 'require Safe' ) ) { $VIM::safe = Safe->new(); $VIM::safe->share_from( 'VIM', ['Msg'] ); }", G_DISCARD | G_VOID );
857
858}
859
860#ifdef DYNAMIC_PERL
861static char *e_noperl = N_("Sorry, this command is disabled: the Perl library could not be loaded.");
862#endif
863
864/*
865 * ":perl"
866 */
867 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100868ex_perl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869{
870 char *err;
871 char *script;
872 STRLEN length;
873 SV *sv;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200874#ifdef HAVE_SANDBOX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 SV *safe;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877
878 script = (char *)script_get(eap, eap->arg);
879 if (eap->skip)
880 {
881 vim_free(script);
882 return;
883 }
884
885 if (perl_interp == NULL)
886 {
887#ifdef DYNAMIC_PERL
888 if (!perl_enabled(TRUE))
889 {
890 EMSG(_(e_noperl));
891 vim_free(script);
892 return;
893 }
894#endif
895 perl_init();
896 }
897
898 {
899 dSP;
900 ENTER;
901 SAVETMPS;
902
903 if (script == NULL)
904 sv = newSVpv((char *)eap->arg, 0);
905 else
906 {
907 sv = newSVpv(script, 0);
908 vim_free(script);
909 }
910
911#ifdef HAVE_SANDBOX
912 if (sandbox)
913 {
Bram Moolenaara1711622011-07-27 14:15:46 +0200914 safe = perl_get_sv("VIM::safe", FALSE);
Bram Moolenaar3f947ea2009-07-14 14:04:54 +0000915# ifndef MAKE_TEST /* avoid a warning for unreachable code */
Bram Moolenaar954e8c52009-11-11 13:45:33 +0000916 if (safe == NULL || !SvTRUE(safe))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 EMSG(_("E299: Perl evaluation forbidden in sandbox without the Safe module"));
918 else
Bram Moolenaar3f947ea2009-07-14 14:04:54 +0000919# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 {
921 PUSHMARK(SP);
922 XPUSHs(safe);
923 XPUSHs(sv);
924 PUTBACK;
925 perl_call_method("reval", G_DISCARD);
926 }
927 }
928 else
929#endif
930 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
931
932 SvREFCNT_dec(sv);
933
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100934 err = CHECK_EVAL_ERR(length);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935
936 FREETMPS;
937 LEAVE;
938
939 if (!length)
940 return;
941
942 msg_split((char_u *)err, highlight_attr[HLF_E]);
943 return;
944 }
945}
946
947 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100948replace_line(linenr_T *line, linenr_T *end)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949{
950 char *str;
951
952 if (SvOK(GvSV(PL_defgv)))
953 {
954 str = SvPV(GvSV(PL_defgv), PL_na);
955 ml_replace(*line, (char_u *)str, 1);
956 changed_bytes(*line, 0);
957 }
958 else
959 {
960 ml_delete(*line, FALSE);
961 deleted_lines_mark(*line, 1L);
962 --(*end);
963 --(*line);
964 }
965 return OK;
966}
967
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100968static struct ref_map_S {
969 void *vim_ref;
970 SV *perl_ref;
971 struct ref_map_S *next;
972} *ref_map = NULL;
973
974 static void
975ref_map_free(void)
976{
977 struct ref_map_S *tofree;
978 struct ref_map_S *refs = ref_map;
979
980 while (refs) {
981 tofree = refs;
982 refs = refs->next;
983 vim_free(tofree);
984 }
985 ref_map = NULL;
986}
987
988 static struct ref_map_S *
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100989ref_map_find_SV(SV *const sv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100990{
991 struct ref_map_S *refs = ref_map;
992 int count = 350;
993
994 while (refs) {
995 if (refs->perl_ref == sv)
996 break;
997 refs = refs->next;
998 count--;
999 }
1000
1001 if (!refs && count > 0) {
1002 refs = (struct ref_map_S *)alloc(sizeof(struct ref_map_S));
1003 if (!refs)
1004 return NULL;
1005 refs->perl_ref = sv;
1006 refs->vim_ref = NULL;
1007 refs->next = ref_map;
1008 ref_map = refs;
1009 }
1010
1011 return refs;
1012}
1013
1014 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001015perl_to_vim(SV *sv, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001016{
1017 if (SvROK(sv))
1018 sv = SvRV(sv);
1019
1020 switch (SvTYPE(sv)) {
1021 case SVt_NULL:
1022 break;
1023 case SVt_NV: /* float */
1024#ifdef FEAT_FLOAT
1025 rettv->v_type = VAR_FLOAT;
1026 rettv->vval.v_float = SvNV(sv);
1027 break;
1028#endif
1029 case SVt_IV: /* integer */
1030 if (!SvROK(sv)) { /* references should be string */
1031 rettv->vval.v_number = SvIV(sv);
1032 break;
1033 }
1034 case SVt_PV: /* string */
1035 {
1036 size_t len = 0;
1037 char * str_from = SvPV(sv, len);
1038 char_u *str_to = (char_u*)alloc(sizeof(char_u) * (len + 1));
1039
1040 if (str_to) {
1041 str_to[len] = '\0';
1042
1043 while (len--) {
1044 if (str_from[len] == '\0')
1045 str_to[len] = '\n';
1046 else
1047 str_to[len] = str_from[len];
1048 }
1049 }
1050
1051 rettv->v_type = VAR_STRING;
1052 rettv->vval.v_string = str_to;
1053 break;
1054 }
1055 case SVt_PVAV: /* list */
1056 {
1057 SSize_t size;
1058 listitem_T * item;
1059 SV ** item2;
1060 list_T * list;
1061 struct ref_map_S * refs;
1062
1063 if ((refs = ref_map_find_SV(sv)) == NULL)
1064 return FAIL;
1065
1066 if (refs->vim_ref)
1067 list = (list_T *) refs->vim_ref;
1068 else
1069 {
1070 if ((list = list_alloc()) == NULL)
1071 return FAIL;
1072 refs->vim_ref = list;
1073
1074 for (size = av_len((AV*)sv); size >= 0; size--)
1075 {
1076 if ((item = listitem_alloc()) == NULL)
1077 break;
1078
1079 item->li_tv.v_type = VAR_NUMBER;
1080 item->li_tv.v_lock = 0;
1081 item->li_tv.vval.v_number = 0;
1082 list_insert(list, item, list->lv_first);
1083
1084 item2 = av_fetch((AV *)sv, size, 0);
1085
1086 if (item2 == NULL || *item2 == NULL ||
Bram Moolenaard99df422016-01-29 23:20:40 +01001087 perl_to_vim(*item2, &item->li_tv) == FAIL)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001088 break;
1089 }
1090 }
1091
1092 list->lv_refcount++;
1093 rettv->v_type = VAR_LIST;
1094 rettv->vval.v_list = list;
1095 break;
1096 }
1097 case SVt_PVHV: /* dictionary */
1098 {
1099 HE * entry;
1100 size_t key_len;
1101 char * key;
1102 dictitem_T * item;
1103 SV * item2;
1104 dict_T * dict;
1105 struct ref_map_S * refs;
1106
1107 if ((refs = ref_map_find_SV(sv)) == NULL)
1108 return FAIL;
1109
1110 if (refs->vim_ref)
1111 dict = (dict_T *) refs->vim_ref;
1112 else
1113 {
1114
1115 if ((dict = dict_alloc()) == NULL)
1116 return FAIL;
1117 refs->vim_ref = dict;
1118
1119 hv_iterinit((HV *)sv);
1120
1121 for (entry = hv_iternext((HV *)sv); entry; entry = hv_iternext((HV *)sv))
1122 {
1123 key_len = 0;
1124 key = hv_iterkey(entry, (I32 *)&key_len);
1125
1126 if (!key || !key_len || strlen(key) < key_len) {
1127 EMSG2("Malformed key Dictionary '%s'", key && *key ? key : "(empty)");
1128 break;
1129 }
1130
1131 if ((item = dictitem_alloc((char_u *)key)) == NULL)
1132 break;
1133
1134 item->di_tv.v_type = VAR_NUMBER;
1135 item->di_tv.v_lock = 0;
1136 item->di_tv.vval.v_number = 0;
1137
1138 if (dict_add(dict, item) == FAIL) {
1139 dictitem_free(item);
1140 break;
1141 }
1142 item2 = hv_iterval((HV *)sv, entry);
1143 if (item2 == NULL || perl_to_vim(item2, &item->di_tv) == FAIL)
1144 break;
1145 }
1146 }
1147
1148 dict->dv_refcount++;
1149 rettv->v_type = VAR_DICT;
1150 rettv->vval.v_dict = dict;
1151 break;
1152 }
1153 default: /* not convertible */
1154 {
1155 char *val = SvPV_nolen(sv);
1156 rettv->v_type = VAR_STRING;
1157 rettv->vval.v_string = val ? vim_strsave((char_u *)val) : NULL;
1158 break;
1159 }
1160 }
1161 return OK;
1162}
1163
1164/*
1165 * "perleval()"
1166 */
1167 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001168do_perleval(char_u *str, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001169{
1170 char *err = NULL;
1171 STRLEN err_len = 0;
1172 SV *sv = NULL;
1173#ifdef HAVE_SANDBOX
1174 SV *safe;
1175#endif
1176
1177 if (perl_interp == NULL)
1178 {
1179#ifdef DYNAMIC_PERL
1180 if (!perl_enabled(TRUE))
1181 {
1182 EMSG(_(e_noperl));
1183 return;
1184 }
1185#endif
1186 perl_init();
1187 }
1188
1189 {
1190 dSP;
1191 ENTER;
1192 SAVETMPS;
1193
1194#ifdef HAVE_SANDBOX
1195 if (sandbox)
1196 {
1197 safe = get_sv("VIM::safe", FALSE);
1198# ifndef MAKE_TEST /* avoid a warning for unreachable code */
1199 if (safe == NULL || !SvTRUE(safe))
1200 EMSG(_("E299: Perl evaluation forbidden in sandbox without the Safe module"));
1201 else
1202# endif
1203 {
1204 sv = newSVpv((char *)str, 0);
1205 PUSHMARK(SP);
1206 XPUSHs(safe);
1207 XPUSHs(sv);
1208 PUTBACK;
1209 call_method("reval", G_SCALAR);
1210 SPAGAIN;
1211 SvREFCNT_dec(sv);
1212 sv = POPs;
1213 }
1214 }
1215 else
1216#endif /* HAVE_SANDBOX */
1217 sv = eval_pv((char *)str, 0);
1218
1219 if (sv) {
1220 perl_to_vim(sv, rettv);
1221 ref_map_free();
1222 err = CHECK_EVAL_ERR(err_len);
1223 }
1224 PUTBACK;
1225 FREETMPS;
1226 LEAVE;
1227 }
1228 if (err_len)
1229 msg_split((char_u *)err, highlight_attr[HLF_E]);
1230}
1231
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232/*
1233 * ":perldo".
1234 */
1235 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001236ex_perldo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237{
1238 STRLEN length;
1239 SV *sv;
1240 char *str;
1241 linenr_T i;
1242
1243 if (bufempty())
1244 return;
1245
1246 if (perl_interp == NULL)
1247 {
1248#ifdef DYNAMIC_PERL
1249 if (!perl_enabled(TRUE))
1250 {
1251 EMSG(_(e_noperl));
1252 return;
1253 }
1254#endif
1255 perl_init();
1256 }
1257 {
1258 dSP;
1259 length = strlen((char *)eap->arg);
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001260 sv = newSV(length + sizeof("sub VIM::perldo {") - 1 + 1);
1261 sv_setpvn(sv, "sub VIM::perldo {", sizeof("sub VIM::perldo {") - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 sv_catpvn(sv, (char *)eap->arg, length);
1263 sv_catpvn(sv, "}", 1);
1264 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
1265 SvREFCNT_dec(sv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001266 str = CHECK_EVAL_ERR(length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 if (length)
1268 goto err;
1269
1270 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
1271 return;
1272
1273 ENTER;
1274 SAVETMPS;
1275 for (i = eap->line1; i <= eap->line2; i++)
1276 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001277 sv_setpv(GvSV(PL_defgv), (char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 PUSHMARK(sp);
1279 perl_call_pv("VIM::perldo", G_SCALAR | G_EVAL);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001280 str = CHECK_EVAL_ERR(length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 if (length)
1282 break;
1283 SPAGAIN;
1284 if (SvTRUEx(POPs))
1285 {
1286 if (replace_line(&i, &eap->line2) != OK)
1287 {
1288 PUTBACK;
1289 break;
1290 }
1291 }
1292 PUTBACK;
1293 }
1294 FREETMPS;
1295 LEAVE;
1296 check_cursor();
1297 update_screen(NOT_VALID);
1298 if (!length)
1299 return;
1300
1301err:
1302 msg_split((char_u *)str, highlight_attr[HLF_E]);
1303 return;
1304 }
1305}
1306
Bram Moolenaar01dd60c2008-07-24 14:24:48 +00001307#ifndef FEAT_WINDOWS
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001308 int
1309win_valid(win_T *w)
1310{
1311 return TRUE;
1312}
1313 int
1314win_count(void)
1315{
1316 return 1;
1317}
1318 win_T *
1319win_find_nr(int n)
1320{
1321 return curwin;
1322}
Bram Moolenaar01dd60c2008-07-24 14:24:48 +00001323#endif
1324
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325XS(boot_VIM);
1326
1327 static void
1328xs_init(pTHX)
1329{
1330 char *file = __FILE__;
1331
1332 /* DynaLoader is a special case */
1333 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1334 newXS("VIM::bootstrap", boot_VIM, file);
1335}
1336
1337typedef win_T * VIWIN;
1338typedef buf_T * VIBUF;
1339
1340MODULE = VIM PACKAGE = VIM
1341
1342void
1343Msg(text, hl=NULL)
1344 char *text;
1345 char *hl;
1346
1347 PREINIT:
1348 int attr;
1349 int id;
1350
1351 PPCODE:
1352 if (text != NULL)
1353 {
1354 attr = 0;
1355 if (hl != NULL)
1356 {
1357 id = syn_name2id((char_u *)hl);
1358 if (id != 0)
1359 attr = syn_id2attr(id);
1360 }
1361 msg_split((char_u *)text, attr);
1362 }
1363
1364void
1365SetOption(line)
1366 char *line;
1367
1368 PPCODE:
1369 if (line != NULL)
1370 do_set((char_u *)line, 0);
1371 update_screen(NOT_VALID);
1372
1373void
1374DoCommand(line)
1375 char *line;
1376
1377 PPCODE:
1378 if (line != NULL)
1379 do_cmdline_cmd((char_u *)line);
1380
1381void
1382Eval(str)
1383 char *str;
1384
1385 PREINIT:
1386 char_u *value;
1387 PPCODE:
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001388 value = eval_to_string((char_u *)str, (char_u **)0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 if (value == NULL)
1390 {
1391 XPUSHs(sv_2mortal(newSViv(0)));
1392 XPUSHs(sv_2mortal(newSVpv("", 0)));
1393 }
1394 else
1395 {
1396 XPUSHs(sv_2mortal(newSViv(1)));
1397 XPUSHs(sv_2mortal(newSVpv((char *)value, 0)));
1398 vim_free(value);
1399 }
1400
1401void
1402Buffers(...)
1403
1404 PREINIT:
1405 buf_T *vimbuf;
1406 int i, b;
1407
1408 PPCODE:
1409 if (items == 0)
1410 {
1411 if (GIMME == G_SCALAR)
1412 {
1413 i = 0;
1414 for (vimbuf = firstbuf; vimbuf; vimbuf = vimbuf->b_next)
1415 ++i;
1416
1417 XPUSHs(sv_2mortal(newSViv(i)));
1418 }
1419 else
1420 {
1421 for (vimbuf = firstbuf; vimbuf; vimbuf = vimbuf->b_next)
1422 XPUSHs(newBUFrv(newSV(0), vimbuf));
1423 }
1424 }
1425 else
1426 {
1427 for (i = 0; i < items; i++)
1428 {
1429 SV *sv = ST(i);
1430 if (SvIOK(sv))
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001431 b = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 else
1433 {
1434 char_u *pat;
1435 STRLEN len;
1436
1437 pat = (char_u *)SvPV(sv, len);
1438 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001439 b = buflist_findpat(pat, pat+len, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 --emsg_off;
1441 }
1442
1443 if (b >= 0)
1444 {
1445 vimbuf = buflist_findnr(b);
1446 if (vimbuf)
1447 XPUSHs(newBUFrv(newSV(0), vimbuf));
1448 }
1449 }
1450 }
1451
1452void
1453Windows(...)
1454
1455 PREINIT:
1456 win_T *vimwin;
1457 int i, w;
1458
1459 PPCODE:
1460 if (items == 0)
1461 {
1462 if (GIMME == G_SCALAR)
1463 XPUSHs(sv_2mortal(newSViv(win_count())));
1464 else
1465 {
1466 for (vimwin = firstwin; vimwin != NULL; vimwin = W_NEXT(vimwin))
1467 XPUSHs(newWINrv(newSV(0), vimwin));
1468 }
1469 }
1470 else
1471 {
1472 for (i = 0; i < items; i++)
1473 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001474 w = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 vimwin = win_find_nr(w);
1476 if (vimwin)
1477 XPUSHs(newWINrv(newSV(0), vimwin));
1478 }
1479 }
1480
1481MODULE = VIM PACKAGE = VIWIN
1482
1483void
1484DESTROY(win)
1485 VIWIN win
1486
1487 CODE:
1488 if (win_valid(win))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001489 win->w_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490
1491SV *
1492Buffer(win)
1493 VIWIN win
1494
1495 CODE:
1496 if (!win_valid(win))
1497 win = curwin;
1498 RETVAL = newBUFrv(newSV(0), win->w_buffer);
1499 OUTPUT:
1500 RETVAL
1501
1502void
1503SetHeight(win, height)
1504 VIWIN win
1505 int height;
1506
1507 PREINIT:
1508 win_T *savewin;
1509
1510 PPCODE:
1511 if (!win_valid(win))
1512 win = curwin;
1513 savewin = curwin;
1514 curwin = win;
1515 win_setheight(height);
1516 curwin = savewin;
1517
1518void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001519Cursor(VIWIN win, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520
1521 PPCODE:
Bram Moolenaara1711622011-07-27 14:15:46 +02001522 if (items == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 {
1524 EXTEND(sp, 2);
1525 if (!win_valid(win))
1526 win = curwin;
1527 PUSHs(sv_2mortal(newSViv(win->w_cursor.lnum)));
1528 PUSHs(sv_2mortal(newSViv(win->w_cursor.col)));
1529 }
Bram Moolenaara1711622011-07-27 14:15:46 +02001530 else if (items == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 {
1532 int lnum, col;
1533
1534 if (!win_valid(win))
1535 win = curwin;
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001536 lnum = (int) SvIV(ST(1));
1537 col = (int) SvIV(ST(2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 win->w_cursor.lnum = lnum;
1539 win->w_cursor.col = col;
1540 check_cursor(); /* put cursor on an existing line */
1541 update_screen(NOT_VALID);
1542 }
1543
1544MODULE = VIM PACKAGE = VIBUF
1545
1546void
1547DESTROY(vimbuf)
1548 VIBUF vimbuf;
1549
1550 CODE:
1551 if (buf_valid(vimbuf))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001552 vimbuf->b_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553
1554void
1555Name(vimbuf)
1556 VIBUF vimbuf;
1557
1558 PPCODE:
1559 if (!buf_valid(vimbuf))
1560 vimbuf = curbuf;
1561 /* No file name returns an empty string */
1562 if (vimbuf->b_fname == NULL)
1563 XPUSHs(sv_2mortal(newSVpv("", 0)));
1564 else
1565 XPUSHs(sv_2mortal(newSVpv((char *)vimbuf->b_fname, 0)));
1566
1567void
1568Number(vimbuf)
1569 VIBUF vimbuf;
1570
1571 PPCODE:
1572 if (!buf_valid(vimbuf))
1573 vimbuf = curbuf;
1574 XPUSHs(sv_2mortal(newSViv(vimbuf->b_fnum)));
1575
1576void
1577Count(vimbuf)
1578 VIBUF vimbuf;
1579
1580 PPCODE:
1581 if (!buf_valid(vimbuf))
1582 vimbuf = curbuf;
1583 XPUSHs(sv_2mortal(newSViv(vimbuf->b_ml.ml_line_count)));
1584
1585void
1586Get(vimbuf, ...)
1587 VIBUF vimbuf;
1588
1589 PREINIT:
1590 char_u *line;
1591 int i;
1592 long lnum;
1593 PPCODE:
1594 if (buf_valid(vimbuf))
1595 {
1596 for (i = 1; i < items; i++)
1597 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001598 lnum = (long) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1600 {
1601 line = ml_get_buf(vimbuf, lnum, FALSE);
1602 XPUSHs(sv_2mortal(newSVpv((char *)line, 0)));
1603 }
1604 }
1605 }
1606
1607void
1608Set(vimbuf, ...)
1609 VIBUF vimbuf;
1610
1611 PREINIT:
1612 int i;
1613 long lnum;
1614 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 PPCODE:
1616 if (buf_valid(vimbuf))
1617 {
1618 if (items < 3)
1619 croak("Usage: VIBUF::Set(vimbuf, lnum, @lines)");
1620
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001621 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 for(i = 2; i < items; i++, lnum++)
1623 {
1624 line = SvPV(ST(i),PL_na);
1625 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1626 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001627 aco_save_T aco;
1628
1629 /* set curwin/curbuf for "vimbuf" and save some things */
1630 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001631
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 if (u_savesub(lnum) == OK)
1633 {
1634 ml_replace(lnum, (char_u *)line, TRUE);
1635 changed_bytes(lnum, 0);
1636 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001637
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001638 /* restore curwin/curbuf and a few other things */
1639 aucmd_restbuf(&aco);
1640 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 }
1642 }
1643 }
1644
1645void
1646Delete(vimbuf, ...)
1647 VIBUF vimbuf;
1648
1649 PREINIT:
1650 long i, lnum = 0, count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 PPCODE:
1652 if (buf_valid(vimbuf))
1653 {
1654 if (items == 2)
1655 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001656 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 count = 1;
1658 }
1659 else if (items == 3)
1660 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001661 lnum = (long) SvIV(ST(1));
1662 count = (long) 1 + SvIV(ST(2)) - lnum;
Bram Moolenaara1711622011-07-27 14:15:46 +02001663 if (count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 count = 1;
Bram Moolenaara1711622011-07-27 14:15:46 +02001665 if (count < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 {
1667 lnum -= count;
1668 count = -count;
1669 }
1670 }
1671 if (items >= 2)
1672 {
1673 for (i = 0; i < count; i++)
1674 {
1675 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1676 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001677 aco_save_T aco;
1678
1679 /* set curwin/curbuf for "vimbuf" and save some things */
1680 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001681
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 if (u_savedel(lnum, 1) == OK)
1683 {
1684 ml_delete(lnum, 0);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00001685 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 deleted_lines_mark(lnum, 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001688
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001689 /* restore curwin/curbuf and a few other things */
1690 aucmd_restbuf(&aco);
1691 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001692
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 update_curbuf(VALID);
1694 }
1695 }
1696 }
1697 }
1698
1699void
1700Append(vimbuf, ...)
1701 VIBUF vimbuf;
1702
1703 PREINIT:
1704 int i;
1705 long lnum;
1706 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 PPCODE:
1708 if (buf_valid(vimbuf))
1709 {
1710 if (items < 3)
1711 croak("Usage: VIBUF::Append(vimbuf, lnum, @lines)");
1712
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001713 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 for (i = 2; i < items; i++, lnum++)
1715 {
1716 line = SvPV(ST(i),PL_na);
1717 if (lnum >= 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1718 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001719 aco_save_T aco;
1720
1721 /* set curwin/curbuf for "vimbuf" and save some things */
1722 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001723
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 if (u_inssub(lnum + 1) == OK)
1725 {
1726 ml_append(lnum, (char_u *)line, (colnr_T)0, FALSE);
1727 appended_lines_mark(lnum, 1L);
1728 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001729
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00001730 /* restore curwin/curbuf and a few other things */
1731 aucmd_restbuf(&aco);
1732 /* Careful: autocommands may have made "vimbuf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001733
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 update_curbuf(VALID);
1735 }
1736 }
1737 }
1738
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001739#ifdef __GNUC__
1740# pragma GCC diagnostic pop
1741#endif