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