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