forked from johnkerl/miller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print.go
365 lines (334 loc) · 11.8 KB
/
print.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// ================================================================
// This handles print, printn, eprint, and eprintn statements.
// ================================================================
package cst
import (
"bytes"
"fmt"
"os"
"github.com/johnkerl/miller/internal/pkg/dsl"
"github.com/johnkerl/miller/internal/pkg/lib"
"github.com/johnkerl/miller/internal/pkg/output"
"github.com/johnkerl/miller/internal/pkg/runtime"
"github.com/johnkerl/miller/internal/pkg/types"
)
// ----------------------------------------------------------------
// Example ASTs:
//
// $ mlr -n put -v 'print $a, $b'
// DSL EXPRESSION:
// print $a, $b
// AST:
// * statement block
// * print statement "print"
// * function callsite
// * direct field value "a"
// * direct field value "b"
// * no-op
//
// $ mlr -n put -v 'print > stdout, $a, $b'
// DSL EXPRESSION:
// print > stdout, $a, $b
// AST:
// * statement block
// * print statement "print"
// * function callsite
// * direct field value "a"
// * direct field value "b"
// * redirect write ">"
// * stdout redirect target "stdout"
//
// $ mlr -n put -v 'print > stderr, $a, $b'
// DSL EXPRESSION:
// print > stderr, $a, $b
// AST:
// * statement block
// * print statement "print"
// * function callsite
// * direct field value "a"
// * direct field value "b"
// * redirect write ">"
// * stderr redirect target "stderr"
//
// $ mlr -n put -v 'print > "foo.dat", $a, $b'
// DSL EXPRESSION:
// print > "foo.dat", $a, $b
// AST:
// * statement block
// * print statement "print"
// * function callsite
// * direct field value "a"
// * direct field value "b"
// * redirect write ">"
// * string literal "foo.dat"
//
// $ mlr -n put -v 'print >> "foo.dat", $a, $b'
// DSL EXPRESSION:
// print >> "foo.dat", $a, $b
// AST:
// * statement block
// * print statement "print"
// * function callsite
// * direct field value "a"
// * direct field value "b"
// * redirect append ">>"
// * string literal "foo.dat"
//
// $ mlr -n put -v 'print | "command", $a, $b'
// DSL EXPRESSION:
// print | "command", $a, $b
// AST:
// * statement block
// * print statement "print"
// * function callsite
// * direct field value "a"
// * direct field value "b"
// * redirect pipe "|"
// * string literal "command"
//
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Corresponding data structures for these cases:
//
// * printToRedirectFunc is either printToStdout, printToStderr, or
// printToFileOrPipe. Only the third of these takes a non-nil
// redirectorTargetEvaluable and a non-nil outputHandlerManager.
//
// * redirectorTargetEvaluable is nil for stdout or stderr.
//
// * The OutputHandlerManager is for file names or commands in >, >> or |.
// This is because the target for the redirect can vary from one record to
// the next, e.g. mlr put 'print > $a.txt, $b'. The OutputHandlerManager
// keeps file-handles for each distinct value of $a.
//
// So:
//
// * print $a, $b
// AST redirectorNode = NodeTypeNoOp
// AST redirectorTargetNode = (none)
// printToRedirectFunc = printToStdout
// redirectorTargetEvaluable = nil
// outputHandlerManager = nil
//
// * print > stdout, $a, $b
// AST redirectorNode = NodeTypeRedirectWrite
// AST redirectorTargetNode = NodeTypeRedirectTargetStdout
// printToRedirectFunc = printToStdout
// redirectorTargetEvaluable = nil
// outputHandlerManager = nil
//
// * print > stderr, $a, $b
// AST redirectorNode = NodeTypeRedirectWrite
// AST redirectorTargetNode = NodeTypeRedirectTargetStderr
// printToRedirectFunc = printToStderr
// redirectorTargetEvaluable = nil
// outputHandlerManager = nil
//
// * print > "foo.dat", $a, $b
// AST redirectorNode = NodeTypeRedirectWrite
// AST redirectorTargetNode = any of various evaluables
// printToRedirectFunc = printToFileOrPipe
// redirectorTargetEvaluable = non-nil
// outputHandlerManager = non-nil
//
// * print >> "foo.dat", $a, $b
// AST redirectorNode = NodeTypeRedirectAppend
// AST redirectorTargetNode = any of various evaluables
// printToRedirectFunc = printToFileOrPipe
// redirectorTargetEvaluable = non-nil
// outputHandlerManager = non-nil
//
// * print | "command", $a, $b
// AST redirectorNode = NodeTypeRedirectPipe
// AST redirectorTargetNode = any of various evaluables
// printToRedirectFunc = printToFileOrPipe
// redirectorTargetEvaluable = non-nil
// outputHandlerManager = non-nil
// ================================================================
type tPrintToRedirectFunc func(
outputString string,
state *runtime.State,
) error
type PrintStatementNode struct {
expressionEvaluables []IEvaluable
terminator string
printToRedirectFunc tPrintToRedirectFunc
redirectorTargetEvaluable IEvaluable // for file/pipe targets
outputHandlerManager output.OutputHandlerManager // for file/pipe targets
}
// ----------------------------------------------------------------
func (root *RootNode) BuildPrintStatementNode(astNode *dsl.ASTNode) (IExecutable, error) {
lib.InternalCodingErrorIf(astNode.Type != dsl.NodeTypePrintStatement)
return root.buildPrintxStatementNode(
astNode,
os.Stdout,
"\n",
)
}
func (root *RootNode) BuildPrintnStatementNode(astNode *dsl.ASTNode) (IExecutable, error) {
lib.InternalCodingErrorIf(astNode.Type != dsl.NodeTypePrintnStatement)
return root.buildPrintxStatementNode(
astNode,
os.Stdout,
"",
)
}
func (root *RootNode) BuildEprintStatementNode(astNode *dsl.ASTNode) (IExecutable, error) {
lib.InternalCodingErrorIf(astNode.Type != dsl.NodeTypeEprintStatement)
return root.buildPrintxStatementNode(
astNode,
os.Stderr,
"\n",
)
}
func (root *RootNode) BuildEprintnStatementNode(astNode *dsl.ASTNode) (IExecutable, error) {
lib.InternalCodingErrorIf(astNode.Type != dsl.NodeTypeEprintnStatement)
return root.buildPrintxStatementNode(
astNode,
os.Stderr,
"",
)
}
// ----------------------------------------------------------------
// Common code for building print/eprint/printn/eprintn nodes
func (root *RootNode) buildPrintxStatementNode(
astNode *dsl.ASTNode,
defaultOutputStream *os.File,
terminator string,
) (IExecutable, error) {
lib.InternalCodingErrorIf(len(astNode.Children) != 2)
expressionsNode := astNode.Children[0]
redirectorNode := astNode.Children[1]
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Things to be printed, e.g. $a and $b in 'print > "foo.dat", $a, $b'.
var expressionEvaluables []IEvaluable = nil
if expressionsNode.Type == dsl.NodeTypeNoOp {
// Just 'print' without 'print $something'
expressionEvaluables = make([]IEvaluable, 1)
expressionEvaluable := root.BuildStringLiteralNode("")
expressionEvaluables[0] = expressionEvaluable
} else if expressionsNode.Type == dsl.NodeTypeFunctionCallsite {
expressionEvaluables = make([]IEvaluable, len(expressionsNode.Children))
for i, childNode := range expressionsNode.Children {
expressionEvaluable, err := root.BuildEvaluableNode(childNode)
if err != nil {
return nil, err
}
expressionEvaluables[i] = expressionEvaluable
}
} else {
lib.InternalCodingErrorIf(true)
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Redirection targets (the thing after > >> |, if any).
retval := &PrintStatementNode{
expressionEvaluables: expressionEvaluables,
terminator: terminator,
printToRedirectFunc: nil,
redirectorTargetEvaluable: nil,
outputHandlerManager: nil,
}
if redirectorNode.Type == dsl.NodeTypeNoOp {
// No > >> or | was provided.
if defaultOutputStream == os.Stdout {
retval.printToRedirectFunc = retval.printToStdout
} else if defaultOutputStream == os.Stderr {
retval.printToRedirectFunc = retval.printToStderr
} else {
lib.InternalCodingErrorIf(true)
}
} else {
// There is > >> or | provided.
lib.InternalCodingErrorIf(redirectorNode.Children == nil)
lib.InternalCodingErrorIf(len(redirectorNode.Children) != 1)
redirectorTargetNode := redirectorNode.Children[0]
var err error = nil
if redirectorTargetNode.Type == dsl.NodeTypeRedirectTargetStdout {
retval.printToRedirectFunc = retval.printToStdout
} else if redirectorTargetNode.Type == dsl.NodeTypeRedirectTargetStderr {
retval.printToRedirectFunc = retval.printToStderr
} else {
retval.printToRedirectFunc = retval.printToFileOrPipe
retval.redirectorTargetEvaluable, err = root.BuildEvaluableNode(redirectorTargetNode)
if err != nil {
return nil, err
}
if redirectorNode.Type == dsl.NodeTypeRedirectWrite {
retval.outputHandlerManager = output.NewFileWritetHandlerManager(root.recordWriterOptions)
} else if redirectorNode.Type == dsl.NodeTypeRedirectAppend {
retval.outputHandlerManager = output.NewFileAppendHandlerManager(root.recordWriterOptions)
} else if redirectorNode.Type == dsl.NodeTypeRedirectPipe {
retval.outputHandlerManager = output.NewPipeWriteHandlerManager(root.recordWriterOptions)
} else {
return nil, fmt.Errorf("mlr: unhandled redirector node type %s.", string(redirectorNode.Type))
}
}
}
// Register this with the CST root node so that open file descriptrs can be
// closed, etc at end of stream.
if retval.outputHandlerManager != nil {
root.RegisterOutputHandlerManager(retval.outputHandlerManager)
}
return retval, nil
}
// ----------------------------------------------------------------
func (node *PrintStatementNode) Execute(state *runtime.State) (*BlockExitPayload, error) {
if len(node.expressionEvaluables) == 0 {
node.printToRedirectFunc(node.terminator, state)
} else {
// 5x faster than fmt.Print() separately: note that os.Stdout is
// non-buffered in Go whereas stdout is buffered in C.
//
// Minus: we need to do our own buffering for performance.
//
// Plus: we never have to worry about forgetting to do fflush(). :)
var buffer bytes.Buffer
for i, expressionEvaluable := range node.expressionEvaluables {
if i > 0 {
buffer.WriteString(" ")
}
evaluation := expressionEvaluable.Evaluate(state)
if !evaluation.IsAbsent() {
buffer.WriteString(evaluation.String())
}
}
buffer.WriteString(node.terminator)
node.printToRedirectFunc(buffer.String(), state)
}
return nil, nil
}
// ----------------------------------------------------------------
func (node *PrintStatementNode) printToStdout(
outputString string,
state *runtime.State,
) error {
// Insert the string into the record-output stream, so that goroutine can
// print it, resulting in deterministic output-ordering.
// The output channel is always non-nil, except for the Miller REPL.
if state.OutputRecordsAndContexts != nil {
state.OutputRecordsAndContexts.PushBack(types.NewOutputString(outputString, state.Context))
} else {
fmt.Print(outputString)
}
return nil
}
// ----------------------------------------------------------------
func (node *PrintStatementNode) printToStderr(
outputString string,
state *runtime.State,
) error {
fmt.Fprint(os.Stderr, outputString)
return nil
}
// ----------------------------------------------------------------
func (node *PrintStatementNode) printToFileOrPipe(
outputString string,
state *runtime.State,
) error {
redirectorTarget := node.redirectorTargetEvaluable.Evaluate(state)
if !redirectorTarget.IsString() {
return fmt.Errorf("mlr: output redirection yielded %s, not string.", redirectorTarget.GetTypeName())
}
outputFileName := redirectorTarget.String()
node.outputHandlerManager.WriteString(outputString, outputFileName)
return nil
}