Commit | Line | Data |
---|---|---|
14a5cf38 | 1 | /* |
71200d45 | 2 | * perlio.c Copyright (c) 1996-2001, Nick Ing-Simmons You may distribute |
14a5cf38 | 3 | * under the terms of either the GNU General Public License or the |
71200d45 | 4 | * Artistic License, as specified in the README file. |
760ac839 LW |
5 | */ |
6 | ||
14a5cf38 | 7 | /* |
71200d45 | 8 | * If we have ActivePerl-like PERL_IMPLICIT_SYS then we need a dTHX to get |
14a5cf38 | 9 | * at the dispatch tables, even when we do not need it for other reasons. |
71200d45 | 10 | * Invent a dSYS macro to abstract this out |
14a5cf38 | 11 | */ |
7bcba3d4 NIS |
12 | #ifdef PERL_IMPLICIT_SYS |
13 | #define dSYS dTHX | |
14 | #else | |
15 | #define dSYS dNOOP | |
16 | #endif | |
17 | ||
760ac839 | 18 | #define VOIDUSED 1 |
12ae5dfc JH |
19 | #ifdef PERL_MICRO |
20 | # include "uconfig.h" | |
21 | #else | |
22 | # include "config.h" | |
23 | #endif | |
760ac839 | 24 | |
6f9d8c32 | 25 | #define PERLIO_NOT_STDIO 0 |
760ac839 | 26 | #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO) |
14a5cf38 | 27 | /* |
71200d45 | 28 | * #define PerlIO FILE |
14a5cf38 | 29 | */ |
760ac839 LW |
30 | #endif |
31 | /* | |
6f9d8c32 | 32 | * This file provides those parts of PerlIO abstraction |
88b61e10 | 33 | * which are not #defined in perlio.h. |
6f9d8c32 | 34 | * Which these are depends on various Configure #ifdef's |
760ac839 LW |
35 | */ |
36 | ||
37 | #include "EXTERN.h" | |
864dbfa3 | 38 | #define PERL_IN_PERLIO_C |
760ac839 LW |
39 | #include "perl.h" |
40 | ||
0c4f7ff0 NIS |
41 | #include "XSUB.h" |
42 | ||
5f1a76d0 NIS |
43 | #undef PerlMemShared_calloc |
44 | #define PerlMemShared_calloc(x,y) calloc(x,y) | |
45 | #undef PerlMemShared_free | |
46 | #define PerlMemShared_free(x) free(x) | |
47 | ||
60382766 | 48 | int |
f5b9d040 | 49 | perlsio_binmode(FILE *fp, int iotype, int mode) |
60382766 | 50 | { |
14a5cf38 | 51 | /* |
71200d45 | 52 | * This used to be contents of do_binmode in doio.c |
14a5cf38 | 53 | */ |
60382766 NIS |
54 | #ifdef DOSISH |
55 | # if defined(atarist) || defined(__MINT__) | |
f5b9d040 | 56 | if (!fflush(fp)) { |
60382766 | 57 | if (mode & O_BINARY) |
14a5cf38 | 58 | ((FILE *) fp)->_flag |= _IOBIN; |
60382766 | 59 | else |
14a5cf38 | 60 | ((FILE *) fp)->_flag &= ~_IOBIN; |
60382766 NIS |
61 | return 1; |
62 | } | |
63 | return 0; | |
64 | # else | |
eb73beca | 65 | dTHX; |
14a5cf38 JH |
66 | #ifdef NETWARE |
67 | if (PerlLIO_setmode(fp, mode) != -1) { | |
68 | #else | |
f5b9d040 | 69 | if (PerlLIO_setmode(fileno(fp), mode) != -1) { |
14a5cf38 | 70 | #endif |
60382766 | 71 | # if defined(WIN32) && defined(__BORLANDC__) |
14a5cf38 | 72 | /* |
71200d45 | 73 | * The translation mode of the stream is maintained independent of |
14a5cf38 | 74 | * the translation mode of the fd in the Borland RTL (heavy |
71200d45 | 75 | * digging through their runtime sources reveal). User has to set |
14a5cf38 | 76 | * the mode explicitly for the stream (though they don't document |
71200d45 | 77 | * this anywhere). GSAR 97-5-24 |
60382766 | 78 | */ |
14a5cf38 | 79 | fseek(fp, 0L, 0); |
60382766 | 80 | if (mode & O_BINARY) |
f5b9d040 | 81 | fp->flags |= _F_BIN; |
60382766 | 82 | else |
14a5cf38 | 83 | fp->flags &= ~_F_BIN; |
60382766 NIS |
84 | # endif |
85 | return 1; | |
86 | } | |
87 | else | |
88 | return 0; | |
89 | # endif | |
90 | #else | |
91 | # if defined(USEMYBINMODE) | |
92 | if (my_binmode(fp, iotype, mode) != FALSE) | |
93 | return 1; | |
94 | else | |
95 | return 0; | |
96 | # else | |
97 | return 1; | |
98 | # endif | |
99 | #endif | |
100 | } | |
101 | ||
eb73beca NIS |
102 | #ifndef PERLIO_LAYERS |
103 | int | |
104 | PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names) | |
105 | { | |
14a5cf38 JH |
106 | if (!names || !*names || strEQ(names, ":crlf") || strEQ(names, ":raw")) { |
107 | return 0; | |
108 | } | |
109 | Perl_croak(aTHX_ "Cannot apply \"%s\" in non-PerlIO perl", names); | |
110 | /* | |
71200d45 | 111 | * NOTREACHED |
14a5cf38 JH |
112 | */ |
113 | return -1; | |
eb73beca NIS |
114 | } |
115 | ||
13621cfb NIS |
116 | void |
117 | PerlIO_destruct(pTHX) | |
118 | { | |
119 | } | |
120 | ||
f5b9d040 NIS |
121 | int |
122 | PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names) | |
123 | { | |
92bff44d | 124 | #ifdef USE_SFIO |
14a5cf38 | 125 | return 1; |
92bff44d | 126 | #else |
14a5cf38 | 127 | return perlsio_binmode(fp, iotype, mode); |
92bff44d | 128 | #endif |
f5b9d040 | 129 | } |
60382766 | 130 | |
14a5cf38 | 131 | /* |
71200d45 | 132 | * De-mux PerlIO_openn() into fdopen, freopen and fopen type entries |
14a5cf38 | 133 | */ |
ee518936 NIS |
134 | |
135 | PerlIO * | |
14a5cf38 JH |
136 | PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd, |
137 | int imode, int perm, PerlIO *old, int narg, SV **args) | |
138 | { | |
139 | if (narg == 1) { | |
140 | if (*args == &PL_sv_undef) | |
141 | return PerlIO_tmpfile(); | |
142 | else { | |
143 | char *name = SvPV_nolen(*args); | |
144 | if (*mode == '#') { | |
145 | fd = PerlLIO_open3(name, imode, perm); | |
146 | if (fd >= 0) | |
147 | return PerlIO_fdopen(fd, (char *) mode + 1); | |
148 | } | |
149 | else if (old) { | |
150 | return PerlIO_reopen(name, mode, old); | |
151 | } | |
152 | else { | |
153 | return PerlIO_open(name, mode); | |
154 | } | |
155 | } | |
156 | } | |
157 | else { | |
158 | return PerlIO_fdopen(fd, (char *) mode); | |
159 | } | |
160 | return NULL; | |
ee518936 NIS |
161 | } |
162 | ||
0c4f7ff0 NIS |
163 | XS(XS_PerlIO__Layer__find) |
164 | { | |
14a5cf38 JH |
165 | dXSARGS; |
166 | if (items < 2) | |
167 | Perl_croak(aTHX_ "Usage class->find(name[,load])"); | |
168 | else { | |
169 | char *name = SvPV_nolen(ST(1)); | |
170 | ST(0) = (strEQ(name, "crlf") | |
171 | || strEQ(name, "raw")) ? &PL_sv_yes : &PL_sv_undef; | |
172 | XSRETURN(1); | |
173 | } | |
0c4f7ff0 NIS |
174 | } |
175 | ||
176 | ||
177 | void | |
178 | Perl_boot_core_PerlIO(pTHX) | |
179 | { | |
14a5cf38 | 180 | newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__); |
0c4f7ff0 NIS |
181 | } |
182 | ||
ac27b0f5 NIS |
183 | #endif |
184 | ||
32e30700 | 185 | |
6f9d8c32 | 186 | #ifdef PERLIO_IS_STDIO |
760ac839 LW |
187 | |
188 | void | |
8ac85365 | 189 | PerlIO_init(void) |
760ac839 | 190 | { |
14a5cf38 JH |
191 | /* |
192 | * Does nothing (yet) except force this file to be included in perl | |
71200d45 | 193 | * binary. That allows this file to force inclusion of other functions |
14a5cf38 | 194 | * that may be required by loadable extensions e.g. for |
71200d45 | 195 | * FileHandle::tmpfile |
14a5cf38 | 196 | */ |
760ac839 LW |
197 | } |
198 | ||
33dcbb9a | 199 | #undef PerlIO_tmpfile |
200 | PerlIO * | |
8ac85365 | 201 | PerlIO_tmpfile(void) |
33dcbb9a | 202 | { |
14a5cf38 | 203 | return tmpfile(); |
33dcbb9a | 204 | } |
205 | ||
14a5cf38 | 206 | #else /* PERLIO_IS_STDIO */ |
760ac839 LW |
207 | |
208 | #ifdef USE_SFIO | |
209 | ||
210 | #undef HAS_FSETPOS | |
211 | #undef HAS_FGETPOS | |
212 | ||
14a5cf38 JH |
213 | /* |
214 | * This section is just to make sure these functions get pulled in from | |
71200d45 | 215 | * libsfio.a |
14a5cf38 | 216 | */ |
760ac839 LW |
217 | |
218 | #undef PerlIO_tmpfile | |
219 | PerlIO * | |
c78749f2 | 220 | PerlIO_tmpfile(void) |
760ac839 | 221 | { |
14a5cf38 | 222 | return sftmp(0); |
760ac839 LW |
223 | } |
224 | ||
225 | void | |
c78749f2 | 226 | PerlIO_init(void) |
760ac839 | 227 | { |
14a5cf38 JH |
228 | /* |
229 | * Force this file to be included in perl binary. Which allows this | |
230 | * file to force inclusion of other functions that may be required by | |
71200d45 | 231 | * loadable extensions e.g. for FileHandle::tmpfile |
14a5cf38 | 232 | */ |
760ac839 | 233 | |
14a5cf38 | 234 | /* |
71200d45 | 235 | * Hack sfio does its own 'autoflush' on stdout in common cases. Flush |
14a5cf38 | 236 | * results in a lot of lseek()s to regular files and lot of small |
71200d45 | 237 | * writes to pipes. |
14a5cf38 JH |
238 | */ |
239 | sfset(sfstdout, SF_SHARE, 0); | |
760ac839 LW |
240 | } |
241 | ||
92bff44d NIS |
242 | PerlIO * |
243 | PerlIO_importFILE(FILE *stdio, int fl) | |
244 | { | |
14a5cf38 JH |
245 | int fd = fileno(stdio); |
246 | PerlIO *r = PerlIO_fdopen(fd, "r+"); | |
247 | return r; | |
92bff44d NIS |
248 | } |
249 | ||
250 | FILE * | |
251 | PerlIO_findFILE(PerlIO *pio) | |
252 | { | |
14a5cf38 JH |
253 | int fd = PerlIO_fileno(pio); |
254 | FILE *f = fdopen(fd, "r+"); | |
255 | PerlIO_flush(pio); | |
256 | if (!f && errno == EINVAL) | |
257 | f = fdopen(fd, "w"); | |
258 | if (!f && errno == EINVAL) | |
259 | f = fdopen(fd, "r"); | |
260 | return f; | |
92bff44d NIS |
261 | } |
262 | ||
263 | ||
14a5cf38 | 264 | #else /* USE_SFIO */ |
6f9d8c32 | 265 | /*======================================================================================*/ |
14a5cf38 | 266 | /* |
71200d45 | 267 | * Implement all the PerlIO interface ourselves. |
9e353e3b | 268 | */ |
760ac839 | 269 | |
76ced9ad NIS |
270 | #include "perliol.h" |
271 | ||
14a5cf38 JH |
272 | /* |
273 | * We _MUST_ have <unistd.h> if we are using lseek() and may have large | |
71200d45 | 274 | * files |
14a5cf38 | 275 | */ |
02f66e2f NIS |
276 | #ifdef I_UNISTD |
277 | #include <unistd.h> | |
278 | #endif | |
06da4f11 NIS |
279 | #ifdef HAS_MMAP |
280 | #include <sys/mman.h> | |
281 | #endif | |
282 | ||
02f66e2f | 283 | |
14a5cf38 JH |
284 | void PerlIO_debug(const char *fmt, ...) |
285 | __attribute__ ((format(__printf__, 1, 2))); | |
6f9d8c32 | 286 | |
6f9d8c32 | 287 | void |
14a5cf38 JH |
288 | PerlIO_debug(const char *fmt, ...) |
289 | { | |
290 | static int dbg = 0; | |
291 | va_list ap; | |
292 | dSYS; | |
293 | va_start(ap, fmt); | |
294 | if (!dbg) { | |
295 | char *s = PerlEnv_getenv("PERLIO_DEBUG"); | |
296 | if (s && *s) | |
297 | dbg = PerlLIO_open3(s, O_WRONLY | O_CREAT | O_APPEND, 0666); | |
298 | else | |
299 | dbg = -1; | |
300 | } | |
301 | if (dbg > 0) { | |
302 | dTHX; | |
303 | SV *sv = newSVpvn("", 0); | |
304 | char *s; | |
305 | STRLEN len; | |
306 | s = CopFILE(PL_curcop); | |
307 | if (!s) | |
308 | s = "(none)"; | |
309 | Perl_sv_catpvf(aTHX_ sv, "%s:%" IVdf " ", s, | |
310 | (IV) CopLINE(PL_curcop)); | |
311 | Perl_sv_vcatpvf(aTHX_ sv, fmt, &ap); | |
312 | ||
313 | s = SvPV(sv, len); | |
314 | PerlLIO_write(dbg, s, len); | |
315 | SvREFCNT_dec(sv); | |
316 | } | |
317 | va_end(ap); | |
6f9d8c32 NIS |
318 | } |
319 | ||
9e353e3b NIS |
320 | /*--------------------------------------------------------------------------------------*/ |
321 | ||
14a5cf38 | 322 | /* |
71200d45 | 323 | * Inner level routines |
14a5cf38 | 324 | */ |
9e353e3b | 325 | |
14a5cf38 | 326 | /* |
71200d45 | 327 | * Table of pointers to the PerlIO structs (malloc'ed) |
14a5cf38 JH |
328 | */ |
329 | PerlIO *_perlio = NULL; | |
05d1247b | 330 | #define PERLIO_TABLE_SIZE 64 |
6f9d8c32 | 331 | |
e3f3bf95 NIS |
332 | |
333 | ||
760ac839 | 334 | PerlIO * |
5f1a76d0 | 335 | PerlIO_allocate(pTHX) |
6f9d8c32 | 336 | { |
14a5cf38 | 337 | /* |
71200d45 | 338 | * Find a free slot in the table, allocating new table as necessary |
14a5cf38 JH |
339 | */ |
340 | PerlIO **last; | |
341 | PerlIO *f; | |
342 | last = &_perlio; | |
343 | while ((f = *last)) { | |
344 | int i; | |
345 | last = (PerlIO **) (f); | |
346 | for (i = 1; i < PERLIO_TABLE_SIZE; i++) { | |
347 | if (!*++f) { | |
348 | return f; | |
349 | } | |
350 | } | |
351 | } | |
352 | f = PerlMemShared_calloc(PERLIO_TABLE_SIZE, sizeof(PerlIO)); | |
353 | if (!f) { | |
354 | return NULL; | |
355 | } | |
356 | *last = f; | |
357 | return f + 1; | |
05d1247b NIS |
358 | } |
359 | ||
360 | void | |
5f1a76d0 | 361 | PerlIO_cleantable(pTHX_ PerlIO **tablep) |
05d1247b | 362 | { |
14a5cf38 JH |
363 | PerlIO *table = *tablep; |
364 | if (table) { | |
365 | int i; | |
366 | PerlIO_cleantable(aTHX_(PerlIO **) & (table[0])); | |
367 | for (i = PERLIO_TABLE_SIZE - 1; i > 0; i--) { | |
368 | PerlIO *f = table + i; | |
369 | if (*f) { | |
370 | PerlIO_close(f); | |
371 | } | |
372 | } | |
373 | PerlMemShared_free(table); | |
374 | *tablep = NULL; | |
05d1247b | 375 | } |
05d1247b NIS |
376 | } |
377 | ||
fcf2db38 NIS |
378 | PerlIO_list_t *PerlIO_known_layers; |
379 | PerlIO_list_t *PerlIO_def_layerlist; | |
380 | ||
381 | PerlIO_list_t * | |
382 | PerlIO_list_alloc(void) | |
383 | { | |
14a5cf38 JH |
384 | PerlIO_list_t *list; |
385 | Newz('L', list, 1, PerlIO_list_t); | |
386 | list->refcnt = 1; | |
387 | return list; | |
fcf2db38 NIS |
388 | } |
389 | ||
390 | void | |
391 | PerlIO_list_free(PerlIO_list_t *list) | |
392 | { | |
14a5cf38 JH |
393 | if (list) { |
394 | if (--list->refcnt == 0) { | |
395 | if (list->array) { | |
396 | dTHX; | |
397 | IV i; | |
398 | for (i = 0; i < list->cur; i++) { | |
399 | if (list->array[i].arg) | |
400 | SvREFCNT_dec(list->array[i].arg); | |
401 | } | |
402 | Safefree(list->array); | |
403 | } | |
404 | Safefree(list); | |
405 | } | |
406 | } | |
fcf2db38 NIS |
407 | } |
408 | ||
409 | void | |
14a5cf38 JH |
410 | PerlIO_list_push(PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg) |
411 | { | |
412 | dTHX; | |
413 | PerlIO_pair_t *p; | |
414 | if (list->cur >= list->len) { | |
415 | list->len += 8; | |
416 | if (list->array) | |
417 | Renew(list->array, list->len, PerlIO_pair_t); | |
418 | else | |
419 | New('l', list->array, list->len, PerlIO_pair_t); | |
420 | } | |
421 | p = &(list->array[list->cur++]); | |
422 | p->funcs = funcs; | |
423 | if ((p->arg = arg)) { | |
424 | SvREFCNT_inc(arg); | |
425 | } | |
fcf2db38 NIS |
426 | } |
427 | ||
4a4a6116 | 428 | |
05d1247b | 429 | void |
acfe0abc | 430 | PerlIO_cleanup_layers(pTHX_ void *data) |
9a6404c5 | 431 | { |
fcf2db38 | 432 | #if 0 |
14a5cf38 JH |
433 | PerlIO_known_layers = Nullhv; |
434 | PerlIO_def_layerlist = Nullav; | |
fcf2db38 | 435 | #endif |
9a6404c5 DM |
436 | } |
437 | ||
438 | void | |
5f1a76d0 | 439 | PerlIO_cleanup() |
05d1247b | 440 | { |
14a5cf38 JH |
441 | dTHX; |
442 | PerlIO_cleantable(aTHX_ & _perlio); | |
6f9d8c32 NIS |
443 | } |
444 | ||
9e353e3b | 445 | void |
13621cfb NIS |
446 | PerlIO_destruct(pTHX) |
447 | { | |
14a5cf38 JH |
448 | PerlIO **table = &_perlio; |
449 | PerlIO *f; | |
450 | while ((f = *table)) { | |
451 | int i; | |
452 | table = (PerlIO **) (f++); | |
453 | for (i = 1; i < PERLIO_TABLE_SIZE; i++) { | |
454 | PerlIO *x = f; | |
455 | PerlIOl *l; | |
456 | while ((l = *x)) { | |
457 | if (l->tab->kind & PERLIO_K_DESTRUCT) { | |
458 | PerlIO_debug("Destruct popping %s\n", l->tab->name); | |
459 | PerlIO_flush(x); | |
460 | PerlIO_pop(aTHX_ x); | |
461 | } | |
462 | else { | |
463 | x = PerlIONext(x); | |
464 | } | |
465 | } | |
466 | f++; | |
467 | } | |
468 | } | |
13621cfb NIS |
469 | } |
470 | ||
471 | void | |
a999f61b | 472 | PerlIO_pop(pTHX_ PerlIO *f) |
760ac839 | 473 | { |
14a5cf38 JH |
474 | PerlIOl *l = *f; |
475 | if (l) { | |
476 | PerlIO_debug("PerlIO_pop f=%p %s\n", f, l->tab->name); | |
477 | if (l->tab->Popped) { | |
478 | /* | |
479 | * If popped returns non-zero do not free its layer structure | |
480 | * it has either done so itself, or it is shared and still in | |
71200d45 | 481 | * use |
14a5cf38 JH |
482 | */ |
483 | if ((*l->tab->Popped) (f) != 0) | |
484 | return; | |
485 | } | |
486 | *f = l->next;; | |
487 | PerlMemShared_free(l); | |
a8c08ecd | 488 | } |
6f9d8c32 NIS |
489 | } |
490 | ||
9e353e3b | 491 | /*--------------------------------------------------------------------------------------*/ |
14a5cf38 | 492 | /* |
71200d45 | 493 | * XS Interface for perl code |
14a5cf38 | 494 | */ |
9e353e3b | 495 | |
fcf2db38 | 496 | PerlIO_funcs * |
2edd7e44 | 497 | PerlIO_find_layer(pTHX_ const char *name, STRLEN len, int load) |
f3862f8b | 498 | { |
14a5cf38 JH |
499 | IV i; |
500 | if ((SSize_t) len <= 0) | |
501 | len = strlen(name); | |
502 | for (i = 0; i < PerlIO_known_layers->cur; i++) { | |
503 | PerlIO_funcs *f = PerlIO_known_layers->array[i].funcs; | |
504 | if (memEQ(f->name, name, len)) { | |
505 | PerlIO_debug("%.*s => %p\n", (int) len, name, f); | |
506 | return f; | |
507 | } | |
508 | } | |
509 | if (load && PL_subname && PerlIO_def_layerlist | |
510 | && PerlIO_def_layerlist->cur >= 2) { | |
511 | SV *pkgsv = newSVpvn("PerlIO", 6); | |
512 | SV *layer = newSVpvn(name, len); | |
513 | ENTER; | |
514 | /* | |
71200d45 | 515 | * The two SVs are magically freed by load_module |
14a5cf38 JH |
516 | */ |
517 | Perl_load_module(aTHX_ 0, pkgsv, Nullsv, layer, Nullsv); | |
518 | LEAVE; | |
519 | return PerlIO_find_layer(aTHX_ name, len, 0); | |
520 | } | |
521 | PerlIO_debug("Cannot find %.*s\n", (int) len, name); | |
522 | return NULL; | |
f3862f8b NIS |
523 | } |
524 | ||
2a1bc955 | 525 | #ifdef USE_ATTRIBUTES_FOR_PERLIO |
b13b2135 NIS |
526 | |
527 | static int | |
528 | perlio_mg_set(pTHX_ SV *sv, MAGIC *mg) | |
529 | { | |
14a5cf38 JH |
530 | if (SvROK(sv)) { |
531 | IO *io = GvIOn((GV *) SvRV(sv)); | |
532 | PerlIO *ifp = IoIFP(io); | |
533 | PerlIO *ofp = IoOFP(io); | |
534 | Perl_warn(aTHX_ "set %" SVf " %p %p %p", sv, io, ifp, ofp); | |
535 | } | |
536 | return 0; | |
b13b2135 NIS |
537 | } |
538 | ||
539 | static int | |
540 | perlio_mg_get(pTHX_ SV *sv, MAGIC *mg) | |
541 | { | |
14a5cf38 JH |
542 | if (SvROK(sv)) { |
543 | IO *io = GvIOn((GV *) SvRV(sv)); | |
544 | PerlIO *ifp = IoIFP(io); | |
545 | PerlIO *ofp = IoOFP(io); | |
546 | Perl_warn(aTHX_ "get %" SVf " %p %p %p", sv, io, ifp, ofp); | |
547 | } | |
548 | return 0; | |
b13b2135 NIS |
549 | } |
550 | ||
551 | static int | |
552 | perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg) | |
553 | { | |
14a5cf38 JH |
554 | Perl_warn(aTHX_ "clear %" SVf, sv); |
555 | return 0; | |
b13b2135 NIS |
556 | } |
557 | ||
558 | static int | |
559 | perlio_mg_free(pTHX_ SV *sv, MAGIC *mg) | |
560 | { | |
14a5cf38 JH |
561 | Perl_warn(aTHX_ "free %" SVf, sv); |
562 | return 0; | |
b13b2135 NIS |
563 | } |
564 | ||
565 | MGVTBL perlio_vtab = { | |
14a5cf38 JH |
566 | perlio_mg_get, |
567 | perlio_mg_set, | |
568 | NULL, /* len */ | |
569 | perlio_mg_clear, | |
570 | perlio_mg_free | |
b13b2135 NIS |
571 | }; |
572 | ||
573 | XS(XS_io_MODIFY_SCALAR_ATTRIBUTES) | |
574 | { | |
14a5cf38 JH |
575 | dXSARGS; |
576 | SV *sv = SvRV(ST(1)); | |
577 | AV *av = newAV(); | |
578 | MAGIC *mg; | |
579 | int count = 0; | |
580 | int i; | |
581 | sv_magic(sv, (SV *) av, PERL_MAGIC_ext, NULL, 0); | |
582 | SvRMAGICAL_off(sv); | |
583 | mg = mg_find(sv, PERL_MAGIC_ext); | |
584 | mg->mg_virtual = &perlio_vtab; | |
585 | mg_magical(sv); | |
586 | Perl_warn(aTHX_ "attrib %" SVf, sv); | |
587 | for (i = 2; i < items; i++) { | |
588 | STRLEN len; | |
589 | const char *name = SvPV(ST(i), len); | |
590 | SV *layer = PerlIO_find_layer(aTHX_ name, len, 1); | |
591 | if (layer) { | |
592 | av_push(av, SvREFCNT_inc(layer)); | |
593 | } | |
594 | else { | |
595 | ST(count) = ST(i); | |
596 | count++; | |
597 | } | |
598 | } | |
599 | SvREFCNT_dec(av); | |
600 | XSRETURN(count); | |
601 | } | |
602 | ||
603 | #endif /* USE_ATTIBUTES_FOR_PERLIO */ | |
2a1bc955 | 604 | |
e3f3bf95 NIS |
605 | SV * |
606 | PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab) | |
f3862f8b | 607 | { |
14a5cf38 JH |
608 | HV *stash = gv_stashpv("PerlIO::Layer", TRUE); |
609 | SV *sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))), stash); | |
610 | return sv; | |
e3f3bf95 NIS |
611 | } |
612 | ||
0c4f7ff0 NIS |
613 | XS(XS_PerlIO__Layer__find) |
614 | { | |
14a5cf38 JH |
615 | dXSARGS; |
616 | if (items < 2) | |
617 | Perl_croak(aTHX_ "Usage class->find(name[,load])"); | |
618 | else { | |
619 | STRLEN len = 0; | |
620 | char *name = SvPV(ST(1), len); | |
621 | bool load = (items > 2) ? SvTRUE(ST(2)) : 0; | |
622 | PerlIO_funcs *layer = PerlIO_find_layer(aTHX_ name, len, load); | |
623 | ST(0) = | |
624 | (layer) ? sv_2mortal(PerlIO_tab_sv(aTHX_ layer)) : | |
625 | &PL_sv_undef; | |
626 | XSRETURN(1); | |
627 | } | |
0c4f7ff0 NIS |
628 | } |
629 | ||
e3f3bf95 NIS |
630 | void |
631 | PerlIO_define_layer(pTHX_ PerlIO_funcs *tab) | |
632 | { | |
14a5cf38 JH |
633 | if (!PerlIO_known_layers) |
634 | PerlIO_known_layers = PerlIO_list_alloc(); | |
635 | PerlIO_list_push(PerlIO_known_layers, tab, Nullsv); | |
636 | PerlIO_debug("define %s %p\n", tab->name, tab); | |
f3862f8b NIS |
637 | } |
638 | ||
1141d9f8 | 639 | int |
fcf2db38 | 640 | PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names) |
1141d9f8 | 641 | { |
14a5cf38 JH |
642 | if (names) { |
643 | const char *s = names; | |
644 | while (*s) { | |
645 | while (isSPACE(*s) || *s == ':') | |
646 | s++; | |
647 | if (*s) { | |
648 | STRLEN llen = 0; | |
649 | const char *e = s; | |
650 | const char *as = Nullch; | |
651 | STRLEN alen = 0; | |
652 | if (!isIDFIRST(*s)) { | |
653 | /* | |
654 | * Message is consistent with how attribute lists are | |
655 | * passed. Even though this means "foo : : bar" is | |
71200d45 | 656 | * seen as an invalid separator character. |
14a5cf38 JH |
657 | */ |
658 | char q = ((*s == '\'') ? '"' : '\''); | |
659 | Perl_warn(aTHX_ | |
660 | "perlio: invalid separator character %c%c%c in layer specification list", | |
661 | q, *s, q); | |
662 | return -1; | |
663 | } | |
664 | do { | |
665 | e++; | |
666 | } while (isALNUM(*e)); | |
667 | llen = e - s; | |
668 | if (*e == '(') { | |
669 | int nesting = 1; | |
670 | as = ++e; | |
671 | while (nesting) { | |
672 | switch (*e++) { | |
673 | case ')': | |
674 | if (--nesting == 0) | |
675 | alen = (e - 1) - as; | |
676 | break; | |
677 | case '(': | |
678 | ++nesting; | |
679 | break; | |
680 | case '\\': | |
681 | /* | |
682 | * It's a nul terminated string, not allowed | |
683 | * to \ the terminating null. Anything other | |
71200d45 | 684 | * character is passed over. |
14a5cf38 JH |
685 | */ |
686 | if (*e++) { | |
687 | break; | |
688 | } | |
689 | /* | |
71200d45 | 690 | * Drop through |
14a5cf38 JH |
691 | */ |
692 | case '\0': | |
693 | e--; | |
694 | Perl_warn(aTHX_ | |
695 | "perlio: argument list not closed for layer \"%.*s\"", | |
696 | (int) (e - s), s); | |
697 | return -1; | |
698 | default: | |
699 | /* | |
71200d45 | 700 | * boring. |
14a5cf38 JH |
701 | */ |
702 | break; | |
703 | } | |
704 | } | |
705 | } | |
706 | if (e > s) { | |
707 | PerlIO_funcs *layer = | |
708 | PerlIO_find_layer(aTHX_ s, llen, 1); | |
709 | if (layer) { | |
710 | PerlIO_list_push(av, layer, | |
711 | (as) ? newSVpvn(as, | |
712 | alen) : | |
713 | &PL_sv_undef); | |
714 | } | |
715 | else { | |
716 | Perl_warn(aTHX_ "perlio: unknown layer \"%.*s\"", | |
717 | (int) llen, s); | |
718 | return -1; | |
719 | } | |
720 | } | |
721 | s = e; | |
722 | } | |
723 | } | |
724 | } | |
725 | return 0; | |
1141d9f8 NIS |
726 | } |
727 | ||
dfebf958 | 728 | void |
fcf2db38 | 729 | PerlIO_default_buffer(pTHX_ PerlIO_list_t *av) |
dfebf958 | 730 | { |
14a5cf38 JH |
731 | PerlIO_funcs *tab = &PerlIO_perlio; |
732 | if (O_BINARY != O_TEXT) { | |
733 | tab = &PerlIO_crlf; | |
734 | } | |
735 | else { | |
736 | if (PerlIO_stdio.Set_ptrcnt) { | |
737 | tab = &PerlIO_stdio; | |
738 | } | |
dfebf958 | 739 | } |
14a5cf38 JH |
740 | PerlIO_debug("Pushing %s\n", tab->name); |
741 | PerlIO_list_push(av, PerlIO_find_layer(aTHX_ tab->name, 0, 0), | |
742 | &PL_sv_undef); | |
dfebf958 NIS |
743 | } |
744 | ||
e3f3bf95 | 745 | SV * |
14a5cf38 | 746 | PerlIO_arg_fetch(PerlIO_list_t *av, IV n) |
e3f3bf95 | 747 | { |
14a5cf38 | 748 | return av->array[n].arg; |
e3f3bf95 NIS |
749 | } |
750 | ||
f3862f8b | 751 | PerlIO_funcs * |
14a5cf38 | 752 | PerlIO_layer_fetch(pTHX_ PerlIO_list_t *av, IV n, PerlIO_funcs *def) |
f3862f8b | 753 | { |
14a5cf38 JH |
754 | if (n >= 0 && n < av->cur) { |
755 | PerlIO_debug("Layer %" IVdf " is %s\n", n, | |
756 | av->array[n].funcs->name); | |
757 | return av->array[n].funcs; | |
758 | } | |
759 | if (!def) | |
760 | Perl_croak(aTHX_ "panic: PerlIO layer array corrupt"); | |
761 | return def; | |
e3f3bf95 NIS |
762 | } |
763 | ||
fcf2db38 | 764 | PerlIO_list_t * |
e3f3bf95 NIS |
765 | PerlIO_default_layers(pTHX) |
766 | { | |
14a5cf38 JH |
767 | if (!PerlIO_def_layerlist) { |
768 | const char *s = (PL_tainting) ? Nullch : PerlEnv_getenv("PERLIO"); | |
769 | PerlIO_funcs *osLayer = &PerlIO_unix; | |
770 | PerlIO_def_layerlist = PerlIO_list_alloc(); | |
771 | PerlIO_define_layer(aTHX_ & PerlIO_unix); | |
e1caacb4 | 772 | #if defined(WIN32) && !defined(UNDER_CE) |
14a5cf38 | 773 | PerlIO_define_layer(aTHX_ & PerlIO_win32); |
2f8118af | 774 | #if 0 |
14a5cf38 | 775 | osLayer = &PerlIO_win32; |
0c4128ad | 776 | #endif |
2f8118af | 777 | #endif |
14a5cf38 JH |
778 | PerlIO_define_layer(aTHX_ & PerlIO_raw); |
779 | PerlIO_define_layer(aTHX_ & PerlIO_perlio); | |
780 | PerlIO_define_layer(aTHX_ & PerlIO_stdio); | |
781 | PerlIO_define_layer(aTHX_ & PerlIO_crlf); | |
06da4f11 | 782 | #ifdef HAS_MMAP |
14a5cf38 | 783 | PerlIO_define_layer(aTHX_ & PerlIO_mmap); |
06da4f11 | 784 | #endif |
14a5cf38 JH |
785 | PerlIO_define_layer(aTHX_ & PerlIO_utf8); |
786 | PerlIO_define_layer(aTHX_ & PerlIO_byte); | |
787 | PerlIO_list_push(PerlIO_def_layerlist, | |
788 | PerlIO_find_layer(aTHX_ osLayer->name, 0, 0), | |
789 | &PL_sv_undef); | |
790 | if (s) { | |
791 | PerlIO_parse_layers(aTHX_ PerlIO_def_layerlist, s); | |
792 | } | |
793 | else { | |
794 | PerlIO_default_buffer(aTHX_ PerlIO_def_layerlist); | |
795 | } | |
1141d9f8 | 796 | } |
14a5cf38 JH |
797 | if (PerlIO_def_layerlist->cur < 2) { |
798 | PerlIO_default_buffer(aTHX_ PerlIO_def_layerlist); | |
f3862f8b | 799 | } |
14a5cf38 | 800 | return PerlIO_def_layerlist; |
e3f3bf95 NIS |
801 | } |
802 | ||
0c4f7ff0 NIS |
803 | void |
804 | Perl_boot_core_PerlIO(pTHX) | |
805 | { | |
806 | #ifdef USE_ATTRIBUTES_FOR_PERLIO | |
14a5cf38 JH |
807 | newXS("io::MODIFY_SCALAR_ATTRIBUTES", XS_io_MODIFY_SCALAR_ATTRIBUTES, |
808 | __FILE__); | |
0c4f7ff0 | 809 | #endif |
14a5cf38 | 810 | newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__); |
0c4f7ff0 | 811 | } |
e3f3bf95 NIS |
812 | |
813 | PerlIO_funcs * | |
814 | PerlIO_default_layer(pTHX_ I32 n) | |
815 | { | |
14a5cf38 JH |
816 | PerlIO_list_t *av = PerlIO_default_layers(aTHX); |
817 | if (n < 0) | |
818 | n += av->cur; | |
819 | return PerlIO_layer_fetch(aTHX_ av, n, &PerlIO_stdio); | |
f3862f8b NIS |
820 | } |
821 | ||
a999f61b NIS |
822 | #define PerlIO_default_top() PerlIO_default_layer(aTHX_ -1) |
823 | #define PerlIO_default_btm() PerlIO_default_layer(aTHX_ 0) | |
60382766 NIS |
824 | |
825 | void | |
1141d9f8 | 826 | PerlIO_stdstreams(pTHX) |
60382766 | 827 | { |
14a5cf38 JH |
828 | if (!_perlio) { |
829 | PerlIO_allocate(aTHX); | |
830 | PerlIO_fdopen(0, "Ir" PERLIO_STDTEXT); | |
831 | PerlIO_fdopen(1, "Iw" PERLIO_STDTEXT); | |
832 | PerlIO_fdopen(2, "Iw" PERLIO_STDTEXT); | |
833 | } | |
60382766 NIS |
834 | } |
835 | ||
836 | PerlIO * | |
14a5cf38 JH |
837 | PerlIO_push(pTHX_ PerlIO *f, PerlIO_funcs *tab, const char *mode, SV *arg) |
838 | { | |
839 | PerlIOl *l = NULL; | |
840 | l = PerlMemShared_calloc(tab->size, sizeof(char)); | |
841 | if (l) { | |
842 | Zero(l, tab->size, char); | |
843 | l->next = *f; | |
844 | l->tab = tab; | |
845 | *f = l; | |
846 | PerlIO_debug("PerlIO_push f=%p %s %s %p\n", f, tab->name, | |
847 | (mode) ? mode : "(Null)", arg); | |
848 | if ((*l->tab->Pushed) (f, mode, arg) != 0) { | |
849 | PerlIO_pop(aTHX_ f); | |
850 | return NULL; | |
851 | } | |
852 | } | |
853 | return f; | |
60382766 NIS |
854 | } |
855 | ||
dfebf958 | 856 | IV |
e3f3bf95 | 857 | PerlIOPop_pushed(PerlIO *f, const char *mode, SV *arg) |
4b803d04 | 858 | { |
14a5cf38 JH |
859 | dTHX; |
860 | PerlIO_pop(aTHX_ f); | |
861 | if (*f) { | |
862 | PerlIO_flush(f); | |
863 | PerlIO_pop(aTHX_ f); | |
864 | return 0; | |
865 | } | |
866 | return -1; | |
4b803d04 NIS |
867 | } |
868 | ||
869 | IV | |
e3f3bf95 | 870 | PerlIORaw_pushed(PerlIO *f, const char *mode, SV *arg) |
dfebf958 | 871 | { |
14a5cf38 | 872 | /* |
71200d45 | 873 | * Remove the dummy layer |
14a5cf38 JH |
874 | */ |
875 | dTHX; | |
876 | PerlIO_pop(aTHX_ f); | |
877 | /* | |
71200d45 | 878 | * Pop back to bottom layer |
14a5cf38 JH |
879 | */ |
880 | if (f && *f) { | |
881 | PerlIO_flush(f); | |
882 | while (!(PerlIOBase(f)->tab->kind & PERLIO_K_RAW)) { | |
883 | if (*PerlIONext(f)) { | |
884 | PerlIO_pop(aTHX_ f); | |
885 | } | |
886 | else { | |
887 | /* | |
71200d45 | 888 | * Nothing bellow - push unix on top then remove it |
14a5cf38 JH |
889 | */ |
890 | if (PerlIO_push(aTHX_ f, PerlIO_default_btm(), mode, arg)) { | |
891 | PerlIO_pop(aTHX_ PerlIONext(f)); | |
892 | } | |
893 | break; | |
894 | } | |
895 | } | |
896 | PerlIO_debug(":raw f=%p :%s\n", f, PerlIOBase(f)->tab->name); | |
897 | return 0; | |
898 | } | |
899 | return -1; | |
dfebf958 NIS |
900 | } |
901 | ||
ac27b0f5 | 902 | int |
14a5cf38 JH |
903 | PerlIO_apply_layera(pTHX_ PerlIO *f, const char *mode, |
904 | PerlIO_list_t *layers, IV n) | |
905 | { | |
906 | IV max = layers->cur; | |
907 | int code = 0; | |
908 | while (n < max) { | |
909 | PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n, NULL); | |
910 | if (tab) { | |
911 | if (!PerlIO_push(aTHX_ f, tab, mode, PerlIOArg)) { | |
912 | code = -1; | |
913 | break; | |
914 | } | |
915 | } | |
916 | n++; | |
917 | } | |
918 | return code; | |
e3f3bf95 NIS |
919 | } |
920 | ||
921 | int | |
ac27b0f5 NIS |
922 | PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names) |
923 | { | |
14a5cf38 JH |
924 | int code = 0; |
925 | if (names) { | |
926 | PerlIO_list_t *layers = PerlIO_list_alloc(); | |
927 | code = PerlIO_parse_layers(aTHX_ layers, names); | |
928 | if (code == 0) { | |
929 | code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0); | |
930 | } | |
931 | PerlIO_list_free(layers); | |
ac27b0f5 | 932 | } |
14a5cf38 | 933 | return code; |
ac27b0f5 NIS |
934 | } |
935 | ||
f3862f8b | 936 | |
60382766 | 937 | /*--------------------------------------------------------------------------------------*/ |
14a5cf38 | 938 | /* |
71200d45 | 939 | * Given the abstraction above the public API functions |
14a5cf38 | 940 | */ |
60382766 NIS |
941 | |
942 | int | |
f5b9d040 | 943 | PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names) |
76ced9ad | 944 | { |
14a5cf38 JH |
945 | PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n", |
946 | f, PerlIOBase(f)->tab->name, iotype, mode, | |
947 | (names) ? names : "(Null)"); | |
89aec743 | 948 | PerlIO_flush(f); |
14a5cf38 JH |
949 | if (!names && (O_TEXT != O_BINARY && (mode & O_BINARY))) { |
950 | PerlIO *top = f; | |
951 | while (*top) { | |
952 | if (PerlIOBase(top)->tab == &PerlIO_crlf) { | |
14a5cf38 JH |
953 | PerlIOBase(top)->flags &= ~PERLIO_F_CRLF; |
954 | break; | |
955 | } | |
956 | top = PerlIONext(top); | |
89aec743 | 957 | PerlIO_flush(top); |
14a5cf38 JH |
958 | } |
959 | } | |
960 | return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE; | |
f5b9d040 NIS |
961 | } |
962 | ||
963 | #undef PerlIO__close | |
964 | int | |
965 | PerlIO__close(PerlIO *f) | |
966 | { | |
14a5cf38 JH |
967 | if (f && *f) |
968 | return (*PerlIOBase(f)->tab->Close) (f); | |
969 | else { | |
970 | SETERRNO(EBADF, SS$_IVCHAN); | |
971 | return -1; | |
972 | } | |
76ced9ad NIS |
973 | } |
974 | ||
5f1a76d0 NIS |
975 | #undef PerlIO_fdupopen |
976 | PerlIO * | |
8cf8f3d1 | 977 | PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param) |
5f1a76d0 | 978 | { |
14a5cf38 | 979 | if (f && *f) { |
8cf8f3d1 | 980 | PerlIO_funcs *tab = PerlIOBase(f)->tab; |
a8fc9800 NIS |
981 | PerlIO *new; |
982 | PerlIO_debug("fdupopen f=%p param=%p\n",f,param); | |
983 | new = (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX),f,param); | |
14a5cf38 JH |
984 | return new; |
985 | } | |
986 | else { | |
987 | SETERRNO(EBADF, SS$_IVCHAN); | |
988 | return NULL; | |
989 | } | |
5f1a76d0 | 990 | } |
f5b9d040 | 991 | |
b931b1d9 NIS |
992 | #undef PerlIO_close |
993 | int | |
994 | PerlIO_close(PerlIO *f) | |
995 | { | |
14a5cf38 JH |
996 | dTHX; |
997 | int code = -1; | |
998 | if (f && *f) { | |
999 | code = (*PerlIOBase(f)->tab->Close) (f); | |
1000 | while (*f) { | |
1001 | PerlIO_pop(aTHX_ f); | |
1002 | } | |
f6c77cf1 | 1003 | } |
14a5cf38 | 1004 | return code; |
b931b1d9 NIS |
1005 | } |
1006 | ||
1007 | #undef PerlIO_fileno | |
1008 | int | |
1009 | PerlIO_fileno(PerlIO *f) | |
1010 | { | |
14a5cf38 JH |
1011 | if (f && *f) |
1012 | return (*PerlIOBase(f)->tab->Fileno) (f); | |
1013 | else { | |
1014 | SETERRNO(EBADF, SS$_IVCHAN); | |
1015 | return -1; | |
1016 | } | |
b931b1d9 NIS |
1017 | } |
1018 | ||
1141d9f8 NIS |
1019 | static const char * |
1020 | PerlIO_context_layers(pTHX_ const char *mode) | |
1021 | { | |
14a5cf38 JH |
1022 | const char *type = NULL; |
1023 | /* | |
71200d45 | 1024 | * Need to supply default layer info from open.pm |
14a5cf38 JH |
1025 | */ |
1026 | if (PL_curcop) { | |
1027 | SV *layers = PL_curcop->cop_io; | |
1028 | if (layers) { | |
1029 | STRLEN len; | |
1030 | type = SvPV(layers, len); | |
1031 | if (type && mode[0] != 'r') { | |
1032 | /* | |
71200d45 | 1033 | * Skip to write part |
14a5cf38 JH |
1034 | */ |
1035 | const char *s = strchr(type, 0); | |
1036 | if (s && (s - type) < len) { | |
1037 | type = s + 1; | |
1038 | } | |
1039 | } | |
1040 | } | |
1041 | } | |
1042 | return type; | |
1141d9f8 NIS |
1043 | } |
1044 | ||
fcf2db38 | 1045 | static PerlIO_funcs * |
2edd7e44 NIS |
1046 | PerlIO_layer_from_ref(pTHX_ SV *sv) |
1047 | { | |
14a5cf38 | 1048 | /* |
71200d45 | 1049 | * For any scalar type load the handler which is bundled with perl |
14a5cf38 JH |
1050 | */ |
1051 | if (SvTYPE(sv) < SVt_PVAV) | |
1052 | return PerlIO_find_layer(aTHX_ "Scalar", 6, 1); | |
1053 | ||
1054 | /* | |
71200d45 | 1055 | * For other types allow if layer is known but don't try and load it |
14a5cf38 JH |
1056 | */ |
1057 | switch (SvTYPE(sv)) { | |
1058 | case SVt_PVAV: | |
1059 | return PerlIO_find_layer(aTHX_ "Array", 5, 0); | |
1060 | case SVt_PVHV: | |
1061 | return PerlIO_find_layer(aTHX_ "Hash", 4, 0); | |
1062 | case SVt_PVCV: | |
1063 | return PerlIO_find_layer(aTHX_ "Code", 4, 0); | |
1064 | case SVt_PVGV: | |
1065 | return PerlIO_find_layer(aTHX_ "Glob", 4, 0); | |
1066 | } | |
1067 | return NULL; | |
2edd7e44 NIS |
1068 | } |
1069 | ||
fcf2db38 | 1070 | PerlIO_list_t * |
14a5cf38 JH |
1071 | PerlIO_resolve_layers(pTHX_ const char *layers, |
1072 | const char *mode, int narg, SV **args) | |
1073 | { | |
1074 | PerlIO_list_t *def = PerlIO_default_layers(aTHX); | |
1075 | int incdef = 1; | |
1076 | if (!_perlio) | |
1077 | PerlIO_stdstreams(aTHX); | |
1078 | if (narg) { | |
1079 | SV *arg = *args; | |
1080 | /* | |
71200d45 NIS |
1081 | * If it is a reference but not an object see if we have a handler |
1082 | * for it | |
14a5cf38 JH |
1083 | */ |
1084 | if (SvROK(arg) && !sv_isobject(arg)) { | |
1085 | PerlIO_funcs *handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg)); | |
1086 | if (handler) { | |
1087 | def = PerlIO_list_alloc(); | |
1088 | PerlIO_list_push(def, handler, &PL_sv_undef); | |
1089 | incdef = 0; | |
1090 | } | |
1091 | /* | |
71200d45 | 1092 | * Don't fail if handler cannot be found :Via(...) etc. may do |
14a5cf38 | 1093 | * something sensible else we will just stringfy and open |
71200d45 | 1094 | * resulting string. |
14a5cf38 JH |
1095 | */ |
1096 | } | |
1097 | } | |
1098 | if (!layers) | |
1099 | layers = PerlIO_context_layers(aTHX_ mode); | |
1100 | if (layers && *layers) { | |
1101 | PerlIO_list_t *av; | |
1102 | if (incdef) { | |
1103 | IV i = def->cur; | |
1104 | av = PerlIO_list_alloc(); | |
1105 | for (i = 0; i < def->cur; i++) { | |
1106 | PerlIO_list_push(av, def->array[i].funcs, | |
1107 | def->array[i].arg); | |
1108 | } | |
1109 | } | |
1110 | else { | |
1111 | av = def; | |
1112 | } | |
1113 | PerlIO_parse_layers(aTHX_ av, layers); | |
1114 | return av; | |
1115 | } | |
1116 | else { | |
1117 | if (incdef) | |
1118 | def->refcnt++; | |
1119 | return def; | |
1120 | } | |
ee518936 NIS |
1121 | } |
1122 | ||
1123 | PerlIO * | |
14a5cf38 JH |
1124 | PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd, |
1125 | int imode, int perm, PerlIO *f, int narg, SV **args) | |
1126 | { | |
1127 | if (!f && narg == 1 && *args == &PL_sv_undef) { | |
1128 | if ((f = PerlIO_tmpfile())) { | |
1129 | if (!layers) | |
1130 | layers = PerlIO_context_layers(aTHX_ mode); | |
1131 | if (layers && *layers) | |
1132 | PerlIO_apply_layers(aTHX_ f, mode, layers); | |
1133 | } | |
1134 | } | |
1135 | else { | |
1136 | PerlIO_list_t *layera = NULL; | |
1137 | IV n; | |
1138 | PerlIO_funcs *tab = NULL; | |
1139 | if (f && *f) { | |
1140 | /* | |
71200d45 NIS |
1141 | * This is "reopen" - it is not tested as perl does not use it |
1142 | * yet | |
14a5cf38 JH |
1143 | */ |
1144 | PerlIOl *l = *f; | |
1145 | layera = PerlIO_list_alloc(); | |
1146 | while (l) { | |
1147 | SV *arg = | |
1148 | (l->tab->Getarg) ? (*l->tab-> | |
1149 | Getarg) (&l) : &PL_sv_undef; | |
1150 | PerlIO_list_push(layera, l->tab, arg); | |
1151 | l = *PerlIONext(&l); | |
1152 | } | |
1153 | } | |
1154 | else { | |
1155 | layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args); | |
1156 | } | |
1157 | /* | |
71200d45 | 1158 | * Start at "top" of layer stack |
14a5cf38 JH |
1159 | */ |
1160 | n = layera->cur - 1; | |
1161 | while (n >= 0) { | |
1162 | PerlIO_funcs *t = PerlIO_layer_fetch(aTHX_ layera, n, NULL); | |
1163 | if (t && t->Open) { | |
1164 | tab = t; | |
1165 | break; | |
1166 | } | |
1167 | n--; | |
1168 | } | |
1169 | if (tab) { | |
1170 | /* | |
71200d45 | 1171 | * Found that layer 'n' can do opens - call it |
14a5cf38 JH |
1172 | */ |
1173 | PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n", | |
1174 | tab->name, layers, mode, fd, imode, perm, f, narg, | |
1175 | args); | |
1176 | f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm, | |
1177 | f, narg, args); | |
1178 | if (f) { | |
1179 | if (n + 1 < layera->cur) { | |
1180 | /* | |
1181 | * More layers above the one that we used to open - | |
71200d45 | 1182 | * apply them now |
14a5cf38 JH |
1183 | */ |
1184 | if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1) | |
1185 | != 0) { | |
1186 | f = NULL; | |
1187 | } | |
1188 | } | |
1189 | } | |
1190 | } | |
1191 | PerlIO_list_free(layera); | |
1192 | } | |
1193 | return f; | |
ee518936 | 1194 | } |
b931b1d9 NIS |
1195 | |
1196 | ||
9e353e3b NIS |
1197 | #undef PerlIO_fdopen |
1198 | PerlIO * | |
1199 | PerlIO_fdopen(int fd, const char *mode) | |
1200 | { | |
14a5cf38 JH |
1201 | dTHX; |
1202 | return PerlIO_openn(aTHX_ Nullch, mode, fd, 0, 0, NULL, 0, NULL); | |
9e353e3b NIS |
1203 | } |
1204 | ||
6f9d8c32 NIS |
1205 | #undef PerlIO_open |
1206 | PerlIO * | |
1207 | PerlIO_open(const char *path, const char *mode) | |
1208 | { | |
14a5cf38 JH |
1209 | dTHX; |
1210 | SV *name = sv_2mortal(newSVpvn(path, strlen(path))); | |
1211 | return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, NULL, 1, &name); | |
6f9d8c32 NIS |
1212 | } |
1213 | ||
9e353e3b NIS |
1214 | #undef PerlIO_reopen |
1215 | PerlIO * | |
1216 | PerlIO_reopen(const char *path, const char *mode, PerlIO *f) | |
6f9d8c32 | 1217 | { |
14a5cf38 JH |
1218 | dTHX; |
1219 | SV *name = sv_2mortal(newSVpvn(path, strlen(path))); | |
1220 | return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, f, 1, &name); | |
760ac839 LW |
1221 | } |
1222 | ||
9e353e3b NIS |
1223 | #undef PerlIO_read |
1224 | SSize_t | |
1225 | PerlIO_read(PerlIO *f, void *vbuf, Size_t count) | |
6f9d8c32 | 1226 | { |
14a5cf38 JH |
1227 | if (f && *f) |
1228 | return (*PerlIOBase(f)->tab->Read) (f, vbuf, count); | |
1229 | else { | |
1230 | SETERRNO(EBADF, SS$_IVCHAN); | |
1231 | return -1; | |
1232 | } | |
760ac839 LW |
1233 | } |
1234 | ||
313ca112 NIS |
1235 | #undef PerlIO_unread |
1236 | SSize_t | |
1237 | PerlIO_unread(PerlIO *f, const void *vbuf, Size_t count) | |
760ac839 | 1238 | { |
14a5cf38 JH |
1239 | if (f && *f) |
1240 | return (*PerlIOBase(f)->tab->Unread) (f, vbuf, count); | |
1241 | else { | |
1242 | SETERRNO(EBADF, SS$_IVCHAN); | |
1243 | return -1; | |
1244 | } | |
760ac839 LW |
1245 | } |
1246 | ||
9e353e3b NIS |
1247 | #undef PerlIO_write |
1248 | SSize_t | |
1249 | PerlIO_write(PerlIO *f, const void *vbuf, Size_t count) | |
760ac839 | 1250 | { |
14a5cf38 JH |
1251 | if (f && *f) |
1252 | return (*PerlIOBase(f)->tab->Write) (f, vbuf, count); | |
1253 | else { | |
1254 | SETERRNO(EBADF, SS$_IVCHAN); | |
1255 | return -1; | |
1256 | } | |
760ac839 LW |
1257 | } |
1258 | ||
9e353e3b | 1259 | #undef PerlIO_seek |
6f9d8c32 | 1260 | int |
9e353e3b | 1261 | PerlIO_seek(PerlIO *f, Off_t offset, int whence) |
760ac839 | 1262 | { |
14a5cf38 JH |
1263 | if (f && *f) |
1264 | return (*PerlIOBase(f)->tab->Seek) (f, offset, whence); | |
1265 | else { | |
1266 | SETERRNO(EBADF, SS$_IVCHAN); | |
1267 | return -1; | |
1268 | } | |
760ac839 LW |
1269 | } |
1270 | ||
9e353e3b NIS |
1271 | #undef PerlIO_tell |
1272 | Off_t | |
1273 | PerlIO_tell(PerlIO *f) | |
760ac839 | 1274 | { |
14a5cf38 JH |
1275 | if (f && *f) |
1276 | return (*PerlIOBase(f)->tab->Tell) (f); | |
1277 | else { | |
1278 | SETERRNO(EBADF, SS$_IVCHAN); | |
1279 | return -1; | |
1280 | } | |
760ac839 LW |
1281 | } |
1282 | ||
9e353e3b | 1283 | #undef PerlIO_flush |
6f9d8c32 | 1284 | int |
9e353e3b | 1285 | PerlIO_flush(PerlIO *f) |
760ac839 | 1286 | { |
14a5cf38 JH |
1287 | if (f) { |
1288 | if (*f) { | |
1289 | PerlIO_funcs *tab = PerlIOBase(f)->tab; | |
1290 | if (tab && tab->Flush) { | |
1291 | return (*tab->Flush) (f); | |
1292 | } | |
1293 | else { | |
1294 | PerlIO_debug("Cannot flush f=%p :%s\n", f, tab->name); | |
1295 | SETERRNO(EBADF, SS$_IVCHAN); | |
1296 | return -1; | |
1297 | } | |
1298 | } | |
1299 | else { | |
1300 | PerlIO_debug("Cannot flush f=%p\n", f); | |
1301 | SETERRNO(EBADF, SS$_IVCHAN); | |
1302 | return -1; | |
1303 | } | |
1304 | } | |
1305 | else { | |
1306 | /* | |
1307 | * Is it good API design to do flush-all on NULL, a potentially | |
1308 | * errorneous input? Maybe some magical value (PerlIO* | |
1309 | * PERLIO_FLUSH_ALL = (PerlIO*)-1;)? Yes, stdio does similar | |
1310 | * things on fflush(NULL), but should we be bound by their design | |
71200d45 | 1311 | * decisions? --jhi |
14a5cf38 JH |
1312 | */ |
1313 | PerlIO **table = &_perlio; | |
1314 | int code = 0; | |
1315 | while ((f = *table)) { | |
1316 | int i; | |
1317 | table = (PerlIO **) (f++); | |
1318 | for (i = 1; i < PERLIO_TABLE_SIZE; i++) { | |
1319 | if (*f && PerlIO_flush(f) != 0) | |
1320 | code = -1; | |
1321 | f++; | |
1322 | } | |
1323 | } | |
1324 | return code; | |
1325 | } | |
760ac839 LW |
1326 | } |
1327 | ||
a9c883f6 NIS |
1328 | void |
1329 | PerlIOBase_flush_linebuf() | |
1330 | { | |
14a5cf38 JH |
1331 | PerlIO **table = &_perlio; |
1332 | PerlIO *f; | |
1333 | while ((f = *table)) { | |
1334 | int i; | |
1335 | table = (PerlIO **) (f++); | |
1336 | for (i = 1; i < PERLIO_TABLE_SIZE; i++) { | |
1337 | if (*f | |
1338 | && (PerlIOBase(f)-> | |
1339 | flags & (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE)) | |
1340 | == (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE)) | |
1341 | PerlIO_flush(f); | |
1342 | f++; | |
1343 | } | |
a9c883f6 | 1344 | } |
a9c883f6 NIS |
1345 | } |
1346 | ||
06da4f11 NIS |
1347 | #undef PerlIO_fill |
1348 | int | |
1349 | PerlIO_fill(PerlIO *f) | |
1350 | { | |
14a5cf38 JH |
1351 | if (f && *f) |
1352 | return (*PerlIOBase(f)->tab->Fill) (f); | |
1353 | else { | |
1354 | SETERRNO(EBADF, SS$_IVCHAN); | |
1355 | return -1; | |
1356 | } | |
06da4f11 NIS |
1357 | } |
1358 | ||
f3862f8b NIS |
1359 | #undef PerlIO_isutf8 |
1360 | int | |
1361 | PerlIO_isutf8(PerlIO *f) | |
1362 | { | |
14a5cf38 JH |
1363 | if (f && *f) |
1364 | return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0; | |
1365 | else { | |
1366 | SETERRNO(EBADF, SS$_IVCHAN); | |
1367 | return -1; | |
1368 | } | |
f3862f8b NIS |
1369 | } |
1370 | ||
9e353e3b | 1371 | #undef PerlIO_eof |
6f9d8c32 | 1372 | int |
9e353e3b | 1373 | PerlIO_eof(PerlIO *f) |
760ac839 | 1374 | { |
14a5cf38 JH |
1375 | if (f && *f) |
1376 | return (*PerlIOBase(f)->tab->Eof) (f); | |
1377 | else { | |
1378 | SETERRNO(EBADF, SS$_IVCHAN); | |
1379 | return -1; | |
1380 | } | |
9e353e3b NIS |
1381 | } |
1382 | ||
1383 | #undef PerlIO_error | |
1384 | int | |
1385 | PerlIO_error(PerlIO *f) | |
1386 | { | |
14a5cf38 JH |
1387 | if (f && *f) |
1388 | return (*PerlIOBase(f)->tab->Error) (f); | |
1389 | else { | |
1390 | SETERRNO(EBADF, SS$_IVCHAN); | |
1391 | return -1; | |
1392 | } | |
9e353e3b NIS |
1393 | } |
1394 | ||
1395 | #undef PerlIO_clearerr | |
1396 | void | |
1397 | PerlIO_clearerr(PerlIO *f) | |
1398 | { | |
14a5cf38 JH |
1399 | if (f && *f) |
1400 | (*PerlIOBase(f)->tab->Clearerr) (f); | |
1401 | else | |
1402 | SETERRNO(EBADF, SS$_IVCHAN); | |
9e353e3b NIS |
1403 | } |
1404 | ||
1405 | #undef PerlIO_setlinebuf | |
1406 | void | |
1407 | PerlIO_setlinebuf(PerlIO *f) | |
1408 | { | |
14a5cf38 JH |
1409 | if (f && *f) |
1410 | (*PerlIOBase(f)->tab->Setlinebuf) (f); | |
1411 | else | |
1412 | SETERRNO(EBADF, SS$_IVCHAN); | |
9e353e3b NIS |
1413 | } |
1414 | ||
1415 | #undef PerlIO_has_base | |
1416 | int | |
1417 | PerlIO_has_base(PerlIO *f) | |
1418 | { | |
14a5cf38 JH |
1419 | if (f && *f) { |
1420 | return (PerlIOBase(f)->tab->Get_base != NULL); | |
1421 | } | |
1422 | return 0; | |
760ac839 LW |
1423 | } |
1424 | ||
9e353e3b NIS |
1425 | #undef PerlIO_fast_gets |
1426 | int | |
1427 | PerlIO_fast_gets(PerlIO *f) | |
760ac839 | 1428 | { |
14a5cf38 JH |
1429 | if (f && *f && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) { |
1430 | PerlIO_funcs *tab = PerlIOBase(f)->tab; | |
1431 | return (tab->Set_ptrcnt != NULL); | |
1432 | } | |
1433 | return 0; | |
9e353e3b NIS |
1434 | } |
1435 | ||
1436 | #undef PerlIO_has_cntptr | |
1437 | int | |
1438 | PerlIO_has_cntptr(PerlIO *f) | |
1439 | { | |
14a5cf38 JH |
1440 | if (f && *f) { |
1441 | PerlIO_funcs *tab = PerlIOBase(f)->tab; | |
1442 | return (tab->Get_ptr != NULL && tab->Get_cnt != NULL); | |
1443 | } | |
1444 | return 0; | |
9e353e3b NIS |
1445 | } |
1446 | ||
1447 | #undef PerlIO_canset_cnt | |
1448 | int | |
1449 | PerlIO_canset_cnt(PerlIO *f) | |
1450 | { | |
14a5cf38 JH |
1451 | if (f && *f) { |
1452 | PerlIOl *l = PerlIOBase(f); | |
1453 | return (l->tab->Set_ptrcnt != NULL); | |
1454 | } | |
1455 | return 0; | |
760ac839 LW |
1456 | } |
1457 | ||
1458 | #undef PerlIO_get_base | |
888911fc | 1459 | STDCHAR * |
a20bf0c3 | 1460 | PerlIO_get_base(PerlIO *f) |
760ac839 | 1461 | { |
14a5cf38 JH |
1462 | if (f && *f) |
1463 | return (*PerlIOBase(f)->tab->Get_base) (f); | |
1464 | return NULL; | |
9e353e3b NIS |
1465 | } |
1466 | ||
1467 | #undef PerlIO_get_bufsiz | |
1468 | int | |
1469 | PerlIO_get_bufsiz(PerlIO *f) | |
1470 | { | |
14a5cf38 JH |
1471 | if (f && *f) |
1472 | return (*PerlIOBase(f)->tab->Get_bufsiz) (f); | |
1473 | return 0; | |
9e353e3b NIS |
1474 | } |
1475 | ||
1476 | #undef PerlIO_get_ptr | |
1477 | STDCHAR * | |
1478 | PerlIO_get_ptr(PerlIO *f) | |
1479 | { | |
14a5cf38 JH |
1480 | PerlIO_funcs *tab = PerlIOBase(f)->tab; |
1481 | if (tab->Get_ptr == NULL) | |
1482 | return NULL; | |
1483 | return (*tab->Get_ptr) (f); | |
9e353e3b NIS |
1484 | } |
1485 | ||
1486 | #undef PerlIO_get_cnt | |
05d1247b | 1487 | int |
9e353e3b NIS |
1488 | PerlIO_get_cnt(PerlIO *f) |
1489 | { | |
14a5cf38 JH |
1490 | PerlIO_funcs *tab = PerlIOBase(f)->tab; |
1491 | if (tab->Get_cnt == NULL) | |
1492 | return 0; | |
1493 | return (*tab->Get_cnt) (f); | |
9e353e3b NIS |
1494 | } |
1495 | ||
1496 | #undef PerlIO_set_cnt | |
1497 | void | |
14a5cf38 | 1498 | PerlIO_set_cnt(PerlIO *f, int cnt) |
9e353e3b | 1499 | { |
14a5cf38 | 1500 | (*PerlIOBase(f)->tab->Set_ptrcnt) (f, NULL, cnt); |
9e353e3b NIS |
1501 | } |
1502 | ||
1503 | #undef PerlIO_set_ptrcnt | |
1504 | void | |
14a5cf38 | 1505 | PerlIO_set_ptrcnt(PerlIO *f, STDCHAR * ptr, int cnt) |
9e353e3b | 1506 | { |
14a5cf38 JH |
1507 | PerlIO_funcs *tab = PerlIOBase(f)->tab; |
1508 | if (tab->Set_ptrcnt == NULL) { | |
1509 | dTHX; | |
1510 | Perl_croak(aTHX_ "PerlIO buffer snooping abuse"); | |
1511 | } | |
1512 | (*PerlIOBase(f)->tab->Set_ptrcnt) (f, ptr, cnt); | |
9e353e3b NIS |
1513 | } |
1514 | ||
1515 | /*--------------------------------------------------------------------------------------*/ | |
14a5cf38 | 1516 | /* |
71200d45 | 1517 | * utf8 and raw dummy layers |
14a5cf38 | 1518 | */ |
dfebf958 | 1519 | |
26fb694e | 1520 | IV |
e3f3bf95 | 1521 | PerlIOUtf8_pushed(PerlIO *f, const char *mode, SV *arg) |
26fb694e | 1522 | { |
14a5cf38 JH |
1523 | if (PerlIONext(f)) { |
1524 | dTHX; | |
1525 | PerlIO_funcs *tab = PerlIOBase(f)->tab; | |
1526 | PerlIO_pop(aTHX_ f); | |
1527 | if (tab->kind & PERLIO_K_UTF8) | |
1528 | PerlIOBase(f)->flags |= PERLIO_F_UTF8; | |
1529 | else | |
1530 | PerlIOBase(f)->flags &= ~PERLIO_F_UTF8; | |
1531 | return 0; | |
1532 | } | |
1533 | return -1; | |
26fb694e NIS |
1534 | } |
1535 | ||
dfebf958 | 1536 | PerlIO_funcs PerlIO_utf8 = { |
14a5cf38 JH |
1537 | "utf8", |
1538 | sizeof(PerlIOl), | |
1539 | PERLIO_K_DUMMY | PERLIO_F_UTF8, | |
1540 | PerlIOUtf8_pushed, | |
1541 | NULL, | |
1542 | NULL, | |
1543 | NULL, | |
1544 | NULL, | |
1545 | NULL, | |
1546 | NULL, | |
1547 | NULL, | |
1548 | NULL, | |
1549 | NULL, | |
1550 | NULL, | |
1551 | NULL, /* flush */ | |
1552 | NULL, /* fill */ | |
1553 | NULL, | |
1554 | NULL, | |
1555 | NULL, | |
1556 | NULL, | |
1557 | NULL, /* get_base */ | |
1558 | NULL, /* get_bufsiz */ | |
1559 | NULL, /* get_ptr */ | |
1560 | NULL, /* get_cnt */ | |
1561 | NULL, /* set_ptrcnt */ | |
26fb694e NIS |
1562 | }; |
1563 | ||
1564 | PerlIO_funcs PerlIO_byte = { | |
14a5cf38 JH |
1565 | "bytes", |
1566 | sizeof(PerlIOl), | |
1567 | PERLIO_K_DUMMY, | |
1568 | PerlIOUtf8_pushed, | |
1569 | NULL, | |
1570 | NULL, | |
1571 | NULL, | |
1572 | NULL, | |
1573 | NULL, | |
1574 | NULL, | |
1575 | NULL, | |
1576 | NULL, | |
1577 | NULL, | |
1578 | NULL, | |
1579 | NULL, /* flush */ | |
1580 | NULL, /* fill */ | |
1581 | NULL, | |
1582 | NULL, | |
1583 | NULL, | |
1584 | NULL, | |
1585 | NULL, /* get_base */ | |
1586 | NULL, /* get_bufsiz */ | |
1587 | NULL, /* get_ptr */ | |
1588 | NULL, /* get_cnt */ | |
1589 | NULL, /* set_ptrcnt */ | |
dfebf958 NIS |
1590 | }; |
1591 | ||
1592 | PerlIO * | |
14a5cf38 JH |
1593 | PerlIORaw_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers, |
1594 | IV n, const char *mode, int fd, int imode, int perm, | |
1595 | PerlIO *old, int narg, SV **args) | |
dfebf958 | 1596 | { |
14a5cf38 JH |
1597 | PerlIO_funcs *tab = PerlIO_default_btm(); |
1598 | return (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm, | |
1599 | old, narg, args); | |
dfebf958 NIS |
1600 | } |
1601 | ||
1602 | PerlIO_funcs PerlIO_raw = { | |
14a5cf38 JH |
1603 | "raw", |
1604 | sizeof(PerlIOl), | |
1605 | PERLIO_K_DUMMY, | |
1606 | PerlIORaw_pushed, | |
1607 | PerlIOBase_popped, | |
1608 | PerlIORaw_open, | |
1609 | NULL, | |
1610 | NULL, | |
1611 | NULL, | |
1612 | NULL, | |
1613 | NULL, | |
1614 | NULL, | |
1615 | NULL, | |
1616 | NULL, | |
1617 | NULL, /* flush */ | |
1618 | NULL, /* fill */ | |
1619 | NULL, | |
1620 | NULL, | |
1621 | NULL, | |
1622 | NULL, | |
1623 | NULL, /* get_base */ | |
1624 | NULL, /* get_bufsiz */ | |
1625 | NULL, /* get_ptr */ | |
1626 | NULL, /* get_cnt */ | |
1627 | NULL, /* set_ptrcnt */ | |
dfebf958 NIS |
1628 | }; |
1629 | /*--------------------------------------------------------------------------------------*/ | |
1630 | /*--------------------------------------------------------------------------------------*/ | |
14a5cf38 | 1631 | /* |
71200d45 | 1632 | * "Methods" of the "base class" |
14a5cf38 | 1633 | */ |
9e353e3b NIS |
1634 | |
1635 | IV | |
1636 | PerlIOBase_fileno(PerlIO *f) | |
1637 | { | |
14a5cf38 | 1638 | return PerlIO_fileno(PerlIONext(f)); |
9e353e3b NIS |
1639 | } |
1640 | ||
f5b9d040 | 1641 | char * |
14a5cf38 JH |
1642 | PerlIO_modestr(PerlIO *f, char *buf) |
1643 | { | |
1644 | char *s = buf; | |
1645 | IV flags = PerlIOBase(f)->flags; | |
1646 | if (flags & PERLIO_F_APPEND) { | |
1647 | *s++ = 'a'; | |
1648 | if (flags & PERLIO_F_CANREAD) { | |
1649 | *s++ = '+'; | |
1650 | } | |
1651 | } | |
1652 | else if (flags & PERLIO_F_CANREAD) { | |
1653 | *s++ = 'r'; | |
1654 | if (flags & PERLIO_F_CANWRITE) | |
1655 | *s++ = '+'; | |
1656 | } | |
1657 | else if (flags & PERLIO_F_CANWRITE) { | |
1658 | *s++ = 'w'; | |
1659 | if (flags & PERLIO_F_CANREAD) { | |
1660 | *s++ = '+'; | |
1661 | } | |
1662 | } | |
5f1a76d0 | 1663 | #if O_TEXT != O_BINARY |
14a5cf38 JH |
1664 | if (!(flags & PERLIO_F_CRLF)) |
1665 | *s++ = 'b'; | |
5f1a76d0 | 1666 | #endif |
14a5cf38 JH |
1667 | *s = '\0'; |
1668 | return buf; | |
f5b9d040 NIS |
1669 | } |
1670 | ||
76ced9ad | 1671 | IV |
e3f3bf95 | 1672 | PerlIOBase_pushed(PerlIO *f, const char *mode, SV *arg) |
9e353e3b | 1673 | { |
14a5cf38 | 1674 | PerlIOl *l = PerlIOBase(f); |
b7953727 | 1675 | #if 0 |
14a5cf38 JH |
1676 | const char *omode = mode; |
1677 | char temp[8]; | |
b7953727 | 1678 | #endif |
14a5cf38 JH |
1679 | PerlIO_funcs *tab = PerlIOBase(f)->tab; |
1680 | l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | | |
1681 | PERLIO_F_TRUNCATE | PERLIO_F_APPEND); | |
1682 | if (tab->Set_ptrcnt != NULL) | |
1683 | l->flags |= PERLIO_F_FASTGETS; | |
1684 | if (mode) { | |
1685 | if (*mode == '#' || *mode == 'I') | |
1686 | mode++; | |
1687 | switch (*mode++) { | |
1688 | case 'r': | |
1689 | l->flags |= PERLIO_F_CANREAD; | |
1690 | break; | |
1691 | case 'a': | |
1692 | l->flags |= PERLIO_F_APPEND | PERLIO_F_CANWRITE; | |
1693 | break; | |
1694 | case 'w': | |
1695 | l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE; | |
1696 | break; | |
1697 | default: | |
1698 | SETERRNO(EINVAL, LIB$_INVARG); | |
1699 | return -1; | |
1700 | } | |
1701 | while (*mode) { | |
1702 | switch (*mode++) { | |
1703 | case '+': | |
1704 | l->flags |= PERLIO_F_CANREAD | PERLIO_F_CANWRITE; | |
1705 | break; | |
1706 | case 'b': | |
1707 | l->flags &= ~PERLIO_F_CRLF; | |
1708 | break; | |
1709 | case 't': | |
1710 | l->flags |= PERLIO_F_CRLF; | |
1711 | break; | |
1712 | default: | |
1713 | SETERRNO(EINVAL, LIB$_INVARG); | |
1714 | return -1; | |
1715 | } | |
1716 | } | |
1717 | } | |
1718 | else { | |
1719 | if (l->next) { | |
1720 | l->flags |= l->next->flags & | |
1721 | (PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_TRUNCATE | | |
1722 | PERLIO_F_APPEND); | |
1723 | } | |
1724 | } | |
5e2ab84b | 1725 | #if 0 |
14a5cf38 JH |
1726 | PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08" UVxf " (%s)\n", |
1727 | f, PerlIOBase(f)->tab->name, (omode) ? omode : "(Null)", | |
1728 | l->flags, PerlIO_modestr(f, temp)); | |
5e2ab84b | 1729 | #endif |
14a5cf38 | 1730 | return 0; |
76ced9ad NIS |
1731 | } |
1732 | ||
1733 | IV | |
1734 | PerlIOBase_popped(PerlIO *f) | |
1735 | { | |
14a5cf38 | 1736 | return 0; |
760ac839 LW |
1737 | } |
1738 | ||
9e353e3b NIS |
1739 | SSize_t |
1740 | PerlIOBase_unread(PerlIO *f, const void *vbuf, Size_t count) | |
1741 | { | |
14a5cf38 JH |
1742 | dTHX; |
1743 | /* | |
71200d45 | 1744 | * Save the position as current head considers it |
14a5cf38 JH |
1745 | */ |
1746 | Off_t old = PerlIO_tell(f); | |
1747 | SSize_t done; | |
1748 | PerlIO_push(aTHX_ f, &PerlIO_pending, "r", Nullsv); | |
1749 | PerlIOSelf(f, PerlIOBuf)->posn = old; | |
1750 | done = PerlIOBuf_unread(f, vbuf, count); | |
1751 | return done; | |
9e353e3b NIS |
1752 | } |
1753 | ||
f6c77cf1 NIS |
1754 | SSize_t |
1755 | PerlIOBase_read(PerlIO *f, void *vbuf, Size_t count) | |
1756 | { | |
14a5cf38 JH |
1757 | STDCHAR *buf = (STDCHAR *) vbuf; |
1758 | if (f) { | |
1759 | if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD)) | |
1760 | return 0; | |
1761 | while (count > 0) { | |
1762 | SSize_t avail = PerlIO_get_cnt(f); | |
1763 | SSize_t take = 0; | |
1764 | if (avail > 0) | |
1765 | take = (count < avail) ? count : avail; | |
1766 | if (take > 0) { | |
1767 | STDCHAR *ptr = PerlIO_get_ptr(f); | |
1768 | Copy(ptr, buf, take, STDCHAR); | |
1769 | PerlIO_set_ptrcnt(f, ptr + take, (avail -= take)); | |
1770 | count -= take; | |
1771 | buf += take; | |
1772 | } | |
1773 | if (count > 0 && avail <= 0) { | |
1774 | if (PerlIO_fill(f) != 0) | |
1775 | break; | |
1776 | } | |
1777 | } | |
1778 | return (buf - (STDCHAR *) vbuf); | |
1779 | } | |
f6c77cf1 | 1780 | return 0; |
f6c77cf1 NIS |
1781 | } |
1782 | ||
9e353e3b | 1783 | IV |
06da4f11 | 1784 | PerlIOBase_noop_ok(PerlIO *f) |
9e353e3b | 1785 | { |
14a5cf38 | 1786 | return 0; |
9e353e3b NIS |
1787 | } |
1788 | ||
1789 | IV | |
06da4f11 NIS |
1790 | PerlIOBase_noop_fail(PerlIO *f) |
1791 | { | |
14a5cf38 | 1792 | return -1; |
06da4f11 NIS |
1793 | } |
1794 | ||
1795 | IV | |
9e353e3b NIS |
1796 | PerlIOBase_close(PerlIO *f) |
1797 | { | |
14a5cf38 JH |
1798 | IV code = 0; |
1799 | PerlIO *n = PerlIONext(f); | |
1800 | if (PerlIO_flush(f) != 0) | |
1801 | code = -1; | |
1802 | if (n && *n && (*PerlIOBase(n)->tab->Close) (n) != 0) | |
1803 | code = -1; | |
1804 | PerlIOBase(f)->flags &= | |
1805 | ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN); | |
1806 | return code; | |
9e353e3b NIS |
1807 | } |
1808 | ||
1809 | IV | |
1810 | PerlIOBase_eof(PerlIO *f) | |
1811 | { | |
14a5cf38 JH |
1812 | if (f && *f) { |
1813 | return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0; | |
1814 | } | |
1815 | return 1; | |
9e353e3b NIS |
1816 | } |
1817 | ||
1818 | IV | |
1819 | PerlIOBase_error(PerlIO *f) | |
1820 | { | |
14a5cf38 JH |
1821 | if (f && *f) { |
1822 | return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0; | |
1823 | } | |
1824 | return 1; | |
9e353e3b NIS |
1825 | } |
1826 | ||
1827 | void | |
1828 | PerlIOBase_clearerr(PerlIO *f) | |
1829 | { | |
14a5cf38 JH |
1830 | if (f && *f) { |
1831 | PerlIO *n = PerlIONext(f); | |
1832 | PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF); | |
1833 | if (n) | |
1834 | PerlIO_clearerr(n); | |
1835 | } | |
9e353e3b NIS |
1836 | } |
1837 | ||
1838 | void | |
1839 | PerlIOBase_setlinebuf(PerlIO *f) | |
1840 | { | |
14a5cf38 JH |
1841 | if (f) { |
1842 | PerlIOBase(f)->flags |= PERLIO_F_LINEBUF; | |
1843 | } | |
9e353e3b NIS |
1844 | } |
1845 | ||
9e353e3b | 1846 | /*--------------------------------------------------------------------------------------*/ |
14a5cf38 | 1847 | /* |
71200d45 | 1848 | * Bottom-most level for UNIX-like case |
14a5cf38 | 1849 | */ |
9e353e3b | 1850 | |
14a5cf38 JH |
1851 | typedef struct { |
1852 | struct _PerlIO base; /* The generic part */ | |
1853 | int fd; /* UNIX like file descriptor */ | |
1854 | int oflags; /* open/fcntl flags */ | |
9e353e3b NIS |
1855 | } PerlIOUnix; |
1856 | ||
6f9d8c32 | 1857 | int |
9e353e3b | 1858 | PerlIOUnix_oflags(const char *mode) |
760ac839 | 1859 | { |
14a5cf38 JH |
1860 | int oflags = -1; |
1861 | if (*mode == 'I' || *mode == '#') | |
1862 | mode++; | |
1863 | switch (*mode) { | |
1864 | case 'r': | |
1865 | oflags = O_RDONLY; | |
1866 | if (*++mode == '+') { | |
1867 | oflags = O_RDWR; | |
1868 | mode++; | |
1869 | } | |
1870 | break; | |
1871 | ||
1872 | case 'w': | |
1873 | oflags = O_CREAT | O_TRUNC; | |
1874 | if (*++mode == '+') { | |
1875 | oflags |= O_RDWR; | |
1876 | mode++; | |
1877 | } | |
1878 | else | |
1879 | oflags |= O_WRONLY; | |
1880 | break; | |
1881 | ||
1882 | case 'a': | |
1883 | oflags = O_CREAT | O_APPEND; | |
1884 | if (*++mode == '+') { | |
1885 | oflags |= O_RDWR; | |
1886 | mode++; | |
1887 | } | |
1888 | else | |
1889 | oflags |= O_WRONLY; | |
1890 | break; | |
1891 | } | |
1892 | if (*mode == 'b') { | |
1893 | oflags |= O_BINARY; | |
1894 | oflags &= ~O_TEXT; | |
1895 | mode++; | |
1896 | } | |
1897 | else if (*mode == 't') { | |
1898 | oflags |= O_TEXT; | |
1899 | oflags &= ~O_BINARY; | |
1900 | mode++; | |
1901 | } | |
1902 | /* | |
71200d45 | 1903 | * Always open in binary mode |
14a5cf38 JH |
1904 | */ |
1905 | oflags |= O_BINARY; | |
1906 | if (*mode || oflags == -1) { | |
1907 | SETERRNO(EINVAL, LIB$_INVARG); | |
1908 | oflags = -1; | |
1909 | } | |
1910 | return oflags; | |
9e353e3b NIS |
1911 | } |
1912 | ||
1913 | IV | |
1914 | PerlIOUnix_fileno(PerlIO *f) | |
1915 | { | |
14a5cf38 | 1916 | return PerlIOSelf(f, PerlIOUnix)->fd; |
9e353e3b NIS |
1917 | } |
1918 | ||
4b803d04 | 1919 | IV |
e3f3bf95 | 1920 | PerlIOUnix_pushed(PerlIO *f, const char *mode, SV *arg) |
4b803d04 | 1921 | { |
14a5cf38 JH |
1922 | IV code = PerlIOBase_pushed(f, mode, arg); |
1923 | if (*PerlIONext(f)) { | |
1924 | PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix); | |
1925 | s->fd = PerlIO_fileno(PerlIONext(f)); | |
1926 | /* | |
71200d45 | 1927 | * XXX could (or should) we retrieve the oflags from the open file |
14a5cf38 | 1928 | * handle rather than believing the "mode" we are passed in? XXX |
71200d45 | 1929 | * Should the value on NULL mode be 0 or -1? |
14a5cf38 JH |
1930 | */ |
1931 | s->oflags = mode ? PerlIOUnix_oflags(mode) : -1; | |
1932 | } | |
1933 | PerlIOBase(f)->flags |= PERLIO_F_OPEN; | |
1934 | return code; | |
4b803d04 NIS |
1935 | } |
1936 | ||
9e353e3b | 1937 | PerlIO * |
14a5cf38 JH |
1938 | PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers, |
1939 | IV n, const char *mode, int fd, int imode, | |
1940 | int perm, PerlIO *f, int narg, SV **args) | |
1941 | { | |
1942 | if (f) { | |
1943 | if (PerlIOBase(f)->flags & PERLIO_F_OPEN) | |
1944 | (*PerlIOBase(f)->tab->Close) (f); | |
1945 | } | |
1946 | if (narg > 0) { | |
1947 | char *path = SvPV_nolen(*args); | |
1948 | if (*mode == '#') | |
1949 | mode++; | |
1950 | else { | |
1951 | imode = PerlIOUnix_oflags(mode); | |
1952 | perm = 0666; | |
1953 | } | |
1954 | if (imode != -1) { | |
1955 | fd = PerlLIO_open3(path, imode, perm); | |
1956 | } | |
1957 | } | |
1958 | if (fd >= 0) { | |
1959 | PerlIOUnix *s; | |
1960 | if (*mode == 'I') | |
1961 | mode++; | |
1962 | if (!f) { | |
1963 | f = PerlIO_allocate(aTHX); | |
1964 | s = PerlIOSelf(PerlIO_push(aTHX_ f, self, mode, PerlIOArg), | |
1965 | PerlIOUnix); | |
1966 | } | |
1967 | else | |
1968 | s = PerlIOSelf(f, PerlIOUnix); | |
1969 | s->fd = fd; | |
1970 | s->oflags = imode; | |
1971 | PerlIOBase(f)->flags |= PERLIO_F_OPEN; | |
1972 | return f; | |
1973 | } | |
1974 | else { | |
1975 | if (f) { | |
1976 | /* | |
71200d45 | 1977 | * FIXME: pop layers ??? |
14a5cf38 JH |
1978 | */ |
1979 | } | |
1980 | return NULL; | |
1981 | } | |
9e353e3b NIS |
1982 | } |
1983 | ||
8cf8f3d1 NIS |
1984 | SV * |
1985 | PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param) | |
71200d45 | 1986 | { |
8cf8f3d1 NIS |
1987 | if (!arg) |
1988 | return Nullsv; | |
71200d45 | 1989 | #ifdef sv_dup |
8cf8f3d1 NIS |
1990 | if (param) { |
1991 | return sv_dup(arg, param); | |
1992 | } | |
1993 | else { | |
1994 | return newSVsv(arg); | |
1995 | } | |
1996 | #else | |
1997 | return newSVsv(arg); | |
71200d45 | 1998 | #endif |
8cf8f3d1 NIS |
1999 | } |
2000 | ||
2001 | PerlIO * | |
2002 | PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param) | |
2003 | { | |
2004 | PerlIO *nexto = PerlIONext(o); | |
a8fc9800 | 2005 | PerlIO_debug("PerlIOBase_dup f=%p o=%p param=%p\n",f,o,param); |
8cf8f3d1 NIS |
2006 | if (*nexto) { |
2007 | PerlIO_funcs *tab = PerlIOBase(nexto)->tab; | |
2008 | f = (*tab->Dup)(aTHX_ f, nexto, param); | |
71200d45 | 2009 | } |
8cf8f3d1 NIS |
2010 | if (f) { |
2011 | PerlIO_funcs *self = PerlIOBase(o)->tab; | |
2012 | SV *arg = Nullsv; | |
2013 | char buf[8]; | |
2014 | if (self->Getarg) { | |
2015 | arg = (*self->Getarg)(o); | |
2016 | if (arg) { | |
2017 | arg = PerlIO_sv_dup(aTHX_ arg, param); | |
2018 | } | |
2019 | } | |
2020 | f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg); | |
2021 | if (!f && arg) { | |
2022 | SvREFCNT_dec(arg); | |
2023 | } | |
71200d45 | 2024 | } |
71200d45 NIS |
2025 | return f; |
2026 | } | |
2027 | ||
2028 | PerlIO * | |
8cf8f3d1 | 2029 | PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param) |
71200d45 NIS |
2030 | { |
2031 | PerlIOUnix *os = PerlIOSelf(o, PerlIOUnix); | |
2032 | int fd = PerlLIO_dup(os->fd); | |
2033 | if (fd >= 0) { | |
2034 | f = PerlIOBase_dup(aTHX_ f, o, param); | |
2035 | if (f) { | |
2036 | /* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */ | |
2037 | PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix); | |
2038 | s->fd = fd; | |
2039 | return f; | |
2040 | } | |
2041 | else { | |
2042 | PerlLIO_close(fd); | |
2043 | } | |
2044 | } | |
2045 | return NULL; | |
2046 | } | |
2047 | ||
2048 | ||
9e353e3b NIS |
2049 | SSize_t |
2050 | PerlIOUnix_read(PerlIO *f, void *vbuf, Size_t count) | |
2051 | { | |
14a5cf38 JH |
2052 | dTHX; |
2053 | int fd = PerlIOSelf(f, PerlIOUnix)->fd; | |
2054 | if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD)) | |
2055 | return 0; | |
2056 | while (1) { | |
2057 | SSize_t len = PerlLIO_read(fd, vbuf, count); | |
2058 | if (len >= 0 || errno != EINTR) { | |
2059 | if (len < 0) | |
2060 | PerlIOBase(f)->flags |= PERLIO_F_ERROR; | |
2061 | else if (len == 0 && count != 0) | |
2062 | PerlIOBase(f)->flags |= PERLIO_F_EOF; | |
2063 | return len; | |
2064 | } | |
2065 | PERL_ASYNC_CHECK(); | |
2066 | } | |
9e353e3b NIS |
2067 | } |
2068 | ||
2069 | SSize_t | |
2070 | PerlIOUnix_write(PerlIO *f, const void *vbuf, Size_t count) | |
2071 | { | |
14a5cf38 JH |
2072 | dTHX; |
2073 | int fd = PerlIOSelf(f, PerlIOUnix)->fd; | |
2074 | while (1) { | |
2075 | SSize_t len = PerlLIO_write(fd, vbuf, count); | |
2076 | if (len >= 0 || errno != EINTR) { | |
2077 | if (len < 0) | |
2078 | PerlIOBase(f)->flags |= PERLIO_F_ERROR; | |
2079 | return len; | |
2080 | } | |
2081 | PERL_ASYNC_CHECK(); | |
06da4f11 | 2082 | } |
9e353e3b NIS |
2083 | } |
2084 | ||
2085 | IV | |
2086 | PerlIOUnix_seek(PerlIO *f, Off_t offset, int whence) | |
2087 | { | |
14a5cf38 JH |
2088 | dSYS; |
2089 | Off_t new = | |
2090 | PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, offset, whence); | |
2091 | PerlIOBase(f)->flags &= ~PERLIO_F_EOF; | |
2092 | return (new == (Off_t) - 1) ? -1 : 0; | |
9e353e3b NIS |
2093 | } |
2094 | ||
2095 | Off_t | |
2096 | PerlIOUnix_tell(PerlIO *f) | |
2097 | { | |
14a5cf38 JH |
2098 | dSYS; |
2099 | return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR); | |
9e353e3b NIS |
2100 | } |
2101 | ||
71200d45 | 2102 | |
9e353e3b NIS |
2103 | IV |
2104 | PerlIOUnix_close(PerlIO *f) | |
2105 | { | |
14a5cf38 JH |
2106 | dTHX; |
2107 | int fd = PerlIOSelf(f, PerlIOUnix)->fd; | |
2108 | int code = 0; | |
2109 | while (PerlLIO_close(fd) != 0) { | |
2110 | if (errno != EINTR) { | |
2111 | code = -1; | |
2112 | break; | |
2113 | } | |
2114 | PERL_ASYNC_CHECK(); | |
2115 | } | |
2116 | if (code == 0) { | |
2117 | PerlIOBase(f)->flags &= ~PERLIO_F_OPEN; | |
2118 | } | |
2119 | return code; | |
9e353e3b NIS |
2120 | } |
2121 | ||
2122 | PerlIO_funcs PerlIO_unix = { | |
14a5cf38 JH |
2123 | "unix", |
2124 | sizeof(PerlIOUnix), | |
2125 | PERLIO_K_RAW, | |
2126 | PerlIOUnix_pushed, | |
2127 | PerlIOBase_noop_ok, | |
2128 | PerlIOUnix_open, | |
2129 | NULL, | |
2130 | PerlIOUnix_fileno, | |
71200d45 | 2131 | PerlIOUnix_dup, |
14a5cf38 JH |
2132 | PerlIOUnix_read, |
2133 | PerlIOBase_unread, | |
2134 | PerlIOUnix_write, | |
2135 | PerlIOUnix_seek, | |
2136 | PerlIOUnix_tell, | |
2137 | PerlIOUnix_close, | |
2138 | PerlIOBase_noop_ok, /* flush */ | |
2139 | PerlIOBase_noop_fail, /* fill */ | |
2140 | PerlIOBase_eof, | |
2141 | PerlIOBase_error, | |
2142 | PerlIOBase_clearerr, | |
2143 | PerlIOBase_setlinebuf, | |
2144 | NULL, /* get_base */ | |
2145 | NULL, /* get_bufsiz */ | |
2146 | NULL, /* get_ptr */ | |
2147 | NULL, /* get_cnt */ | |
2148 | NULL, /* set_ptrcnt */ | |
9e353e3b NIS |
2149 | }; |
2150 | ||
2151 | /*--------------------------------------------------------------------------------------*/ | |
14a5cf38 | 2152 | /* |
71200d45 | 2153 | * stdio as a layer |
14a5cf38 | 2154 | */ |
9e353e3b | 2155 | |
14a5cf38 JH |
2156 | typedef struct { |
2157 | struct _PerlIO base; | |
2158 | FILE *stdio; /* The stream */ | |
9e353e3b NIS |
2159 | } PerlIOStdio; |
2160 | ||
2161 | IV | |
2162 | PerlIOStdio_fileno(PerlIO *f) | |
2163 | { | |
14a5cf38 JH |
2164 | dSYS; |
2165 | return PerlSIO_fileno(PerlIOSelf(f, PerlIOStdio)->stdio); | |
9e353e3b NIS |
2166 | } |
2167 | ||
766a733e | 2168 | char * |
14a5cf38 JH |
2169 | PerlIOStdio_mode(const char *mode, char *tmode) |
2170 | { | |
2171 | char *ret = tmode; | |
2172 | while (*mode) { | |
2173 | *tmode++ = *mode++; | |
2174 | } | |
2175 | if (O_BINARY != O_TEXT) { | |
2176 | *tmode++ = 'b'; | |
2177 | } | |
2178 | *tmode = '\0'; | |
2179 | return ret; | |
2180 | } | |
2181 | ||
2182 | /* | |
71200d45 | 2183 | * This isn't used yet ... |
14a5cf38 | 2184 | */ |
4b803d04 | 2185 | IV |
e3f3bf95 | 2186 | PerlIOStdio_pushed(PerlIO *f, const char *mode, SV *arg) |
4b803d04 | 2187 | { |
14a5cf38 JH |
2188 | if (*PerlIONext(f)) { |
2189 | dSYS; | |
2190 | PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio); | |
2191 | char tmode[8]; | |
2192 | FILE *stdio = | |
2193 | PerlSIO_fdopen(PerlIO_fileno(PerlIONext(f)), mode = | |
2194 | PerlIOStdio_mode(mode, tmode)); | |
2195 | if (stdio) | |
2196 | s->stdio = stdio; | |
2197 | else | |
2198 | return -1; | |
2199 | } | |
2200 | return PerlIOBase_pushed(f, mode, arg); | |
4b803d04 NIS |
2201 | } |
2202 | ||
9e353e3b NIS |
2203 | #undef PerlIO_importFILE |
2204 | PerlIO * | |
2205 | PerlIO_importFILE(FILE *stdio, int fl) | |
2206 | { | |
14a5cf38 JH |
2207 | dTHX; |
2208 | PerlIO *f = NULL; | |
2209 | if (stdio) { | |
2210 | PerlIOStdio *s = | |
2211 | PerlIOSelf(PerlIO_push | |
2212 | (aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio, | |
2213 | "r+", Nullsv), PerlIOStdio); | |
2214 | s->stdio = stdio; | |
2215 | } | |
2216 | return f; | |
9e353e3b NIS |
2217 | } |
2218 | ||
2219 | PerlIO * | |
14a5cf38 JH |
2220 | PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers, |
2221 | IV n, const char *mode, int fd, int imode, | |
2222 | int perm, PerlIO *f, int narg, SV **args) | |
2223 | { | |
2224 | char tmode[8]; | |
2225 | if (f) { | |
2226 | char *path = SvPV_nolen(*args); | |
2227 | PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio); | |
2228 | FILE *stdio = | |
2229 | PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)), | |
2230 | s->stdio); | |
2231 | if (!s->stdio) | |
2232 | return NULL; | |
2233 | s->stdio = stdio; | |
2234 | return f; | |
2235 | } | |
2236 | else { | |
2237 | if (narg > 0) { | |
2238 | char *path = SvPV_nolen(*args); | |
2239 | if (*mode == '#') { | |
2240 | mode++; | |
2241 | fd = PerlLIO_open3(path, imode, perm); | |
2242 | } | |
2243 | else { | |
2244 | FILE *stdio = PerlSIO_fopen(path, mode); | |
2245 | if (stdio) { | |
2246 | PerlIOStdio *s = | |
2247 | PerlIOSelf(PerlIO_push | |
2248 | (aTHX_(f = PerlIO_allocate(aTHX)), self, | |
2249 | (mode = PerlIOStdio_mode(mode, tmode)), | |
2250 | PerlIOArg), | |
2251 | PerlIOStdio); | |
2252 | s->stdio = stdio; | |
2253 | } | |
2254 | return f; | |
2255 | } | |
2256 | } | |
2257 | if (fd >= 0) { | |
2258 | FILE *stdio = NULL; | |
2259 | int init = 0; | |
2260 | if (*mode == 'I') { | |
2261 | init = 1; | |
2262 | mode++; | |
2263 | } | |
2264 | if (init) { | |
2265 | switch (fd) { | |
2266 | case 0: | |
2267 | stdio = PerlSIO_stdin; | |
2268 | break; | |
2269 | case 1: | |
2270 | stdio = PerlSIO_stdout; | |
2271 | break; | |
2272 | case 2: | |
2273 | stdio = PerlSIO_stderr; | |
2274 | break; | |
2275 | } | |
2276 | } | |
2277 | else { | |
2278 | stdio = PerlSIO_fdopen(fd, mode = | |
2279 | PerlIOStdio_mode(mode, tmode)); | |
2280 | } | |
2281 | if (stdio) { | |
2282 | PerlIOStdio *s = | |
2283 | PerlIOSelf(PerlIO_push | |
2284 | (aTHX_(f = PerlIO_allocate(aTHX)), self, | |
2285 | mode, PerlIOArg), PerlIOStdio); | |
2286 | s->stdio = stdio; | |
2287 | return f; | |
2288 | } | |
2289 | } | |
2290 | } | |
ee518936 | 2291 | return NULL; |
9e353e3b NIS |
2292 | } |
2293 | ||
2294 | SSize_t | |
2295 | PerlIOStdio_read(PerlIO *f, void *vbuf, Size_t count) | |
2296 | { | |
14a5cf38 JH |
2297 | dSYS; |
2298 | FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio; | |
2299 | SSize_t got = 0; | |
2300 | if (count == 1) { | |
2301 | STDCHAR *buf = (STDCHAR *) vbuf; | |
2302 | /* | |
2303 | * Perl is expecting PerlIO_getc() to fill the buffer Linux's | |
71200d45 | 2304 | * stdio does not do that for fread() |
14a5cf38 JH |
2305 | */ |
2306 | int ch = PerlSIO_fgetc(s); | |
2307 | if (ch != EOF) { | |
2308 | *buf = ch; | |
2309 | got = 1; | |
2310 | } | |
2311 | } | |
2312 | else | |
2313 | got = PerlSIO_fread(vbuf, 1, count, s); | |
2314 | return got; | |
9e353e3b NIS |
2315 | } |
2316 | ||
2317 | SSize_t | |
2318 | PerlIOStdio_unread(PerlIO *f, const void *vbuf, Size_t count) | |
2319 | { | |
14a5cf38 JH |
2320 | dSYS; |
2321 | FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio; | |
2322 | STDCHAR *buf = ((STDCHAR *) vbuf) + count - 1; | |
2323 | SSize_t unread = 0; | |
2324 | while (count > 0) { | |
2325 | int ch = *buf-- & 0xff; | |
2326 | if (PerlSIO_ungetc(ch, s) != ch) | |
2327 | break; | |
2328 | unread++; | |
2329 | count--; | |
2330 | } | |
2331 | return unread; | |
9e353e3b NIS |
2332 | } |
2333 | ||
2334 | SSize_t | |
2335 | PerlIOStdio_write(PerlIO *f, const void *vbuf, Size_t count) | |
2336 | { | |
14a5cf38 JH |
2337 | dSYS; |
2338 | return PerlSIO_fwrite(vbuf, 1, count, | |
2339 | PerlIOSelf(f, PerlIOStdio)->stdio); | |
9e353e3b NIS |
2340 | } |
2341 | ||
2342 | IV | |
2343 | PerlIOStdio_seek(PerlIO *f, Off_t offset, int whence) | |
2344 | { | |
14a5cf38 JH |
2345 | dSYS; |
2346 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; | |
2347 | return PerlSIO_fseek(stdio, offset, whence); | |
9e353e3b NIS |
2348 | } |
2349 | ||
2350 | Off_t | |
2351 | PerlIOStdio_tell(PerlIO *f) | |
2352 | { | |
14a5cf38 JH |
2353 | dSYS; |
2354 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; | |
2355 | return PerlSIO_ftell(stdio); | |
9e353e3b NIS |
2356 | } |
2357 | ||
2358 | IV | |
2359 | PerlIOStdio_close(PerlIO *f) | |
2360 | { | |
14a5cf38 | 2361 | dSYS; |
af130d45 | 2362 | #ifdef SOCKS5_VERSION_NAME |
14a5cf38 JH |
2363 | int optval; |
2364 | Sock_size_t optlen = sizeof(int); | |
8e4bc33b | 2365 | #endif |
14a5cf38 JH |
2366 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; |
2367 | return ( | |
af130d45 | 2368 | #ifdef SOCKS5_VERSION_NAME |
14a5cf38 JH |
2369 | (getsockopt |
2370 | (PerlIO_fileno(f), SOL_SOCKET, SO_TYPE, (void *) &optval, | |
2371 | &optlen) < | |
2372 | 0) ? PerlSIO_fclose(stdio) : close(PerlIO_fileno(f)) | |
8e4bc33b | 2373 | #else |
14a5cf38 | 2374 | PerlSIO_fclose(stdio) |
8e4bc33b | 2375 | #endif |
14a5cf38 | 2376 | ); |
8e4bc33b | 2377 | |
9e353e3b NIS |
2378 | } |
2379 | ||
2380 | IV | |
2381 | PerlIOStdio_flush(PerlIO *f) | |
2382 | { | |
14a5cf38 JH |
2383 | dSYS; |
2384 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; | |
2385 | if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) { | |
2386 | return PerlSIO_fflush(stdio); | |
2387 | } | |
2388 | else { | |
88b61e10 | 2389 | #if 0 |
14a5cf38 JH |
2390 | /* |
2391 | * FIXME: This discards ungetc() and pre-read stuff which is not | |
71200d45 | 2392 | * right if this is just a "sync" from a layer above Suspect right |
14a5cf38 | 2393 | * design is to do _this_ but not have layer above flush this |
71200d45 | 2394 | * layer read-to-read |
14a5cf38 JH |
2395 | */ |
2396 | /* | |
71200d45 | 2397 | * Not writeable - sync by attempting a seek |
14a5cf38 JH |
2398 | */ |
2399 | int err = errno; | |
2400 | if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0) | |
2401 | errno = err; | |
88b61e10 | 2402 | #endif |
14a5cf38 JH |
2403 | } |
2404 | return 0; | |
9e353e3b NIS |
2405 | } |
2406 | ||
2407 | IV | |
06da4f11 NIS |
2408 | PerlIOStdio_fill(PerlIO *f) |
2409 | { | |
14a5cf38 JH |
2410 | dSYS; |
2411 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; | |
2412 | int c; | |
2413 | /* | |
71200d45 | 2414 | * fflush()ing read-only streams can cause trouble on some stdio-s |
14a5cf38 JH |
2415 | */ |
2416 | if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) { | |
2417 | if (PerlSIO_fflush(stdio) != 0) | |
2418 | return EOF; | |
2419 | } | |
2420 | c = PerlSIO_fgetc(stdio); | |
2421 | if (c == EOF || PerlSIO_ungetc(c, stdio) != c) | |
2422 | return EOF; | |
2423 | return 0; | |
06da4f11 NIS |
2424 | } |
2425 | ||
2426 | IV | |
9e353e3b NIS |
2427 | PerlIOStdio_eof(PerlIO *f) |
2428 | { | |
14a5cf38 JH |
2429 | dSYS; |
2430 | return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio); | |
9e353e3b NIS |
2431 | } |
2432 | ||
2433 | IV | |
2434 | PerlIOStdio_error(PerlIO *f) | |
2435 | { | |
14a5cf38 JH |
2436 | dSYS; |
2437 | return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio); | |
9e353e3b NIS |
2438 | } |
2439 | ||
2440 | void | |
2441 | PerlIOStdio_clearerr(PerlIO *f) | |
2442 | { | |
14a5cf38 JH |
2443 | dSYS; |
2444 | PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio); | |
9e353e3b NIS |
2445 | } |
2446 | ||
2447 | void | |
2448 | PerlIOStdio_setlinebuf(PerlIO *f) | |
2449 | { | |
14a5cf38 | 2450 | dSYS; |
9e353e3b | 2451 | #ifdef HAS_SETLINEBUF |
14a5cf38 | 2452 | PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio); |
9e353e3b | 2453 | #else |
14a5cf38 | 2454 | PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, Nullch, _IOLBF, 0); |
9e353e3b NIS |
2455 | #endif |
2456 | } | |
2457 | ||
2458 | #ifdef FILE_base | |
2459 | STDCHAR * | |
2460 | PerlIOStdio_get_base(PerlIO *f) | |
2461 | { | |
14a5cf38 JH |
2462 | dSYS; |
2463 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; | |
2464 | return PerlSIO_get_base(stdio); | |
9e353e3b NIS |
2465 | } |
2466 | ||
2467 | Size_t | |
2468 | PerlIOStdio_get_bufsiz(PerlIO *f) | |
2469 | { | |
14a5cf38 JH |
2470 | dSYS; |
2471 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; | |
2472 | return PerlSIO_get_bufsiz(stdio); | |
9e353e3b NIS |
2473 | } |
2474 | #endif | |
2475 | ||
2476 | #ifdef USE_STDIO_PTR | |
2477 | STDCHAR * | |
2478 | PerlIOStdio_get_ptr(PerlIO *f) | |
2479 | { | |
14a5cf38 JH |
2480 | dSYS; |
2481 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; | |
2482 | return PerlSIO_get_ptr(stdio); | |
9e353e3b NIS |
2483 | } |
2484 | ||
2485 | SSize_t | |
2486 | PerlIOStdio_get_cnt(PerlIO *f) | |
2487 | { | |
14a5cf38 JH |
2488 | dSYS; |
2489 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; | |
2490 | return PerlSIO_get_cnt(stdio); | |
9e353e3b NIS |
2491 | } |
2492 | ||
2493 | void | |
14a5cf38 | 2494 | PerlIOStdio_set_ptrcnt(PerlIO *f, STDCHAR * ptr, SSize_t cnt) |
9e353e3b | 2495 | { |
14a5cf38 JH |
2496 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; |
2497 | dSYS; | |
2498 | if (ptr != NULL) { | |
9e353e3b | 2499 | #ifdef STDIO_PTR_LVALUE |
14a5cf38 | 2500 | PerlSIO_set_ptr(stdio, ptr); |
9e353e3b | 2501 | #ifdef STDIO_PTR_LVAL_SETS_CNT |
14a5cf38 JH |
2502 | if (PerlSIO_get_cnt(stdio) != (cnt)) { |
2503 | dTHX; | |
2504 | assert(PerlSIO_get_cnt(stdio) == (cnt)); | |
2505 | } | |
9e353e3b NIS |
2506 | #endif |
2507 | #if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT)) | |
14a5cf38 | 2508 | /* |
71200d45 | 2509 | * Setting ptr _does_ change cnt - we are done |
14a5cf38 JH |
2510 | */ |
2511 | return; | |
9e353e3b | 2512 | #endif |
14a5cf38 JH |
2513 | #else /* STDIO_PTR_LVALUE */ |
2514 | PerlProc_abort(); | |
2515 | #endif /* STDIO_PTR_LVALUE */ | |
2516 | } | |
2517 | /* | |
71200d45 | 2518 | * Now (or only) set cnt |
14a5cf38 | 2519 | */ |
9e353e3b | 2520 | #ifdef STDIO_CNT_LVALUE |
14a5cf38 JH |
2521 | PerlSIO_set_cnt(stdio, cnt); |
2522 | #else /* STDIO_CNT_LVALUE */ | |
9e353e3b | 2523 | #if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT)) |
14a5cf38 JH |
2524 | PerlSIO_set_ptr(stdio, |
2525 | PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) - | |
2526 | cnt)); | |
2527 | #else /* STDIO_PTR_LVAL_SETS_CNT */ | |
2528 | PerlProc_abort(); | |
2529 | #endif /* STDIO_PTR_LVAL_SETS_CNT */ | |
2530 | #endif /* STDIO_CNT_LVALUE */ | |
9e353e3b NIS |
2531 | } |
2532 | ||
2533 | #endif | |
2534 | ||
71200d45 | 2535 | PerlIO * |
8cf8f3d1 | 2536 | PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param) |
71200d45 | 2537 | { |
a8fc9800 | 2538 | return PerlIOBase_dup(aTHX_ f, o, param); |
71200d45 NIS |
2539 | } |
2540 | ||
9e353e3b | 2541 | PerlIO_funcs PerlIO_stdio = { |
14a5cf38 JH |
2542 | "stdio", |
2543 | sizeof(PerlIOStdio), | |
2544 | PERLIO_K_BUFFERED, | |
2545 | PerlIOBase_pushed, | |
2546 | PerlIOBase_noop_ok, | |
2547 | PerlIOStdio_open, | |
2548 | NULL, | |
2549 | PerlIOStdio_fileno, | |
71200d45 | 2550 | PerlIOStdio_dup, |
14a5cf38 JH |
2551 | PerlIOStdio_read, |
2552 | PerlIOStdio_unread, | |
2553 | PerlIOStdio_write, | |
2554 | PerlIOStdio_seek, | |
2555 | PerlIOStdio_tell, | |
2556 | PerlIOStdio_close, | |
2557 | PerlIOStdio_flush, | |
2558 | PerlIOStdio_fill, | |
2559 | PerlIOStdio_eof, | |
2560 | PerlIOStdio_error, | |
2561 | PerlIOStdio_clearerr, | |
2562 | PerlIOStdio_setlinebuf, | |
9e353e3b | 2563 | #ifdef FILE_base |
14a5cf38 JH |
2564 | PerlIOStdio_get_base, |
2565 | PerlIOStdio_get_bufsiz, | |
9e353e3b | 2566 | #else |
14a5cf38 JH |
2567 | NULL, |
2568 | NULL, | |
9e353e3b NIS |
2569 | #endif |
2570 | #ifdef USE_STDIO_PTR | |
14a5cf38 JH |
2571 | PerlIOStdio_get_ptr, |
2572 | PerlIOStdio_get_cnt, | |
0eb1d8a4 | 2573 | #if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT))) |
14a5cf38 JH |
2574 | PerlIOStdio_set_ptrcnt |
2575 | #else /* STDIO_PTR_LVALUE */ | |
2576 | NULL | |
2577 | #endif /* STDIO_PTR_LVALUE */ | |
2578 | #else /* USE_STDIO_PTR */ | |
2579 | NULL, | |
2580 | NULL, | |
2581 | NULL | |
2582 | #endif /* USE_STDIO_PTR */ | |
9e353e3b NIS |
2583 | }; |
2584 | ||
2585 | #undef PerlIO_exportFILE | |
2586 | FILE * | |
2587 | PerlIO_exportFILE(PerlIO *f, int fl) | |
2588 | { | |
14a5cf38 JH |
2589 | FILE *stdio; |
2590 | PerlIO_flush(f); | |
2591 | stdio = fdopen(PerlIO_fileno(f), "r+"); | |
2592 | if (stdio) { | |
2593 | dTHX; | |
2594 | PerlIOStdio *s = | |
2595 | PerlIOSelf(PerlIO_push(aTHX_ f, &PerlIO_stdio, "r+", Nullsv), | |
2596 | PerlIOStdio); | |
2597 | s->stdio = stdio; | |
2598 | } | |
2599 | return stdio; | |
9e353e3b NIS |
2600 | } |
2601 | ||
2602 | #undef PerlIO_findFILE | |
2603 | FILE * | |
2604 | PerlIO_findFILE(PerlIO *f) | |
2605 | { | |
14a5cf38 JH |
2606 | PerlIOl *l = *f; |
2607 | while (l) { | |
2608 | if (l->tab == &PerlIO_stdio) { | |
2609 | PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio); | |
2610 | return s->stdio; | |
2611 | } | |
2612 | l = *PerlIONext(&l); | |
f7e7eb72 | 2613 | } |
14a5cf38 | 2614 | return PerlIO_exportFILE(f, 0); |
9e353e3b NIS |
2615 | } |
2616 | ||
2617 | #undef PerlIO_releaseFILE | |
2618 | void | |
2619 | PerlIO_releaseFILE(PerlIO *p, FILE *f) | |
2620 | { | |
2621 | } | |
2622 | ||
2623 | /*--------------------------------------------------------------------------------------*/ | |
14a5cf38 | 2624 | /* |
71200d45 | 2625 | * perlio buffer layer |
14a5cf38 | 2626 | */ |
9e353e3b | 2627 | |
5e2ab84b | 2628 | IV |
e3f3bf95 | 2629 | PerlIOBuf_pushed(PerlIO *f, const char *mode, SV *arg) |
5e2ab84b | 2630 | { |
14a5cf38 JH |
2631 | dSYS; |
2632 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); | |
2633 | int fd = PerlIO_fileno(f); | |
2634 | Off_t posn; | |
2635 | if (fd >= 0 && PerlLIO_isatty(fd)) { | |
2636 | PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY; | |
2637 | } | |
2638 | posn = PerlIO_tell(PerlIONext(f)); | |
2639 | if (posn != (Off_t) - 1) { | |
2640 | b->posn = posn; | |
2641 | } | |
2642 | return PerlIOBase_pushed(f, mode, arg); | |
5e2ab84b NIS |
2643 | } |
2644 | ||
9e353e3b | 2645 | PerlIO * |
14a5cf38 JH |
2646 | PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers, |
2647 | IV n, const char *mode, int fd, int imode, int perm, | |
2648 | PerlIO *f, int narg, SV **args) | |
2649 | { | |
2650 | if (f) { | |
2651 | PerlIO *next = PerlIONext(f); | |
2652 | PerlIO_funcs *tab = | |
2653 | PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab); | |
2654 | next = | |
2655 | (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm, | |
2656 | next, narg, args); | |
2657 | if (!next | |
2658 | || (*PerlIOBase(f)->tab->Pushed) (f, mode, PerlIOArg) != 0) { | |
2659 | return NULL; | |
2660 | } | |
2661 | } | |
2662 | else { | |
2663 | PerlIO_funcs *tab = | |
2664 | PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm()); | |
2665 | int init = 0; | |
2666 | if (*mode == 'I') { | |
2667 | init = 1; | |
2668 | /* | |
71200d45 | 2669 | * mode++; |
14a5cf38 JH |
2670 | */ |
2671 | } | |
2672 | f = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm, | |
2673 | NULL, narg, args); | |
2674 | if (f) { | |
2675 | PerlIO_push(aTHX_ f, self, mode, PerlIOArg); | |
2676 | fd = PerlIO_fileno(f); | |
ee518936 | 2677 | #if O_BINARY != O_TEXT |
14a5cf38 | 2678 | /* |
71200d45 | 2679 | * do something about failing setmode()? --jhi |
14a5cf38 JH |
2680 | */ |
2681 | PerlLIO_setmode(fd, O_BINARY); | |
ee518936 | 2682 | #endif |
14a5cf38 JH |
2683 | if (init && fd == 2) { |
2684 | /* | |
71200d45 | 2685 | * Initial stderr is unbuffered |
14a5cf38 JH |
2686 | */ |
2687 | PerlIOBase(f)->flags |= PERLIO_F_UNBUF; | |
2688 | } | |
2689 | } | |
ee518936 | 2690 | } |
14a5cf38 | 2691 | return f; |
9e353e3b NIS |
2692 | } |
2693 | ||
14a5cf38 JH |
2694 | /* |
2695 | * This "flush" is akin to sfio's sync in that it handles files in either | |
71200d45 | 2696 | * read or write state |
14a5cf38 | 2697 | */ |
9e353e3b NIS |
2698 | IV |
2699 | PerlIOBuf_flush(PerlIO *f) | |
6f9d8c32 | 2700 | { |
14a5cf38 JH |
2701 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
2702 | int code = 0; | |
2703 | if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) { | |
2704 | /* | |
71200d45 | 2705 | * write() the buffer |
14a5cf38 JH |
2706 | */ |
2707 | STDCHAR *buf = b->buf; | |
2708 | STDCHAR *p = buf; | |
2709 | PerlIO *n = PerlIONext(f); | |
2710 | while (p < b->ptr) { | |
2711 | SSize_t count = PerlIO_write(n, p, b->ptr - p); | |
2712 | if (count > 0) { | |
2713 | p += count; | |
2714 | } | |
2715 | else if (count < 0 || PerlIO_error(n)) { | |
2716 | PerlIOBase(f)->flags |= PERLIO_F_ERROR; | |
2717 | code = -1; | |
2718 | break; | |
2719 | } | |
2720 | } | |
2721 | b->posn += (p - buf); | |
2722 | } | |
2723 | else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) { | |
2724 | STDCHAR *buf = PerlIO_get_base(f); | |
2725 | /* | |
71200d45 | 2726 | * Note position change |
14a5cf38 JH |
2727 | */ |
2728 | b->posn += (b->ptr - buf); | |
2729 | if (b->ptr < b->end) { | |
2730 | /* | |
71200d45 | 2731 | * We did not consume all of it |
14a5cf38 JH |
2732 | */ |
2733 | if (PerlIO_seek(PerlIONext(f), b->posn, SEEK_SET) == 0) { | |
2734 | b->posn = PerlIO_tell(PerlIONext(f)); | |
2735 | } | |
2736 | } | |
2737 | } | |
2738 | b->ptr = b->end = b->buf; | |
2739 | PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF); | |
2740 | /* | |
71200d45 | 2741 | * FIXME: Is this right for read case ? |
14a5cf38 JH |
2742 | */ |
2743 | if (PerlIO_flush(PerlIONext(f)) != 0) | |
2744 | code = -1; | |
2745 | return code; | |
6f9d8c32 NIS |
2746 | } |
2747 | ||
06da4f11 NIS |
2748 | IV |
2749 | PerlIOBuf_fill(PerlIO *f) | |
2750 | { | |
14a5cf38 JH |
2751 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
2752 | PerlIO *n = PerlIONext(f); | |
2753 | SSize_t avail; | |
2754 | /* | |
2755 | * FIXME: doing the down-stream flush is a bad idea if it causes | |
2756 | * pre-read data in stdio buffer to be discarded but this is too | |
2757 | * simplistic - as it skips _our_ hosekeeping and breaks tell tests. | |
71200d45 | 2758 | * if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) { } |
14a5cf38 JH |
2759 | */ |
2760 | if (PerlIO_flush(f) != 0) | |
2761 | return -1; | |
2762 | if (PerlIOBase(f)->flags & PERLIO_F_TTY) | |
2763 | PerlIOBase_flush_linebuf(); | |
2764 | ||
2765 | if (!b->buf) | |
2766 | PerlIO_get_base(f); /* allocate via vtable */ | |
2767 | ||
2768 | b->ptr = b->end = b->buf; | |
2769 | if (PerlIO_fast_gets(n)) { | |
2770 | /* | |
2771 | * Layer below is also buffered We do _NOT_ want to call its | |
2772 | * ->Read() because that will loop till it gets what we asked for | |
2773 | * which may hang on a pipe etc. Instead take anything it has to | |
71200d45 | 2774 | * hand, or ask it to fill _once_. |
14a5cf38 JH |
2775 | */ |
2776 | avail = PerlIO_get_cnt(n); | |
2777 | if (avail <= 0) { | |
2778 | avail = PerlIO_fill(n); | |
2779 | if (avail == 0) | |
2780 | avail = PerlIO_get_cnt(n); | |
2781 | else { | |
2782 | if (!PerlIO_error(n) && PerlIO_eof(n)) | |
2783 | avail = 0; | |
2784 | } | |
2785 | } | |
2786 | if (avail > 0) { | |
2787 | STDCHAR *ptr = PerlIO_get_ptr(n); | |
2788 | SSize_t cnt = avail; | |
2789 | if (avail > b->bufsiz) | |
2790 | avail = b->bufsiz; | |
2791 | Copy(ptr, b->buf, avail, STDCHAR); | |
2792 | PerlIO_set_ptrcnt(n, ptr + avail, cnt - avail); | |
2793 | } | |
2794 | } | |
2795 | else { | |
2796 | avail = PerlIO_read(n, b->ptr, b->bufsiz); | |
2797 | } | |
2798 | if (avail <= 0) { | |
2799 | if (avail == 0) | |
2800 | PerlIOBase(f)->flags |= PERLIO_F_EOF; | |
2801 | else | |
2802 | PerlIOBase(f)->flags |= PERLIO_F_ERROR; | |
2803 | return -1; | |
2804 | } | |
2805 | b->end = b->buf + avail; | |
2806 | PerlIOBase(f)->flags |= PERLIO_F_RDBUF; | |
2807 | return 0; | |
06da4f11 NIS |
2808 | } |
2809 | ||
6f9d8c32 | 2810 | SSize_t |
9e353e3b | 2811 | PerlIOBuf_read(PerlIO *f, void *vbuf, Size_t count) |
6f9d8c32 | 2812 | { |
14a5cf38 JH |
2813 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
2814 | if (f) { | |
2815 | if (!b->ptr) | |
2816 | PerlIO_get_base(f); | |
2817 | return PerlIOBase_read(f, vbuf, count); | |
2818 | } | |
2819 | return 0; | |
6f9d8c32 NIS |
2820 | } |
2821 | ||
9e353e3b NIS |
2822 | SSize_t |
2823 | PerlIOBuf_unread(PerlIO *f, const void *vbuf, Size_t count) | |
760ac839 | 2824 | { |
14a5cf38 JH |
2825 | const STDCHAR *buf = (const STDCHAR *) vbuf + count; |
2826 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); | |
2827 | SSize_t unread = 0; | |
2828 | SSize_t avail; | |
2829 | if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) | |
2830 | PerlIO_flush(f); | |
2831 | if (!b->buf) | |
2832 | PerlIO_get_base(f); | |
2833 | if (b->buf) { | |
2834 | if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) { | |
2835 | /* | |
2836 | * Buffer is already a read buffer, we can overwrite any chars | |
71200d45 | 2837 | * which have been read back to buffer start |
14a5cf38 JH |
2838 | */ |
2839 | avail = (b->ptr - b->buf); | |
2840 | } | |
2841 | else { | |
2842 | /* | |
2843 | * Buffer is idle, set it up so whole buffer is available for | |
71200d45 | 2844 | * unread |
14a5cf38 JH |
2845 | */ |
2846 | avail = b->bufsiz; | |
2847 | b->end = b->buf + avail; | |
2848 | b->ptr = b->end; | |
2849 | PerlIOBase(f)->flags |= PERLIO_F_RDBUF; | |
2850 | /* | |
71200d45 | 2851 | * Buffer extends _back_ from where we are now |
14a5cf38 JH |
2852 | */ |
2853 | b->posn -= b->bufsiz; | |
2854 | } | |
2855 | if (avail > (SSize_t) count) { | |
2856 | /* | |
71200d45 | 2857 | * If we have space for more than count, just move count |
14a5cf38 JH |
2858 | */ |
2859 | avail = count; | |
2860 | } | |
2861 | if (avail > 0) { | |
2862 | b->ptr -= avail; | |
2863 | buf -= avail; | |
2864 | /* | |
2865 | * In simple stdio-like ungetc() case chars will be already | |
71200d45 | 2866 | * there |
14a5cf38 JH |
2867 | */ |
2868 | if (buf != b->ptr) { | |
2869 | Copy(buf, b->ptr, avail, STDCHAR); | |
2870 | } | |
2871 | count -= avail; | |
2872 | unread += avail; | |
2873 | PerlIOBase(f)->flags &= ~PERLIO_F_EOF; | |
2874 | } | |
2875 | } | |
2876 | return unread; | |
760ac839 LW |
2877 | } |
2878 | ||
9e353e3b NIS |
2879 | SSize_t |
2880 | PerlIOBuf_write(PerlIO *f, const void *vbuf, Size_t count) | |
760ac839 | 2881 | { |
14a5cf38 JH |
2882 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
2883 | const STDCHAR *buf = (const STDCHAR *) vbuf; | |
2884 | Size_t written = 0; | |
2885 | if (!b->buf) | |
2886 | PerlIO_get_base(f); | |
2887 | if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) | |
2888 | return 0; | |
2889 | while (count > 0) { | |
2890 | SSize_t avail = b->bufsiz - (b->ptr - b->buf); | |
2891 | if ((SSize_t) count < avail) | |
2892 | avail = count; | |
2893 | PerlIOBase(f)->flags |= PERLIO_F_WRBUF; | |
2894 | if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) { | |
2895 | while (avail > 0) { | |
2896 | int ch = *buf++; | |
2897 | *(b->ptr)++ = ch; | |
2898 | count--; | |
2899 | avail--; | |
2900 | written++; | |
2901 | if (ch == '\n') { | |
2902 | PerlIO_flush(f); | |
2903 | break; | |
2904 | } | |
2905 | } | |
2906 | } | |
2907 | else { | |
2908 | if (avail) { | |
2909 | Copy(buf, b->ptr, avail, STDCHAR); | |
2910 | count -= avail; | |
2911 | buf += avail; | |
2912 | written += avail; | |
2913 | b->ptr += avail; | |
2914 | } | |
2915 | } | |
2916 | if (b->ptr >= (b->buf + b->bufsiz)) | |
2917 | PerlIO_flush(f); | |
2918 | } | |
2919 | if (PerlIOBase(f)->flags & PERLIO_F_UNBUF) | |
2920 | PerlIO_flush(f); | |
2921 | return written; | |
9e353e3b NIS |
2922 | } |
2923 | ||
2924 | IV | |
2925 | PerlIOBuf_seek(PerlIO *f, Off_t offset, int whence) | |
2926 | { | |
14a5cf38 JH |
2927 | IV code; |
2928 | if ((code = PerlIO_flush(f)) == 0) { | |
2929 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); | |
2930 | PerlIOBase(f)->flags &= ~PERLIO_F_EOF; | |
2931 | code = PerlIO_seek(PerlIONext(f), offset, whence); | |
2932 | if (code == 0) { | |
2933 | b->posn = PerlIO_tell(PerlIONext(f)); | |
2934 | } | |
9e353e3b | 2935 | } |
14a5cf38 | 2936 | return code; |
9e353e3b NIS |
2937 | } |
2938 | ||
2939 | Off_t | |
2940 | PerlIOBuf_tell(PerlIO *f) | |
2941 | { | |
14a5cf38 JH |
2942 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
2943 | /* | |
71200d45 | 2944 | * b->posn is file position where b->buf was read, or will be written |
14a5cf38 JH |
2945 | */ |
2946 | Off_t posn = b->posn; | |
2947 | if (b->buf) { | |
2948 | /* | |
71200d45 | 2949 | * If buffer is valid adjust position by amount in buffer |
14a5cf38 JH |
2950 | */ |
2951 | posn += (b->ptr - b->buf); | |
2952 | } | |
2953 | return posn; | |
9e353e3b NIS |
2954 | } |
2955 | ||
2956 | IV | |
2957 | PerlIOBuf_close(PerlIO *f) | |
2958 | { | |
14a5cf38 JH |
2959 | IV code = PerlIOBase_close(f); |
2960 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); | |
2961 | if (b->buf && b->buf != (STDCHAR *) & b->oneword) { | |
2962 | PerlMemShared_free(b->buf); | |
2963 | } | |
2964 | b->buf = NULL; | |
2965 | b->ptr = b->end = b->buf; | |
2966 | PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF); | |
2967 | return code; | |
760ac839 LW |
2968 | } |
2969 | ||
9e353e3b NIS |
2970 | STDCHAR * |
2971 | PerlIOBuf_get_ptr(PerlIO *f) | |
2972 | { | |
14a5cf38 JH |
2973 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
2974 | if (!b->buf) | |
2975 | PerlIO_get_base(f); | |
2976 | return b->ptr; | |
9e353e3b NIS |
2977 | } |
2978 | ||
05d1247b | 2979 | SSize_t |
9e353e3b NIS |
2980 | PerlIOBuf_get_cnt(PerlIO *f) |
2981 | { | |
14a5cf38 JH |
2982 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
2983 | if (!b->buf) | |
2984 | PerlIO_get_base(f); | |
2985 | if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) | |
2986 | return (b->end - b->ptr); | |
2987 | return 0; | |
9e353e3b NIS |
2988 | } |
2989 | ||
2990 | STDCHAR * | |
2991 | PerlIOBuf_get_base(PerlIO *f) | |
2992 | { | |
14a5cf38 JH |
2993 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
2994 | if (!b->buf) { | |
2995 | if (!b->bufsiz) | |
2996 | b->bufsiz = 4096; | |
2997 | b->buf = PerlMemShared_calloc(b->bufsiz, sizeof(STDCHAR)); | |
2998 | if (!b->buf) { | |
2999 | b->buf = (STDCHAR *) & b->oneword; | |
3000 | b->bufsiz = sizeof(b->oneword); | |
3001 | } | |
3002 | b->ptr = b->buf; | |
3003 | b->end = b->ptr; | |
06da4f11 | 3004 | } |
14a5cf38 | 3005 | return b->buf; |
9e353e3b NIS |
3006 | } |
3007 | ||
3008 | Size_t | |
3009 | PerlIOBuf_bufsiz(PerlIO *f) | |
3010 | { | |
14a5cf38 JH |
3011 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3012 | if (!b->buf) | |
3013 | PerlIO_get_base(f); | |
3014 | return (b->end - b->buf); | |
9e353e3b NIS |
3015 | } |
3016 | ||
3017 | void | |
14a5cf38 | 3018 | PerlIOBuf_set_ptrcnt(PerlIO *f, STDCHAR * ptr, SSize_t cnt) |
9e353e3b | 3019 | { |
14a5cf38 JH |
3020 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3021 | if (!b->buf) | |
3022 | PerlIO_get_base(f); | |
3023 | b->ptr = ptr; | |
3024 | if (PerlIO_get_cnt(f) != cnt || b->ptr < b->buf) { | |
3025 | dTHX; | |
3026 | assert(PerlIO_get_cnt(f) == cnt); | |
3027 | assert(b->ptr >= b->buf); | |
3028 | } | |
3029 | PerlIOBase(f)->flags |= PERLIO_F_RDBUF; | |
760ac839 LW |
3030 | } |
3031 | ||
71200d45 | 3032 | PerlIO * |
8cf8f3d1 | 3033 | PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param) |
71200d45 | 3034 | { |
a8fc9800 | 3035 | return PerlIOBase_dup(aTHX_ f, o, param); |
71200d45 NIS |
3036 | } |
3037 | ||
3038 | ||
3039 | ||
9e353e3b | 3040 | PerlIO_funcs PerlIO_perlio = { |
14a5cf38 JH |
3041 | "perlio", |
3042 | sizeof(PerlIOBuf), | |
3043 | PERLIO_K_BUFFERED, | |
3044 | PerlIOBuf_pushed, | |
3045 | PerlIOBase_noop_ok, | |
3046 | PerlIOBuf_open, | |
3047 | NULL, | |
3048 | PerlIOBase_fileno, | |
71200d45 | 3049 | PerlIOBuf_dup, |
14a5cf38 JH |
3050 | PerlIOBuf_read, |
3051 | PerlIOBuf_unread, | |
3052 | PerlIOBuf_write, | |
3053 | PerlIOBuf_seek, | |
3054 | PerlIOBuf_tell, | |
3055 | PerlIOBuf_close, | |
3056 | PerlIOBuf_flush, | |
3057 | PerlIOBuf_fill, | |
3058 | PerlIOBase_eof, | |
3059 | PerlIOBase_error, | |
3060 | PerlIOBase_clearerr, | |
3061 | PerlIOBase_setlinebuf, | |
3062 | PerlIOBuf_get_base, | |
3063 | PerlIOBuf_bufsiz, | |
3064 | PerlIOBuf_get_ptr, | |
3065 | PerlIOBuf_get_cnt, | |
3066 | PerlIOBuf_set_ptrcnt, | |
9e353e3b NIS |
3067 | }; |
3068 | ||
66ecd56b | 3069 | /*--------------------------------------------------------------------------------------*/ |
14a5cf38 | 3070 | /* |
71200d45 | 3071 | * Temp layer to hold unread chars when cannot do it any other way |
14a5cf38 | 3072 | */ |
5e2ab84b NIS |
3073 | |
3074 | IV | |
3075 | PerlIOPending_fill(PerlIO *f) | |
3076 | { | |
14a5cf38 | 3077 | /* |
71200d45 | 3078 | * Should never happen |
14a5cf38 JH |
3079 | */ |
3080 | PerlIO_flush(f); | |
3081 | return 0; | |
5e2ab84b NIS |
3082 | } |
3083 | ||
3084 | IV | |
3085 | PerlIOPending_close(PerlIO *f) | |
3086 | { | |
14a5cf38 | 3087 | /* |
71200d45 | 3088 | * A tad tricky - flush pops us, then we close new top |
14a5cf38 JH |
3089 | */ |
3090 | PerlIO_flush(f); | |
3091 | return PerlIO_close(f); | |
5e2ab84b NIS |
3092 | } |
3093 | ||
3094 | IV | |
3095 | PerlIOPending_seek(PerlIO *f, Off_t offset, int whence) | |
3096 | { | |
14a5cf38 | 3097 | /* |
71200d45 | 3098 | * A tad tricky - flush pops us, then we seek new top |
14a5cf38 JH |
3099 | */ |
3100 | PerlIO_flush(f); | |
3101 | return PerlIO_seek(f, offset, whence); | |
5e2ab84b NIS |
3102 | } |
3103 | ||
3104 | ||
3105 | IV | |
3106 | PerlIOPending_flush(PerlIO *f) | |
3107 | { | |
14a5cf38 JH |
3108 | dTHX; |
3109 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); | |
3110 | if (b->buf && b->buf != (STDCHAR *) & b->oneword) { | |
3111 | PerlMemShared_free(b->buf); | |
3112 | b->buf = NULL; | |
3113 | } | |
3114 | PerlIO_pop(aTHX_ f); | |
3115 | return 0; | |
5e2ab84b NIS |
3116 | } |
3117 | ||
3118 | void | |
14a5cf38 | 3119 | PerlIOPending_set_ptrcnt(PerlIO *f, STDCHAR * ptr, SSize_t cnt) |
5e2ab84b | 3120 | { |
14a5cf38 JH |
3121 | if (cnt <= 0) { |
3122 | PerlIO_flush(f); | |
3123 | } | |
3124 | else { | |
3125 | PerlIOBuf_set_ptrcnt(f, ptr, cnt); | |
3126 | } | |
5e2ab84b NIS |
3127 | } |
3128 | ||
3129 | IV | |
14a5cf38 | 3130 | PerlIOPending_pushed(PerlIO *f, const char *mode, SV *arg) |
5e2ab84b | 3131 | { |
14a5cf38 JH |
3132 | IV code = PerlIOBase_pushed(f, mode, arg); |
3133 | PerlIOl *l = PerlIOBase(f); | |
3134 | /* | |
71200d45 NIS |
3135 | * Our PerlIO_fast_gets must match what we are pushed on, or sv_gets() |
3136 | * etc. get muddled when it changes mid-string when we auto-pop. | |
14a5cf38 JH |
3137 | */ |
3138 | l->flags = (l->flags & ~(PERLIO_F_FASTGETS | PERLIO_F_UTF8)) | | |
3139 | (PerlIOBase(PerlIONext(f))-> | |
3140 | flags & (PERLIO_F_FASTGETS | PERLIO_F_UTF8)); | |
3141 | return code; | |
5e2ab84b NIS |
3142 | } |
3143 | ||
3144 | SSize_t | |
3145 | PerlIOPending_read(PerlIO *f, void *vbuf, Size_t count) | |
3146 | { | |
14a5cf38 JH |
3147 | SSize_t avail = PerlIO_get_cnt(f); |
3148 | SSize_t got = 0; | |
3149 | if (count < avail) | |
3150 | avail = count; | |
3151 | if (avail > 0) | |
3152 | got = PerlIOBuf_read(f, vbuf, avail); | |
3153 | if (got >= 0 && got < count) { | |
3154 | SSize_t more = | |
3155 | PerlIO_read(f, ((STDCHAR *) vbuf) + got, count - got); | |
3156 | if (more >= 0 || got == 0) | |
3157 | got += more; | |
3158 | } | |
3159 | return got; | |
5e2ab84b NIS |
3160 | } |
3161 | ||
5e2ab84b | 3162 | PerlIO_funcs PerlIO_pending = { |
14a5cf38 JH |
3163 | "pending", |
3164 | sizeof(PerlIOBuf), | |
3165 | PERLIO_K_BUFFERED, | |
3166 | PerlIOPending_pushed, | |
3167 | PerlIOBase_noop_ok, | |
3168 | NULL, | |
3169 | NULL, | |
3170 | PerlIOBase_fileno, | |
71200d45 | 3171 | PerlIOBuf_dup, |
14a5cf38 JH |
3172 | PerlIOPending_read, |
3173 | PerlIOBuf_unread, | |
3174 | PerlIOBuf_write, | |
3175 | PerlIOPending_seek, | |
3176 | PerlIOBuf_tell, | |
3177 | PerlIOPending_close, | |
3178 | PerlIOPending_flush, | |
3179 | PerlIOPending_fill, | |
3180 | PerlIOBase_eof, | |
3181 | PerlIOBase_error, | |
3182 | PerlIOBase_clearerr, | |
3183 | PerlIOBase_setlinebuf, | |
3184 | PerlIOBuf_get_base, | |
3185 | PerlIOBuf_bufsiz, | |
3186 | PerlIOBuf_get_ptr, | |
3187 | PerlIOBuf_get_cnt, | |
3188 | PerlIOPending_set_ptrcnt, | |
5e2ab84b NIS |
3189 | }; |
3190 | ||
3191 | ||
3192 | ||
3193 | /*--------------------------------------------------------------------------------------*/ | |
14a5cf38 JH |
3194 | /* |
3195 | * crlf - translation On read translate CR,LF to "\n" we do this by | |
3196 | * overriding ptr/cnt entries to hand back a line at a time and keeping a | |
71200d45 | 3197 | * record of which nl we "lied" about. On write translate "\n" to CR,LF |
66ecd56b NIS |
3198 | */ |
3199 | ||
14a5cf38 JH |
3200 | typedef struct { |
3201 | PerlIOBuf base; /* PerlIOBuf stuff */ | |
71200d45 | 3202 | STDCHAR *nl; /* Position of crlf we "lied" about in the |
14a5cf38 | 3203 | * buffer */ |
99efab12 NIS |
3204 | } PerlIOCrlf; |
3205 | ||
f5b9d040 | 3206 | IV |
14a5cf38 | 3207 | PerlIOCrlf_pushed(PerlIO *f, const char *mode, SV *arg) |
f5b9d040 | 3208 | { |
14a5cf38 JH |
3209 | IV code; |
3210 | PerlIOBase(f)->flags |= PERLIO_F_CRLF; | |
3211 | code = PerlIOBuf_pushed(f, mode, arg); | |
5e2ab84b | 3212 | #if 0 |
14a5cf38 JH |
3213 | PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08" UVxf "\n", |
3214 | f, PerlIOBase(f)->tab->name, (mode) ? mode : "(Null)", | |
3215 | PerlIOBase(f)->flags); | |
5e2ab84b | 3216 | #endif |
14a5cf38 | 3217 | return code; |
f5b9d040 NIS |
3218 | } |
3219 | ||
3220 | ||
99efab12 NIS |
3221 | SSize_t |
3222 | PerlIOCrlf_unread(PerlIO *f, const void *vbuf, Size_t count) | |
3223 | { | |
14a5cf38 JH |
3224 | PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf); |
3225 | if (c->nl) { | |
3226 | *(c->nl) = 0xd; | |
3227 | c->nl = NULL; | |
3228 | } | |
3229 | if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) | |
3230 | return PerlIOBuf_unread(f, vbuf, count); | |
3231 | else { | |
3232 | const STDCHAR *buf = (const STDCHAR *) vbuf + count; | |
3233 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); | |
3234 | SSize_t unread = 0; | |
3235 | if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) | |
3236 | PerlIO_flush(f); | |
3237 | if (!b->buf) | |
3238 | PerlIO_get_base(f); | |
3239 | if (b->buf) { | |
3240 | if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) { | |
3241 | b->end = b->ptr = b->buf + b->bufsiz; | |
3242 | PerlIOBase(f)->flags |= PERLIO_F_RDBUF; | |
3243 | b->posn -= b->bufsiz; | |
3244 | } | |
3245 | while (count > 0 && b->ptr > b->buf) { | |
3246 | int ch = *--buf; | |
3247 | if (ch == '\n') { | |
3248 | if (b->ptr - 2 >= b->buf) { | |
3249 | *--(b->ptr) = 0xa; | |
3250 | *--(b->ptr) = 0xd; | |
3251 | unread++; | |
3252 | count--; | |
3253 | } | |
3254 | else { | |
3255 | buf++; | |
3256 | break; | |
3257 | } | |
3258 | } | |
3259 | else { | |
3260 | *--(b->ptr) = ch; | |
3261 | unread++; | |
3262 | count--; | |
3263 | } | |
3264 | } | |
3265 | } | |
3266 | return unread; | |
3267 | } | |
99efab12 NIS |
3268 | } |
3269 | ||
3270 | SSize_t | |
3271 | PerlIOCrlf_get_cnt(PerlIO *f) | |
3272 | { | |
14a5cf38 JH |
3273 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3274 | if (!b->buf) | |
3275 | PerlIO_get_base(f); | |
3276 | if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) { | |
3277 | PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf); | |
3278 | if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && !c->nl) { | |
3279 | STDCHAR *nl = b->ptr; | |
3280 | scan: | |
3281 | while (nl < b->end && *nl != 0xd) | |
3282 | nl++; | |
3283 | if (nl < b->end && *nl == 0xd) { | |
3284 | test: | |
3285 | if (nl + 1 < b->end) { | |
3286 | if (nl[1] == 0xa) { | |
3287 | *nl = '\n'; | |
3288 | c->nl = nl; | |
3289 | } | |
3290 | else { | |
3291 | /* | |
71200d45 | 3292 | * Not CR,LF but just CR |
14a5cf38 JH |
3293 | */ |
3294 | nl++; | |
3295 | goto scan; | |
3296 | } | |
3297 | } | |
3298 | else { | |
3299 | /* | |
71200d45 | 3300 | * Blast - found CR as last char in buffer |
14a5cf38 JH |
3301 | */ |
3302 | if (b->ptr < nl) { | |
3303 | /* | |
3304 | * They may not care, defer work as long as | |
71200d45 | 3305 | * possible |
14a5cf38 JH |
3306 | */ |
3307 | return (nl - b->ptr); | |
3308 | } | |
3309 | else { | |
3310 | int code; | |
3311 | b->ptr++; /* say we have read it as far as | |
3312 | * flush() is concerned */ | |
3313 | b->buf++; /* Leave space an front of buffer */ | |
3314 | b->bufsiz--; /* Buffer is thus smaller */ | |
3315 | code = PerlIO_fill(f); /* Fetch some more */ | |
3316 | b->bufsiz++; /* Restore size for next time */ | |
3317 | b->buf--; /* Point at space */ | |
3318 | b->ptr = nl = b->buf; /* Which is what we hand | |
3319 | * off */ | |
3320 | b->posn--; /* Buffer starts here */ | |
3321 | *nl = 0xd; /* Fill in the CR */ | |
3322 | if (code == 0) | |
3323 | goto test; /* fill() call worked */ | |
3324 | /* | |
71200d45 | 3325 | * CR at EOF - just fall through |
14a5cf38 JH |
3326 | */ |
3327 | } | |
3328 | } | |
3329 | } | |
3330 | } | |
3331 | return (((c->nl) ? (c->nl + 1) : b->end) - b->ptr); | |
3332 | } | |
3333 | return 0; | |
99efab12 NIS |
3334 | } |
3335 | ||
3336 | void | |
14a5cf38 JH |
3337 | PerlIOCrlf_set_ptrcnt(PerlIO *f, STDCHAR * ptr, SSize_t cnt) |
3338 | { | |
3339 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); | |
3340 | PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf); | |
3341 | IV flags = PerlIOBase(f)->flags; | |
3342 | if (!b->buf) | |
3343 | PerlIO_get_base(f); | |
3344 | if (!ptr) { | |
3345 | if (c->nl) | |
3346 | ptr = c->nl + 1; | |
3347 | else { | |
3348 | ptr = b->end; | |
3349 | if ((flags & PERLIO_F_CRLF) && ptr > b->buf && ptr[-1] == 0xd) | |
3350 | ptr--; | |
3351 | } | |
3352 | ptr -= cnt; | |
3353 | } | |
3354 | else { | |
3355 | /* | |
71200d45 | 3356 | * Test code - delete when it works ... |
14a5cf38 JH |
3357 | */ |
3358 | STDCHAR *chk; | |
3359 | if (c->nl) | |
3360 | chk = c->nl + 1; | |
3361 | else { | |
3362 | chk = b->end; | |
3363 | if ((flags & PERLIO_F_CRLF) && chk > b->buf && chk[-1] == 0xd) | |
3364 | chk--; | |
3365 | } | |
3366 | chk -= cnt; | |
3367 | ||
3368 | if (ptr != chk) { | |
3369 | dTHX; | |
3370 | Perl_croak(aTHX_ "ptr wrong %p != %p fl=%08" UVxf | |
3371 | " nl=%p e=%p for %d", ptr, chk, flags, c->nl, | |
3372 | b->end, cnt); | |
3373 | } | |
3374 | } | |
3375 | if (c->nl) { | |
3376 | if (ptr > c->nl) { | |
3377 | /* | |
71200d45 | 3378 | * They have taken what we lied about |
14a5cf38 JH |
3379 | */ |
3380 | *(c->nl) = 0xd; | |
3381 | c->nl = NULL; | |
3382 | ptr++; | |
3383 | } | |
3384 | } | |
3385 | b->ptr = ptr; | |
3386 | PerlIOBase(f)->flags |= PERLIO_F_RDBUF; | |
99efab12 NIS |
3387 | } |
3388 | ||
3389 | SSize_t | |
3390 | PerlIOCrlf_write(PerlIO *f, const void *vbuf, Size_t count) | |
3391 | { | |
14a5cf38 JH |
3392 | if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) |
3393 | return PerlIOBuf_write(f, vbuf, count); | |
3394 | else { | |
3395 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); | |
3396 | const STDCHAR *buf = (const STDCHAR *) vbuf; | |
3397 | const STDCHAR *ebuf = buf + count; | |
3398 | if (!b->buf) | |
3399 | PerlIO_get_base(f); | |
3400 | if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) | |
3401 | return 0; | |
3402 | while (buf < ebuf) { | |
3403 | STDCHAR *eptr = b->buf + b->bufsiz; | |
3404 | PerlIOBase(f)->flags |= PERLIO_F_WRBUF; | |
3405 | while (buf < ebuf && b->ptr < eptr) { | |
3406 | if (*buf == '\n') { | |
3407 | if ((b->ptr + 2) > eptr) { | |
3408 | /* | |
71200d45 | 3409 | * Not room for both |
14a5cf38 JH |
3410 | */ |
3411 | PerlIO_flush(f); | |
3412 | break; | |
3413 | } | |
3414 | else { | |
3415 | *(b->ptr)++ = 0xd; /* CR */ | |
3416 | *(b->ptr)++ = 0xa; /* LF */ | |
3417 | buf++; | |
3418 | if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) { | |
3419 | PerlIO_flush(f); | |
3420 | break; | |
3421 | } | |
3422 | } | |
3423 | } | |
3424 | else { | |
3425 | int ch = *buf++; | |
3426 | *(b->ptr)++ = ch; | |
3427 | } | |
3428 | if (b->ptr >= eptr) { | |
3429 | PerlIO_flush(f); | |
3430 | break; | |
3431 | } | |
3432 | } | |
3433 | } | |
3434 | if (PerlIOBase(f)->flags & PERLIO_F_UNBUF) | |
3435 | PerlIO_flush(f); | |
3436 | return (buf - (STDCHAR *) vbuf); | |
3437 | } | |
99efab12 NIS |
3438 | } |
3439 | ||
3440 | IV | |
3441 | PerlIOCrlf_flush(PerlIO *f) | |
3442 | { | |
14a5cf38 JH |
3443 | PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf); |
3444 | if (c->nl) { | |
3445 | *(c->nl) = 0xd; | |
3446 | c->nl = NULL; | |
3447 | } | |
3448 | return PerlIOBuf_flush(f); | |
99efab12 NIS |
3449 | } |
3450 | ||
66ecd56b | 3451 | PerlIO_funcs PerlIO_crlf = { |
14a5cf38 JH |
3452 | "crlf", |
3453 | sizeof(PerlIOCrlf), | |
3454 | PERLIO_K_BUFFERED | PERLIO_K_CANCRLF, | |
3455 | PerlIOCrlf_pushed, | |
3456 | PerlIOBase_noop_ok, /* popped */ | |
3457 | PerlIOBuf_open, | |
3458 | NULL, | |
3459 | PerlIOBase_fileno, | |
71200d45 | 3460 | PerlIOBuf_dup, |
14a5cf38 JH |
3461 | PerlIOBuf_read, /* generic read works with ptr/cnt lies |
3462 | * ... */ | |
3463 | PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */ | |
3464 | PerlIOCrlf_write, /* Put CR,LF in buffer for each '\n' */ | |
3465 | PerlIOBuf_seek, | |
3466 | PerlIOBuf_tell, | |
3467 | PerlIOBuf_close, | |
3468 | PerlIOCrlf_flush, | |
3469 | PerlIOBuf_fill, | |
3470 | PerlIOBase_eof, | |
3471 | PerlIOBase_error, | |
3472 | PerlIOBase_clearerr, | |
3473 | PerlIOBase_setlinebuf, | |
3474 | PerlIOBuf_get_base, | |
3475 | PerlIOBuf_bufsiz, | |
3476 | PerlIOBuf_get_ptr, | |
3477 | PerlIOCrlf_get_cnt, | |
3478 | PerlIOCrlf_set_ptrcnt, | |
66ecd56b NIS |
3479 | }; |
3480 | ||
06da4f11 NIS |
3481 | #ifdef HAS_MMAP |
3482 | /*--------------------------------------------------------------------------------------*/ | |
14a5cf38 | 3483 | /* |
71200d45 | 3484 | * mmap as "buffer" layer |
14a5cf38 | 3485 | */ |
06da4f11 | 3486 | |
14a5cf38 JH |
3487 | typedef struct { |
3488 | PerlIOBuf base; /* PerlIOBuf stuff */ | |
3489 | Mmap_t mptr; /* Mapped address */ | |
3490 | Size_t len; /* mapped length */ | |
3491 | STDCHAR *bbuf; /* malloced buffer if map fails */ | |
06da4f11 NIS |
3492 | } PerlIOMmap; |
3493 | ||
c3d7c7c9 NIS |
3494 | static size_t page_size = 0; |
3495 | ||
06da4f11 NIS |
3496 | IV |
3497 | PerlIOMmap_map(PerlIO *f) | |
3498 | { | |
14a5cf38 JH |
3499 | dTHX; |
3500 | PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap); | |
3501 | IV flags = PerlIOBase(f)->flags; | |
3502 | IV code = 0; | |
3503 | if (m->len) | |
3504 | abort(); | |
3505 | if (flags & PERLIO_F_CANREAD) { | |
3506 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); | |
3507 | int fd = PerlIO_fileno(f); | |
3508 | struct stat st; | |
3509 | code = fstat(fd, &st); | |
3510 | if (code == 0 && S_ISREG(st.st_mode)) { | |
3511 | SSize_t len = st.st_size - b->posn; | |
3512 | if (len > 0) { | |
3513 | Off_t posn; | |
3514 | if (!page_size) { | |
68d873c6 | 3515 | #if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_PAGE_SIZE)) |
14a5cf38 JH |
3516 | { |
3517 | SETERRNO(0, SS$_NORMAL); | |
68d873c6 | 3518 | # ifdef _SC_PAGESIZE |
14a5cf38 | 3519 | page_size = sysconf(_SC_PAGESIZE); |
68d873c6 | 3520 | # else |
14a5cf38 | 3521 | page_size = sysconf(_SC_PAGE_SIZE); |
14aaf8e8 | 3522 | # endif |
14a5cf38 JH |
3523 | if ((long) page_size < 0) { |
3524 | if (errno) { | |
3525 | SV *error = ERRSV; | |
3526 | char *msg; | |
3527 | STRLEN n_a; | |
3528 | (void) SvUPGRADE(error, SVt_PV); | |
3529 | msg = SvPVx(error, n_a); | |
3530 | Perl_croak(aTHX_ "panic: sysconf: %s", | |
3531 | msg); | |
3532 | } | |
3533 | else | |
3534 | Perl_croak(aTHX_ | |
3535 | "panic: sysconf: pagesize unknown"); | |
3536 | } | |
3537 | } | |
68d873c6 JH |
3538 | #else |
3539 | # ifdef HAS_GETPAGESIZE | |
14a5cf38 | 3540 | page_size = getpagesize(); |
68d873c6 JH |
3541 | # else |
3542 | # if defined(I_SYS_PARAM) && defined(PAGESIZE) | |
14a5cf38 | 3543 | page_size = PAGESIZE; /* compiletime, bad */ |
68d873c6 JH |
3544 | # endif |
3545 | # endif | |
3546 | #endif | |
14a5cf38 JH |
3547 | if ((IV) page_size <= 0) |
3548 | Perl_croak(aTHX_ "panic: bad pagesize %" IVdf, | |
3549 | (IV) page_size); | |
3550 | } | |
3551 | if (b->posn < 0) { | |
3552 | /* | |
3553 | * This is a hack - should never happen - open should | |
71200d45 | 3554 | * have set it ! |
14a5cf38 JH |
3555 | */ |
3556 | b->posn = PerlIO_tell(PerlIONext(f)); | |
3557 | } | |
3558 | posn = (b->posn / page_size) * page_size; | |
3559 | len = st.st_size - posn; | |
3560 | m->mptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, posn); | |
3561 | if (m->mptr && m->mptr != (Mmap_t) - 1) { | |
a5262162 | 3562 | #if 0 && defined(HAS_MADVISE) && defined(MADV_SEQUENTIAL) |
14a5cf38 | 3563 | madvise(m->mptr, len, MADV_SEQUENTIAL); |
06da4f11 | 3564 | #endif |
a5262162 | 3565 | #if 0 && defined(HAS_MADVISE) && defined(MADV_WILLNEED) |
14a5cf38 | 3566 | madvise(m->mptr, len, MADV_WILLNEED); |
a5262162 | 3567 | #endif |
14a5cf38 JH |
3568 | PerlIOBase(f)->flags = |
3569 | (flags & ~PERLIO_F_EOF) | PERLIO_F_RDBUF; | |
3570 | b->end = ((STDCHAR *) m->mptr) + len; | |
3571 | b->buf = ((STDCHAR *) m->mptr) + (b->posn - posn); | |
3572 | b->ptr = b->buf; | |
3573 | m->len = len; | |
3574 | } | |
3575 | else { | |
3576 | b->buf = NULL; | |
3577 | } | |
3578 | } | |
3579 | else { | |
3580 | PerlIOBase(f)->flags = | |
3581 | flags | PERLIO_F_EOF | PERLIO_F_RDBUF; | |
3582 | b->buf = NULL; | |
3583 | b->ptr = b->end = b->ptr; | |
3584 | code = -1; | |
3585 | } | |
3586 | } | |
3587 | } | |
3588 | return code; | |
06da4f11 NIS |
3589 | } |
3590 | ||
3591 | IV | |
3592 | PerlIOMmap_unmap(PerlIO *f) | |
3593 | { | |
14a5cf38 JH |
3594 | PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap); |
3595 | PerlIOBuf *b = &m->base; | |
3596 | IV code = 0; | |
3597 | if (m->len) { | |
3598 | if (b->buf) { | |
3599 | code = munmap(m->mptr, m->len); | |
3600 | b->buf = NULL; | |
3601 | m->len = 0; | |
3602 | m->mptr = NULL; | |
3603 | if (PerlIO_seek(PerlIONext(f), b->posn, SEEK_SET) != 0) | |
3604 | code = -1; | |
3605 | } | |
3606 | b->ptr = b->end = b->buf; | |
3607 | PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF); | |
3608 | } | |
3609 | return code; | |
06da4f11 NIS |
3610 | } |
3611 | ||
3612 | STDCHAR * | |
3613 | PerlIOMmap_get_base(PerlIO *f) | |
3614 | { | |
14a5cf38 JH |
3615 | PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap); |
3616 | PerlIOBuf *b = &m->base; | |
3617 | if (b->buf && (PerlIOBase(f)->flags & PERLIO_F_RDBUF)) { | |
3618 | /* | |
71200d45 | 3619 | * Already have a readbuffer in progress |
14a5cf38 JH |
3620 | */ |
3621 | return b->buf; | |
3622 | } | |
3623 | if (b->buf) { | |
3624 | /* | |
71200d45 | 3625 | * We have a write buffer or flushed PerlIOBuf read buffer |
14a5cf38 JH |
3626 | */ |
3627 | m->bbuf = b->buf; /* save it in case we need it again */ | |
3628 | b->buf = NULL; /* Clear to trigger below */ | |
3629 | } | |
3630 | if (!b->buf) { | |
3631 | PerlIOMmap_map(f); /* Try and map it */ | |
3632 | if (!b->buf) { | |
3633 | /* | |
71200d45 | 3634 | * Map did not work - recover PerlIOBuf buffer if we have one |
14a5cf38 JH |
3635 | */ |
3636 | b->buf = m->bbuf; | |
3637 | } | |
3638 | } | |
3639 | b->ptr = b->end = b->buf; | |
3640 | if (b->buf) | |
3641 | return b->buf; | |
3642 | return PerlIOBuf_get_base(f); | |
06da4f11 NIS |
3643 | } |
3644 | ||
3645 | SSize_t | |
3646 | PerlIOMmap_unread(PerlIO *f, const void *vbuf, Size_t count) | |
3647 | { | |
14a5cf38 JH |
3648 | PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap); |
3649 | PerlIOBuf *b = &m->base; | |
3650 | if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) | |
3651 | PerlIO_flush(f); | |
3652 | if (b->ptr && (b->ptr - count) >= b->buf | |
3653 | && memEQ(b->ptr - count, vbuf, count)) { | |
3654 | b->ptr -= count; | |
3655 | PerlIOBase(f)->flags &= ~PERLIO_F_EOF; | |
3656 | return count; | |
3657 | } | |
3658 | if (m->len) { | |
3659 | /* | |
71200d45 | 3660 | * Loose the unwritable mapped buffer |
14a5cf38 JH |
3661 | */ |
3662 | PerlIO_flush(f); | |
3663 | /* | |
71200d45 | 3664 | * If flush took the "buffer" see if we have one from before |
14a5cf38 JH |
3665 | */ |
3666 | if (!b->buf && m->bbuf) | |
3667 | b->buf = m->bbuf; | |
3668 | if (!b->buf) { | |
3669 | PerlIOBuf_get_base(f); | |
3670 | m->bbuf = b->buf; | |
3671 | } | |
3672 | } | |
3673 | return PerlIOBuf_unread(f, vbuf, count); | |
06da4f11 NIS |
3674 | } |
3675 | ||
3676 | SSize_t | |
3677 | PerlIOMmap_write(PerlIO *f, const void *vbuf, Size_t count) | |
3678 | { | |
14a5cf38 JH |
3679 | PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap); |
3680 | PerlIOBuf *b = &m->base; | |
3681 | if (!b->buf || !(PerlIOBase(f)->flags & PERLIO_F_WRBUF)) { | |
3682 | /* | |
71200d45 | 3683 | * No, or wrong sort of, buffer |
14a5cf38 JH |
3684 | */ |
3685 | if (m->len) { | |
3686 | if (PerlIOMmap_unmap(f) != 0) | |
3687 | return 0; | |
3688 | } | |
3689 | /* | |
71200d45 | 3690 | * If unmap took the "buffer" see if we have one from before |
14a5cf38 JH |
3691 | */ |
3692 | if (!b->buf && m->bbuf) | |
3693 | b->buf = m->bbuf; | |
3694 | if (!b->buf) { | |
3695 | PerlIOBuf_get_base(f); | |
3696 | m->bbuf = b->buf; | |
3697 | } | |
06da4f11 | 3698 | } |
14a5cf38 | 3699 | return PerlIOBuf_write(f, vbuf, count); |
06da4f11 NIS |
3700 | } |
3701 | ||
3702 | IV | |
3703 | PerlIOMmap_flush(PerlIO *f) | |
3704 | { | |
14a5cf38 JH |
3705 | PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap); |
3706 | PerlIOBuf *b = &m->base; | |
3707 | IV code = PerlIOBuf_flush(f); | |
3708 | /* | |
71200d45 | 3709 | * Now we are "synced" at PerlIOBuf level |
14a5cf38 JH |
3710 | */ |
3711 | if (b->buf) { | |
3712 | if (m->len) { | |
3713 | /* | |
71200d45 | 3714 | * Unmap the buffer |
14a5cf38 JH |
3715 | */ |
3716 | if (PerlIOMmap_unmap(f) != 0) | |
3717 | code = -1; | |
3718 | } | |
3719 | else { | |
3720 | /* | |
3721 | * We seem to have a PerlIOBuf buffer which was not mapped | |
71200d45 | 3722 | * remember it in case we need one later |
14a5cf38 JH |
3723 | */ |
3724 | m->bbuf = b->buf; | |
3725 | } | |
3726 | } | |
3727 | return code; | |
06da4f11 NIS |
3728 | } |
3729 | ||
3730 | IV | |
3731 | PerlIOMmap_fill(PerlIO *f) | |
3732 | { | |
14a5cf38 JH |
3733 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3734 | IV code = PerlIO_flush(f); | |
3735 | if (code == 0 && !b->buf) { | |
3736 | code = PerlIOMmap_map(f); | |
3737 | } | |
3738 | if (code == 0 && !(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) { | |
3739 | code = PerlIOBuf_fill(f); | |
3740 | } | |
3741 | return code; | |
06da4f11 NIS |
3742 | } |
3743 | ||
3744 | IV | |
3745 | PerlIOMmap_close(PerlIO *f) | |
3746 | { | |
14a5cf38 JH |
3747 | PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap); |
3748 | PerlIOBuf *b = &m->base; | |
3749 | IV code = PerlIO_flush(f); | |
3750 | if (m->bbuf) { | |
3751 | b->buf = m->bbuf; | |
3752 | m->bbuf = NULL; | |
3753 | b->ptr = b->end = b->buf; | |
3754 | } | |
3755 | if (PerlIOBuf_close(f) != 0) | |
3756 | code = -1; | |
3757 | return code; | |
06da4f11 NIS |
3758 | } |
3759 | ||
71200d45 | 3760 | PerlIO * |
8cf8f3d1 | 3761 | PerlIOMmap_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param) |
71200d45 | 3762 | { |
a8fc9800 | 3763 | return PerlIOBase_dup(aTHX_ f, o, param); |
71200d45 NIS |
3764 | } |
3765 | ||
06da4f11 NIS |
3766 | |
3767 | PerlIO_funcs PerlIO_mmap = { | |
14a5cf38 JH |
3768 | "mmap", |
3769 | sizeof(PerlIOMmap), | |
3770 | PERLIO_K_BUFFERED, | |
3771 | PerlIOBuf_pushed, | |
3772 | PerlIOBase_noop_ok, | |
3773 | PerlIOBuf_open, | |
3774 | NULL, | |
3775 | PerlIOBase_fileno, | |
71200d45 | 3776 | PerlIOMmap_dup, |
14a5cf38 JH |
3777 | PerlIOBuf_read, |
3778 | PerlIOMmap_unread, | |
3779 | PerlIOMmap_write, | |
3780 | PerlIOBuf_seek, | |
3781 | PerlIOBuf_tell, | |
3782 | PerlIOBuf_close, | |
3783 | PerlIOMmap_flush, | |
3784 | PerlIOMmap_fill, | |
3785 | PerlIOBase_eof, | |
3786 | PerlIOBase_error, | |
3787 | PerlIOBase_clearerr, | |
3788 | PerlIOBase_setlinebuf, | |
3789 | PerlIOMmap_get_base, | |
3790 | PerlIOBuf_bufsiz, | |
3791 | PerlIOBuf_get_ptr, | |
3792 | PerlIOBuf_get_cnt, | |
3793 | PerlIOBuf_set_ptrcnt, | |
06da4f11 NIS |
3794 | }; |
3795 | ||
14a5cf38 | 3796 | #endif /* HAS_MMAP */ |
06da4f11 | 3797 | |
9e353e3b NIS |
3798 | void |
3799 | PerlIO_init(void) | |
760ac839 | 3800 | { |
14a5cf38 | 3801 | dTHX; |
43c11ae3 | 3802 | #ifndef WIN32 |
14a5cf38 | 3803 | call_atexit(PerlIO_cleanup_layers, NULL); |
43c11ae3 | 3804 | #endif |
14a5cf38 | 3805 | if (!_perlio) { |
be696b0a | 3806 | #ifndef WIN32 |
14a5cf38 | 3807 | atexit(&PerlIO_cleanup); |
be696b0a | 3808 | #endif |
14a5cf38 | 3809 | } |
760ac839 LW |
3810 | } |
3811 | ||
9e353e3b NIS |
3812 | #undef PerlIO_stdin |
3813 | PerlIO * | |
3814 | PerlIO_stdin(void) | |
3815 | { | |
14a5cf38 JH |
3816 | if (!_perlio) { |
3817 | dTHX; | |
3818 | PerlIO_stdstreams(aTHX); | |
3819 | } | |
3820 | return &_perlio[1]; | |
9e353e3b NIS |
3821 | } |
3822 | ||
3823 | #undef PerlIO_stdout | |
3824 | PerlIO * | |
3825 | PerlIO_stdout(void) | |
3826 | { | |
14a5cf38 JH |
3827 | if (!_perlio) { |
3828 | dTHX; | |
3829 | PerlIO_stdstreams(aTHX); | |
3830 | } | |
3831 | return &_perlio[2]; | |
9e353e3b NIS |
3832 | } |
3833 | ||
3834 | #undef PerlIO_stderr | |
3835 | PerlIO * | |
3836 | PerlIO_stderr(void) | |
3837 | { | |
14a5cf38 JH |
3838 | if (!_perlio) { |
3839 | dTHX; | |
3840 | PerlIO_stdstreams(aTHX); | |
3841 | } | |
3842 | return &_perlio[3]; | |
9e353e3b NIS |
3843 | } |
3844 | ||
3845 | /*--------------------------------------------------------------------------------------*/ | |
3846 | ||
3847 | #undef PerlIO_getname | |
3848 | char * | |
3849 | PerlIO_getname(PerlIO *f, char *buf) | |
3850 | { | |
14a5cf38 JH |
3851 | dTHX; |
3852 | char *name = NULL; | |
a15cef0c | 3853 | #ifdef VMS |
14a5cf38 JH |
3854 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; |
3855 | if (stdio) | |
3856 | name = fgetname(stdio, buf); | |
a15cef0c | 3857 | #else |
14a5cf38 | 3858 | Perl_croak(aTHX_ "Don't know how to get file name"); |
a15cef0c | 3859 | #endif |
14a5cf38 | 3860 | return name; |
9e353e3b NIS |
3861 | } |
3862 | ||
3863 | ||
3864 | /*--------------------------------------------------------------------------------------*/ | |
14a5cf38 JH |
3865 | /* |
3866 | * Functions which can be called on any kind of PerlIO implemented in | |
71200d45 | 3867 | * terms of above |
14a5cf38 | 3868 | */ |
9e353e3b NIS |
3869 | |
3870 | #undef PerlIO_getc | |
6f9d8c32 | 3871 | int |
9e353e3b | 3872 | PerlIO_getc(PerlIO *f) |
760ac839 | 3873 | { |
14a5cf38 JH |
3874 | STDCHAR buf[1]; |
3875 | SSize_t count = PerlIO_read(f, buf, 1); | |
3876 | if (count == 1) { | |
3877 | return (unsigned char) buf[0]; | |
3878 | } | |
3879 | return EOF; | |
313ca112 NIS |
3880 | } |
3881 | ||
3882 | #undef PerlIO_ungetc | |
3883 | int | |
3884 | PerlIO_ungetc(PerlIO *f, int ch) | |
3885 | { | |
14a5cf38 JH |
3886 | if (ch != EOF) { |
3887 | STDCHAR buf = ch; | |
3888 | if (PerlIO_unread(f, &buf, 1) == 1) | |
3889 | return ch; | |
3890 | } | |
3891 | return EOF; | |
760ac839 LW |
3892 | } |
3893 | ||
9e353e3b NIS |
3894 | #undef PerlIO_putc |
3895 | int | |
3896 | PerlIO_putc(PerlIO *f, int ch) | |
760ac839 | 3897 | { |
14a5cf38 JH |
3898 | STDCHAR buf = ch; |
3899 | return PerlIO_write(f, &buf, 1); | |
760ac839 LW |
3900 | } |
3901 | ||
9e353e3b | 3902 | #undef PerlIO_puts |
760ac839 | 3903 | int |
9e353e3b | 3904 | PerlIO_puts(PerlIO *f, const char *s) |
760ac839 | 3905 | { |
14a5cf38 JH |
3906 | STRLEN len = strlen(s); |
3907 | return PerlIO_write(f, s, len); | |
760ac839 LW |
3908 | } |
3909 | ||
3910 | #undef PerlIO_rewind | |
3911 | void | |
c78749f2 | 3912 | PerlIO_rewind(PerlIO *f) |
760ac839 | 3913 | { |
14a5cf38 JH |
3914 | PerlIO_seek(f, (Off_t) 0, SEEK_SET); |
3915 | PerlIO_clearerr(f); | |
6f9d8c32 NIS |
3916 | } |
3917 | ||
3918 | #undef PerlIO_vprintf | |
3919 | int | |
3920 | PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap) | |
3921 | { | |
14a5cf38 JH |
3922 | dTHX; |
3923 | SV *sv = newSVpvn("", 0); | |
3924 | char *s; | |
3925 | STRLEN len; | |
3926 | SSize_t wrote; | |
2cc61e15 | 3927 | #ifdef NEED_VA_COPY |
14a5cf38 JH |
3928 | va_list apc; |
3929 | Perl_va_copy(ap, apc); | |
3930 | sv_vcatpvf(sv, fmt, &apc); | |
2cc61e15 | 3931 | #else |
14a5cf38 | 3932 | sv_vcatpvf(sv, fmt, &ap); |
2cc61e15 | 3933 | #endif |
14a5cf38 JH |
3934 | s = SvPV(sv, len); |
3935 | wrote = PerlIO_write(f, s, len); | |
3936 | SvREFCNT_dec(sv); | |
3937 | return wrote; | |
760ac839 LW |
3938 | } |
3939 | ||
3940 | #undef PerlIO_printf | |
6f9d8c32 | 3941 | int |
14a5cf38 | 3942 | PerlIO_printf(PerlIO *f, const char *fmt, ...) |
760ac839 | 3943 | { |
14a5cf38 JH |
3944 | va_list ap; |
3945 | int result; | |
3946 | va_start(ap, fmt); | |
3947 | result = PerlIO_vprintf(f, fmt, ap); | |
3948 | va_end(ap); | |
3949 | return result; | |
760ac839 LW |
3950 | } |
3951 | ||
3952 | #undef PerlIO_stdoutf | |
6f9d8c32 | 3953 | int |
14a5cf38 | 3954 | PerlIO_stdoutf(const char *fmt, ...) |
760ac839 | 3955 | { |
14a5cf38 JH |
3956 | va_list ap; |
3957 | int result; | |
3958 | va_start(ap, fmt); | |
3959 | result = PerlIO_vprintf(PerlIO_stdout(), fmt, ap); | |
3960 | va_end(ap); | |
3961 | return result; | |
760ac839 LW |
3962 | } |
3963 | ||
3964 | #undef PerlIO_tmpfile | |
3965 | PerlIO * | |
c78749f2 | 3966 | PerlIO_tmpfile(void) |
760ac839 | 3967 | { |
14a5cf38 | 3968 | /* |
71200d45 | 3969 | * I have no idea how portable mkstemp() is ... |
14a5cf38 | 3970 | */ |
83b075c3 | 3971 | #if defined(WIN32) || !defined(HAVE_MKSTEMP) |
14a5cf38 JH |
3972 | dTHX; |
3973 | PerlIO *f = NULL; | |
3974 | FILE *stdio = PerlSIO_tmpfile(); | |
3975 | if (stdio) { | |
3976 | PerlIOStdio *s = | |
3977 | PerlIOSelf(PerlIO_push | |
3978 | (aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio, | |
3979 | "w+", Nullsv), PerlIOStdio); | |
3980 | s->stdio = stdio; | |
3981 | } | |
3982 | return f; | |
83b075c3 | 3983 | #else |
14a5cf38 JH |
3984 | dTHX; |
3985 | SV *sv = newSVpv("/tmp/PerlIO_XXXXXX", 0); | |
3986 | int fd = mkstemp(SvPVX(sv)); | |
3987 | PerlIO *f = NULL; | |
3988 | if (fd >= 0) { | |
3989 | f = PerlIO_fdopen(fd, "w+"); | |
3990 | if (f) { | |
3991 | PerlIOBase(f)->flags |= PERLIO_F_TEMP; | |
3992 | } | |
3993 | PerlLIO_unlink(SvPVX(sv)); | |
3994 | SvREFCNT_dec(sv); | |
3995 | } | |
3996 | return f; | |
83b075c3 | 3997 | #endif |
760ac839 LW |
3998 | } |
3999 | ||
6f9d8c32 NIS |
4000 | #undef HAS_FSETPOS |
4001 | #undef HAS_FGETPOS | |
4002 | ||
14a5cf38 JH |
4003 | #endif /* USE_SFIO */ |
4004 | #endif /* PERLIO_IS_STDIO */ | |
760ac839 | 4005 | |
9e353e3b | 4006 | /*======================================================================================*/ |
14a5cf38 | 4007 | /* |
71200d45 NIS |
4008 | * Now some functions in terms of above which may be needed even if we are |
4009 | * not in true PerlIO mode | |
9e353e3b NIS |
4010 | */ |
4011 | ||
760ac839 LW |
4012 | #ifndef HAS_FSETPOS |
4013 | #undef PerlIO_setpos | |
4014 | int | |
766a733e | 4015 | PerlIO_setpos(PerlIO *f, SV *pos) |
760ac839 | 4016 | { |
14a5cf38 JH |
4017 | dTHX; |
4018 | if (SvOK(pos)) { | |
4019 | STRLEN len; | |
4020 | Off_t *posn = (Off_t *) SvPV(pos, len); | |
4021 | if (f && len == sizeof(Off_t)) | |
4022 | return PerlIO_seek(f, *posn, SEEK_SET); | |
4023 | } | |
4024 | SETERRNO(EINVAL, SS$_IVCHAN); | |
4025 | return -1; | |
760ac839 | 4026 | } |
c411622e | 4027 | #else |
c411622e | 4028 | #undef PerlIO_setpos |
4029 | int | |
766a733e | 4030 | PerlIO_setpos(PerlIO *f, SV *pos) |
c411622e | 4031 | { |
14a5cf38 JH |
4032 | dTHX; |
4033 | if (SvOK(pos)) { | |
4034 | STRLEN len; | |
4035 | Fpos_t *fpos = (Fpos_t *) SvPV(pos, len); | |
4036 | if (f && len == sizeof(Fpos_t)) { | |
2d4389e4 | 4037 | #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64) |
14a5cf38 | 4038 | return fsetpos64(f, fpos); |
d9b3e12d | 4039 | #else |
14a5cf38 | 4040 | return fsetpos(f, fpos); |
d9b3e12d | 4041 | #endif |
14a5cf38 | 4042 | } |
766a733e | 4043 | } |
14a5cf38 JH |
4044 | SETERRNO(EINVAL, SS$_IVCHAN); |
4045 | return -1; | |
c411622e | 4046 | } |
4047 | #endif | |
760ac839 LW |
4048 | |
4049 | #ifndef HAS_FGETPOS | |
4050 | #undef PerlIO_getpos | |
4051 | int | |
766a733e | 4052 | PerlIO_getpos(PerlIO *f, SV *pos) |
760ac839 | 4053 | { |
14a5cf38 JH |
4054 | dTHX; |
4055 | Off_t posn = PerlIO_tell(f); | |
4056 | sv_setpvn(pos, (char *) &posn, sizeof(posn)); | |
4057 | return (posn == (Off_t) - 1) ? -1 : 0; | |
760ac839 | 4058 | } |
c411622e | 4059 | #else |
c411622e | 4060 | #undef PerlIO_getpos |
4061 | int | |
766a733e | 4062 | PerlIO_getpos(PerlIO *f, SV *pos) |
c411622e | 4063 | { |
14a5cf38 JH |
4064 | dTHX; |
4065 | Fpos_t fpos; | |
4066 | int code; | |
2d4389e4 | 4067 | #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64) |
14a5cf38 | 4068 | code = fgetpos64(f, &fpos); |
d9b3e12d | 4069 | #else |
14a5cf38 | 4070 | code = fgetpos(f, &fpos); |
d9b3e12d | 4071 | #endif |
14a5cf38 JH |
4072 | sv_setpvn(pos, (char *) &fpos, sizeof(fpos)); |
4073 | return code; | |
c411622e | 4074 | } |
4075 | #endif | |
760ac839 LW |
4076 | |
4077 | #if (defined(PERLIO_IS_STDIO) || !defined(USE_SFIO)) && !defined(HAS_VPRINTF) | |
4078 | ||
4079 | int | |
c78749f2 | 4080 | vprintf(char *pat, char *args) |
662a7e3f CS |
4081 | { |
4082 | _doprnt(pat, args, stdout); | |
14a5cf38 JH |
4083 | return 0; /* wrong, but perl doesn't use the return |
4084 | * value */ | |
662a7e3f CS |
4085 | } |
4086 | ||
4087 | int | |
c78749f2 | 4088 | vfprintf(FILE *fd, char *pat, char *args) |
760ac839 LW |
4089 | { |
4090 | _doprnt(pat, args, fd); | |
14a5cf38 JH |
4091 | return 0; /* wrong, but perl doesn't use the return |
4092 | * value */ | |
760ac839 LW |
4093 | } |
4094 | ||
4095 | #endif | |
4096 | ||
4097 | #ifndef PerlIO_vsprintf | |
6f9d8c32 | 4098 | int |
8ac85365 | 4099 | PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap) |
760ac839 | 4100 | { |
14a5cf38 JH |
4101 | int val = vsprintf(s, fmt, ap); |
4102 | if (n >= 0) { | |
4103 | if (strlen(s) >= (STRLEN) n) { | |
4104 | dTHX; | |
4105 | (void) PerlIO_puts(Perl_error_log, | |
4106 | "panic: sprintf overflow - memory corrupted!\n"); | |
4107 | my_exit(1); | |
4108 | } | |
760ac839 | 4109 | } |
14a5cf38 | 4110 | return val; |
760ac839 LW |
4111 | } |
4112 | #endif | |
4113 | ||
4114 | #ifndef PerlIO_sprintf | |
6f9d8c32 | 4115 | int |
14a5cf38 | 4116 | PerlIO_sprintf(char *s, int n, const char *fmt, ...) |
760ac839 | 4117 | { |
14a5cf38 JH |
4118 | va_list ap; |
4119 | int result; | |
4120 | va_start(ap, fmt); | |
4121 | result = PerlIO_vsprintf(s, n, fmt, ap); | |
4122 | va_end(ap); | |
4123 | return result; | |
760ac839 LW |
4124 | } |
4125 | #endif |