forked from google/shlex
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordbreak.go
122 lines (112 loc) · 3.45 KB
/
wordbreak.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
114
115
116
117
118
119
120
121
122
package shlex
import "encoding/json"
const BASH_WORDBREAKS = " \t\r\n" + `"'><=;|&(:`
type WordbreakType int
const (
WORDBREAK_UNKNOWN WordbreakType = iota
// https://www.gnu.org/software/bash/manual/html_node/Redirections.html
WORDBREAK_REDIRECT_INPUT
WORDBREAK_REDIRECT_OUTPUT
WORDBREAK_REDIRECT_OUTPUT_APPEND
WORDBREAK_REDIRECT_OUTPUT_BOTH
WORDBREAK_REDIRECT_OUTPUT_BOTH_APPEND
WORDBREAK_REDIRECT_INPUT_STRING
WORDBREAK_REDIRECT_INPUT_DUPLICATE
WORDBREAK_REDIRECT_INPUT_OUTPUT
// https://www.gnu.org/software/bash/manual/html_node/Pipelines.html
WORDBREAK_PIPE
WORDBREAK_PIPE_WITH_STDERR
// https://www.gnu.org/software/bash/manual/html_node/Lists.html)
WORDBREAK_LIST_ASYNC
WORDBREAK_LIST_SEQUENTIAL
WORDBREAK_LIST_AND
WORDBREAK_LIST_OR
// COMP_WORDBREAKS
WORDBREAK_CUSTOM
)
var wordbreakTypes = map[WordbreakType]string{
WORDBREAK_UNKNOWN: "WORDBREAK_UNKNOWN",
WORDBREAK_REDIRECT_INPUT: "WORDBREAK_REDIRECT_INPUT",
WORDBREAK_REDIRECT_OUTPUT: "WORDBREAK_REDIRECT_OUTPUT",
WORDBREAK_REDIRECT_OUTPUT_APPEND: "WORDBREAK_REDIRECT_OUTPUT_APPEND",
WORDBREAK_REDIRECT_OUTPUT_BOTH: "WORDBREAK_REDIRECT_OUTPUT_BOTH",
WORDBREAK_REDIRECT_OUTPUT_BOTH_APPEND: "WORDBREAK_REDIRECT_OUTPUT_BOTH_APPEND",
WORDBREAK_REDIRECT_INPUT_STRING: "WORDBREAK_REDIRECT_INPUT_STRING",
WORDBREAK_REDIRECT_INPUT_DUPLICATE: "WORDBREAK_REDIRECT_INPUT_DUPLICATE",
WORDBREAK_REDIRECT_INPUT_OUTPUT: "WORDBREAK_REDIRECT_INPUT_OUTPUT",
WORDBREAK_PIPE: "WORDBREAK_PIPE",
WORDBREAK_PIPE_WITH_STDERR: "WORDBREAK_PIPE_WITH_STDERR",
WORDBREAK_LIST_ASYNC: "WORDBREAK_LIST_ASYNC",
WORDBREAK_LIST_SEQUENTIAL: "WORDBREAK_LIST_SEQUENTIAL",
WORDBREAK_LIST_AND: "WORDBREAK_LIST_AND",
WORDBREAK_LIST_OR: "WORDBREAK_LIST_OR",
WORDBREAK_CUSTOM: "WORDBREAK_CUSTOM",
}
func (w WordbreakType) MarshalJSON() ([]byte, error) {
return json.Marshal(wordbreakTypes[w])
}
func (w WordbreakType) IsPipelineDelimiter() bool {
switch w {
case
WORDBREAK_PIPE,
WORDBREAK_PIPE_WITH_STDERR,
WORDBREAK_LIST_ASYNC,
WORDBREAK_LIST_SEQUENTIAL,
WORDBREAK_LIST_AND,
WORDBREAK_LIST_OR:
return true
default:
return false
}
}
func (w WordbreakType) IsRedirect() bool {
switch w {
case
WORDBREAK_REDIRECT_INPUT,
WORDBREAK_REDIRECT_OUTPUT,
WORDBREAK_REDIRECT_OUTPUT_APPEND,
WORDBREAK_REDIRECT_OUTPUT_BOTH,
WORDBREAK_REDIRECT_OUTPUT_BOTH_APPEND,
WORDBREAK_REDIRECT_INPUT_STRING,
WORDBREAK_REDIRECT_INPUT_DUPLICATE,
WORDBREAK_REDIRECT_INPUT_OUTPUT:
return true
default:
return false
}
}
func wordbreakType(t Token) WordbreakType {
switch t.RawValue {
case "<":
return WORDBREAK_REDIRECT_INPUT
case ">":
return WORDBREAK_REDIRECT_OUTPUT
case ">>":
return WORDBREAK_REDIRECT_OUTPUT_APPEND
case "&>", ">&":
return WORDBREAK_REDIRECT_OUTPUT_BOTH
case "&>>":
return WORDBREAK_REDIRECT_OUTPUT_BOTH_APPEND
case "<<<":
return WORDBREAK_REDIRECT_INPUT_STRING
case "<&":
return WORDBREAK_REDIRECT_INPUT_DUPLICATE
case "<>":
return WORDBREAK_REDIRECT_INPUT_OUTPUT
case "|":
return WORDBREAK_PIPE
case "|&":
return WORDBREAK_PIPE_WITH_STDERR
case "&":
return WORDBREAK_LIST_ASYNC
case ";":
return WORDBREAK_LIST_SEQUENTIAL
case "&&":
return WORDBREAK_LIST_AND
case "||":
return WORDBREAK_LIST_OR
default:
// TODO check COMP_WORDBREAKS -> WORDBREAK_OTHER
return WORDBREAK_UNKNOWN
}
}