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