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