/
io.ops
527 lines (371 loc) · 11.4 KB
/
io.ops
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*
* $Id$
* Copyright (C) 2002-2008, Parrot Foundation.
** io.ops
*/
BEGIN_OPS_PREAMBLE
#include "../io/io_private.h"
END_OPS_PREAMBLE
=head1 NAME
io.ops - I/O Opcodes
=cut
=head1 DESCRIPTION
Parrot's IO API
=cut
###############################################################################
=head2 Parrot IO API Operations
=over 4
=cut
########################################
=item B<close>(invar PMC)
Close IO object $1
=cut
inline op close(invar PMC) :base_io {
Parrot_io_close(interp, $1);
}
########################################
=item B<fdopen>(out PMC, in INT, in STR)
Create ParrotIO object in $1 as a copy of file descriptor $2.
=cut
inline op fdopen(out PMC, in INT, in STR) :filesys_open {
$1 = Parrot_io_fdopen(interp, PMCNULL, (PIOHANDLE)$2, $3);
if (!$1)
$1 = pmc_new(interp, enum_class_Undef);
}
=item B<getstdin>(out PMC)
Create a new ParrotIO object for the stdin file descriptor and
store it in $1
=item B<getstdout>(out PMC)
Create a new ParrotIO object for the stdout file descriptor and
store it in $1
=item B<getstderr>(out PMC)
Create a new ParrotIO object for the stderr file descriptor and
store it in $1
=cut
inline op getstdin(out PMC) :base_io {
$1 = _PIO_STDIN(interp);
}
inline op getstdout(out PMC) :base_io {
$1 = _PIO_STDOUT(interp);
}
inline op getstderr(out PMC) :base_io {
$1 = _PIO_STDERR(interp);
}
#########################################
=item B<setstdin>(invar PMC)
Sets the standard input for a bare C<read> op to go to the supplied PMC.
Call C<getstdin> first if you care about retaining the previous PMC.
=item B<setstdout>(invar PMC)
Sets the standard output for a bare C<print> op to go to the supplied PMC.
Call C<getstdout> first if you care about retaining the previous PMC.
=item B<setstderr>(invar PMC)
Sets the standard error for a bare C<printerr> op to go to the supplied PMC.
Call C<getstderr> first if you care about retaining the previous PMC.
=cut
inline op setstdin(invar PMC) :base_io {
_PIO_STDIN(interp) = $1;
}
inline op setstdout(invar PMC) :base_io {
_PIO_STDOUT(interp) = $1;
}
inline op setstderr(invar PMC) :base_io {
_PIO_STDERR(interp) = $1;
}
#########################################
=item B<open>(out PMC, in STR, in STR)
Open URL (file, address, database, in core image) named $2 with
a mode string in $3 and create an IO object in $1.
The mode consists of a string of characters specified in any order:
r : read
w : write
a : append (Note: you must specify "wa", not just "a")
p : pipe
=item B<open>(out PMC, in STR)
Open URL (file, address, database, in core image) named $2 with
read mode and create an IO object in $1.
=cut
inline op open(out PMC, in STR, in STR) :filesys_open {
if (STRING_IS_NULL($2) || STRING_IS_NULL($3)) {
opcode_t *handler = Parrot_ex_throw_from_op_args(interp, expr NEXT(),
EXCEPTION_UNEXPECTED_NULL, "Invalid open");
goto ADDRESS(handler);
}
else {
$1 = Parrot_io_open(interp, PMCNULL, $2, $3);
PARROT_ASSERT(! PMC_IS_NULL($1));
}
}
inline op open(out PMC, in STR) :filesys_open {
if (STRING_IS_NULL($2)) {
opcode_t *handler = Parrot_ex_throw_from_op_args(interp, expr NEXT(),
EXCEPTION_UNEXPECTED_NULL, "Invalid open");
goto ADDRESS(handler);
}
else {
$1 = Parrot_io_open(interp, PMCNULL, $2, NULL);
PARROT_ASSERT(! PMC_IS_NULL($1));
}
}
########################################
=item B<print>(in INT)
=item B<print>(in NUM)
=item B<print>(invar PMC)
=item B<print>(in STR)
Print $1 to standard output.
=cut
inline op print(in INT) :base_io {
Parrot_io_printf(interp, INTVAL_FMT, (INTVAL)$1);
}
inline op print(in NUM) :base_io {
#ifdef PARROT_HAS_NEGATIVE_ZERO
Parrot_io_printf(interp, FLOATVAL_FMT, $1);
#else
/* Workaround for older msvcrt and openbsd. TT #313 */
if (Parrot_is_nzero($1)) {
Parrot_io_printf(interp, "-0");
}
else {
Parrot_io_printf(interp, FLOATVAL_FMT, $1);
}
#endif
}
op print(in STR) :base_io {
STRING * const s = $1;
if (s && Parrot_str_byte_length(interp, s))
Parrot_io_putps(interp, _PIO_STDOUT(interp), s);
}
op print(invar PMC) :base_io {
PMC * const p = $1;
STRING * const s = (VTABLE_get_string(interp, p));
if (s)
Parrot_io_putps(interp, _PIO_STDOUT(interp), s);
}
=item B<say>(in INT)
=item B<say>(in NUM)
=item B<say>(invar PMC)
=item B<say>(in STR)
Print $1 to standard output with a trailing newline.
=cut
inline op say(in INT) :base_io {
Parrot_io_printf(interp, INTVAL_FMT "\n", (INTVAL)$1);
}
inline op say(in NUM) :base_io {
#ifdef PARROT_HAS_NEGATIVE_ZERO
Parrot_io_printf(interp, FLOATVAL_FMT "\n", $1);
#else
/* Workaround for older msvcrt and openbsd. TT #313 */
if (Parrot_is_nzero($1)) {
Parrot_io_printf(interp, "-0\n");
}
else {
Parrot_io_printf(interp, FLOATVAL_FMT "\n", $1);
}
#endif
}
op say(in STR) :base_io {
STRING * const s = $1;
if (s && Parrot_str_byte_length(interp, s))
Parrot_io_putps(interp, _PIO_STDOUT(interp), s);
Parrot_io_putps(interp, _PIO_STDOUT(interp), Parrot_str_new_constant(interp, "\n"));
}
op say(invar PMC) :base_io {
PMC * const p = $1;
if (PMC_IS_NULL(p)) {
opcode_t *handler = Parrot_ex_throw_from_op_args(interp, expr NEXT(),
EXCEPTION_UNEXPECTED_NULL,
"Null PMC in say");
goto ADDRESS(handler);
}
else {
STRING * const s = VTABLE_get_string(interp, p);
if (s)
Parrot_io_putps(interp, _PIO_STDOUT(interp), s);
Parrot_io_putps(interp, _PIO_STDOUT(interp), Parrot_str_new_constant(interp, "\n"));
}
}
##########################################
=item B<printerr>(in INT)
=item B<printerr>(in NUM)
=item B<printerr>(in STR)
=item B<printerr>(invar PMC)
Print $1 to standard error.
=cut
op printerr(in INT) :base_io {
Parrot_io_eprintf(interp, INTVAL_FMT, $1);
}
op printerr(in NUM) :base_io {
Parrot_io_eprintf(interp, FLOATVAL_FMT, $1);
}
op printerr(in STR) :base_io {
STRING * const s = $1;
if (s && Parrot_str_byte_length(interp, s))
Parrot_io_putps(interp, _PIO_STDERR(interp), s);
}
op printerr(invar PMC) :base_io {
PMC * const p = $1;
STRING * const s = (VTABLE_get_string(interp, p));
if (s)
Parrot_io_putps(interp, _PIO_STDERR(interp), s);
}
##########################################
=item B<print>(invar PMC, in INT)
=item B<print>(invar PMC, in NUM)
=item B<print>(invar PMC, in STR)
=item B<print>(invar PMC, invar PMC)
Print $2 on the IO stream object $1.
=cut
op print(invar PMC, in INT) :base_io {
if ($1) {
STRING * const s = Parrot_str_from_int(interp, $2);
Parrot_io_putps(interp, $1, s);
}
}
op print(invar PMC, in NUM) :base_io {
if ($1) {
STRING * const s = Parrot_sprintf_c(interp, FLOATVAL_FMT, $2);
Parrot_io_putps(interp, $1, s);
}
}
op print(invar PMC, in STR) :base_io {
if ($2 && $1) {
Parrot_io_putps(interp, $1, $2);
}
}
op print(invar PMC, invar PMC) :base_io {
if ($2 && $1) {
STRING * const s = VTABLE_get_string(interp, $2);
Parrot_io_putps(interp, $1, s);
}
}
##########################################
=item B<read>(out STR, in INT)
Read up to N bytes from standard input stream
=item B<read>(out STR, invar PMC, in INT)
Read up to N bytes from IO PMC stream.
=cut
op read(out STR, in INT) :base_io {
$1 = Parrot_io_reads(interp, _PIO_STDIN(interp), (size_t)$2);
}
op read(out STR, invar PMC, in INT) :base_io {
$1 = Parrot_io_reads(interp, $2, (size_t)$3);
}
=item B<readline>(out STR, invar PMC)
Read a line up to EOL from filehandle $2.
This switches the filehandle to linebuffer-mode.
=cut
inline op readline(out STR, invar PMC) :base_io {
$1 = Parrot_io_readline(interp, $2);
}
##########################################
=item B<peek>(out STR)
Returns the next byte from standard input, but does not
remove it from the stream.
=item B<peek>(out STR, invar PMC)
Reads the next byte from an IO PMC, but does not
remove it from the stream.
=cut
op peek(out STR) :base_io {
STRING ** const s = &$1;
*s = NULL;
if (Parrot_io_peek(interp, _PIO_STDIN(interp), s) < 0) {
$1 = Parrot_str_new_noinit(interp, enum_stringrep_one, 0);
}
}
op peek(out STR, invar PMC) :base_io {
STRING ** const s = &$1;
*s = NULL;
if (Parrot_io_peek(interp, $2, s) < 0) {
$1 = Parrot_str_new_noinit(interp, enum_stringrep_one, 0);
}
}
##########################################
=item B<stat>(out INT, in STR, in INT)
=item B<stat>(out INT, in INT, in INT)
Stat the file. Return stat element $3 for file $2 into $1. The queryable
items currently are:
EXISTS 0
FILESIZE 1
ISDIR 2
ISDEV 3
CREATETIME 4 (Time file was created)
ACCESSTIME 5 (Time file was last accessed)
MODIFYTIME 6 (Time file data was changed)
CHANGETIME 7 (Time file metadata was changed)
BACKUPTIME 8 (Time of last backup)
UID 9
GID 10
=cut
op stat(out INT, in STR, in INT) {
$1 = Parrot_stat_info_intval(interp, $2, $3);
}
op stat(out INT, in INT, in INT) {
$1 = Parrot_fstat_info_intval(interp, $2, $3);
}
##########################################
=item B<seek>(invar PMC, in INT, in INT)
seek:
Set file position to offset $2 on IO stream $1. 'whence' is
indicated by the value in $3. The valid values for 'whence' are:
Value Meaning
0 Seek from the beginning of the file
1 Seek from the current position
2 Seek from the end of the file
[ Note: the above values for 'whence' is just an educated guess
at this point ]
=item B<seek>(invar PMC, in INT, in INT, in INT)
64bit seek:
Set file position to offset ($2 << 32 | $3) on IO stream $1. 'whence' is
indicated by the value in $4. This allows 64-bit seeks with only 32-bit
INTVALS.
=cut
op seek(invar PMC, in INT, in INT) :base_io {
if ($1) {
if (Parrot_io_seek(interp, $1, Parrot_io_make_offset($2), $3) < 0) {
opcode_t *handler = Parrot_ex_throw_from_op_args(interp, expr NEXT(),
EXCEPTION_PIO_ERROR,
"seek failed (32bit)");
goto ADDRESS(handler);
}
}
}
op seek(invar PMC, in INT, in INT, in INT) :base_io {
if ($1) {
if (Parrot_io_seek(interp, $1, Parrot_io_make_offset32($2, $3), $4) < 0) {
opcode_t *handler = Parrot_ex_throw_from_op_args(interp, expr NEXT(),
EXCEPTION_PIO_ERROR,
"seek failed (64bit)");
goto ADDRESS(handler);
}
}
}
=item B<tell>(out INT, invar PMC)
tell:
Get the current file position of stream $2 and store it in $1.
On systems where INTVAL is 32bit the result will be truncated if the
position is beyond 2 GiB
=item B<tell>(out INT, out INT, invar PMC)
64bit tell:
Get the current file positon of stream $3 in two parts of 32-bit each
($1 = pos >> 32, $2 = pos & 0xffffffff).
=cut
op tell(out INT, invar PMC) :base_io {
if ($2)
$1 = (INTVAL)Parrot_io_tell(interp, $2);
}
op tell(out INT, out INT, invar PMC) :base_io {
if ($3) {
PIOOFF_T pos;
pos = Parrot_io_tell(interp, $3);
$1 = (INTVAL)(pos >> 31);
$2 = (INTVAL)(pos & 0xffffffff);
}
}
=back
=cut
###############################################################################
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4:
*/