4 * Author : Paul Marquess
5 * Date : 11th November 2001
8 * Copyright (c) 1995-2001 Paul Marquess. All rights reserved.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the same terms as Perl itself.
14 #define PERL_NO_GET_CONTEXT
22 /* Internal defines */
23 #define PERL_MODULE(s) IoBOTTOM_NAME(s)
24 #define PERL_OBJECT(s) IoTOP_GV(s)
25 #define FILTER_ACTIVE(s) IoLINES(s)
26 #define BUF_OFFSET(sv) IoPAGE_LEN(sv)
27 #define CODE_REF(sv) IoPAGE(sv)
29 #define SET_LEN(sv,len) \
30 do { SvPVX(sv)[len] = '\0'; SvCUR_set(sv, len); } while (0)
35 #define MY_CXT_KEY "Filter::Util::Call::_guts" XS_VERSION
44 #define fdebug (MY_CXT.x_fdebug)
45 #define current_idx (MY_CXT.x_current_idx)
49 filter_call(pTHX_ int idx, SV *buf_sv, int maxlen)
52 SV *my_sv = FILTER_DATA(idx);
59 warn("**** In filter_call - maxlen = %d, out len buf = %d idx = %d my_sv = %d [%s]\n",
60 maxlen, SvCUR(buf_sv), idx, SvCUR(my_sv), SvPVX(my_sv) ) ;
64 /* anything left from last time */
65 if ((n = SvCUR(my_sv))) {
67 out_ptr = SvPVX(my_sv) + BUF_OFFSET(my_sv) ;
72 warn("BLOCK(%d): size = %d, maxlen = %d\n",
75 sv_catpvn(buf_sv, out_ptr, maxlen > n ? n : maxlen );
77 BUF_OFFSET(my_sv) = 0 ;
81 BUF_OFFSET(my_sv) += maxlen ;
82 SvCUR_set(my_sv, n - maxlen) ;
88 if ((p = ninstr(out_ptr, out_ptr + n - 1, nl, nl))) {
90 sv_catpvn(buf_sv, out_ptr, p - out_ptr + 1);
92 n = n - (p - out_ptr + 1);
93 BUF_OFFSET(my_sv) += (p - out_ptr + 1);
96 warn("recycle %d - leaving %d, returning %d [%s]",
97 idx, n, SvCUR(buf_sv), SvPVX(buf_sv)) ;
101 else /* no EOL, so append the complete buffer */
102 sv_catpvn(buf_sv, out_ptr, n) ;
109 BUF_OFFSET(my_sv) = 0 ;
111 if (FILTER_ACTIVE(my_sv))
117 warn("gonna call %s::filter\n", PERL_MODULE(my_sv)) ;
122 SAVEINT(current_idx) ; /* save current idx */
125 SAVESPTR(DEFSV) ; /* save $_ */
126 /* make $_ use our buffer */
127 DEFSV = sv_2mortal(newSVpv("", 0)) ;
131 if (CODE_REF(my_sv)) {
132 /* if (SvROK(PERL_OBJECT(my_sv)) && SvTYPE(SvRV(PERL_OBJECT(my_sv))) == SVt_PVCV) { */
133 count = perl_call_sv((SV*)PERL_OBJECT(my_sv), G_SCALAR);
136 XPUSHs((SV*)PERL_OBJECT(my_sv)) ;
140 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 = %d [%s]\n",
153 n, SvCUR(DEFSV), SvPVX(DEFSV) ) ;
155 sv_setpvn(my_sv, SvPVX(DEFSV), SvCUR(DEFSV)) ;
162 n = FILTER_READ(idx + 1, my_sv, maxlen) ;
166 /* Either EOF or an error */
169 warn ("filter_read %d returned %d , returning %d\n", idx, n,
170 (SvCUR(buf_sv)>0) ? SvCUR(buf_sv) : n);
172 /* PERL_MODULE(my_sv) ; */
173 /* PERL_OBJECT(my_sv) ; */
174 filter_del(filter_call);
176 /* If error, return the code */
180 /* return what we have so far else signal eof */
181 return (SvCUR(buf_sv)>0) ? SvCUR(buf_sv) : n;
189 MODULE = Filter::Util::Call PACKAGE = Filter::Util::Call
194 #define IDX current_idx
202 SV * buffer = DEFSV ;
204 RETVAL = FILTER_READ(IDX + 1, buffer, size) ;
213 real_import(object, perlmodule, coderef)
221 (void)SvPOK_only(sv) ;
222 filter_add(filter_call, sv) ;
224 PERL_MODULE(sv) = savepv(perlmodule) ;
225 PERL_OBJECT(sv) = (GV*) newSVsv(object) ;
226 FILTER_ACTIVE(sv) = TRUE ;
228 CODE_REF(sv) = coderef ;
238 if (PL_rsfp_filters && IDX <= av_len(PL_rsfp_filters) && FILTER_DATA(IDX) && FILTER_ACTIVE(FILTER_DATA(IDX)))
239 FILTER_ACTIVE(FILTER_DATA(IDX)) = FALSE ;
244 unimport(package="$Package", ...)
247 filter_del(filter_call);
254 /* temporary hack to control debugging in toke.c */
256 filter_add(NULL, (fdebug) ? (SV*)"1" : (SV*)"0");