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