1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
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.
9 * if_perl.xs: Main code for Perl interface support.
10 * Mostly written by Sven Verdoolaege.
13 #define _memory_h /* avoid memset redeclaration */
14 #define IN_PERL_FILE /* don't include if_perl.pro from proto.h */
20 * Work around clashes between Perl and Vim namespace. proto.h doesn't
21 * include if_perl.pro and perlsfio.pro when IN_PERL_FILE is defined, because
22 * we need the CV typedef. proto.h can't be moved to after including
23 * if_perl.h, because we get all sorts of name clashes then.
27 # include "proto/if_perl.pro"
28 # include "proto/if_perlsfio.pro"
32 /* Perl compatibility stuff. This should ensure compatibility with older
37 # include <patchlevel.h>
38 # define PERL_REVISION 5
39 # define PERL_VERSION PATCHLEVEL
40 # define PERL_SUBVERSION SUBVERSION
44 * Quoting Jan Dubois of Active State:
45 * ActivePerl build 822 still identifies itself as 5.8.8 but already
46 * contains many of the changes from the upcoming Perl 5.8.9 release.
48 * The changes include addition of two symbols (Perl_sv_2iv_flags,
49 * Perl_newXS_flags) not present in earlier releases.
51 * Jan Dubois suggested the following guarding scheme.
53 * Active State defined ACTIVEPERL_VERSION as a string in versions before
54 * 5.8.8; and so the comparison to 822 below needs to be guarded.
56 #if (PERL_REVISION == 5) && (PERL_VERSION == 8) && (PERL_SUBVERSION >= 8)
57 # if (ACTIVEPERL_VERSION >= 822) || (PERL_SUBVERSION >= 9)
58 # define PERL589_OR_LATER
61 #if (PERL_REVISION == 5) && (PERL_VERSION >= 9)
62 # define PERL589_OR_LATER
74 /* Compatibility hacks over */
76 static PerlInterpreter *perl_interp = NULL;
77 static void xs_init __ARGS((pTHX));
78 static void VIM_init __ARGS((void));
79 EXTERN_C void boot_DynaLoader __ARGS((pTHX_ CV*));
82 * For dynamic linked perl. (Windows)
84 #if defined(DYNAMIC_PERL) || defined(PROTO)
88 # define perl_alloc dll_perl_alloc
89 # define perl_construct dll_perl_construct
90 # define perl_parse dll_perl_parse
91 # define perl_run dll_perl_run
92 # define perl_destruct dll_perl_destruct
93 # define perl_free dll_perl_free
94 # define Perl_get_context dll_Perl_get_context
95 # define Perl_croak dll_Perl_croak
97 # define Perl_croak_nocontext dll_Perl_croak_nocontext
98 # define Perl_call_argv dll_Perl_call_argv
99 # define Perl_call_pv dll_Perl_call_pv
100 # define Perl_eval_sv dll_Perl_eval_sv
101 # define Perl_get_sv dll_Perl_get_sv
102 # define Perl_eval_pv dll_Perl_eval_pv
103 # define Perl_call_method dll_Perl_call_method
105 # define Perl_dowantarray dll_Perl_dowantarray
106 # define Perl_free_tmps dll_Perl_free_tmps
107 # define Perl_gv_stashpv dll_Perl_gv_stashpv
108 # define Perl_markstack_grow dll_Perl_markstack_grow
109 # define Perl_mg_find dll_Perl_mg_find
110 # define Perl_newXS dll_Perl_newXS
111 # define Perl_newSV dll_Perl_newSV
112 # define Perl_newSViv dll_Perl_newSViv
113 # define Perl_newSVpv dll_Perl_newSVpv
114 # define Perl_pop_scope dll_Perl_pop_scope
115 # define Perl_push_scope dll_Perl_push_scope
116 # define Perl_save_int dll_Perl_save_int
117 # define Perl_stack_grow dll_Perl_stack_grow
118 # define Perl_set_context dll_Perl_set_context
119 # define Perl_sv_2bool dll_Perl_sv_2bool
120 # define Perl_sv_2iv dll_Perl_sv_2iv
121 # define Perl_sv_2mortal dll_Perl_sv_2mortal
122 # if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
123 # define Perl_sv_2pv_flags dll_Perl_sv_2pv_flags
124 # define Perl_sv_2pv_nolen dll_Perl_sv_2pv_nolen
126 # define Perl_sv_2pv dll_Perl_sv_2pv
128 # define Perl_sv_bless dll_Perl_sv_bless
129 # if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
130 # define Perl_sv_catpvn_flags dll_Perl_sv_catpvn_flags
132 # define Perl_sv_catpvn dll_Perl_sv_catpvn
134 #ifdef PERL589_OR_LATER
135 # define Perl_sv_2iv_flags dll_Perl_sv_2iv_flags
136 # define Perl_newXS_flags dll_Perl_newXS_flags
138 # define Perl_sv_free dll_Perl_sv_free
139 # if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
140 # define Perl_sv_free2 dll_Perl_sv_free2
142 # define Perl_sv_isa dll_Perl_sv_isa
143 # define Perl_sv_magic dll_Perl_sv_magic
144 # define Perl_sv_setiv dll_Perl_sv_setiv
145 # define Perl_sv_setpv dll_Perl_sv_setpv
146 # define Perl_sv_setpvn dll_Perl_sv_setpvn
147 # if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
148 # define Perl_sv_setsv_flags dll_Perl_sv_setsv_flags
150 # define Perl_sv_setsv dll_Perl_sv_setsv
152 # define Perl_sv_upgrade dll_Perl_sv_upgrade
153 # define Perl_Tstack_sp_ptr dll_Perl_Tstack_sp_ptr
154 # define Perl_Top_ptr dll_Perl_Top_ptr
155 # define Perl_Tstack_base_ptr dll_Perl_Tstack_base_ptr
156 # define Perl_Tstack_max_ptr dll_Perl_Tstack_max_ptr
157 # define Perl_Ttmps_ix_ptr dll_Perl_Ttmps_ix_ptr
158 # define Perl_Ttmps_floor_ptr dll_Perl_Ttmps_floor_ptr
159 # define Perl_Tmarkstack_ptr_ptr dll_Perl_Tmarkstack_ptr_ptr
160 # define Perl_Tmarkstack_max_ptr dll_Perl_Tmarkstack_max_ptr
161 # define Perl_TSv_ptr dll_Perl_TSv_ptr
162 # define Perl_TXpv_ptr dll_Perl_TXpv_ptr
163 # define Perl_Tna_ptr dll_Perl_Tna_ptr
164 # define Perl_Idefgv_ptr dll_Perl_Idefgv_ptr
165 # define Perl_Ierrgv_ptr dll_Perl_Ierrgv_ptr
166 # define Perl_Isv_yes_ptr dll_Perl_Isv_yes_ptr
167 # define boot_DynaLoader dll_boot_DynaLoader
169 # define Perl_sys_init dll_Perl_sys_init
170 # define Perl_sys_term dll_Perl_sys_term
171 # define Perl_ISv_ptr dll_Perl_ISv_ptr
172 # define Perl_Istack_max_ptr dll_Perl_Istack_max_ptr
173 # define Perl_Istack_base_ptr dll_Perl_Istack_base_ptr
174 # define Perl_Itmps_ix_ptr dll_Perl_Itmps_ix_ptr
175 # define Perl_Itmps_floor_ptr dll_Perl_Itmps_floor_ptr
176 # define Perl_IXpv_ptr dll_Perl_IXpv_ptr
177 # define Perl_Ina_ptr dll_Perl_Ina_ptr
178 # define Perl_Imarkstack_ptr_ptr dll_Perl_Imarkstack_ptr_ptr
179 # define Perl_Imarkstack_max_ptr dll_Perl_Imarkstack_max_ptr
180 # define Perl_Istack_sp_ptr dll_Perl_Istack_sp_ptr
181 # define Perl_Iop_ptr dll_Perl_Iop_ptr
182 # define Perl_call_list dll_Perl_call_list
183 # define Perl_Iscopestack_ix_ptr dll_Perl_Iscopestack_ix_ptr
184 # define Perl_Iunitcheckav_ptr dll_Perl_Iunitcheckav_ptr
186 #ifndef DYNAMIC_PERL /* just generating prototypes */
188 typedef int XSINIT_t;
189 typedef int XSUBADDR_t;
193 * Declare HANDLE for perl.dll and function pointers.
195 static HANDLE hPerlLib = NULL;
197 static PerlInterpreter* (*perl_alloc)();
198 static void (*perl_construct)(PerlInterpreter*);
199 static void (*perl_destruct)(PerlInterpreter*);
200 static void (*perl_free)(PerlInterpreter*);
201 static int (*perl_run)(PerlInterpreter*);
202 static int (*perl_parse)(PerlInterpreter*, XSINIT_t, int, char**, char**);
203 static void* (*Perl_get_context)(void);
204 static void (*Perl_croak)(pTHX_ const char*, ...);
205 static void (*Perl_croak_nocontext)(const char*, ...);
206 static I32 (*Perl_dowantarray)(pTHX);
207 static void (*Perl_free_tmps)(pTHX);
208 static HV* (*Perl_gv_stashpv)(pTHX_ const char*, I32);
209 static void (*Perl_markstack_grow)(pTHX);
210 static MAGIC* (*Perl_mg_find)(pTHX_ SV*, int);
211 static CV* (*Perl_newXS)(pTHX_ char*, XSUBADDR_t, char*);
212 static SV* (*Perl_newSV)(pTHX_ STRLEN);
213 static SV* (*Perl_newSViv)(pTHX_ IV);
214 static SV* (*Perl_newSVpv)(pTHX_ const char*, STRLEN);
215 static I32 (*Perl_call_argv)(pTHX_ const char*, I32, char**);
216 static I32 (*Perl_call_pv)(pTHX_ const char*, I32);
217 static I32 (*Perl_eval_sv)(pTHX_ SV*, I32);
218 static SV* (*Perl_get_sv)(pTHX_ const char*, I32);
219 static SV* (*Perl_eval_pv)(pTHX_ const char*, I32);
220 static SV* (*Perl_call_method)(pTHX_ const char*, I32);
221 static void (*Perl_pop_scope)(pTHX);
222 static void (*Perl_push_scope)(pTHX);
223 static void (*Perl_save_int)(pTHX_ int*);
224 static SV** (*Perl_stack_grow)(pTHX_ SV**, SV**p, int);
225 static SV** (*Perl_set_context)(void*);
226 static bool (*Perl_sv_2bool)(pTHX_ SV*);
227 static IV (*Perl_sv_2iv)(pTHX_ SV*);
228 static SV* (*Perl_sv_2mortal)(pTHX_ SV*);
229 #if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
230 static char* (*Perl_sv_2pv_flags)(pTHX_ SV*, STRLEN*, I32);
231 static char* (*Perl_sv_2pv_nolen)(pTHX_ SV*);
233 static char* (*Perl_sv_2pv)(pTHX_ SV*, STRLEN*);
235 static SV* (*Perl_sv_bless)(pTHX_ SV*, HV*);
236 #if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
237 static void (*Perl_sv_catpvn_flags)(pTHX_ SV* , const char*, STRLEN, I32);
239 static void (*Perl_sv_catpvn)(pTHX_ SV*, const char*, STRLEN);
241 #ifdef PERL589_OR_LATER
242 static IV (*Perl_sv_2iv_flags)(pTHX_ SV* sv, I32 flags);
243 static CV * (*Perl_newXS_flags)(pTHX_ const char *name, XSUBADDR_t subaddr, const char *const filename, const char *const proto, U32 flags);
245 static void (*Perl_sv_free)(pTHX_ SV*);
246 static int (*Perl_sv_isa)(pTHX_ SV*, const char*);
247 static void (*Perl_sv_magic)(pTHX_ SV*, SV*, int, const char*, I32);
248 static void (*Perl_sv_setiv)(pTHX_ SV*, IV);
249 static void (*Perl_sv_setpv)(pTHX_ SV*, const char*);
250 static void (*Perl_sv_setpvn)(pTHX_ SV*, const char*, STRLEN);
251 #if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
252 static void (*Perl_sv_setsv_flags)(pTHX_ SV*, SV*, I32);
254 static void (*Perl_sv_setsv)(pTHX_ SV*, SV*);
256 static bool (*Perl_sv_upgrade)(pTHX_ SV*, U32);
257 static SV*** (*Perl_Tstack_sp_ptr)(register PerlInterpreter*);
258 static OP** (*Perl_Top_ptr)(register PerlInterpreter*);
259 static SV*** (*Perl_Tstack_base_ptr)(register PerlInterpreter*);
260 static SV*** (*Perl_Tstack_max_ptr)(register PerlInterpreter*);
261 static I32* (*Perl_Ttmps_ix_ptr)(register PerlInterpreter*);
262 static I32* (*Perl_Ttmps_floor_ptr)(register PerlInterpreter*);
263 static I32** (*Perl_Tmarkstack_ptr_ptr)(register PerlInterpreter*);
264 static I32** (*Perl_Tmarkstack_max_ptr)(register PerlInterpreter*);
265 static SV** (*Perl_TSv_ptr)(register PerlInterpreter*);
266 static XPV** (*Perl_TXpv_ptr)(register PerlInterpreter*);
267 static STRLEN* (*Perl_Tna_ptr)(register PerlInterpreter*);
268 static GV** (*Perl_Idefgv_ptr)(register PerlInterpreter*);
269 static GV** (*Perl_Ierrgv_ptr)(register PerlInterpreter*);
270 static SV* (*Perl_Isv_yes_ptr)(register PerlInterpreter*);
271 static void (*boot_DynaLoader)_((pTHX_ CV*));
273 #if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
274 static void (*Perl_sv_free2)(pTHX_ SV*);
275 static void (*Perl_sys_init)(int* argc, char*** argv);
276 static void (*Perl_sys_term)(void);
277 static SV** (*Perl_ISv_ptr)(register PerlInterpreter*);
278 static SV*** (*Perl_Istack_max_ptr)(register PerlInterpreter*);
279 static SV*** (*Perl_Istack_base_ptr)(register PerlInterpreter*);
280 static XPV** (*Perl_IXpv_ptr)(register PerlInterpreter*);
281 static I32* (*Perl_Itmps_ix_ptr)(register PerlInterpreter*);
282 static I32* (*Perl_Itmps_floor_ptr)(register PerlInterpreter*);
283 static STRLEN* (*Perl_Ina_ptr)(register PerlInterpreter*);
284 static I32** (*Perl_Imarkstack_ptr_ptr)(register PerlInterpreter*);
285 static I32** (*Perl_Imarkstack_max_ptr)(register PerlInterpreter*);
286 static SV*** (*Perl_Istack_sp_ptr)(register PerlInterpreter*);
287 static OP** (*Perl_Iop_ptr)(register PerlInterpreter*);
288 static void (*Perl_call_list)(pTHX_ I32, AV*);
289 static I32* (*Perl_Iscopestack_ix_ptr)(register PerlInterpreter*);
290 static AV** (*Perl_Iunitcheckav_ptr)(register PerlInterpreter*);
294 * Table of name to function pointer of perl.
296 #define PERL_PROC FARPROC
300 } perl_funcname_table[] = {
301 {"perl_alloc", (PERL_PROC*)&perl_alloc},
302 {"perl_construct", (PERL_PROC*)&perl_construct},
303 {"perl_destruct", (PERL_PROC*)&perl_destruct},
304 {"perl_free", (PERL_PROC*)&perl_free},
305 {"perl_run", (PERL_PROC*)&perl_run},
306 {"perl_parse", (PERL_PROC*)&perl_parse},
307 {"Perl_get_context", (PERL_PROC*)&Perl_get_context},
308 {"Perl_croak", (PERL_PROC*)&Perl_croak},
309 {"Perl_croak_nocontext", (PERL_PROC*)&Perl_croak_nocontext},
310 {"Perl_dowantarray", (PERL_PROC*)&Perl_dowantarray},
311 {"Perl_free_tmps", (PERL_PROC*)&Perl_free_tmps},
312 {"Perl_gv_stashpv", (PERL_PROC*)&Perl_gv_stashpv},
313 {"Perl_markstack_grow", (PERL_PROC*)&Perl_markstack_grow},
314 {"Perl_mg_find", (PERL_PROC*)&Perl_mg_find},
315 {"Perl_newXS", (PERL_PROC*)&Perl_newXS},
316 {"Perl_newSV", (PERL_PROC*)&Perl_newSV},
317 {"Perl_newSViv", (PERL_PROC*)&Perl_newSViv},
318 {"Perl_newSVpv", (PERL_PROC*)&Perl_newSVpv},
319 {"Perl_call_argv", (PERL_PROC*)&Perl_call_argv},
320 {"Perl_call_pv", (PERL_PROC*)&Perl_call_pv},
321 {"Perl_eval_sv", (PERL_PROC*)&Perl_eval_sv},
322 {"Perl_get_sv", (PERL_PROC*)&Perl_get_sv},
323 {"Perl_eval_pv", (PERL_PROC*)&Perl_eval_pv},
324 {"Perl_call_method", (PERL_PROC*)&Perl_call_method},
325 {"Perl_pop_scope", (PERL_PROC*)&Perl_pop_scope},
326 {"Perl_push_scope", (PERL_PROC*)&Perl_push_scope},
327 {"Perl_save_int", (PERL_PROC*)&Perl_save_int},
328 {"Perl_stack_grow", (PERL_PROC*)&Perl_stack_grow},
329 {"Perl_set_context", (PERL_PROC*)&Perl_set_context},
330 {"Perl_sv_2bool", (PERL_PROC*)&Perl_sv_2bool},
331 {"Perl_sv_2iv", (PERL_PROC*)&Perl_sv_2iv},
332 {"Perl_sv_2mortal", (PERL_PROC*)&Perl_sv_2mortal},
333 #if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
334 {"Perl_sv_2pv_flags", (PERL_PROC*)&Perl_sv_2pv_flags},
335 {"Perl_sv_2pv_nolen", (PERL_PROC*)&Perl_sv_2pv_nolen},
337 {"Perl_sv_2pv", (PERL_PROC*)&Perl_sv_2pv},
339 #ifdef PERL589_OR_LATER
340 {"Perl_sv_2iv_flags", (PERL_PROC*)&Perl_sv_2iv_flags},
341 {"Perl_newXS_flags", (PERL_PROC*)&Perl_newXS_flags},
343 {"Perl_sv_bless", (PERL_PROC*)&Perl_sv_bless},
344 #if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
345 {"Perl_sv_catpvn_flags", (PERL_PROC*)&Perl_sv_catpvn_flags},
347 {"Perl_sv_catpvn", (PERL_PROC*)&Perl_sv_catpvn},
349 {"Perl_sv_free", (PERL_PROC*)&Perl_sv_free},
350 {"Perl_sv_isa", (PERL_PROC*)&Perl_sv_isa},
351 {"Perl_sv_magic", (PERL_PROC*)&Perl_sv_magic},
352 {"Perl_sv_setiv", (PERL_PROC*)&Perl_sv_setiv},
353 {"Perl_sv_setpv", (PERL_PROC*)&Perl_sv_setpv},
354 {"Perl_sv_setpvn", (PERL_PROC*)&Perl_sv_setpvn},
355 #if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
356 {"Perl_sv_setsv_flags", (PERL_PROC*)&Perl_sv_setsv_flags},
358 {"Perl_sv_setsv", (PERL_PROC*)&Perl_sv_setsv},
360 {"Perl_sv_upgrade", (PERL_PROC*)&Perl_sv_upgrade},
361 #if (PERL_REVISION == 5) && (PERL_VERSION < 10)
362 {"Perl_Tstack_sp_ptr", (PERL_PROC*)&Perl_Tstack_sp_ptr},
363 {"Perl_Top_ptr", (PERL_PROC*)&Perl_Top_ptr},
364 {"Perl_Tstack_base_ptr", (PERL_PROC*)&Perl_Tstack_base_ptr},
365 {"Perl_Tstack_max_ptr", (PERL_PROC*)&Perl_Tstack_max_ptr},
366 {"Perl_Ttmps_ix_ptr", (PERL_PROC*)&Perl_Ttmps_ix_ptr},
367 {"Perl_Ttmps_floor_ptr", (PERL_PROC*)&Perl_Ttmps_floor_ptr},
368 {"Perl_Tmarkstack_ptr_ptr", (PERL_PROC*)&Perl_Tmarkstack_ptr_ptr},
369 {"Perl_Tmarkstack_max_ptr", (PERL_PROC*)&Perl_Tmarkstack_max_ptr},
370 {"Perl_TSv_ptr", (PERL_PROC*)&Perl_TSv_ptr},
371 {"Perl_TXpv_ptr", (PERL_PROC*)&Perl_TXpv_ptr},
372 {"Perl_Tna_ptr", (PERL_PROC*)&Perl_Tna_ptr},
374 {"Perl_sv_free2", (PERL_PROC*)&Perl_sv_free2},
375 {"Perl_sys_init", (PERL_PROC*)&Perl_sys_init},
376 {"Perl_sys_term", (PERL_PROC*)&Perl_sys_term},
377 {"Perl_ISv_ptr", (PERL_PROC*)&Perl_ISv_ptr},
378 {"Perl_Istack_sp_ptr", (PERL_PROC*)&Perl_Istack_sp_ptr},
379 {"Perl_Iop_ptr", (PERL_PROC*)&Perl_Iop_ptr},
380 {"Perl_Istack_base_ptr", (PERL_PROC*)&Perl_Istack_base_ptr},
381 {"Perl_Istack_max_ptr", (PERL_PROC*)&Perl_Istack_max_ptr},
382 {"Perl_Itmps_ix_ptr", (PERL_PROC*)&Perl_Itmps_ix_ptr},
383 {"Perl_Itmps_floor_ptr", (PERL_PROC*)&Perl_Itmps_floor_ptr},
384 {"Perl_Imarkstack_ptr_ptr", (PERL_PROC*)&Perl_Imarkstack_ptr_ptr},
385 {"Perl_Imarkstack_max_ptr", (PERL_PROC*)&Perl_Imarkstack_max_ptr},
386 {"Perl_ISv_ptr", (PERL_PROC*)&Perl_ISv_ptr},
387 {"Perl_IXpv_ptr", (PERL_PROC*)&Perl_IXpv_ptr},
388 {"Perl_Ina_ptr", (PERL_PROC*)&Perl_Ina_ptr},
389 {"Perl_call_list", (PERL_PROC*)&Perl_call_list},
390 {"Perl_Iscopestack_ix_ptr", (PERL_PROC*)&Perl_Iscopestack_ix_ptr},
391 {"Perl_Iunitcheckav_ptr", (PERL_PROC*)&Perl_Iunitcheckav_ptr},
393 {"Perl_Idefgv_ptr", (PERL_PROC*)&Perl_Idefgv_ptr},
394 {"Perl_Ierrgv_ptr", (PERL_PROC*)&Perl_Ierrgv_ptr},
395 {"Perl_Isv_yes_ptr", (PERL_PROC*)&Perl_Isv_yes_ptr},
396 {"boot_DynaLoader", (PERL_PROC*)&boot_DynaLoader},
401 * Make all runtime-links of perl.
403 * 1. Get module handle using LoadLibraryEx.
404 * 2. Get pointer to perl function by GetProcAddress.
405 * 3. Repeat 2, until get all functions will be used.
407 * Parameter 'libname' provides name of DLL.
411 perl_runtime_link_init(char *libname, int verbose)
415 if (hPerlLib != NULL)
417 if (!(hPerlLib = LoadLibraryEx(libname, NULL, 0)))
420 EMSG2(_("E370: Could not load library %s"), libname);
423 for (i = 0; perl_funcname_table[i].ptr; ++i)
425 if (!(*perl_funcname_table[i].ptr = GetProcAddress(hPerlLib,
426 perl_funcname_table[i].name)))
428 FreeLibrary(hPerlLib);
431 EMSG2(_(e_loadfunc), perl_funcname_table[i].name);
439 * If runtime-link-perl(DLL) was loaded successfully, return TRUE.
440 * There were no DLL loaded, return FALSE.
443 perl_enabled(verbose)
446 return perl_runtime_link_init(DYNAMIC_PERL_DLL, verbose) == OK;
448 #endif /* DYNAMIC_PERL */
451 * perl_init(): initialize perl interpreter
452 * We have to call perl_parse to initialize some structures,
453 * there's nothing to actually parse.
458 char *bootargs[] = { "VI", NULL };
460 static char *argv[] = { "", "-e", "" };
462 #if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
463 Perl_sys_init(&argc, (char***)&argv);
465 perl_interp = perl_alloc();
466 perl_construct(perl_interp);
467 perl_parse(perl_interp, xs_init, argc, argv, 0);
468 perl_call_argv("VIM::bootstrap", (long)G_DISCARD, bootargs);
471 sfdisc(PerlIO_stdout(), sfdcnewvim());
472 sfdisc(PerlIO_stderr(), sfdcnewvim());
473 sfsetbuf(PerlIO_stdout(), NULL, 0);
474 sfsetbuf(PerlIO_stderr(), NULL, 0);
479 * perl_end(): clean up after ourselves
486 perl_run(perl_interp);
487 perl_destruct(perl_interp);
488 perl_free(perl_interp);
490 #if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
497 FreeLibrary(hPerlLib);
504 * msg_split(): send a message to the message handling routines
505 * split at '\n' first though.
510 int attr; /* highlighting attributes */
513 char *token = (char *)s;
515 while ((next = strchr(token, '\n')) && !got_int)
517 *next++ = '\0'; /* replace \n with \0 */
518 msg_attr((char_u *)token, attr);
521 if (*token && !got_int)
522 msg_attr((char_u *)token, attr);
527 * This stub is needed because an "#ifdef FEAT_EVAL" around Eval() doesn't
531 eval_to_string(arg, nextcmd, dolist)
541 * Create a new reference to an SV pointing to the SCR structure
542 * The b_perl_private/w_perl_private part of the SCR structure points to the
543 * SV, so there can only be one such SV for a particular SCR structure. When
544 * the last reference has gone (DESTROY is called),
545 * b_perl_private/w_perl_private is reset; When the screen goes away before
546 * all references are gone, the value of the SV is reset;
547 * any subsequent use of any of those reference will produce
548 * a warning. (see typemap)
556 sv_upgrade(rv, SVt_RV);
557 if (ptr->w_perl_private == NULL)
559 ptr->w_perl_private = newSV(0);
560 sv_setiv(ptr->w_perl_private, (IV)ptr);
563 SvREFCNT_inc(ptr->w_perl_private);
564 SvRV(rv) = ptr->w_perl_private;
566 return sv_bless(rv, gv_stashpv("VIWIN", TRUE));
574 sv_upgrade(rv, SVt_RV);
575 if (ptr->b_perl_private == NULL)
577 ptr->b_perl_private = newSV(0);
578 sv_setiv(ptr->b_perl_private, (IV)ptr);
581 SvREFCNT_inc(ptr->b_perl_private);
582 SvRV(rv) = ptr->b_perl_private;
584 return sv_bless(rv, gv_stashpv("VIBUF", TRUE));
589 * Remove all refences to the window to be destroyed
595 if (wp->w_perl_private)
596 sv_setiv((SV *)wp->w_perl_private, 0);
604 if (bp->b_perl_private)
605 sv_setiv((SV *)bp->b_perl_private, 0);
610 # if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
611 I32 cur_val(pTHX_ IV iv, SV *sv);
613 I32 cur_val(IV iv, SV *sv);
617 * Handler for the magic variables $main::curwin and $main::curbuf.
618 * The handler is put into the magic vtbl for these variables.
619 * (This is effectively a C-level equivalent of a tied variable).
620 * There is no "set" function as the variables are read-only.
622 # if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
623 I32 cur_val(pTHX_ IV iv, SV *sv)
625 I32 cur_val(IV iv, SV *sv)
630 rv = newWINrv(newSV(0), curwin);
632 rv = newBUFrv(newSV(0), curbuf);
638 struct ufuncs cw_funcs = { cur_val, 0, 0 };
639 struct ufuncs cb_funcs = { cur_val, 0, 1 };
642 * VIM_init(): Vim-specific initialisation.
643 * Make the magical main::curwin and main::curbuf variables
648 static char cw[] = "main::curwin";
649 static char cb[] = "main::curbuf";
652 sv = perl_get_sv(cw, TRUE);
653 sv_magic(sv, NULL, 'U', (char *)&cw_funcs, sizeof(cw_funcs));
656 sv = perl_get_sv(cb, TRUE);
657 sv_magic(sv, NULL, 'U', (char *)&cb_funcs, sizeof(cb_funcs));
661 * Setup the Safe compartment.
662 * It shouldn't be a fatal error if the Safe module is missing.
663 * XXX: Only shares the 'Msg' routine (which has to be called
666 (void)perl_eval_pv( "if ( eval( 'require Safe' ) ) { $VIM::safe = Safe->new(); $VIM::safe->share_from( 'VIM', ['Msg'] ); }", G_DISCARD | G_VOID );
671 static char *e_noperl = N_("Sorry, this command is disabled: the Perl library could not be loaded.");
687 script = (char *)script_get(eap, eap->arg);
694 if (perl_interp == NULL)
697 if (!perl_enabled(TRUE))
713 sv = newSVpv((char *)eap->arg, 0);
716 sv = newSVpv(script, 0);
723 if ((safe = perl_get_sv( "VIM::safe", FALSE )) == NULL || !SvTRUE(safe))
724 EMSG(_("E299: Perl evaluation forbidden in sandbox without the Safe module"));
731 perl_call_method("reval", G_DISCARD);
736 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
740 err = SvPV(GvSV(PL_errgv), length);
748 msg_split((char_u *)err, highlight_attr[HLF_E]);
754 replace_line(line, end)
755 linenr_T *line, *end;
759 if (SvOK(GvSV(PL_defgv)))
761 str = SvPV(GvSV(PL_defgv), PL_na);
762 ml_replace(*line, (char_u *)str, 1);
763 changed_bytes(*line, 0);
767 ml_delete(*line, FALSE);
768 deleted_lines_mark(*line, 1L);
790 if (perl_interp == NULL)
793 if (!perl_enabled(TRUE))
803 length = strlen((char *)eap->arg);
804 sv = newSV(length + sizeof("sub VIM::perldo {") - 1 + 1);
805 sv_setpvn(sv, "sub VIM::perldo {", sizeof("sub VIM::perldo {") - 1);
806 sv_catpvn(sv, (char *)eap->arg, length);
807 sv_catpvn(sv, "}", 1);
808 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
810 str = SvPV(GvSV(PL_errgv), length);
814 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
819 for (i = eap->line1; i <= eap->line2; i++)
821 sv_setpv(GvSV(PL_defgv), (char *)ml_get(i));
823 perl_call_pv("VIM::perldo", G_SCALAR | G_EVAL);
824 str = SvPV(GvSV(PL_errgv), length);
830 if (replace_line(&i, &eap->line2) != OK)
841 update_screen(NOT_VALID);
846 msg_split((char_u *)str, highlight_attr[HLF_E]);
852 int win_valid(win_T *w) { return TRUE; }
853 int win_count() { return 1; }
854 win_T *win_find_nr(int n) { return curwin; }
858 XS(XS_VIM_SetOption);
859 XS(XS_VIM_DoCommand);
863 XS(XS_VIWIN_DESTROY);
865 XS(XS_VIWIN_SetHeight);
867 XS(XS_VIBUF_DESTROY);
880 char *file = __FILE__;
882 /* DynaLoader is a special case */
883 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
884 newXS("VIM::bootstrap", boot_VIM, file);
887 typedef win_T * VIWIN;
888 typedef buf_T * VIBUF;
890 MODULE = VIM PACKAGE = VIM
907 id = syn_name2id((char_u *)hl);
909 attr = syn_id2attr(id);
911 msg_split((char_u *)text, attr);
920 do_set((char_u *)line, 0);
921 update_screen(NOT_VALID);
929 do_cmdline_cmd((char_u *)line);
938 value = eval_to_string((char_u *)str, (char_u **)0, TRUE);
941 XPUSHs(sv_2mortal(newSViv(0)));
942 XPUSHs(sv_2mortal(newSVpv("", 0)));
946 XPUSHs(sv_2mortal(newSViv(1)));
947 XPUSHs(sv_2mortal(newSVpv((char *)value, 0)));
961 if (GIMME == G_SCALAR)
964 for (vimbuf = firstbuf; vimbuf; vimbuf = vimbuf->b_next)
967 XPUSHs(sv_2mortal(newSViv(i)));
971 for (vimbuf = firstbuf; vimbuf; vimbuf = vimbuf->b_next)
972 XPUSHs(newBUFrv(newSV(0), vimbuf));
977 for (i = 0; i < items; i++)
987 pat = (char_u *)SvPV(sv, len);
989 b = buflist_findpat(pat, pat+len, FALSE, FALSE);
995 vimbuf = buflist_findnr(b);
997 XPUSHs(newBUFrv(newSV(0), vimbuf));
1012 if (GIMME == G_SCALAR)
1013 XPUSHs(sv_2mortal(newSViv(win_count())));
1016 for (vimwin = firstwin; vimwin != NULL; vimwin = W_NEXT(vimwin))
1017 XPUSHs(newWINrv(newSV(0), vimwin));
1022 for (i = 0; i < items; i++)
1025 vimwin = win_find_nr(w);
1027 XPUSHs(newWINrv(newSV(0), vimwin));
1031 MODULE = VIM PACKAGE = VIWIN
1039 win->w_perl_private = 0;
1046 if (!win_valid(win))
1048 RETVAL = newBUFrv(newSV(0), win->w_buffer);
1053 SetHeight(win, height)
1061 if (!win_valid(win))
1065 win_setheight(height);
1076 if (!win_valid(win))
1078 PUSHs(sv_2mortal(newSViv(win->w_cursor.lnum)));
1079 PUSHs(sv_2mortal(newSViv(win->w_cursor.col)));
1085 if (!win_valid(win))
1089 win->w_cursor.lnum = lnum;
1090 win->w_cursor.col = col;
1091 check_cursor(); /* put cursor on an existing line */
1092 update_screen(NOT_VALID);
1095 MODULE = VIM PACKAGE = VIBUF
1102 if (buf_valid(vimbuf))
1103 vimbuf->b_perl_private = 0;
1110 if (!buf_valid(vimbuf))
1112 /* No file name returns an empty string */
1113 if (vimbuf->b_fname == NULL)
1114 XPUSHs(sv_2mortal(newSVpv("", 0)));
1116 XPUSHs(sv_2mortal(newSVpv((char *)vimbuf->b_fname, 0)));
1123 if (!buf_valid(vimbuf))
1125 XPUSHs(sv_2mortal(newSViv(vimbuf->b_fnum)));
1132 if (!buf_valid(vimbuf))
1134 XPUSHs(sv_2mortal(newSViv(vimbuf->b_ml.ml_line_count)));
1145 if (buf_valid(vimbuf))
1147 for (i = 1; i < items; i++)
1150 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1152 line = ml_get_buf(vimbuf, lnum, FALSE);
1153 XPUSHs(sv_2mortal(newSVpv((char *)line, 0)));
1167 if (buf_valid(vimbuf))
1170 croak("Usage: VIBUF::Set(vimbuf, lnum, @lines)");
1173 for(i = 2; i < items; i++, lnum++)
1175 line = SvPV(ST(i),PL_na);
1176 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1180 /* set curwin/curbuf for "vimbuf" and save some things */
1181 aucmd_prepbuf(&aco, vimbuf);
1183 if (u_savesub(lnum) == OK)
1185 ml_replace(lnum, (char_u *)line, TRUE);
1186 changed_bytes(lnum, 0);
1189 /* restore curwin/curbuf and a few other things */
1190 aucmd_restbuf(&aco);
1191 /* Careful: autocommands may have made "vimbuf" invalid! */
1201 long i, lnum = 0, count = 0;
1203 if (buf_valid(vimbuf))
1210 else if (items == 3)
1213 count = 1 + SvIV(ST(2)) - lnum;
1224 for (i = 0; i < count; i++)
1226 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1230 /* set curwin/curbuf for "vimbuf" and save some things */
1231 aucmd_prepbuf(&aco, vimbuf);
1233 if (u_savedel(lnum, 1) == OK)
1236 deleted_lines_mark(lnum, 1L);
1237 if (aco.save_buf == curbuf)
1241 /* restore curwin/curbuf and a few other things */
1242 aucmd_restbuf(&aco);
1243 /* Careful: autocommands may have made "vimbuf" invalid! */
1245 update_curbuf(VALID);
1260 if (buf_valid(vimbuf))
1263 croak("Usage: VIBUF::Append(vimbuf, lnum, @lines)");
1266 for (i = 2; i < items; i++, lnum++)
1268 line = SvPV(ST(i),PL_na);
1269 if (lnum >= 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
1273 /* set curwin/curbuf for "vimbuf" and save some things */
1274 aucmd_prepbuf(&aco, vimbuf);
1276 if (u_inssub(lnum + 1) == OK)
1278 ml_append(lnum, (char_u *)line, (colnr_T)0, FALSE);
1279 appended_lines_mark(lnum, 1L);
1282 /* restore curwin/curbuf and a few other things */
1283 aucmd_restbuf(&aco);
1284 /* Careful: autocommands may have made "vimbuf" invalid! */
1286 update_curbuf(VALID);