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
//! File access word set
use exception::{
FILE_IO_EXCEPTION, INVALID_MEMORY_ADDRESS, INVALID_NUMERIC_ARGUMENT, RESULT_OUT_OF_RANGE,
};
use std::fs::{self, OpenOptions};
use std::io::{Read, Seek, SeekFrom, Write};
use Core;
use Memory;
const PATH_NAME_MAX_LEN: usize = 256;
pub trait FileAccess: Core {
fn add_file_access(&mut self) {
self.add_primitive("file-size", FileAccess::file_size);
self.add_primitive("file-position", FileAccess::file_position);
self.add_primitive("close-file", FileAccess::close_file);
self.add_primitive("create-file", FileAccess::create_file);
self.add_primitive("delete-file", FileAccess::delete_file);
self.add_primitive("open-file", FileAccess::open_file);
self.add_primitive("read-file", FileAccess::read_file);
self.add_primitive("write-file", FileAccess::write_file);
self.add_primitive("resize-file", FileAccess::resize_file);
self.add_primitive("reposition-file", FileAccess::reposition_file);
}
/// ( fileid -- ud ior )
///
/// ud is the size, in characters, of the file identified by fileid. ior is
/// the implementation-defined I/O result code. This operation does not
/// affect the value returned by FILE- POSITION. ud is undefined if ior is
/// non-zero.
///
/// Note: As rtForth does not support double-length integers, the higher
/// part of ud is 0. rtForth also does not support unsigned integers, the
/// maximum value of ud allowed is isize::max_value(). So an exception
/// RESULT_OUT_OF_RANGE will be returned for a file size larger than
/// isize::max_value().
fn file_size(&mut self) {
let fileid = self.s_stack().pop();
if fileid <= 0 {
self.s_stack()
.push3(-1, -1, INVALID_NUMERIC_ARGUMENT.into());
return;
}
let fileid = fileid as usize - 1;
if fileid < self.files().len() {
let ud = match &self.files()[fileid] {
&Some(ref f) => match f.metadata() {
Ok(m) => {
let ud = m.len();
if ud <= isize::max_value() as u64 {
Ok(ud)
} else {
Err(RESULT_OUT_OF_RANGE)
}
}
Err(_) => Err(FILE_IO_EXCEPTION),
},
&None => Err(INVALID_NUMERIC_ARGUMENT),
};
match ud {
Ok(ud) => {
self.s_stack().push3(ud as isize, 0, 0);
}
Err(e) => {
self.s_stack().push3(-1, -1, e.into());
}
}
} else {
self.s_stack()
.push3(-1, -1, INVALID_NUMERIC_ARGUMENT.into());
}
}
/// ( fileid -- ud ior )
///
/// ud is the current file position for the file identified by fileid. ior
/// is the implementation-defined I/O result code. ud is undefined if ior
/// is non-zero.
///
/// Note: As rtForth does not support double-length integers, the higher
/// part of ud is 0. rtForth also does not support unsigned integers, the
/// maximum value of ud allowed is isize::max_value(). So an exception
/// RESULT_OUT_OF_RANGE will be returned for a file position larger than
/// isize::max_value().
fn file_position(&mut self) {
let fileid = self.s_stack().pop();
if fileid <= 0 {
self.s_stack()
.push3(-1, -1, INVALID_NUMERIC_ARGUMENT.into());
return;
}
let fileid = fileid as usize - 1;
if fileid < self.files().len() {
let ud = match &mut self.files_mut()[fileid] {
&mut Some(ref mut f) => match f.seek(SeekFrom::Current(0)) {
Ok(ud) => {
if ud <= isize::max_value() as u64 {
Ok(ud)
} else {
Err(RESULT_OUT_OF_RANGE)
}
}
Err(_) => Err(FILE_IO_EXCEPTION),
},
&mut None => Err(INVALID_NUMERIC_ARGUMENT),
};
match ud {
Ok(ud) => {
self.s_stack().push3(ud as isize, 0, 0);
}
Err(e) => self.s_stack().push3(-1, -1, e.into()),
}
} else {
self.s_stack()
.push3(-1, -1, INVALID_NUMERIC_ARGUMENT.into());
}
}
/// ( fileid -- ior )
///
/// Close the file identified by fileid. ior is the implementation-defined
/// I/O result code.
fn close_file(&mut self) {
let fileid = self.s_stack().pop();
if fileid <= 0 {
self.s_stack().push(INVALID_NUMERIC_ARGUMENT.into());
return;
}
let fileid = fileid as usize - 1;
if fileid < self.files().len() && self.files()[fileid].is_some() {
self.files_mut()[fileid] = None;
self.s_stack().push(0);
} else {
self.s_stack().push(INVALID_NUMERIC_ARGUMENT.into());
}
}
/// ( c-addr u fam -- fileid ior )
///
/// Create the file named in the character string specified by c-addr and
/// u, and open it with file access method fam. The meaning of values of
/// fam is implementation defined. If a file with the same name already
/// exists, recreate it as an empty file.
///
/// If the file was successfully created and opened, ior is zero, fileid
/// is its identifier, and the file has been positioned to the start of
/// the file.
///
/// Otherwise, ior is the implementation-defined I/O result code and fileid
/// is undefined.
fn create_file(&mut self) {
let (caddr, u, fam) = self.s_stack().pop3();
let caddr = caddr as usize;
let u = u as usize;
if u > PATH_NAME_MAX_LEN {
self.s_stack().push2(-1, INVALID_NUMERIC_ARGUMENT.into());
return;
}
let mut options = OpenOptions::new();
match fam {
0 => {
// Impossible to create a read-only file.
self.s_stack().push2(-1, INVALID_NUMERIC_ARGUMENT.into());
return;
}
1 => {
options.write(true).truncate(true).create(true);
}
2 => {
options.read(true).write(true).truncate(true).create(true);
}
_ => {
self.s_stack().push2(-1, INVALID_NUMERIC_ARGUMENT.into());
return;
}
};
let file = {
if self.data_space().start() <= caddr && caddr + u <= self.data_space().limit() {
let path_name = unsafe { self.data_space().str_from_raw_parts(caddr, u) };
match options.open(&path_name) {
Err(_) => Err(FILE_IO_EXCEPTION),
Ok(file) => Ok(file),
}
} else {
Err(INVALID_MEMORY_ADDRESS)
}
};
match file {
Err(e) => {
self.s_stack().push2(-1, e.into());
}
Ok(file) => {
let position = self.files().iter().position(|x| x.is_none());
match position {
Some(p) => {
self.files_mut()[p] = Some(file);
self.s_stack().push2(p as isize + 1, 0);
}
None => {
let fileid = self.files().len() as isize;
self.s_stack().push2(fileid + 1, 0);
self.files_mut().push(Some(file));
}
}
}
}
}
/// ( c-addr u -- ior )
///
/// Delete the file named in the character string specified by c-addr u.
/// ior is the implementation-defined I/O result code.
fn delete_file(&mut self) {
let (caddr, u) = self.s_stack().pop2();
let caddr = caddr as usize;
let u = u as usize;
if u > PATH_NAME_MAX_LEN {
self.s_stack().push2(-1, INVALID_NUMERIC_ARGUMENT.into());
} else {
let result = {
if self.data_space().start() <= caddr && caddr + u <= self.data_space().limit() {
let path_name = unsafe { self.data_space().str_from_raw_parts(caddr, u) };
match fs::remove_file(path_name) {
Err(_) => FILE_IO_EXCEPTION.into(),
Ok(_) => 0,
}
} else {
INVALID_MEMORY_ADDRESS.into()
}
};
self.s_stack().push(result);
}
}
/// ( c-addr u fam -- fileid ior )
/// Open the file named in the character string specified by c-addr u,
/// with file access method indicated by fam. The meaning of values of fam
/// is implementation defined.
///
/// If the file is successfully opened, ior is zero, fileid is its
/// identifier, and the file has been positioned to the start of the file.
///
/// Otherwise, ior is the implementation-defined I/O result code and fileid
/// is undefined.
fn open_file(&mut self) {
let (caddr, u, fam) = self.s_stack().pop3();
let caddr = caddr as usize;
let u = u as usize;
if u > PATH_NAME_MAX_LEN {
self.s_stack().push2(-1, INVALID_NUMERIC_ARGUMENT.into());
return;
}
let mut options = OpenOptions::new();
match fam {
0 => {
options.read(true);
}
1 => {
options.write(true);
}
2 => {
options.read(true).write(true);
}
_ => {
self.s_stack().push2(-1, INVALID_NUMERIC_ARGUMENT.into());
return;
}
};
let file = {
if self.data_space().start() <= caddr && caddr + u <= self.data_space().limit() {
let path_name = unsafe { self.data_space().str_from_raw_parts(caddr, u) };
match options.open(&path_name) {
Err(_) => Err(FILE_IO_EXCEPTION),
Ok(file) => Ok(file),
}
} else {
Err(INVALID_MEMORY_ADDRESS)
}
};
match file {
Err(e) => {
self.s_stack().push2(-1, e.into());
}
Ok(file) => {
let position = self.files_mut().iter().position(|x| x.is_none());
match position {
Some(p) => {
self.files_mut()[p] = Some(file);
self.s_stack().push2(p as isize + 1, 0);
}
None => {
let fileid = self.files().len() as isize;
self.s_stack().push2(fileid + 1, 0);
self.files_mut().push(Some(file));
}
}
}
}
}
/// ( c-addr u1 fileid -- u2 ior )
///
/// Read u1 consecutive characters to c-addr from the current position of
/// the file identified by fileid.
///
/// If u1 characters are read without an exception, ior is zero and u2 is
/// equal to u1.
///
/// If the end of the file is reached before u1 characters are read, ior is
/// zero and u2 is the number of characters actually read.
///
/// If the operation is initiated when the value returned by FILE-POSITION
/// is equal to the value returned by FILE-SIZE for the file identified by
/// fileid, ior is zero and u2 is zero.
///
/// If an exception occurs, ior is the implementation-defined I/O result
/// code, and u2 is the number of characters transferred to c-addr without
/// an exception.
///
/// An ambiguous condition exists if the operation is initiated when the
/// value returned by FILE-POSITION is greater than the value returned by
/// FILE-SIZE for the file identified by fileid, or if the requested
/// operation attempts to read portions of the file not written.
///
/// At the conclusion of the operation, FILE-POSITION returns the next file
/// position after the last character read.
fn read_file(&mut self) {
let (caddr, u1, fileid) = self.s_stack().pop3();
let caddr = caddr as usize;
let u1 = u1 as usize;
let fileid = fileid as usize;
if fileid == 0 {
self.s_stack().push2(-1, INVALID_NUMERIC_ARGUMENT.into());
return;
}
let fileid = fileid - 1;
if fileid >= self.files().len() || self.files()[fileid].is_none() {
self.s_stack().push2(0, INVALID_NUMERIC_ARGUMENT.into());
} else {
let mut file = self.files_mut()[fileid].take().unwrap();
let result = {
if self.data_space().start() <= caddr && caddr + u1 <= self.data_space().limit() {
let mut buf = unsafe { self.data_space().buffer_from_raw_parts_mut(caddr, u1) };
file.read(&mut buf).or(Err(FILE_IO_EXCEPTION.into()))
} else {
Err(INVALID_MEMORY_ADDRESS.into())
}
};
match result {
Ok(u2) => {
self.s_stack().push2(u2 as _, 0);
}
Err(e) => {
self.s_stack().push2(0, e);
}
}
self.files_mut()[fileid] = Some(file);
}
}
/// ( c-addr u fileid -- ior )
///
/// Write u characters from c-addr to the file identified by fileid
/// starting at its current position. ior is the implementation-defined I/O
/// result code.
///
/// At the conclusion of the operation, FILE-POSITION returns the next file
/// position after the last character written to the file, and FILE-SIZE
/// returns a value greater than or equal to the value returned by
/// FILE-POSITION.
fn write_file(&mut self) {
let (caddr, u, fileid) = self.s_stack().pop3();
let caddr = caddr as usize;
let u = u as usize;
if fileid <= 0 {
self.s_stack().push(INVALID_NUMERIC_ARGUMENT.into());
return;
}
let fileid = fileid as usize - 1;
if fileid < self.files().len() {
match self.files_mut()[fileid].take() {
Some(mut f) => {
let result = {
if self.data_space().start() <= caddr
&& caddr + u <= self.data_space().limit()
{
let buf = unsafe {
self.data_space().buffer_from_raw_parts(caddr as _, u as _)
};
f.write_all(buf).or(Err(FILE_IO_EXCEPTION))
} else {
Err(INVALID_MEMORY_ADDRESS)
}
};
match result {
Ok(_) => self.s_stack().push(0),
Err(_) => self.s_stack().push(FILE_IO_EXCEPTION.into()),
}
self.files_mut()[fileid] = Some(f);
}
None => {
self.s_stack().push(INVALID_NUMERIC_ARGUMENT.into());
}
}
} else {
self.s_stack().push(INVALID_NUMERIC_ARGUMENT.into());
}
}
/// ( ud fileid -- ior )
///
/// Set the size of the file identified by fileid to ud. ior is the
/// implementation-defined I/O result code.
///
/// If the resultant file is larger than the file before the operation,
/// the portion of the file added as a result of the operation might not
/// have been written.
///
/// At the conclusion of the operation, FILE-SIZE returns the value ud and
/// FILE- POSITION returns an unspecified value.
///
/// Note: As rtForth does not support double-length integers, the higher
/// part of ud is 0. rtForth also does not support unsigned integers, the
/// maximum value of ud allowed is isize::max_value(). So an exception
/// INVALID_NUMERIC_ARGUMENT will be returned for a ud larger than
/// isize::max_value().
fn resize_file(&mut self) {
let (ud_lower, ud_upper, fileid) = self.s_stack().pop3();
if fileid <= 0 {
self.s_stack().push(INVALID_NUMERIC_ARGUMENT.into());
return;
}
let fileid = fileid as usize - 1;
let ud_lower = ud_lower as usize;
if ud_upper != 0 {
self.s_stack().push(INVALID_NUMERIC_ARGUMENT.into());
} else if ud_lower > isize::max_value() as usize {
self.s_stack().push(INVALID_NUMERIC_ARGUMENT.into());
} else if fileid >= self.files().len() {
self.s_stack().push(INVALID_NUMERIC_ARGUMENT.into());
} else {
match self.files_mut()[fileid].take() {
Some(f) => {
match f.set_len(ud_lower as u64) {
Ok(_) => {
self.s_stack().push(0);
}
Err(_) => {
self.s_stack().push(FILE_IO_EXCEPTION.into());
}
}
self.files_mut()[fileid] = Some(f);
}
None => {
self.s_stack().push(INVALID_NUMERIC_ARGUMENT.into());
}
}
}
}
/// ( ud fileid -- ior )
///
/// Reposition the file identified by fileid to ud. ior is the
/// implementation-defined I/O result code. An ambiguous condition exists
/// if the file is positioned outside the file boundaries.
///
/// At the conclusion of the operation, FILE-POSITION returns the value ud.
///
/// Note: As rtForth does not support double-length integers, the higher
/// part of ud is 0. rtForth also does not support unsigned integers, the
/// maximum value of ud allowed is isize::max_value(). So an exception
/// INVALID_NUMERIC_ARGUMENT will be returned for a ud larger than
/// isize::max_value().
fn reposition_file(&mut self) {
let (ud_lower, ud_upper, fileid) = self.s_stack().pop3();
if fileid <= 0 {
self.s_stack().push(INVALID_NUMERIC_ARGUMENT.into());
return;
}
let fileid = fileid as usize - 1;
let ud_lower = ud_lower as usize;
if ud_upper != 0 {
self.s_stack().push(INVALID_NUMERIC_ARGUMENT.into());
} else if ud_lower > isize::max_value() as usize {
self.s_stack().push(INVALID_NUMERIC_ARGUMENT.into());
} else if fileid >= self.files().len() {
self.s_stack().push(INVALID_NUMERIC_ARGUMENT.into());
} else {
match self.files_mut()[fileid].take() {
Some(mut f) => {
match f.seek(SeekFrom::Start(ud_lower as u64)) {
Ok(_) => {
self.s_stack().push(0);
}
Err(_) => {
self.s_stack().push(FILE_IO_EXCEPTION.into());
}
}
self.files_mut()[fileid] = Some(f);
}
None => {
self.s_stack().push(INVALID_NUMERIC_ARGUMENT.into());
}
}
}
}
}