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