-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (96 loc) · 1.84 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"joshua/green/gonecat/gonecat"
"log"
"os"
"strconv"
)
const HelpMsg = `Usage: gnc [options] address port
-4 Use IPv4
-6 Use IPv6
-C Do not send CRLF as line-ending
-d Detach from stdin
-h This help text
-I length TCP receive buffer length
-l Listen mode
-o file Redirect output to file
-u UDP mode
`
func main() {
gct := ParseArguments()
if gct == nil {
fmt.Print(HelpMsg)
return
}
gonecat := gonecat.GetCat(gct)
err := gonecat.Execute()
if err != nil {
log.Fatal(err)
}
}
// ParseArguments parses the command line arguments and returns a GCArguments
// object. If the command line arguments are invalid, nil is returned.
func ParseArguments() *gonecat.GCArguments {
if len(os.Args) == 1 {
return nil
}
gct := new(gonecat.GCArguments)
gct.UseDefaults()
i := 1
for i < len(os.Args) {
arg := os.Args[i]
switch arg {
case "-4":
gct.IPVersion = "4"
case "-6":
gct.IPVersion = "6"
case "-C":
gct.SendCRLF = false
case "-d":
gct.ReadStdin = false
case "-h", "--help":
return nil
case "-I":
i++
bsize, err := strconv.Atoi(os.Args[i])
if err != nil {
return nil
}
gct.BufferSize = bsize
case "-u":
gct.Protocol = "udp"
case "-l":
gct.Listening = true
case "-o":
i++
path := os.Args[i]
output, err := os.Create(path)
if err != nil {
log.Fatal(err)
}
gct.Output = output
default:
if i+1 >= len(os.Args) {
return nil
}
gct.AddrStr = arg
gct.AddrPort = os.Args[i+1]
i++
}
i++
}
if gct.AddrStr == "" || gct.AddrPort == "" {
return nil
}
gct.ReadPipe = isPipeConnected()
return gct
}
// isPipeConnected returns true if stdin is connected to a pipe
func isPipeConnected() bool {
info, err := os.Stdin.Stat()
if err != nil {
log.Fatal(err)
}
return info.Mode()&os.ModeCharDevice == 0
}