1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) 1998 Malcolm Beattie */
7 /* Modified by PH to get rid of the "na" usage, March 1999.
8 Modified further by PH for general tidying for Exim 4.
9 Threaded Perl support added by Stefan Traby, Nov 2002
13 /* This Perl add-on can be distributed under the same terms as Exim itself. */
14 /* See the file NOTICE for conditions of use and distribution. */
18 #define EXIM_TRUE TRUE
21 #define EXIM_FALSE FALSE
24 #define EXIM_DEBUG DEBUG
32 #define ERRSV (GvSV(errgv))
35 /* Some people like very old perl versions, so avoid any build side-effects. */
42 # define EXTERN_C extern
45 EXTERN_C void boot_DynaLoader(pTHX_ CV *cv);
48 static PerlInterpreter *interp_perl = 0;
57 croak("Usage: Exim::expand_string(string)");
59 str = expand_string(US SvPV(ST(0), len));
60 ST(0) = sv_newmortal();
62 sv_setpv(ST(0), (const char *) str);
63 else if (!expand_string_forcedfail)
64 croak("syntax error in Exim::expand_string argument: %s",
65 expand_string_message);
73 croak("Usage: Exim::debug_write(string)");
74 debug_printf("%s", US SvPV(ST(0), len));
82 croak("Usage: Exim::log_write(string)");
83 log_write(0, LOG_MAIN, "%s", US SvPV(ST(0), len));
86 static void xs_init(pTHX)
88 char *file = __FILE__;
89 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
90 newXS("Exim::expand_string", xs_expand_string, file);
91 newXS("Exim::debug_write", xs_debug_write, file);
92 newXS("Exim::log_write", xs_log_write, file);
96 init_perl(uschar *startup_code)
99 static char *argv[3] = { "exim-perl", "/dev/null", 0 };
103 if (interp_perl) return 0;
104 interp_perl = perl_alloc();
105 perl_construct(interp_perl);
106 perl_parse(interp_perl, xs_init, argc, argv, 0);
107 perl_run(interp_perl);
111 /*********************************************************************/
112 /* These lines by PH added to make "warn" output go to the Exim log; I
113 hope this doesn't break anything. */
116 "$SIG{__WARN__} = sub { my($s) = $_[0];"
118 "Exim::log_write($s) };", 0);
120 perl_eval_sv(sv, G_SCALAR|G_DISCARD|G_KEEPERR);
122 if (SvTRUE(ERRSV)) return US SvPV(ERRSV, len);
123 /*********************************************************************/
125 sv = newSVpv(CS startup_code, 0);
127 perl_eval_sv(sv, G_SCALAR|G_DISCARD|G_KEEPERR);
129 if (SvTRUE(ERRSV)) return US SvPV(ERRSV, len);
131 setlocale(LC_ALL, "C"); /* In case it got changed */
141 perl_destruct(interp_perl);
142 perl_free(interp_perl);
147 call_perl_cat(uschar *yield, int *sizep, int *ptrp, uschar **errstrp,
148 uschar *name, uschar **arg)
158 *errstrp = US"the Perl interpreter has not been started";
165 while (*arg != NULL) XPUSHs(newSVpv(CS (*arg++), 0));
167 items = perl_call_pv(CS name, G_SCALAR|G_EVAL);
173 *errstrp = US SvPV(ERRSV, len);
181 str = US SvPV(sv, len);
182 yield = string_cat(yield, sizep, ptrp, str, (int)len);
186 setlocale(LC_ALL, "C"); /* In case it got changed */