-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpawk.go
More file actions
1035 lines (923 loc) · 29.3 KB
/
Copy pathpawk.go
File metadata and controls
1035 lines (923 loc) · 29.3 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020 Georgios Theodorou
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
// #include <unistd.h>
import "C"
import (
// "time"
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
_ "net/http/pprof"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime/debug"
"strconv"
"strings"
"github.com/gthd/goawk/interp"
"github.com/gthd/goawk/parser"
"github.com/gthd/helper"
"github.com/pborman/getopt/v2"
)
type chunk struct {
buff []byte
}
func check(e error) {
if e != nil {
panic(e)
}
}
var (
value helper.Helper
numberOfThreads int
numCores int
numSockets int
fieldSeparator = " "
offsetFieldSeparator = " "
fileName = ""
dumpFile = ""
eventualAwkCommand string
endStatement string
nameSlice []string
min float64
max float64
indexEnd [][]int
emptyStmt bool
text []byte
pp *parser.Program
hasEnd bool
hasBegin bool
bbb string
associativeValues map[string]map[string]float64
associativeValue map[string]float64
associativeArrays map[int]map[string]float64
arraysPerFile map[int][]*received
ok bool
flag bool
actionStatement string
okArray []bool
actions map[int]string
indexes []int
myVariable []string
actionArgument string
proceed = true
input = bytes.NewReader([]byte("foo bar\n\nbaz buz"))
actionString string
files []string
printText string
operations []bool
// toRemove []string
numOfArgs int
subFileSize int
defaultSize int
multiple int
)
type received struct {
results []float64
functionNames []string
associativeArray map[string]float64
}
// Used to parse input arguments given by the user from console
func init() {
getopt.FlagLong(&fieldSeparator, "field-separator", 'F', "the field separator")
getopt.FlagLong(&numberOfThreads, "threads", 'n', "the number of threads to be used")
getopt.FlagLong(&fileName, "progfile", 'f', "the file name")
getopt.FlagLong(&dumpFile, "dump-variables", 'd', "the file to print the global variables")
getopt.FlagLong(&value, "string", 'v', "strings")
getopt.FlagLong(&offsetFieldSeparator, "offset-field-separator", 'o', "the offset field separator")
}
// Used when the awk command is provided inside a file rather than written in the console
func getCommand(commandFile string) string {
command := ""
f, err := os.Open(commandFile) //open the file to process it
check(err)
finfo, err := f.Stat()
check(err)
fsize := int(finfo.Size())
buf := make([]byte, fsize)
bytesContained, err := f.Read(buf)
check(err)
command = string(buf[:bytesContained])
return command
}
// Used to open a file for reading/writing operations
func openFile(f string) *os.File {
file, err := os.Open(f) //open the file to process
check(err)
return file
}
// Returns the size of the file according to which the file will be divided
func getSize(file *os.File) int {
fileinfo, err := file.Stat()
check(err)
filesize := int(fileinfo.Size())
return filesize
}
// Returns the starting index and the ending index for all the print statements of the awk command
func returnBeginPrintIndices(statement string) ([]int, []int) {
var phrase = `print`
var startingIndex []int
var endingIndex []int
compiled := regexp.MustCompile(phrase)
index := compiled.FindAllStringIndex(statement, -1)
if len(index) > 0 {
for i := range index {
startingIndex = append(startingIndex, index[i][0])
}
for iter, b := range []byte(statement) {
if b == 59 {
endingIndex = append(endingIndex, iter)
}
}
// checks whether the first ending index is after the first starting index
for {
if len(endingIndex) > 0 && endingIndex[0] < startingIndex[0] {
endingIndex = endingIndex[1:]
} else {
break
}
}
if len(endingIndex) == 0 {
endingIndex = append(endingIndex, len(statement))
} else if startingIndex[len(startingIndex)-1] > endingIndex[len(endingIndex)-1] {
endingIndex = append(endingIndex, len(statement))
}
// checks whether all ending indexes are after their respective starting indexes
var tracker = 0
var test []int
// Since ending Index should contain
endingIndex = endingIndex[:len(startingIndex)]
for i := 0; i < len(endingIndex); i++ {
if endingIndex[i] > startingIndex[tracker] {
tracker++
test = append(test, endingIndex[i])
}
}
endingIndex = test
return startingIndex, endingIndex
}
return startingIndex, endingIndex
}
func helpFileReading(file *os.File, numberOfThreads int) (int, int) {
memory := int(C.sysconf(C._SC_PHYS_PAGES)*C.sysconf(C._SC_PAGE_SIZE)) - 2500000000
subFileSize = int(memory / numberOfThreads)
for {
multiple++
defaultSize = int(getSize(file) / (numberOfThreads * multiple))
if defaultSize < subFileSize {
break
}
}
return defaultSize, multiple
}
var end int
var bytesToRead int
var index int64
// Used to divide the file to n equal parts that will be fed to the n different processors running in parallel
func divideFile(file *os.File, n int, defaultSize int, multiple int) []chunk {
chunk := make([]chunk, n)
for thread := 0; thread < n; thread++ {
//In this way we check that the chunk does not end just before new line
bytesToRead = defaultSize + (bytesToRead - end) + 1
//the byte length that gets handled by every thread
b := make([]byte, bytesToRead)
io.ReadAtLeast(file, b, bytesToRead)
for i := bytesToRead - 1; i > 0; i-- {
if b[i] == 10 {
end = i
break
}
}
// fmt.Println(end-bytesToRead)
if thread > 0 {
//For all threads other than the first, start from position 1 to exclude \n at the beginning of each chunk
chunk[thread].buff = b[1:end]
} else if thread == 0 && multiple == 1 {
chunk[thread].buff = b[:end]
} else if thread == 0 {
chunk[thread].buff = b[1:end]
}
_, err := file.Seek(index+int64(end), 0)
index += int64(end)
check(err)
}
return chunk
}
// Responsible for communicating with the goAwk dependency. Returns the parsed awk Command
func goAwk(chunk []byte, prog *parser.Program, fieldSeparator string, offsetFieldSeparator string, funcs map[string]interface{}, threadID int) ([]float64, []string, map[string]float64) {
config := &interp.Config{
Stdin: bytes.NewReader(chunk),
Vars: []string{"OFS", offsetFieldSeparator, "FS", fieldSeparator},
Funcs: funcs,
Thread: threadID,
}
_, err, res, names, arrays := interp.ExecProgram(prog, config)
check(err)
return res, names, arrays
}
// Checks whether a string is contained inside a slice.
func isContained(s string, slice []string) bool {
flag := false
for _, k := range slice {
if k == s {
flag = true
}
if strings.Contains(s, k) {
flag = true
}
}
return flag
}
func getNumCores() int {
out, _ := exec.Command("lscpu").Output()
outstring := strings.TrimSpace(string(out))
lines := strings.Split(outstring, "\n")
for _, line := range lines {
fields := strings.Split(line, ":")
if len(fields) < 2 {
continue
}
key := strings.TrimSpace(fields[0])
value := strings.TrimSpace(fields[1])
switch key {
case "Core(s) per socket":
t, _ := strconv.Atoi(value)
numCores = int(t)
case "Socket(s)":
t, _ := strconv.Atoi(value)
numSockets = int(t)
}
}
return numCores * numSockets
}
func getFunctions() map[string]interface{} {
funcs := map[string]interface{}{
"min": func(num1 float64, num2 float64) float64 {
if num1 < num2 {
return num1
}
return num2
},
"max": func(num1 float64, num2 float64) float64 {
if num1 > num2 {
return num1
}
return num2
},
"and": func(bool1 bool, bool2 bool) bool {
return bool1 && bool2
},
"or": func(bool1 bool, bool2 bool) bool {
return bool1 || bool2
},
"xor": func(bool1 bool, bool2 bool) bool {
return bool1 != bool2
},
}
return funcs
}
func main() {
debug.SetGCPercent(1)
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
getopt.Parse()
args := getopt.Args()
awkCommand := ""
if fileName == "" {
awkCommand = args[0]
args = args[1:]
} else {
awkCommand = getCommand(fileName)
}
values := value.ParseMultipleOptions()
// used for passing to the BEGIN statement the values given from console with -v option
var periodContextFmt = `[Bb][Ee][Gg][Ii][Nn]\s*{`
sent := regexp.MustCompile(periodContextFmt)
ind := sent.FindAllStringIndex(awkCommand, -1)
var argString string
for _, va := range values {
argString = argString + va + ";"
}
var newAwkCommand string
if len(values) > 0 {
if len(ind) > 0 {
newAwkCommand = string(awkCommand[:ind[0][1]]) + argString + string(awkCommand[ind[0][1]:])
} else {
newAwkCommand = "BEGIN { " + argString[:len(argString)-1] + "} " + awkCommand
}
} else {
newAwkCommand = awkCommand
}
// Handles variable assignment in BEGIN as well as print statement
// CANNOT have something like this BEGIN {print "cndckd" ; emp=1 ; print "kcndkc"}
// SHOULD BE BEGIN {print "cndckd" ; print "kcndkc" ; emp=1}
// OR BEGIN {emp=1 ; print "cndckd" ; print "kcndkc"}
if strings.Contains(newAwkCommand, "BEGIN") { //Is it only BEGIN ? Or it can be Begin ?
// beginStatement := prog.Begin[0].String()
hasBegin = true
beginStatement := newAwkCommand[:strings.Index(newAwkCommand, "}")+1]
printStartIndex, printEndIndex := returnBeginPrintIndices(beginStatement)
// If print exists in BEGIN
if len(printStartIndex) > 0 {
// checks that print operation have something to print
for i := 0; i < len(printEndIndex); i++ {
if printEndIndex[i]-printStartIndex[i] <= 1 {
panic("Wrong syntax! Print No " + strconv.Itoa(i+1) + " does not contain anything")
}
}
// builds new string that contains everything except print statements
var str strings.Builder
str.WriteString(beginStatement[:printStartIndex[0]])
for iter := 1; iter < len(printEndIndex); iter++ {
str.WriteString(beginStatement[printEndIndex[iter-1]:printStartIndex[iter]])
}
str.WriteString(beginStatement[printEndIndex[len(printEndIndex)-1]:])
mystring := str.String()
indexOfBegin := strings.Index(newAwkCommand, `}`)
if string(mystring[len(mystring)-1]) != "}" {
mystring = mystring + `}`
}
eventualAwkCommand = mystring + newAwkCommand[indexOfBegin+1:]
for iter := 0; iter < len(printEndIndex); iter++ {
printvariable := beginStatement[printStartIndex[iter]:printEndIndex[iter]]
if string(printvariable[6]) == "\"" && string(printvariable[len(printvariable)-1]) == "\"" {
fmt.Printf(" %s ", printvariable[7:len(printvariable)-1])
} else if string(printvariable[6]) == "\"" && string(printvariable[len(printvariable)-2]) == "\"" {
fmt.Printf(" %s ", printvariable[7:len(printvariable)-2])
} else {
panic("Not provided a valid argument to print in BEGIN statement")
}
}
} else {
eventualAwkCommand = newAwkCommand
}
} else {
eventualAwkCommand = newAwkCommand
}
// Remove the END statement, gets handled on its own at the end
if strings.Contains(newAwkCommand, "END") {
hasEnd = true
var regexstring = `[Ee][Nn][Dd]\s*{`
comp := regexp.MustCompile(regexstring)
indexEnd = comp.FindAllStringIndex(eventualAwkCommand, -1)
endStatement = eventualAwkCommand[indexEnd[0][0]:]
eventualAwkCommand = strings.ReplaceAll(eventualAwkCommand, endStatement, "")
}
// Removes BEGIN and END Statements from the initial AWK command
init := eventualAwkCommand
if hasEnd && hasBegin {
bbb = eventualAwkCommand[strings.Index(eventualAwkCommand, "}")+1 : indexEnd[0][0]]
} else if hasEnd && !hasBegin {
bbb = eventualAwkCommand[:indexEnd[0][0]]
} else if hasBegin && !hasEnd {
bbb = eventualAwkCommand[strings.Index(eventualAwkCommand, "}")+1:]
} else {
bbb = eventualAwkCommand
}
// Gets the indexes of the print functions in the action statements
printStartIndex, printEndIndex := returnBeginPrintIndices(bbb)
// Responsible for removing print statements from action statement and creating a new awk command that does not include them
if len(printStartIndex) > 0 && !strings.Contains(eventualAwkCommand, "for") {
for i := 0; i < len(printEndIndex); i++ {
if printEndIndex[i]-printStartIndex[i] <= 1 {
panic("Wrong syntax! Print No " + strconv.Itoa(i+1) + " does not contain anything")
}
}
// builds new string that contains everything except print statements
var str strings.Builder
str.WriteString(bbb[:printStartIndex[0]])
for iter := 1; iter < len(printEndIndex); iter++ {
str.WriteString(bbb[printEndIndex[iter-1]:printStartIndex[iter]])
}
str.WriteString(bbb[printEndIndex[len(printEndIndex)-1]:])
mystring := str.String()
if string(mystring[len(mystring)-1]) != "}" {
mystring = mystring + `}`
}
if len(strings.TrimSpace(mystring)) == 2 {
emptyStmt = true
}
// Create new AWK command that does not contain the print statements
if hasBegin && hasEnd {
abc := eventualAwkCommand[:strings.Index(eventualAwkCommand, "}")+1]
def := eventualAwkCommand[indexEnd[0][0]:]
eventualAwkCommand = abc + mystring + def
} else if hasBegin && !hasEnd {
abc := eventualAwkCommand[:strings.Index(eventualAwkCommand, "}")+1]
eventualAwkCommand = abc + mystring
} else if !hasBegin && hasEnd {
def := newAwkCommand[indexEnd[0][0]:]
eventualAwkCommand = mystring + def
} else if !hasBegin && !hasEnd {
eventualAwkCommand = mystring
}
} else {
eventualAwkCommand = init
}
// Responsible for distinguishing action statements in AWK commands that contains multiple blocks
actions = make(map[int]string)
for i, b := range []byte(eventualAwkCommand) {
if b == 10 {
indexes = append(indexes, i)
}
}
indexes = append(indexes, len([]byte(eventualAwkCommand)))
for i := range indexes {
if i == 0 {
actionString = string([]byte(eventualAwkCommand)[:indexes[i]])
actionString = strings.TrimPrefix(actionString, "\n")
actionString = strings.TrimSuffix(actionString, "\n")
if !(actionString == "{" || actionString == "}") {
actions[i] = actionString
}
} else if i != len(indexes)-1 {
actionString = string([]byte(eventualAwkCommand)[indexes[i-1]:indexes[i]])
actionString = strings.TrimPrefix(actionString, "\n")
actionString = strings.TrimSuffix(actionString, "\n")
if !(actionString == "{" || actionString == "}") {
actions[i] = actionString
}
} else {
actionString = string([]byte(eventualAwkCommand)[indexes[i-1]:])
actionString = strings.TrimPrefix(actionString, "\n")
actionString = strings.TrimSuffix(actionString, "\n")
if len(strings.TrimSpace(string([]byte(eventualAwkCommand)[indexes[i-1]:]))) > 0 && !(actionString == "{" || actionString == "}") {
actions[i] = actionString
}
}
}
// Creates the config struct to be passed in GoAwk's Parser
funcs := getFunctions()
config := &parser.ParserConfig{
Funcs: funcs,
}
// Checks if an action statement contains an empty if operator, should be executed in one thread
for k := range actions {
if strings.Contains(actions[k], "{") && strings.Contains(actions[k], "}") {
actStatement := actions[k][strings.Index(actions[k], "{")+1 : strings.Index(actions[k], "}")]
if strings.Contains(actStatement, "if") {
if len(strings.TrimSpace(actStatement[strings.Index(actStatement, ")")+1:])) == 0 {
fmt.Println("Command gets executed in one thread !")
oneThreadProg, err, _ := parser.ParseProgram([]byte(awkCommand), config)
check(err)
for _, file := range args {
file := openFile(file)
defer file.Close()
defaultSize, multiple = helpFileReading(file, 1)
for iter := 0; iter < multiple; iter++ {
text = append(text, divideFile(file, 1, defaultSize, multiple)[0].buff...)
}
}
input := bytes.NewReader(text)
oneThreadConfig := &interp.Config{
Stdin: input,
Output: nil,
Error: ioutil.Discard,
Vars: []string{"OFS", offsetFieldSeparator, "FS", fieldSeparator},
Funcs: funcs,
}
_, err, _ = interp.ExecOneThread(oneThreadProg, oneThreadConfig, associativeArrays)
check(err)
end, err, _ := parser.ParseProgram([]byte(endStatement), nil)
check(err)
configEnd := &interp.Config{
Stdin: input,
Output: nil,
Error: ioutil.Discard,
Vars: []string{"OFS", " ", "FS", " "},
Funcs: funcs,
}
_, err, _ = interp.ExecOneThread(end, configEnd, associativeArrays)
check(err)
os.Exit(0)
}
}
}
}
prog, err, varTypes := parser.ParseProgram([]byte(eventualAwkCommand), config)
check(err)
// Responsible for executing the print statements that exist in the action statement. Uses one thread since print cannot be parallelised
if len(printStartIndex) > 0 && len(prog.Actions) == 1 && !strings.Contains(eventualAwkCommand, "for") {
fmt.Println("Command gets executed in one thread !")
if len(prog.Actions) == 1 {
pp, err, _ = parser.ParseProgram([]byte(bbb), nil)
check(err)
} else {
pp, err, _ = parser.ParseProgram([]byte(bbb[printStartIndex[0]-1:printEndIndex[0]]), nil)
check(err)
}
for _, file := range args {
file := openFile(file)
defer file.Close()
defaultSize, multiple = helpFileReading(file, 1)
for iter := 0; iter < multiple; iter++ {
text = append(text, divideFile(file, 1, defaultSize, multiple)[0].buff...)
}
}
input := bytes.NewReader(text)
config := &interp.Config{
Stdin: input,
Output: nil,
Error: ioutil.Discard,
Vars: []string{"OFS", offsetFieldSeparator, "FS", fieldSeparator},
}
_, err, _ = interp.ExecOneThread(pp, config, associativeArrays)
check(err)
end, err, _ := parser.ParseProgram([]byte(endStatement), nil)
check(err)
configEnd := &interp.Config{
Stdin: input,
Output: nil,
Error: ioutil.Discard,
Vars: []string{"OFS", " ", "FS", " "},
Funcs: funcs,
}
_, err, _ = interp.ExecOneThread(end, configEnd, associativeArrays)
check(err)
os.Exit(0)
}
funcnames := make([]string, 0, len(funcs))
for k := range funcs {
funcnames = append(funcnames, k)
}
// In case a command contains local arguments then it cannot be parallelized, so it gets executed in one thread
if len(varTypes) > 1 {
oneThreadProg, err, _ := parser.ParseProgram([]byte(awkCommand), config)
fmt.Println("Command gets executed in one thread !")
check(err)
for _, file := range args {
file := openFile(file)
defer file.Close()
defaultSize, multiple = helpFileReading(file, 1)
for iter := 0; iter < multiple; iter++ {
text = append(text, divideFile(file, 1, defaultSize, multiple)[0].buff...)
}
}
input := bytes.NewReader(text)
oneThreadConfig := &interp.Config{
Stdin: input,
Output: nil,
Error: ioutil.Discard,
Vars: []string{"OFS", offsetFieldSeparator, "FS", fieldSeparator},
Funcs: funcs,
}
_, err, _ = interp.ExecOneThread(oneThreadProg, oneThreadConfig, associativeArrays)
check(err)
end, err, _ := parser.ParseProgram([]byte(endStatement), nil)
check(err)
configEnd := &interp.Config{
Stdin: input,
Output: nil,
Error: ioutil.Discard,
Vars: []string{"OFS", " ", "FS", " "},
Funcs: funcs,
}
_, err, _ = interp.ExecOneThread(end, configEnd, associativeArrays)
check(err)
os.Exit(0)
}
// Used for creating the dump file in case the -d option is passed. Unlike gawk in case -d not provided with file then the dump file is not written
if dumpFile != "" {
dumpFile = `text_files/` + dumpFile
if _, err := os.Stat(dumpFile); err == nil {
err := os.Remove(dumpFile)
check(err)
}
dfile, err := os.OpenFile(dumpFile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
check(err)
defer dfile.Close()
for k := range varTypes[""] {
if k == "ARGV" {
continue
}
_, err = dfile.Write([]byte(k + "\n"))
check(err)
}
}
// Used for ensuring that only accumulation and assignment operations are allowed in action statements.
if len(prog.Actions) > 0 {
for _, pat := range prog.Actions {
actionStatement = pat.Stmts.String()
subAwkCommands := strings.Split(actionStatement, "\n")
for _, awkCommand := range subAwkCommands {
if len([]byte(strings.TrimSpace(awkCommand))) != 0 {
if isContained(awkCommand, funcnames) {
operations = append(operations, true)
} else if strings.Contains(awkCommand, "+") || strings.Contains(awkCommand, "-") {
operations = append(operations, false)
}
}
}
ok = false
if len(funcnames) > 0 {
actionSlice := strings.Fields(actionStatement)
for _, s := range actionSlice {
for _, n := range funcnames {
if strings.Contains(s, n) {
nameSlice = append(nameSlice, n)
ok = true
}
}
}
}
for _, char := range actionStatement {
if string(char) == "+" || string(char) == "-" {
ok = true
}
}
// stores to myVariable slice all the variables that exist in the action Statement
for _, char := range actionStatement {
if string(char) == "+" || string(char) == "-" || string(char) == "=" && proceed {
myVariable = append(myVariable, actionArgument)
actionArgument = ""
proceed = false
} else if string(char) != " " && proceed {
actionArgument = actionArgument + string(char)
} else if uint64([]byte(string(char))[0]) == 10 {
proceed = true
}
}
okArray = append(okArray, ok)
}
ok = true
for _, isOk := range okArray {
ok = ok && isOk
}
// If action statement does not contain a user defined function or an accumulation operation
if !ok && !strings.Contains(actionStatement, "print") {
fmt.Println("Command gets executed in one thread !")
oneThreadProg, err, _ := parser.ParseProgram([]byte(awkCommand), config)
check(err)
for _, file := range args {
file := openFile(file)
defer file.Close()
defaultSize, multiple = helpFileReading(file, 1)
for iter := 0; iter < multiple; iter++ {
text = append(text, divideFile(file, 1, defaultSize, multiple)[0].buff...)
}
}
input := bytes.NewReader(text)
oneThreadConfig := &interp.Config{
Stdin: input,
Output: nil,
Error: ioutil.Discard,
Vars: []string{"OFS", offsetFieldSeparator, "FS", fieldSeparator},
Funcs: funcs,
}
_, err, _ = interp.ExecOneThread(oneThreadProg, oneThreadConfig, associativeArrays)
check(err)
end, err, _ := parser.ParseProgram([]byte(endStatement), nil)
check(err)
configEnd := &interp.Config{
Stdin: input,
Output: nil,
Error: ioutil.Discard,
Vars: []string{"OFS", " ", "FS", " "},
Funcs: funcs,
}
_, err, _ = interp.ExecOneThread(end, configEnd, associativeArrays)
check(err)
os.Exit(0)
}
}
// checks that there are not empty variables
var variable []string
for _, vvv := range myVariable {
if isContained(vvv, variable) {
continue
}
if strings.Contains(vvv, "for") {
continue
}
if len([]byte(vvv)) > 0 {
variable = append(variable, vvv)
}
}
// In case there is an action body
if len(prog.Actions) > 0 {
// Goroutines usage for allowing paralle processing.
numCores = getNumCores()
fmt.Println("Number of cores is:", numCores)
numCores = 8
if numberOfThreads > numCores {
fmt.Println("Number of threads surpasses available CPU cores. Reverting to " + strconv.Itoa(numCores) + " threads. (Equal to the maximum number of CPU cores)")
numberOfThreads = numCores
}
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
dir += "/temp_files"
if _, err := os.Stat(dir); !os.IsNotExist(err) {
os.RemoveAll(dir)
}
os.MkdirAll(dir, 0777)
array := make([]*received, numberOfThreads)
arraysPerFile := make(map[int][]*received)
l := 0
channel := make(chan *received)
for _, file := range args {
file := openFile(file)
defer file.Close()
defaultSize, multiple = helpFileReading(file, numberOfThreads)
for iter := 0; iter < multiple; iter++ {
chunks := divideFile(file, numberOfThreads, defaultSize, multiple)
// for _, c := range chunks {
// fmt.Println("BEGIN")
// fmt.Println(string(c.buff))
// fmt.Println("END")
// }
for i := 0; i < numberOfThreads; i++ {
go func(chunks []chunk, i int, r chan<- *received) {
chunk := chunks[i]
res, names, arrays := goAwk(chunk.buff, prog, fieldSeparator, offsetFieldSeparator, funcs, i)
got := &received{results: res, functionNames: names, associativeArray: arrays}
r <- got
}(chunks, i, channel)
}
for i := 0; i < numberOfThreads; i++ {
array[i] = <-channel
}
arraysPerFile[l] = array
array = make([]*received, numberOfThreads)
l++
}
}
// Performs the suitable Reduction
mapOfVariables := make(map[string]float64)
for f := 0; f < l; f++ {
array = arraysPerFile[f]
j := 0
if len(variable) > 0 {
if len(operations) == len(variable) {
for i := 0; i < len(operations); i++ {
if operations[i] { //means we deal with native function
if nameSlice[j] == "min" {
min = array[0].results[i]
for _, ar := range array {
if len(ar.results) > 0 {
if ar.results[i] < min {
min = ar.results[i]
}
}
}
mapOfVariables[variable[i]] = min
} else if nameSlice[j] == "max" {
max = array[0].results[i]
for _, ar := range array {
if len(ar.results) > 0 {
if ar.results[i] > max {
max = ar.results[i]
}
}
}
mapOfVariables[variable[i]] = max
}
j++
} else {
for _, ar := range array {
if len(ar.results) > 0 {
mapOfVariables[variable[i]] += ar.results[i]
}
}
}
}
} else {
numOfArgs = len(variable)
r, _ := regexp.Compile("\\[[^\\]]*\\]")
for _, v := range variable {
isMatch := r.MatchString(v)
if isMatch {
numOfArgs++
}
}
if numOfArgs != len(operations) {
panic("Cannot use same variable in different reduction operations !")
}
}
}
if len(variable) > 0 {
if len(array[0].associativeArray) > 0 {
associativeValue = make(map[string]float64)
associativeValues = make(map[string]map[string]float64)
r, _ := regexp.Compile("\\[[^\\]]*\\]")
for i := 0; i < len(variable); i++ {
match := r.MatchString(variable[i])
if match {
for _, ar := range array {
for k := range ar.associativeArray {
associativeValue[k] += ar.associativeArray[k]
// associativeValues[variable[i]][k] += ar.associativeArray[k]
}
}
} else {
if mapOfVariables[variable[i]] == float64(0) {
for _, ar := range array {
for k := range ar.associativeArray {
mapOfVariables[variable[i]] += ar.associativeArray[k]
}
}
}
}
}
}
}
}
r, _ := regexp.Compile("\\[[^\\]]*\\]")
for i := 0; i < len(variable); i++ {
match := r.MatchString(variable[i])
if match {
variable[i] = variable[i][:strings.Index(variable[i], "[")]
associativeValues[variable[i]] = associativeValue
}
}
end, err, _ := parser.ParseProgram([]byte(endStatement), nil)
check(err)
arrayKeys := make([]string, 0, len(end.Arrays))
for k := range end.Arrays {
arrayKeys = append(arrayKeys, k)
}
associativeArrays = make(map[int]map[string]float64)
for i, k := range arrayKeys {
if k == "ARGV" {
associativeArrays[i] = make(map[string]float64)
} else {
for _, vf := range variable {
if vf == k {
associativeArrays[i] = associativeValues[k]
}
}
}
}
keys := make([]string, 0, len(end.Scalars))
for k := range end.Scalars {
keys = append(keys, k)
}
for _, k := range keys {
if isContained(k, variable) {
end.Scalars[k] = mapOfVariables[k]
} //else {
// panic("END Statement contains variables that have not been assigned!")
// toRemove = append(toRemove, k)
// }
}
// for _, rem := range toRemove {
// delete(end.Scalars, rem)
// }
input := bytes.NewReader([]byte(""))
configEnd := &interp.Config{
Stdin: input,
Output: nil,
Error: ioutil.Discard,
Vars: []string{"OFS", " ", "FS", " "},
Funcs: funcs,
}
myErr := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {