summaryrefslogtreecommitdiff
path: root/pkgtools/pkglint/files/mkshtypes.go
blob: 1f3e5f74f8c2d6ca105faa600f89d6ffe5dadfdd (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package pkglint

import (
	"netbsd.org/pkglint/regex"
	"strings"
)

// MkShList is a list of shell commands, separated by newlines or semicolons.
//
// Example:
//  cd $dir && echo "In $dir"; cd ..; ls -l
type MkShList struct {
	AndOrs []*MkShAndOr

	// The separators after each AndOr.
	// There may be one less entry than in AndOrs.
	Separators []MkShSeparator
}

func NewMkShList() *MkShList {
	return &MkShList{nil, nil}
}

func (list *MkShList) AddAndOr(andor *MkShAndOr) *MkShList {
	list.AndOrs = append(list.AndOrs, andor)
	return list
}

func (list *MkShList) AddSeparator(separator MkShSeparator) *MkShList {
	list.Separators = append(list.Separators, separator)
	return list
}

// MkShAndOr is a group of commands that are connected with && or ||
// conditions.
//
// The operators && and || have the same precedence and are evaluated
// strictly from left to right.
//
// Example:
//  cd $dir && echo "In $dir" || echo "Cannot cd into $dir"
type MkShAndOr struct {
	Pipes []*MkShPipeline
	Ops   []string // Each element is either "&&" or "||"
}

func NewMkShAndOr(pipeline *MkShPipeline) *MkShAndOr {
	return &MkShAndOr{[]*MkShPipeline{pipeline}, nil}
}

func (andor *MkShAndOr) Add(op string, pipeline *MkShPipeline) *MkShAndOr {
	andor.Pipes = append(andor.Pipes, pipeline)
	andor.Ops = append(andor.Ops, op)
	return andor
}

// MkShPipeline is a group of commands, connected by pipelines.
//
// Example: grep word file | sed s,^,---,
type MkShPipeline struct {
	Negated bool
	Cmds    []*MkShCommand
}

func NewMkShPipeline(negated bool, cmds []*MkShCommand) *MkShPipeline {
	return &MkShPipeline{negated, cmds}
}

func (pipe *MkShPipeline) Add(cmd *MkShCommand) *MkShPipeline {
	pipe.Cmds = append(pipe.Cmds, cmd)
	return pipe
}

// MkShCommand is a simple or compound shell command.
//
// Examples:
//  LC_ALL=C sort */*.c > sorted
//  dir() { ls -l "$@"; }
//  { echo "first"; echo "second"; }
type MkShCommand struct {
	Simple    *MkShSimpleCommand
	Compound  *MkShCompoundCommand
	FuncDef   *MkShFunctionDefinition
	Redirects []*MkShRedirection // For Compound and FuncDef
}

// MkShCompoundCommand is a group of commands.
//
// Examples:
//  { echo "first"; echo "second"; }
//  for f in *.c; do compile "$f"; done
//  if [ -f "$file" ]; then echo "It exists"; fi
//  while sleep 1; do printf .; done
type MkShCompoundCommand struct {
	Brace    *MkShList
	Subshell *MkShList
	For      *MkShFor
	Case     *MkShCase
	If       *MkShIf
	Loop     *MkShLoop
}

// MkShFor is a "for" loop.
//
// Example:
//  for f in *.c; do compile "$f"; done
type MkShFor struct {
	Varname string
	Values  []*ShToken
	Body    *MkShList
}

// MkShCase is a "case" statement, including all its branches.
//
// Example:
//  case $filename in *.c) echo "C source" ;; esac
type MkShCase struct {
	Word  *ShToken
	Cases []*MkShCaseItem
}

// MkShCaseItem is one branch of a "case" statement.
//
// Example:
//  *.c) echo "C source" ;;
type MkShCaseItem struct {
	Patterns  []*ShToken
	Action    *MkShList
	Separator MkShSeparator
	Var       *ShToken // ${PATTERNS:@p@ (${p}) action ;; @}
}

// MkShIf is a conditional statement, possibly having
// many branches.
//
// Example:
//  if [ -f "$file" ]; then echo "It exists"; fi
type MkShIf struct {
	Conds   []*MkShList
	Actions []*MkShList
	Else    *MkShList
}

func (cl *MkShIf) Prepend(cond *MkShList, action *MkShList) {
	cl.Conds = append([]*MkShList{cond}, cl.Conds...)
	cl.Actions = append([]*MkShList{action}, cl.Actions...)
}

// MkShLoop is a "while" or "until" loop.
//
// Example:
//  while sleep 1; do printf .; done
type MkShLoop struct {
	Cond   *MkShList
	Action *MkShList
	Until  bool
}

// MkShFunctionDefinition is the definition of a shell function.
//
// Example:
//  dir() { ls -l "$@"; }
type MkShFunctionDefinition struct {
	Name string
	Body *MkShCompoundCommand
}

// MkShSimpleCommand is a shell command that does not involve any
// pipeline or conditionals.
//
// Example:
//  LC_ALL=C sort */*.c > sorted
type MkShSimpleCommand struct {
	Assignments  []*ShToken
	Name         *ShToken
	Args         []*ShToken
	Redirections []*MkShRedirection
}

// StrCommand is structurally similar to MkShSimpleCommand, but all
// components are converted to strings to allow for simpler checks,
// especially for analyzing command line options.
//
// Example:
//  LC_ALL=C sort */*.c > sorted
type StrCommand struct {
	Assignments []string
	Name        string
	Args        []string
}

func NewStrCommand(cmd *MkShSimpleCommand) *StrCommand {
	strcmd := StrCommand{
		make([]string, len(cmd.Assignments)),
		"",
		make([]string, len(cmd.Args))}
	for i, assignment := range cmd.Assignments {
		strcmd.Assignments[i] = assignment.MkText
	}
	if cmd.Name != nil {
		strcmd.Name = cmd.Name.MkText
	}
	for i, arg := range cmd.Args {
		strcmd.Args[i] = arg.MkText
	}
	return &strcmd
}

// HasOption checks whether one of the arguments is exactly the given opt.
func (c *StrCommand) HasOption(opt string) bool {
	for _, arg := range c.Args {
		if arg == opt {
			return true
		}
	}
	return false
}

func (c *StrCommand) AnyArgMatches(pattern regex.Pattern) bool {
	for _, arg := range c.Args {
		if matches(arg, pattern) {
			return true
		}
	}
	return false
}

func (c *StrCommand) String() string {
	strs := append([]string(nil), c.Assignments...)
	if c.Name != "" {
		strs = append(strs, c.Name)
	}
	strs = append(strs, c.Args...)
	return strings.Join(strs, " ")
}

// MkShRedirection is a single file descriptor redirection.
//
// Examples:
//  > sorted
//  2>&1
type MkShRedirection struct {
	Fd     int      // Or -1
	Op     string   // See io_file in shell.y for possible values
	Target *ShToken // The filename or &fd
}

// MkShSeparator is one of ; & newline.
type MkShSeparator uint8

const (
	sepNone       MkShSeparator = iota
	sepSemicolon                // ;
	sepBackground               // &
	sepNewline                  // \n
)