-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathis_available.sh
executable file
·478 lines (440 loc) · 12.1 KB
/
is_available.sh
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
#!/usr/bin/env bash
#
# is_available.sh - check if a tool is an executable file, and in some cases, is sane
#
# This script was written in 2024 by:
#
# @xexyl
# https://xexyl.net Cody Boone Ferguson
# https://ioccc.xexyl.net
# and:
# chongo (Landon Curt Noll, http://www.isthe.com/chongo/index.html) /\oo/\
#
# "Because sometimes even the IOCCC Judges need some help." :-)
#
# "Because even the hard working helpers sometime need a helping hand." :-)
#
# Share and enjoy! :-)
export VERSION="1.1.1 2024-10-01"
NAME=$(basename "$0")
export NAME
export PRINT_WHERE=""
export V_FLAG="0"
export USAGE="usage: $0 [-h] [-V] [-v level] [-w] tool
-h print help and exit
-V print version and exit
-v level set verbosity level for this script: (def level: $V_FLAG)
-w if tool is a sane executable file, print path to the tool (def: be silent)
tool tool to check if it is an executable file
NOTE: If tool is one of: shellcheck picky independ seqcexit checknr
then a sanity check using known good data and args will also be performed
Exit codes:
0 all OK
1 tool is not an executable file or failed a sanity test
2 -h and help string printed or -V and version string printed
3 invalid command line
>= 10 internal error or missing file or directory
$NAME version: $VERSION"
# parse args
#
while getopts :hVv:w flag; do
case "$flag" in
h) echo "$USAGE" 1>&2
exit 2
;;
V) echo "$VERSION"
exit 2
;;
v) V_FLAG="$OPTARG";
;;
w) PRINT_WHERE="true"
;;
\?) echo "$0: ERROR: invalid option: -$OPTARG" 1>&2
echo 1>&2
echo "$USAGE" 1>&2
exit 3
;;
:) echo "$0: ERROR: option -$OPTARG requires an argument" 1>&2
echo 1>&2
echo "$USAGE" 1>&2
exit 3
;;
*)
;;
esac
done
# check args
#
shift $(( OPTIND - 1 ));
if [[ $# -ne 1 ]]; then
echo "$0: ERROR: expected one arg, got $#" 1>&2
echo 1>&2
echo "$USAGE" 1>&2
exit 3
elif [[ -z "$1" ]]; then
echo "$0: ERROR: expected one non-empty arg" 1>&2
echo 1>&2
echo "$USAGE" 1>&2
exit 3
fi
export ARG="$1"
# look for tool along the $PATH
#
TOOL="$(type -P "$ARG")"
if [[ -n "$TOOL" ]]; then
if [[ "$V_FLAG" -ge 1 ]]; then
echo "$0: debug[1]: found tool along \$PATH: $TOOL" 1>&2
fi
else
if [[ "$V_FLAG" -ge 1 ]]; then
echo "$0: debug[1]: tool not found along \$PATH: $ARG" 1>&2
fi
exit 1
fi
# paranoia
#
# In case we are dealing with a BOGUS type -P.
#
if [[ ! -f $TOOL ]]; then
if [[ "$V_FLAG" -ge 1 ]]; then
echo "$0: debug[1]: tool does not exist: $TOOL" 1>&2
fi
exit 1
elif [[ "$V_FLAG" -ge 3 ]]; then
echo "$0: debug[3]: tool exists: $TOOL" 1>&2
fi
# more paranoia - tool must be an executable file
#
if [[ ! -f $TOOL ]]; then
if [[ "$V_FLAG" -ge 1 ]]; then
echo "$0: debug[1]: tool is not a file: $TOOL" 1>&2
fi
exit 1
elif [[ "$V_FLAG" -ge 3 ]]; then
echo "$0: debug[3]: tool is a file: $TOOL" 1>&2
fi
if [[ ! -x $TOOL ]]; then
if [[ "$V_FLAG" -ge 1 ]]; then
echo "$0: debug[1]: tool is not an executable file: $TOOL" 1>&2
fi
exit 1
elif [[ "$V_FLAG" -ge 3 ]]; then
echo "$0: debug[3]: tool is an executable file: $TOOL" 1>&2
fi
# sanity check if the tool is one of the special: shellcheck, picky, independ, or seqcexit
#
TOOL_BASENAME="${TOOL##*/}"
export TOOL_BASENAME
case "$TOOL_BASENAME" in
# try shellcheck on a trivial bash script
#
shellcheck)
# form a trivial bash script
#
export TMP_BASH_SCRIPT=".tmp.$NAME.BASH_SCRIPT.$$.sh"
trap 'rm -f $TMP_BASH_SCRIPT; exit' 0 1 2 3 15
rm -f "$TMP_BASH_SCRIPT"
if [[ -e $TMP_BASH_SCRIPT ]]; then
echo "$0: ERROR: cannot remove temporary bash script: $TMP_BASH_SCRIPT" 1>&2
exit 10
fi
printf '%s\n%s\n' '#!/usr/bin/env bash' 'exit 0' > "$TMP_BASH_SCRIPT"
if [[ ! -e $TMP_BASH_SCRIPT ]]; then
echo "$0: ERROR: cannot create temporary bash script file: $TMP_BASH_SCRIPT" 1>&2
exit 11
fi
chmod +x "$TMP_BASH_SCRIPT"
status="$?"
if [[ $status -ne 0 ]]; then
echo "$0: ERROR: chmod +x $TMP_BASH_SCRIPT" \
"failed, error code: $status" 1>&2
exit 12
fi
# try shellcheck on the trivial bash script
#
if [[ "$V_FLAG" -ge 3 ]]; then
echo "$0: debug[3]: about to run: $TOOL --shell=bash -- $TMP_BASH_SCRIPT" 1>&2
fi
TOOL_OUTPUT=$("$TOOL" --shell=bash -- "$TMP_BASH_SCRIPT" 2>&1)
status="$?"
export TOOL_OUTPUT
if [[ $status -ne 0 ]]; then
if [[ "$V_FLAG" -ge 1 ]]; then
echo "$0: debug[1]: tool failed the trivial test: $TOOL" 1>&2
fi
if [[ "$V_FLAG" -ge 3 ]]; then
echo "$0: debug[3]: $TOOL --shell=bash -- $TMP_BASH_SCRIPT failed," \
"error: $status" 1>&2
fi
exit 1
elif [[ -n "$TOOL_OUTPUT" ]]; then
if [[ "$V_FLAG" -ge 1 ]]; then
echo "$0: debug[1]: unexpected output from the trivial test: $TOOL" 1>&2
if [[ "$V_FLAG" -ge 3 ]]; then
echo "$0: debug[3]: unexpected output starts below" 1>&2
echo "$TOOL_OUTPUT" 1>&2
echo "$0: debug[3]: unexpected output ends above" 1>&2
fi
fi
exit 1
elif [[ "$V_FLAG" -ge 3 ]]; then
echo "$0: debug[3]: tool passed the trivial test: $TOOL" 1>&2
fi
;;
picky)
# form a trivial C source file
#
export TMP_C_SRC=".tmp.$NAME.C_SRC.$$.c"
trap 'rm -f $TMP_C_SRC; exit' 0 1 2 3 15
rm -f "$TMP_C_SRC"
if [[ -e $TMP_C_SRC ]]; then
echo "$0: ERROR: cannot remove temporary C source: $TMP_C_SRC" 1>&2
exit 13
fi
cat > "$TMP_C_SRC" << EOF
#include <stdio.h>
int
main(void)
{
printf("hello, world\n");
return 0;
}
EOF
if [[ ! -e $TMP_C_SRC ]]; then
echo "$0: ERROR: cannot create temporary C source file: $TMP_C_SRC" 1>&2
exit 14
fi
# try picky on the trivial C source
#
if [[ "$V_FLAG" -ge 3 ]]; then
echo "$0: debug[3]: about to run: $TOOL -w132 -u -s -t8 -v -e -- $TMP_C_SRC" 1>&2
fi
TOOL_OUTPUT=$("$TOOL" -w132 -u -s -t8 -v -e -- "$TMP_C_SRC" 2>&1)
status="$?"
export TOOL_OUTPUT
if [[ $status -ne 0 ]]; then
if [[ "$V_FLAG" -ge 1 ]]; then
echo "$0: debug[1]: tool failed the trivial test: $TOOL" 1>&2
fi
if [[ "$V_FLAG" -ge 3 ]]; then
echo "$0: debug[3]: $TOOL -w132 -u -s -t8 -v -e -- $TMP_C_SRC failed," \
"error: $status" 1>&2
fi
exit 1
elif [[ -n "$TOOL_OUTPUT" ]]; then
if [[ "$V_FLAG" -ge 1 ]]; then
echo "$0: debug[1]: unexpected output from the trivial test: $TOOL" 1>&2
if [[ "$V_FLAG" -ge 3 ]]; then
echo "$0: debug[3]: unexpected output starts below" 1>&2
echo "$TOOL_OUTPUT" 1>&2
echo "$0: debug[3]: unexpected output ends above" 1>&2
fi
fi
exit 1
elif [[ "$V_FLAG" -ge 3 ]]; then
echo "$0: debug[3]: tool passed the trivial test: $TOOL" 1>&2
fi
;;
independ)
# find the C compiler
#
CC="$(type -P "cc")"
if [[ -n "$CC" ]]; then
if [[ "$V_FLAG" -ge 3 ]]; then
echo "$0: debug[3]: found C compiler along \$PATH: $CC" 1>&2
fi
else
if [[ "$V_FLAG" -ge 3 ]]; then
echo "$0: debug[3]: C compiler not found along \$PATH: cc" 1>&2
fi
exit 15
fi
# paranoia - # In case we are dealing with a BOGUS type -P.
if [[ ! -f $CC ]]; then
if [[ "$V_FLAG" -ge 3 ]]; then
echo "$0: debug[3]: C compiler does not exist: $CC" 1>&2
fi
exit 16
elif [[ "$V_FLAG" -ge 5 ]]; then
echo "$0: debug[5]: C compiler exists: $CC" 1>&2
fi
# more paranoia - C compiler must be an executable file
if [[ ! -f $CC ]]; then
if [[ "$V_FLAG" -ge 3 ]]; then
echo "$0: debug[3]: C compiler is not a file: $CC" 1>&2
fi
exit 17
elif [[ "$V_FLAG" -ge 5 ]]; then
echo "$0: debug[5]: C compiler is a file: $CC" 1>&2
fi
if [[ ! -x $CC ]]; then
if [[ "$V_FLAG" -ge 3 ]]; then
echo "$0: debug[3]: C compiler is not an executable file: $CC" 1>&2
fi
exit 18
elif [[ "$V_FLAG" -ge 5 ]]; then
echo "$0: debug[5]: C compiler is an executable file: $CC" 1>&2
fi
# form a trivial C source file
#
export TMP_C_SRC=".tmp.$NAME.C_SRC.$$.c"
trap 'rm -f $TMP_C_SRC; exit' 0 1 2 3 15
rm -f "$TMP_C_SRC"
if [[ -e $TMP_C_SRC ]]; then
echo "$0: ERROR: cannot remove temporary C source: $TMP_C_SRC" 1>&2
exit 19
fi
cat > "$TMP_C_SRC" << EOF
#include <stdio.h>
int
main(void)
{
printf("hello, world\n");
return 0;
}
EOF
if [[ ! -e $TMP_C_SRC ]]; then
echo "$0: ERROR: cannot create temporary C source file: $TMP_C_SRC" 1>&2
exit 20
fi
# try independ on the trivial C source
#
if [[ "$V_FLAG" -ge 3 ]]; then
echo "$0: debug[3]: about to run: $CC -MM $TMP_C_SRC | $TOOL" 1>&2
fi
TOOL_OUTPUT=$("$CC" -MM "$TMP_C_SRC" | "$TOOL" 2>&1)
status_codes=("${PIPESTATUS[@]}")
export TOOL_OUTPUT
if [[ ${status_codes[*]} =~ [1-9] ]]; then
if [[ "$V_FLAG" -ge 1 ]]; then
echo "$0: debug[1]: tool failed the trivial test: $TOOL" 1>&2
fi
if [[ "$V_FLAG" -ge 3 ]]; then
echo "$0: debug[3]: $CC -MM $TMP_C_SRC | $TOOL failed," \
"error codes: ${status_codes[*]}" 1>&2
fi
exit 1
elif [[ -z "$TOOL_OUTPUT" ]]; then
if [[ "$V_FLAG" -ge 1 ]]; then
echo "$0: debug[1]: no output from the trivial test: $TOOL" 1>&2
fi
exit 1
elif [[ "$V_FLAG" -ge 3 ]]; then
echo "$0: debug[3]: tool passed the trivial test: $TOOL" 1>&2
fi
;;
seqcexit)
# form a trivial C source file
#
export TMP_C_SRC=".tmp.$NAME.C_SRC.$$.c"
trap 'rm -f $TMP_C_SRC; exit' 0 1 2 3 15
rm -f "$TMP_C_SRC"
if [[ -e $TMP_C_SRC ]]; then
echo "$0: ERROR: cannot remove temporary C source: $TMP_C_SRC" 1>&2
exit 21
fi
cat > "$TMP_C_SRC" << EOF
#include <stdio.h>
int
main(void)
{
printf("hello, world\n");
return 0;
}
EOF
if [[ ! -e $TMP_C_SRC ]]; then
echo "$0: ERROR: cannot create temporary C source file: $TMP_C_SRC" 1>&2
exit 22
fi
# try seqcexit on the trivial C source
#
if [[ "$V_FLAG" -ge 3 ]]; then
echo "$0: debug[3]: about to run: $TOOL -- $TMP_C_SRC" 1>&2
fi
TOOL_OUTPUT=$("$TOOL" -- "$TMP_C_SRC" 2>&1)
status="$?"
export TOOL_OUTPUT
if [[ $status -ne 0 ]]; then
if [[ "$V_FLAG" -ge 1 ]]; then
echo "$0: debug[1]: tool failed the trivial test: $TOOL" 1>&2
fi
if [[ "$V_FLAG" -ge 3 ]]; then
echo "$0: debug[3]: $TOOL -- $TMP_C_SRC failed," \
"error: $status" 1>&2
fi
exit 1
elif [[ -n "$TOOL_OUTPUT" ]]; then
if [[ "$V_FLAG" -ge 1 ]]; then
echo "$0: debug[1]: unexpected output from the trivial test: $TOOL" 1>&2
if [[ "$V_FLAG" -ge 3 ]]; then
echo "$0: debug[3]: unexpected output starts below" 1>&2
echo "$TOOL_OUTPUT" 1>&2
echo "$0: debug[3]: unexpected output ends above" 1>&2
fi
fi
exit 1
elif [[ "$V_FLAG" -ge 3 ]]; then
echo "$0: debug[3]: tool passed the trivial test: $TOOL" 1>&2
fi
;;
checknr)
# form a trivial man page with an error
#
export TMP_MAN_PAGE=".tmp.$NAME.MAN_PAGE.$$.1"
trap 'rm -f $TMP_MAN_PAGE; exit' 0 1 2 3 15
rm -f "$TMP_MAN_PAGE"
if [[ -e $TMP_MAN_PAGE ]]; then
echo "$0: ERROR: cannot remove temporary erroneous man page: $TMP_MAN_PAGE" 1>&2
exit 21
fi
cat > "$TMP_MAN_PAGE" << EOF
.TH foo 1 "11 July 2024" "foo" "foo"
.SH NAME
.B foo
\- foo bar baz
.SH SYNOPSIS
\\fB
EOF
if [[ ! -e $TMP_MAN_PAGE ]]; then
echo "$0: ERROR: cannot create temporary erroneous man page: $TMP_MAN_PAGE" 1>&2
exit 22
fi
# try checknr on the trivial (erroneous) man page
#
if [[ "$V_FLAG" -ge 3 ]]; then
echo "$0: debug[3]: about to run: $TOOL -- $TMP_MAN_PAGE" 1>&2
fi
TOOL_OUTPUT=$("$TOOL" -c.BR.SS.BI.IR.RB.RI -- "$TMP_MAN_PAGE" 2>&1)
status="$?"
export TOOL_OUTPUT
if [[ $status -eq 0 ]]; then
if [[ "$V_FLAG" -ge 1 ]]; then
echo "$0: debug[1]: tool failed the trivial test: $TOOL" 1>&2
fi
if [[ "$V_FLAG" -ge 3 ]]; then
echo "$0: debug[3]: $TOOL -c.BR.SS.BI.IR.RB.RI -- $TMP_MAN_PAGE failed," \
"exit code expected to be non-zero, is: $status" 1>&2
fi
exit 1
elif [[ "$V_FLAG" -ge 3 ]]; then
echo "$0: debug[3]: tool passed the trivial test: $TOOL exited $status" 1>&2
fi
;;
# case: not a special tool, testing as an executable file is good enough (we hope)
#
*)
if [[ "$V_FLAG" -ge 3 ]]; then
echo "$0: debug[3]: tool is not special, skipping sanity check" 1>&2
fi
;;
esac
# if -w, report where the OK tool is found to stdout
#
# If we got here, the tool must is OK.
#
if [[ -n $PRINT_WHERE ]]; then
echo "$TOOL"
fi
# All Done!!! All Done!!! -- Jessica Noll, Age 2
#
exit 0