Shells in OCaml
3
fork

Configure Feed

Select the types of activity you want to include in your feed.

Output Redirection

Simple output redirection using > and >>

+51 -1
+1
src/bin/main.ml
··· 21 21 state = Merry_posix.State.make ~home:(Sys.getenv "HOME") (); 22 22 executor; 23 23 fs = env#fs; 24 + options = Merry.Eval.Options.default; 24 25 } 25 26 ast 26 27 in
+26 -1
src/lib/eval.ml
··· 4 4 -----------------------------------------------------------------*) 5 5 open Import 6 6 7 + module Options = struct 8 + type t = { noclobber : bool } 9 + 10 + let default = { noclobber = false } 11 + 12 + let with_options ?noclobber t = 13 + { noclobber = Option.value ~default:t.noclobber noclobber } 14 + end 15 + 7 16 (** An evaluator over the AST *) 8 17 module Make (S : Types.State) (E : Types.Exec) = struct 9 18 (* What follows uses the POSIX definition of what a shell does ($ 2.1). ··· 17 26 | WordLiteral s -> s 18 27 | WordDoubleQuoted s -> 19 28 String.concat " " (List.map word_component_to_string s) 29 + | WordSingleQuoted s -> 30 + String.concat " " (List.map word_component_to_string s) 20 31 | v -> 21 32 Fmt.failwith "Conversion of %a" Yojson.Safe.pp 22 33 (Ast.word_component_to_yojson v) ··· 36 47 method list f t = List.map f t 37 48 end 38 49 39 - type ctx = { state : S.t; executor : E.t; fs : Eio.Fs.dir_ty Eio.Path.t } 50 + type ctx = { 51 + state : S.t; 52 + executor : E.t; 53 + fs : Eio.Fs.dir_ty Eio.Path.t; 54 + options : Options.t; 55 + } 40 56 41 57 class default_ctx_fold = 42 58 object (_) ··· 145 161 (Types.Redirect 146 162 (n, fd_of_int ~close_unix:false ~sw m, `Blocking)) 147 163 | _ -> None) 164 + | (Io_op_great | Io_op_dgreat) as v -> 165 + (* Simple file creation *) 166 + let append = v = Io_op_dgreat in 167 + let w = 168 + Eio.Path.open_out ~sw ~append ~create:(`If_missing 0o644) 169 + (ctx.fs / word_components_to_string file) 170 + in 171 + let fd = Eio_unix.Resource.fd_opt w |> Option.get in 172 + Some (Types.Redirect (n, fd, `Blocking)) 148 173 | _ -> Fmt.failwith "Redirections ...") 149 174 | Ast.IoRedirect_IoHere _ -> 150 175 Fmt.failwith "HERE documents not yet implemented!"
+24
test/simple.t
··· 74 74 $ osh -c "cat 3<hello.md <&3" 75 75 # Hello, World! 76 76 --------------- 77 + 78 + But also, some simple errors should work too. 79 + 80 + $ osh -c "cat <1" 81 + osh: internal error, uncaught exception: 82 + Eio.Io Fs Not_found Unix_error (No such file or directory, "openat", "1"), 83 + opening <fs:1> 84 + 85 + [125] 86 + 87 + 2.6 Output Redirection 88 + 89 + Simple example of redirecting some output 90 + 91 + $ osh -c "echo hello > hello.txt && cat hello.txt" 92 + hello 93 + $ stat --format="%A" hello.txt 94 + -rw-r--r-- 95 + 96 + Simple appending example 97 + 98 + $ osh -c "echo world >> hello.txt && cat hello.txt" 99 + hello 100 + world