-
Notifications
You must be signed in to change notification settings - Fork 3
/
jparse_bug_report.sh
executable file
·1784 lines (1589 loc) · 53.2 KB
/
jparse_bug_report.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
#
# jparse_bug_report.sh - produce a file suitable for filing a bug report
#
# Collect system information to help users report bugs and issues
# using the jparse tools and parser.
#
# When you run this script without any arguments:
#
# ./jparse_bug-report.sh
#
# After printing logs of stuff on the terminal, a file of the form:
#
# bug-report.YYYYMMDD.HHMMSS.txt
#
# will be created in the current directory, where YYYYMMDD.HHMMSS is the
# date and time in your default local time zone.
#
# If you are reporting a bug, the bug-report.YYYYMMDD.HHMMSS.txt file should
# be uploaded as part of your bug report. Report / create a GitHub issue
# by going to:
#
# https://github.com/xexyl/jparse/issues
#
# Please upload the bug-report.YYYYMMDD.HHMMSS.txt file as part of your report.
#
# This script was written in 2022 for the mkiocccentry repo
# (https://github.com/ioccc-src/mkiocccentry) by:
#
# @xexyl
# https://xexyl.net Cody Boone Ferguson
# https://ioccc.xexyl.net
#
# with some minor improvements by:
#
# chongo (Landon Curt Noll, http://www.isthe.com/chongo/index.html) /\oo/\
#
# and then adapted to the jparse repo in 2024.
#
# "Because sometimes even the IOCCC Judges need some help." :-)
#
# setup
#
# Maintain this list towards the top of file, in sorted order.
#
# Do NOT put this tool (jparse_bug_report.sh) in the list, it will
# cause an infinite loop.
#
export TOOLS="
jparse
jsemcgen.sh
jsemtblgen
jstrencode
jstrdecode
run_bison.sh
run_flex.sh
test_jparse/jnum_chk
test_jparse/jnum_gen
test_jparse/jparse_test.sh
test_jparse/jstr_test.sh
verge
"
# we need this to find overriding Makefile.local in all directories to see if
# the user is overriding any Makefile. As well, we check if the directory even
# is searchable and has a Makefile.
export SUBDIRS="
test_jparse
"
CC="$(type -P cc 2>/dev/null)"
export CC
# we need to determine if the system has gmake first
MAKE="$(type -P gmake)"
if [[ -z "$MAKE" ]]; then
# ...but if it does not, then we hope that make is GNU make, or will at
# least work.
MAKE="$(type -P make)"
fi
export MAKE
export MAKE_FLAGS="V=@ S=@ Q= E=@ I= Q_V_OPTION=1 INSTALL_V= MAKE_CD_Q="
export BUG_REPORT_VERSION="2.0.4 2024-12-31"
export FAILURE_SUMMARY=
export NOTICE_SUMMARY=
export DBG_LEVEL="0"
export V_FLAG="0"
export T_FLAG=""
export X_FLAG=""
export L_FLAG=""
export EXIT_CODE=0
export USAGE="usage: $0 [-h] [-V] [-v level] [-D level] [-t] [-x] [-l] [-L logfile] [-m make] [-M make_flags] [-c cc]
-h print help and exit
-V print version and exit
-v level set verbosity level for this script: (def level: $V_FLAG)
-D level set verbosity level for tests (def: $DBG_LEVEL)
-t disable make actions (def: run make actions)
-x remove bug report if no problems detected
-l only write to log file
-L logfile specify logfile name (def: based on date and time)
-m make specify path to make(1) (def: $MAKE)
-M make_flags set any make flags (def: $MAKE_FLAGS)
-c cc set path to cc (def: $CC)
Exit codes:
0 all tests OK
1 failed to create a bug report file
2 help mode exit or print version mode exit
3 invalid command line
4 error in function call
5 required file doesn't exist, wrong type or wrong permissions
>= 10 at least one check failed
jparse_bug_report.sh version: $BUG_REPORT_VERSION"
# Determine the name of the log file
#
# NOTE: log file does not have an underscore in the name because we want to
# distinguish it from this script which does have an underscore in it.
#
LOGFILE="bug-report.$(date +%Y%m%d.%H%M%S).txt"
# parse args
#
export V_FLAG="0"
while getopts :hVv:D:txlL:m:M:c: flag; do
case "$flag" in
h) echo "$USAGE" 1>&2
exit 2
;;
V) echo "$BUG_REPORT_VERSION" 1>&2
exit 2
;;
v) V_FLAG="$OPTARG";
;;
D) DBG_LEVEL="$OPTARG";
;;
t) T_FLAG="-t"
;;
x) X_FLAG="-x"
;;
l) L_FLAG="-l"
;;
L) LOGFILE="$OPTARG"
;;
m) MAKE="$OPTARG"
;;
M) MAKE_FLAGS="$OPTARG"
;;
c) CC="$OPTARG"
;;
\?) echo "$0: ERROR: invalid option: -$OPTARG" 1>&2
echo 1>&2
echo "$USAGE" 1>&2
exit 3
;;
# BTW: did you ever notice that for this type of command line error, an
# option requiring an arg but was not given an arg, makes a smiley face? :-)
:) echo "$0: ERROR: option -$OPTARG requires an argument" 1>&2
echo 1>&2
echo "$USAGE" 1>&2
exit 3
;;
*)
;;
esac
done
# make sure that make is an executable file
#
# First, if the user for some reason provided an empty string, try and correct
# it for them:
if [[ -z "$MAKE" ]]; then
# try for gmake first
MAKE="$(type -P gmake)"
if [[ -z "$MAKE" ]]; then
MAKE="$(type -P make)"
fi
fi
if [[ -z "$MAKE" ]]; then
echo "make variable MAKE unset! Try -m make option." 1>&2
exit 3
fi
if [[ ! -e "$MAKE" ]]; then
echo "make does not exist: $MAKE" 1>&2
echo "Use -m make option to set path." 1>&2
exit 3
fi
if [[ ! -f "$MAKE" ]]; then
echo "make is not a regular file: $MAKE" 1>&2
echo "Use -m make option to set path." 1>&2
exit 3
fi
if [[ ! -x "$MAKE" ]]; then
echo "make is not an executable file: $MAKE" 1>&2
echo "Use -m make option to set path." 1>&2
exit 3
fi
# make sure that cc is an executable file
#
# First, if the user for some reason provided an empty string, try and correct
# it for them:
if [[ -z "$CC" ]]; then
# try for gcc first
CC="$(type -P gcc)"
if [[ -z "$CC" ]]; then
CC="$(type -P cc)"
fi
fi
if [[ -z "$CC" ]]; then
echo "CC variable cc unset!" 1>&2
echo "Use -c cc option to set path." 1>&2
exit 3
fi
if [[ ! -e "$CC" ]]; then
echo "cc does not exist: $CC" 1>&2
echo "Use -c cc option to set path." 1>&2
exit 3
fi
if [[ ! -f "$CC" ]]; then
echo "cc is not a regular file: $CC" 1>&2
echo "Use -c cc option to set path." 1>&2
exit 3
fi
if [[ ! -x "$CC" ]]; then
echo "cc is not an executable file: $CC" 1>&2
echo "Use -c cc option to set path." 1>&2
exit 3
fi
# the LOGFILE name will have been either generated or specified by the -L option
# but now we have to export it and create it.
export LOGFILE
# attempt to create a writable log file
#
rm -f "$LOGFILE"
touch "$LOGFILE"
if [[ ! -e "$LOGFILE" ]]; then
echo "$0: ERROR: could not create log file: $LOGFILE"
exit 1
fi
if [[ ! -w "$LOGFILE" ]]; then
echo "$0: ERROR: log file not writable: $LOGFILE"
exit 1
fi
# write_echo - write a message to either the log file or both the log file and
# stdout
#
write_echo()
{
local MSG="$*"
if [[ -z "$L_FLAG" ]]; then
echo "$MSG" | tee -a -- "$LOGFILE"
else
echo "$MSG" >> "$LOGFILE"
fi
}
# exec_command - invoke command redirecting output only to the log file or to
# both stdout and the log file
exec_command()
{
local COMMAND=$*
if [[ -z "$L_FLAG" ]]; then
# SC2086 (info): Double quote to prevent globbing and word splitting.
# https://www.shellcheck.net/wiki/SC2086
# shellcheck disable=SC2086
command ${COMMAND} 2>&1 | tee -a -- "$LOGFILE"
return "${PIPESTATUS[0]}"
else
# SC2086 (info): Double quote to prevent globbing and word splitting.
# https://www.shellcheck.net/wiki/SC2086
# shellcheck disable=SC2086
command ${COMMAND} >> "$LOGFILE" 2>&1
return $?
fi
}
# exec_command_lines - invoke command redirecting output only to the log file or
# to both stdout and the log file but filter through head to show only N lines.
exec_command_lines()
{
local LINES="$1"
local COMMAND="${*:2}"
if [[ -z "$L_FLAG" ]]; then
# SC2086 (info): Double quote to prevent globbing and word splitting.
# https://www.shellcheck.net/wiki/SC2086
# shellcheck disable=SC2086
command ${COMMAND} 2>&1 | head -n "$LINES" | tee -a -- "$LOGFILE"
return "${PIPESTATUS[0]}"
else
# SC2086 (info): Double quote to prevent globbing and word splitting.
# https://www.shellcheck.net/wiki/SC2053
# shellcheck disable=SC2086
command ${COMMAND} | head -n "$LINES" >> "$LOGFILE" 2>&1
return "${PIPESTATUS[0]}"
fi
}
# is_exec - determine if arg exists, is a regular file and is executable
#
# NOTE: don't exit if the file is not an executable
is_exec()
{
if [[ $# -ne 1 ]]; then
echo "$0: ERROR: expected 1 arg to is_exec, found $#" | tee -a -- "$LOGFILE"
exit 4
else
declare f="$1"
if [[ ! -e "$f" ]]; then
write_echo "$0: ERROR: $1 does not exist"
return 1
fi
if [[ ! -f "$f" ]]; then
write_echo "$0: ERROR: $1 is not a regular file"
return 1
fi
if [[ ! -x "$f" ]]; then
write_echo "$0: ERROR: $1 is not executable"
return 1
fi
return 0
fi
}
# is_exec_quiet - determine if arg exists, is a regular file and is executable
#
# NOTE: don't exit if the file is not an executable
is_exec_quiet()
{
if [[ $# -ne 1 ]]; then
echo "$0: ERROR: expected 1 arg to is_exec_quiet, found $#" | tee -a -- "$LOGFILE"
exit 4
else
declare f="$1"
if [[ ! -e "$f" ]]; then
return 1
fi
if [[ ! -f "$f" ]]; then
return 1
fi
if [[ ! -x "$f" ]]; then
return 1
fi
return 0
fi
}
# is_read_exec_dir - determine if arg exists, is a directory and is searchable
#
# NOTE: don't exit if the directory is not searchable
is_read_exec_dir()
{
if [[ $# -ne 1 ]]; then
echo "$0: ERROR: expected 1 arg to is_read_exec_dir, found $#" | tee -a -- "$LOGFILE"
exit 4
else
declare f="$1"
if [[ ! -e "$f" ]]; then
write_echo "$0: ERROR: $1 does not exist"
return 1
fi
if [[ ! -d "$f" ]]; then
write_echo "$0: ERROR: $1 is not a directory"
return 1
fi
if [[ ! -r "$f" ]]; then
write_echo "$0: ERROR: $1 is not readable"
return 1
fi
if [[ ! -x "$f" ]]; then
write_echo "$0: ERROR: $1 is not searchable"
return 1
fi
return 0
fi
}
# type_of - determine if a name is an alias, a path or a built-in
#
# NOTE: an alias is highly unlikely to be found in a script but if it something
# is aliased by some chance we'll hopefully know that from this function.
type_of()
{
# parse args
#
if [[ $# -ne 2 ]]; then
echo "$0: ERROR: function expects 2 args, found $#" | tee -a -- "$LOGFILE"
exit 4
fi
local CODE="$1"
local COMMAND=$2
write_echo
write_echo "## CHECKING TYPE OF \"$COMMAND\""
TYPE_OF="$(type -a "$COMMAND" 2>/dev/null)"
status=$?
if [[ -n "$TYPE_OF" ]]; then
write_echo "$TYPE_OF"
elif [[ "$status" -ne 0 ]]; then
EXIT_CODE="$CODE"
write_echo "$0: ERROR: \"type -a $COMMAND\" FAILED WITH EXIT CODE $status: NEW EXIT_CODE: $EXIT_CODE"
FAILURE_SUMMARY="$FAILURE_SUMMARY
\"type -a $COMMAND\" non-zero exit code: $status"
write_echo "### ISSUE DETECTED: \"$COMMAND\" returned $status"
fi
write_echo "## TYPE OF \"$COMMAND\" ABOVE"
}
# type_of_optional - determine if a name is an alias, a path or a built-in
#
# NOTE: an alias is highly unlikely to be found in a script but if it something
# is aliased by some chance we'll hopefully know that from this function.
type_of_optional()
{
# parse args
#
if [[ $# -ne 1 ]]; then
echo "$0: ERROR: function expects 2 args, found $#" | tee -a -- "$LOGFILE"
exit 4
fi
local COMMAND=$1
write_echo
write_echo "## CHECKING TYPE OF \"$COMMAND\""
TYPE_OF="$(type -a "$COMMAND" 2>/dev/null)"
status=$?
if [[ -n "$TYPE_OF" ]]; then
write_echo "$TYPE_OF"
elif [[ "$status" -ne 0 ]]; then
write_echo "$0: OPTIONAL COMMAND $COMMAND NOT FOUND: \"type -a $COMMAND\" returned $status"
fi
write_echo "## TYPE OF \"$COMMAND\" ABOVE"
}
# test_compile - try compiling a test program with jparse/jparse.h
#
# NOTE: it is not considered an issue if the compilation fails, though if the
# appropriate temporary files cannot be created a warning is issued.
#
# NOTE: this function does not make use of the -I or -L options with cc.
test_compile()
{
write_echo "## TEST COMPILING WITH: \"#include <jparse/jparse.h>\""
# extra firewall check
#
if [[ -z "$CC" ]]; then
write_echo "$0: CC variable empty, skipping test compile"
return 0
fi
# set up for compile test
#
PROG_FILE=$(mktemp -u "jparse_bug_report.XXXXXXXXXX.prog")
status="$?"
if [[ $status -ne 0 ]]; then
write_echo "$0: WARNING: \"mktemp -u $PROG_FILE\" exit code: $status" 1>&2
write_echo "$0: WARNING: will skip test compile" 1>&2
return 0
fi
SOURCE_FILE=$(mktemp -u "jparse_bug_report.XXXXXXXXXX.c")
status="$?"
if [[ $status -ne 0 ]]; then
write_echo "$0: WARNING: \"mktemp -u $SOURCE_FILE\" exit code: $status" 1>&2
write_echo "$0: WARNING: will skip test compile" 1>&2
rm -f "$PROG_FILE"
return 0
fi
printf "#include <jparse/jparse.h>\nint main(void){return 0;}" > "$SOURCE_FILE"
exec_command "${CC}" "$SOURCE_FILE" -o "$PROG_FILE" -ljparse -ldbg -ldyn_array
status="$?"
if [[ $status -eq 0 ]]; then
write_echo "## TEST COMPILING WITH: \"#include <jparse/jparse.h>\" WORKS"
else
write_echo "## TEST COMPILING WITH: \"#include <jparse/jparse.h>\" DOES NOT WORK"
fi
# delete temporary files
rm -f "$PROG_FILE" "$SOURCE_FILE"
return 0
}
# get path to tools we might need for get_version and get_version_optional
# functions below
#
WHAT="$(type -P what)"
IDENT="$(type -P ident)"
STRINGS="$(type -P strings)" # this should always exist but we check anyway
# get_version_optional - try and get version of an optional tool
#
# A question is how do to determine the version of a tool when there's no
# universal option to get the version of _all_ tools (and in some tools we
# cannot detect the version as it will block instead or in the case of echo or
# yes just print the args).
#
# This is a good question and there's no guarantee we can obtain a version. In
# that case we will report it as an unknown version. Nevertheless we go through
# a series of steps as follows. First (step 0) we attempt to get the path to the
# command and then we run the following on the path (if it appears to be a
# built-in we cannot use the path of course).
#
# 1) use --version (note: under macOS and BSD this will fail on several
# tools and in some tools it will fail under linux as well)
# 2) use -V (note: this might fail due to multiple reasons)
# 3) use -v (note: this might fail due to multiple reasons)
# 4) try what(1) (note: this is a macOS and seems available under some BSDs
# as well - but despite the man page saying it conforms to to IEEE Std
# 1003.1-2001 ("POSIX.1") it is not available under linux)
# 5) try ident(1) (note: this appears to be a BSD command that's not
# available under macOS either)
# 6) try strings(1) with showing just the first 10 lines
#
# As soon as one of these returns a zero exit code (except for what(1) which is
# described in the function below) we will stop. If we get through all steps
# without any results we will mark it as an unknown version. The first step to
# succeed we will run a second time to record the output.
#
# Unfortunately this is far from perfect but we hope that it will help in a lot
# of cases. A note about what(1) and ident(1) is that we will only check for it
# once and if it does not exist we won't try it again. We also use the command
# command -p in hopes to get the actual path. This also might not be perfect.
#
# usage:
# get_version_optional command
#
# command - command to try and obtain the version
#
# NOTE: we don't want the path to the tool in this function as we try
# determining that instead.
#
get_version_optional()
{
# parse args
#
if [[ $# -ne 1 ]]; then
echo "$0: ERROR: function expects 1 arg, found $#" | tee -a -- "$LOGFILE"
exit 4
fi
local COMMAND
local EXIT=0
local IS_EXEC=1
COMMAND="$(type -P "$1")"
if ! is_exec_quiet "$COMMAND"; then
# if not executable we can try doing it as a built-in. This might or
# might not need to be a better check. Although some of the tools are
# built-ins in say zsh it does not appear to be in bash so it's possibly
# not a problem (since we use /usr/bin/env bash).
#
# Additionally trying to run command on a built-in in zsh (for example:
# true) will work because it's also a file. Also if it fails to run we
# will know there's a problem and likely due to something missing so the
# below might be all that's necessary.
COMMAND="$1"
IS_EXEC=0
fi
write_echo "## VERSION CHECK FOR: \"$COMMAND\""
# try --version
#
command "${COMMAND}" --version >/dev/null 2>&1 </dev/null
status=$?
if [[ "$status" -eq 0 ]]; then
exec_command "${COMMAND}" --version </dev/null
write_echo "## \"$COMMAND --version\" ABOVE"
write_echo ""
return
fi
# try -v
#
command "${COMMAND}" -v >/dev/null 2>&1 </dev/null
status=$?
if [[ "$status" -eq 0 ]]; then
exec_command "${COMMAND}" -v </dev/null
write_echo "## \"$COMMAND -v\" ABOVE"
write_echo ""
fi
# try -V too as some tools use -V
#
command "${COMMAND}" -V >/dev/null 2>&1 </dev/null
status=$?
if [[ "$status" -eq 0 ]]; then
exec_command "${COMMAND}" -V </dev/null
write_echo "## \"$COMMAND -V\" ABOVE"
write_echo ""
return
fi
if [[ "$IS_EXEC" -eq 1 ]]; then
# try what(1) if available
#
# An important note is that what(1) might not get the correct
# information. Now what(1) on Earth do I mean?
#
# Well, for instance, running it on bmake(1):
#
# $ what ./bmake
# ./bmake:
# Copyright (c) 1988, 1989, 1990, 1993 The Regents of the University of California. All rights reserved.
#
# which(1) is entirely useless.
#
# The question is should ident(1) come first but the trouble is I don't
# actually know what it looks like. Also if the tool in question does not
# have the appropriate string it won't give us anything useful either.
# Here's what what(1) looks like on cut(1) which as can be seen is useful:
#
# $ what /usr/bin/cut
# /usr/bin/cut:
# PROGRAM:cut PROJECT:text_cmds-154
# PROGRAM:cut PROJECT:text_cmds-154
#
# Looking at the Apple website this is indeed the version (or it was
# when this was originally written). Thus because it's not something
# that will work in all cases instead we will try ident(1) as well even
# if what(1) succeeds. If either succeeds we will not try strings(1).
#
if [[ -n "$WHAT" ]]; then
$WHAT "${COMMAND}" >/dev/null 2>&1 </dev/null
status=$?
if [[ "$status" -eq 0 ]]; then
exec_command "$WHAT" "${COMMAND}" </dev/null
write_echo "## OUTPUT OF \"what $COMMAND\" ABOVE"
write_echo ""
EXIT=1
fi
fi
# try ident(1) if available
#
# The same or similar caveats for what(1) might apply here too but I have no
# way to test this.
#
if [[ -n "$IDENT" ]]; then
$IDENT "${COMMAND}" >/dev/null 2>&1 </dev/null
status=$?
if [[ "$status" -eq 0 ]]; then
exec_command "$IDENT" "${COMMAND}" </dev/null
write_echo "## OUTPUT OF \"ident $COMMAND\" ABOVE"
write_echo ""
EXIT=1
fi
fi
# if we got output from either what(1) or ident(1) then skip strings(1)
#
if [[ -n "$EXIT" ]]; then
return
fi
# try strings(1) if available. The filter is arbitrarily selected. For some
# tools it might not be enough lines but if we get here it's probably not
# going to be what we want anyway.
#
# Now a question to be answered is should we even use strings? The reason to
# ask such a question is it's likely to work which means that we might never
# reach the unknown version and strings(1) probably won't actually give us
# the version.
#
# This is why we warn that there's a possible unknown version and only if
# strings fails do we report positively that the version is unknown.
#
if [[ -n "$STRINGS" ]]; then
write_echo "$0: unknown version for \"$COMMAND\": trying strings"
NOTICE_SUMMARY="$NOTICE_SUMMARY
Notice: unknown version for \"$COMMAND\": trying \"strings $COMMAND\""
$STRINGS "${COMMAND}" | head -n 15 >/dev/null 2>&1
status=${PIPESTATUS[0]}
if [[ "$status" -eq 0 ]]; then
exec_command_lines 15 "$STRINGS" "${COMMAND}"
write_echo "## OUTPUT OF \"strings $COMMAND\" ABOVE"
write_echo ""
return
fi
fi
# report unknown version
#
write_echo "Notice: unknown version for optional command: \"$COMMAND\""
NOTICE_SUMMARY="$NOTICE_SUMMARY
$0: Notice: unknown version for optional command: \"$COMMAND\""
write_echo ""
# mention that an optional command is not found or not executable
#
else
write_echo "Optional command is not found or not executable: \"$COMMAND\""
write_echo ""
fi
return 0;
}
# get_version - try and get version of a tool
#
# usage:
# get_version command
#
# command - command to try and obtain the version
#
# NOTE: we don't want the path to the tool in this function as we try
# determining that instead.
#
get_version()
{
# parse args
#
if [[ $# -ne 1 ]]; then
echo "$0: ERROR: function expects 1 arg, found $#" | tee -a -- "$LOGFILE"
exit 4
fi
local COMMAND
local EXIT=0
local IS_EXEC=1
COMMAND="$(type -P "$1")"
if ! is_exec_quiet "$COMMAND"; then
# if not executable we can try doing it as a built-in. This might or
# might not need to be a better check. Although some of the tools are
# built-ins in say zsh it does not appear to be in bash so it's possibly
# not a problem (since we use /usr/bin/env bash).
#
# Additionally trying to run command on a built-in in zsh (for example:
# true) will work because it's also a file. Also if it fails to run we
# will know there's a problem and likely due to something missing so the
# below might be all that's necessary.
COMMAND="$1"
IS_EXEC=0
fi
write_echo "## VERSION CHECK FOR: \"$COMMAND\""
# First check if this is bash. If it is we can do something special for
# version information.
if [[ "$(basename "${COMMAND}")" = "bash" ]]; then
write_echo "## RUNNING: \"echo \$BASH_VERSION\""
exec_command "echo $BASH_VERSION"
write_echo "## \"\$BASH_VERSION\" ABOVE"
# get bash MAJOR version
write_echo "## RUNNING: \"echo \${BASH_VERSINFO[0]}\""
exec_command "echo ${BASH_VERSINFO[0]}"
write_echo "## BASH \"\${BASH_VERSINFO[0]}\" (MAJOR VERSION) ABOVE"
# get bash MINOR version
write_echo "## RUNNING: \"echo \${BASH_VERSINFO[1]}\""
exec_command "echo ${BASH_VERSINFO[1]}"
write_echo "## BASH MINOR \"\${BASH_VERSINFO[1]}\" (MINOR VERSION) ABOVE"
# get bash PATCH LEVEL
write_echo "## RUNNING: \"echo \${BASH_VERSINFO[2]}\""
exec_command "echo ${BASH_VERSINFO[2]}"
write_echo "## BASH \"\${BASH_VERSINFO[2]}\" (PATCH LEVEL) ABOVE"
# get bash BUILD version
write_echo "## RUNNING: \"echo \${BASH_VERSINFO[3]}\""
exec_command "echo ${BASH_VERSINFO[3]}"
write_echo "## BASH \"\${BASH_VERSINFO[3]}\" (BUILD VERSION) ABOVE"
# get bash RELEASE STATUS (e.g. beta)
write_echo "## RUNNING: \"echo \${BASH_VERSINFO[4]}\""
exec_command "echo ${BASH_VERSINFO[4]}"
write_echo "## BASH \"\${BASH_VERSINFO[4]}\" (RELEASE) STATUS ABOVE"
# get MACHTYPE
#
# We use $MACHTYPE instead of ${BASH_VERSINFO[5]}. Why? Just in case
# some versions of bash do not have it and since it's meant to be the
# same value the name here can serve as additional documentation. Of
# course we could get that elsewhere but this is a bash variable so it
# seems fitting that it is put here.
write_echo "## RUNNING: \"echo \$MACHTYPE\""
exec_command "echo $MACHTYPE"
write_echo "## \"\$MACHTYPE\" (BASH SYSTEM TYPE) ABOVE"
# Don't return from this function even after all this because trying
# --version on bash might prove useful in some way even though we should
# have got the information above.
fi
# try --version
#
command "${COMMAND}" --version >/dev/null 2>&1 </dev/null
status=$?
if [[ "$status" -eq 0 ]]; then
exec_command "${COMMAND}" --version </dev/null
write_echo "## OUTPUT OF \"$COMMAND --version\" ABOVE"
write_echo ""
return
fi
# try -v
#
command "${COMMAND}" -v >/dev/null 2>&1 </dev/null
status=$?
if [[ "$status" -eq 0 ]]; then
exec_command "${COMMAND}" -v </dev/null
write_echo "## OUTPUT OF \"$COMMAND -v\" ABOVE"
write_echo ""
fi
# try -V too, as some commands use -V instead
#
command "${COMMAND}" -V >/dev/null 2>&1 </dev/null
status=$?
if [[ "$status" -eq 0 ]]; then
exec_command "${COMMAND}" -V </dev/null
write_echo "## OUTPUT OF \"$COMMAND -V\" ABOVE"
write_echo ""
return
fi
if [[ "$IS_EXEC" -eq 1 ]]; then
# try what(1) if available
#
# An important note is that what(1) might not get the correct
# information. Now what(1) on Earth do I mean?
#
# Well, for instance, running it on bmake(1):
#
# $ what ./bmake
# ./bmake:
# Copyright (c) 1988, 1989, 1990, 1993 The Regents of the University of California. All rights reserved.
#
# which(1) is entirely useless.
#
# The question is should ident(1) come first but the trouble is I don't
# actually know what it looks like. Also if the tool in question does not
# have the appropriate string it won't give us anything useful either.
# Here's what what looks like on cut(1) which as can be seen is useful:
#
# $ what /usr/bin/cut
# /usr/bin/cut:
# PROGRAM:cut PROJECT:text_cmds-154
# PROGRAM:cut PROJECT:text_cmds-154
#
# Looking at the Apple website this is indeed the version (or it was
# when this was originally written). Thus because it's not something
# that will work in all cases instead we will try ident(1) as well even
# if this succeeds. If either succeeds we will not try strings(1).
#
if [[ -n "$WHAT" ]]; then
$WHAT "${COMMAND}" >/dev/null 2>&1 </dev/null
status=$?
if [[ "$status" -eq 0 ]]; then
exec_command "$WHAT" "${COMMAND}" </dev/null
write_echo "## OUTPUT OF \"what $COMMAND\" ABOVE"
write_echo ""
EXIT=1
fi
fi
# try ident(1) if available
#
# The same or similar caveats for what(1) might apply here too but I have no
# way to test this.
#
if [[ -n "$IDENT" ]]; then
$IDENT "${COMMAND}" >/dev/null 2>&1 </dev/null
status=$?
if [[ "$status" -eq 0 ]]; then
exec_command "$IDENT" "${COMMAND}" </dev/null
write_echo "## OUTPUT OF \"ident $COMMAND\" ABOVE"
write_echo ""
EXIT=1
fi
fi
# if we got output from either what(1) or ident(1) then skip strings(1)
#
if [[ -n "$EXIT" ]]; then
return
fi
# try strings(1) if available. The filter is arbitrarily selected. For some
# tools it might not be enough lines but if we get here it's probably not
# going to be what we want anyway.
#
# Now a question to be answered is should we even use strings? The reason to
# ask such a question is it's likely to work which means that we might never
# reach the unknown version and strings(1) probably won't actually give us
# the version.
#
# This is why we warn that there's a possible unknown version and only if
# strings fails do we report positively that the version is unknown.
#
if [[ -n "$STRINGS" ]]; then
write_echo "Notice: unknown version for \"$COMMAND\": trying strings"
NOTICE_SUMMARY="$NOTICE_SUMMARY
Notice: unknown version for \"$COMMAND\": trying strings"
$STRINGS "${COMMAND}" | head -n 15 >/dev/null 2>&1
status=${PIPESTATUS[0]}
if [[ "$status" -eq 0 ]]; then
exec_command_lines 15 "$STRINGS" "${COMMAND}"
write_echo "## OUTPUT OF \"strings ${COMMAND}\" ABOVE"
write_echo ""
return
fi
fi
# report unknown version
#
write_echo "Notice: unknown version for required command: \"$COMMAND\""
NOTICE_SUMMARY="$NOTICE_SUMMARY
$0: Notice: unknown version for required command: \"${COMMAND}\""
write_echo ""
# note that a required command is not found or not executable
#
else
write_echo "Notice: required command is not found or is not executable: \"${COMMAND}\""
NOTICE_SUMMARY="$NOTICE_SUMMARY
Notice: required command is not found or is not executable: \"${COMMAND}\""
write_echo ""
fi
return 0;
}
# get_version_minimal - try and get version of a tool by a limited number of ways
#
# usage:
# get_version_minimal command
#
# command - command to try and obtain the version
#
# NOTE: we don't want the path to the tool in this function as we try
# determining that instead.
#
# NOTE: don't warn if we cannot get the version and don't use strings(1) either.
#
get_version_minimal()
{
# parse args
#
if [[ $# -ne 1 ]]; then
echo "$0: ERROR: function expects 1 arg, found $#" | tee -a -- "$LOGFILE"
exit 4
fi
local COMMAND
local EXIT=0
local IS_EXEC=1
COMMAND="$(type -P "$1")"
if ! is_exec_quiet "$COMMAND"; then
# if not executable we can try doing it as a built-in. This might or
# might not need to be a better check. Although some of the tools are
# built-ins in say zsh it does not appear to be in bash so it's possibly
# not a problem (since we use /usr/bin/env bash).
#
# Additionally trying to run command on a built-in in zsh (for example:
# true) will work because it's also a file. Also if it fails to run we
# will know there's a problem and likely due to something missing so the
# below might be all that's necessary.
COMMAND="$1"
IS_EXEC=0
fi
write_echo "## VERSION CHECK FOR: \"$COMMAND\""
# try --version
#
command "${COMMAND}" --version >/dev/null 2>&1 </dev/null
status=$?
if [[ "$status" -eq 0 ]]; then
exec_command "${COMMAND}" --version </dev/null
write_echo "## OUTPUT OF \"$COMMAND --version\" ABOVE"
write_echo ""
return
fi
if [[ "$IS_EXEC" -eq 1 ]]; then
# try what(1) if available
#
# An important note is that what(1) might not get the correct
# information. Now what(1) on Earth do I mean?
#
# Well, for instance, running it on bmake(1):
#
# $ what ./bmake
# ./bmake:
# Copyright (c) 1988, 1989, 1990, 1993 The Regents of the University of California. All rights reserved.
#
# which(1) is entirely useless.
#
# The question is should ident(1) come first but the trouble is I don't
# actually know what it looks like. Also if the tool in question does not
# have the appropriate string it won't give us anything useful either.
# Here's what what looks like on cut(1) which as can be seen is useful:
#
# $ what /usr/bin/cut
# /usr/bin/cut: