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