This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update Filter::Util::Call to CPAN version 1.51
[perl5.git] / ext / PerlIO-scalar / scalar.xs
CommitLineData
f6c77cf1
NIS
1#define PERL_NO_GET_CONTEXT
2#include "EXTERN.h"
3#include "perl.h"
4#include "XSUB.h"
5#ifdef PERLIO_LAYERS
6
7#include "perliol.h"
8
b38d579d
TC
9static const char code_point_warning[] =
10 "Strings with code points over 0xFF may not be mapped into in-memory file handles\n";
11
14d89041
NIS
12typedef struct {
13 struct _PerlIO base; /* Base "class" info */
14 SV *var;
15 Off_t posn;
f6c77cf1
NIS
16} PerlIOScalar;
17
18IV
14d89041
NIS
19PerlIOScalar_pushed(pTHX_ PerlIO * f, const char *mode, SV * arg,
20 PerlIO_funcs * tab)
f6c77cf1 21{
14d89041
NIS
22 IV code;
23 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
24 /* If called (normally) via open() then arg is ref to scalar we are
25 * using, otherwise arg (from binmode presumably) is either NULL
26 * or the _name_ of the scalar
27 */
82505891 28 if (arg && SvOK(arg)) {
cba44c14 29 if (SvROK(arg)) {
47d6f3d6
FC
30 if (SvREADONLY(SvRV(arg)) && !SvIsCOW(SvRV(arg))
31 && mode && *mode != 'r') {
b35bc0c6 32 if (ckWARN(WARN_LAYER))
76c6a213 33 Perl_warner(aTHX_ packWARN(WARN_LAYER), "%s", PL_no_modify);
42bc49da 34 SETERRNO(EINVAL, SS_IVCHAN);
b35bc0c6
RGS
35 return -1;
36 }
14d89041 37 s->var = SvREFCNT_inc(SvRV(arg));
22ccb26d
BM
38 SvGETMAGIC(s->var);
39 if (!SvPOK(s->var) && SvOK(s->var))
40 (void)SvPV_nomg_const_nolen(s->var);
14d89041
NIS
41 }
42 else {
43 s->var =
44 SvREFCNT_inc(perl_get_sv
45 (SvPV_nolen(arg), GV_ADD | GV_ADDMULTI));
46 }
564dc057 47 }
14d89041 48 else {
c2b90b61 49 s->var = newSVpvs("");
564dc057 50 }
5a2bc23b 51 SvUPGRADE(s->var, SVt_PV);
552908b1 52
14d89041 53 code = PerlIOBase_pushed(aTHX_ f, mode, Nullsv, tab);
47cc46ee 54 if (!SvOK(s->var) || (PerlIOBase(f)->flags) & PERLIO_F_TRUNCATE)
526fd1b4
FC
55 {
56 sv_force_normal(s->var);
b162af07 57 SvCUR_set(s->var, 0);
2b2b0f4f 58 if (SvPOK(s->var)) *SvPVX(s->var) = 0;
526fd1b4 59 }
02c3c86b
TC
60 if (SvUTF8(s->var) && !sv_utf8_downgrade(s->var, TRUE)) {
61 if (ckWARN(WARN_UTF8))
b38d579d 62 Perl_warner(aTHX_ packWARN(WARN_UTF8), code_point_warning);
02c3c86b
TC
63 SETERRNO(EINVAL, SS_IVCHAN);
64 SvREFCNT_dec(s->var);
65 s->var = Nullsv;
66 return -1;
67 }
14d89041 68 if ((PerlIOBase(f)->flags) & PERLIO_F_APPEND)
7f7e7b58 69 s->posn = SvOK(s->var) ? sv_len(s->var) : 0;
14d89041
NIS
70 else
71 s->posn = 0;
ffe0bb5a 72 SvSETMAGIC(s->var);
14d89041 73 return code;
f6c77cf1
NIS
74}
75
76IV
14d89041 77PerlIOScalar_popped(pTHX_ PerlIO * f)
f6c77cf1 78{
14d89041
NIS
79 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
80 if (s->var) {
81 SvREFCNT_dec(s->var);
82 s->var = Nullsv;
83 }
84 return 0;
f6c77cf1
NIS
85}
86
87IV
14d89041 88PerlIOScalar_close(pTHX_ PerlIO * f)
f6c77cf1 89{
14d89041
NIS
90 IV code = PerlIOBase_close(aTHX_ f);
91 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
92 return code;
f6c77cf1
NIS
93}
94
95IV
14d89041 96PerlIOScalar_fileno(pTHX_ PerlIO * f)
f6c77cf1 97{
c33e8be1 98 PERL_UNUSED_ARG(f);
14d89041 99 return -1;
f6c77cf1
NIS
100}
101
102IV
14d89041 103PerlIOScalar_seek(pTHX_ PerlIO * f, Off_t offset, int whence)
f6c77cf1 104{
14d89041 105 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
e94207f0 106
14d89041 107 switch (whence) {
42bc49da 108 case SEEK_SET:
14d89041
NIS
109 s->posn = offset;
110 break;
42bc49da 111 case SEEK_CUR:
14d89041
NIS
112 s->posn = offset + s->posn;
113 break;
42bc49da 114 case SEEK_END:
b6597275
FC
115 {
116 STRLEN oldcur;
117 (void)SvPV(s->var, oldcur);
118 s->posn = offset + oldcur;
14d89041 119 break;
b6597275 120 }
14d89041 121 }
42bc49da
JH
122 if (s->posn < 0) {
123 if (ckWARN(WARN_LAYER))
124 Perl_warner(aTHX_ packWARN(WARN_LAYER), "Offset outside string");
125 SETERRNO(EINVAL, SS_IVCHAN);
126 return -1;
14d89041
NIS
127 }
128 return 0;
f6c77cf1
NIS
129}
130
131Off_t
14d89041 132PerlIOScalar_tell(pTHX_ PerlIO * f)
f6c77cf1 133{
14d89041
NIS
134 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
135 return s->posn;
f6c77cf1
NIS
136}
137
ffe0bb5a
DM
138
139SSize_t
140PerlIOScalar_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
141{
142 if (!f)
143 return 0;
144 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD)) {
145 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
146 SETERRNO(EBADF, SS_IVCHAN);
0ea86a10 147 Perl_PerlIO_save_errno(aTHX_ f);
ffe0bb5a
DM
148 return 0;
149 }
150 {
151 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
152 SV *sv = s->var;
153 char *p;
ab9f1586
FC
154 STRLEN len;
155 I32 got;
ffe0bb5a 156 p = SvPV(sv, len);
b38d579d
TC
157 if (SvUTF8(sv)) {
158 if (sv_utf8_downgrade(sv, TRUE)) {
159 p = SvPV_nomg(sv, len);
160 }
161 else {
162 if (ckWARN(WARN_UTF8))
163 Perl_warner(aTHX_ packWARN(WARN_UTF8), code_point_warning);
164 SETERRNO(EINVAL, SS_IVCHAN);
165 return -1;
166 }
167 }
ffe0bb5a
DM
168 got = len - (STRLEN)(s->posn);
169 if (got <= 0)
170 return 0;
c33e8be1 171 if ((STRLEN)got > (STRLEN)count)
ffe0bb5a
DM
172 got = (STRLEN)count;
173 Copy(p + (STRLEN)(s->posn), vbuf, got, STDCHAR);
174 s->posn += (Off_t)got;
175 return (SSize_t)got;
176 }
177}
178
f6c77cf1 179SSize_t
14d89041 180PerlIOScalar_write(pTHX_ PerlIO * f, const void *vbuf, Size_t count)
f6c77cf1 181{
14d89041
NIS
182 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
183 Off_t offset;
184 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
185 SV *sv = s->var;
186 char *dst;
ffe0bb5a 187 SvGETMAGIC(sv);
c5a04db8
FC
188 if (!SvROK(sv)) sv_force_normal(sv);
189 if (SvOK(sv)) SvPV_force_nomg_nolen(sv);
eeee3e08
TC
190 if (SvUTF8(sv) && !sv_utf8_downgrade(sv, TRUE)) {
191 if (ckWARN(WARN_UTF8))
192 Perl_warner(aTHX_ packWARN(WARN_UTF8), code_point_warning);
193 SETERRNO(EINVAL, SS_IVCHAN);
194 return 0;
195 }
14d89041 196 if ((PerlIOBase(f)->flags) & PERLIO_F_APPEND) {
8af88444 197 dst = SvGROW(sv, SvCUR(sv) + count + 1);
14d89041
NIS
198 offset = SvCUR(sv);
199 s->posn = offset + count;
200 }
201 else {
b6597275 202 STRLEN const cur = SvCUR(sv);
8380d35a 203 if ((STRLEN)s->posn > cur) {
8af88444 204 dst = SvGROW(sv, (STRLEN)s->posn + count + 1);
b6597275
FC
205 Zero(SvPVX(sv) + cur, (STRLEN)s->posn - cur, char);
206 }
8af88444
BL
207 else if ((s->posn + count) >= cur)
208 dst = SvGROW(sv, (STRLEN)s->posn + count + 1);
14d89041 209 else
ffe0bb5a 210 dst = SvPVX(sv);
14d89041
NIS
211 offset = s->posn;
212 s->posn += count;
213 }
214 Move(vbuf, dst + offset, count, char);
8af88444 215 if ((STRLEN) s->posn > SvCUR(sv)) {
4a9d6100 216 SvCUR_set(sv, (STRLEN)s->posn);
8af88444
BL
217 dst[(STRLEN) s->posn] = 0;
218 }
ffe0bb5a
DM
219 SvPOK_on(sv);
220 SvSETMAGIC(sv);
14d89041 221 return count;
09bf542c 222 }
14d89041
NIS
223 else
224 return 0;
f6c77cf1
NIS
225}
226
227IV
14d89041 228PerlIOScalar_fill(pTHX_ PerlIO * f)
f6c77cf1 229{
c33e8be1 230 PERL_UNUSED_ARG(f);
14d89041 231 return -1;
f6c77cf1
NIS
232}
233
234IV
14d89041 235PerlIOScalar_flush(pTHX_ PerlIO * f)
f6c77cf1 236{
c33e8be1 237 PERL_UNUSED_ARG(f);
14d89041 238 return 0;
f6c77cf1
NIS
239}
240
241STDCHAR *
14d89041 242PerlIOScalar_get_base(pTHX_ PerlIO * f)
f6c77cf1 243{
14d89041
NIS
244 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
245 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
ffe0bb5a 246 SvGETMAGIC(s->var);
14d89041
NIS
247 return (STDCHAR *) SvPV_nolen(s->var);
248 }
9849c14c 249 return (STDCHAR *) NULL;
f6c77cf1
NIS
250}
251
252STDCHAR *
14d89041 253PerlIOScalar_get_ptr(pTHX_ PerlIO * f)
f6c77cf1 254{
14d89041
NIS
255 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
256 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
257 return PerlIOScalar_get_base(aTHX_ f) + s->posn;
258 }
9849c14c 259 return (STDCHAR *) NULL;
f6c77cf1
NIS
260}
261
262SSize_t
14d89041 263PerlIOScalar_get_cnt(pTHX_ PerlIO * f)
f6c77cf1 264{
14d89041
NIS
265 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
266 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
71edc894 267 STRLEN len;
5a2bc23b 268 (void)SvPV(s->var,len);
71edc894
FC
269 if (len > (STRLEN) s->posn)
270 return len - (STRLEN)s->posn;
14d89041
NIS
271 else
272 return 0;
273 }
75effbe0 274 return 0;
f6c77cf1
NIS
275}
276
277Size_t
14d89041 278PerlIOScalar_bufsiz(pTHX_ PerlIO * f)
f6c77cf1 279{
14d89041
NIS
280 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
281 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
ffe0bb5a 282 SvGETMAGIC(s->var);
14d89041
NIS
283 return SvCUR(s->var);
284 }
285 return 0;
f6c77cf1
NIS
286}
287
288void
14d89041 289PerlIOScalar_set_ptrcnt(pTHX_ PerlIO * f, STDCHAR * ptr, SSize_t cnt)
f6c77cf1 290{
14d89041 291 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
71edc894 292 STRLEN len;
c33e8be1 293 PERL_UNUSED_ARG(ptr);
5a2bc23b 294 (void)SvPV(s->var,len);
71edc894 295 s->posn = len - cnt;
f6c77cf1
NIS
296}
297
298PerlIO *
14d89041
NIS
299PerlIOScalar_open(pTHX_ PerlIO_funcs * self, PerlIO_list_t * layers, IV n,
300 const char *mode, int fd, int imode, int perm,
301 PerlIO * f, int narg, SV ** args)
f6c77cf1 302{
14d89041 303 SV *arg = (narg > 0) ? *args : PerlIOArg;
c33e8be1
Z
304 PERL_UNUSED_ARG(fd);
305 PERL_UNUSED_ARG(imode);
306 PERL_UNUSED_ARG(perm);
14d89041
NIS
307 if (SvROK(arg) || SvPOK(arg)) {
308 if (!f) {
309 f = PerlIO_allocate(aTHX);
310 }
e3feee4e 311 if ( (f = PerlIO_push(aTHX_ f, self, mode, arg)) ) {
14d89041
NIS
312 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
313 }
314 return f;
315 }
316 return NULL;
f6c77cf1
NIS
317}
318
ecdeb87c 319SV *
14d89041 320PerlIOScalar_arg(pTHX_ PerlIO * f, CLONE_PARAMS * param, int flags)
ecdeb87c 321{
14d89041
NIS
322 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
323 SV *var = s->var;
324 if (flags & PERLIO_DUP_CLONE)
325 var = PerlIO_sv_dup(aTHX_ var, param);
326 else if (flags & PERLIO_DUP_FD) {
327 /* Equivalent (guesses NI-S) of dup() is to create a new scalar */
328 var = newSVsv(var);
329 }
330 else {
331 var = SvREFCNT_inc(var);
332 }
333 return newRV_noinc(var);
ecdeb87c
NIS
334}
335
8cf8f3d1 336PerlIO *
14d89041
NIS
337PerlIOScalar_dup(pTHX_ PerlIO * f, PerlIO * o, CLONE_PARAMS * param,
338 int flags)
8cf8f3d1 339{
49b69fb3
FC
340 /* Duplication causes the scalar layer to be pushed on to clone, caus-
341 ing the cloned scalar to be set to the empty string by
342 PerlIOScalar_pushed. So set aside our scalar temporarily. */
343 PerlIOScalar * const os = PerlIOSelf(o, PerlIOScalar);
8380d35a 344 PerlIOScalar *fs = NULL; /* avoid "may be used uninitialized" warning */
49b69fb3
FC
345 SV * const var = os->var;
346 os->var = newSVpvs("");
14d89041 347 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
7b3cf1c0 348 fs = PerlIOSelf(f, PerlIOScalar);
49b69fb3
FC
349 /* var has been set by implicit push, so replace it */
350 SvREFCNT_dec(fs->var);
14d89041 351 }
49b69fb3
FC
352 SvREFCNT_dec(os->var);
353 os->var = var;
7b3cf1c0
FC
354 if (f) {
355 SV * const rv = PerlIOScalar_arg(aTHX_ o, param, flags);
356 fs->var = SvREFCNT_inc(SvRV(rv));
357 SvREFCNT_dec(rv);
358 fs->posn = os->posn;
359 }
14d89041 360 return f;
8cf8f3d1 361}
f6c77cf1 362
27da23d5 363PERLIO_FUNCS_DECL(PerlIO_scalar) = {
14d89041
NIS
364 sizeof(PerlIO_funcs),
365 "scalar",
366 sizeof(PerlIOScalar),
367 PERLIO_K_BUFFERED | PERLIO_K_RAW,
368 PerlIOScalar_pushed,
369 PerlIOScalar_popped,
370 PerlIOScalar_open,
371 PerlIOBase_binmode,
372 PerlIOScalar_arg,
373 PerlIOScalar_fileno,
374 PerlIOScalar_dup,
ffe0bb5a 375 PerlIOScalar_read,
3ff3a8b6 376 NULL, /* unread */
14d89041
NIS
377 PerlIOScalar_write,
378 PerlIOScalar_seek,
379 PerlIOScalar_tell,
380 PerlIOScalar_close,
381 PerlIOScalar_flush,
382 PerlIOScalar_fill,
383 PerlIOBase_eof,
384 PerlIOBase_error,
385 PerlIOBase_clearerr,
386 PerlIOBase_setlinebuf,
387 PerlIOScalar_get_base,
388 PerlIOScalar_bufsiz,
389 PerlIOScalar_get_ptr,
390 PerlIOScalar_get_cnt,
391 PerlIOScalar_set_ptrcnt,
f6c77cf1
NIS
392};
393
394
395#endif /* Layers available */
396
e934609f 397MODULE = PerlIO::scalar PACKAGE = PerlIO::scalar
f6c77cf1 398
acf4de66
A
399PROTOTYPES: ENABLE
400
f6c77cf1
NIS
401BOOT:
402{
403#ifdef PERLIO_LAYERS
27da23d5 404 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_scalar));
f6c77cf1
NIS
405#endif
406}
407