-
Notifications
You must be signed in to change notification settings - Fork 0
/
tableize
executable file
·149 lines (131 loc) · 5.81 KB
/
tableize
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
#!/usr/bin/env dash
# TODO: keep track of which scripts require GNU versions of utilities,
# as opposed of plain ol' busybox. test all the awk scripts, etc.
tableize() {
local name="$1"
local rc="$2"
[ -n "$rc" ] || rc="$(readlink -f "$(dirname "$name")" )"
[ -d "$rc" ] || { printf "%s: %s\n" "$0" 'failed to determine rc directory' >&2; }
cd "$rc" || { printf "%s: %s\n" "$0" 'failed to change directory' >&2; }
# the script column has to be stupid wide because names are repeated twice for anchors.
# column lengths: 45, 10, 4, 4, 4, 4
printf '%s\n' '| script | preference | ash | bash | dash | ksh | mksh | oksh | osh | posh | yash | zsh |'
printf '%s\n' '| --------------------------------------------- | ---------- | --- | ---- | ---- | --- | ---- | ---- | --- | ---- | ---- | --- |'
local x_ash=0 x_bash=0 x_dash=0 x_ksh=0 x_mksh=0 x_oksh=0 x_osh=0 x_posh=0 x_yash=0 x_zsh=0
local f=
for f in sh/*; do
[ ! -d "$f" ] || continue # do not recurse or anything
[ -f "$f" ] || continue # probably failed to glob
local fn="${f##*/}"
# ignore some stuff:
[ "$fn" = "${fn#_}" ] || continue # completion files
[ "$fn" = "${fn%.bak}" ] || continue # backup files
local i=0 ash= bash= dash= ksh= mksh= oksh= osh= posh= yash= zsh= pref=
while IFS= read -r line; do
: $((i+=1))
[ $i -le 10 ] || break # act like head -n10
#printf '%s\n' "$i: $line"
local code="$line"
code="${code#${code%%[! ]*}}" # ltrim
local decom="$code"
#decom="${decom#${decom%%[!#]*}}" # ltrim #s
decom="${decom#\#}"
[ "$code" != "$decom" ] || continue # only care about comments
decom="${decom#${decom%%[! ]*}}" # ltrim
decom="${decom%${decom##*[! ]}}" # rtrim
_pref=
case "$decom" in
('!/bin/sh') _pref=sh;;
('!/usr/bin/env ash') _pref=ash;;
('!/usr/bin/env bash') _pref=bash;;
('!/usr/bin/env dash') _pref=dash;;
('!/usr/bin/env false') _pref=false;;
('!/usr/bin/env perl') _pref=perl;;
('!/usr/bin/env sh') _pref=sh;;
('!/usr/bin/env zsh') _pref=zsh;;
('!/usr/bin/sh') _pref=sh;;
#*) printf '\033[1mno match:\033[0m [%s]\n' "$decom";;
esac
if [ -n "$_pref" ]; then
pref="$_pref"
continue
fi
check=0
set -f
IFS=' '
for flag in $decom; do case "$flag" in
(YES_ZSH) zsh=yes;;
(YES_BASH) bash=yes;;
(YES_DASH) dash=yes;;
(YES_ASH) ash=yes;;
(FAKE_COMPAT)
ash=yes bash=yes dash=yes
ksh=yes mksh=yes oksh=yes
osh=yes posh=yes yash=yes
zsh=yes;;
(NO_ZSH) zsh=no;;
(NO_BASH) bash=no;;
(NO_DASH) dash=no;;
(NO_ASH) ash=no;;
(compat:) check=1;;
(+ash) [ "$check" = 0 ] || ash=yes;;
(+bash) [ "$check" = 0 ] || bash=yes;;
(+dash) [ "$check" = 0 ] || dash=yes;;
(+ksh) [ "$check" = 0 ] || ksh=yes;;
(+mksh) [ "$check" = 0 ] || mksh=yes;;
(+oksh) [ "$check" = 0 ] || oksh=yes;;
(+osh) [ "$check" = 0 ] || osh=yes;;
(+posh) [ "$check" = 0 ] || posh=yes;;
(+yash) [ "$check" = 0 ] || yash=yes;;
(+zsh) [ "$check" = 0 ] || zsh=yes;;
(-ash) [ "$check" = 0 ] || ash=no;;
(-bash) [ "$check" = 0 ] || bash=no;;
(-dash) [ "$check" = 0 ] || dash=no;;
(-ksh) [ "$check" = 0 ] || ksh=no;;
(-mksh) [ "$check" = 0 ] || mksh=no;;
(-oksh) [ "$check" = 0 ] || oksh=no;;
(-osh) [ "$check" = 0 ] || osh=no;;
(-posh) [ "$check" = 0 ] || posh=no;;
(-yash) [ "$check" = 0 ] || yash=no;;
(-zsh) [ "$check" = 0 ] || zsh=no;;
('#'*) break;;
esac done
done < "$f"
#printf '%s' "script $fn has a preference for $pref and support for" >&2
#[ "$zsh" != yes ] || printf ' %s' zsh >&2
#[ "$bash" != yes ] || printf ' %s' bash >&2
#[ "$dash" != yes ] || printf ' %s' dash >&2
#[ "$ash" != yes ] || printf ' %s' ash >&2
#printf '\n' >&2
local yay='✔️' # yay='y'
local nay='⭕' # nay='✖️' # nay='n'
local huh='❔' # huh='?'
local etc='*n/a*'
# TODO: escape underscores in filenames.
printf '| [%s](#%s) ' "$fn" "$fn"
printf "%$(( 40 - 2 * ${#fn} ))s" '' # funky way to pad the rest of the column
local p="$pref"
[ "$pref" != false ] || p='**false**'
[ "$pref" != perl ] || p='**perl**'
[ "$pref" != sh ] || p='*sh*'
[ -n "$p" ] && printf '| %10s ' "$p" || printf '| %9s%s ' '' "$huh"
local n=
for n in ash bash dash ksh mksh oksh osh posh yash zsh; do
eval "local t=\$$n"
[ "$t" != no ] || eval ": \$((x_$n+=1))"
local w="$huh"
[ "$pref" != perl ] || w="$etc"
[ "$t" != yes ] || w="$yay"
[ "$t" != no ] || w="$nay"
# can't use '%4s' here because printf gets confused by UTF-8.
printf '| %s ' "$w"
done
printf '|\n'
done
local n=
for n in ash bash dash ksh mksh oksh osh posh yash zsh; do
eval "local x=\$x_$n"
printf >&2 'incompatibilities for %4s: %2s\n' "$n" "$x"
done
}
tableize "$0" "$@"