-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
260 lines (216 loc) · 5.9 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
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
//go:generate go install golang.org/x/tools/cmd/[email protected]
//go:generate goyacc parser.y
package main
import (
"bufio"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
)
const CELL_VERSION = "0.1.0"
type ExecContext struct {
code string
topath string
frompath string
spreadsheet *Spreadsheet
exitCode int
scope *Scope
ndollars uint16
functions map[string]*Function
funcRet Node
doExit bool
doBreak bool
doContinue bool
doReturn bool
in *bufio.Reader
out io.Writer
errout io.Writer
doTextRowLoop bool
doExcelRowLoop bool
initSheet string
}
var execContext *ExecContext
func NewExecContext() *ExecContext {
con := &ExecContext{}
con.scope = NewScope()
con.functions = builtinFunctions()
con.in = bufio.NewReader(os.Stdin)
con.out = os.Stdout
con.errout = os.Stderr
con.scope.set("FS", NewStringExpression(" "))
con.scope.set("OFS", NewStringExpression(" "))
con.scope.set("RS", NewStringExpression("\n"))
con.scope.set("ORS", NewStringExpression("\n"))
con.scope.set("NR", NewNumberExpression(0))
con.scope.set("SER", NewNumberExpression(1))
return con
}
func main() {
flag.Usage = usage
con := NewExecContext()
var pgpath string
var showVer bool
var fs string
var ser int
flag.StringVar(&con.topath, "to", "", "output xlsx filepath")
flag.StringVar(&con.frompath, "from", "", "input xlsx filepath")
flag.StringVar(&pgpath, "f", "", "program filepath")
flag.StringVar(&fs, "F", "", "specify field separator")
flag.BoolVar(&showVer, "V", false, "show version")
flag.BoolVar(&con.doTextRowLoop, "n", false, "wrap your script inside while(gets()){... ;} loop")
flag.BoolVar(&con.doExcelRowLoop, "N", false, "wrap your script inside for(NER = SER; NER <= LR; NER++){... ;} loop")
flag.IntVar(&ser, "s", 1, "specify special var SER(start excel row)")
flag.StringVar(&con.initSheet, "S", "", "specify active sheet by name")
flag.Parse()
// -V option
if showVer {
showVersion()
}
args := flag.Args()
if len(args) < 1 && pgpath == "" {
flag.Usage()
os.Exit(1)
}
// -f option
if pgpath != "" {
con.code = readProg(pgpath)
} else {
con.code = args[0]
}
// -F option
if fs != "" {
con.scope.set("FS", NewStringExpression(fs))
}
// -s option
con.scope.set("SER", NewNumberExpression(float64(ser)))
// text file specify
if pgpath == "" {
if 1 < len(args) {
switchStdin(con, args[1:])
}
} else {
if 0 < len(args) {
switchStdin(con, args)
}
}
run(con)
os.Exit(con.exitCode)
}
func switchStdin(con *ExecContext, files []string) {
rary := make([]io.Reader, len(files))
for i := 0; i < len(files); i++ {
f, err := os.Open(files[i])
if err != nil {
fatalError("could not open file '%s'", f)
}
rary[i] = f
}
con.in = bufio.NewReader(io.MultiReader(rary...))
}
func showVersion() {
fmt.Printf("Cell %s\n", CELL_VERSION)
os.Exit(0)
}
func usage() {
msg := `Cell is an Excel file(xlsx) processing language for command line.
Usage: cell [options] 'program' [file...]
Usage: cell [options] -f programfile [file...]
Options:
-to output-xlsx-file-path
Specify the path of the processed Excel file that will be saved
-from input-xlsx-file-path
Specify the Excel file to be processed. No overwriting will be done. The default is an empty book containing only Sheet1.
-f program-file
Read the Cell program source from the file program-file, instead of from the first command line argument.
-F fs
Use fs for the input field separator (the value of the FS predefined variable).
-n
Wrap your script inside while(gets()){... ;} loop
-N
Wrap your script inside for(NER = SER; NER <= LR; NER++){... ;} loop (NER and SER, LR are predefined variables)
-s row-no
Specify the special variable SER(Start Excel Row) (default 1)
-S
Specify default active sheet by name
-V
Print version information.
-h
Show this help
Examples:
cell -to greeting.xlsx '["A1"] = "Hello, world"'
cell -F ":" -to users.xlsx -n '["A".NR] = $1' /etc/passwd`
fmt.Fprintf(os.Stderr, "%s\n", msg)
}
func readProg(filename string) string {
if !fileExist(filename) {
fatalError("program file '%s' is not found", filename)
}
bytes, err := ioutil.ReadFile(filename)
if err != nil {
fatalError("on error occured reading file '%s'", filename)
}
return string(bytes)
}
func fileExist(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}
func beforeRun() {
// setup spreadsheet
sheet, err := NewSpreadsheet(execContext.frompath, execContext.topath)
if err != nil {
fatalError("on error occured loading xlsx file")
}
execContext.spreadsheet = sheet
// -S option
if execContext.initSheet != "" {
execContext.scope.set("@", NewStringExpression(execContext.initSheet))
}
// -N option
if execContext.doExcelRowLoop {
execContext.code = "for(NER = SER; NER <= LR; NER++){ " + execContext.code + "; }"
}
// -n option
if execContext.doTextRowLoop {
execContext.code = "while(gets()){ " + execContext.code + "; }"
}
}
func afterRun() {
// request full calculate to excel
execContext.spreadsheet.file.WorkBook.CalcPr.FullCalcOnLoad = true
if execContext.topath != "" {
if err := execContext.spreadsheet.writeSpreadsheet(); err != nil {
fatalError("on error occured writting xlsx file")
}
}
}
func run(con *ExecContext) {
execContext = con
beforeRun()
execScript()
afterRun()
}
func execScript() int {
yyDebug = 1
yyErrorVerbose = true
lexer := NewLexer(execContext.code)
yyParse(lexer)
lexer.ast.eval()
if execContext.doBreak {
fatalError("'break' is not allowed outside a loop")
}
if execContext.doContinue {
fatalError("'continue' is not allowed outside a loop")
}
return 0
}
func fatalError(format string, a ...interface{}) {
if len(a) > 0 {
fmt.Fprintf(os.Stderr, "ERROR: "+format+"\n", a...)
} else {
fmt.Fprintf(os.Stderr, "ERROR: "+format+"\n")
}
os.Exit(1)
}