@@ -131,6 +131,31 @@ def Rd.go(fuel: Nat, s: Socket, st: Rd, +want: U32) -> IO(Socket & Rd):
131131 IO.bind(Socket & Result<&1, &1, U32 & String, List<&2, U32>>, Socket & Rd, H.Sock.recv_bytes(s, 65536),
132132 r => Rd.got(r, st, want, s2 => st2 => Rd.go(f, s2, st2, want)))
133133
134+ # reading until the screen shows text (a byte count is not enough: a
135+ # Windows pseudoconsole sends mode escapes before the program's first line)
136+ def Rd.till.k(+st: Rd, s: Socket, +text: String, k: Socket -> Rd -> IO(Socket & Rd)) -> IO(Socket & Rd):
137+ match st:
138+ case Rd{+v, +n}:
139+ Rd.more(has(screen(v), text), s, Rd{v, n}, k)
140+
141+ def Rd.till.got(r: Socket & Result<&1, &1, U32 & String, List<&2, U32>>, st: Rd, +text: String, k: Socket -> Rd -> IO(Socket & Rd)) -> IO(Socket & Rd):
142+ (s, res) = r
143+ match res st:
144+ case Done{Nil{}} _:
145+ IO.pure(Socket & Rd, (s, st))
146+ case Done{bs} Rd{v, +n}:
147+ Rd.till.k(Rd.fed(v, n, bs), s, text, k)
148+ case Fail{_} _:
149+ IO.pure(Socket & Rd, (s, st))
150+
151+ def Rd.till(fuel: Nat, s: Socket, st: Rd, +text: String) -> IO(Socket & Rd):
152+ match fuel:
153+ case 0n:
154+ IO.pure(Socket & Rd, (s, st))
155+ case 1n+f:
156+ IO.bind(Socket & Result<&1, &1, U32 & String, List<&2, U32>>, Socket & Rd, H.Sock.recv_bytes(s, 65536),
157+ r => Rd.till.got(r, st, text, s2 => st2 => Rd.till(f, s2, st2, text)))
158+
134159def Rd.vt(st: Rd) -> V.Vt:
135160 match st:
136161 case Rd{v, _}:
@@ -199,7 +224,7 @@ def case.size.more(r: Socket & V.Vt, +pid: U32) -> IO(U32):
199224def case.size.k(started: U32 & Socket) -> IO(U32):
200225 (+pid, s) = started
201226 do IO<U32>:
202- r1 : Socket & V.Vt <- IO.bind(Socket & Rd, Socket & V.Vt, Rd.go (1000n, s, Rd{V.Vt.new(100, 30), 0}, 6 ), r => IO.pure(Socket & V.Vt, Rd.keep(r)))
227+ r1 : Socket & V.Vt <- IO.bind(Socket & Rd, Socket & V.Vt, Rd.till (1000n, s, Rd{V.Vt.new(100, 30), 0}, "24 80" ), r => IO.pure(Socket & V.Vt, Rd.keep(r)))
203228 a : U32 <- case.size.more(r1, pid)
204229 return a
205230
@@ -229,27 +254,35 @@ def case.env(r: Result<&1, &1, U32 & String, U32 & Socket>) -> IO(U32):
229254 case Fail{e}:
230255 fail("pty env", e)
231256
232- # closing the master hangs up the shell (SIGHUP: 129)
233- def case.close.more(r: Socket & V.Vt, +pid: U32) -> IO(U32):
257+ # closing the master hangs up the shell (SIGHUP: 129; on Windows the
258+ # pseudoconsole closes and the shell ends with a code of its own)
259+ def case.close.code(win: Bool, +code: U32) -> IO(U32):
260+ match win:
261+ case True{}:
262+ check("pty close ends the child", Bool.show(Bool.not(U32.is_eq(code, 0))), "True")
263+ case False{}:
264+ check("pty close kills child", U32.show(code), "129")
265+
266+ def case.close.more(r: Socket & V.Vt, +pid: U32, win: Bool) -> IO(U32):
234267 (s, +v) = r
235268 do IO<U32>:
236269 Socket.close(s)
237270 code : U32 <- H.Proc.wait(pid)
238271 a : U32 <- check.has("pty child up", screen(v), "up")
239- b : U32 <- check("pty close kills child", U32.show( code), "129" )
272+ b : U32 <- case. close. code(win, code )
240273 return (a + b : U32)
241274
242- def case.close.k(started: U32 & Socket) -> IO(U32):
275+ def case.close.k(started: U32 & Socket, win: Bool ) -> IO(U32):
243276 (+pid, s) = started
244277 do IO<U32>:
245- r1 : Socket & V.Vt <- IO.bind(Socket & Rd, Socket & V.Vt, Rd.go (1000n, s, Rd{V.Vt.new(80, 24), 0}, 3 ), r => IO.pure(Socket & V.Vt, Rd.keep(r)))
246- a : U32 <- case.close.more(r1, pid)
278+ r1 : Socket & V.Vt <- IO.bind(Socket & Rd, Socket & V.Vt, Rd.till (1000n, s, Rd{V.Vt.new(80, 24), 0}, "up" ), r => IO.pure(Socket & V.Vt, Rd.keep(r)))
279+ a : U32 <- case.close.more(r1, pid, win )
247280 return a
248281
249- def case.close(r: Result<&1, &1, U32 & String, U32 & Socket>) -> IO(U32):
282+ def case.close(win: Bool, r: Result<&1, &1, U32 & String, U32 & Socket>) -> IO(U32):
250283 match r:
251284 case Done{started}:
252- case.close.k(started)
285+ case.close.k(started, win )
253286 case Fail{e}:
254287 fail("pty close", e)
255288
@@ -267,7 +300,7 @@ def case.hup.more(r: Socket & V.Vt, +pid: U32) -> IO(U32):
267300def case.hup.k(started: U32 & Socket) -> IO(U32):
268301 (+pid, s) = started
269302 do IO<U32>:
270- r1 : Socket & V.Vt <- IO.bind(Socket & Rd, Socket & V.Vt, Rd.go (1000n, s, Rd{V.Vt.new(80, 24), 0}, 3 ), r => IO.pure(Socket & V.Vt, Rd.keep(r)))
303+ r1 : Socket & V.Vt <- IO.bind(Socket & Rd, Socket & V.Vt, Rd.till (1000n, s, Rd{V.Vt.new(80, 24), 0}, "up" ), r => IO.pure(Socket & V.Vt, Rd.keep(r)))
271304 a : U32 <- case.hup.more(r1, pid)
272305 return a
273306
@@ -395,19 +428,39 @@ def done.k(ok: Bool, +n: U32) -> IO(Unit):
395428def done(+n: U32) -> IO(Unit):
396429 done.k(U32.is_eq(n, 0), n)
397430
431+ # a case that needs a POSIX host (/tmp as a path, /proc), named as skipped on
432+ # Windows
433+ def posix.only(win: Bool, name: String, run: IO(U32)) -> IO(U32):
434+ match win:
435+ case True{}:
436+ do IO<U32>:
437+ IO.print("ok " ++ name ++ " skipped on Windows")
438+ return 0
439+ case False{}:
440+ run
441+
442+ def is.win(r: Result<&1, &1, U32 & String, String>) -> Bool:
443+ match r:
444+ case Done{os}:
445+ String.eq(os, "Windows_NT")
446+ case Fail{_}:
447+ False{}
448+
398449def main() -> IO(Unit):
399450 do IO<Unit>:
400451 # the runtime's loop (loop.js) is part of the program, as in the hub:
401452 # on Windows nothing spawns without it
402453 rt : String <- LP.Rt.loop()
403454 IO.print("js pty: starting (" ++ rt ++ " loop)")
455+ os : Result<&1, &1, U32 & String, String> <- IO.get_env("OS")
456+ +win : Bool = is.win(os)
404457 a : U32 <- IO.bind(Result<&1, &1, U32 & String, U32 & Socket>, U32, P.Pty.spawn("sh", [], "", 80, 24), case.shell)
405458 b : U32 <- IO.bind(Result<&1, &1, U32 & String, U32 & Socket>, U32, P.Pty.spawn("sh", ["-c", "stty size; read x; stty size"], "", 80, 24), case.size)
406- c : U32 <- IO.bind(Result<&1, &1, U32 & String, U32 & Socket>, U32, P.Pty.spawn("sh", ["-c", "echo $TERM; echo $COLORTERM; echo $LINES$COLUMNS; pwd; [ -t 0 ] && printf tty; : </dev/tty && echo ' ctty'; echo $(ls /dev/fd/)"], "/tmp", 80, 24), case.env)
407- d : U32 <- IO.bind(Result<&1, &1, U32 & String, U32 & Socket>, U32, P.Pty.spawn("sh", ["-c", "echo up; sleep 30; echo done"], "", 80, 24), case.close)
459+ c : U32 <- posix.only(win, "pty env cwd ctty", IO.bind(Result<&1, &1, U32 & String, U32 & Socket>, U32, P.Pty.spawn("sh", ["-c", "echo $TERM; echo $COLORTERM; echo $LINES$COLUMNS; pwd; [ -t 0 ] && printf tty; : </dev/tty && echo ' ctty'; echo $(ls /dev/fd/)"], "/tmp", 80, 24), case.env) )
460+ d : U32 <- IO.bind(Result<&1, &1, U32 & String, U32 & Socket>, U32, P.Pty.spawn("sh", ["-c", "echo up; sleep 30; echo done"], "", 80, 24), r => case.close(win, r) )
408461 e : U32 <- IO.bind(Result<&1, &1, U32 & String, U32 & Socket>, U32, P.Pty.spawn("definitely-not-a-command", [], "", 80, 24), case.missing)
409462 f : U32 <- IO.bind(Result<&1, &1, U32 & String, U32 & Socket>, U32, P.Pty.spawn("sh", ["-c", "true"], "/definitely/not/a/dir", 80, 24), case.badcwd)
410463 g : U32 <- IO.bind(Result<&1, &1, U32 & String, U32 & Socket>, U32, P.Pty.spawn("sh", ["-c", "echo up; sleep 30; echo done"], "", 80, 24), case.hup)
411464 h : U32 <- IO.bind(Result<&1, &1, U32 & String, U32 & Socket>, U32, P.Pty.spawn("sh", ["-c", "stty raw -echo; sleep 1; head -c 20000 | wc -c"], "", 80, 24), case.full)
412- i : U32 <- IO.bind(Result<&1, &1, U32 & String, U32 & Socket>, U32, P.Pty.spawn("sh", ["-c", "c=$(ulimit -s); p=$(awk '/Max stack size/{print $4}' /proc/$PPID/limits); if [ \"$p\" = unlimited ] || [ \"$p\" -gt 8388608 ]; then w=8192; else w=$((p/1024)); fi; [ \"$c\" = \"$w\" ] && echo stack-ok || echo \"child $c hub $p\""], "", 80, 24), case.stack)
465+ i : U32 <- posix.only(win, "pty stack limit", IO.bind(Result<&1, &1, U32 & String, U32 & Socket>, U32, P.Pty.spawn("sh", ["-c", "c=$(ulimit -s); p=$(awk '/Max stack size/{print $4}' /proc/$PPID/limits); if [ \"$p\" = unlimited ] || [ \"$p\" -gt 8388608 ]; then w=8192; else w=$((p/1024)); fi; [ \"$c\" = \"$w\" ] && echo stack-ok || echo \"child $c hub $p\""], "", 80, 24), case.stack) )
413466 done((a + b + c + d + e + f + g + h + i : U32))
0 commit comments