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