-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
206 lines (185 loc) · 4.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
package main
import (
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"strconv"
"strings"
"time"
)
var FS, inFile, outfile, datafile string
func main() {
t1 := time.Now()
flag.StringVar(&FS, "D", ",", "-D <char>")
flag.StringVar(&inFile, "f", "", "-f <textfile>")
flag.StringVar(&outfile, "o", "", "-o <awkfile>")
flag.StringVar(&datafile, "A", "", "-A <datafile>")
flag.Parse()
var fileIN, fileOUT = os.Stdin, os.Stdout
if inFile != "" {
file, err := os.Open(inFile)
if err != nil {
log.Fatal("Cannot Open inputfile! Exiting")
}
fileIN = file
}
if outfile != "" {
file, err := os.Create(outfile)
if err != nil {
log.Fatal("Cannot Open outputfile! Exiting")
}
fileOUT = file
}
if datafile != "" {
f, err := ioutil.TempFile(os.TempDir(), "awk-")
if err != nil {
log.Fatal("could not create tmpfile: ", err)
}
defer os.Remove(f.Name())
defer f.Close()
fileData, err := os.Open(datafile)
if err != nil {
log.Fatal("Cannot Open datafile! Exiting", err)
}
convert(FS, fileIN, f)
_, err = f.Seek(0, 0)
if err != nil {
log.Fatal("Could not reset script file to Start of file: ", err)
}
awk(f, fileData, fileOUT)
} else {
convert(FS, fileIN, fileOUT)
}
log.Println("Convert took ", time.Since(t1))
}
func convert(FS string, inFILE, outFILE *os.File) {
osOutFile := os.Stdout
osInFile := os.Stdin
os.Stdout = outFILE
data, err := io.ReadAll(inFILE)
if err != nil {
log.Fatal("STDIN is broken")
}
text := string(data)
section := 1
num := strings.Count(text, "<@>")
if num > 1 {
section = 0
}
text = strings.ReplaceAll(text, "<@>", "<@><->")
text = strings.ReplaceAll(text, "\\<", "<(>")
text = strings.ReplaceAll(text, "\\>", "<)>")
text = strings.ReplaceAll(text, "\n", "\\n\n")
text = strings.ReplaceAll(text, "<", "\n<")
text = strings.ReplaceAll(text, ">", ">\n")
lines := strings.Split(string(text), "\n")
fmt.Println(`
###############################################################################
# #
# This is an automatic build awk-script #
# It is constructed by a reimplementation of the Program MKAWK #
# Which was written by M.Wellner, Berlin Feb.1989 #
# Rewritten by P.Wellner, Berlin Jan.2023 #
# #
###############################################################################
BEGIN { FS="` + FS + `"
`)
if section == 1 {
fmt.Println("}\n{")
}
skip := false
for _, line := range lines {
if skip {
if strings.Contains(line, "\\n") {
skip = false
}
continue
}
if strings.Contains(line, "<") && strings.Contains(line, ">") {
command, err := parseCommand(line, section)
if err == nil {
if command.Type() == "SKIP" {
skip = true
}
if command.Type() == "NEXT_SECTION" {
section++
}
fmt.Print(command)
continue
}
log.Printf("ERR: Could not parse cmd %q : %v", line, err)
}
fmt.Printf(`printf "%s"`+"\n", line)
}
fmt.Println("} # END OF AWK-SCRIPT")
os.Stdout = osOutFile
os.Stdin = osInFile
}
func parseCommand(line string, section int) (Command, error) {
cmd := line[1 : len(line)-1]
switch cmd[0] {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '$':
return parseNumberCommand(cmd)
case '-':
return SkipCommand{}, nil
case '(':
return TextCommand{Text: "<"}, nil
case ')':
return TextCommand{Text: ">"}, nil
case '!':
return AWKCommand{Text: cmd[1:]}, nil
case '#':
return CommentaryCommand{Text: cmd[1:]}, nil
case '%':
return parseFormatCommand(cmd)
case '?':
if strings.TrimSpace(cmd[1:]) == "" {
return AWKCommand{Text: "}"}, nil
}
return AWKCommand{Text: fmt.Sprintf("if (%s) {", cmd[1:])}, nil
case '@':
return NextSectionCommand{Section: section}, nil
}
return nil, fmt.Errorf("no such command: %q", line)
}
func parseNumberCommand(cmd string) (Command, error) {
if cmd[0] == '$' {
cmd = cmd[1:]
}
number, err := strconv.Atoi(cmd)
if err != nil {
return nil, err
}
return NumberCommand{Number: number}, nil
}
func parseFormatCommand(cmd string) (Command, error) {
before, after, found := strings.Cut(cmd, ",")
if !found {
return nil, errors.New("no data for format found")
}
return FormatCommand{Format: before, Data: after}, nil
}
func awk(fileScript, fileData, fileOut *os.File) {
cmd := exec.Command("awk")
cmd.Stdin = fileData
cmd.Stdout = fileOut
cmd.Args = append(cmd.Args, "-f", fileScript.Name())
log.Println(cmd)
err := cmd.Run()
if err != nil {
var exitError *exec.ExitError
if errors.As(err, &exitError) {
log.Println(exitError)
log.Println(exitError.ProcessState)
log.Println(exitError.Stderr)
log.Println(exitError.ExitCode())
} else {
log.Println("There was an unknown error executing Script: ", err)
}
}
}