-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsc
376 lines (307 loc) · 7.72 KB
/
sc
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
#
# = [Beta] Aspell Speedy Check module =
#
# [
# |*| Source: https://github.com/MasterInQuestion/ShellUtils/blob/main/sc
# |*| Last update: CE 2021-05-15 11:26 UTC ]
#
# ----
#
# An Unix Shell script wrapper delegating to Aspell performing speedy spell check against the list of input files to ensure that all had been spelled out grammarly.
# Don't know anything particular to check? `sc /` works.
# (assuming the script was named "sc" and put effective under a directory of "$PATH")
#
# Compatibility Note:
# This script should work with all modern reasonable Unix shells.
# (tested with BusyBox environment; using its `ash`)
#
# Beta status:
# Old work mostly complete with some undone improvements.
# |1| Documentation.
# |2| Proper handling for STDIN input. (current implementation asserted file input)
# |3| Pipe input, and Null-separated single input list: shall be better handled.
# (typically that from `find -print0` output)
#
#
#
#
# == Implementation ==
#
# === Environment ===
#
# Modify the utility paths here to their corresponding value if necessary:
# [[
aspell='/usr/bin/aspell';
GNU_CoreUtils_readlink='/usr/bin/readlink';
GNU_grep='/usr/bin/grep';
perl='/usr/bin/perl';
# ]]
#
printHelp () {
rm -rf "$previouslyAddedDirs" "$files" "$filesToAdd"&
cat "$0";
exit;
};
inputEscape () {
# [Note 1]
PERLIO=':raw' "$perl" -p \
-e 'BEGIN { $^H |= 0x02000008; $^H{reflags_charset} = 4; $/ = "\x00"; }' \
-e '
s/(?='\'')/'\''\\'\''/g;
s/^\x00*/'\''/;
s/\x00*$/'\'' /;
';
};
checkDuplicate () {
x0="'${x0//\'/"'\\''"}'";
"$GNU_grep" -zF -e "$x0" -q "$1"; # [Note 2]
};
checkDuplicateStdin () {
x0="'${x0//\'/"'\\''"}'";
{ "$GNU_grep" -zF -e "$x0" -q <<EOF
$1
EOF
}; # [Note 2] [Note 3]
};
tempDir="$( readlink -f "${TMP:-"${TMPDIR:-"/tmp"}"}" )" || exit;
previouslyAddedDirs="$tempDir"'/[Aspell Speedy Check] Temp file: "$previouslyAddedDirs"';
files="$tempDir"'/[Aspell Speedy Check] Temp file: "$files"';
filesToAdd="$tempDir"'/[Aspell Speedy Check] Temp file: "$filesToAdd"';
unset tempDir;
{
echo -n >| "$previouslyAddedDirs" &&
echo -n >| "$files" && {
rm -rf "$filesToAdd";
mkfifo -m 600 "$filesToAdd";
};
} || {
rm -rf "$previouslyAddedDirs" "$files"&
exit;
};
# === Argument parsing ===
[ $# -ne 0 ] || printHelp;
for x0;
do {
[ -n "$x0" ] || continue;
# >>>>
if
[ -z $__ ] && {
[ "$x0" != '--' ] || { __=1; continue; };
} &&
# Whatever starting with "-" (before 1st "--") would be considered an "option":
{ grep -E -e '^-' -q <<EOF
$x0
EOF
};
# [ ^
# Using Shell string manipulation and comparison for this case:
# E.g. {
# x1="${x0#-}";
# [ ${#x0} -ne ${#x1} ];
# };
# ; may achieve better performance. (whatsoever marginally)
#
# The `grep` here is mostly demonstrational. ]
then {
# ==== Options ====
[ "$x0" != '--help' ] || printHelp;
options="$options '${x0//\'/"'\\''"}'";
};
else {
# ==== Files ====
# >>>> (1)
x0="$( readlink -f -- "$x0" )";
if
[ "$x0" != '/' ];
# ===== Normal case =====
then {
if
[ -d "$x0" ];
then
# >>>> (2)
checkDuplicate "$previouslyAddedDirs" || {
echo -nE "$x0 " >> "$previouslyAddedDirs"; # [Note 4]
x1="$( cat "$files" )";
eval '
for x0 in \
'"$(
eval '
find '"$x0"' -follow -type f -print0 |
# GNU CoreUtils `readlink` would be required to have it work normally:
xargs -0r '\'"${GNU_CoreUtils_readlink//\'/"'\\''"}"\'' -fz |
sort -uz | # [Note 5]
inputEscape;
';
)"';
do
checkDuplicateStdin "$x1" || echo -nE "$x0 "; # [Note 4]
done > "$filesToAdd"&
';
x0="$( cat "$filesToAdd" )";
echo -nE "$x0" >> "$files"; # [Note 4]
};
# <<<< (2)
else
checkDuplicate "$files" || echo -nE "$x0 " >> "$files"; # [Note 4]
fi;
};
# ===== Root directory =====
else
# >>>> (2)
grep -F -e "'/'" -q "$previouslyAddedDirs" || {
echo -nE "'/' " >> "$previouslyAddedDirs"; # [Note 4]
x1="$(
find '/' -type f -print0 |
sort -z | # [Note 5]
inputEscape;
)";
eval '
for x0 in \
'"$( cat "$files" )"';
do
checkDuplicateStdin "$x1" || echo -nE "$x0 "; # [Note 4]
done > "$filesToAdd"&
';
x0="$( cat "$filesToAdd" )";
echo -nE "${x0}${x1} " >| "$files"; # [Note 4]
unset x1;
};
# <<<< (2)
fi;
# <<<< (1)
};
fi;
# <<<<
};
done;
x1="$( cat "$files" )";
rm -rf "$previouslyAddedDirs" "$files" "$filesToAdd"&
#
# Commented to workaround some problematic shell string replace behavior on lengthy input: [Note 6]
# [[
# previouslyAddedDirs="'${previouslyAddedDirs//\'/"'\\''"}'";
# files="'${files//\'/"'\\''"}'";
#
# x1="${x1/"$previouslyAddedDirs "}";
# x1="${x1/"$files "}";
# ]]
# (no harm except possibly outputting 2 more meaningless entries on fringe cases)
#
unset previouslyAddedDirs files filesToAdd;
# === Delegating to Aspell ===
eval '
unset options;
for file in \
'"$x1"';
do
x0="$(
# Check `aspell --help` (or just googling) for parameter explanation.
'\'"${aspell//\'/"'\\''"}"\'' \
--encoding=UTF-8 \
--mode=url \
--lang=en_US \
--norm-form=none \
--ignore=1 \
--ignore-case=false \
--dont-run-together \
--run-together-limit=2 \
--run-together-min=3 \
--camel-case=false \
'"$options"' \
list \
2>&1 < "$file";
)";
x1=$?;
# Print only when `aspell` outputs non-empty:
[ -n "$x0" ] && {
fileEscaped="${file%\\}";
if
[ ${#file} -eq ${#fileEscaped} ];
then
fileEscaped="${fileEscaped//\"/\\\"}";
else
fileEscaped="${fileEscaped//\"/\\\"}\\\\";
fi;
printf '\''|*| "%s":\n'\'' "$fileEscaped";
echo -E '\''[['\''; # [Note 4]
if
[ $x1 -eq 0 ];
then
# Sorting and deduplication:
printf '\''%s\n'\'' "$( sort -u <<EOF
$x0
EOF
)";
else
printf '\''%s\n'\'' "$x0";
fi;
echo -e '\'']]\n'\''; # [Note 4]
};
done;
unset file fileEscaped x0 x1;
';
#
#
#
#
# == Notes & References ==
#
#
# === Notes ===
#
# [ [Note 1]
# Perl magic related.
# PLACEHOLDER
# https://github.com/MasterInQuestion/perlp ]
#
# [ [Note 2]
# The checking here would not work as intended, if the input was of some silly filenames that shall contain "\n" (Line Feed) (U+000A, 0x0A, ): [ Also check: [Note 5] ]
# As `grep` normally operates on a "per-line" basis (thus the embedding "\n" cannot be matched normally).
#
# To fix the problem would require using the GNU `grep`:
# [[
# grep -zF -e ...
# ]]
# (applying the "-z" flag: choosing "\x00" (Null) (U+0000, 0x00, �) as the record separator instead of "\n") ]
#
# [ [Note 3]
# Removing the part:
# [[
# { grep -F -e "$x0" -q <<EOF
# $1
# EOF
# };
# ]]
# (skipping the duplicate check on previously added files)
# Can significantly speed up directory handling. (mostly for ones that contain many items)
#
# Off-Topic:
# Despite the logic has been highly optimized, due to its actuation being expensive:
# The commands may be actually slower, than simply adding everything blindly then perform a `sort -uz` at the last stage (before delegating to Aspell). ]
#
# [ [Note 4]
# Another problem is that some unreasonable shells may not recognize the "-e" / "-E" flags of `echo` normally:
# Regarding them as normal output content and performing escape sequence interpretation unconditionally: resulting in unwanted output.
#
# `printf` should work portably, but which might cause unnecessary performance overhead:
# |1| It's not necessarily a shell built-in (while `echo` is, in pretty much all cases).
# ; which means whose execution can be much more expensive.
# (a prominent example here: `mksh`)
# |2| Interpreting the `printf` formatting directive is regardless more complex:
# ; i.e. more work has to be done; than to simply `echo` something directly.
# (despite the usually marginal performance difference) ]
#
# [ [Note 5]
# As of BusyBox v1.33.0, `sort -z` doesn't work properly.
# (incorrect record splitting behavior with "\n")
# More details: ${URL} ]
#
# [ [Note 6]
# Actually significant speed-up can be noticed in general cases. (with BusyBox `ash`) ]
#
#
# === References ===
#
# [ [1]
# PLACEHOLDER ]
#