diff --git a/cli/go.mod b/cli/go.mod index 6bd6b99..3aa8505 100644 --- a/cli/go.mod +++ b/cli/go.mod @@ -7,4 +7,5 @@ require ( github.com/gorilla/websocket v1.4.2 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pkg/term v1.1.0 // indirect + golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect ) diff --git a/cli/go.sum b/cli/go.sum index 2362830..2ca51e4 100644 --- a/cli/go.sum +++ b/cli/go.sum @@ -13,6 +13,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= golang.org/x/sys v0.0.0-20200909081042-eff7692f9009 h1:W0lCpv29Hv0UaM1LXb9QlBHLNP8UFfcKjblhVCWftOM= golang.org/x/sys v0.0.0-20200909081042-eff7692f9009/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf h1:2ucpDCmfkl8Bd/FsLtiD653Wf96cW37s+iGx93zsu4k= +golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/cli/src/main.go b/cli/src/main.go index 512a6d1..f79ce45 100644 --- a/cli/src/main.go +++ b/cli/src/main.go @@ -16,12 +16,14 @@ import ( "github.com/alecthomas/kong" "github.com/gorilla/websocket" "github.com/pkg/errors" - "github.com/pkg/term" + "github.com/pkg/term/termios" + "golang.org/x/sys/unix" ) var cli struct { - Lang string `arg help:"Name of programming language."` - File string `arg optional type:"path" help:"File to run."` + Lang string `arg:"" help:"Name of programming language."` + File string `arg:"" optional:"" type:"existingfile" help:"File to run."` + Raw bool `short:"r" default:"false" help:"Pass ctrl-C to Riju instead of terminating the connection."` Host string `default:"https://riju.codes/api/v1" help:"URL of Riju API."` } @@ -78,14 +80,21 @@ func run() error { return err } defer conn.Close() - tty, err := term.Open("/dev/tty") - if err != nil { - return errors.Wrap(err, "failed to open stdin tty") + var origAttr unix.Termios + if err := termios.Tcgetattr(os.Stdin.Fd(), &origAttr); err != nil { + return err } - if err := tty.SetRaw(); err != nil { - return errors.Wrap(err, "failed to set raw mode") + rawAttr := origAttr + termios.Cfmakeraw(&rawAttr) + if !cli.Raw { + // Do not pass ctrl-C over pty, instead invoke our + // signal handler and abort. + rawAttr.Lflag |= syscall.ISIG } - defer tty.Restore() + if err := termios.Tcsetattr(os.Stdin.Fd(), termios.TCSAFLUSH, &rawAttr); err != nil { + return err + } + defer termios.Tcsetattr(os.Stdin.Fd(), termios.TCSAFLUSH, &origAttr) sigint := make(chan os.Signal, 1) sigterm := make(chan os.Signal, 1) signal.Notify(sigint, syscall.SIGINT)