/
processfile.go
149 lines (138 loc) · 3.46 KB
/
processfile.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
package main
import (
"bufio"
"fmt"
"io"
"regexp"
"strings"
"github.com/batmac/ccat/pkg/globalctx"
"github.com/batmac/ccat/pkg/highlighter"
"github.com/batmac/ccat/pkg/log"
"github.com/batmac/ccat/pkg/mutators/pipeline"
_ "github.com/batmac/ccat/pkg/mutators/single"
"github.com/batmac/ccat/pkg/openers"
"github.com/batmac/ccat/pkg/scanners"
"github.com/batmac/ccat/pkg/term"
// _ "net/http/pprof"
)
func processFile(w io.Writer, path string) {
log.Debugf("processing %s...\n", path)
globalctx.Set("path", path)
from, err := openers.Open(path, *argLockIn)
if err != nil {
log.Printf("opening %s: %v", path, err)
setErrored()
return
}
if len(*argMutators) > 0 {
r, w := io.Pipe()
err := pipeline.NewPipeline(*argMutators, w, from)
if err != nil {
setErrored()
log.Fatal(err)
}
from = r
}
fromOrig := from
defer func() {
// I don't want to determine if already closed, try to close it, it will fail if it is already closed
err := fromOrig.Close()
if err != nil {
log.Debugln(err)
}
log.Debugf("final closed %s...\n", path)
}()
/*************************************/
if *argHuman {
if term.IsArt(path) && len(*argMutators) == 0 {
log.Debugf("is art, displaying directly...\n")
term.PrintArt(from)
return
}
expectingBinary := globalctx.Get("expectingBinary")
if expectingBinary == nil || !expectingBinary.(bool) {
log.Debugln("highlighting...")
hl := globalctx.Get("hintLexer")
if len(*argLexer) == 0 && hl != nil && len(hl.(string)) != 0 {
hl := hl.(string)
argLexer = &hl
}
r, w := io.Pipe()
err := highlighter.Go(w, from, *highlighter.NewOptions(
path,
strings.ToLower(*argStyle),
strings.ToLower(*argFormatter),
*argLexer,
))
if err != nil {
setErrored()
log.Printf("error while highlighting: %v", err)
} else {
from = r
}
}
}
log.Debugln("initializing Scanner...")
// go http.ListenAndServe(":8090", nil)
scanner := bufio.NewScanner(from)
splitFn := scanners.ScanBytes
// splitFn := scanners.ScanLines
if len(tokens) > 0 || *argLineNumber || *argOnlyMatching {
log.Debugln("splitting on Lines...")
splitFn = scanners.ScanLines
}
hintSlowOutput := globalctx.Get("hintSlowOutput")
if *argSplitByWords || hintSlowOutput != nil && hintSlowOutput.(bool) {
if *argLineNumber {
log.Println("warning: -n is not compatible with -w or your last mutator, ignoring -n")
*argLineNumber = false
}
log.Debugln("splitting on Words...")
splitFn = scanners.ScanWords
}
scanner.Split(splitFn)
lineNumber := 1
log.Debugln("start Scanner...")
for scanner.Scan() {
var matched bool
text := scanner.Bytes()
for _, token := range tokens {
var err error
var regexpPrefix string
if *argInsensitive {
regexpPrefix = "(?i)"
}
// fmt.Println("text ", text)
// fmt.Println("token ", token)
matched, err = regexp.Match(regexpPrefix+token, text)
if err != nil {
setErrored()
log.Println(err)
}
if matched {
color := tmap[token]
text = []byte(color.Sprint(string(text)))
break
}
}
if *argLineNumber {
fmt.Printf("%d ", lineNumber)
lineNumber++
}
if !*argOnlyMatching || matched && *argOnlyMatching {
_, _ = w.Write(text)
}
}
if err := scanner.Err(); err != nil {
setErrored()
log.Println(err)
}
log.Debugf("end Scanner, processing %v completed.\n", path)
if len(*argMutators) > 0 {
log.Debugln("Wait()ing pipeline...")
pipeline.Wait()
}
}
func setErrored() {
globalctx.SetErrored()
}