-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsing.go
287 lines (273 loc) · 8.4 KB
/
parsing.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
/*
Copyright 2023, RoboMac
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
// Holds the command parsing of dir.
import (
_ "embed"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/gobwas/glob"
)
// Format-Print only if cond == true
func conditionalPrint(cond bool, format string, a ...any) {
if cond {
fmt.Printf(format, a...)
}
}
// Choices are:
// Default: current directory, all files, no filtering.
// Passed value is a directory name - list all files in it.
// Passed value is a file name or wildcard pattern - list matching files.
// Passed value has both. i.e. the beginning is a directory to start in,
// with a wildcard or filename at the end. Has a slash + content.
func parseFileName(param string) {
fileMask := param
conditionalPrint((show_errors || debug_messages) && (len(start_directory) > 0 || filenameParsed),
" *** WARNING: Multiple filename parameters found. Had %s %s, now %s.\nShould you quote to avoid globbing?\n",
start_directory, file_mask, param)
conditionalPrint(debug_messages, "Parsing file name %s\n", param)
if strings.HasPrefix(param, "~") {
home, _ := os.UserHomeDir()
param = strings.Replace(param, "~", home, 1)
}
// Do we need to deal with a directory specification?
if strings.Contains(param, "/") {
// We have a start directory. Do we have a file pattern? See if this opens.
d, err := os.Stat(param)
if err == nil {
if d.IsDir() {
start_directory = param
// No filemask. Done
conditionalPrint(debug_messages, "Parsed %s to directory, no file.\n", param)
return
}
}
// Try with just the end.
dirPath := param[:strings.LastIndex(param, "/")]
d, err = os.Stat(dirPath)
if err == nil {
if d.IsDir() {
start_directory = dirPath
fileMask = param[strings.LastIndex(param, "/")+1:]
} else {
extension := "," + dirPath[strings.LastIndex(dirPath, ".")+1:] + ","
if strings.Contains(Extensions[ARCHIVE], extension) {
// Flag this as the source file to be read.
pathIsArchive = true
start_directory = dirPath
fileMask = param[strings.LastIndex(param, "/")+1:]
}
}
}
}
// Is this actually a directory name itself?
d, err := os.Stat(param)
if err == nil && d.IsDir() {
start_directory = param
return
}
// We have a mask. Build the globber
file_mask = fileMask
haveGlobber = true // We don't yet have it... we have to process all the parameters to see if case-sensitive first.
filenameParsed = true
conditionalPrint(debug_messages, "Parameter %s parsed to directory %s, file mask %s.\n", param, start_directory, file_mask)
}
func parseDateRange(v string) (time.Time, time.Time) {
var err error
dateRange := strings.Split(v, ":")
if len(dateRange) == 0 {
conditionalPrint(show_errors, "Invalid date range: %s\n", v)
return mindate, maxdate
}
mindate, err = time.Parse("2006-01-02", dateRange[0])
if err != nil {
conditionalPrint(show_errors, "Invalid date range: %s - %s\n", v, err.Error())
}
if (len(dateRange) > 1) && (len(dateRange[1]) > 1) {
maxdate, err = time.Parse("2006-01-02", dateRange[1])
if err != nil {
conditionalPrint(show_errors, "Invalid date range: %s - %s\n", v, err.Error())
}
maxdate = maxdate.Add((time.Hour * 24) - time.Duration(maxdate.Hour()))
}
return mindate, maxdate
}
func parseSizeRange(v string) {
var err error
sizeRange := strings.Split(v, ":")
if len(sizeRange) == 0 {
conditionalPrint(show_errors, "Invalid size range: %s\n", v)
return
}
if len(sizeRange[0]) > 0 {
minsize, err = strconv.ParseInt(sizeRange[0], 10, 64)
if err != nil {
conditionalPrint(show_errors, "Invalid size range: %s - %s\n", v, err.Error())
}
}
if len(sizeRange) > 1 && len(sizeRange[1]) > 0 {
maxsize, err = strconv.ParseInt(sizeRange[1], 10, 64)
if err != nil {
conditionalPrint(show_errors, "Invalid size range: %s - %s\n", v, err.Error())
}
}
}
func parseCmdLine() {
var args = os.Args[1:] // 0 is program name
// args is all strings that are space-separated.
// The filename is the only thing that doesn't start with - or /
for i, s := range args {
conditionalPrint(debug_messages, "Processing argument %d: %s.\n", i, s)
// Can't use / as flag separator if /Users, e.g., is valid
// There are no one-or-two character / folders. But check for /usr vs /o-n
// So -*, /x, /xx and /x{+_}x are legal as parameters
isParam := strings.HasPrefix(s, "-") || s == "/?" || s == "/help"
if (!isParam) && (strings.HasPrefix(s, "/")) {
if len(s) == 3 {
isParam = true
} else if (len(s) == 4) && (strings.Contains(s, "-") || strings.Contains(s, "+")) {
isParam = true
}
}
if isParam {
// Linux apps often allow params to be combined on a line. That could be
// tricky for /on or other multi-character flags
// sort: o{-}{ndstx} (t and x are both extension)
// header: v+ or v-
// Version 1 just handles them separate
p := s[1:]
values := ""
if strings.Contains(p, "=") {
pieces := strings.Split(p, "=")
p = pieces[0]
values = pieces[1]
}
switch p {
case "?", "h", "help", "-help", "-h":
fmt.Println(helptext)
os.Exit(0)
case "on":
sortby = sortorder{SORT_NAME, true}
case "o-n":
sortby = sortorder{SORT_NAME, false}
case "od":
sortby = sortorder{SORT_DATE, true}
case "o-d":
sortby = sortorder{SORT_DATE, false}
case "oc":
sortby = sortorder{SORT_CREATED, true}
case "o-c":
sortby = sortorder{SORT_CREATED, false}
case "oa":
sortby = sortorder{SORT_ACCESSED, true}
case "o-a":
sortby = sortorder{SORT_ACCESSED, false}
case "ox":
sortby = sortorder{SORT_EXT, true}
case "o-x":
sortby = sortorder{SORT_EXT, false}
case "ot":
sortby = sortorder{SORT_TYPE, true}
case "o-t":
sortby = sortorder{SORT_TYPE, false}
case "os":
sortby = sortorder{SORT_SIZE, true}
case "o-s":
sortby = sortorder{SORT_SIZE, false}
case "ah-":
listhidden = false
case "cs":
case_sensitive = true
case "b+":
bare = true
include_path = true
size_calculations = false
directory_header = false
case "b":
bare = true
size_calculations = false
directory_header = false
include_path = false
case "c": // Change column definition for output
columnDef = values
case "d+":
listfiles = false
listdirectories = true
case "d-":
listdirectories = false
case "debug":
debug_messages = true
case "error", "errors":
show_errors = true
case "G-":
use_colors = false
case "G":
use_colors = true
use_enhanced_colors = false
case "G+":
use_colors = true
use_enhanced_colors = true
case "ma": // Accessed Date
parseDateRange(values)
minmaxdatetype = "a"
case "mc": // Created Date
parseDateRange(values)
minmaxdatetype = "c"
case "md": // Parse dates, compare to Time.IsZero()
parseDateRange(values)
minmaxdatetype = "m"
case "ms": // Parse sizes
parseSizeRange(values)
case "r":
recurse_directories = true
case "sc": // Use commas (local sep) in file sizes
filesizes_format = SIZE_SEPARATOR
case "sh": // Use GB,TB, etc. in file sizes
filesizes_format = SIZE_QUANTA
case "sr": // Standard default sizes - bytes with no formatting
filesizes_format = SIZE_NATURAL
case "t":
listfiles = false
case "tc": // Case-sensitive search
text_search_type = SEARCH_CASE
text_regex = regexp.MustCompile(values)
case "ti": // Case-insensitive search
text_search_type = SEARCH_NOCASE
text_regex = regexp.MustCompile("(?i)" + values)
case "tr": // Regex search
text_search_type = SEARCH_REGEX
text_regex = regexp.MustCompile(values)
case "version", "v":
fmt.Println(versionDate)
os.Exit(0)
case "exclude", "x":
exclude_exts = strings.Split(strings.ToUpper(values), ",")
case "z":
listInArchives = true
}
} else {
parseFileName(s)
}
}
if haveGlobber {
mask := file_mask
if !case_sensitive {
mask = strings.ToUpper(mask)
}
matcher = glob.MustCompile(mask)
}
}