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