4 * Author : Paul Marquess
5 * Date : 2014-12-09 02:48:44 rurban
8 * Copyright (c) 1995-2011 Paul Marquess. All rights reserved.
9 * Copyright (c) 2011-2014 Reini Urban. All rights reserved.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the same terms as Perl itself.
15 #define PERL_NO_GET_CONTEXT
23 /* Internal defines */
24 #define PERL_MODULE(s) IoBOTTOM_NAME(s)
25 #define PERL_OBJECT(s) IoTOP_GV(s)
26 #define FILTER_ACTIVE(s) IoLINES(s)
27 #define BUF_OFFSET(sv) IoPAGE_LEN(sv)
28 #define CODE_REF(sv) IoPAGE(sv)
29 #ifndef PERL_FILTER_EXISTS
30 # define PERL_FILTER_EXISTS(i) (PL_rsfp_filters && (i) <= av_len(PL_rsfp_filters))
33 #define SET_LEN(sv,len) \
34 do { SvPVX(sv)[len] = '\0'; SvCUR_set(sv, len); } while (0)
39 #define MY_CXT_KEY "Filter::Util::Call::_guts" XS_VERSION
48 #define fdebug (MY_CXT.x_fdebug)
49 #define current_idx (MY_CXT.x_current_idx)
53 filter_call(pTHX_ int idx, SV *buf_sv, int maxlen)
56 SV *my_sv = FILTER_DATA(idx);
57 const char *nl = "\n";
63 warn("**** In filter_call - maxlen = %d, out len buf = %" IVdf " idx = %d my_sv = %" IVdf " [%s]\n",
64 maxlen, (IV)SvCUR(buf_sv), idx, (IV)SvCUR(my_sv), SvPVX(my_sv) ) ;
68 /* anything left from last time */
69 if ((n = SvCUR(my_sv))) {
71 out_ptr = SvPVX(my_sv) + BUF_OFFSET(my_sv) ;
76 warn("BLOCK(%d): size = %d, maxlen = %d\n",
79 sv_catpvn(buf_sv, out_ptr, maxlen > n ? n : maxlen );
81 BUF_OFFSET(my_sv) = 0 ;
85 BUF_OFFSET(my_sv) += maxlen ;
86 SvCUR_set(my_sv, n - maxlen) ;
92 if ((p = ninstr(out_ptr, out_ptr + n, nl, nl + 1))) {
94 sv_catpvn(buf_sv, out_ptr, p - out_ptr + 1);
96 n = n - (p - out_ptr + 1);
97 BUF_OFFSET(my_sv) += (p - out_ptr + 1);
100 warn("recycle %d - leaving %d, returning %" IVdf " [%s]",
101 idx, n, (IV)SvCUR(buf_sv), SvPVX(buf_sv)) ;
103 return SvCUR(buf_sv);
105 else /* no EOL, so append the complete buffer */
106 sv_catpvn(buf_sv, out_ptr, n) ;
113 BUF_OFFSET(my_sv) = 0 ;
115 if (FILTER_ACTIVE(my_sv))
121 warn("gonna call %s::filter\n", PERL_MODULE(my_sv)) ;
126 SAVEINT(current_idx) ; /* save current idx */
129 SAVE_DEFSV ; /* save $_ */
130 /* make $_ use our buffer */
131 DEFSV_set(newSVpv("", 0)) ;
134 if (CODE_REF(my_sv)) {
135 /* if (SvROK(PERL_OBJECT(my_sv)) && SvTYPE(SvRV(PERL_OBJECT(my_sv))) == SVt_PVCV) { */
136 count = perl_call_sv((SV*)PERL_OBJECT(my_sv), G_SCALAR);
139 XPUSHs((SV*)PERL_OBJECT(my_sv)) ;
141 count = perl_call_method("filter", G_SCALAR);
146 croak("Filter::Util::Call - %s::filter returned %d values, 1 was expected \n",
147 PERL_MODULE(my_sv), count ) ;
152 warn("status = %d, length op buf = %" IVdf " [%s]\n",
153 n, (IV)SvCUR(DEFSV), SvPVX(DEFSV) ) ;
155 sv_setpvn(my_sv, SvPVX(DEFSV), SvCUR(DEFSV)) ;
164 n = FILTER_READ(idx + 1, my_sv, maxlen) ;
168 /* Either EOF or an error */
171 warn ("filter_read %d returned %d , returning %" IVdf "\n", idx, n,
172 (SvCUR(buf_sv)>0) ? (IV)SvCUR(buf_sv) : (IV)n);
174 /* PERL_MODULE(my_sv) ; */
175 /* PERL_OBJECT(my_sv) ; */
176 filter_del(filter_call);
178 /* If error, return the code */
182 /* return what we have so far else signal eof */
183 return (SvCUR(buf_sv)>0) ? (int)SvCUR(buf_sv) : n;
191 MODULE = Filter::Util::Call PACKAGE = Filter::Util::Call
196 #define IDX current_idx
204 SV * buffer = DEFSV ;
206 RETVAL = FILTER_READ(IDX + 1, buffer, size) ;
215 real_import(object, perlmodule, coderef)
223 (void)SvPOK_only(sv) ;
224 filter_add(filter_call, sv) ;
226 PERL_MODULE(sv) = savepv(perlmodule) ;
227 PERL_OBJECT(sv) = (GV*) newSVsv(object) ;
228 FILTER_ACTIVE(sv) = TRUE ;
230 CODE_REF(sv) = coderef ;
240 if (PERL_FILTER_EXISTS(IDX) && FILTER_DATA(IDX) && FILTER_ACTIVE(FILTER_DATA(IDX)))
241 FILTER_ACTIVE(FILTER_DATA(IDX)) = FALSE ;
246 unimport(package="$Package", ...)
249 PERL_UNUSED_VAR(package);
250 filter_del(filter_call);
261 /* temporary hack to control debugging in toke.c */
263 filter_add(NULL, (fdebug) ? (SV*)"1" : (SV*)"0");