# Pipes to a started program, and this program's stdin, as Files of UTF-8 lines. # A POLYFILL: Bend has no pipes to a started program, no way to kill one, and no stdin handle yet. # Delete this module when Base has them, and call Base instead. # A line has at most max bytes with its LF or CRLF: a longer one fails with E2BIG (7). import Base # Code # ==== # This program's stdin. A File owns its descriptor: ask for it once. def stdin() -> IO(File): import "./std.c" import "./std.js" # Start a program, with its stdin and stdout as pipes: its process id, the File that writes its # stdin, and the File that reads its stdout. Its stderr is ours. Closing the stdin File gives it # EOF; nothing waits for it to exit. On JS, it runs on Bun on Linux and macOS only; elsewhere it # fails with ENOSYS. def open( program: String, args: List ) -> IO(Result<&1, &1, U32 & String, U32 & (File & File)>): import "./open.c" import "./open.js" # Kills the program that open started with pid, unless it has ended: then it fails with ESRCH (3). # It never kills a program that open did not start, though the system gives an ended program's pid # to another one. def kill(pid: U32) -> IO(Result<&1, &1, U32 & String, Unit>): import "./open.c" import "./open.js" # A line with no LF that ends the input is the last line; one that does not is too long. def read_line.peeked( next: File & Result<&1, &1, U32 & String, String>, text: String ) -> File & Result<&1, &1, U32 & String, Maybe>: match next: case (file, Fail{error}): (file, Fail{error}) case (file, Done{SNil{}}): (file, Done{Some{text}}) case (file, Done{SCon{_, _}}): (file, Fail{(7, "a line is longer than its cap")}) # The line of a piece that ends with its LF: the piece without the LF, and the CR before it, if any. def read_line.unended(+piece: String) -> String: +line = String.take(piece, Nat.sub(String.length(piece), 1n)) Bool.pick( String, String.ends_with(line, "\r"), String.take(line, Nat.sub(String.length(line), 1n)), line ) def read_line.if( file: File, ended: Bool, piece: String ) -> IO(File & Result<&1, &1, U32 & String, Maybe>): match ended: case True{}: IO.pure( File & Result<&1, &1, U32 & String, Maybe>, (file, Done{Some{read_line.unended(piece)}}) ) case False{}: do IO>>: next: File & Result<&1, &1, U32 & String, String> <- File.read_line(file, 1) return read_line.peeked(next, piece) def read_line.got( got: File & Result<&1, &1, U32 & String, String> ) -> IO(File & Result<&1, &1, U32 & String, Maybe>): match got: case (file, Fail{error}): IO.pure(File & Result<&1, &1, U32 & String, Maybe>, (file, Fail{error})) case (file, Done{SNil{}}): IO.pure(File & Result<&1, &1, U32 & String, Maybe>, (file, Done{None{}})) case (file, Done{SCon{h, t}}): +piece: String = SCon{h, t} read_line.if(file, String.ends_with(piece, "\n"), piece) # The next line of file, as it comes, without its LF or CRLF; None at the end of the file. def read_line(file: File, max: U32) -> IO(File & Result<&1, &1, U32 & String, Maybe>): IO.bind( File & Result<&1, &1, U32 & String, String>, File & Result<&1, &1, U32 & String, Maybe>, File.read_line(file, max), read_line.got ) def write_lines.text(lines: List) -> String: match lines: case Nil{}: "" case Con{line, rest}: line ++ "\n" ++ write_lines.text(rest) # Writes each line to file, with its LF. def write_lines(file: File, lines: List) -> IO(File & Result<&1, &1, U32 & String, Unit>): File.write(file, write_lines.text(lines))