Commit | Line | Data |
---|---|---|
14a5cf38 | 1 | /* |
be3c0a43 | 2 | * perlio.c Copyright (c) 1996-2002, Nick Ing-Simmons You may distribute |
14a5cf38 | 3 | * under the terms of either the GNU General Public License or the |
71200d45 | 4 | * Artistic License, as specified in the README file. |
760ac839 LW |
5 | */ |
6 | ||
14a5cf38 | 7 | /* |
d31a8517 AT |
8 | * Hour after hour for nearly three weary days he had jogged up and down, |
9 | * over passes, and through long dales, and across many streams. | |
10 | */ | |
11 | ||
12 | /* | |
71200d45 | 13 | * If we have ActivePerl-like PERL_IMPLICIT_SYS then we need a dTHX to get |
14a5cf38 | 14 | * at the dispatch tables, even when we do not need it for other reasons. |
71200d45 | 15 | * Invent a dSYS macro to abstract this out |
14a5cf38 | 16 | */ |
7bcba3d4 NIS |
17 | #ifdef PERL_IMPLICIT_SYS |
18 | #define dSYS dTHX | |
19 | #else | |
20 | #define dSYS dNOOP | |
21 | #endif | |
22 | ||
760ac839 | 23 | #define VOIDUSED 1 |
12ae5dfc JH |
24 | #ifdef PERL_MICRO |
25 | # include "uconfig.h" | |
26 | #else | |
27 | # include "config.h" | |
28 | #endif | |
760ac839 | 29 | |
6f9d8c32 | 30 | #define PERLIO_NOT_STDIO 0 |
760ac839 | 31 | #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO) |
14a5cf38 | 32 | /* |
71200d45 | 33 | * #define PerlIO FILE |
14a5cf38 | 34 | */ |
760ac839 LW |
35 | #endif |
36 | /* | |
6f9d8c32 | 37 | * This file provides those parts of PerlIO abstraction |
88b61e10 | 38 | * which are not #defined in perlio.h. |
6f9d8c32 | 39 | * Which these are depends on various Configure #ifdef's |
760ac839 LW |
40 | */ |
41 | ||
42 | #include "EXTERN.h" | |
864dbfa3 | 43 | #define PERL_IN_PERLIO_C |
760ac839 LW |
44 | #include "perl.h" |
45 | ||
32af7c23 CL |
46 | #ifdef PERL_IMPLICIT_CONTEXT |
47 | #undef dSYS | |
48 | #define dSYS dTHX | |
49 | #endif | |
50 | ||
0c4f7ff0 NIS |
51 | #include "XSUB.h" |
52 | ||
60382766 | 53 | int |
f5b9d040 | 54 | perlsio_binmode(FILE *fp, int iotype, int mode) |
60382766 | 55 | { |
14a5cf38 | 56 | /* |
71200d45 | 57 | * This used to be contents of do_binmode in doio.c |
14a5cf38 | 58 | */ |
60382766 NIS |
59 | #ifdef DOSISH |
60 | # if defined(atarist) || defined(__MINT__) | |
f5b9d040 | 61 | if (!fflush(fp)) { |
60382766 | 62 | if (mode & O_BINARY) |
14a5cf38 | 63 | ((FILE *) fp)->_flag |= _IOBIN; |
60382766 | 64 | else |
14a5cf38 | 65 | ((FILE *) fp)->_flag &= ~_IOBIN; |
60382766 NIS |
66 | return 1; |
67 | } | |
68 | return 0; | |
69 | # else | |
eb73beca | 70 | dTHX; |
14a5cf38 JH |
71 | #ifdef NETWARE |
72 | if (PerlLIO_setmode(fp, mode) != -1) { | |
73 | #else | |
f5b9d040 | 74 | if (PerlLIO_setmode(fileno(fp), mode) != -1) { |
14a5cf38 | 75 | #endif |
60382766 | 76 | # if defined(WIN32) && defined(__BORLANDC__) |
14a5cf38 | 77 | /* |
71200d45 | 78 | * The translation mode of the stream is maintained independent of |
14a5cf38 | 79 | * the translation mode of the fd in the Borland RTL (heavy |
71200d45 | 80 | * digging through their runtime sources reveal). User has to set |
14a5cf38 | 81 | * the mode explicitly for the stream (though they don't document |
71200d45 | 82 | * this anywhere). GSAR 97-5-24 |
60382766 | 83 | */ |
14a5cf38 | 84 | fseek(fp, 0L, 0); |
60382766 | 85 | if (mode & O_BINARY) |
f5b9d040 | 86 | fp->flags |= _F_BIN; |
60382766 | 87 | else |
14a5cf38 | 88 | fp->flags &= ~_F_BIN; |
60382766 NIS |
89 | # endif |
90 | return 1; | |
91 | } | |
92 | else | |
93 | return 0; | |
94 | # endif | |
95 | #else | |
96 | # if defined(USEMYBINMODE) | |
04a0f00b | 97 | dTHX; |
60382766 NIS |
98 | if (my_binmode(fp, iotype, mode) != FALSE) |
99 | return 1; | |
100 | else | |
101 | return 0; | |
102 | # else | |
103 | return 1; | |
104 | # endif | |
105 | #endif | |
106 | } | |
107 | ||
06c7082d | 108 | #ifndef O_ACCMODE |
22569500 | 109 | #define O_ACCMODE 3 /* Assume traditional implementation */ |
06c7082d NIS |
110 | #endif |
111 | ||
112 | int | |
113 | PerlIO_intmode2str(int rawmode, char *mode, int *writing) | |
114 | { | |
115 | int result = rawmode & O_ACCMODE; | |
116 | int ix = 0; | |
117 | int ptype; | |
118 | switch (result) { | |
119 | case O_RDONLY: | |
120 | ptype = IoTYPE_RDONLY; | |
121 | break; | |
122 | case O_WRONLY: | |
123 | ptype = IoTYPE_WRONLY; | |
124 | break; | |
125 | case O_RDWR: | |
126 | default: | |
127 | ptype = IoTYPE_RDWR; | |
128 | break; | |
129 | } | |
130 | if (writing) | |
131 | *writing = (result != O_RDONLY); | |
132 | ||
133 | if (result == O_RDONLY) { | |
134 | mode[ix++] = 'r'; | |
135 | } | |
136 | #ifdef O_APPEND | |
137 | else if (rawmode & O_APPEND) { | |
138 | mode[ix++] = 'a'; | |
139 | if (result != O_WRONLY) | |
140 | mode[ix++] = '+'; | |
141 | } | |
142 | #endif | |
143 | else { | |
144 | if (result == O_WRONLY) | |
145 | mode[ix++] = 'w'; | |
146 | else { | |
147 | mode[ix++] = 'r'; | |
148 | mode[ix++] = '+'; | |
149 | } | |
150 | } | |
151 | if (rawmode & O_BINARY) | |
152 | mode[ix++] = 'b'; | |
153 | mode[ix] = '\0'; | |
154 | return ptype; | |
155 | } | |
156 | ||
eb73beca NIS |
157 | #ifndef PERLIO_LAYERS |
158 | int | |
159 | PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names) | |
160 | { | |
6874a2de NIS |
161 | if (!names || !*names |
162 | || strEQ(names, ":crlf") | |
163 | || strEQ(names, ":raw") | |
164 | || strEQ(names, ":bytes") | |
165 | ) { | |
14a5cf38 JH |
166 | return 0; |
167 | } | |
168 | Perl_croak(aTHX_ "Cannot apply \"%s\" in non-PerlIO perl", names); | |
169 | /* | |
71200d45 | 170 | * NOTREACHED |
14a5cf38 JH |
171 | */ |
172 | return -1; | |
eb73beca NIS |
173 | } |
174 | ||
13621cfb NIS |
175 | void |
176 | PerlIO_destruct(pTHX) | |
177 | { | |
178 | } | |
179 | ||
f5b9d040 NIS |
180 | int |
181 | PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names) | |
182 | { | |
92bff44d | 183 | #ifdef USE_SFIO |
14a5cf38 | 184 | return 1; |
92bff44d | 185 | #else |
14a5cf38 | 186 | return perlsio_binmode(fp, iotype, mode); |
92bff44d | 187 | #endif |
f5b9d040 | 188 | } |
60382766 | 189 | |
e0fa5af2 | 190 | PerlIO * |
ecdeb87c | 191 | PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) |
e0fa5af2 | 192 | { |
0553478e NIS |
193 | #ifdef PERL_MICRO |
194 | return NULL; | |
195 | #else | |
196 | #ifdef PERL_IMPLICIT_SYS | |
22569500 | 197 | return PerlSIO_fdupopen(f); |
0553478e | 198 | #else |
30753f56 NIS |
199 | #ifdef WIN32 |
200 | return win32_fdupopen(f); | |
201 | #else | |
e0fa5af2 NIS |
202 | if (f) { |
203 | int fd = PerlLIO_dup(PerlIO_fileno(f)); | |
204 | if (fd >= 0) { | |
06c7082d NIS |
205 | char mode[8]; |
206 | int omode = fcntl(fd, F_GETFL); | |
a5936e02 JH |
207 | #ifdef DJGPP |
208 | omode = djgpp_get_stream_mode(f); | |
209 | #endif | |
06c7082d | 210 | PerlIO_intmode2str(omode,mode,NULL); |
e0fa5af2 | 211 | /* the r+ is a hack */ |
06c7082d | 212 | return PerlIO_fdopen(fd, mode); |
e0fa5af2 NIS |
213 | } |
214 | return NULL; | |
215 | } | |
216 | else { | |
93189314 | 217 | SETERRNO(EBADF, SS_IVCHAN); |
e0fa5af2 | 218 | } |
7114a2d2 | 219 | #endif |
e0fa5af2 | 220 | return NULL; |
0553478e | 221 | #endif |
30753f56 | 222 | #endif |
e0fa5af2 NIS |
223 | } |
224 | ||
225 | ||
14a5cf38 | 226 | /* |
71200d45 | 227 | * De-mux PerlIO_openn() into fdopen, freopen and fopen type entries |
14a5cf38 | 228 | */ |
ee518936 NIS |
229 | |
230 | PerlIO * | |
14a5cf38 JH |
231 | PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd, |
232 | int imode, int perm, PerlIO *old, int narg, SV **args) | |
233 | { | |
7cf31beb NIS |
234 | if (narg) { |
235 | if (narg > 1) { | |
3b8752bb | 236 | Perl_croak(aTHX_ "More than one argument to open"); |
7cf31beb | 237 | } |
14a5cf38 JH |
238 | if (*args == &PL_sv_undef) |
239 | return PerlIO_tmpfile(); | |
240 | else { | |
241 | char *name = SvPV_nolen(*args); | |
242 | if (*mode == '#') { | |
243 | fd = PerlLIO_open3(name, imode, perm); | |
244 | if (fd >= 0) | |
245 | return PerlIO_fdopen(fd, (char *) mode + 1); | |
246 | } | |
247 | else if (old) { | |
248 | return PerlIO_reopen(name, mode, old); | |
249 | } | |
250 | else { | |
251 | return PerlIO_open(name, mode); | |
252 | } | |
253 | } | |
254 | } | |
255 | else { | |
256 | return PerlIO_fdopen(fd, (char *) mode); | |
257 | } | |
258 | return NULL; | |
ee518936 NIS |
259 | } |
260 | ||
0c4f7ff0 NIS |
261 | XS(XS_PerlIO__Layer__find) |
262 | { | |
14a5cf38 JH |
263 | dXSARGS; |
264 | if (items < 2) | |
265 | Perl_croak(aTHX_ "Usage class->find(name[,load])"); | |
266 | else { | |
267 | char *name = SvPV_nolen(ST(1)); | |
268 | ST(0) = (strEQ(name, "crlf") | |
269 | || strEQ(name, "raw")) ? &PL_sv_yes : &PL_sv_undef; | |
270 | XSRETURN(1); | |
271 | } | |
0c4f7ff0 NIS |
272 | } |
273 | ||
274 | ||
275 | void | |
276 | Perl_boot_core_PerlIO(pTHX) | |
277 | { | |
14a5cf38 | 278 | newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__); |
0c4f7ff0 NIS |
279 | } |
280 | ||
ac27b0f5 NIS |
281 | #endif |
282 | ||
32e30700 | 283 | |
6f9d8c32 | 284 | #ifdef PERLIO_IS_STDIO |
760ac839 LW |
285 | |
286 | void | |
e8632036 | 287 | PerlIO_init(pTHX) |
760ac839 | 288 | { |
14a5cf38 JH |
289 | /* |
290 | * Does nothing (yet) except force this file to be included in perl | |
71200d45 | 291 | * binary. That allows this file to force inclusion of other functions |
14a5cf38 | 292 | * that may be required by loadable extensions e.g. for |
71200d45 | 293 | * FileHandle::tmpfile |
14a5cf38 | 294 | */ |
760ac839 LW |
295 | } |
296 | ||
33dcbb9a | 297 | #undef PerlIO_tmpfile |
298 | PerlIO * | |
8ac85365 | 299 | PerlIO_tmpfile(void) |
33dcbb9a | 300 | { |
14a5cf38 | 301 | return tmpfile(); |
33dcbb9a | 302 | } |
303 | ||
22569500 | 304 | #else /* PERLIO_IS_STDIO */ |
760ac839 LW |
305 | |
306 | #ifdef USE_SFIO | |
307 | ||
308 | #undef HAS_FSETPOS | |
309 | #undef HAS_FGETPOS | |
310 | ||
14a5cf38 JH |
311 | /* |
312 | * This section is just to make sure these functions get pulled in from | |
71200d45 | 313 | * libsfio.a |
14a5cf38 | 314 | */ |
760ac839 LW |
315 | |
316 | #undef PerlIO_tmpfile | |
317 | PerlIO * | |
c78749f2 | 318 | PerlIO_tmpfile(void) |
760ac839 | 319 | { |
14a5cf38 | 320 | return sftmp(0); |
760ac839 LW |
321 | } |
322 | ||
323 | void | |
e8632036 | 324 | PerlIO_init(pTHX) |
760ac839 | 325 | { |
14a5cf38 JH |
326 | /* |
327 | * Force this file to be included in perl binary. Which allows this | |
328 | * file to force inclusion of other functions that may be required by | |
71200d45 | 329 | * loadable extensions e.g. for FileHandle::tmpfile |
14a5cf38 | 330 | */ |
760ac839 | 331 | |
14a5cf38 | 332 | /* |
71200d45 | 333 | * Hack sfio does its own 'autoflush' on stdout in common cases. Flush |
14a5cf38 | 334 | * results in a lot of lseek()s to regular files and lot of small |
71200d45 | 335 | * writes to pipes. |
14a5cf38 JH |
336 | */ |
337 | sfset(sfstdout, SF_SHARE, 0); | |
760ac839 LW |
338 | } |
339 | ||
b9d6bf13 | 340 | /* This is not the reverse of PerlIO_exportFILE(), PerlIO_releaseFILE() is. */ |
92bff44d | 341 | PerlIO * |
4b069b44 | 342 | PerlIO_importFILE(FILE *stdio, const char *mode) |
92bff44d | 343 | { |
14a5cf38 | 344 | int fd = fileno(stdio); |
4b069b44 | 345 | if (!mode || !*mode) { |
81428673 | 346 | mode = "r+"; |
4b069b44 NIS |
347 | } |
348 | return PerlIO_fdopen(fd, mode); | |
92bff44d NIS |
349 | } |
350 | ||
351 | FILE * | |
352 | PerlIO_findFILE(PerlIO *pio) | |
353 | { | |
14a5cf38 JH |
354 | int fd = PerlIO_fileno(pio); |
355 | FILE *f = fdopen(fd, "r+"); | |
356 | PerlIO_flush(pio); | |
357 | if (!f && errno == EINVAL) | |
358 | f = fdopen(fd, "w"); | |
359 | if (!f && errno == EINVAL) | |
360 | f = fdopen(fd, "r"); | |
361 | return f; | |
92bff44d NIS |
362 | } |
363 | ||
364 | ||
22569500 | 365 | #else /* USE_SFIO */ |
6f9d8c32 | 366 | /*======================================================================================*/ |
14a5cf38 | 367 | /* |
71200d45 | 368 | * Implement all the PerlIO interface ourselves. |
9e353e3b | 369 | */ |
760ac839 | 370 | |
76ced9ad NIS |
371 | #include "perliol.h" |
372 | ||
14a5cf38 JH |
373 | /* |
374 | * We _MUST_ have <unistd.h> if we are using lseek() and may have large | |
71200d45 | 375 | * files |
14a5cf38 | 376 | */ |
02f66e2f NIS |
377 | #ifdef I_UNISTD |
378 | #include <unistd.h> | |
379 | #endif | |
06da4f11 NIS |
380 | #ifdef HAS_MMAP |
381 | #include <sys/mman.h> | |
382 | #endif | |
383 | ||
02f66e2f | 384 | |
14a5cf38 JH |
385 | void PerlIO_debug(const char *fmt, ...) |
386 | __attribute__ ((format(__printf__, 1, 2))); | |
6f9d8c32 | 387 | |
6f9d8c32 | 388 | void |
14a5cf38 JH |
389 | PerlIO_debug(const char *fmt, ...) |
390 | { | |
391 | static int dbg = 0; | |
392 | va_list ap; | |
393 | dSYS; | |
394 | va_start(ap, fmt); | |
395 | if (!dbg) { | |
396 | char *s = PerlEnv_getenv("PERLIO_DEBUG"); | |
397 | if (s && *s) | |
398 | dbg = PerlLIO_open3(s, O_WRONLY | O_CREAT | O_APPEND, 0666); | |
399 | else | |
400 | dbg = -1; | |
401 | } | |
402 | if (dbg > 0) { | |
403 | dTHX; | |
70ace5da NIS |
404 | #ifdef USE_ITHREADS |
405 | /* Use fixed buffer as sv_catpvf etc. needs SVs */ | |
406 | char buffer[1024]; | |
407 | char *s; | |
408 | STRLEN len; | |
409 | s = CopFILE(PL_curcop); | |
410 | if (!s) | |
411 | s = "(none)"; | |
412 | sprintf(buffer, "%s:%" IVdf " ", s, (IV) CopLINE(PL_curcop)); | |
22569500 | 413 | len = strlen(buffer); |
70ace5da NIS |
414 | vsprintf(buffer+len, fmt, ap); |
415 | PerlLIO_write(dbg, buffer, strlen(buffer)); | |
416 | #else | |
14a5cf38 JH |
417 | SV *sv = newSVpvn("", 0); |
418 | char *s; | |
419 | STRLEN len; | |
420 | s = CopFILE(PL_curcop); | |
421 | if (!s) | |
422 | s = "(none)"; | |
423 | Perl_sv_catpvf(aTHX_ sv, "%s:%" IVdf " ", s, | |
424 | (IV) CopLINE(PL_curcop)); | |
425 | Perl_sv_vcatpvf(aTHX_ sv, fmt, &ap); | |
426 | ||
427 | s = SvPV(sv, len); | |
428 | PerlLIO_write(dbg, s, len); | |
429 | SvREFCNT_dec(sv); | |
70ace5da | 430 | #endif |
14a5cf38 JH |
431 | } |
432 | va_end(ap); | |
6f9d8c32 NIS |
433 | } |
434 | ||
9e353e3b NIS |
435 | /*--------------------------------------------------------------------------------------*/ |
436 | ||
14a5cf38 | 437 | /* |
71200d45 | 438 | * Inner level routines |
14a5cf38 | 439 | */ |
9e353e3b | 440 | |
14a5cf38 | 441 | /* |
71200d45 | 442 | * Table of pointers to the PerlIO structs (malloc'ed) |
14a5cf38 | 443 | */ |
05d1247b | 444 | #define PERLIO_TABLE_SIZE 64 |
6f9d8c32 | 445 | |
760ac839 | 446 | PerlIO * |
5f1a76d0 | 447 | PerlIO_allocate(pTHX) |
6f9d8c32 | 448 | { |
14a5cf38 | 449 | /* |
71200d45 | 450 | * Find a free slot in the table, allocating new table as necessary |
14a5cf38 JH |
451 | */ |
452 | PerlIO **last; | |
453 | PerlIO *f; | |
a1ea730d | 454 | last = &PL_perlio; |
14a5cf38 JH |
455 | while ((f = *last)) { |
456 | int i; | |
457 | last = (PerlIO **) (f); | |
458 | for (i = 1; i < PERLIO_TABLE_SIZE; i++) { | |
459 | if (!*++f) { | |
460 | return f; | |
461 | } | |
462 | } | |
463 | } | |
3a1ee7e8 | 464 | Newz('I',f,PERLIO_TABLE_SIZE,PerlIO); |
14a5cf38 JH |
465 | if (!f) { |
466 | return NULL; | |
467 | } | |
468 | *last = f; | |
469 | return f + 1; | |
05d1247b NIS |
470 | } |
471 | ||
a1ea730d NIS |
472 | #undef PerlIO_fdupopen |
473 | PerlIO * | |
ecdeb87c | 474 | PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) |
a1ea730d | 475 | { |
04892f78 | 476 | if (PerlIOValid(f)) { |
a1ea730d NIS |
477 | PerlIO_funcs *tab = PerlIOBase(f)->tab; |
478 | PerlIO *new; | |
fe5a182c | 479 | PerlIO_debug("fdupopen f=%p param=%p\n",(void*)f,(void*)param); |
22569500 | 480 | new = (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX),f,param, flags); |
a1ea730d NIS |
481 | return new; |
482 | } | |
483 | else { | |
93189314 | 484 | SETERRNO(EBADF, SS_IVCHAN); |
a1ea730d NIS |
485 | return NULL; |
486 | } | |
487 | } | |
488 | ||
489 | void | |
5f1a76d0 | 490 | PerlIO_cleantable(pTHX_ PerlIO **tablep) |
05d1247b | 491 | { |
14a5cf38 JH |
492 | PerlIO *table = *tablep; |
493 | if (table) { | |
494 | int i; | |
495 | PerlIO_cleantable(aTHX_(PerlIO **) & (table[0])); | |
496 | for (i = PERLIO_TABLE_SIZE - 1; i > 0; i--) { | |
497 | PerlIO *f = table + i; | |
498 | if (*f) { | |
499 | PerlIO_close(f); | |
500 | } | |
501 | } | |
3a1ee7e8 | 502 | Safefree(table); |
14a5cf38 | 503 | *tablep = NULL; |
05d1247b | 504 | } |
05d1247b NIS |
505 | } |
506 | ||
fcf2db38 NIS |
507 | |
508 | PerlIO_list_t * | |
3a1ee7e8 | 509 | PerlIO_list_alloc(pTHX) |
fcf2db38 | 510 | { |
14a5cf38 JH |
511 | PerlIO_list_t *list; |
512 | Newz('L', list, 1, PerlIO_list_t); | |
513 | list->refcnt = 1; | |
514 | return list; | |
fcf2db38 NIS |
515 | } |
516 | ||
517 | void | |
3a1ee7e8 | 518 | PerlIO_list_free(pTHX_ PerlIO_list_t *list) |
fcf2db38 | 519 | { |
14a5cf38 JH |
520 | if (list) { |
521 | if (--list->refcnt == 0) { | |
522 | if (list->array) { | |
14a5cf38 JH |
523 | IV i; |
524 | for (i = 0; i < list->cur; i++) { | |
525 | if (list->array[i].arg) | |
526 | SvREFCNT_dec(list->array[i].arg); | |
527 | } | |
528 | Safefree(list->array); | |
529 | } | |
530 | Safefree(list); | |
531 | } | |
532 | } | |
fcf2db38 NIS |
533 | } |
534 | ||
535 | void | |
3a1ee7e8 | 536 | PerlIO_list_push(pTHX_ PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg) |
14a5cf38 | 537 | { |
14a5cf38 JH |
538 | PerlIO_pair_t *p; |
539 | if (list->cur >= list->len) { | |
540 | list->len += 8; | |
541 | if (list->array) | |
542 | Renew(list->array, list->len, PerlIO_pair_t); | |
543 | else | |
544 | New('l', list->array, list->len, PerlIO_pair_t); | |
545 | } | |
546 | p = &(list->array[list->cur++]); | |
547 | p->funcs = funcs; | |
548 | if ((p->arg = arg)) { | |
549 | SvREFCNT_inc(arg); | |
550 | } | |
fcf2db38 NIS |
551 | } |
552 | ||
3a1ee7e8 NIS |
553 | PerlIO_list_t * |
554 | PerlIO_clone_list(pTHX_ PerlIO_list_t *proto, CLONE_PARAMS *param) | |
555 | { | |
694c95cf JH |
556 | PerlIO_list_t *list = (PerlIO_list_t *) NULL; |
557 | if (proto) { | |
558 | int i; | |
559 | list = PerlIO_list_alloc(aTHX); | |
560 | for (i=0; i < proto->cur; i++) { | |
561 | SV *arg = Nullsv; | |
562 | if (proto->array[i].arg) | |
563 | arg = PerlIO_sv_dup(aTHX_ proto->array[i].arg,param); | |
564 | PerlIO_list_push(aTHX_ list, proto->array[i].funcs, arg); | |
565 | } | |
3a1ee7e8 NIS |
566 | } |
567 | return list; | |
568 | } | |
4a4a6116 | 569 | |
05d1247b | 570 | void |
3a1ee7e8 | 571 | PerlIO_clone(pTHX_ PerlInterpreter *proto, CLONE_PARAMS *param) |
9a6404c5 | 572 | { |
3aaf42a7 | 573 | #ifdef USE_ITHREADS |
3a1ee7e8 NIS |
574 | PerlIO **table = &proto->Iperlio; |
575 | PerlIO *f; | |
576 | PL_perlio = NULL; | |
577 | PL_known_layers = PerlIO_clone_list(aTHX_ proto->Iknown_layers, param); | |
578 | PL_def_layerlist = PerlIO_clone_list(aTHX_ proto->Idef_layerlist, param); | |
579 | PerlIO_allocate(aTHX); /* root slot is never used */ | |
694c95cf | 580 | PerlIO_debug("Clone %p from %p\n",aTHX,proto); |
3a1ee7e8 NIS |
581 | while ((f = *table)) { |
582 | int i; | |
583 | table = (PerlIO **) (f++); | |
584 | for (i = 1; i < PERLIO_TABLE_SIZE; i++) { | |
585 | if (*f) { | |
93a8090d | 586 | (void) fp_dup(f, 0, param); |
3a1ee7e8 NIS |
587 | } |
588 | f++; | |
589 | } | |
590 | } | |
3aaf42a7 | 591 | #endif |
9a6404c5 DM |
592 | } |
593 | ||
594 | void | |
13621cfb NIS |
595 | PerlIO_destruct(pTHX) |
596 | { | |
a1ea730d | 597 | PerlIO **table = &PL_perlio; |
14a5cf38 | 598 | PerlIO *f; |
694c95cf JH |
599 | #ifdef USE_ITHREADS |
600 | PerlIO_debug("Destruct %p\n",aTHX); | |
601 | #endif | |
14a5cf38 JH |
602 | while ((f = *table)) { |
603 | int i; | |
604 | table = (PerlIO **) (f++); | |
605 | for (i = 1; i < PERLIO_TABLE_SIZE; i++) { | |
606 | PerlIO *x = f; | |
607 | PerlIOl *l; | |
608 | while ((l = *x)) { | |
609 | if (l->tab->kind & PERLIO_K_DESTRUCT) { | |
610 | PerlIO_debug("Destruct popping %s\n", l->tab->name); | |
611 | PerlIO_flush(x); | |
612 | PerlIO_pop(aTHX_ x); | |
613 | } | |
614 | else { | |
615 | x = PerlIONext(x); | |
616 | } | |
617 | } | |
618 | f++; | |
619 | } | |
620 | } | |
13621cfb NIS |
621 | } |
622 | ||
623 | void | |
a999f61b | 624 | PerlIO_pop(pTHX_ PerlIO *f) |
760ac839 | 625 | { |
14a5cf38 JH |
626 | PerlIOl *l = *f; |
627 | if (l) { | |
fe5a182c | 628 | PerlIO_debug("PerlIO_pop f=%p %s\n", (void*)f, l->tab->name); |
14a5cf38 JH |
629 | if (l->tab->Popped) { |
630 | /* | |
631 | * If popped returns non-zero do not free its layer structure | |
632 | * it has either done so itself, or it is shared and still in | |
71200d45 | 633 | * use |
14a5cf38 | 634 | */ |
f62ce20a | 635 | if ((*l->tab->Popped) (aTHX_ f) != 0) |
14a5cf38 JH |
636 | return; |
637 | } | |
b47cad08 | 638 | *f = l->next; |
3a1ee7e8 | 639 | Safefree(l); |
a8c08ecd | 640 | } |
6f9d8c32 NIS |
641 | } |
642 | ||
9e353e3b | 643 | /*--------------------------------------------------------------------------------------*/ |
14a5cf38 | 644 | /* |
71200d45 | 645 | * XS Interface for perl code |
14a5cf38 | 646 | */ |
9e353e3b | 647 | |
fcf2db38 | 648 | PerlIO_funcs * |
2edd7e44 | 649 | PerlIO_find_layer(pTHX_ const char *name, STRLEN len, int load) |
f3862f8b | 650 | { |
14a5cf38 JH |
651 | IV i; |
652 | if ((SSize_t) len <= 0) | |
653 | len = strlen(name); | |
3a1ee7e8 NIS |
654 | for (i = 0; i < PL_known_layers->cur; i++) { |
655 | PerlIO_funcs *f = PL_known_layers->array[i].funcs; | |
14a5cf38 | 656 | if (memEQ(f->name, name, len)) { |
fe5a182c | 657 | PerlIO_debug("%.*s => %p\n", (int) len, name, (void*)f); |
14a5cf38 JH |
658 | return f; |
659 | } | |
660 | } | |
3a1ee7e8 NIS |
661 | if (load && PL_subname && PL_def_layerlist |
662 | && PL_def_layerlist->cur >= 2) { | |
d7a09b41 SR |
663 | if (PL_in_load_module) { |
664 | Perl_croak(aTHX_ "Recursive call to Perl_load_module in PerlIO_find_layer"); | |
665 | return NULL; | |
666 | } else { | |
667 | SV *pkgsv = newSVpvn("PerlIO", 6); | |
668 | SV *layer = newSVpvn(name, len); | |
669 | ENTER; | |
670 | SAVEINT(PL_in_load_module); | |
671 | PL_in_load_module++; | |
672 | /* | |
673 | * The two SVs are magically freed by load_module | |
674 | */ | |
675 | Perl_load_module(aTHX_ 0, pkgsv, Nullsv, layer, Nullsv); | |
676 | PL_in_load_module--; | |
677 | LEAVE; | |
678 | return PerlIO_find_layer(aTHX_ name, len, 0); | |
679 | } | |
14a5cf38 JH |
680 | } |
681 | PerlIO_debug("Cannot find %.*s\n", (int) len, name); | |
682 | return NULL; | |
f3862f8b NIS |
683 | } |
684 | ||
2a1bc955 | 685 | #ifdef USE_ATTRIBUTES_FOR_PERLIO |
b13b2135 NIS |
686 | |
687 | static int | |
688 | perlio_mg_set(pTHX_ SV *sv, MAGIC *mg) | |
689 | { | |
14a5cf38 JH |
690 | if (SvROK(sv)) { |
691 | IO *io = GvIOn((GV *) SvRV(sv)); | |
692 | PerlIO *ifp = IoIFP(io); | |
693 | PerlIO *ofp = IoOFP(io); | |
694 | Perl_warn(aTHX_ "set %" SVf " %p %p %p", sv, io, ifp, ofp); | |
695 | } | |
696 | return 0; | |
b13b2135 NIS |
697 | } |
698 | ||
699 | static int | |
700 | perlio_mg_get(pTHX_ SV *sv, MAGIC *mg) | |
701 | { | |
14a5cf38 JH |
702 | if (SvROK(sv)) { |
703 | IO *io = GvIOn((GV *) SvRV(sv)); | |
704 | PerlIO *ifp = IoIFP(io); | |
705 | PerlIO *ofp = IoOFP(io); | |
706 | Perl_warn(aTHX_ "get %" SVf " %p %p %p", sv, io, ifp, ofp); | |
707 | } | |
708 | return 0; | |
b13b2135 NIS |
709 | } |
710 | ||
711 | static int | |
712 | perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg) | |
713 | { | |
14a5cf38 JH |
714 | Perl_warn(aTHX_ "clear %" SVf, sv); |
715 | return 0; | |
b13b2135 NIS |
716 | } |
717 | ||
718 | static int | |
719 | perlio_mg_free(pTHX_ SV *sv, MAGIC *mg) | |
720 | { | |
14a5cf38 JH |
721 | Perl_warn(aTHX_ "free %" SVf, sv); |
722 | return 0; | |
b13b2135 NIS |
723 | } |
724 | ||
725 | MGVTBL perlio_vtab = { | |
14a5cf38 JH |
726 | perlio_mg_get, |
727 | perlio_mg_set, | |
22569500 | 728 | NULL, /* len */ |
14a5cf38 JH |
729 | perlio_mg_clear, |
730 | perlio_mg_free | |
b13b2135 NIS |
731 | }; |
732 | ||
733 | XS(XS_io_MODIFY_SCALAR_ATTRIBUTES) | |
734 | { | |
14a5cf38 JH |
735 | dXSARGS; |
736 | SV *sv = SvRV(ST(1)); | |
737 | AV *av = newAV(); | |
738 | MAGIC *mg; | |
739 | int count = 0; | |
740 | int i; | |
741 | sv_magic(sv, (SV *) av, PERL_MAGIC_ext, NULL, 0); | |
742 | SvRMAGICAL_off(sv); | |
743 | mg = mg_find(sv, PERL_MAGIC_ext); | |
744 | mg->mg_virtual = &perlio_vtab; | |
745 | mg_magical(sv); | |
746 | Perl_warn(aTHX_ "attrib %" SVf, sv); | |
747 | for (i = 2; i < items; i++) { | |
748 | STRLEN len; | |
749 | const char *name = SvPV(ST(i), len); | |
750 | SV *layer = PerlIO_find_layer(aTHX_ name, len, 1); | |
751 | if (layer) { | |
752 | av_push(av, SvREFCNT_inc(layer)); | |
753 | } | |
754 | else { | |
755 | ST(count) = ST(i); | |
756 | count++; | |
757 | } | |
758 | } | |
759 | SvREFCNT_dec(av); | |
760 | XSRETURN(count); | |
761 | } | |
762 | ||
22569500 | 763 | #endif /* USE_ATTIBUTES_FOR_PERLIO */ |
2a1bc955 | 764 | |
e3f3bf95 NIS |
765 | SV * |
766 | PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab) | |
f3862f8b | 767 | { |
14a5cf38 JH |
768 | HV *stash = gv_stashpv("PerlIO::Layer", TRUE); |
769 | SV *sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))), stash); | |
770 | return sv; | |
e3f3bf95 NIS |
771 | } |
772 | ||
0c4f7ff0 NIS |
773 | XS(XS_PerlIO__Layer__find) |
774 | { | |
14a5cf38 JH |
775 | dXSARGS; |
776 | if (items < 2) | |
777 | Perl_croak(aTHX_ "Usage class->find(name[,load])"); | |
778 | else { | |
779 | STRLEN len = 0; | |
780 | char *name = SvPV(ST(1), len); | |
781 | bool load = (items > 2) ? SvTRUE(ST(2)) : 0; | |
782 | PerlIO_funcs *layer = PerlIO_find_layer(aTHX_ name, len, load); | |
783 | ST(0) = | |
784 | (layer) ? sv_2mortal(PerlIO_tab_sv(aTHX_ layer)) : | |
785 | &PL_sv_undef; | |
786 | XSRETURN(1); | |
787 | } | |
0c4f7ff0 NIS |
788 | } |
789 | ||
e3f3bf95 NIS |
790 | void |
791 | PerlIO_define_layer(pTHX_ PerlIO_funcs *tab) | |
792 | { | |
3a1ee7e8 NIS |
793 | if (!PL_known_layers) |
794 | PL_known_layers = PerlIO_list_alloc(aTHX); | |
795 | PerlIO_list_push(aTHX_ PL_known_layers, tab, Nullsv); | |
fe5a182c | 796 | PerlIO_debug("define %s %p\n", tab->name, (void*)tab); |
f3862f8b NIS |
797 | } |
798 | ||
1141d9f8 | 799 | int |
fcf2db38 | 800 | PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names) |
1141d9f8 | 801 | { |
14a5cf38 JH |
802 | if (names) { |
803 | const char *s = names; | |
804 | while (*s) { | |
805 | while (isSPACE(*s) || *s == ':') | |
806 | s++; | |
807 | if (*s) { | |
808 | STRLEN llen = 0; | |
809 | const char *e = s; | |
810 | const char *as = Nullch; | |
811 | STRLEN alen = 0; | |
812 | if (!isIDFIRST(*s)) { | |
813 | /* | |
814 | * Message is consistent with how attribute lists are | |
815 | * passed. Even though this means "foo : : bar" is | |
71200d45 | 816 | * seen as an invalid separator character. |
14a5cf38 JH |
817 | */ |
818 | char q = ((*s == '\'') ? '"' : '\''); | |
99ef548b | 819 | if (ckWARN(WARN_LAYER)) |
22569500 | 820 | Perl_warner(aTHX_ packWARN(WARN_LAYER), |
1e616cf5 JH |
821 | "perlio: invalid separator character %c%c%c in layer specification list %s", |
822 | q, *s, q, s); | |
93189314 | 823 | SETERRNO(EINVAL, LIB_INVARG); |
14a5cf38 JH |
824 | return -1; |
825 | } | |
826 | do { | |
827 | e++; | |
828 | } while (isALNUM(*e)); | |
829 | llen = e - s; | |
830 | if (*e == '(') { | |
831 | int nesting = 1; | |
832 | as = ++e; | |
833 | while (nesting) { | |
834 | switch (*e++) { | |
835 | case ')': | |
836 | if (--nesting == 0) | |
837 | alen = (e - 1) - as; | |
838 | break; | |
839 | case '(': | |
840 | ++nesting; | |
841 | break; | |
842 | case '\\': | |
843 | /* | |
844 | * It's a nul terminated string, not allowed | |
845 | * to \ the terminating null. Anything other | |
71200d45 | 846 | * character is passed over. |
14a5cf38 JH |
847 | */ |
848 | if (*e++) { | |
849 | break; | |
850 | } | |
851 | /* | |
71200d45 | 852 | * Drop through |
14a5cf38 JH |
853 | */ |
854 | case '\0': | |
855 | e--; | |
22569500 NIS |
856 | if (ckWARN(WARN_LAYER)) |
857 | Perl_warner(aTHX_ packWARN(WARN_LAYER), | |
14a5cf38 JH |
858 | "perlio: argument list not closed for layer \"%.*s\"", |
859 | (int) (e - s), s); | |
860 | return -1; | |
861 | default: | |
862 | /* | |
71200d45 | 863 | * boring. |
14a5cf38 JH |
864 | */ |
865 | break; | |
866 | } | |
867 | } | |
868 | } | |
869 | if (e > s) { | |
99ef548b | 870 | bool warn_layer = ckWARN(WARN_LAYER); |
14a5cf38 JH |
871 | PerlIO_funcs *layer = |
872 | PerlIO_find_layer(aTHX_ s, llen, 1); | |
873 | if (layer) { | |
3a1ee7e8 | 874 | PerlIO_list_push(aTHX_ av, layer, |
14a5cf38 JH |
875 | (as) ? newSVpvn(as, |
876 | alen) : | |
877 | &PL_sv_undef); | |
878 | } | |
879 | else { | |
22569500 | 880 | if (warn_layer) |
99ef548b | 881 | Perl_warner(aTHX_ packWARN(WARN_LAYER), "perlio: unknown layer \"%.*s\"", |
14a5cf38 JH |
882 | (int) llen, s); |
883 | return -1; | |
884 | } | |
885 | } | |
886 | s = e; | |
887 | } | |
888 | } | |
889 | } | |
890 | return 0; | |
1141d9f8 NIS |
891 | } |
892 | ||
dfebf958 | 893 | void |
fcf2db38 | 894 | PerlIO_default_buffer(pTHX_ PerlIO_list_t *av) |
dfebf958 | 895 | { |
14a5cf38 | 896 | PerlIO_funcs *tab = &PerlIO_perlio; |
35990314 | 897 | #ifdef PERLIO_USING_CRLF |
6ce75a77 | 898 | tab = &PerlIO_crlf; |
846be114 | 899 | #else |
6ce75a77 | 900 | if (PerlIO_stdio.Set_ptrcnt) |
22569500 | 901 | tab = &PerlIO_stdio; |
846be114 | 902 | #endif |
14a5cf38 | 903 | PerlIO_debug("Pushing %s\n", tab->name); |
3a1ee7e8 | 904 | PerlIO_list_push(aTHX_ av, PerlIO_find_layer(aTHX_ tab->name, 0, 0), |
14a5cf38 | 905 | &PL_sv_undef); |
dfebf958 NIS |
906 | } |
907 | ||
e3f3bf95 | 908 | SV * |
14a5cf38 | 909 | PerlIO_arg_fetch(PerlIO_list_t *av, IV n) |
e3f3bf95 | 910 | { |
14a5cf38 | 911 | return av->array[n].arg; |
e3f3bf95 NIS |
912 | } |
913 | ||
f3862f8b | 914 | PerlIO_funcs * |
14a5cf38 | 915 | PerlIO_layer_fetch(pTHX_ PerlIO_list_t *av, IV n, PerlIO_funcs *def) |
f3862f8b | 916 | { |
14a5cf38 JH |
917 | if (n >= 0 && n < av->cur) { |
918 | PerlIO_debug("Layer %" IVdf " is %s\n", n, | |
919 | av->array[n].funcs->name); | |
920 | return av->array[n].funcs; | |
921 | } | |
922 | if (!def) | |
923 | Perl_croak(aTHX_ "panic: PerlIO layer array corrupt"); | |
924 | return def; | |
e3f3bf95 NIS |
925 | } |
926 | ||
4ec2216f NIS |
927 | IV |
928 | PerlIOPop_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) | |
929 | { | |
930 | if (PerlIOValid(f)) { | |
931 | PerlIO_flush(f); | |
932 | PerlIO_pop(aTHX_ f); | |
933 | return 0; | |
934 | } | |
935 | return -1; | |
936 | } | |
937 | ||
938 | PerlIO_funcs PerlIO_remove = { | |
939 | sizeof(PerlIO_funcs), | |
940 | "pop", | |
941 | 0, | |
942 | PERLIO_K_DUMMY | PERLIO_K_UTF8, | |
943 | PerlIOPop_pushed, | |
944 | NULL, | |
945 | NULL, | |
946 | NULL, | |
947 | NULL, | |
948 | NULL, | |
949 | NULL, | |
950 | NULL, | |
951 | NULL, | |
952 | NULL, | |
953 | NULL, | |
954 | NULL, /* flush */ | |
955 | NULL, /* fill */ | |
956 | NULL, | |
957 | NULL, | |
958 | NULL, | |
959 | NULL, | |
960 | NULL, /* get_base */ | |
961 | NULL, /* get_bufsiz */ | |
962 | NULL, /* get_ptr */ | |
963 | NULL, /* get_cnt */ | |
964 | NULL, /* set_ptrcnt */ | |
965 | }; | |
966 | ||
fcf2db38 | 967 | PerlIO_list_t * |
e3f3bf95 NIS |
968 | PerlIO_default_layers(pTHX) |
969 | { | |
3a1ee7e8 | 970 | if (!PL_def_layerlist) { |
14a5cf38 JH |
971 | const char *s = (PL_tainting) ? Nullch : PerlEnv_getenv("PERLIO"); |
972 | PerlIO_funcs *osLayer = &PerlIO_unix; | |
3a1ee7e8 | 973 | PL_def_layerlist = PerlIO_list_alloc(aTHX); |
14a5cf38 | 974 | PerlIO_define_layer(aTHX_ & PerlIO_unix); |
e1caacb4 | 975 | #if defined(WIN32) && !defined(UNDER_CE) |
14a5cf38 | 976 | PerlIO_define_layer(aTHX_ & PerlIO_win32); |
2f8118af | 977 | #if 0 |
14a5cf38 | 978 | osLayer = &PerlIO_win32; |
0c4128ad | 979 | #endif |
2f8118af | 980 | #endif |
14a5cf38 JH |
981 | PerlIO_define_layer(aTHX_ & PerlIO_raw); |
982 | PerlIO_define_layer(aTHX_ & PerlIO_perlio); | |
983 | PerlIO_define_layer(aTHX_ & PerlIO_stdio); | |
984 | PerlIO_define_layer(aTHX_ & PerlIO_crlf); | |
06da4f11 | 985 | #ifdef HAS_MMAP |
14a5cf38 | 986 | PerlIO_define_layer(aTHX_ & PerlIO_mmap); |
06da4f11 | 987 | #endif |
14a5cf38 | 988 | PerlIO_define_layer(aTHX_ & PerlIO_utf8); |
4ec2216f | 989 | PerlIO_define_layer(aTHX_ & PerlIO_remove); |
14a5cf38 | 990 | PerlIO_define_layer(aTHX_ & PerlIO_byte); |
3a1ee7e8 | 991 | PerlIO_list_push(aTHX_ PL_def_layerlist, |
14a5cf38 JH |
992 | PerlIO_find_layer(aTHX_ osLayer->name, 0, 0), |
993 | &PL_sv_undef); | |
994 | if (s) { | |
3a1ee7e8 | 995 | PerlIO_parse_layers(aTHX_ PL_def_layerlist, s); |
14a5cf38 JH |
996 | } |
997 | else { | |
3a1ee7e8 | 998 | PerlIO_default_buffer(aTHX_ PL_def_layerlist); |
14a5cf38 | 999 | } |
1141d9f8 | 1000 | } |
3a1ee7e8 NIS |
1001 | if (PL_def_layerlist->cur < 2) { |
1002 | PerlIO_default_buffer(aTHX_ PL_def_layerlist); | |
f3862f8b | 1003 | } |
3a1ee7e8 | 1004 | return PL_def_layerlist; |
e3f3bf95 NIS |
1005 | } |
1006 | ||
0c4f7ff0 NIS |
1007 | void |
1008 | Perl_boot_core_PerlIO(pTHX) | |
1009 | { | |
1010 | #ifdef USE_ATTRIBUTES_FOR_PERLIO | |
14a5cf38 JH |
1011 | newXS("io::MODIFY_SCALAR_ATTRIBUTES", XS_io_MODIFY_SCALAR_ATTRIBUTES, |
1012 | __FILE__); | |
0c4f7ff0 | 1013 | #endif |
14a5cf38 | 1014 | newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__); |
0c4f7ff0 | 1015 | } |
e3f3bf95 NIS |
1016 | |
1017 | PerlIO_funcs * | |
1018 | PerlIO_default_layer(pTHX_ I32 n) | |
1019 | { | |
14a5cf38 JH |
1020 | PerlIO_list_t *av = PerlIO_default_layers(aTHX); |
1021 | if (n < 0) | |
1022 | n += av->cur; | |
1023 | return PerlIO_layer_fetch(aTHX_ av, n, &PerlIO_stdio); | |
f3862f8b NIS |
1024 | } |
1025 | ||
a999f61b NIS |
1026 | #define PerlIO_default_top() PerlIO_default_layer(aTHX_ -1) |
1027 | #define PerlIO_default_btm() PerlIO_default_layer(aTHX_ 0) | |
60382766 NIS |
1028 | |
1029 | void | |
1141d9f8 | 1030 | PerlIO_stdstreams(pTHX) |
60382766 | 1031 | { |
a1ea730d | 1032 | if (!PL_perlio) { |
14a5cf38 JH |
1033 | PerlIO_allocate(aTHX); |
1034 | PerlIO_fdopen(0, "Ir" PERLIO_STDTEXT); | |
1035 | PerlIO_fdopen(1, "Iw" PERLIO_STDTEXT); | |
1036 | PerlIO_fdopen(2, "Iw" PERLIO_STDTEXT); | |
1037 | } | |
60382766 NIS |
1038 | } |
1039 | ||
1040 | PerlIO * | |
14a5cf38 JH |
1041 | PerlIO_push(pTHX_ PerlIO *f, PerlIO_funcs *tab, const char *mode, SV *arg) |
1042 | { | |
2dc2558e NIS |
1043 | if (tab->fsize != sizeof(PerlIO_funcs)) { |
1044 | mismatch: | |
1045 | Perl_croak(aTHX_ "Layer does not match this perl"); | |
1046 | } | |
1047 | if (tab->size) { | |
1048 | PerlIOl *l = NULL; | |
1049 | if (tab->size < sizeof(PerlIOl)) { | |
1050 | goto mismatch; | |
1051 | } | |
1052 | /* Real layer with a data area */ | |
1053 | Newc('L',l,tab->size,char,PerlIOl); | |
1054 | if (l && f) { | |
1055 | Zero(l, tab->size, char); | |
1056 | l->next = *f; | |
1057 | l->tab = tab; | |
1058 | *f = l; | |
1059 | PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name, | |
1060 | (mode) ? mode : "(Null)", (void*)arg); | |
1061 | if ((*l->tab->Pushed) (aTHX_ f, mode, arg, tab) != 0) { | |
1062 | PerlIO_pop(aTHX_ f); | |
1063 | return NULL; | |
1064 | } | |
1065 | } | |
1066 | } | |
1067 | else if (f) { | |
1068 | /* Pseudo-layer where push does its own stack adjust */ | |
00f51856 NIS |
1069 | PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name, |
1070 | (mode) ? mode : "(Null)", (void*)arg); | |
2dc2558e | 1071 | if ((*tab->Pushed) (aTHX_ f, mode, arg, tab) != 0) { |
14a5cf38 JH |
1072 | return NULL; |
1073 | } | |
1074 | } | |
1075 | return f; | |
60382766 NIS |
1076 | } |
1077 | ||
dfebf958 | 1078 | IV |
86e05cf2 NIS |
1079 | PerlIOBase_binmode(pTHX_ PerlIO *f) |
1080 | { | |
1081 | if (PerlIOValid(f)) { | |
1082 | /* Is layer suitable for raw stream ? */ | |
1083 | if (PerlIOBase(f)->tab->kind & PERLIO_K_RAW) { | |
1084 | /* Yes - turn off UTF-8-ness, to undo UTF-8 locale effects */ | |
1085 | PerlIOBase(f)->flags &= ~PERLIO_F_UTF8; | |
1086 | } | |
1087 | else { | |
1088 | /* Not suitable - pop it */ | |
1089 | PerlIO_pop(aTHX_ f); | |
1090 | } | |
1091 | return 0; | |
1092 | } | |
1093 | return -1; | |
1094 | } | |
1095 | ||
1096 | IV | |
2dc2558e | 1097 | PerlIORaw_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
dfebf958 | 1098 | { |
86e05cf2 | 1099 | |
04892f78 | 1100 | if (PerlIOValid(f)) { |
86e05cf2 NIS |
1101 | PerlIO *t; |
1102 | PerlIOl *l; | |
14a5cf38 | 1103 | PerlIO_flush(f); |
86e05cf2 NIS |
1104 | /* |
1105 | * Strip all layers that are not suitable for a raw stream | |
1106 | */ | |
1107 | t = f; | |
1108 | while (t && (l = *t)) { | |
1109 | if (l->tab->Binmode) { | |
1110 | /* Has a handler - normal case */ | |
1111 | if ((*l->tab->Binmode)(aTHX_ f) == 0) { | |
1112 | if (*t == l) { | |
1113 | /* Layer still there - move down a layer */ | |
1114 | t = PerlIONext(t); | |
1115 | } | |
1116 | } | |
1117 | else { | |
1118 | return -1; | |
1119 | } | |
14a5cf38 JH |
1120 | } |
1121 | else { | |
86e05cf2 NIS |
1122 | /* No handler - pop it */ |
1123 | PerlIO_pop(aTHX_ t); | |
14a5cf38 JH |
1124 | } |
1125 | } | |
86e05cf2 NIS |
1126 | if (PerlIOValid(f)) { |
1127 | PerlIO_debug(":raw f=%p :%s\n", (void*)f, PerlIOBase(f)->tab->name); | |
1128 | return 0; | |
1129 | } | |
14a5cf38 JH |
1130 | } |
1131 | return -1; | |
dfebf958 NIS |
1132 | } |
1133 | ||
ac27b0f5 | 1134 | int |
14a5cf38 | 1135 | PerlIO_apply_layera(pTHX_ PerlIO *f, const char *mode, |
d9dac8cd | 1136 | PerlIO_list_t *layers, IV n, IV max) |
14a5cf38 | 1137 | { |
14a5cf38 JH |
1138 | int code = 0; |
1139 | while (n < max) { | |
1140 | PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n, NULL); | |
1141 | if (tab) { | |
1142 | if (!PerlIO_push(aTHX_ f, tab, mode, PerlIOArg)) { | |
1143 | code = -1; | |
1144 | break; | |
1145 | } | |
1146 | } | |
1147 | n++; | |
1148 | } | |
1149 | return code; | |
e3f3bf95 NIS |
1150 | } |
1151 | ||
1152 | int | |
ac27b0f5 NIS |
1153 | PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names) |
1154 | { | |
14a5cf38 | 1155 | int code = 0; |
53f1b6d2 | 1156 | if (f && names) { |
3a1ee7e8 | 1157 | PerlIO_list_t *layers = PerlIO_list_alloc(aTHX); |
14a5cf38 JH |
1158 | code = PerlIO_parse_layers(aTHX_ layers, names); |
1159 | if (code == 0) { | |
d9dac8cd | 1160 | code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0, layers->cur); |
14a5cf38 | 1161 | } |
3a1ee7e8 | 1162 | PerlIO_list_free(aTHX_ layers); |
ac27b0f5 | 1163 | } |
14a5cf38 | 1164 | return code; |
ac27b0f5 NIS |
1165 | } |
1166 | ||
f3862f8b | 1167 | |
60382766 | 1168 | /*--------------------------------------------------------------------------------------*/ |
14a5cf38 | 1169 | /* |
71200d45 | 1170 | * Given the abstraction above the public API functions |
14a5cf38 | 1171 | */ |
60382766 NIS |
1172 | |
1173 | int | |
f5b9d040 | 1174 | PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names) |
76ced9ad | 1175 | { |
14a5cf38 | 1176 | PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n", |
fe5a182c | 1177 | (void*)f, PerlIOBase(f)->tab->name, iotype, mode, |
14a5cf38 | 1178 | (names) ? names : "(Null)"); |
03c0554d NIS |
1179 | if (names) { |
1180 | /* Do not flush etc. if (e.g.) switching encodings. | |
1181 | if a pushed layer knows it needs to flush lower layers | |
1182 | (for example :unix which is never going to call them) | |
1183 | it can do the flush when it is pushed. | |
1184 | */ | |
1185 | return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE; | |
1186 | } | |
1187 | else { | |
86e05cf2 | 1188 | /* Fake 5.6 legacy of using this call to turn ON O_TEXT */ |
35990314 | 1189 | #ifdef PERLIO_USING_CRLF |
03c0554d NIS |
1190 | /* Legacy binmode only has meaning if O_TEXT has a value distinct from |
1191 | O_BINARY so we can look for it in mode. | |
1192 | */ | |
1193 | if (!(mode & O_BINARY)) { | |
1194 | /* Text mode */ | |
86e05cf2 NIS |
1195 | /* FIXME?: Looking down the layer stack seems wrong, |
1196 | but is a way of reaching past (say) an encoding layer | |
1197 | to flip CRLF-ness of the layer(s) below | |
1198 | */ | |
03c0554d NIS |
1199 | while (*f) { |
1200 | /* Perhaps we should turn on bottom-most aware layer | |
1201 | e.g. Ilya's idea that UNIX TTY could serve | |
1202 | */ | |
1203 | if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) { | |
1204 | if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) { | |
1205 | /* Not in text mode - flush any pending stuff and flip it */ | |
1206 | PerlIO_flush(f); | |
1207 | PerlIOBase(f)->flags |= PERLIO_F_CRLF; | |
1208 | } | |
1209 | /* Only need to turn it on in one layer so we are done */ | |
1210 | return TRUE; | |
ed53a2bb | 1211 | } |
03c0554d | 1212 | f = PerlIONext(f); |
14a5cf38 | 1213 | } |
03c0554d NIS |
1214 | /* Not finding a CRLF aware layer presumably means we are binary |
1215 | which is not what was requested - so we failed | |
1216 | We _could_ push :crlf layer but so could caller | |
1217 | */ | |
1218 | return FALSE; | |
14a5cf38 | 1219 | } |
6ce75a77 | 1220 | #endif |
86e05cf2 NIS |
1221 | /* Legacy binmode is now _defined_ as being equivalent to pushing :raw |
1222 | So code that used to be here is now in PerlIORaw_pushed(). | |
03c0554d | 1223 | */ |
86e05cf2 | 1224 | return PerlIO_push(aTHX_ f, &PerlIO_raw, Nullch, Nullsv) ? TRUE : FALSE; |
14a5cf38 | 1225 | } |
f5b9d040 NIS |
1226 | } |
1227 | ||
f5b9d040 | 1228 | int |
e87a358a | 1229 | PerlIO__close(pTHX_ PerlIO *f) |
f5b9d040 | 1230 | { |
04892f78 | 1231 | if (PerlIOValid(f)) |
f62ce20a | 1232 | return (*PerlIOBase(f)->tab->Close) (aTHX_ f); |
14a5cf38 | 1233 | else { |
93189314 | 1234 | SETERRNO(EBADF, SS_IVCHAN); |
14a5cf38 JH |
1235 | return -1; |
1236 | } | |
76ced9ad NIS |
1237 | } |
1238 | ||
b931b1d9 | 1239 | int |
e87a358a | 1240 | Perl_PerlIO_close(pTHX_ PerlIO *f) |
b931b1d9 | 1241 | { |
14a5cf38 | 1242 | int code = -1; |
04892f78 | 1243 | if (PerlIOValid(f)) { |
f62ce20a | 1244 | code = (*PerlIOBase(f)->tab->Close) (aTHX_ f); |
14a5cf38 JH |
1245 | while (*f) { |
1246 | PerlIO_pop(aTHX_ f); | |
1247 | } | |
f6c77cf1 | 1248 | } |
14a5cf38 | 1249 | return code; |
b931b1d9 NIS |
1250 | } |
1251 | ||
b931b1d9 | 1252 | int |
e87a358a | 1253 | Perl_PerlIO_fileno(pTHX_ PerlIO *f) |
b931b1d9 | 1254 | { |
04892f78 | 1255 | if (PerlIOValid(f)) |
f62ce20a | 1256 | return (*PerlIOBase(f)->tab->Fileno) (aTHX_ f); |
14a5cf38 | 1257 | else { |
93189314 | 1258 | SETERRNO(EBADF, SS_IVCHAN); |
14a5cf38 JH |
1259 | return -1; |
1260 | } | |
b931b1d9 NIS |
1261 | } |
1262 | ||
1141d9f8 NIS |
1263 | static const char * |
1264 | PerlIO_context_layers(pTHX_ const char *mode) | |
1265 | { | |
14a5cf38 JH |
1266 | const char *type = NULL; |
1267 | /* | |
71200d45 | 1268 | * Need to supply default layer info from open.pm |
14a5cf38 JH |
1269 | */ |
1270 | if (PL_curcop) { | |
1271 | SV *layers = PL_curcop->cop_io; | |
1272 | if (layers) { | |
1273 | STRLEN len; | |
1274 | type = SvPV(layers, len); | |
1275 | if (type && mode[0] != 'r') { | |
1276 | /* | |
71200d45 | 1277 | * Skip to write part |
14a5cf38 JH |
1278 | */ |
1279 | const char *s = strchr(type, 0); | |
eb160463 | 1280 | if (s && (STRLEN)(s - type) < len) { |
14a5cf38 JH |
1281 | type = s + 1; |
1282 | } | |
1283 | } | |
1284 | } | |
1285 | } | |
1286 | return type; | |
1141d9f8 NIS |
1287 | } |
1288 | ||
fcf2db38 | 1289 | static PerlIO_funcs * |
2edd7e44 NIS |
1290 | PerlIO_layer_from_ref(pTHX_ SV *sv) |
1291 | { | |
14a5cf38 | 1292 | /* |
71200d45 | 1293 | * For any scalar type load the handler which is bundled with perl |
14a5cf38 JH |
1294 | */ |
1295 | if (SvTYPE(sv) < SVt_PVAV) | |
e934609f | 1296 | return PerlIO_find_layer(aTHX_ "scalar", 6, 1); |
14a5cf38 JH |
1297 | |
1298 | /* | |
71200d45 | 1299 | * For other types allow if layer is known but don't try and load it |
14a5cf38 JH |
1300 | */ |
1301 | switch (SvTYPE(sv)) { | |
1302 | case SVt_PVAV: | |
1303 | return PerlIO_find_layer(aTHX_ "Array", 5, 0); | |
1304 | case SVt_PVHV: | |
1305 | return PerlIO_find_layer(aTHX_ "Hash", 4, 0); | |
1306 | case SVt_PVCV: | |
1307 | return PerlIO_find_layer(aTHX_ "Code", 4, 0); | |
1308 | case SVt_PVGV: | |
1309 | return PerlIO_find_layer(aTHX_ "Glob", 4, 0); | |
1310 | } | |
1311 | return NULL; | |
2edd7e44 NIS |
1312 | } |
1313 | ||
fcf2db38 | 1314 | PerlIO_list_t * |
14a5cf38 JH |
1315 | PerlIO_resolve_layers(pTHX_ const char *layers, |
1316 | const char *mode, int narg, SV **args) | |
1317 | { | |
1318 | PerlIO_list_t *def = PerlIO_default_layers(aTHX); | |
1319 | int incdef = 1; | |
a1ea730d | 1320 | if (!PL_perlio) |
14a5cf38 JH |
1321 | PerlIO_stdstreams(aTHX); |
1322 | if (narg) { | |
1323 | SV *arg = *args; | |
1324 | /* | |
71200d45 NIS |
1325 | * If it is a reference but not an object see if we have a handler |
1326 | * for it | |
14a5cf38 JH |
1327 | */ |
1328 | if (SvROK(arg) && !sv_isobject(arg)) { | |
1329 | PerlIO_funcs *handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg)); | |
1330 | if (handler) { | |
3a1ee7e8 NIS |
1331 | def = PerlIO_list_alloc(aTHX); |
1332 | PerlIO_list_push(aTHX_ def, handler, &PL_sv_undef); | |
14a5cf38 JH |
1333 | incdef = 0; |
1334 | } | |
1335 | /* | |
e934609f | 1336 | * Don't fail if handler cannot be found :via(...) etc. may do |
14a5cf38 | 1337 | * something sensible else we will just stringfy and open |
71200d45 | 1338 | * resulting string. |
14a5cf38 JH |
1339 | */ |
1340 | } | |
1341 | } | |
1342 | if (!layers) | |
1343 | layers = PerlIO_context_layers(aTHX_ mode); | |
1344 | if (layers && *layers) { | |
1345 | PerlIO_list_t *av; | |
1346 | if (incdef) { | |
1347 | IV i = def->cur; | |
3a1ee7e8 | 1348 | av = PerlIO_list_alloc(aTHX); |
14a5cf38 | 1349 | for (i = 0; i < def->cur; i++) { |
3a1ee7e8 | 1350 | PerlIO_list_push(aTHX_ av, def->array[i].funcs, |
14a5cf38 JH |
1351 | def->array[i].arg); |
1352 | } | |
1353 | } | |
1354 | else { | |
1355 | av = def; | |
1356 | } | |
0cff2cf3 NIS |
1357 | if (PerlIO_parse_layers(aTHX_ av, layers) == 0) { |
1358 | return av; | |
1359 | } | |
1360 | else { | |
1361 | PerlIO_list_free(aTHX_ av); | |
1362 | return (PerlIO_list_t *) NULL; | |
1363 | } | |
14a5cf38 JH |
1364 | } |
1365 | else { | |
1366 | if (incdef) | |
1367 | def->refcnt++; | |
1368 | return def; | |
1369 | } | |
ee518936 NIS |
1370 | } |
1371 | ||
1372 | PerlIO * | |
14a5cf38 JH |
1373 | PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd, |
1374 | int imode, int perm, PerlIO *f, int narg, SV **args) | |
1375 | { | |
1376 | if (!f && narg == 1 && *args == &PL_sv_undef) { | |
1377 | if ((f = PerlIO_tmpfile())) { | |
1378 | if (!layers) | |
1379 | layers = PerlIO_context_layers(aTHX_ mode); | |
1380 | if (layers && *layers) | |
1381 | PerlIO_apply_layers(aTHX_ f, mode, layers); | |
1382 | } | |
1383 | } | |
1384 | else { | |
1385 | PerlIO_list_t *layera = NULL; | |
1386 | IV n; | |
1387 | PerlIO_funcs *tab = NULL; | |
04892f78 | 1388 | if (PerlIOValid(f)) { |
14a5cf38 | 1389 | /* |
71200d45 NIS |
1390 | * This is "reopen" - it is not tested as perl does not use it |
1391 | * yet | |
14a5cf38 JH |
1392 | */ |
1393 | PerlIOl *l = *f; | |
3a1ee7e8 | 1394 | layera = PerlIO_list_alloc(aTHX); |
14a5cf38 | 1395 | while (l) { |
04892f78 NIS |
1396 | SV *arg = (l->tab->Getarg) |
1397 | ? (*l->tab->Getarg) (aTHX_ &l, NULL, 0) | |
1398 | : &PL_sv_undef; | |
3a1ee7e8 | 1399 | PerlIO_list_push(aTHX_ layera, l->tab, arg); |
14a5cf38 JH |
1400 | l = *PerlIONext(&l); |
1401 | } | |
1402 | } | |
1403 | else { | |
1404 | layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args); | |
0cff2cf3 NIS |
1405 | if (!layera) { |
1406 | return NULL; | |
1407 | } | |
14a5cf38 JH |
1408 | } |
1409 | /* | |
71200d45 | 1410 | * Start at "top" of layer stack |
14a5cf38 JH |
1411 | */ |
1412 | n = layera->cur - 1; | |
1413 | while (n >= 0) { | |
1414 | PerlIO_funcs *t = PerlIO_layer_fetch(aTHX_ layera, n, NULL); | |
1415 | if (t && t->Open) { | |
1416 | tab = t; | |
1417 | break; | |
1418 | } | |
1419 | n--; | |
1420 | } | |
1421 | if (tab) { | |
1422 | /* | |
71200d45 | 1423 | * Found that layer 'n' can do opens - call it |
14a5cf38 | 1424 | */ |
7cf31beb | 1425 | if (narg > 1 && !(tab->kind & PERLIO_K_MULTIARG)) { |
3b8752bb | 1426 | Perl_croak(aTHX_ "More than one argument to open(,':%s')",tab->name); |
7cf31beb | 1427 | } |
14a5cf38 | 1428 | PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n", |
fe5a182c JH |
1429 | tab->name, layers, mode, fd, imode, perm, |
1430 | (void*)f, narg, (void*)args); | |
14a5cf38 JH |
1431 | f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm, |
1432 | f, narg, args); | |
1433 | if (f) { | |
1434 | if (n + 1 < layera->cur) { | |
1435 | /* | |
1436 | * More layers above the one that we used to open - | |
71200d45 | 1437 | * apply them now |
14a5cf38 | 1438 | */ |
d9dac8cd NIS |
1439 | if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1, layera->cur) != 0) { |
1440 | /* If pushing layers fails close the file */ | |
1441 | PerlIO_close(f); | |
14a5cf38 JH |
1442 | f = NULL; |
1443 | } | |
1444 | } | |
1445 | } | |
1446 | } | |
3a1ee7e8 | 1447 | PerlIO_list_free(aTHX_ layera); |
14a5cf38 JH |
1448 | } |
1449 | return f; | |
ee518936 | 1450 | } |
b931b1d9 NIS |
1451 | |
1452 | ||
9e353e3b | 1453 | SSize_t |
e87a358a | 1454 | Perl_PerlIO_read(pTHX_ PerlIO *f, void *vbuf, Size_t count) |
6f9d8c32 | 1455 | { |
04892f78 | 1456 | if (PerlIOValid(f)) |
f62ce20a | 1457 | return (*PerlIOBase(f)->tab->Read) (aTHX_ f, vbuf, count); |
14a5cf38 | 1458 | else { |
93189314 | 1459 | SETERRNO(EBADF, SS_IVCHAN); |
14a5cf38 JH |
1460 | return -1; |
1461 | } | |
760ac839 LW |
1462 | } |
1463 | ||
313ca112 | 1464 | SSize_t |
e87a358a | 1465 | Perl_PerlIO_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count) |
760ac839 | 1466 | { |
04892f78 | 1467 | if (PerlIOValid(f)) |
f62ce20a | 1468 | return (*PerlIOBase(f)->tab->Unread) (aTHX_ f, vbuf, count); |
14a5cf38 | 1469 | else { |
93189314 | 1470 | SETERRNO(EBADF, SS_IVCHAN); |
14a5cf38 JH |
1471 | return -1; |
1472 | } | |
760ac839 LW |
1473 | } |
1474 | ||
9e353e3b | 1475 | SSize_t |
e87a358a | 1476 | Perl_PerlIO_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count) |
760ac839 | 1477 | { |
04892f78 | 1478 | if (PerlIOValid(f)) |
f62ce20a | 1479 | return (*PerlIOBase(f)->tab->Write) (aTHX_ f, vbuf, count); |
14a5cf38 | 1480 | else { |
93189314 | 1481 | SETERRNO(EBADF, SS_IVCHAN); |
14a5cf38 JH |
1482 | return -1; |
1483 | } | |
760ac839 LW |
1484 | } |
1485 | ||
6f9d8c32 | 1486 | int |
e87a358a | 1487 | Perl_PerlIO_seek(pTHX_ PerlIO *f, Off_t offset, int whence) |
760ac839 | 1488 | { |
04892f78 | 1489 | if (PerlIOValid(f)) |
f62ce20a | 1490 | return (*PerlIOBase(f)->tab->Seek) (aTHX_ f, offset, whence); |
14a5cf38 | 1491 | else { |
93189314 | 1492 | SETERRNO(EBADF, SS_IVCHAN); |
14a5cf38 JH |
1493 | return -1; |
1494 | } | |
760ac839 LW |
1495 | } |
1496 | ||
9e353e3b | 1497 | Off_t |
e87a358a | 1498 | Perl_PerlIO_tell(pTHX_ PerlIO *f) |
760ac839 | 1499 | { |
04892f78 | 1500 | if (PerlIOValid(f)) |
f62ce20a | 1501 | return (*PerlIOBase(f)->tab->Tell) (aTHX_ f); |
14a5cf38 | 1502 | else { |
93189314 | 1503 | SETERRNO(EBADF, SS_IVCHAN); |
14a5cf38 JH |
1504 | return -1; |
1505 | } | |
760ac839 LW |
1506 | } |
1507 | ||
6f9d8c32 | 1508 | int |
e87a358a | 1509 | Perl_PerlIO_flush(pTHX_ PerlIO *f) |
760ac839 | 1510 | { |
14a5cf38 JH |
1511 | if (f) { |
1512 | if (*f) { | |
1513 | PerlIO_funcs *tab = PerlIOBase(f)->tab; | |
1514 | if (tab && tab->Flush) { | |
f62ce20a | 1515 | return (*tab->Flush) (aTHX_ f); |
14a5cf38 JH |
1516 | } |
1517 | else { | |
fe5a182c | 1518 | PerlIO_debug("Cannot flush f=%p :%s\n", (void*)f, tab->name); |
93189314 | 1519 | SETERRNO(EBADF, SS_IVCHAN); |
14a5cf38 JH |
1520 | return -1; |
1521 | } | |
1522 | } | |
1523 | else { | |
fe5a182c | 1524 | PerlIO_debug("Cannot flush f=%p\n", (void*)f); |
93189314 | 1525 | SETERRNO(EBADF, SS_IVCHAN); |
14a5cf38 JH |
1526 | return -1; |
1527 | } | |
1528 | } | |
1529 | else { | |
1530 | /* | |
1531 | * Is it good API design to do flush-all on NULL, a potentially | |
1532 | * errorneous input? Maybe some magical value (PerlIO* | |
1533 | * PERLIO_FLUSH_ALL = (PerlIO*)-1;)? Yes, stdio does similar | |
1534 | * things on fflush(NULL), but should we be bound by their design | |
71200d45 | 1535 | * decisions? --jhi |
14a5cf38 | 1536 | */ |
a1ea730d | 1537 | PerlIO **table = &PL_perlio; |
14a5cf38 JH |
1538 | int code = 0; |
1539 | while ((f = *table)) { | |
1540 | int i; | |
1541 | table = (PerlIO **) (f++); | |
1542 | for (i = 1; i < PERLIO_TABLE_SIZE; i++) { | |
1543 | if (*f && PerlIO_flush(f) != 0) | |
1544 | code = -1; | |
1545 | f++; | |
1546 | } | |
1547 | } | |
1548 | return code; | |
1549 | } | |
760ac839 LW |
1550 | } |
1551 | ||
a9c883f6 | 1552 | void |
f62ce20a | 1553 | PerlIOBase_flush_linebuf(pTHX) |
a9c883f6 | 1554 | { |
a1ea730d | 1555 | PerlIO **table = &PL_perlio; |
14a5cf38 JH |
1556 | PerlIO *f; |
1557 | while ((f = *table)) { | |
1558 | int i; | |
1559 | table = (PerlIO **) (f++); | |
1560 | for (i = 1; i < PERLIO_TABLE_SIZE; i++) { | |
1561 | if (*f | |
1562 | && (PerlIOBase(f)-> | |
1563 | flags & (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE)) | |
1564 | == (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE)) | |
1565 | PerlIO_flush(f); | |
1566 | f++; | |
1567 | } | |
a9c883f6 | 1568 | } |
a9c883f6 NIS |
1569 | } |
1570 | ||
06da4f11 | 1571 | int |
e87a358a | 1572 | Perl_PerlIO_fill(pTHX_ PerlIO *f) |
06da4f11 | 1573 | { |
04892f78 | 1574 | if (PerlIOValid(f)) |
f62ce20a | 1575 | return (*PerlIOBase(f)->tab->Fill) (aTHX_ f); |
14a5cf38 | 1576 | else { |
93189314 | 1577 | SETERRNO(EBADF, SS_IVCHAN); |
14a5cf38 JH |
1578 | return -1; |
1579 | } | |
06da4f11 NIS |
1580 | } |
1581 | ||
f3862f8b NIS |
1582 | int |
1583 | PerlIO_isutf8(PerlIO *f) | |
1584 | { | |
04892f78 | 1585 | if (PerlIOValid(f)) |
14a5cf38 JH |
1586 | return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0; |
1587 | else { | |
93189314 | 1588 | SETERRNO(EBADF, SS_IVCHAN); |
14a5cf38 JH |
1589 | return -1; |
1590 | } | |
f3862f8b NIS |
1591 | } |
1592 | ||
6f9d8c32 | 1593 | int |
e87a358a | 1594 | Perl_PerlIO_eof(pTHX_ PerlIO *f) |
760ac839 | 1595 | { |
04892f78 | 1596 | if (PerlIOValid(f)) |
f62ce20a | 1597 | return (*PerlIOBase(f)->tab->Eof) (aTHX_ f); |
14a5cf38 | 1598 | else { |
93189314 | 1599 | SETERRNO(EBADF, SS_IVCHAN); |
14a5cf38 JH |
1600 | return -1; |
1601 | } | |
9e353e3b NIS |
1602 | } |
1603 | ||
9e353e3b | 1604 | int |
e87a358a | 1605 | Perl_PerlIO_error(pTHX_ PerlIO *f) |
9e353e3b | 1606 | { |
04892f78 | 1607 | if (PerlIOValid(f)) |
f62ce20a | 1608 | return (*PerlIOBase(f)->tab->Error) (aTHX_ f); |
14a5cf38 | 1609 | else { |
93189314 | 1610 | SETERRNO(EBADF, SS_IVCHAN); |
14a5cf38 JH |
1611 | return -1; |
1612 | } | |
9e353e3b NIS |
1613 | } |
1614 | ||
9e353e3b | 1615 | void |
e87a358a | 1616 | Perl_PerlIO_clearerr(pTHX_ PerlIO *f) |
9e353e3b | 1617 | { |
04892f78 | 1618 | if (PerlIOValid(f)) |
f62ce20a | 1619 | (*PerlIOBase(f)->tab->Clearerr) (aTHX_ f); |
14a5cf38 | 1620 | else |
93189314 | 1621 | SETERRNO(EBADF, SS_IVCHAN); |
9e353e3b NIS |
1622 | } |
1623 | ||
9e353e3b | 1624 | void |
e87a358a | 1625 | Perl_PerlIO_setlinebuf(pTHX_ PerlIO *f) |
9e353e3b | 1626 | { |
04892f78 | 1627 | if (PerlIOValid(f)) |
f62ce20a | 1628 | (*PerlIOBase(f)->tab->Setlinebuf) (aTHX_ f); |
14a5cf38 | 1629 | else |
93189314 | 1630 | SETERRNO(EBADF, SS_IVCHAN); |
9e353e3b NIS |
1631 | } |
1632 | ||
9e353e3b NIS |
1633 | int |
1634 | PerlIO_has_base(PerlIO *f) | |
1635 | { | |
04892f78 | 1636 | if (PerlIOValid(f)) { |
14a5cf38 JH |
1637 | return (PerlIOBase(f)->tab->Get_base != NULL); |
1638 | } | |
1639 | return 0; | |
760ac839 LW |
1640 | } |
1641 | ||
9e353e3b NIS |
1642 | int |
1643 | PerlIO_fast_gets(PerlIO *f) | |
760ac839 | 1644 | { |
04892f78 | 1645 | if (PerlIOValid(f) && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) { |
14a5cf38 JH |
1646 | PerlIO_funcs *tab = PerlIOBase(f)->tab; |
1647 | return (tab->Set_ptrcnt != NULL); | |
1648 | } | |
1649 | return 0; | |
9e353e3b NIS |
1650 | } |
1651 | ||
9e353e3b NIS |
1652 | int |
1653 | PerlIO_has_cntptr(PerlIO *f) | |
1654 | { | |
04892f78 | 1655 | if (PerlIOValid(f)) { |
14a5cf38 JH |
1656 | PerlIO_funcs *tab = PerlIOBase(f)->tab; |
1657 | return (tab->Get_ptr != NULL && tab->Get_cnt != NULL); | |
1658 | } | |
1659 | return 0; | |
9e353e3b NIS |
1660 | } |
1661 | ||
9e353e3b NIS |
1662 | int |
1663 | PerlIO_canset_cnt(PerlIO *f) | |
1664 | { | |
04892f78 | 1665 | if (PerlIOValid(f)) { |
14a5cf38 JH |
1666 | PerlIOl *l = PerlIOBase(f); |
1667 | return (l->tab->Set_ptrcnt != NULL); | |
1668 | } | |
1669 | return 0; | |
760ac839 LW |
1670 | } |
1671 | ||
888911fc | 1672 | STDCHAR * |
e87a358a | 1673 | Perl_PerlIO_get_base(pTHX_ PerlIO *f) |
760ac839 | 1674 | { |
04892f78 | 1675 | if (PerlIOValid(f)) |
f62ce20a | 1676 | return (*PerlIOBase(f)->tab->Get_base) (aTHX_ f); |
14a5cf38 | 1677 | return NULL; |
9e353e3b NIS |
1678 | } |
1679 | ||
9e353e3b | 1680 | int |
e87a358a | 1681 | Perl_PerlIO_get_bufsiz(pTHX_ PerlIO *f) |
9e353e3b | 1682 | { |
04892f78 | 1683 | if (PerlIOValid(f)) |
f62ce20a | 1684 | return (*PerlIOBase(f)->tab->Get_bufsiz) (aTHX_ f); |
14a5cf38 | 1685 | return 0; |
9e353e3b NIS |
1686 | } |
1687 | ||
9e353e3b | 1688 | STDCHAR * |
e87a358a | 1689 | Perl_PerlIO_get_ptr(pTHX_ PerlIO *f) |
9e353e3b | 1690 | { |
04892f78 NIS |
1691 | if (PerlIOValid(f)) { |
1692 | PerlIO_funcs *tab = PerlIOBase(f)->tab; | |
1693 | if (tab->Get_ptr == NULL) | |
1694 | return NULL; | |
1695 | return (*tab->Get_ptr) (aTHX_ f); | |
1696 | } | |
1697 | return NULL; | |
9e353e3b NIS |
1698 | } |
1699 | ||
05d1247b | 1700 | int |
e87a358a | 1701 | Perl_PerlIO_get_cnt(pTHX_ PerlIO *f) |
9e353e3b | 1702 | { |
04892f78 NIS |
1703 | if (PerlIOValid(f)) { |
1704 | PerlIO_funcs *tab = PerlIOBase(f)->tab; | |
1705 | if (tab->Get_cnt == NULL) | |
1706 | return 0; | |
1707 | return (*tab->Get_cnt) (aTHX_ f); | |
1708 | } | |
1709 | return 0; | |
9e353e3b NIS |
1710 | } |
1711 | ||
9e353e3b | 1712 | void |
e87a358a | 1713 | Perl_PerlIO_set_cnt(pTHX_ PerlIO *f, int cnt) |
9e353e3b | 1714 | { |
04892f78 NIS |
1715 | if (PerlIOValid(f)) { |
1716 | (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, NULL, cnt); | |
1717 | } | |
9e353e3b NIS |
1718 | } |
1719 | ||
9e353e3b | 1720 | void |
e87a358a | 1721 | Perl_PerlIO_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, int cnt) |
9e353e3b | 1722 | { |
04892f78 NIS |
1723 | if (PerlIOValid(f)) { |
1724 | PerlIO_funcs *tab = PerlIOBase(f)->tab; | |
1725 | if (tab->Set_ptrcnt == NULL) { | |
1726 | Perl_croak(aTHX_ "PerlIO buffer snooping abuse"); | |
1727 | } | |
1728 | (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, ptr, cnt); | |
14a5cf38 | 1729 | } |
9e353e3b NIS |
1730 | } |
1731 | ||
4ec2216f | 1732 | |
9e353e3b | 1733 | /*--------------------------------------------------------------------------------------*/ |
14a5cf38 | 1734 | /* |
71200d45 | 1735 | * utf8 and raw dummy layers |
14a5cf38 | 1736 | */ |
dfebf958 | 1737 | |
26fb694e | 1738 | IV |
2dc2558e | 1739 | PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
26fb694e | 1740 | { |
00f51856 | 1741 | if (PerlIOValid(f)) { |
14a5cf38 JH |
1742 | if (tab->kind & PERLIO_K_UTF8) |
1743 | PerlIOBase(f)->flags |= PERLIO_F_UTF8; | |
1744 | else | |
1745 | PerlIOBase(f)->flags &= ~PERLIO_F_UTF8; | |
1746 | return 0; | |
1747 | } | |
1748 | return -1; | |
26fb694e NIS |
1749 | } |
1750 | ||
dfebf958 | 1751 | PerlIO_funcs PerlIO_utf8 = { |
2dc2558e | 1752 | sizeof(PerlIO_funcs), |
14a5cf38 | 1753 | "utf8", |
2dc2558e | 1754 | 0, |
6874a2de | 1755 | PERLIO_K_DUMMY | PERLIO_K_UTF8, |
14a5cf38 JH |
1756 | PerlIOUtf8_pushed, |
1757 | NULL, | |
1758 | NULL, | |
1759 | NULL, | |
1760 | NULL, | |
1761 | NULL, | |
1762 | NULL, | |
1763 | NULL, | |
1764 | NULL, | |
1765 | NULL, | |
1766 | NULL, | |
22569500 NIS |
1767 | NULL, /* flush */ |
1768 | NULL, /* fill */ | |
14a5cf38 JH |
1769 | NULL, |
1770 | NULL, | |
1771 | NULL, | |
1772 | NULL, | |
22569500 NIS |
1773 | NULL, /* get_base */ |
1774 | NULL, /* get_bufsiz */ | |
1775 | NULL, /* get_ptr */ | |
1776 | NULL, /* get_cnt */ | |
1777 | NULL, /* set_ptrcnt */ | |
26fb694e NIS |
1778 | }; |
1779 | ||
1780 | PerlIO_funcs PerlIO_byte = { | |
2dc2558e | 1781 | sizeof(PerlIO_funcs), |
14a5cf38 | 1782 | "bytes", |
2dc2558e | 1783 | 0, |
14a5cf38 JH |
1784 | PERLIO_K_DUMMY, |
1785 | PerlIOUtf8_pushed, | |
1786 | NULL, | |
1787 | NULL, | |
1788 | NULL, | |
1789 | NULL, | |
1790 | NULL, | |
1791 | NULL, | |
1792 | NULL, | |
1793 | NULL, | |
1794 | NULL, | |
1795 | NULL, | |
22569500 NIS |
1796 | NULL, /* flush */ |
1797 | NULL, /* fill */ | |
14a5cf38 JH |
1798 | NULL, |
1799 | NULL, | |
1800 | NULL, | |
1801 | NULL, | |
22569500 NIS |
1802 | NULL, /* get_base */ |
1803 | NULL, /* get_bufsiz */ | |
1804 | NULL, /* get_ptr */ | |
1805 | NULL, /* get_cnt */ | |
1806 | NULL, /* set_ptrcnt */ | |
dfebf958 NIS |
1807 | }; |
1808 | ||
1809 | PerlIO * | |
14a5cf38 JH |
1810 | PerlIORaw_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers, |
1811 | IV n, const char *mode, int fd, int imode, int perm, | |
1812 | PerlIO *old, int narg, SV **args) | |
dfebf958 | 1813 | { |
14a5cf38 JH |
1814 | PerlIO_funcs *tab = PerlIO_default_btm(); |
1815 | return (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm, | |
1816 | old, narg, args); | |
dfebf958 NIS |
1817 | } |
1818 | ||
1819 | PerlIO_funcs PerlIO_raw = { | |
2dc2558e | 1820 | sizeof(PerlIO_funcs), |
14a5cf38 | 1821 | "raw", |
2dc2558e | 1822 | 0, |
14a5cf38 JH |
1823 | PERLIO_K_DUMMY, |
1824 | PerlIORaw_pushed, | |
1825 | PerlIOBase_popped, | |
1826 | PerlIORaw_open, | |
1827 | NULL, | |
1828 | NULL, | |
1829 | NULL, | |
1830 | NULL, | |
1831 | NULL, | |
1832 | NULL, | |
1833 | NULL, | |
1834 | NULL, | |
22569500 NIS |
1835 | NULL, /* flush */ |
1836 | NULL, /* fill */ | |
14a5cf38 JH |
1837 | NULL, |
1838 | NULL, | |
1839 | NULL, | |
1840 | NULL, | |
22569500 NIS |
1841 | NULL, /* get_base */ |
1842 | NULL, /* get_bufsiz */ | |
1843 | NULL, /* get_ptr */ | |
1844 | NULL, /* get_cnt */ | |
1845 | NULL, /* set_ptrcnt */ | |
dfebf958 NIS |
1846 | }; |
1847 | /*--------------------------------------------------------------------------------------*/ | |
1848 | /*--------------------------------------------------------------------------------------*/ | |
14a5cf38 | 1849 | /* |
71200d45 | 1850 | * "Methods" of the "base class" |
14a5cf38 | 1851 | */ |
9e353e3b NIS |
1852 | |
1853 | IV | |
f62ce20a | 1854 | PerlIOBase_fileno(pTHX_ PerlIO *f) |
9e353e3b | 1855 | { |
04892f78 | 1856 | return PerlIOValid(f) ? PerlIO_fileno(PerlIONext(f)) : -1; |
9e353e3b NIS |
1857 | } |
1858 | ||
f5b9d040 | 1859 | char * |
81428673 | 1860 | PerlIO_modestr(PerlIO * f, char *buf) |
14a5cf38 JH |
1861 | { |
1862 | char *s = buf; | |
81428673 NIS |
1863 | if (PerlIOValid(f)) { |
1864 | IV flags = PerlIOBase(f)->flags; | |
1865 | if (flags & PERLIO_F_APPEND) { | |
1866 | *s++ = 'a'; | |
1867 | if (flags & PERLIO_F_CANREAD) { | |
1868 | *s++ = '+'; | |
1869 | } | |
14a5cf38 | 1870 | } |
81428673 NIS |
1871 | else if (flags & PERLIO_F_CANREAD) { |
1872 | *s++ = 'r'; | |
1873 | if (flags & PERLIO_F_CANWRITE) | |
1874 | *s++ = '+'; | |
1875 | } | |
1876 | else if (flags & PERLIO_F_CANWRITE) { | |
1877 | *s++ = 'w'; | |
1878 | if (flags & PERLIO_F_CANREAD) { | |
1879 | *s++ = '+'; | |
1880 | } | |
14a5cf38 | 1881 | } |
35990314 | 1882 | #ifdef PERLIO_USING_CRLF |
81428673 NIS |
1883 | if (!(flags & PERLIO_F_CRLF)) |
1884 | *s++ = 'b'; | |
5f1a76d0 | 1885 | #endif |
81428673 | 1886 | } |
14a5cf38 JH |
1887 | *s = '\0'; |
1888 | return buf; | |
f5b9d040 NIS |
1889 | } |
1890 | ||
81428673 | 1891 | |
76ced9ad | 1892 | IV |
2dc2558e | 1893 | PerlIOBase_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
9e353e3b | 1894 | { |
14a5cf38 | 1895 | PerlIOl *l = PerlIOBase(f); |
b7953727 | 1896 | #if 0 |
14a5cf38 JH |
1897 | const char *omode = mode; |
1898 | char temp[8]; | |
b7953727 | 1899 | #endif |
14a5cf38 JH |
1900 | l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | |
1901 | PERLIO_F_TRUNCATE | PERLIO_F_APPEND); | |
1902 | if (tab->Set_ptrcnt != NULL) | |
1903 | l->flags |= PERLIO_F_FASTGETS; | |
1904 | if (mode) { | |
1905 | if (*mode == '#' || *mode == 'I') | |
1906 | mode++; | |
1907 | switch (*mode++) { | |
1908 | case 'r': | |
1909 | l->flags |= PERLIO_F_CANREAD; | |
1910 | break; | |
1911 | case 'a': | |
1912 | l->flags |= PERLIO_F_APPEND | PERLIO_F_CANWRITE; | |
1913 | break; | |
1914 | case 'w': | |
1915 | l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE; | |
1916 | break; | |
1917 | default: | |
93189314 | 1918 | SETERRNO(EINVAL, LIB_INVARG); |
14a5cf38 JH |
1919 | return -1; |
1920 | } | |
1921 | while (*mode) { | |
1922 | switch (*mode++) { | |
1923 | case '+': | |
1924 | l->flags |= PERLIO_F_CANREAD | PERLIO_F_CANWRITE; | |
1925 | break; | |
1926 | case 'b': | |
1927 | l->flags &= ~PERLIO_F_CRLF; | |
1928 | break; | |
1929 | case 't': | |
1930 | l->flags |= PERLIO_F_CRLF; | |
1931 | break; | |
1932 | default: | |
93189314 | 1933 | SETERRNO(EINVAL, LIB_INVARG); |
14a5cf38 JH |
1934 | return -1; |
1935 | } | |
1936 | } | |
1937 | } | |
1938 | else { | |
1939 | if (l->next) { | |
1940 | l->flags |= l->next->flags & | |
1941 | (PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_TRUNCATE | | |
1942 | PERLIO_F_APPEND); | |
1943 | } | |
1944 | } | |
5e2ab84b | 1945 | #if 0 |
14a5cf38 JH |
1946 | PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08" UVxf " (%s)\n", |
1947 | f, PerlIOBase(f)->tab->name, (omode) ? omode : "(Null)", | |
1948 | l->flags, PerlIO_modestr(f, temp)); | |
5e2ab84b | 1949 | #endif |
14a5cf38 | 1950 | return 0; |
76ced9ad NIS |
1951 | } |
1952 | ||
1953 | IV | |
f62ce20a | 1954 | PerlIOBase_popped(pTHX_ PerlIO *f) |
76ced9ad | 1955 | { |
14a5cf38 | 1956 | return 0; |
760ac839 LW |
1957 | } |
1958 | ||
9e353e3b | 1959 | SSize_t |
f62ce20a | 1960 | PerlIOBase_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count) |
9e353e3b | 1961 | { |
14a5cf38 | 1962 | /* |
71200d45 | 1963 | * Save the position as current head considers it |
14a5cf38 JH |
1964 | */ |
1965 | Off_t old = PerlIO_tell(f); | |
1966 | SSize_t done; | |
1967 | PerlIO_push(aTHX_ f, &PerlIO_pending, "r", Nullsv); | |
1968 | PerlIOSelf(f, PerlIOBuf)->posn = old; | |
f62ce20a | 1969 | done = PerlIOBuf_unread(aTHX_ f, vbuf, count); |
14a5cf38 | 1970 | return done; |
9e353e3b NIS |
1971 | } |
1972 | ||
f6c77cf1 | 1973 | SSize_t |
f62ce20a | 1974 | PerlIOBase_read(pTHX_ PerlIO *f, void *vbuf, Size_t count) |
f6c77cf1 | 1975 | { |
14a5cf38 JH |
1976 | STDCHAR *buf = (STDCHAR *) vbuf; |
1977 | if (f) { | |
1978 | if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD)) | |
1979 | return 0; | |
1980 | while (count > 0) { | |
1981 | SSize_t avail = PerlIO_get_cnt(f); | |
1982 | SSize_t take = 0; | |
1983 | if (avail > 0) | |
eb160463 | 1984 | take = ((SSize_t)count < avail) ? count : avail; |
14a5cf38 JH |
1985 | if (take > 0) { |
1986 | STDCHAR *ptr = PerlIO_get_ptr(f); | |
1987 | Copy(ptr, buf, take, STDCHAR); | |
1988 | PerlIO_set_ptrcnt(f, ptr + take, (avail -= take)); | |
1989 | count -= take; | |
1990 | buf += take; | |
1991 | } | |
1992 | if (count > 0 && avail <= 0) { | |
1993 | if (PerlIO_fill(f) != 0) | |
1994 | break; | |
1995 | } | |
1996 | } | |
1997 | return (buf - (STDCHAR *) vbuf); | |
1998 | } | |
f6c77cf1 | 1999 | return 0; |
f6c77cf1 NIS |
2000 | } |
2001 | ||
9e353e3b | 2002 | IV |
f62ce20a | 2003 | PerlIOBase_noop_ok(pTHX_ PerlIO *f) |
9e353e3b | 2004 | { |
14a5cf38 | 2005 | return 0; |
9e353e3b NIS |
2006 | } |
2007 | ||
2008 | IV | |
f62ce20a | 2009 | PerlIOBase_noop_fail(pTHX_ PerlIO *f) |
06da4f11 | 2010 | { |
14a5cf38 | 2011 | return -1; |
06da4f11 NIS |
2012 | } |
2013 | ||
2014 | IV | |
f62ce20a | 2015 | PerlIOBase_close(pTHX_ PerlIO *f) |
9e353e3b | 2016 | { |
14a5cf38 JH |
2017 | IV code = 0; |
2018 | PerlIO *n = PerlIONext(f); | |
2019 | if (PerlIO_flush(f) != 0) | |
2020 | code = -1; | |
04892f78 | 2021 | if (PerlIOValid(n) && (*PerlIOBase(n)->tab->Close)(aTHX_ n) != 0) |
14a5cf38 JH |
2022 | code = -1; |
2023 | PerlIOBase(f)->flags &= | |
2024 | ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN); | |
2025 | return code; | |
9e353e3b NIS |
2026 | } |
2027 | ||
2028 | IV | |
f62ce20a | 2029 | PerlIOBase_eof(pTHX_ PerlIO *f) |
9e353e3b | 2030 | { |
04892f78 | 2031 | if (PerlIOValid(f)) { |
14a5cf38 JH |
2032 | return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0; |
2033 | } | |
2034 | return 1; | |
9e353e3b NIS |
2035 | } |
2036 | ||
2037 | IV | |
f62ce20a | 2038 | PerlIOBase_error(pTHX_ PerlIO *f) |
9e353e3b | 2039 | { |
04892f78 | 2040 | if (PerlIOValid(f)) { |
14a5cf38 JH |
2041 | return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0; |
2042 | } | |
2043 | return 1; | |
9e353e3b NIS |
2044 | } |
2045 | ||
2046 | void | |
f62ce20a | 2047 | PerlIOBase_clearerr(pTHX_ PerlIO *f) |
9e353e3b | 2048 | { |
04892f78 | 2049 | if (PerlIOValid(f)) { |
14a5cf38 JH |
2050 | PerlIO *n = PerlIONext(f); |
2051 | PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF); | |
04892f78 | 2052 | if (PerlIOValid(n)) |
14a5cf38 JH |
2053 | PerlIO_clearerr(n); |
2054 | } | |
9e353e3b NIS |
2055 | } |
2056 | ||
2057 | void | |
f62ce20a | 2058 | PerlIOBase_setlinebuf(pTHX_ PerlIO *f) |
9e353e3b | 2059 | { |
04892f78 | 2060 | if (PerlIOValid(f)) { |
14a5cf38 JH |
2061 | PerlIOBase(f)->flags |= PERLIO_F_LINEBUF; |
2062 | } | |
9e353e3b NIS |
2063 | } |
2064 | ||
93a8090d NIS |
2065 | SV * |
2066 | PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param) | |
2067 | { | |
2068 | if (!arg) | |
2069 | return Nullsv; | |
2070 | #ifdef sv_dup | |
2071 | if (param) { | |
2072 | return sv_dup(arg, param); | |
2073 | } | |
2074 | else { | |
2075 | return newSVsv(arg); | |
2076 | } | |
2077 | #else | |
2078 | return newSVsv(arg); | |
2079 | #endif | |
2080 | } | |
2081 | ||
2082 | PerlIO * | |
ecdeb87c | 2083 | PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags) |
93a8090d NIS |
2084 | { |
2085 | PerlIO *nexto = PerlIONext(o); | |
04892f78 | 2086 | if (PerlIOValid(nexto)) { |
93a8090d | 2087 | PerlIO_funcs *tab = PerlIOBase(nexto)->tab; |
ecdeb87c | 2088 | f = (*tab->Dup)(aTHX_ f, nexto, param, flags); |
93a8090d NIS |
2089 | } |
2090 | if (f) { | |
2091 | PerlIO_funcs *self = PerlIOBase(o)->tab; | |
2092 | SV *arg = Nullsv; | |
2093 | char buf[8]; | |
fe5a182c JH |
2094 | PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n", |
2095 | self->name, (void*)f, (void*)o, (void*)param); | |
93a8090d | 2096 | if (self->Getarg) { |
ecdeb87c | 2097 | arg = (*self->Getarg)(aTHX_ o,param,flags); |
93a8090d NIS |
2098 | } |
2099 | f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg); | |
ecdeb87c | 2100 | if (arg) { |
93a8090d NIS |
2101 | SvREFCNT_dec(arg); |
2102 | } | |
2103 | } | |
2104 | return f; | |
2105 | } | |
2106 | ||
2107 | #define PERLIO_MAX_REFCOUNTABLE_FD 2048 | |
168d5872 | 2108 | #ifdef USE_THREADS |
93a8090d | 2109 | perl_mutex PerlIO_mutex; |
93a8090d | 2110 | #endif |
168d5872 | 2111 | int PerlIO_fd_refcnt[PERLIO_MAX_REFCOUNTABLE_FD]; |
93a8090d NIS |
2112 | |
2113 | void | |
2114 | PerlIO_init(pTHX) | |
2115 | { | |
2116 | /* Place holder for stdstreams call ??? */ | |
168d5872 | 2117 | #ifdef USE_THREADS |
93a8090d NIS |
2118 | MUTEX_INIT(&PerlIO_mutex); |
2119 | #endif | |
2120 | } | |
2121 | ||
168d5872 NIS |
2122 | void |
2123 | PerlIOUnix_refcnt_inc(int fd) | |
2124 | { | |
2125 | if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) { | |
2126 | #ifdef USE_THREADS | |
2127 | MUTEX_LOCK(&PerlIO_mutex); | |
2128 | #endif | |
2129 | PerlIO_fd_refcnt[fd]++; | |
2130 | PerlIO_debug("fd %d refcnt=%d\n",fd,PerlIO_fd_refcnt[fd]); | |
2131 | #ifdef USE_THREADS | |
2132 | MUTEX_UNLOCK(&PerlIO_mutex); | |
2133 | #endif | |
2134 | } | |
2135 | } | |
2136 | ||
168d5872 NIS |
2137 | int |
2138 | PerlIOUnix_refcnt_dec(int fd) | |
2139 | { | |
2140 | int cnt = 0; | |
2141 | if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) { | |
2142 | #ifdef USE_THREADS | |
2143 | MUTEX_LOCK(&PerlIO_mutex); | |
2144 | #endif | |
2145 | cnt = --PerlIO_fd_refcnt[fd]; | |
2146 | PerlIO_debug("fd %d refcnt=%d\n",fd,cnt); | |
2147 | #ifdef USE_THREADS | |
2148 | MUTEX_UNLOCK(&PerlIO_mutex); | |
2149 | #endif | |
2150 | } | |
2151 | return cnt; | |
2152 | } | |
2153 | ||
694c95cf JH |
2154 | void |
2155 | PerlIO_cleanup(pTHX) | |
2156 | { | |
2157 | int i; | |
2158 | #ifdef USE_ITHREADS | |
9f4bd222 NIS |
2159 | PerlIO_debug("Cleanup layers for %p\n",aTHX); |
2160 | #else | |
2161 | PerlIO_debug("Cleanup layers\n"); | |
694c95cf JH |
2162 | #endif |
2163 | /* Raise STDIN..STDERR refcount so we don't close them */ | |
2164 | for (i=0; i < 3; i++) | |
2165 | PerlIOUnix_refcnt_inc(i); | |
2166 | PerlIO_cleantable(aTHX_ &PL_perlio); | |
2167 | /* Restore STDIN..STDERR refcount */ | |
2168 | for (i=0; i < 3; i++) | |
2169 | PerlIOUnix_refcnt_dec(i); | |
9f4bd222 NIS |
2170 | |
2171 | if (PL_known_layers) { | |
2172 | PerlIO_list_free(aTHX_ PL_known_layers); | |
2173 | PL_known_layers = NULL; | |
2174 | } | |
2175 | if(PL_def_layerlist) { | |
2176 | PerlIO_list_free(aTHX_ PL_def_layerlist); | |
2177 | PL_def_layerlist = NULL; | |
2178 | } | |
694c95cf JH |
2179 | } |
2180 | ||
2181 | ||
2182 | ||
9e353e3b | 2183 | /*--------------------------------------------------------------------------------------*/ |
14a5cf38 | 2184 | /* |
71200d45 | 2185 | * Bottom-most level for UNIX-like case |
14a5cf38 | 2186 | */ |
9e353e3b | 2187 | |
14a5cf38 | 2188 | typedef struct { |
22569500 NIS |
2189 | struct _PerlIO base; /* The generic part */ |
2190 | int fd; /* UNIX like file descriptor */ | |
2191 | int oflags; /* open/fcntl flags */ | |
9e353e3b NIS |
2192 | } PerlIOUnix; |
2193 | ||
6f9d8c32 | 2194 | int |
9e353e3b | 2195 | PerlIOUnix_oflags(const char *mode) |
760ac839 | 2196 | { |
14a5cf38 JH |
2197 | int oflags = -1; |
2198 | if (*mode == 'I' || *mode == '#') | |
2199 | mode++; | |
2200 | switch (*mode) { | |
2201 | case 'r': | |
2202 | oflags = O_RDONLY; | |
2203 | if (*++mode == '+') { | |
2204 | oflags = O_RDWR; | |
2205 | mode++; | |
2206 | } | |
2207 | break; | |
2208 | ||
2209 | case 'w': | |
2210 | oflags = O_CREAT | O_TRUNC; | |
2211 | if (*++mode == '+') { | |
2212 | oflags |= O_RDWR; | |
2213 | mode++; | |
2214 | } | |
2215 | else | |
2216 | oflags |= O_WRONLY; | |
2217 | break; | |
2218 | ||
2219 | case 'a': | |
2220 | oflags = O_CREAT | O_APPEND; | |
2221 | if (*++mode == '+') { | |
2222 | oflags |= O_RDWR; | |
2223 | mode++; | |
2224 | } | |
2225 | else | |
2226 | oflags |= O_WRONLY; | |
2227 | break; | |
2228 | } | |
2229 | if (*mode == 'b') { | |
2230 | oflags |= O_BINARY; | |
2231 | oflags &= ~O_TEXT; | |
2232 | mode++; | |
2233 | } | |
2234 | else if (*mode == 't') { | |
2235 | oflags |= O_TEXT; | |
2236 | oflags &= ~O_BINARY; | |
2237 | mode++; | |
2238 | } | |
2239 | /* | |
71200d45 | 2240 | * Always open in binary mode |
14a5cf38 JH |
2241 | */ |
2242 | oflags |= O_BINARY; | |
2243 | if (*mode || oflags == -1) { | |
93189314 | 2244 | SETERRNO(EINVAL, LIB_INVARG); |
14a5cf38 JH |
2245 | oflags = -1; |
2246 | } | |
2247 | return oflags; | |
9e353e3b NIS |
2248 | } |
2249 | ||
2250 | IV | |
f62ce20a | 2251 | PerlIOUnix_fileno(pTHX_ PerlIO *f) |
9e353e3b | 2252 | { |
14a5cf38 | 2253 | return PerlIOSelf(f, PerlIOUnix)->fd; |
9e353e3b NIS |
2254 | } |
2255 | ||
4b803d04 | 2256 | IV |
2dc2558e | 2257 | PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
4b803d04 | 2258 | { |
2dc2558e | 2259 | IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab); |
93a8090d | 2260 | PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix); |
14a5cf38 | 2261 | if (*PerlIONext(f)) { |
4b069b44 | 2262 | /* We never call down so do any pending stuff now */ |
03c0554d | 2263 | PerlIO_flush(PerlIONext(f)); |
14a5cf38 JH |
2264 | s->fd = PerlIO_fileno(PerlIONext(f)); |
2265 | /* | |
71200d45 | 2266 | * XXX could (or should) we retrieve the oflags from the open file |
14a5cf38 | 2267 | * handle rather than believing the "mode" we are passed in? XXX |
71200d45 | 2268 | * Should the value on NULL mode be 0 or -1? |
14a5cf38 JH |
2269 | */ |
2270 | s->oflags = mode ? PerlIOUnix_oflags(mode) : -1; | |
2271 | } | |
2272 | PerlIOBase(f)->flags |= PERLIO_F_OPEN; | |
2273 | return code; | |
4b803d04 NIS |
2274 | } |
2275 | ||
9e353e3b | 2276 | PerlIO * |
14a5cf38 JH |
2277 | PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers, |
2278 | IV n, const char *mode, int fd, int imode, | |
2279 | int perm, PerlIO *f, int narg, SV **args) | |
2280 | { | |
d9dac8cd | 2281 | if (PerlIOValid(f)) { |
14a5cf38 | 2282 | if (PerlIOBase(f)->flags & PERLIO_F_OPEN) |
f62ce20a | 2283 | (*PerlIOBase(f)->tab->Close)(aTHX_ f); |
14a5cf38 JH |
2284 | } |
2285 | if (narg > 0) { | |
2286 | char *path = SvPV_nolen(*args); | |
2287 | if (*mode == '#') | |
2288 | mode++; | |
2289 | else { | |
2290 | imode = PerlIOUnix_oflags(mode); | |
2291 | perm = 0666; | |
2292 | } | |
2293 | if (imode != -1) { | |
2294 | fd = PerlLIO_open3(path, imode, perm); | |
2295 | } | |
2296 | } | |
2297 | if (fd >= 0) { | |
2298 | PerlIOUnix *s; | |
2299 | if (*mode == 'I') | |
2300 | mode++; | |
2301 | if (!f) { | |
2302 | f = PerlIO_allocate(aTHX); | |
d9dac8cd NIS |
2303 | } |
2304 | if (!PerlIOValid(f)) { | |
a33cf58c NIS |
2305 | if (!(f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) { |
2306 | return NULL; | |
2307 | } | |
d9dac8cd | 2308 | } |
a33cf58c | 2309 | s = PerlIOSelf(f, PerlIOUnix); |
14a5cf38 JH |
2310 | s->fd = fd; |
2311 | s->oflags = imode; | |
2312 | PerlIOBase(f)->flags |= PERLIO_F_OPEN; | |
22569500 | 2313 | PerlIOUnix_refcnt_inc(fd); |
14a5cf38 JH |
2314 | return f; |
2315 | } | |
2316 | else { | |
2317 | if (f) { | |
2318 | /* | |
71200d45 | 2319 | * FIXME: pop layers ??? |
14a5cf38 JH |
2320 | */ |
2321 | } | |
2322 | return NULL; | |
2323 | } | |
9e353e3b NIS |
2324 | } |
2325 | ||
71200d45 | 2326 | PerlIO * |
ecdeb87c | 2327 | PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags) |
71200d45 NIS |
2328 | { |
2329 | PerlIOUnix *os = PerlIOSelf(o, PerlIOUnix); | |
93a8090d | 2330 | int fd = os->fd; |
ecdeb87c NIS |
2331 | if (flags & PERLIO_DUP_FD) { |
2332 | fd = PerlLIO_dup(fd); | |
2333 | } | |
93a8090d | 2334 | if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) { |
ecdeb87c | 2335 | f = PerlIOBase_dup(aTHX_ f, o, param, flags); |
71200d45 NIS |
2336 | if (f) { |
2337 | /* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */ | |
2338 | PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix); | |
2339 | s->fd = fd; | |
93a8090d | 2340 | PerlIOUnix_refcnt_inc(fd); |
71200d45 NIS |
2341 | return f; |
2342 | } | |
71200d45 NIS |
2343 | } |
2344 | return NULL; | |
2345 | } | |
2346 | ||
2347 | ||
9e353e3b | 2348 | SSize_t |
f62ce20a | 2349 | PerlIOUnix_read(pTHX_ PerlIO *f, void *vbuf, Size_t count) |
9e353e3b | 2350 | { |
14a5cf38 | 2351 | int fd = PerlIOSelf(f, PerlIOUnix)->fd; |
81428673 | 2352 | if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD) || |
1fd8f4ce | 2353 | PerlIOBase(f)->flags & (PERLIO_F_EOF|PERLIO_F_ERROR)) { |
14a5cf38 | 2354 | return 0; |
1fd8f4ce | 2355 | } |
14a5cf38 JH |
2356 | while (1) { |
2357 | SSize_t len = PerlLIO_read(fd, vbuf, count); | |
2358 | if (len >= 0 || errno != EINTR) { | |
2359 | if (len < 0) | |
2360 | PerlIOBase(f)->flags |= PERLIO_F_ERROR; | |
2361 | else if (len == 0 && count != 0) | |
2362 | PerlIOBase(f)->flags |= PERLIO_F_EOF; | |
2363 | return len; | |
2364 | } | |
2365 | PERL_ASYNC_CHECK(); | |
2366 | } | |
9e353e3b NIS |
2367 | } |
2368 | ||
2369 | SSize_t | |
f62ce20a | 2370 | PerlIOUnix_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count) |
9e353e3b | 2371 | { |
14a5cf38 JH |
2372 | int fd = PerlIOSelf(f, PerlIOUnix)->fd; |
2373 | while (1) { | |
2374 | SSize_t len = PerlLIO_write(fd, vbuf, count); | |
2375 | if (len >= 0 || errno != EINTR) { | |
2376 | if (len < 0) | |
2377 | PerlIOBase(f)->flags |= PERLIO_F_ERROR; | |
2378 | return len; | |
2379 | } | |
2380 | PERL_ASYNC_CHECK(); | |
06da4f11 | 2381 | } |
9e353e3b NIS |
2382 | } |
2383 | ||
2384 | IV | |
f62ce20a | 2385 | PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence) |
9e353e3b | 2386 | { |
14a5cf38 JH |
2387 | Off_t new = |
2388 | PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, offset, whence); | |
2389 | PerlIOBase(f)->flags &= ~PERLIO_F_EOF; | |
2390 | return (new == (Off_t) - 1) ? -1 : 0; | |
9e353e3b NIS |
2391 | } |
2392 | ||
2393 | Off_t | |
f62ce20a | 2394 | PerlIOUnix_tell(pTHX_ PerlIO *f) |
9e353e3b | 2395 | { |
14a5cf38 | 2396 | return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR); |
9e353e3b NIS |
2397 | } |
2398 | ||
71200d45 | 2399 | |
9e353e3b | 2400 | IV |
f62ce20a | 2401 | PerlIOUnix_close(pTHX_ PerlIO *f) |
9e353e3b | 2402 | { |
14a5cf38 JH |
2403 | int fd = PerlIOSelf(f, PerlIOUnix)->fd; |
2404 | int code = 0; | |
168d5872 NIS |
2405 | if (PerlIOBase(f)->flags & PERLIO_F_OPEN) { |
2406 | if (PerlIOUnix_refcnt_dec(fd) > 0) { | |
93a8090d NIS |
2407 | PerlIOBase(f)->flags &= ~PERLIO_F_OPEN; |
2408 | return 0; | |
22569500 | 2409 | } |
93a8090d NIS |
2410 | } |
2411 | else { | |
93189314 | 2412 | SETERRNO(EBADF,SS_IVCHAN); |
93a8090d NIS |
2413 | return -1; |
2414 | } | |
14a5cf38 JH |
2415 | while (PerlLIO_close(fd) != 0) { |
2416 | if (errno != EINTR) { | |
2417 | code = -1; | |
2418 | break; | |
2419 | } | |
2420 | PERL_ASYNC_CHECK(); | |
2421 | } | |
2422 | if (code == 0) { | |
2423 | PerlIOBase(f)->flags &= ~PERLIO_F_OPEN; | |
2424 | } | |
2425 | return code; | |
9e353e3b NIS |
2426 | } |
2427 | ||
2428 | PerlIO_funcs PerlIO_unix = { | |
2dc2558e | 2429 | sizeof(PerlIO_funcs), |
14a5cf38 JH |
2430 | "unix", |
2431 | sizeof(PerlIOUnix), | |
2432 | PERLIO_K_RAW, | |
2433 | PerlIOUnix_pushed, | |
44798d05 | 2434 | PerlIOBase_popped, |
14a5cf38 | 2435 | PerlIOUnix_open, |
86e05cf2 | 2436 | PerlIOBase_binmode, /* binmode */ |
14a5cf38 JH |
2437 | NULL, |
2438 | PerlIOUnix_fileno, | |
71200d45 | 2439 | PerlIOUnix_dup, |
14a5cf38 JH |
2440 | PerlIOUnix_read, |
2441 | PerlIOBase_unread, | |
2442 | PerlIOUnix_write, | |
2443 | PerlIOUnix_seek, | |
2444 | PerlIOUnix_tell, | |
2445 | PerlIOUnix_close, | |
22569500 NIS |
2446 | PerlIOBase_noop_ok, /* flush */ |
2447 | PerlIOBase_noop_fail, /* fill */ | |
14a5cf38 JH |
2448 | PerlIOBase_eof, |
2449 | PerlIOBase_error, | |
2450 | PerlIOBase_clearerr, | |
2451 | PerlIOBase_setlinebuf, | |
22569500 NIS |
2452 | NULL, /* get_base */ |
2453 | NULL, /* get_bufsiz */ | |
2454 | NULL, /* get_ptr */ | |
2455 | NULL, /* get_cnt */ | |
2456 | NULL, /* set_ptrcnt */ | |
9e353e3b NIS |
2457 | }; |
2458 | ||
2459 | /*--------------------------------------------------------------------------------------*/ | |
14a5cf38 | 2460 | /* |
71200d45 | 2461 | * stdio as a layer |
14a5cf38 | 2462 | */ |
9e353e3b | 2463 | |
313e59c8 NIS |
2464 | #if defined(VMS) && !defined(STDIO_BUFFER_WRITABLE) |
2465 | /* perl5.8 - This ensures the last minute VMS ungetc fix is not | |
2466 | broken by the last second glibc 2.3 fix | |
2467 | */ | |
2468 | #define STDIO_BUFFER_WRITABLE | |
2469 | #endif | |
2470 | ||
2471 | ||
14a5cf38 JH |
2472 | typedef struct { |
2473 | struct _PerlIO base; | |
22569500 | 2474 | FILE *stdio; /* The stream */ |
9e353e3b NIS |
2475 | } PerlIOStdio; |
2476 | ||
2477 | IV | |
f62ce20a | 2478 | PerlIOStdio_fileno(pTHX_ PerlIO *f) |
9e353e3b | 2479 | { |
439ba545 | 2480 | FILE *s; |
81428673 | 2481 | if (PerlIOValid(f) && (s = PerlIOSelf(f, PerlIOStdio)->stdio)) { |
439ba545 NIS |
2482 | return PerlSIO_fileno(s); |
2483 | } | |
2484 | errno = EBADF; | |
2485 | return -1; | |
9e353e3b NIS |
2486 | } |
2487 | ||
766a733e | 2488 | char * |
14a5cf38 JH |
2489 | PerlIOStdio_mode(const char *mode, char *tmode) |
2490 | { | |
2491 | char *ret = tmode; | |
2492 | while (*mode) { | |
2493 | *tmode++ = *mode++; | |
2494 | } | |
35990314 | 2495 | #ifdef PERLIO_USING_CRLF |
6ce75a77 JH |
2496 | *tmode++ = 'b'; |
2497 | #endif | |
14a5cf38 JH |
2498 | *tmode = '\0'; |
2499 | return ret; | |
2500 | } | |
2501 | ||
4b803d04 | 2502 | IV |
2dc2558e | 2503 | PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
4b803d04 | 2504 | { |
1fd8f4ce NIS |
2505 | PerlIO *n; |
2506 | if (PerlIOValid(f) && PerlIOValid(n = PerlIONext(f))) { | |
2507 | PerlIO_funcs *toptab = PerlIOBase(n)->tab; | |
2508 | if (toptab == tab) { | |
2509 | /* Top is already stdio - pop self (duplicate) and use original */ | |
2510 | PerlIO_pop(aTHX_ f); | |
2511 | return 0; | |
2512 | } else { | |
2513 | int fd = PerlIO_fileno(n); | |
2514 | char tmode[8]; | |
2515 | FILE *stdio; | |
81428673 | 2516 | if (fd >= 0 && (stdio = PerlSIO_fdopen(fd, |
1fd8f4ce NIS |
2517 | mode = PerlIOStdio_mode(mode, tmode)))) { |
2518 | PerlIOSelf(f, PerlIOStdio)->stdio = stdio; | |
2519 | /* We never call down so do any pending stuff now */ | |
2520 | PerlIO_flush(PerlIONext(f)); | |
81428673 | 2521 | } |
1fd8f4ce NIS |
2522 | else { |
2523 | return -1; | |
2524 | } | |
2525 | } | |
14a5cf38 | 2526 | } |
2dc2558e | 2527 | return PerlIOBase_pushed(aTHX_ f, mode, arg, tab); |
4b803d04 NIS |
2528 | } |
2529 | ||
22569500 | 2530 | |
9e353e3b | 2531 | PerlIO * |
4b069b44 | 2532 | PerlIO_importFILE(FILE *stdio, const char *mode) |
9e353e3b | 2533 | { |
14a5cf38 JH |
2534 | dTHX; |
2535 | PerlIO *f = NULL; | |
2536 | if (stdio) { | |
22569500 | 2537 | PerlIOStdio *s; |
4b069b44 NIS |
2538 | if (!mode || !*mode) { |
2539 | /* We need to probe to see how we can open the stream | |
2540 | so start with read/write and then try write and read | |
2541 | we dup() so that we can fclose without loosing the fd. | |
2542 | ||
2543 | Note that the errno value set by a failing fdopen | |
2544 | varies between stdio implementations. | |
2545 | */ | |
2546 | int fd = PerlLIO_dup(fileno(stdio)); | |
a33cf58c | 2547 | FILE *f2 = PerlSIO_fdopen(fd, (mode = "r+")); |
4b069b44 | 2548 | if (!f2) { |
a33cf58c | 2549 | f2 = PerlSIO_fdopen(fd, (mode = "w")); |
4b069b44 NIS |
2550 | } |
2551 | if (!f2) { | |
a33cf58c | 2552 | f2 = PerlSIO_fdopen(fd, (mode = "r")); |
4b069b44 NIS |
2553 | } |
2554 | if (!f2) { | |
2555 | /* Don't seem to be able to open */ | |
2556 | PerlLIO_close(fd); | |
2557 | return f; | |
2558 | } | |
2559 | fclose(f2); | |
22569500 | 2560 | } |
a33cf58c NIS |
2561 | if ((f = PerlIO_push(aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio, mode, Nullsv))) { |
2562 | s = PerlIOSelf(f, PerlIOStdio); | |
2563 | s->stdio = stdio; | |
2564 | } | |
14a5cf38 JH |
2565 | } |
2566 | return f; | |
9e353e3b NIS |
2567 | } |
2568 | ||
2569 | PerlIO * | |
14a5cf38 JH |
2570 | PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers, |
2571 | IV n, const char *mode, int fd, int imode, | |
2572 | int perm, PerlIO *f, int narg, SV **args) | |
2573 | { | |
2574 | char tmode[8]; | |
d9dac8cd | 2575 | if (PerlIOValid(f)) { |
14a5cf38 JH |
2576 | char *path = SvPV_nolen(*args); |
2577 | PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio); | |
1751d015 NIS |
2578 | FILE *stdio; |
2579 | PerlIOUnix_refcnt_dec(fileno(s->stdio)); | |
2580 | stdio = PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)), | |
14a5cf38 JH |
2581 | s->stdio); |
2582 | if (!s->stdio) | |
2583 | return NULL; | |
2584 | s->stdio = stdio; | |
1751d015 | 2585 | PerlIOUnix_refcnt_inc(fileno(s->stdio)); |
14a5cf38 JH |
2586 | return f; |
2587 | } | |
2588 | else { | |
2589 | if (narg > 0) { | |
2590 | char *path = SvPV_nolen(*args); | |
2591 | if (*mode == '#') { | |
2592 | mode++; | |
2593 | fd = PerlLIO_open3(path, imode, perm); | |
2594 | } | |
2595 | else { | |
2596 | FILE *stdio = PerlSIO_fopen(path, mode); | |
2597 | if (stdio) { | |
d9dac8cd NIS |
2598 | PerlIOStdio *s; |
2599 | if (!f) { | |
2600 | f = PerlIO_allocate(aTHX); | |
2601 | } | |
a33cf58c | 2602 | if ((f = PerlIO_push(aTHX_ f, self, |
14a5cf38 | 2603 | (mode = PerlIOStdio_mode(mode, tmode)), |
a33cf58c NIS |
2604 | PerlIOArg))) { |
2605 | s = PerlIOSelf(f, PerlIOStdio); | |
2606 | s->stdio = stdio; | |
2607 | PerlIOUnix_refcnt_inc(fileno(s->stdio)); | |
2608 | } | |
602c3c4b NIS |
2609 | return f; |
2610 | } | |
2611 | else { | |
2612 | return NULL; | |
14a5cf38 | 2613 | } |
14a5cf38 JH |
2614 | } |
2615 | } | |
2616 | if (fd >= 0) { | |
2617 | FILE *stdio = NULL; | |
2618 | int init = 0; | |
2619 | if (*mode == 'I') { | |
2620 | init = 1; | |
2621 | mode++; | |
2622 | } | |
2623 | if (init) { | |
2624 | switch (fd) { | |
2625 | case 0: | |
2626 | stdio = PerlSIO_stdin; | |
2627 | break; | |
2628 | case 1: | |
2629 | stdio = PerlSIO_stdout; | |
2630 | break; | |
2631 | case 2: | |
2632 | stdio = PerlSIO_stderr; | |
2633 | break; | |
2634 | } | |
2635 | } | |
2636 | else { | |
2637 | stdio = PerlSIO_fdopen(fd, mode = | |
2638 | PerlIOStdio_mode(mode, tmode)); | |
2639 | } | |
2640 | if (stdio) { | |
d9dac8cd NIS |
2641 | PerlIOStdio *s; |
2642 | if (!f) { | |
2643 | f = PerlIO_allocate(aTHX); | |
2644 | } | |
a33cf58c NIS |
2645 | if ((f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) { |
2646 | s = PerlIOSelf(f, PerlIOStdio); | |
2647 | s->stdio = stdio; | |
2648 | PerlIOUnix_refcnt_inc(fileno(s->stdio)); | |
2649 | } | |
14a5cf38 JH |
2650 | return f; |
2651 | } | |
2652 | } | |
2653 | } | |
ee518936 | 2654 | return NULL; |
9e353e3b NIS |
2655 | } |
2656 | ||
1751d015 | 2657 | PerlIO * |
ecdeb87c | 2658 | PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags) |
1751d015 NIS |
2659 | { |
2660 | /* This assumes no layers underneath - which is what | |
2661 | happens, but is not how I remember it. NI-S 2001/10/16 | |
2662 | */ | |
ecdeb87c | 2663 | if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) { |
694c95cf | 2664 | FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio; |
9217ff3f NIS |
2665 | int fd = fileno(stdio); |
2666 | char mode[8]; | |
ecdeb87c | 2667 | if (flags & PERLIO_DUP_FD) { |
9217ff3f NIS |
2668 | int dfd = PerlLIO_dup(fileno(stdio)); |
2669 | if (dfd >= 0) { | |
2670 | stdio = PerlSIO_fdopen(dfd, PerlIO_modestr(o,mode)); | |
2671 | goto set_this; | |
ecdeb87c NIS |
2672 | } |
2673 | else { | |
2674 | /* FIXME: To avoid messy error recovery if dup fails | |
2675 | re-use the existing stdio as though flag was not set | |
2676 | */ | |
2677 | } | |
2678 | } | |
9217ff3f NIS |
2679 | stdio = PerlSIO_fdopen(fd, PerlIO_modestr(o,mode)); |
2680 | set_this: | |
694c95cf JH |
2681 | PerlIOSelf(f, PerlIOStdio)->stdio = stdio; |
2682 | PerlIOUnix_refcnt_inc(fileno(stdio)); | |
1751d015 NIS |
2683 | } |
2684 | return f; | |
2685 | } | |
2686 | ||
2687 | IV | |
f62ce20a | 2688 | PerlIOStdio_close(pTHX_ PerlIO *f) |
1751d015 | 2689 | { |
1751d015 NIS |
2690 | #ifdef SOCKS5_VERSION_NAME |
2691 | int optval; | |
2692 | Sock_size_t optlen = sizeof(int); | |
2693 | #endif | |
2694 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; | |
439ba545 NIS |
2695 | if (!stdio) { |
2696 | errno = EBADF; | |
2697 | return -1; | |
2698 | } | |
9217ff3f NIS |
2699 | else { |
2700 | int fd = fileno(stdio); | |
2701 | int dupfd = -1; | |
2702 | IV result; | |
2703 | if (PerlIOUnix_refcnt_dec(fd) > 0) { | |
2704 | /* File descriptor still in use */ | |
2705 | if (fd < 3) { | |
2706 | /* For STD* handles don't close the stdio at all */ | |
2707 | return PerlIO_flush(f); | |
2708 | } | |
2709 | else { | |
2710 | /* Tricky - must fclose(stdio) to free memory but not close(fd) */ | |
997ec9b4 NIS |
2711 | #ifdef USE_THREADS |
2712 | /* Sarathy pointed out that another thread could reuse | |
2713 | fd after fclose() but before we dup2() below | |
2714 | so take out a MUTEX to shut them out | |
2715 | */ | |
2716 | MUTEX_LOCK(&PerlIO_mutex); | |
2717 | #endif | |
9217ff3f NIS |
2718 | dupfd = PerlLIO_dup(fd); |
2719 | } | |
2720 | } | |
2721 | result = ( | |
1751d015 NIS |
2722 | #ifdef SOCKS5_VERSION_NAME |
2723 | (getsockopt | |
2724 | (PerlIO_fileno(f), SOL_SOCKET, SO_TYPE, (void *) &optval, | |
2725 | &optlen) < | |
2726 | 0) ? PerlSIO_fclose(stdio) : close(PerlIO_fileno(f)) | |
2727 | #else | |
2728 | PerlSIO_fclose(stdio) | |
2729 | #endif | |
2730 | ); | |
9217ff3f NIS |
2731 | if (dupfd >= 0) { |
2732 | /* We need to restore fd from the saved copy */ | |
2733 | if (PerlLIO_dup2(dupfd,fd) != fd) | |
2734 | result = -1; | |
997ec9b4 NIS |
2735 | #ifdef USE_THREADS |
2736 | MUTEX_UNLOCK(&PerlIO_mutex); | |
2737 | #endif | |
9217ff3f NIS |
2738 | if (PerlLIO_close(dupfd) != 0) |
2739 | result = -1; | |
2740 | } | |
2741 | return result; | |
2742 | } | |
1751d015 NIS |
2743 | } |
2744 | ||
2745 | ||
2746 | ||
9e353e3b | 2747 | SSize_t |
f62ce20a | 2748 | PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count) |
9e353e3b | 2749 | { |
14a5cf38 JH |
2750 | FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio; |
2751 | SSize_t got = 0; | |
2752 | if (count == 1) { | |
2753 | STDCHAR *buf = (STDCHAR *) vbuf; | |
2754 | /* | |
2755 | * Perl is expecting PerlIO_getc() to fill the buffer Linux's | |
71200d45 | 2756 | * stdio does not do that for fread() |
14a5cf38 JH |
2757 | */ |
2758 | int ch = PerlSIO_fgetc(s); | |
2759 | if (ch != EOF) { | |
2760 | *buf = ch; | |
2761 | got = 1; | |
2762 | } | |
2763 | } | |
2764 | else | |
2765 | got = PerlSIO_fread(vbuf, 1, count, s); | |
2766 | return got; | |
9e353e3b NIS |
2767 | } |
2768 | ||
2769 | SSize_t | |
f62ce20a | 2770 | PerlIOStdio_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count) |
9e353e3b | 2771 | { |
14a5cf38 | 2772 | SSize_t unread = 0; |
93679785 NIS |
2773 | FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio; |
2774 | ||
313e59c8 | 2775 | #ifdef STDIO_BUFFER_WRITABLE |
9f7cd136 | 2776 | if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) { |
93679785 NIS |
2777 | STDCHAR *buf = ((STDCHAR *) vbuf) + count; |
2778 | STDCHAR *base = PerlIO_get_base(f); | |
2779 | SSize_t cnt = PerlIO_get_cnt(f); | |
2780 | STDCHAR *ptr = PerlIO_get_ptr(f); | |
2781 | SSize_t avail = ptr - base; | |
2782 | if (avail > 0) { | |
2783 | if (avail > count) { | |
2784 | avail = count; | |
2785 | } | |
2786 | ptr -= avail; | |
2787 | Move(buf-avail,ptr,avail,STDCHAR); | |
2788 | count -= avail; | |
2789 | unread += avail; | |
2790 | PerlIO_set_ptrcnt(f,ptr,cnt+avail); | |
9f7cd136 NIS |
2791 | if (PerlSIO_feof(s) && unread >= 0) |
2792 | PerlSIO_clearerr(s); | |
2793 | } | |
2794 | } | |
313e59c8 NIS |
2795 | else |
2796 | #endif | |
2797 | if (PerlIO_has_cntptr(f)) { | |
9f7cd136 NIS |
2798 | /* We can get pointer to buffer but not its base |
2799 | Do ungetc() but check chars are ending up in the | |
2800 | buffer | |
2801 | */ | |
2802 | STDCHAR *eptr = (STDCHAR*)PerlSIO_get_ptr(s); | |
2803 | STDCHAR *buf = ((STDCHAR *) vbuf) + count; | |
2804 | while (count > 0) { | |
2805 | int ch = *--buf & 0xFF; | |
2806 | if (ungetc(ch,s) != ch) { | |
2807 | /* ungetc did not work */ | |
2808 | break; | |
2809 | } | |
2810 | if ((STDCHAR*)PerlSIO_get_ptr(s) != --eptr || ((*eptr & 0xFF) != ch)) { | |
2811 | /* Did not change pointer as expected */ | |
2812 | fgetc(s); /* get char back again */ | |
2813 | break; | |
2814 | } | |
2815 | /* It worked ! */ | |
2816 | count--; | |
2817 | unread++; | |
93679785 NIS |
2818 | } |
2819 | } | |
2820 | ||
2821 | if (count > 0) { | |
2822 | unread += PerlIOBase_unread(aTHX_ f, vbuf, count); | |
14a5cf38 JH |
2823 | } |
2824 | return unread; | |
9e353e3b NIS |
2825 | } |
2826 | ||
2827 | SSize_t | |
f62ce20a | 2828 | PerlIOStdio_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count) |
9e353e3b | 2829 | { |
14a5cf38 JH |
2830 | return PerlSIO_fwrite(vbuf, 1, count, |
2831 | PerlIOSelf(f, PerlIOStdio)->stdio); | |
9e353e3b NIS |
2832 | } |
2833 | ||
2834 | IV | |
f62ce20a | 2835 | PerlIOStdio_seek(pTHX_ PerlIO *f, Off_t offset, int whence) |
9e353e3b | 2836 | { |
14a5cf38 JH |
2837 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; |
2838 | return PerlSIO_fseek(stdio, offset, whence); | |
9e353e3b NIS |
2839 | } |
2840 | ||
2841 | Off_t | |
f62ce20a | 2842 | PerlIOStdio_tell(pTHX_ PerlIO *f) |
9e353e3b | 2843 | { |
14a5cf38 JH |
2844 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; |
2845 | return PerlSIO_ftell(stdio); | |
9e353e3b NIS |
2846 | } |
2847 | ||
2848 | IV | |
f62ce20a | 2849 | PerlIOStdio_flush(pTHX_ PerlIO *f) |
9e353e3b | 2850 | { |
14a5cf38 JH |
2851 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; |
2852 | if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) { | |
2853 | return PerlSIO_fflush(stdio); | |
2854 | } | |
2855 | else { | |
88b61e10 | 2856 | #if 0 |
14a5cf38 JH |
2857 | /* |
2858 | * FIXME: This discards ungetc() and pre-read stuff which is not | |
71200d45 | 2859 | * right if this is just a "sync" from a layer above Suspect right |
14a5cf38 | 2860 | * design is to do _this_ but not have layer above flush this |
71200d45 | 2861 | * layer read-to-read |
14a5cf38 JH |
2862 | */ |
2863 | /* | |
71200d45 | 2864 | * Not writeable - sync by attempting a seek |
14a5cf38 JH |
2865 | */ |
2866 | int err = errno; | |
2867 | if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0) | |
2868 | errno = err; | |
88b61e10 | 2869 | #endif |
14a5cf38 JH |
2870 | } |
2871 | return 0; | |
9e353e3b NIS |
2872 | } |
2873 | ||
2874 | IV | |
f62ce20a | 2875 | PerlIOStdio_eof(pTHX_ PerlIO *f) |
9e353e3b | 2876 | { |
14a5cf38 | 2877 | return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio); |
9e353e3b NIS |
2878 | } |
2879 | ||
2880 | IV | |
f62ce20a | 2881 | PerlIOStdio_error(pTHX_ PerlIO *f) |
9e353e3b | 2882 | { |
14a5cf38 | 2883 | return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio); |
9e353e3b NIS |
2884 | } |
2885 | ||
2886 | void | |
f62ce20a | 2887 | PerlIOStdio_clearerr(pTHX_ PerlIO *f) |
9e353e3b | 2888 | { |
14a5cf38 | 2889 | PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio); |
9e353e3b NIS |
2890 | } |
2891 | ||
2892 | void | |
f62ce20a | 2893 | PerlIOStdio_setlinebuf(pTHX_ PerlIO *f) |
9e353e3b NIS |
2894 | { |
2895 | #ifdef HAS_SETLINEBUF | |
14a5cf38 | 2896 | PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio); |
9e353e3b | 2897 | #else |
14a5cf38 | 2898 | PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, Nullch, _IOLBF, 0); |
9e353e3b NIS |
2899 | #endif |
2900 | } | |
2901 | ||
2902 | #ifdef FILE_base | |
2903 | STDCHAR * | |
f62ce20a | 2904 | PerlIOStdio_get_base(pTHX_ PerlIO *f) |
9e353e3b | 2905 | { |
14a5cf38 | 2906 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; |
cc00df79 | 2907 | return (STDCHAR*)PerlSIO_get_base(stdio); |
9e353e3b NIS |
2908 | } |
2909 | ||
2910 | Size_t | |
f62ce20a | 2911 | PerlIOStdio_get_bufsiz(pTHX_ PerlIO *f) |
9e353e3b | 2912 | { |
14a5cf38 JH |
2913 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; |
2914 | return PerlSIO_get_bufsiz(stdio); | |
9e353e3b NIS |
2915 | } |
2916 | #endif | |
2917 | ||
2918 | #ifdef USE_STDIO_PTR | |
2919 | STDCHAR * | |
f62ce20a | 2920 | PerlIOStdio_get_ptr(pTHX_ PerlIO *f) |
9e353e3b | 2921 | { |
14a5cf38 | 2922 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; |
cc00df79 | 2923 | return (STDCHAR*)PerlSIO_get_ptr(stdio); |
9e353e3b NIS |
2924 | } |
2925 | ||
2926 | SSize_t | |
f62ce20a | 2927 | PerlIOStdio_get_cnt(pTHX_ PerlIO *f) |
9e353e3b | 2928 | { |
14a5cf38 JH |
2929 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; |
2930 | return PerlSIO_get_cnt(stdio); | |
9e353e3b NIS |
2931 | } |
2932 | ||
2933 | void | |
f62ce20a | 2934 | PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt) |
9e353e3b | 2935 | { |
14a5cf38 | 2936 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; |
14a5cf38 | 2937 | if (ptr != NULL) { |
9e353e3b | 2938 | #ifdef STDIO_PTR_LVALUE |
22569500 | 2939 | PerlSIO_set_ptr(stdio, (void*)ptr); /* LHS STDCHAR* cast non-portable */ |
9e353e3b | 2940 | #ifdef STDIO_PTR_LVAL_SETS_CNT |
14a5cf38 | 2941 | if (PerlSIO_get_cnt(stdio) != (cnt)) { |
14a5cf38 JH |
2942 | assert(PerlSIO_get_cnt(stdio) == (cnt)); |
2943 | } | |
9e353e3b NIS |
2944 | #endif |
2945 | #if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT)) | |
14a5cf38 | 2946 | /* |
71200d45 | 2947 | * Setting ptr _does_ change cnt - we are done |
14a5cf38 JH |
2948 | */ |
2949 | return; | |
9e353e3b | 2950 | #endif |
22569500 | 2951 | #else /* STDIO_PTR_LVALUE */ |
14a5cf38 | 2952 | PerlProc_abort(); |
22569500 | 2953 | #endif /* STDIO_PTR_LVALUE */ |
14a5cf38 JH |
2954 | } |
2955 | /* | |
71200d45 | 2956 | * Now (or only) set cnt |
14a5cf38 | 2957 | */ |
9e353e3b | 2958 | #ifdef STDIO_CNT_LVALUE |
14a5cf38 | 2959 | PerlSIO_set_cnt(stdio, cnt); |
22569500 | 2960 | #else /* STDIO_CNT_LVALUE */ |
9e353e3b | 2961 | #if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT)) |
14a5cf38 JH |
2962 | PerlSIO_set_ptr(stdio, |
2963 | PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) - | |
2964 | cnt)); | |
22569500 | 2965 | #else /* STDIO_PTR_LVAL_SETS_CNT */ |
14a5cf38 | 2966 | PerlProc_abort(); |
22569500 NIS |
2967 | #endif /* STDIO_PTR_LVAL_SETS_CNT */ |
2968 | #endif /* STDIO_CNT_LVALUE */ | |
9e353e3b NIS |
2969 | } |
2970 | ||
93679785 | 2971 | |
9e353e3b NIS |
2972 | #endif |
2973 | ||
93679785 NIS |
2974 | IV |
2975 | PerlIOStdio_fill(pTHX_ PerlIO *f) | |
2976 | { | |
2977 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; | |
2978 | int c; | |
2979 | /* | |
2980 | * fflush()ing read-only streams can cause trouble on some stdio-s | |
2981 | */ | |
2982 | if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) { | |
2983 | if (PerlSIO_fflush(stdio) != 0) | |
2984 | return EOF; | |
2985 | } | |
2986 | c = PerlSIO_fgetc(stdio); | |
2987 | if (c == EOF) | |
2988 | return EOF; | |
2989 | ||
2990 | #if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT))) | |
313e59c8 NIS |
2991 | |
2992 | #ifdef STDIO_BUFFER_WRITABLE | |
9f7cd136 | 2993 | if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) { |
93679785 NIS |
2994 | /* Fake ungetc() to the real buffer in case system's ungetc |
2995 | goes elsewhere | |
2996 | */ | |
2997 | STDCHAR *base = (STDCHAR*)PerlSIO_get_base(stdio); | |
2998 | SSize_t cnt = PerlSIO_get_cnt(stdio); | |
2999 | STDCHAR *ptr = (STDCHAR*)PerlSIO_get_ptr(stdio); | |
3000 | if (ptr == base+1) { | |
3001 | *--ptr = (STDCHAR) c; | |
3002 | PerlIOStdio_set_ptrcnt(aTHX_ f,ptr,cnt+1); | |
3003 | if (PerlSIO_feof(stdio)) | |
3004 | PerlSIO_clearerr(stdio); | |
3005 | return 0; | |
3006 | } | |
3007 | } | |
313e59c8 NIS |
3008 | else |
3009 | #endif | |
3010 | if (PerlIO_has_cntptr(f)) { | |
9f7cd136 NIS |
3011 | STDCHAR ch = c; |
3012 | if (PerlIOStdio_unread(aTHX_ f,&ch,1) == 1) { | |
3013 | return 0; | |
3014 | } | |
3015 | } | |
93679785 NIS |
3016 | #endif |
3017 | ||
3018 | #if defined(VMS) | |
3019 | /* An ungetc()d char is handled separately from the regular | |
3020 | * buffer, so we stuff it in the buffer ourselves. | |
3021 | * Should never get called as should hit code above | |
3022 | */ | |
bad9695d NIS |
3023 | *(--((*stdio)->_ptr)) = (unsigned char) c; |
3024 | (*stdio)->_cnt++; | |
93679785 NIS |
3025 | #else |
3026 | /* If buffer snoop scheme above fails fall back to | |
9f7cd136 | 3027 | using ungetc(). |
93679785 NIS |
3028 | */ |
3029 | if (PerlSIO_ungetc(c, stdio) != c) | |
3030 | return EOF; | |
3031 | #endif | |
3032 | return 0; | |
3033 | } | |
3034 | ||
3035 | ||
3036 | ||
9e353e3b | 3037 | PerlIO_funcs PerlIO_stdio = { |
2dc2558e | 3038 | sizeof(PerlIO_funcs), |
14a5cf38 JH |
3039 | "stdio", |
3040 | sizeof(PerlIOStdio), | |
86e05cf2 | 3041 | PERLIO_K_BUFFERED|PERLIO_K_RAW, |
1fd8f4ce | 3042 | PerlIOStdio_pushed, |
44798d05 | 3043 | PerlIOBase_popped, |
14a5cf38 | 3044 | PerlIOStdio_open, |
86e05cf2 | 3045 | PerlIOBase_binmode, /* binmode */ |
14a5cf38 JH |
3046 | NULL, |
3047 | PerlIOStdio_fileno, | |
71200d45 | 3048 | PerlIOStdio_dup, |
14a5cf38 JH |
3049 | PerlIOStdio_read, |
3050 | PerlIOStdio_unread, | |
3051 | PerlIOStdio_write, | |
3052 | PerlIOStdio_seek, | |
3053 | PerlIOStdio_tell, | |
3054 | PerlIOStdio_close, | |
3055 | PerlIOStdio_flush, | |
3056 | PerlIOStdio_fill, | |
3057 | PerlIOStdio_eof, | |
3058 | PerlIOStdio_error, | |
3059 | PerlIOStdio_clearerr, | |
3060 | PerlIOStdio_setlinebuf, | |
9e353e3b | 3061 | #ifdef FILE_base |
14a5cf38 JH |
3062 | PerlIOStdio_get_base, |
3063 | PerlIOStdio_get_bufsiz, | |
9e353e3b | 3064 | #else |
14a5cf38 JH |
3065 | NULL, |
3066 | NULL, | |
9e353e3b NIS |
3067 | #endif |
3068 | #ifdef USE_STDIO_PTR | |
14a5cf38 JH |
3069 | PerlIOStdio_get_ptr, |
3070 | PerlIOStdio_get_cnt, | |
0eb1d8a4 | 3071 | #if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT))) |
14a5cf38 | 3072 | PerlIOStdio_set_ptrcnt |
22569500 | 3073 | #else /* STDIO_PTR_LVALUE */ |
14a5cf38 | 3074 | NULL |
22569500 NIS |
3075 | #endif /* STDIO_PTR_LVALUE */ |
3076 | #else /* USE_STDIO_PTR */ | |
14a5cf38 JH |
3077 | NULL, |
3078 | NULL, | |
3079 | NULL | |
22569500 | 3080 | #endif /* USE_STDIO_PTR */ |
9e353e3b NIS |
3081 | }; |
3082 | ||
b9d6bf13 JH |
3083 | /* Note that calls to PerlIO_exportFILE() are reversed using |
3084 | * PerlIO_releaseFILE(), not importFILE. */ | |
9e353e3b | 3085 | FILE * |
81428673 | 3086 | PerlIO_exportFILE(PerlIO * f, const char *mode) |
9e353e3b | 3087 | { |
e87a358a | 3088 | dTHX; |
81428673 NIS |
3089 | FILE *stdio = NULL; |
3090 | if (PerlIOValid(f)) { | |
3091 | char buf[8]; | |
3092 | PerlIO_flush(f); | |
3093 | if (!mode || !*mode) { | |
3094 | mode = PerlIO_modestr(f, buf); | |
3095 | } | |
3096 | stdio = PerlSIO_fdopen(PerlIO_fileno(f), mode); | |
3097 | if (stdio) { | |
3098 | PerlIOl *l = *f; | |
3099 | /* De-link any lower layers so new :stdio sticks */ | |
3100 | *f = NULL; | |
3101 | if ((f = PerlIO_push(aTHX_ f, &PerlIO_stdio, buf, Nullsv))) { | |
3102 | PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio); | |
3103 | s->stdio = stdio; | |
3104 | /* Link previous lower layers under new one */ | |
3105 | *PerlIONext(f) = l; | |
3106 | } | |
3107 | else { | |
3108 | /* restore layers list */ | |
3109 | *f = l; | |
3110 | } | |
a33cf58c | 3111 | } |
14a5cf38 JH |
3112 | } |
3113 | return stdio; | |
9e353e3b NIS |
3114 | } |
3115 | ||
81428673 | 3116 | |
9e353e3b NIS |
3117 | FILE * |
3118 | PerlIO_findFILE(PerlIO *f) | |
3119 | { | |
14a5cf38 JH |
3120 | PerlIOl *l = *f; |
3121 | while (l) { | |
3122 | if (l->tab == &PerlIO_stdio) { | |
3123 | PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio); | |
3124 | return s->stdio; | |
3125 | } | |
3126 | l = *PerlIONext(&l); | |
f7e7eb72 | 3127 | } |
4b069b44 NIS |
3128 | /* Uses fallback "mode" via PerlIO_modestr() in PerlIO_exportFILE */ |
3129 | return PerlIO_exportFILE(f, Nullch); | |
9e353e3b NIS |
3130 | } |
3131 | ||
b9d6bf13 | 3132 | /* Use this to reverse PerlIO_exportFILE calls. */ |
9e353e3b NIS |
3133 | void |
3134 | PerlIO_releaseFILE(PerlIO *p, FILE *f) | |
3135 | { | |
22569500 NIS |
3136 | PerlIOl *l; |
3137 | while ((l = *p)) { | |
3138 | if (l->tab == &PerlIO_stdio) { | |
3139 | PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio); | |
3140 | if (s->stdio == f) { | |
3141 | dTHX; | |
3142 | PerlIO_pop(aTHX_ p); | |
3143 | return; | |
3144 | } | |
3145 | } | |
3146 | p = PerlIONext(p); | |
3147 | } | |
3148 | return; | |
9e353e3b NIS |
3149 | } |
3150 | ||
3151 | /*--------------------------------------------------------------------------------------*/ | |
14a5cf38 | 3152 | /* |
71200d45 | 3153 | * perlio buffer layer |
14a5cf38 | 3154 | */ |
9e353e3b | 3155 | |
5e2ab84b | 3156 | IV |
2dc2558e | 3157 | PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
5e2ab84b | 3158 | { |
14a5cf38 JH |
3159 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3160 | int fd = PerlIO_fileno(f); | |
14a5cf38 JH |
3161 | if (fd >= 0 && PerlLIO_isatty(fd)) { |
3162 | PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY; | |
3163 | } | |
4b069b44 NIS |
3164 | if (*PerlIONext(f)) { |
3165 | Off_t posn = PerlIO_tell(PerlIONext(f)); | |
3166 | if (posn != (Off_t) - 1) { | |
3167 | b->posn = posn; | |
3168 | } | |
14a5cf38 | 3169 | } |
2dc2558e | 3170 | return PerlIOBase_pushed(aTHX_ f, mode, arg, tab); |
5e2ab84b NIS |
3171 | } |
3172 | ||
9e353e3b | 3173 | PerlIO * |
14a5cf38 JH |
3174 | PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers, |
3175 | IV n, const char *mode, int fd, int imode, int perm, | |
3176 | PerlIO *f, int narg, SV **args) | |
3177 | { | |
04892f78 | 3178 | if (PerlIOValid(f)) { |
14a5cf38 | 3179 | PerlIO *next = PerlIONext(f); |
04892f78 NIS |
3180 | PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab); |
3181 | next = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm, | |
14a5cf38 | 3182 | next, narg, args); |
2dc2558e | 3183 | if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg, self) != 0) { |
14a5cf38 JH |
3184 | return NULL; |
3185 | } | |
3186 | } | |
3187 | else { | |
04892f78 | 3188 | PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm()); |
14a5cf38 JH |
3189 | int init = 0; |
3190 | if (*mode == 'I') { | |
3191 | init = 1; | |
3192 | /* | |
71200d45 | 3193 | * mode++; |
14a5cf38 JH |
3194 | */ |
3195 | } | |
3196 | f = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm, | |
d9dac8cd | 3197 | f, narg, args); |
14a5cf38 | 3198 | if (f) { |
22569500 | 3199 | if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) { |
b26b1ab5 NC |
3200 | /* |
3201 | * if push fails during open, open fails. close will pop us. | |
3202 | */ | |
3203 | PerlIO_close (f); | |
3204 | return NULL; | |
3205 | } else { | |
3206 | fd = PerlIO_fileno(f); | |
b26b1ab5 NC |
3207 | if (init && fd == 2) { |
3208 | /* | |
3209 | * Initial stderr is unbuffered | |
3210 | */ | |
3211 | PerlIOBase(f)->flags |= PERLIO_F_UNBUF; | |
3212 | } | |
23b84778 IZ |
3213 | #ifdef PERLIO_USING_CRLF |
3214 | # ifdef PERLIO_IS_BINMODE_FD | |
3215 | if (PERLIO_IS_BINMODE_FD(fd)) | |
3216 | PerlIO_binmode(f, '<'/*not used*/, O_BINARY, Nullch); | |
3217 | else | |
3218 | # endif | |
3219 | /* | |
3220 | * do something about failing setmode()? --jhi | |
3221 | */ | |
3222 | PerlLIO_setmode(fd, O_BINARY); | |
3223 | #endif | |
14a5cf38 JH |
3224 | } |
3225 | } | |
ee518936 | 3226 | } |
14a5cf38 | 3227 | return f; |
9e353e3b NIS |
3228 | } |
3229 | ||
14a5cf38 JH |
3230 | /* |
3231 | * This "flush" is akin to sfio's sync in that it handles files in either | |
71200d45 | 3232 | * read or write state |
14a5cf38 | 3233 | */ |
9e353e3b | 3234 | IV |
f62ce20a | 3235 | PerlIOBuf_flush(pTHX_ PerlIO *f) |
6f9d8c32 | 3236 | { |
14a5cf38 JH |
3237 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3238 | int code = 0; | |
04892f78 | 3239 | PerlIO *n = PerlIONext(f); |
14a5cf38 JH |
3240 | if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) { |
3241 | /* | |
71200d45 | 3242 | * write() the buffer |
14a5cf38 JH |
3243 | */ |
3244 | STDCHAR *buf = b->buf; | |
3245 | STDCHAR *p = buf; | |
14a5cf38 JH |
3246 | while (p < b->ptr) { |
3247 | SSize_t count = PerlIO_write(n, p, b->ptr - p); | |
3248 | if (count > 0) { | |
3249 | p += count; | |
3250 | } | |
3251 | else if (count < 0 || PerlIO_error(n)) { | |
3252 | PerlIOBase(f)->flags |= PERLIO_F_ERROR; | |
3253 | code = -1; | |
3254 | break; | |
3255 | } | |
3256 | } | |
3257 | b->posn += (p - buf); | |
3258 | } | |
3259 | else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) { | |
3260 | STDCHAR *buf = PerlIO_get_base(f); | |
3261 | /* | |
71200d45 | 3262 | * Note position change |
14a5cf38 JH |
3263 | */ |
3264 | b->posn += (b->ptr - buf); | |
3265 | if (b->ptr < b->end) { | |
4b069b44 NIS |
3266 | /* We did not consume all of it - try and seek downstream to |
3267 | our logical position | |
14a5cf38 | 3268 | */ |
4b069b44 | 3269 | if (PerlIOValid(n) && PerlIO_seek(n, b->posn, SEEK_SET) == 0) { |
04892f78 NIS |
3270 | /* Reload n as some layers may pop themselves on seek */ |
3271 | b->posn = PerlIO_tell(n = PerlIONext(f)); | |
14a5cf38 | 3272 | } |
ba5c3fe9 | 3273 | else { |
4b069b44 NIS |
3274 | /* Seek failed (e.g. pipe or tty). Do NOT clear buffer or pre-read |
3275 | data is lost for good - so return saying "ok" having undone | |
3276 | the position adjust | |
3277 | */ | |
3278 | b->posn -= (b->ptr - buf); | |
ba5c3fe9 NIS |
3279 | return code; |
3280 | } | |
14a5cf38 JH |
3281 | } |
3282 | } | |
3283 | b->ptr = b->end = b->buf; | |
3284 | PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF); | |
04892f78 | 3285 | /* We check for Valid because of dubious decision to make PerlIO_flush(NULL) flush all */ |
04892f78 | 3286 | if (PerlIOValid(n) && PerlIO_flush(n) != 0) |
14a5cf38 JH |
3287 | code = -1; |
3288 | return code; | |
6f9d8c32 NIS |
3289 | } |
3290 | ||
06da4f11 | 3291 | IV |
f62ce20a | 3292 | PerlIOBuf_fill(pTHX_ PerlIO *f) |
06da4f11 | 3293 | { |
14a5cf38 JH |
3294 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3295 | PerlIO *n = PerlIONext(f); | |
3296 | SSize_t avail; | |
3297 | /* | |
4b069b44 NIS |
3298 | * Down-stream flush is defined not to loose read data so is harmless. |
3299 | * we would not normally be fill'ing if there was data left in anycase. | |
14a5cf38 JH |
3300 | */ |
3301 | if (PerlIO_flush(f) != 0) | |
3302 | return -1; | |
3303 | if (PerlIOBase(f)->flags & PERLIO_F_TTY) | |
f62ce20a | 3304 | PerlIOBase_flush_linebuf(aTHX); |
14a5cf38 JH |
3305 | |
3306 | if (!b->buf) | |
22569500 | 3307 | PerlIO_get_base(f); /* allocate via vtable */ |
14a5cf38 JH |
3308 | |
3309 | b->ptr = b->end = b->buf; | |
4b069b44 NIS |
3310 | |
3311 | if (!PerlIOValid(n)) { | |
3312 | PerlIOBase(f)->flags |= PERLIO_F_EOF; | |
3313 | return -1; | |
3314 | } | |
3315 | ||
14a5cf38 JH |
3316 | if (PerlIO_fast_gets(n)) { |
3317 | /* | |
04892f78 | 3318 | * Layer below is also buffered. We do _NOT_ want to call its |
14a5cf38 JH |
3319 | * ->Read() because that will loop till it gets what we asked for |
3320 | * which may hang on a pipe etc. Instead take anything it has to | |
71200d45 | 3321 | * hand, or ask it to fill _once_. |
14a5cf38 JH |
3322 | */ |
3323 | avail = PerlIO_get_cnt(n); | |
3324 | if (avail <= 0) { | |
3325 | avail = PerlIO_fill(n); | |
3326 | if (avail == 0) | |
3327 | avail = PerlIO_get_cnt(n); | |
3328 | else { | |
3329 | if (!PerlIO_error(n) && PerlIO_eof(n)) | |
3330 | avail = 0; | |
3331 | } | |
3332 | } | |
3333 | if (avail > 0) { | |
3334 | STDCHAR *ptr = PerlIO_get_ptr(n); | |
3335 | SSize_t cnt = avail; | |
eb160463 | 3336 | if (avail > (SSize_t)b->bufsiz) |
14a5cf38 JH |
3337 | avail = b->bufsiz; |
3338 | Copy(ptr, b->buf, avail, STDCHAR); | |
3339 | PerlIO_set_ptrcnt(n, ptr + avail, cnt - avail); | |
3340 | } | |
3341 | } | |
3342 | else { | |
3343 | avail = PerlIO_read(n, b->ptr, b->bufsiz); | |
3344 | } | |
3345 | if (avail <= 0) { | |
3346 | if (avail == 0) | |
3347 | PerlIOBase(f)->flags |= PERLIO_F_EOF; | |
3348 | else | |
3349 | PerlIOBase(f)->flags |= PERLIO_F_ERROR; | |
3350 | return -1; | |
3351 | } | |
3352 | b->end = b->buf + avail; | |
3353 | PerlIOBase(f)->flags |= PERLIO_F_RDBUF; | |
3354 | return 0; | |
06da4f11 NIS |
3355 | } |
3356 | ||
6f9d8c32 | 3357 | SSize_t |
f62ce20a | 3358 | PerlIOBuf_read(pTHX_ PerlIO *f, void *vbuf, Size_t count) |
6f9d8c32 | 3359 | { |
14a5cf38 | 3360 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
04892f78 | 3361 | if (PerlIOValid(f)) { |
14a5cf38 JH |
3362 | if (!b->ptr) |
3363 | PerlIO_get_base(f); | |
f62ce20a | 3364 | return PerlIOBase_read(aTHX_ f, vbuf, count); |
14a5cf38 JH |
3365 | } |
3366 | return 0; | |
6f9d8c32 NIS |
3367 | } |
3368 | ||
9e353e3b | 3369 | SSize_t |
f62ce20a | 3370 | PerlIOBuf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count) |
760ac839 | 3371 | { |
14a5cf38 JH |
3372 | const STDCHAR *buf = (const STDCHAR *) vbuf + count; |
3373 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); | |
3374 | SSize_t unread = 0; | |
3375 | SSize_t avail; | |
3376 | if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) | |
3377 | PerlIO_flush(f); | |
3378 | if (!b->buf) | |
3379 | PerlIO_get_base(f); | |
3380 | if (b->buf) { | |
3381 | if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) { | |
3382 | /* | |
3383 | * Buffer is already a read buffer, we can overwrite any chars | |
71200d45 | 3384 | * which have been read back to buffer start |
14a5cf38 JH |
3385 | */ |
3386 | avail = (b->ptr - b->buf); | |
3387 | } | |
3388 | else { | |
3389 | /* | |
3390 | * Buffer is idle, set it up so whole buffer is available for | |
71200d45 | 3391 | * unread |
14a5cf38 JH |
3392 | */ |
3393 | avail = b->bufsiz; | |
3394 | b->end = b->buf + avail; | |
3395 | b->ptr = b->end; | |
3396 | PerlIOBase(f)->flags |= PERLIO_F_RDBUF; | |
3397 | /* | |
71200d45 | 3398 | * Buffer extends _back_ from where we are now |
14a5cf38 JH |
3399 | */ |
3400 | b->posn -= b->bufsiz; | |
3401 | } | |
3402 | if (avail > (SSize_t) count) { | |
3403 | /* | |
71200d45 | 3404 | * If we have space for more than count, just move count |
14a5cf38 JH |
3405 | */ |
3406 | avail = count; | |
3407 | } | |
3408 | if (avail > 0) { | |
3409 | b->ptr -= avail; | |
3410 | buf -= avail; | |
3411 | /* | |
3412 | * In simple stdio-like ungetc() case chars will be already | |
71200d45 | 3413 | * there |
14a5cf38 JH |
3414 | */ |
3415 | if (buf != b->ptr) { | |
3416 | Copy(buf, b->ptr, avail, STDCHAR); | |
3417 | } | |
3418 | count -= avail; | |
3419 | unread += avail; | |
3420 | PerlIOBase(f)->flags &= ~PERLIO_F_EOF; | |
3421 | } | |
3422 | } | |
93679785 NIS |
3423 | if (count > 0) { |
3424 | unread += PerlIOBase_unread(aTHX_ f, vbuf, count); | |
3425 | } | |
14a5cf38 | 3426 | return unread; |
760ac839 LW |
3427 | } |
3428 | ||
9e353e3b | 3429 | SSize_t |
f62ce20a | 3430 | PerlIOBuf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count) |
760ac839 | 3431 | { |
14a5cf38 JH |
3432 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3433 | const STDCHAR *buf = (const STDCHAR *) vbuf; | |
3434 | Size_t written = 0; | |
3435 | if (!b->buf) | |
3436 | PerlIO_get_base(f); | |
3437 | if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) | |
3438 | return 0; | |
0678cb22 NIS |
3439 | if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) { |
3440 | if (PerlIO_flush(f) != 0) { | |
3441 | return 0; | |
3442 | } | |
3443 | } | |
14a5cf38 JH |
3444 | while (count > 0) { |
3445 | SSize_t avail = b->bufsiz - (b->ptr - b->buf); | |
3446 | if ((SSize_t) count < avail) | |
3447 | avail = count; | |
3448 | PerlIOBase(f)->flags |= PERLIO_F_WRBUF; | |
3449 | if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) { | |
3450 | while (avail > 0) { | |
3451 | int ch = *buf++; | |
3452 | *(b->ptr)++ = ch; | |
3453 | count--; | |
3454 | avail--; | |
3455 | written++; | |
3456 | if (ch == '\n') { | |
3457 | PerlIO_flush(f); | |
3458 | break; | |
3459 | } | |
3460 | } | |
3461 | } | |
3462 | else { | |
3463 | if (avail) { | |
3464 | Copy(buf, b->ptr, avail, STDCHAR); | |
3465 | count -= avail; | |
3466 | buf += avail; | |
3467 | written += avail; | |
3468 | b->ptr += avail; | |
3469 | } | |
3470 | } | |
3471 | if (b->ptr >= (b->buf + b->bufsiz)) | |
3472 | PerlIO_flush(f); | |
3473 | } | |
3474 | if (PerlIOBase(f)->flags & PERLIO_F_UNBUF) | |
3475 | PerlIO_flush(f); | |
3476 | return written; | |
9e353e3b NIS |
3477 | } |
3478 | ||
3479 | IV | |
f62ce20a | 3480 | PerlIOBuf_seek(pTHX_ PerlIO *f, Off_t offset, int whence) |
9e353e3b | 3481 | { |
14a5cf38 JH |
3482 | IV code; |
3483 | if ((code = PerlIO_flush(f)) == 0) { | |
3484 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); | |
3485 | PerlIOBase(f)->flags &= ~PERLIO_F_EOF; | |
3486 | code = PerlIO_seek(PerlIONext(f), offset, whence); | |
3487 | if (code == 0) { | |
3488 | b->posn = PerlIO_tell(PerlIONext(f)); | |
3489 | } | |
9e353e3b | 3490 | } |
14a5cf38 | 3491 | return code; |
9e353e3b NIS |
3492 | } |
3493 | ||
3494 | Off_t | |
f62ce20a | 3495 | PerlIOBuf_tell(pTHX_ PerlIO *f) |
9e353e3b | 3496 | { |
14a5cf38 JH |
3497 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3498 | /* | |
71200d45 | 3499 | * b->posn is file position where b->buf was read, or will be written |
14a5cf38 JH |
3500 | */ |
3501 | Off_t posn = b->posn; | |
0678cb22 NIS |
3502 | if ((PerlIOBase(f)->flags & PERLIO_F_APPEND) && |
3503 | (PerlIOBase(f)->flags & PERLIO_F_WRBUF)) { | |
3504 | #if 1 | |
3505 | /* As O_APPEND files are normally shared in some sense it is better | |
3506 | to flush : | |
3507 | */ | |
3508 | PerlIO_flush(f); | |
3509 | #else | |
3510 | /* when file is NOT shared then this is sufficient */ | |
3511 | PerlIO_seek(PerlIONext(f),0, SEEK_END); | |
3512 | #endif | |
3513 | posn = b->posn = PerlIO_tell(PerlIONext(f)); | |
3514 | } | |
14a5cf38 JH |
3515 | if (b->buf) { |
3516 | /* | |
71200d45 | 3517 | * If buffer is valid adjust position by amount in buffer |
14a5cf38 JH |
3518 | */ |
3519 | posn += (b->ptr - b->buf); | |
3520 | } | |
3521 | return posn; | |
9e353e3b NIS |
3522 | } |
3523 | ||
3524 | IV | |
44798d05 NIS |
3525 | PerlIOBuf_popped(pTHX_ PerlIO *f) |
3526 | { | |
3527 | IV code = PerlIOBase_popped(aTHX_ f); | |
3528 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); | |
3529 | if (b->buf && b->buf != (STDCHAR *) & b->oneword) { | |
3530 | Safefree(b->buf); | |
3531 | } | |
3532 | b->buf = NULL; | |
3533 | b->ptr = b->end = b->buf; | |
3534 | PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF); | |
3535 | return code; | |
3536 | } | |
3537 | ||
3538 | IV | |
f62ce20a | 3539 | PerlIOBuf_close(pTHX_ PerlIO *f) |
9e353e3b | 3540 | { |
f62ce20a | 3541 | IV code = PerlIOBase_close(aTHX_ f); |
14a5cf38 JH |
3542 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3543 | if (b->buf && b->buf != (STDCHAR *) & b->oneword) { | |
3a1ee7e8 | 3544 | Safefree(b->buf); |
14a5cf38 JH |
3545 | } |
3546 | b->buf = NULL; | |
3547 | b->ptr = b->end = b->buf; | |
3548 | PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF); | |
3549 | return code; | |
760ac839 LW |
3550 | } |
3551 | ||
9e353e3b | 3552 | STDCHAR * |
f62ce20a | 3553 | PerlIOBuf_get_ptr(pTHX_ PerlIO *f) |
9e353e3b | 3554 | { |
14a5cf38 JH |
3555 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3556 | if (!b->buf) | |
3557 | PerlIO_get_base(f); | |
3558 | return b->ptr; | |
9e353e3b NIS |
3559 | } |
3560 | ||
05d1247b | 3561 | SSize_t |
f62ce20a | 3562 | PerlIOBuf_get_cnt(pTHX_ PerlIO *f) |
9e353e3b | 3563 | { |
14a5cf38 JH |
3564 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3565 | if (!b->buf) | |
3566 | PerlIO_get_base(f); | |
3567 | if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) | |
3568 | return (b->end - b->ptr); | |
3569 | return 0; | |
9e353e3b NIS |
3570 | } |
3571 | ||
3572 | STDCHAR * | |
f62ce20a | 3573 | PerlIOBuf_get_base(pTHX_ PerlIO *f) |
9e353e3b | 3574 | { |
14a5cf38 JH |
3575 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3576 | if (!b->buf) { | |
3577 | if (!b->bufsiz) | |
3578 | b->bufsiz = 4096; | |
a1ea730d | 3579 | b->buf = |
7fcdafbd | 3580 | Newz('B',b->buf,b->bufsiz, STDCHAR); |
14a5cf38 JH |
3581 | if (!b->buf) { |
3582 | b->buf = (STDCHAR *) & b->oneword; | |
3583 | b->bufsiz = sizeof(b->oneword); | |
3584 | } | |
3585 | b->ptr = b->buf; | |
3586 | b->end = b->ptr; | |
06da4f11 | 3587 | } |
14a5cf38 | 3588 | return b->buf; |
9e353e3b NIS |
3589 | } |
3590 | ||
3591 | Size_t | |
f62ce20a | 3592 | PerlIOBuf_bufsiz(pTHX_ PerlIO *f) |
9e353e3b | 3593 | { |
14a5cf38 JH |
3594 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3595 | if (!b->buf) | |
3596 | PerlIO_get_base(f); | |
3597 | return (b->end - b->buf); | |
9e353e3b NIS |
3598 | } |
3599 | ||
3600 | void | |
f62ce20a | 3601 | PerlIOBuf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt) |
9e353e3b | 3602 | { |
14a5cf38 JH |
3603 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3604 | if (!b->buf) | |
3605 | PerlIO_get_base(f); | |
3606 | b->ptr = ptr; | |
3607 | if (PerlIO_get_cnt(f) != cnt || b->ptr < b->buf) { | |
14a5cf38 JH |
3608 | assert(PerlIO_get_cnt(f) == cnt); |
3609 | assert(b->ptr >= b->buf); | |
3610 | } | |
3611 | PerlIOBase(f)->flags |= PERLIO_F_RDBUF; | |
760ac839 LW |
3612 | } |
3613 | ||
71200d45 | 3614 | PerlIO * |
ecdeb87c | 3615 | PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags) |
71200d45 | 3616 | { |
ecdeb87c | 3617 | return PerlIOBase_dup(aTHX_ f, o, param, flags); |
71200d45 NIS |
3618 | } |
3619 | ||
3620 | ||
3621 | ||
9e353e3b | 3622 | PerlIO_funcs PerlIO_perlio = { |
2dc2558e | 3623 | sizeof(PerlIO_funcs), |
14a5cf38 JH |
3624 | "perlio", |
3625 | sizeof(PerlIOBuf), | |
86e05cf2 | 3626 | PERLIO_K_BUFFERED|PERLIO_K_RAW, |
14a5cf38 | 3627 | PerlIOBuf_pushed, |
44798d05 | 3628 | PerlIOBuf_popped, |
14a5cf38 | 3629 | PerlIOBuf_open, |
86e05cf2 | 3630 | PerlIOBase_binmode, /* binmode */ |
14a5cf38 JH |
3631 | NULL, |
3632 | PerlIOBase_fileno, | |
71200d45 | 3633 | PerlIOBuf_dup, |
14a5cf38 JH |
3634 | PerlIOBuf_read, |
3635 | PerlIOBuf_unread, | |
3636 | PerlIOBuf_write, | |
3637 | PerlIOBuf_seek, | |
3638 | PerlIOBuf_tell, | |
3639 | PerlIOBuf_close, | |
3640 | PerlIOBuf_flush, | |
3641 | PerlIOBuf_fill, | |
3642 | PerlIOBase_eof, | |
3643 | PerlIOBase_error, | |
3644 | PerlIOBase_clearerr, | |
3645 | PerlIOBase_setlinebuf, | |
3646 | PerlIOBuf_get_base, | |
3647 | PerlIOBuf_bufsiz, | |
3648 | PerlIOBuf_get_ptr, | |
3649 | PerlIOBuf_get_cnt, | |
3650 | PerlIOBuf_set_ptrcnt, | |
9e353e3b NIS |
3651 | }; |
3652 | ||
66ecd56b | 3653 | /*--------------------------------------------------------------------------------------*/ |
14a5cf38 | 3654 | /* |
71200d45 | 3655 | * Temp layer to hold unread chars when cannot do it any other way |
14a5cf38 | 3656 | */ |
5e2ab84b NIS |
3657 | |
3658 | IV | |
f62ce20a | 3659 | PerlIOPending_fill(pTHX_ PerlIO *f) |
5e2ab84b | 3660 | { |
14a5cf38 | 3661 | /* |
71200d45 | 3662 | * Should never happen |
14a5cf38 JH |
3663 | */ |
3664 | PerlIO_flush(f); | |
3665 | return 0; | |
5e2ab84b NIS |
3666 | } |
3667 | ||
3668 | IV | |
f62ce20a | 3669 | PerlIOPending_close(pTHX_ PerlIO *f) |
5e2ab84b | 3670 | { |
14a5cf38 | 3671 | /* |
71200d45 | 3672 | * A tad tricky - flush pops us, then we close new top |
14a5cf38 JH |
3673 | */ |
3674 | PerlIO_flush(f); | |
3675 | return PerlIO_close(f); | |
5e2ab84b NIS |
3676 | } |
3677 | ||
3678 | IV | |
f62ce20a | 3679 | PerlIOPending_seek(pTHX_ PerlIO *f, Off_t offset, int whence) |
5e2ab84b | 3680 | { |
14a5cf38 | 3681 | /* |
71200d45 | 3682 | * A tad tricky - flush pops us, then we seek new top |
14a5cf38 JH |
3683 | */ |
3684 | PerlIO_flush(f); | |
3685 | return PerlIO_seek(f, offset, whence); | |
5e2ab84b NIS |
3686 | } |
3687 | ||
3688 | ||
3689 | IV | |
f62ce20a | 3690 | PerlIOPending_flush(pTHX_ PerlIO *f) |
5e2ab84b | 3691 | { |
14a5cf38 JH |
3692 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3693 | if (b->buf && b->buf != (STDCHAR *) & b->oneword) { | |
3a1ee7e8 | 3694 | Safefree(b->buf); |
14a5cf38 JH |
3695 | b->buf = NULL; |
3696 | } | |
3697 | PerlIO_pop(aTHX_ f); | |
3698 | return 0; | |
5e2ab84b NIS |
3699 | } |
3700 | ||
3701 | void | |
f62ce20a | 3702 | PerlIOPending_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt) |
5e2ab84b | 3703 | { |
14a5cf38 JH |
3704 | if (cnt <= 0) { |
3705 | PerlIO_flush(f); | |
3706 | } | |
3707 | else { | |
f62ce20a | 3708 | PerlIOBuf_set_ptrcnt(aTHX_ f, ptr, cnt); |
14a5cf38 | 3709 | } |
5e2ab84b NIS |
3710 | } |
3711 | ||
3712 | IV | |
2dc2558e | 3713 | PerlIOPending_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
5e2ab84b | 3714 | { |
2dc2558e | 3715 | IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab); |
14a5cf38 JH |
3716 | PerlIOl *l = PerlIOBase(f); |
3717 | /* | |
71200d45 NIS |
3718 | * Our PerlIO_fast_gets must match what we are pushed on, or sv_gets() |
3719 | * etc. get muddled when it changes mid-string when we auto-pop. | |
14a5cf38 JH |
3720 | */ |
3721 | l->flags = (l->flags & ~(PERLIO_F_FASTGETS | PERLIO_F_UTF8)) | | |