builtins/
io.rs

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
use bridge_adapters::add_builtin;
use compile_state::state::SloshVm;
use slvm::{VMError, VMResult, Value};
use std::fs::{self, OpenOptions};
use std::io::{Read, Write};
extern crate unicode_reader;
use bridge_macros::sl_sh_fn;
use shell::builtins::expand_tilde;
use slvm::io::HeapIo;

fn fopen(vm: &mut SloshVm, registers: &[Value]) -> VMResult<Value> {
    let mut args = registers.iter();
    if let Some(a) = args.next() {
        if let Value::Keyword(sym) = a {
            let ret = match vm.get_interned(*sym) {
                "stdin" => Some(HeapIo::stdin()),
                "stdout" => Some(HeapIo::stdout()),
                "stderr" => Some(HeapIo::stderr()),
                _ => None,
            };
            if let Some(ret) = ret {
                if args.next().is_some() {
                    return Err(VMError::new(
                        "io",
                        "fopen: if first form is a symbol then other forms not valid".to_string(),
                    ));
                }
                return Ok(vm.alloc_io(ret));
            }
        }
        let file_name = match a {
            Value::String(h) => vm.get_string(*h),
            Value::StringConst(i) => vm.get_interned(*i),
            _ => {
                return Err(VMError::new("io", "fopen: first form must evaluate to a string (filename) or :stdin, :stdout, :stderr"));
            }
        };
        let file_name = expand_tilde(file_name.into());
        let mut opts = OpenOptions::new();
        let mut is_read = false;
        let mut is_write = false;
        let mut error_nil = false;
        for a in args {
            if let Value::Keyword(i) = a {
                match vm.get_interned(*i) {
                    "read" => {
                        is_read = true;
                        opts.read(true);
                    }
                    "write" => {
                        is_write = true;
                        opts.write(true);
                    }
                    "append" => {
                        is_write = true;
                        opts.append(true);
                    }
                    "truncate" => {
                        is_write = true;
                        opts.write(true);
                        opts.truncate(true);
                    }
                    "create" => {
                        is_write = true;
                        opts.write(true);
                        opts.create(true);
                    }
                    "create-new" => {
                        is_write = true;
                        opts.write(true);
                        opts.create_new(true);
                    }
                    "on-error-nil" => {
                        error_nil = true;
                    }
                    _ => {
                        let msg = format!("open: invalid directive, {}", vm.get_interned(*i));
                        return Err(VMError::new("io", msg));
                    }
                };
            } else {
                let msg = format!("fopen: {} invalid", a.display_type(vm));
                return Err(VMError::new("io", msg));
            }
        }
        if !is_write && !is_read {
            is_read = true;
            opts.read(true);
        }
        let file = match opts.open(&file_name) {
            Ok(file) => file,
            Err(err) => {
                if error_nil {
                    return Ok(Value::Nil);
                } else {
                    return Err(VMError::new(
                        "io",
                        format!("fopen: Error opening {}: {}", file_name.display(), err),
                    ));
                }
            }
        };
        return match (is_read, is_write) {
            (true, true) | (false, false) => {
                let io = HeapIo::from_file(file);
                Ok(vm.alloc_io(io))
            }
            (true, false) => {
                let io = HeapIo::from_file(file);
                io.to_buf_reader()
                    .expect("Could not create a buf reader for file open to read!");
                Ok(vm.alloc_io(io))
            }
            (false, true) => {
                let io = HeapIo::from_file(file);
                io.to_buf_writer()
                    .expect("Could not create a buf writer for file open to write!");
                Ok(vm.alloc_io(io))
            }
        };
    }
    Err(VMError::new(
        "io",
        "fopen takes at least one form (a file name)",
    ))
}

fn fclose(vm: &mut SloshVm, registers: &[Value]) -> VMResult<Value> {
    let mut args = registers.iter();
    if let (Some(exp), None) = (args.next(), args.next()) {
        return if let Value::Io(h) = exp {
            let io = vm.get_io(*h);
            io.close();
            Ok(Value::True)
        } else {
            Err(VMError::new("io", "fclose requires a file"))
        };
    }
    Err(VMError::new("io", "fclose takes one form (file to close)"))
}

fn builtin_read_line(vm: &mut SloshVm, registers: &[Value]) -> VMResult<Value> {
    let mut args = registers.iter();
    if let (Some(Value::Io(h)), None) = (args.next(), args.next()) {
        let mut io = vm.get_io(*h).get_io();

        let mut byte = [0_u8];
        let mut line_bytes = Vec::new();
        while io.read(&mut byte)? == 1 {
            line_bytes.push(byte[0]);
            if byte[0] == b'\n' {
                break;
            }
        }
        let line = match String::from_utf8(line_bytes) {
            Ok(line) => line,
            Err(e) => return Err(VMError::new("read", e.to_string())),
        };
        drop(io);
        if line.is_empty() {
            Ok(Value::Nil)
        } else {
            Ok(vm.alloc_string(line))
        }
    } else {
        Err(VMError::new("io", "read-line takes one form (file)"))
    }
}

fn builtin_flush(vm: &mut SloshVm, registers: &[Value]) -> VMResult<Value> {
    let mut args = registers.iter();
    if let (Some(Value::Io(h)), None) = (args.next(), args.next()) {
        let mut io = vm.get_io(*h).get_io();
        io.flush()?;
        Ok(Value::True)
    } else {
        Err(VMError::new("io", "fflush takes one form (file)"))
    }
}

/// Usage: (fs-rm \"/dir/or/file/to/remove\")
///
/// Takes a file or directory as a string and removes it. Works recursively for directories.
///
/// Section: file
///
/// Example:
/// (def fp nil)
/// (let (a-file (get-temp-file))
///     (test::assert-true (fs-exists? a-file))
///     (set! fp a-file)
///     (fs-rm a-file))
/// (test::assert-false (nil? fp))
/// (test::assert-false (fs-exists? fp))
#[sl_sh_fn(fn_name = "fs-rm")]
fn fs_rm(path: String) -> VMResult<Value> {
    let path = expand_tilde(path.into());
    let p = path.as_path();
    if p.exists() {
        if p.is_dir() {
            fs::remove_dir_all(p).map_err(|e| VMError::new("io", e.to_string()))?;
        } else {
            fs::remove_file(p).map_err(|e| VMError::new("io", e.to_string()))?;
        };
        Ok(Value::True)
    } else {
        Err(VMError::new(
            "io",
            format!("path does not exist: {}", path.display()),
        ))
    }
}

pub fn add_io_builtins(env: &mut SloshVm) {
    intern_fs_rm(env);

    add_builtin(
        env,
        "fopen",
        fopen,
        "Usage: (fopen filename option*)

Open a file.  If you use :read and :write then you get a read/write unbuffered file.  Including
one of :read or :write will provide a file buffered for read or write (this is faster).
Note: :append, :truncate, :create, :create-new all imply :write.

Options are:
    :read
    :write
    :append
    :truncate
    :create
    :create-new
    :on-error-nil

Section: file

Example:
(with-temp-file (fn (tmp-file)
    (let (test-open-f (fopen tmp-file :create :truncate))
        (fprn test-open-f \"Test Line One\")
        (fclose test-open-f)
        (set! test-open-f (fopen tmp-file :read))
        (defer (fclose test-open-f))
        (test::assert-equal \"Test Line One\n\" (read-line test-open-f)))))
",
    );
    add_builtin(
        env,
        "fclose",
        fclose,
        "Usage: (fclose file)

Close a file.

Section: file

Example:
(with-temp-file (fn (tmp-file)
    (let (tst-file (fopen tmp-file :create :truncate))
        (fprn tst-file \"Test Line Two\")
        (fclose tst-file)
        (set! tst-file (fopen tmp-file :read))
        (defer (fclose tst-file))
        (test::assert-equal \"Test Line Two\n\" (read-line tst-file)))))
",
    );
    add_builtin(
        env,
        "read-line",
        builtin_read_line,
        r#"Usage: (read-line file) -> string

Read a line from a file.  Returns Nil if there is nothing left to read.

Section: file

Example:
(with-temp-file (fn (tmp)
    (let (tst-file (fopen tmp :create :truncate))
        (fprn tst-file "Test Line Read Line One")
        (fpr tst-file "Test Line Read Line Two")
        (fclose tst-file)
        (set! tst-file (fopen tmp :read))
        (defer (fclose tst-file))
        (test::assert-equal "Test Line Read Line One\n" (read-line tst-file))
        (test::assert-equal "Test Line Read Line Two" (read-line tst-file)))))
"#,
    );
    add_builtin(
        env,
        "fflush",
        builtin_flush,
        "Usage: (flush file)

Flush a file.

Section: file

Example:
(with-temp-file (fn (tmp-file)
    (let (tst-file (fopen tmp-file :create :truncate)
          tst-file-read (fopen tmp-file :read))
        (defer (fclose tst-file))
        (defer (fclose tst-file-read))
        (fprn tst-file \"Test Line Three\")
        (fflush tst-file)
        (test::assert-equal \"Test Line Three\n\" (read-line tst-file-read)))))
",
    );
}