subfile.xs

filter: Statementstmt 80.3% 90 / 112 Branchbran 69.4% 50 / 72 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 76.0% 140 / 184 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/PerlIO/subfile.pm »
1 /* -*- c -*- */
2 /* subfile.xs
3 *
4 * Copyright (C) 2001-2003, Nicholas Clark
5 *
6 * You may distribute this work under the terms of either the GNU General
7 * Public License or the Artistic License, as specified in perl's README
8 * file.
9 *
10 */
11 #define PERL_NO_GET_CONTEXT
12  
13 #include "EXTERN.h"
14 #include "perl.h"
15 #include "XSUB.h"
16 #include "perliol.h"
17  
18 typedef struct {
19 PerlIOBuf base;
20 Off_t start;
21 Off_t end; /* byte beyond the end. 0 will make file unbounded. */
22 } PerlIOSubfile;
23  
24  
25 static IV
26 14 PerlIOSubfile_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
27 {
28 IV code;
29 14 PerlIOSubfile *s = PerlIOSelf(f,PerlIOSubfile);
30 Off_t new;
31  
32 #if DEBUG_LAYERSUBFILE
33 PerlIO_debug("PerlIOSubfile_seek f=%p offset=%08"UVxf" whence=%d "
34 "s->start=%08"UVxf" s->end=%08"UVxf"\n",
35 f, (UV)offset, whence, (UV)s->start, (UV)s->end);
36 #endif
37  
38 14 ▶ if (whence == SEEK_SET)
Branch: 1 branch
BranchTrueFalseTotal
if (whence == SEEK_SET) 7 7 14
39 7 offset = new = s->start + offset;
40 7 ▶ else if (whence == SEEK_CUR)
Branch: 1 branch
BranchTrueFalseTotal
else if (whence == SEEK_CUR) 4 3 7
41 4 new = PerlIOBuf_tell(aTHX_ f) + offset;
42 3 ▶ else if (whence == SEEK_END) {
Branch: 1 branch
BranchTrueFalseTotal
else if (whence == SEEK_END) { 3 0 3
43 3 offset = new = s->end + offset;
44 3 whence = SEEK_SET;
45 } else
46 0 return -1;
47  
48  
49 14 ▶ if (new < s->start) {
Branch: 1 branch
BranchTrueFalseTotal
if (new < s->start) { 2 12 14
50 #if DEBUG_LAYERSUBFILE
51 PerlIO_debug(" new=%08"UVxf" whence=%d fell off start\n", (UV)new, whence);
52 #endif
53 2 errno = EINVAL;
54 2 return -1;
55 }
56  
57 12 code = PerlIOBuf_seek(aTHX_ f, offset, whence);
58  
59 #if DEBUG_LAYERSUBFILE
60 PerlIO_debug(" new=%08"UVxf" whence=%d code=%d\n", (UV)new, whence,
61 (int) code);
62 #endif
63  
64 assert (PerlIOBuf_tell(aTHX_ f) >= s->start);
65 12 return code;
66 }
67  
68 static Off_t
69 11 PerlIOSubfile_tell (pTHX_ PerlIO *f)
70 {
71 11 PerlIOSubfile *s = PerlIOSelf(f,PerlIOSubfile);
72 11 Off_t real = PerlIOBuf_tell(aTHX_ f);
73  
74 #if DEBUG_LAYERSUBFILE
75 PerlIO_debug("PerlIOSubfile_tell f=%p real=%08"UVxf" return %08"UVxf
76 " s->start=%08"UVxf" s->end=%08"UVxf"\n",
77 f, (UV)real, (UV)(real-s->start), (UV)s->start, (UV)s->end);
78 #endif
79  
80 assert (real >= s->start);
81 assert (s->end == 0 || (real < s->end));
82  
83 11 return real - s->start;
84 }
85  
86 static IV
87 31 PerlIOSubfile_fill(pTHX_ PerlIO *f)
88 {
89 31 PerlIOSubfile *s = PerlIOSelf(f,PerlIOSubfile);
90 31 Off_t real = PerlIOBuf_tell(aTHX_ f);
91  
92 #if DEBUG_LAYERSUBFILE
93 PerlIO_debug("PerlIOSubfile_fill f=%p real=%08"UVxf
94 " s->start=%08"UVxf" s->end=%08"UVxf"\n",
95 f, (UV)real, (UV)s->start, (UV)s->end);
96 #endif
97  
98 31 ▶ if ((s->end == 0) || (real < s->end)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((s->end == 0) || (real < s->end)) { 22 9 31
if ((s->end == 0) || (real < s->end)) { 9 13 22
99 18 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
100 18 IV code = PerlIOBuf_fill(aTHX_ f);
101 SSize_t avail;
102  
103 #if DEBUG_LAYERSUBFILE
104 PerlIO_debug(" code=%-3d avail=%08"UVxf" b->buf=%p b->end=%p\n", code,
105 (UV)(b->end - b->buf), b->buf, b->end);
106 #endif
107 18 ▶ if (code)
Branch: 1 branch
BranchTrueFalseTotal
if (code) 1 17 18
108 1 return code;
109  
110 17 avail = b->end - b->buf;
111 17 ▶ if (s->end && ((real + avail) >= s->end)) {
Branch: 2 branches
BranchTrueFalseTotal
if (s->end && ((real + avail) >= s->end)) { 9 8 17
if (s->end && ((real + avail) >= s->end)) { 9 0 9
112 9 avail = s->end - real;
113 9 b->end = b->buf + avail;
114 #if DEBUG_LAYERSUBFILE
115 PerlIO_debug(" truncate avail=%08"UVxf" b->buf=%p b->end=%p\n", avail,
116 b->buf, b->end);
117 #endif
118 assert (avail > 0);
119 }
120 17 return 0;
121 }
122 13 PerlIOBase(f)->flags |= PERLIO_F_EOF;
123 13 return -1;
124 }
125  
126 static SV *
127 0 PerlIOSubfile_getarg(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
128 {
129 0 PerlIOSubfile *s = PerlIOSelf(f,PerlIOSubfile);
130 0 SV *sv = newSVpvf("start=%08"UVxf",end=%08"UVxf, (UV)s->start, (UV)s->end);
131 0 ▶ return sv ? sv : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
return sv ? sv : &PL_sv_undef; 0 0 0
132 }
133  
134 static IV
135 9 PerlIOSubfile_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg,
136 PerlIO_funcs *tab)
137 {
138 9 IV code = 0;
139 9 PerlIOSubfile *s = PerlIOSelf(f,PerlIOSubfile);
140  
141 STRLEN len; /* This is effectively also a flag: 0 means "no arguments" */
142 const char *argstr;
143 bool is_UV;
144  
145 9 ▶ if (arg && SvOK(arg)) {
Branch: 2 branches
BranchTrueFalseTotal
if (arg && SvOK(arg)) { 9 0 9
if (arg && SvOK(arg)) { 8 1 9
146 8 ▶ if (SvIOK(arg) && !SvPOK(arg))
Branch: 2 branches
BranchTrueFalseTotal
if (SvIOK(arg) && !SvPOK(arg)) 0 8 8
if (SvIOK(arg) && !SvPOK(arg)) 0 0 0
147 0 len = is_UV = 1;
148 else {
149 8 is_UV = 0;
150 8 argstr = SvPV(arg, len);
151 }
152 } else {
153 1 argstr = NULL;
154 1 len = 0;
155 }
156  
157 #if DEBUG_LAYERSUBFILE
158 PerlIO_debug("PerlIOSubfile_pushed f=%p %s %s fl=%08"UVxf" s=%p\n",
159 f,PerlIOBase(f)->tab->name,(mode) ? mode : "(Null)",
160 PerlIOBase(f)->flags, s);
161 if (argstr) {
162 if (is_UV)
163 PerlIO_debug(" arg=%08"UVxf"\n", SvUV(arg));
164 else
165 PerlIO_debug(" len=%d argstr=%.*s\n", (int)len, (int)len, argstr);
166 }
167 #endif
168  
169 9 code = PerlIOBuf_pushed(aTHX_ f,mode,&PL_sv_undef,tab);
170 9 ▶ if (code)
Branch: 1 branch
BranchTrueFalseTotal
if (code) 0 9 9
171 0 return code;
172  
173 9 s->start = PerlIOBuf_tell(aTHX_ f);
174 9 s->end = 0;
175  
176 9 ▶ if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE)
Branch: 1 branch
BranchTrueFalseTotal
if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) 0 9 9
177 0 return -1; /* Not having any of the write stuff. */
178  
179 9 ▶ if (len) {
Branch: 1 branch
BranchTrueFalseTotal
if (len) { 8 1 9
180 8 ▶ if (is_UV) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_UV) { 0 8 8
181 0 s->end = s->start + SvUV(arg);
182 #if DEBUG_LAYERSUBFILE
183 PerlIO_debug(" end is %08"UVxf"\n", (UV)s->end);
184 #endif
185 } else {
186 8 const char *end = argstr + len;
187 dTHX; /* fetch context */
188  
189 3 while (1) {
190 11 const char *comma = memchr (argstr, ',', end - argstr);
191 11 ▶ STRLEN this_len = comma ? (comma - argstr) : (end - argstr);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN this_len = comma ? (comma - argstr) : (end - argstr); 3 8 11
192 11 const char *value = memchr (argstr, '=', this_len);
193  
194 #if DEBUG_LAYERSUBFILE
195 PerlIO_debug(" processing len=%d argstr=%.*s value=%p\n",
196 (int)this_len, (int)this_len, argstr, value);
197 #endif
198  
199 11 ▶ if (value) { /* value points at the '=' sign. */
Branch: 1 branch
BranchTrueFalseTotal
if (value) { /* value points at the '=' sign. */ 11 0 11
200 11 STRLEN name_len = (value - argstr);
201 11 int save_errno = errno;
202 11 int relative = 0;
203 Off_t offset;
204  
205 11 value++;
206 11 ▶ while (isSPACE(*value))
Branch: 1 branch
BranchTrueFalseTotal
while (isSPACE(*value)) 0 11 11
207 0 value++;
208 11 ▶ if (*value == '+') {
Branch: 1 branch
BranchTrueFalseTotal
if (*value == '+') { 2 9 11
209 2 relative = 1;
210 2 value++;
211 9 ▶ } else if (*value == '-') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (*value == '-') { 1 8 9
212 1 relative = -1;
213 1 value++;
214 }
215  
216 11 ▶ if (!isDIGIT(*value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!isDIGIT(*value)) { 0 11 11
217 0 errno = EINVAL;
218 0 return -1;
219 }
220  
221 11 errno = 0;
222 11 offset = Strtoul (value, (char **) &value, 0); /* Guess the base */
223 11 ▶ if (errno)
Branch: 1 branch
BranchTrueFalseTotal
if (errno) 0 11 11
224 0 return -1;
225 11 errno = save_errno;
226  
227 11 ▶ while (isSPACE(*value))
Branch: 1 branch
BranchTrueFalseTotal
while (isSPACE(*value)) 0 11 11
228 0 value++;
229 11 ▶ if (value != (argstr + this_len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (value != (argstr + this_len)) { 0 11 11
230 #if DEBUG_LAYERSUBFILE
231 PerlIO_debug(" failing this_len=%d argstr=%p value=%p, not %p,"
232 " offset=%08"UVxf"\n", (int)this_len, argstr, value,
233 (value + this_len), (UV)offset);
234 #endif
235 0 errno = EINVAL;
236 0 return -1;
237 }
238  
239 11 ▶ if (name_len == 5 && memEQ (argstr, "start", 5)) {
Branch: 2 branches
BranchTrueFalseTotal
if (name_len == 5 && memEQ (argstr, "start", 5)) { 5 6 11
if (name_len == 5 && memEQ (argstr, "start", 5)) { 5 0 5
240 5 ▶ if (relative) {
Branch: 1 branch
BranchTrueFalseTotal
if (relative) { 1 4 5
241 1 IV code = PerlIOBuf_seek(aTHX_ f, relative * offset, SEEK_CUR);
242 1 ▶ if (code)
Branch: 1 branch
BranchTrueFalseTotal
if (code) 0 1 1
243 0 return code;
244 assert (PerlIOBuf_tell(aTHX_ f) == s->start + relative * offset);
245 1 s->start += relative * offset;
246 #if DEBUG_LAYERSUBFILE
247 PerlIO_debug(" rel start now %08"UVxf" %08"UVxf"\n",
248 (UV)s->start, (UV)PerlIOBuf_tell(aTHX_ f));
249 #endif
250 } else {
251 4 IV code = PerlIOBuf_seek(aTHX_ f, offset, SEEK_SET);
252 4 ▶ if (code)
Branch: 1 branch
BranchTrueFalseTotal
if (code) 0 4 4
253 0 return code;
254 assert (PerlIOBuf_tell(aTHX_ f) == offset);
255 4 s->start = offset;
256 #if DEBUG_LAYERSUBFILE
257 PerlIO_debug(" abs start now %08"UVxf" %08"UVxf"\n",
258 (UV)s->start, (UV)PerlIOBuf_tell(aTHX_ f));
259 #endif
260 }
261 6 ▶ } else if (name_len == 3 && memEQ (argstr, "end", 3)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (name_len == 3 && memEQ (argstr, "end", 3)) { 6 0 6
} else if (name_len == 3 && memEQ (argstr, "end", 3)) { 6 0 6
262 6 ▶ if (relative)
Branch: 1 branch
BranchTrueFalseTotal
if (relative) 2 4 6
263 2 s->end = s->start + relative * offset;
264 else
265 4 s->end = offset;
266 #if DEBUG_LAYERSUBFILE
267 PerlIO_debug(" end now %08"UVxf" relative=%d\n", (UV)s->end,
268 relative);
269 #endif
270 } else {
271 0 Perl_warn(aTHX_
272 "perlio: layer :subfile, unregonised argument \"%.*s\"",
273 (int)this_len, argstr);
274 }
275 } else {
276 0 Perl_warn(aTHX_
277 "perlio: layer :subfile, argument \"%.*s\" has no value",
278 (int)this_len, argstr);
279 }
280 11 ▶ if (!comma)
Branch: 1 branch
BranchTrueFalseTotal
if (!comma) 8 3 11
281 8 break;
282 3 argstr = comma + 1;
283 }
284 }
285 }
286 9 return 0;
287 }
288  
289 static SSize_t
290 0 PerlIO_write_fail(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
291 {
292 0 return -1;
293 }
294  
295 PerlIO_funcs PerlIO_subfile = {
296 sizeof(PerlIO_funcs),
297 "subfile",
298 sizeof(PerlIOSubfile),
299 PERLIO_K_BUFFERED,
300 PerlIOSubfile_pushed,
301 PerlIOBase_noop_ok,
302 PerlIOBuf_open,
303 PerlIOBase_binmode,
304 PerlIOSubfile_getarg,
305 PerlIOBase_fileno,
306 PerlIOBuf_dup,
307 PerlIOBuf_read,
308 PerlIOBuf_unread,
309 PerlIO_write_fail,
310 PerlIOSubfile_seek,
311 PerlIOSubfile_tell,
312 PerlIOBuf_close,
313 PerlIOBuf_flush,
314 PerlIOSubfile_fill,
315 PerlIOBase_eof,
316 PerlIOBase_error,
317 PerlIOBase_clearerr,
318 PerlIOBase_setlinebuf,
319 PerlIOBuf_get_base,
320 PerlIOBuf_bufsiz,
321 PerlIOBuf_get_ptr,
322 PerlIOBuf_get_cnt,
323 PerlIOBuf_set_ptrcnt,
324 };
325  
326 MODULE = PerlIO::subfile PACKAGE = PerlIO::subfile
327  
328 PROTOTYPES: DISABLE
329  
330 BOOT:
331 1 PerlIO_define_layer(aTHX_ &PerlIO_subfile);
↑ Summary blib/lib/PerlIO/subfile.pm »