1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) 1998 Malcolm Beattie */
6 /* Copyright (C) 1999 - 2018 Exim maintainers */
8 /* Modified by PH to get rid of the "na" usage, March 1999.
9 Modified further by PH for general tidying for Exim 4.
10 Threaded Perl support added by Stefan Traby, Nov 2002
14 /* This Perl add-on can be distributed under the same terms as Exim itself. */
15 /* See the file NOTICE for conditions of use and distribution. */
20 #define EXIM_TRUE TRUE
23 #define EXIM_FALSE FALSE
26 #define EXIM_DEBUG DEBUG
34 #define ERRSV (GvSV(errgv))
37 /* Some people like very old perl versions, so avoid any build side-effects. */
44 # define EXTERN_C extern
47 EXTERN_C void boot_DynaLoader(pTHX_ CV *cv);
50 static PerlInterpreter *interp_perl = 0;
59 croak("Usage: Exim::expand_string(string)");
61 str = expand_string(US SvPV(ST(0), len));
62 ST(0) = sv_newmortal();
64 sv_setpv(ST(0), CCS str);
65 else if (!f.expand_string_forcedfail)
66 croak("syntax error in Exim::expand_string argument: %s",
67 expand_string_message);
75 croak("Usage: Exim::debug_write(string)");
76 debug_printf("%s", US SvPV(ST(0), len));
84 croak("Usage: Exim::log_write(string)");
85 log_write(0, LOG_MAIN, "%s", US SvPV(ST(0), len));
88 static void xs_init(pTHX)
90 char *file = __FILE__;
91 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
92 newXS("Exim::expand_string", xs_expand_string, file);
93 newXS("Exim::debug_write", xs_debug_write, file);
94 newXS("Exim::log_write", xs_log_write, file);
98 init_perl(uschar *startup_code)
101 static char *argv[4] = { "exim-perl" };
105 if (opt_perl_taintmode) argv[argc++] = "-T";
106 argv[argc++] = "/dev/null";
109 assert(sizeof(argv)/sizeof(argv[0]) > argc);
111 if (interp_perl) return 0;
112 interp_perl = perl_alloc();
113 perl_construct(interp_perl);
114 perl_parse(interp_perl, xs_init, argc, argv, 0);
115 perl_run(interp_perl);
119 /*********************************************************************/
120 /* These lines by PH added to make "warn" output go to the Exim log; I
121 hope this doesn't break anything. */
124 "$SIG{__WARN__} = sub { my($s) = $_[0];"
126 "Exim::log_write($s) };", 0);
128 perl_eval_sv(sv, G_SCALAR|G_DISCARD|G_KEEPERR);
130 if (SvTRUE(ERRSV)) return US SvPV(ERRSV, len);
131 /*********************************************************************/
133 sv = newSVpv(CS startup_code, 0);
135 perl_eval_sv(sv, G_SCALAR|G_DISCARD|G_KEEPERR);
137 if (SvTRUE(ERRSV)) return US SvPV(ERRSV, len);
139 setlocale(LC_ALL, "C"); /* In case it got changed */
149 perl_destruct(interp_perl);
150 perl_free(interp_perl);
155 call_perl_cat(gstring * yield, uschar **errstrp, uschar *name, uschar **arg)
165 *errstrp = US"the Perl interpreter has not been started";
172 while (*arg != NULL) XPUSHs(newSVpv(CS (*arg++), 0));
174 items = perl_call_pv(CS name, G_SCALAR|G_EVAL);
180 *errstrp = US SvPV(ERRSV, len);
188 str = US SvPV(sv, len);
189 yield = string_catn(yield, str, (int)len);
193 setlocale(LC_ALL, "C"); /* In case it got changed */