forked from parrot/parrot
/
io.ops
261 lines (182 loc) · 4.94 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
/*
** io.ops
*/
VERSION = PARROT_VERSION;
=head1 NAME
io.ops
=cut
=head1 DESCRIPTION
Parrot's IO API
=cut
###############################################################################
=head2 Parrot IO API Operations
=over 4
=cut
########################################
=item B<close>(inout PMC)
Close IO object $1
=cut
inline op close(inout PMC) {
PIO_close(interpreter, (ParrotIO*)(($1)->data));
goto NEXT();
}
#########################################
=item B<open>(out PMC, in STR, in STR)
Open URL (file, address, database, in core image) named $2 with
Perl style mode string in $3 and create an IO object in $1.
=cut
inline op open(out PMC, in STR, in STR) {
ParrotIO * io;
char * path, * mode;
size_t size;
/* We probably need to make this into a String API */
size = ($2)->bufused + 1;
path = mem_allocate(interpreter, &size);
memcpy(path, ($2)->bufstart, ($2)->bufused);
*(path + ($2)->bufused) = 0;
size = ($3)->bufused + 1;
mode = mem_allocate(interpreter, &size);
memcpy(mode, ($3)->bufstart, ($3)->bufused);
*(mode + ($3)->bufused) = 0;
io = PIO_open(interpreter, path, mode);
if(!io) {
/* FIXME: Handle error */
}
$1 = new_io_pmc(interpreter, io);
goto NEXT();
}
##########################################
=item B<print>(in PMC, in STR)
Print String $2 on the IO stream object $1.
=cut
op print(in PMC, in STR) {
ParrotIO * io;
io = (ParrotIO*)($1->data);
if ($2 && io) {
PIO_write(interpreter, io, ($2)->bufstart, string_length($2));
}
goto NEXT();
}
##########################################
=item B<printerr>(in STR)
Print $1 to interp.stderr IO stream (unbuffered)
=cut
op printerr(in STR) {
if ($1) {
PIO_write(interpreter, PIO_STDERR(interpreter), ($1)->bufstart,
string_length($1));
}
goto NEXT();
}
########################################
=item B<puts>(in STR)
=item B<puts>(in INT)
=item B<puts>(in NUM)
Print $1 to standard output stream
This will go away when print ops are all migrated to
use ParrotIO instead of STDIO. Right now ParrotIO is
not stable enough to replace STDIO.
=cut
op puts(in STR) {
if (($1) && string_length($1)) {
PIO_write(interpreter, PIO_STDOUT(interpreter), ($1)->bufstart,
string_length($1));
}
goto NEXT();
}
op puts(in INT) {
STRING * s = string_from_int(interpreter, $1);
if (string_length(s)) {
PIO_write(interpreter, PIO_STDOUT(interpreter), s->bufstart,
string_length(s));
}
goto NEXT();
}
op puts(in NUM) {
STRING * s = Parrot_sprintf_c(interpreter, "%Vf", $1);
if (string_length(s)) {
PIO_write(interpreter, PIO_STDOUT(interpreter), s->bufstart,
string_length(s));
}
goto NEXT();
}
########################################
=item B<read>(out STR, in INT)
Read up to N bytes from standard input stream
If stream is linebuffered, will return at EOL, for
files it will read MIN(MAX(avail, N), 65535) bytes.
Warning: This is a quick hack.
=item B<read>(out STR, in PMC, in INT)
Read up to N bytes from IO PMC stream.
If stream is linebuffered, will return at EOL, for
files it will read MIN(MAX(avail, N), 65535) bytes.
Warning: This is a quick hack.
=cut
op read(out STR, in INT) {
UINTVAL n;
INTVAL nr;
if($2 >= 65535)
n = 65535;
else
n = $2;
$1 = string_make(interpreter, NULL, n, NULL, 0, NULL);
memset(($1)->bufstart, 0, n);
nr = PIO_read(interpreter, PIO_STDIN(interpreter), ($1)->bufstart, (size_t)n);
if(nr > 0)
($1)->strlen = ($1)->bufused = nr;
else
($1)->strlen = ($1)->bufused = 0;
goto NEXT();
}
op read(out STR, in PMC, in INT) {
UINTVAL n;
INTVAL nr;
if($3 >= 65535)
n = 65535;
else
n = $3;
$1 = string_make(interpreter, NULL, n, NULL, 0, NULL);
memset(($1)->bufstart, 0, n);
nr = PIO_read(interpreter, (ParrotIO*)($2->data), ($1)->bufstart, (size_t)n);
if(nr > 0)
($1)->strlen = ($1)->bufused = nr;
else
($1)->strlen = ($1)->bufused = 0;
goto NEXT();
}
##########################################
=item B<seek>(out INT, in PMC, in INT, in INT)
32bit seek:
Set file position to offset $3 on IO stream $2. 'whence' is
indicated by the value in $4.
=item B<seek>(out INT, in PMC, in INT, in INT, in INT)
64bit seek:
Set file position to offset ($3 << 32 | $4) on IO stream $2. 'whence' is
indicated by the value in $4.
=cut
op seek(out INT, in PMC, in INT, in INT) {
ParrotIO * io;
io = (ParrotIO*)($2->data);
if (io) {
$1 = (INTVAL)PIO_seek(interpreter, io, 0, $3, $4);
}
goto NEXT();
}
op seek(out INT, in PMC, in INT, in INT, in INT) {
ParrotIO * io;
io = (ParrotIO*)($2->data);
if (io) {
$1 = (INTVAL)PIO_seek(interpreter, io, $3, $4, $5);
}
goto NEXT();
}
########################################
=back
=cut
###############################################################################
=head1 COPYRIGHT
Copyright (C) 2001-2002 Yet Another Society. All rights reserved.
=head1 LICENSE
This program is free software. It is subject to the same license
as the Parrot interpreter itself.
=cut