Debian Codegraph @

example/example.go

raw symbols SCIP
1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package main
6
7 import (
8 "fmt"
9 "os"
10 "os/exec"
11 "os/signal"
12 "strconv"
13 "syscall"
14
15 "github.com/google/goterm/term"
16 )
17
18 // Constants for IO and greeting.
19 const (
20 file = "/tmp/goscript-" // file the base filename of the logfile
21 bufSz = 8192 // BUFSZ size of the buffers used for IO
22 Welcome = "Examplescript up and running" // Welcome Welcome message printed when the application starts
23 )
24
25 // A version of the UNIX command "script" with no errchecking buffered IO or cool features
26 func main() {
27 // Get PTYs up
28 pty, _ := term.OpenPTY()
29 defer pty.Close()
30 // Save the current Stdin attributes
31 backupTerm, _ := term.Attr(os.Stdin)
32 // Copy attributes
33 myTerm := backupTerm
34 // Change the Stdin term to RAW so we get everything
35 myTerm.Raw()
36 myTerm.Set(os.Stdin)
37 // Set the backup attributes on our PTY slave
38 backupTerm.Set(pty.Slave)
39 // Make sure we'll get the attributes back when exiting
40 defer backupTerm.Set(os.Stdin)
41 // Get the snooping going
42 go Snoop(pty)
43 // Handle changes in termsize
44 sig := make(chan os.Signal, 2)
45 // Notify if window size changes or shell dies
46 signal.Notify(sig, syscall.SIGWINCH, syscall.SIGCLD)
47 // Start up the slaveshell
48 cmd := exec.Command(os.Getenv("SHELL"), "")
49 cmd.Stdin, cmd.Stdout, cmd.Stderr = pty.Slave, pty.Slave, pty.Slave
50 cmd.Args = nil
51 cmd.SysProcAttr = &syscall.SysProcAttr{
52 Setsid: true,
53 Setctty: true}
54 cmd.Start()
55 // Get the initial winsize
56 myTerm.Winsz(os.Stdin)
57 myTerm.Winsz(pty.Slave)
58 // If the termsize changes , propagate to our PTY
59 for {
60 switch <-sig {
61 case syscall.SIGWINCH:
62 myTerm.Winsz(os.Stdin)
63 myTerm.Setwinsz(pty.Slave)
64 default:
65 return
66 }
67 }
68 }
69
70 // Snoop gets the script file up and running and kicks of the reader and writer functions
71 func Snoop(pty *term.PTY) {
72 // Just something that might be a bit uniqe
73 pid := os.Getpid()
74 pidcol, _ := term.NewColor256(strconv.Itoa(pid), strconv.Itoa(pid%256), "")
75 greet := fmt.Sprintln("\n", term.Green(Welcome), " pid:", pidcol,
76 " file:", term.Yellow(file+strconv.Itoa(pid)+"\n"))
77 // Our logfile
78 file, _ := os.Create(file + strconv.Itoa(pid))
79 os.Stdout.Write([]byte(greet))
80 go reader(pty.Master, file)
81 go writer(pty.Master)
82 }
83
84 // reader reads from master and writes to file and stdout
85 func reader(master *os.File, log *os.File) {
86 var buf = make([]byte, bufSz)
87 defer func() {
88 log.Sync()
89 log.Close()
90 }()
91 for {
92 nr, _ := master.Read(buf)
93 os.Stdout.Write(buf[:nr])
94 log.Write(buf[:nr])
95 }
96 }
97
98 // writer reads from stdin and writes to master
99 func writer(master *os.File) {
100 var buf = make([]byte, bufSz)
101 for {
102 nr, _ := os.Stdin.Read(buf)
103 master.Write(buf[:nr])
104 }
105 }