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