impl.bend source
impl.bend on the hub · documented module
# Talks in lines to a started program, over pipes, with or without a deadline.# Send lines, then read lines until one starts with a given text. It knows no protocol.import Baseimport bend-stdio-pipes@0.2.0.0/impl.bend as Stdio# A running program: its process id, the File that writes its stdin, the File that reads its stdout,# and the most bytes in one of its lines, with its line end.type PipeSession is Type: PipeSession{pid: U32, input: File, out: File, line_cap: U32}# A line of the program, as PipeSession.ask sorts it.type Heard is Type: Line{text: String} Last{text: String} Ended{} Failed{error: U32 & String}def PipeSession.pid(session: PipeSession) -> PipeSession & U32: match session: case PipeSession{+pid, input, out, line_cap}: (PipeSession{pid, input, out, line_cap}, pid)def PipeSession.started( opened: Result<&1, &1, U32 & String, U32 & (File & File)>, line_cap: U32) -> Result<&1, &1, U32 & String, PipeSession>: match opened: case Fail{error}: Fail{error} case Done{(pid, (input, out))}: Done{PipeSession{pid, input, out, line_cap}}# Starts program with args. A longer line of it fails with E2BIG (7).def PipeSession.start( program: String, args: List<String>, line_cap: U32) -> IO(Result<&1, &1, U32 & String, PipeSession>): do IO<Result<&1, &1, U32 & String, PipeSession>>: opened: Result<&1, &1, U32 & String, U32 & (File & File)> <- Stdio.open(program, args) return PipeSession.started(opened, line_cap)# The code of the error that PipeSession.ask gives when the program ended before its answer:# EPIPE.def PipeSession.ended() -> U32: 32def Heard.read( read: File & Result<&1, &1, U32 & String, Maybe<String>>, +until: String) -> File & Heard: match read: case (out, Fail{error}): (out, Failed{error}) case (out, Done{None{}}): (out, Ended{}) case (out, Done{Some{line}}): +text = String.trim_end(line) (out, Bool.pick(Heard, String.starts_with(text, until), Last{text}, Line{text}))# said holds the lines so far, the last first; fuel bounds the lines, far beyond any answer.def PipeSession.ask.go( fuel: Nat, at: File & Heard, +until: String, +line_cap: U32, said: List<&2, String>) -> IO(File & Result<&1, &1, U32 & String, List<&2, String>>): match fuel: case 0n: match at: case (out, _): IO.pure( File & Result<&1, &1, U32 & String, List<&2, String>>, (out, Fail{(5, "the program said too many lines before " ++ until)}) ) case 1n+rest: match at: case (out, Failed{error}): IO.pure(File & Result<&1, &1, U32 & String, List<&2, String>>, (out, Fail{error})) case (out, Ended{}): IO.pure( File & Result<&1, &1, U32 & String, List<&2, String>>, (out, Fail{(PipeSession.ended(), "the program ended before it said " ++ until)}) ) case (out, Last{text}): IO.pure( File & Result<&1, &1, U32 & String, List<&2, String>>, (out, Done{List.reverse(&2, String, text <> said)}) ) case (out, Line{text}): do IO<File & Result<&1, &1, U32 & String, List<&2, String>>>: read: File & Result<&1, &1, U32 & String, Maybe<String>> <- Stdio.read_line(out, line_cap) PipeSession.ask.go(rest, Heard.read(read, until), until, line_cap, text <> said)def PipeSession.asked( pid: U32, input: File, line_cap: U32, answer: File & Result<&1, &1, U32 & String, List<&2, String>>) -> IO(PipeSession & Result<&1, &1, U32 & String, List<&2, String>>): match answer: case (out, result): IO.pure( PipeSession & Result<&1, &1, U32 & String, List<&2, String>>, (PipeSession{pid, input, out, line_cap}, result) )def PipeSession.wrote( pid: U32, wrote: File & Result<&1, &1, U32 & String, Unit>, out: File, +line_cap: U32, +until: String) -> IO(PipeSession & Result<&1, &1, U32 & String, List<&2, String>>): match wrote: case (input, Fail{error}): IO.pure( PipeSession & Result<&1, &1, U32 & String, List<&2, String>>, (PipeSession{pid, input, out, line_cap}, Fail{error}) ) case (input, Done{_}): do IO<PipeSession & Result<&1, &1, U32 & String, List<&2, String>>>: read: File & Result<&1, &1, U32 & String, Maybe<String>> <- Stdio.read_line(out, line_cap) answer: File & Result<&1, &1, U32 & String, List<&2, String>> <- PipeSession.ask.go(4294967295n, Heard.read(read, until), until, line_cap, []) PipeSession.asked(pid, input, line_cap, answer)# Sends lines, then answers the program's lines up to the first that starts with until.def PipeSession.ask( session: PipeSession, lines: List<String>, +until: String) -> IO(PipeSession & Result<&1, &1, U32 & String, List<&2, String>>): match session: case PipeSession{pid, input, out, line_cap}: do IO<PipeSession & Result<&1, &1, U32 & String, List<&2, String>>>: wrote: File & Result<&1, &1, U32 & String, Unit> <- Stdio.write_lines(input, lines) PipeSession.wrote(pid, wrote, out, line_cap, until)# Kills the program, whether or not it has ended, and closes its Files.def PipeSession.kill(session: PipeSession) -> IO(Unit): match session: case PipeSession{pid, input, out, _}: do IO<Unit>: killed: Result<&1, &1, U32 & String, Unit> <- Stdio.kill(pid) File.close(input) File.close(out) return Unit{}# What PipeSession.ask gives.def PipeSession.Answer() -> Type: PipeSession & Result<&1, &1, U32 & String, List<&2, String>># The answer that came once the program was killed: its Files close.def PipeSession.ask.late(late: Maybe<&1, PipeSession.Answer()>) -> IO(Maybe<PipeSession.Answer()>): match late: case Some{(PipeSession{_, input, out, _}, _)}: do IO<Maybe<PipeSession.Answer()>>: File.close(input) File.close(out) return None{} case None{}: IO.pure(Maybe<PipeSession.Answer()>, None{})def PipeSession.ask.polled( +pid: U32, +chan: Chan(PipeSession.Answer()), polled: Poll<&1, &1, Maybe<&1, PipeSession.Answer()>, Unit>) -> IO(Maybe<PipeSession.Answer()>): match polled: case Ready{Some{answer}}: do IO<Maybe<PipeSession.Answer()>>: Chan.close(PipeSession.Answer(), chan) return Some{answer} case Ready{None{}}: IO.pure(Maybe<PipeSession.Answer()>, None{}) case Wait{_}: do IO<Maybe<PipeSession.Answer()>>: killed: Result<&1, &1, U32 & String, Unit> <- Stdio.kill(pid) late: Maybe<&1, PipeSession.Answer()> <- Chan.recv(PipeSession.Answer(), chan) Chan.close(PipeSession.Answer(), chan) PipeSession.ask.late(late)# As PipeSession.ask, within ms; None when the program has not answered by then: it is killed, and# the session is gone. Nothing of it runs on after.def PipeSession.ask.within( session: PipeSession, lines: List<String>, +until: String, ms: U32) -> IO(Maybe<PipeSession.Answer()>): match session: case PipeSession{+pid, input, out, line_cap}: do IO<Maybe<PipeSession.Answer()>>: +chan: Chan(PipeSession.Answer()) <- Chan.new(PipeSession.Answer(), 1) forked: Chan( PipeSession.Answer() ) <- IO.fork.go( PipeSession.Answer(), PipeSession.ask(PipeSession{pid, input, out, line_cap}, lines, until), chan ) polled: Poll<&1, &1, Maybe<&1, PipeSession.Answer()>, Unit> <- Chan.try_recv(PipeSession.Answer(), chan, ms) PipeSession.ask.polled(pid, chan, polled)def PipeSession.end.go( fuel: Nat, read: File & Result<&1, &1, U32 & String, Maybe<String>>, +line_cap: U32, said: List<&2, String>) -> IO(Result<&1, &1, U32 & String, List<&2, String>>): match fuel read: case 0n Tuple{out, _}: do IO<Result<&1, &1, U32 & String, List<&2, String>>>: File.close(out) return Fail{(5, "the program said too many lines after its input ended")} case 1n+rest Tuple{out, Fail{error}}: do IO<Result<&1, &1, U32 & String, List<&2, String>>>: File.close(out) return Fail{error} case 1n+rest Tuple{out, Done{None{}}}: do IO<Result<&1, &1, U32 & String, List<&2, String>>>: File.close(out) return Done{List.reverse(&2, String, said)} case 1n+rest Tuple{out, Done{Some{line}}}: do IO<Result<&1, &1, U32 & String, List<&2, String>>>: next: File & Result<&1, &1, U32 & String, Maybe<String>> <- Stdio.read_line(out, line_cap) PipeSession.end.go(rest, next, line_cap, String.trim_end(line) <> said)# Closes the program's stdin, then answers what it said until it ended.def PipeSession.end(session: PipeSession) -> IO(Result<&1, &1, U32 & String, List<&2, String>>): match session: case PipeSession{_, input, out, +line_cap}: do IO<Result<&1, &1, U32 & String, List<&2, String>>>: File.close(input) read: File & Result<&1, &1, U32 & String, Maybe<String>> <- Stdio.read_line(out, line_cap) PipeSession.end.go(4294967295n, read, line_cap, [])