tee.c

filter: Statementstmt 85.9% 135 / 157 Branchbran 71.3% 87 / 122 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 79.5% 222 / 279 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/PerlIO/Util.pm ↑ Summary PerlIO-Util.xs »
1 /*
2 :tee - write to files.
3  
4 Usage: open(my $out, '>>:tee', \*STDOUT, \*SOCKET, $file, \$scalar)
5 $out->push_layer(tee => $another);
6 */
7  
8 #include "perlioutil.h"
9  
10 #define TeeOut(f) (PerlIOSelf(f, PerlIOTee)->out)
11 #define TeeArg(f) (PerlIOSelf(f, PerlIOTee)->arg)
12  
13 /* copied from perlio.c */
14 static PerlIO_funcs *
15 10 PerlIO_layer_from_ref(pTHX_ SV* const sv)
16 {
17 dVAR;
18 /*
19 * For any scalar type load the handler which is bundled with perl
20 */
21 10 ▶ if (SvTYPE(sv) < SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(sv) < SVt_PVAV) { 10 0 10
22 10 PerlIO_funcs *f = PerlIO_find_layer(aTHX_ STR_WITH_LEN("scalar"), 1);
23 /* This isn't supposed to happen, since PerlIO::scalar is core,
24 * but could happen anyway in smaller installs or with PAR */
25 10 ▶ if (!f)
Branch: 1 branch
BranchTrueFalseTotal
if (!f) 0 10 10
26 0 PerlIOUtil_warnif(aTHX_ packWARN(WARN_LAYER), "Unknown PerlIO layer \"scalar\"");
27 10 return f;
28 }
29  
30 /*
31 * For other types allow if layer is known but don't try and load it
32 */
33 0 switch (SvTYPE(sv)) {
34 0 case SVt_PVAV:
35 0 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Array"), 0);
36 0 case SVt_PVHV:
37 0 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Hash"), 0);
38 0 case SVt_PVCV:
39 0 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Code"), 0);
40 0 case SVt_PVGV:
41 0 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Glob"), 0);
42 0 default:
43 0 return NULL;
44 }
45 } /* PerlIO_layer_from_ref() */
46  
47 static PerlIO*
48 2 PerlIO_dup(pTHX_ PerlIO* newfp, PerlIO* const oldfp, CLONE_PARAMS* const params, int const flags){
49 2 ▶ if(PerlIOValid(oldfp)){
Branch: 2 branches
BranchTrueFalseTotal
if(PerlIOValid(oldfp)){ 2 0 2
if(PerlIOValid(oldfp)){ 2 0 2
50 PerlIO* (*my_dup)(pTHX_ PerlIO*, PerlIO*, CLONE_PARAMS*, int);
51  
52 2 my_dup = PerlIOBase(oldfp)->tab->Dup;
53  
54 2 ▶ if(!newfp) newfp = PerlIO_allocate(aTHX);
Branch: 1 branch
BranchTrueFalseTotal
if(!newfp) newfp = PerlIO_allocate(aTHX); 1 1 2
55 2 ▶ if(!my_dup) my_dup = PerlIOBase_dup;
Branch: 1 branch
BranchTrueFalseTotal
if(!my_dup) my_dup = PerlIOBase_dup; 0 2 2
56  
57 2 return my_dup(aTHX_ newfp, oldfp, params, flags);
58 }
59  
60 0 SETERRNO(EBADF, SS_IVCHAN);
61 0 return NULL;
62 }
63  
64 typedef struct {
65 struct _PerlIO base; /* virtual table and flags */
66  
67 SV* arg;
68  
69 PerlIO* out;
70 } PerlIOTee;
71  
72  
73 static PerlIO*
74 20 PerlIOTee_open(pTHX_ PerlIO_funcs* const self, PerlIO_list_t* const layers, IV const n,
75 const char* const mode, int const fd, int const imode, int const perm,
76 PerlIO* f, int const narg, SV** const args){
77 SV* arg;
78  
79 20 ▶ if(!(PerlIOUnix_oflags(mode) & O_WRONLY)){ /* cannot open:tee for reading */
Branch: 1 branch
BranchTrueFalseTotal
if(!(PerlIOUnix_oflags(mode) & O_WRONLY)){ /* cannot open:tee for reading */ 1 19 20
80 1 SETERRNO(EINVAL, LIB_INVARG);
81 1 return NULL;
82 }
83  
84 19 f = PerlIOUtil_openn(aTHX_ NULL, layers, n, mode,
85 fd, imode, perm, f, 1, args);
86  
87 19 ▶ if(!f){
Branch: 1 branch
BranchTrueFalseTotal
if(!f){ 1 18 19
88 1 return NULL;
89 }
90  
91 18 ▶ if(narg > 1){
Branch: 1 branch
BranchTrueFalseTotal
if(narg > 1){ 17 1 18
92 int i;
93 34 ▶ for(i = 1; i < narg; i++){
Branch: 1 branch
BranchTrueFalseTotal
for(i = 1; i < narg; i++){ 19 15 34
94 19 ▶ if(!PerlIO_push(aTHX_ f, self, mode, args[i])){
Branch: 1 branch
BranchTrueFalseTotal
if(!PerlIO_push(aTHX_ f, self, mode, args[i])){ 2 17 19
95 2 PerlIO_close(f);
96 2 return NULL;
97 }
98 }
99 }
100  
101 16 arg = PerlIOArg;
102 16 ▶ if(arg && SvOK(arg)){
Branch: 2 branches
BranchTrueFalseTotal
if(arg && SvOK(arg)){ 16 0 16
if(arg && SvOK(arg)){ 1 15 16
103 1 ▶ if(!PerlIO_push(aTHX_ f, self, mode, arg)){
Branch: 1 branch
BranchTrueFalseTotal
if(!PerlIO_push(aTHX_ f, self, mode, arg)){ 1 0 1
104 1 PerlIO_close(f);
105 1 return NULL;
106 }
107 }
108  
109 15 return f;
110 }
111  
112  
113 static SV*
114 13 parse_fname(pTHX_ SV* const arg, const char** const mode){
115 STRLEN len;
116 13 const char* pv = SvPV_const(arg, len);
117  
118 13 switch (*pv){
119 2 case '>':
120 2 pv++;
121 2 len--;
122 2 ▶ if(*pv == '>'){ /* ">> file" */
Branch: 1 branch
BranchTrueFalseTotal
if(*pv == '>'){ /* ">> file" */ 1 1 2
123 1 pv++;
124 1 len--;
125 1 *mode = "a";
126 }
127 else{ /* "> file" */
128 1 *mode = "w";
129 }
130 4 ▶ while(isSPACE(*pv)){
Branch: 1 branch
BranchTrueFalseTotal
while(isSPACE(*pv)){ 2 2 4
131 2 pv++;
132 2 len--;
133 }
134 2 break;
135  
136 2 case '+':
137 case '<':
138 case '|':
139 2 return NULL;
140 11 default:
141 /* noop */;
142 }
143 11 return newSVpvn(pv, len);
144 }
145  
146 static IO*
147 52 sv_2io_or_null(pTHX_ SV* sv){
148 52 ▶ if(SvROK(sv)) sv = SvRV(sv);
Branch: 1 branch
BranchTrueFalseTotal
if(SvROK(sv)) sv = SvRV(sv); 25 27 52
149  
150 52 switch(SvTYPE(sv)){
151 5 case SVt_PVGV:
152 5 ▶ return GvIO(sv);
Branch: 4 branches
BranchTrueFalseTotal
return GvIO(sv); 5 0 5
return GvIO(sv); 0 5 5
return GvIO(sv); 0 0 0
return GvIO(sv); 5 0 5
153 0 case SVt_PVIO:
154 0 return (IO*)sv;
155 47 default:
156 NOOP;
157 }
158 47 return NULL;
159 }
160  
161 static IV
162 31 PerlIOTee_pushed(pTHX_ PerlIO* const f, const char* mode, SV* const arg, PerlIO_funcs* const tab){
163 PerlIO* nx;
164 IO* io;
165 31 ▶ PerlIOTee* const proto = (mode && !arg) ? (PerlIOTee*)(mode) : NULL; /* dup */
Branch: 2 branches
BranchTrueFalseTotal
PerlIOTee* const proto = (mode && !arg) ? (PerlIOTee*)(mode) : NULL; /* dup */ 21 10 31
PerlIOTee* const proto = (mode && !arg) ? (PerlIOTee*)(mode) : NULL; /* dup */ 1 20 21
166  
167 PERL_UNUSED_ARG(tab);
168  
169 31 ▶ if(!(PerlIOValid(f) && (nx = PerlIONext(f)) && PerlIOValid(nx))){
Branch: 5 branches
BranchTrueFalseTotal
if(!(PerlIOValid(f) && (nx = PerlIONext(f)) && PerlIOValid(nx))){ 31 0 31
if(!(PerlIOValid(f) && (nx = PerlIONext(f)) && PerlIOValid(nx))){ 31 0 31
if(!(PerlIOValid(f) && (nx = PerlIONext(f)) && PerlIOValid(nx))){ 31 0 31
if(!(PerlIOValid(f) && (nx = PerlIONext(f)) && PerlIOValid(nx))){ 31 0 31
if(!(PerlIOValid(f) && (nx = PerlIONext(f)) && PerlIOValid(nx))){ 1 30 31
170 1 SETERRNO(EBADF, SS_IVCHAN);
171 1 return -1;
172 }
173  
174  
175 30 ▶ if(!IOLflag(nx, PERLIO_F_CANWRITE)) goto cannot_tee;
Branch: 1 branch
BranchTrueFalseTotal
if(!IOLflag(nx, PERLIO_F_CANWRITE)) goto cannot_tee; 1 29 30
176  
177 29 ▶ if(arg && !SvOK(arg)){
Branch: 2 branches
BranchTrueFalseTotal
if(arg && !SvOK(arg)){ 28 1 29
if(arg && !SvOK(arg)){ 1 27 28
178 1 SETERRNO(EINVAL, LIB_INVARG);
179 1 return -1;
180 }
181  
182 28 ▶ if(proto){ /* dup */
Branch: 1 branch
BranchTrueFalseTotal
if(proto){ /* dup */ 1 27 28
183 1 TeeOut(f) = proto->out;
184 1 TeeArg(f) = proto->arg;
185 }
186 27 ▶ else if((io = sv_2io_or_null(aTHX_ arg))){ /* pushed \*FILEHANDLE */
Branch: 1 branch
BranchTrueFalseTotal
else if((io = sv_2io_or_null(aTHX_ arg))){ /* pushed \*FILEHANDLE */ 3 24 27
187 3 ▶ if(!( IoOFP(io) && IOLflag(IoOFP(io), PERLIO_F_CANWRITE) )){
Branch: 2 branches
BranchTrueFalseTotal
if(!( IoOFP(io) && IOLflag(IoOFP(io), PERLIO_F_CANWRITE) )){ 1 2 3
if(!( IoOFP(io) && IOLflag(IoOFP(io), PERLIO_F_CANWRITE) )){ 0 2 2
188 1 cannot_tee:
189 2 SETERRNO(EBADF, SS_IVCHAN);
190 2 return -1;
191 }
192  
193 2 TeeArg(f) = SvREFCNT_inc_simple_NN( arg );
194 2 TeeOut(f) = IoOFP(io);
195 }
196 else{
197 24 PerlIO_list_t* const layers = PL_def_layerlist;
198 24 PerlIO_funcs* tab = NULL;
199  
200 24 ▶ TAINT_IF(SvTAINTED(arg));
Branch: 2 branches
BranchTrueFalseTotal
TAINT_IF(SvTAINTED(arg)); 0 24 24
TAINT_IF(SvTAINTED(arg)); 0 0 0
201 24 ▶ TAINT_PROPER(":tee");
Branch: 1 branch
BranchTrueFalseTotal
TAINT_PROPER(":tee"); 0 24 24
202  
203 24 ▶ if(SvPOK(arg) && SvCUR(arg) > 1){
Branch: 2 branches
BranchTrueFalseTotal
if(SvPOK(arg) && SvCUR(arg) > 1){ 14 10 24
if(SvPOK(arg) && SvCUR(arg) > 1){ 13 1 14
204 13 TeeArg(f) = parse_fname(aTHX_ arg, &mode);
205 13 ▶ if(!TeeArg(f)){
Branch: 1 branch
BranchTrueFalseTotal
if(!TeeArg(f)){ 2 11 13
206 2 SETERRNO(EINVAL, LIB_INVARG);
207 2 return -1;
208 }
209 }
210 else{
211 11 TeeArg(f) = newSVsv(arg);
212 }
213  
214 22 ▶ if( SvROK(TeeArg(f)) ){
Branch: 1 branch
BranchTrueFalseTotal
if( SvROK(TeeArg(f)) ){ 10 12 22
215 10 tab = PerlIO_layer_from_ref(aTHX_ SvRV(TeeArg(f)));
216 }
217  
218 22 ▶ if(!mode){
Branch: 1 branch
BranchTrueFalseTotal
if(!mode){ 2 20 22
219 2 mode = "w";
220 }
221  
222 22 TeeOut(f) = PerlIOUtil_openn(aTHX_ tab, layers,
223 22 layers->cur, mode, -1, 0, 0, NULL, 1, &(TeeArg(f)));
224  
225 /*dump_perlio(aTHX_ TeeOut(f), 0);*/
226 }
227 25 ▶ if(!PerlIOValid(TeeOut(f))){
Branch: 2 branches
BranchTrueFalseTotal
if(!PerlIOValid(TeeOut(f))){ 23 2 25
if(!PerlIOValid(TeeOut(f))){ 0 23 23
228 2 return -1; /* failure */
229 }
230  
231 23 PerlIOBase(f)->flags = PerlIOBase(nx)->flags;
232  
233 23 IOLflag_on(TeeOut(f),
234 PerlIOBase(f)->flags & (PERLIO_F_UTF8 | PERLIO_F_LINEBUF | PERLIO_F_UNBUF));
235  
236 23 return 0;
237 }
238  
239 static IV
240 31 PerlIOTee_popped(pTHX_ PerlIO* const f){
241 #if 0
242 printf("#popped:%s(my_perl=%p, f=%p) arg=%p(%d), out=%p\n",
243 PerlIOBase(f)->tab->name, my_perl, f,
244 TeeArg(f), (TeeArg(f) ? (int)SvREFCNT(TeeArg(f)) : 0), TeeOut(f));
245 #endif
246  
247 31 ▶ if(TeeArg(f)){
Branch: 1 branch
BranchTrueFalseTotal
if(TeeArg(f)){ 25 6 31
248 25 ▶ if(sv_2io_or_null(aTHX_ TeeArg(f)) == NULL){
Branch: 1 branch
BranchTrueFalseTotal
if(sv_2io_or_null(aTHX_ TeeArg(f)) == NULL){ 23 2 25
249 23 PerlIO_close(TeeOut(f));
250 }
251 25 ▶ if(SvREFCNT(TeeArg(f)) > 0) /* for 5.8.8 */
Branch: 1 branch
BranchTrueFalseTotal
if(SvREFCNT(TeeArg(f)) > 0) /* for 5.8.8 */ 25 0 25
252 25 SvREFCNT_dec(TeeArg(f));
253  
254 }
255 6 ▶ else if(TeeOut(f)){ /* dup()-ed fp */
Branch: 1 branch
BranchTrueFalseTotal
else if(TeeOut(f)){ /* dup()-ed fp */ 0 6 6
256 0 PerlIO_close(TeeOut(f));
257 }
258 31 return 0;
259 }
260  
261 static IV
262 7 PerlIOTee_binmode(pTHX_ PerlIO* const f){
263 7 ▶ if(!PerlIOValid(f)){
Branch: 2 branches
BranchTrueFalseTotal
if(!PerlIOValid(f)){ 7 0 7
if(!PerlIOValid(f)){ 0 7 7
264 0 return -1;
265 }
266  
267 7 PerlIOBase_binmode(aTHX_ f); /* remove PERLIO_F_UTF8 */
268  
269 7 PerlIO_binmode(aTHX_ PerlIONext(f), '>', O_BINARY, NULL);
270  
271 /* warn("Tee_binmode %s", PerlIOBase(f)->tab->name); */
272 /* there is a case where an unknown layer is supplied */
273 7 ▶ if( PerlIOBase(f)->tab != &PerlIO_tee ){
Branch: 1 branch
BranchTrueFalseTotal
if( PerlIOBase(f)->tab != &PerlIO_tee ){ 0 7 7
274 #if 0 /* May, 2008 */
275 PerlIO* t = PerlIONext(f);
276 int n = 0;
277 int ok = 0;
278  
279 while(PerlIOValid(t)){
280 if(PerlIOBase(t)->tab == &PerlIO_tee){
281 n++;
282 if(PerlIO_binmode(aTHX_ TeeOut(t), '>'/*not used*/,
283 O_BINARY, NULL)){
284 ok++;
285 }
286 }
287  
288 t = PerlIONext(t);
289 }
290 return n == ok ? 0 : -1;
291 #endif
292 0 return 0;
293 }
294  
295 7 return PerlIO_binmode(aTHX_ TeeOut(f), '>'/*not used*/,
296 7 ▶ O_BINARY, NULL) ? 0 : -1;
Branch: 1 branch
BranchTrueFalseTotal
O_BINARY, NULL) ? 0 : -1; 7 0 7
297 }
298  
299 static SV*
300 9 PerlIOTee_getarg(pTHX_ PerlIO* const f, CLONE_PARAMS* const param, int const flags){
301 PERL_UNUSED_ARG(flags);
302  
303 9 return PerlIO_sv_dup(aTHX_ TeeArg(f), param);
304 }
305  
306 static PerlIO*
307 1 PerlIOTee_dup(pTHX_ PerlIO* f, PerlIO* const o, CLONE_PARAMS* const param, int const flags){
308 #if 0
309 printf("#dup:%s (my_perl=%p, f=%p, o=%p, {proto_perl=%p,flags=0x%x}, flags=%d)\n",
310 PerlIOBase(o)->tab->name, my_perl, f, o, param->proto_perl,
311 (unsigned)param->flags, flags);
312 #endif
313  
314 1 f = PerlIO_dup(aTHX_ f, PerlIONext(o), param, flags);
315  
316 1 ▶ if(f){
Branch: 1 branch
BranchTrueFalseTotal
if(f){ 1 0 1
317 PerlIOTee proto;
318 #if 0
319 IO* io;
320 proto.arg = PerlIOTee_getarg(aTHX_ o, param, flags);
321 if((io = sv_2io_or_null(aTHX_ proto.arg))){
322 proto.out = IoOFP(io);
323 }
324 else{
325 proto.out = PerlIO_fdupopen(aTHX_ TeeOut(o), param, flags);
326 }
327 #else
328 1 ▶ if(!SvROK(TeeArg(o))){
Branch: 1 branch
BranchTrueFalseTotal
if(!SvROK(TeeArg(o))){ 1 0 1
329 1 proto.arg = PerlIO_sv_dup(aTHX_ TeeArg(o), param);
330 //SvREFCNT_inc_simple_void_NN(proto.arg);
331 }
332 else{
333 0 proto.arg = NULL;
334 }
335  
336 1 proto.out = PerlIO_dup(aTHX_ NULL, TeeOut(o), param, flags);
337 #endif
338  
339 #if 0
340 printf("# newarg=%p(%d), oldarg=%p(%d)\n",
341 proto.arg, (int)(proto.arg ? SvREFCNT(proto.arg) : 0),
342 TeeArg(o), (int)(TeeArg(o) ? SvREFCNT(TeeArg(o)) : 0) );
343 #endif
344 1 f = PerlIO_push(aTHX_ f, PerlIOBase(o)->tab, (const char*)&proto, NULL);
345 }
346  
347 1 return f;
348 }
349  
350 static SSize_t
351 35 PerlIOTee_write(pTHX_ PerlIO* const f, const void* const vbuf, Size_t const count){
352 35 ▶ if(PerlIO_write(TeeOut(f), vbuf, count) != (SSize_t)count){
Branch: 1 branch
BranchTrueFalseTotal
if(PerlIO_write(TeeOut(f), vbuf, count) != (SSize_t)count){ 1 34 35
353 1 PerlIOUtil_warnif(aTHX_ packWARN(WARN_IO), "Failed to write to tee-out");
354 }
355  
356 34 return PerlIO_write(PerlIONext(f), vbuf, count);
357 }
358  
359 static IV
360 44 PerlIOTee_flush(pTHX_ PerlIO* const f){
361 44 ▶ if(TeeOut(f) && PerlIO_flush(TeeOut(f)) != 0){
Branch: 2 branches
BranchTrueFalseTotal
if(TeeOut(f) && PerlIO_flush(TeeOut(f)) != 0){ 44 0 44
if(TeeOut(f) && PerlIO_flush(TeeOut(f)) != 0){ 2 42 44
362 2 PerlIOUtil_warnif(aTHX_ packWARN(WARN_IO), "Failed to flush tee-out");
363 }
364  
365 43 return PerlIO_flush(PerlIONext(f));
366 }
367  
368 static IV
369 4 PerlIOTee_seek(pTHX_ PerlIO* const f, Off_t const offset, int const whence){
370 4 ▶ if(PerlIO_seek(TeeOut(f), offset, whence) != 0){
Branch: 1 branch
BranchTrueFalseTotal
if(PerlIO_seek(TeeOut(f), offset, whence) != 0){ 1 3 4
371 1 PerlIOUtil_warnif(aTHX_ packWARN(WARN_IO), "Failed to seek tee-out");
372 }
373  
374 3 return PerlIO_seek(PerlIONext(f), offset, whence);
375 }
376  
377 static Off_t
378 8 PerlIOTee_tell(pTHX_ PerlIO* const f){
379 8 PerlIO* const nx = PerlIONext(f);
380  
381 8 return PerlIO_tell(nx);
382 }
383  
384 PerlIO*
385 0 PerlIOTee_teeout(pTHX_ const PerlIO* const f){
386 0 ▶ return PerlIOValid(f) ? TeeOut(f) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return PerlIOValid(f) ? TeeOut(f) : NULL; 0 0 0
return PerlIOValid(f) ? TeeOut(f) : NULL; 0 0 0
387 }
388  
389  
390 PERLIO_FUNCS_DECL(PerlIO_tee) = {
391 sizeof(PerlIO_funcs),
392 "tee",
393 sizeof(PerlIOTee),
394 PERLIO_K_BUFFERED | PERLIO_K_RAW | PERLIO_K_MULTIARG,
395 PerlIOTee_pushed,
396 PerlIOTee_popped,
397 PerlIOTee_open,
398 PerlIOTee_binmode,
399 PerlIOTee_getarg,
400 NULL, /* fileno */
401 PerlIOTee_dup,
402 NULL, /* read */
403 NULL, /* unread */
404 PerlIOTee_write,
405 PerlIOTee_seek,
406 PerlIOTee_tell,
407 NULL, /* close */
408 PerlIOTee_flush,
409 NULL, /* fill */
410 NULL, /* eof */
411 NULL, /* error */
412 NULL, /* clearerror */
413 NULL, /* setlinebuf */
414 NULL, /* get_base */
415 NULL, /* bufsiz */
416 NULL, /* get_ptr */
417 NULL, /* get_cnt */
418 NULL, /* set_ptrcnt */
419 };
420  
421  
« blib/lib/PerlIO/Util.pm ↑ Summary PerlIO-Util.xs »