Browse code

#214 Better signal handling

Guillaume J. Charmes authored on 2013/03/28 15:54:53
Showing 2 changed files
... ...
@@ -8,6 +8,7 @@ import (
8 8
 	"io"
9 9
 	"log"
10 10
 	"os"
11
+	"os/signal"
11 12
 )
12 13
 
13 14
 func main() {
... ...
@@ -53,6 +54,15 @@ func runCommand(args []string) error {
53 53
 			return err
54 54
 		}
55 55
 		defer term.Restore(0, oldState)
56
+		c := make(chan os.Signal, 1)
57
+		signal.Notify(c, os.Interrupt)
58
+		go func() {
59
+			for _ = range c {
60
+				term.Restore(0, oldState)
61
+				log.Printf("\nSIGINT received\n")
62
+				os.Exit(0)
63
+			}
64
+		}()
56 65
 	}
57 66
 	// FIXME: we want to use unix sockets here, but net.UnixConn doesn't expose
58 67
 	// CloseWrite(), which we need to cleanly signal that stdin is closed without
... ...
@@ -15,7 +15,7 @@ void MakeRaw(int fd) {
15 15
   ioctl(fd, TCGETS, &t);
16 16
 
17 17
   t.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
18
-  t.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
18
+  t.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN);
19 19
   t.c_cflag &= ~(CSIZE | PARENB);
20 20
   t.c_cflag |= CS8;
21 21