#!/usr/bin/pil
# 18jun23abu
# Pseudo Terminal (PilBox)
# pty [host] [flg]

(load "@lib/term.l")

(setq *Port 8081)  # Sync with ~/Port in PilBox

(unless (setq *Sock (connect (or (opt) "localhost") (inc *Port)))
   (bye) )
(out *Sock (in "~/.pty" (echo)))  # Sync with ~/.pty in PilBox

(finish (prinl))

(de sendCmd @
   (out *Sock (space) (pass println))
   (in *Sock (till "-") (till "\n")) )

(unless (opt)
   (task (port (+ *Port 2))
      (let? S (accept @)
         (catch '(NIL)
            (in S
               (when (= (rd) (in "~/.pty" (line T)))
                  (casq (rd)
                     (+ (in (rd) (out S (echo))))
                     (- (out (rd) (echo))) ) ) )
            (off *Msg) )
         (close S)
         (and *Msg (prinl @)) ) ) )

(when (getTerm)
   (sendCmd
      (cons 'setTerm (sys "TERM") @)
      '(off *Err) )
   (de *Winch
      (sendCmd (cons 'setTerm (sys "TERM") (getTerm))) ) )

(raw T)
(call "stty" "intr" NIL)

(task *Sock
   (in @
      (ifn (rd 1)
         (bye)
         (wr @)
         (flush) ) ) )

(loop
   (and (key) (out *Sock (prin @))) )
