-
Notifications
You must be signed in to change notification settings - Fork 5
/
echo.go
73 lines (69 loc) · 1.29 KB
/
echo.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
package main
import (
"flag"
"fmt"
"os"
"strings"
)
func usage() {
fmt.Fprintln(os.Stderr, "Usage: echo [options] [string ...]")
}
var enableEscapeChars = flag.Bool("e", false, "Enable escape characters")
var omitNewline = flag.Bool("n", false, "Don't print trailing newline")
var disableEscapeChars = flag.Bool("E", true, "Disable escape characters")
func main() {
flag.Parse()
concatenated := strings.Join(flag.Args(), " ")
a := []rune(concatenated)
length := len(a)
ai := 0
if length != 0 {
for i := 0; i < length; {
c := a[i]
i++
if (*enableEscapeChars == true || *disableEscapeChars == false) && c == '\\' && i < length {
c = a[i]
i++
switch c {
case 'a':
c = '\a'
case 'b':
c = '\b'
case 'c':
os.Exit(0)
case 'e':
c = '\x1B'
case 'f':
c = '\f'
case 'n':
c = '\n'
case 'r':
c = '\r'
case 't':
c = '\t'
case 'v':
c = '\v'
case '\\':
c = '\\'
case 'x':
c = a[i]
i++
if '9' >= c && c >= '0' && i < length {
hex := (c - '0')
c = a[i]
i++
if '9' >= c && c >= '0' && i <= length {
c = 16*(c-'0') + hex
}
}
}
}
a[ai] = c
ai++
}
}
os.Stdout.WriteString(string(a[:ai]))
if *omitNewline == false {
fmt.Print("\n")
}
}