]>
Commit | Line | Data |
---|---|---|
a3c29314 RM |
1 | #!/bin/bash |
2 | ||
3 | ## This script is potentially dangerous! Don't run it on | |
4 | ## arbitrary commands. | |
5 | ||
6 | export LC_ALL=C | |
7 | ||
8 | if [ "$#" -lt 1 ]; then | |
9 | echo "usage: $0 program..." >&2 | |
10 | echo " or try 'make checkusage' to check all built programs" >&2 | |
11 | exit 1 | |
12 | fi | |
13 | ||
14 | builddir="." | |
15 | cmds=$(echo $@ | tr ' ' '\n' | sort) | |
16 | ||
99267518 RM |
17 | # set env to dump all output to files |
18 | test -z "$CU_DUMP" || rm -f /tmp/checkusage--{help,version,unknownopt} | |
19 | ||
a3c29314 RM |
20 | ## Set alternative options for --help, --version |
21 | ## or --unknownopt. "x" means "not implemented". | |
22 | ## | |
23 | ## Examples: | |
24 | ## alt_whereis__help="-h" # in past whereis(1) had no longopt for --help | |
25 | ## alt_more__help="x" # more(1) had neither --help nor -h | |
26 | ||
e8e9267f RM |
27 | alt_fsck__unknownopt="x" # fsck passes unknown opts to fsck.ext4, etc. |
28 | alt_mkfs__unknownopt="x" # dito | |
29 | alt_kill__unknownopt="inval pids" # trick, kill does not use errtryhelp() | |
30 | if [ $(id -ru) -eq 0 ]; then | |
31 | alt_sulogin__unknownopt="x" # would hang at pwd prompt | |
32 | fi | |
a3c29314 RM |
33 | |
34 | function exec_option { | |
35 | local cmdb=$1 | |
36 | local cmd=$2 | |
37 | opt=$3 | |
99267518 | 38 | local tofile="/tmp/checkusage$opt" |
a3c29314 RM |
39 | |
40 | local alt="alt_${cmdb}${opt}" | |
41 | alt=${alt//-/_} | |
42 | alt=${alt//./_} | |
43 | alt=$(eval printf -- \"\$${alt}\") | |
44 | if test -n "$alt"; then | |
45 | if test "$alt" = "x"; then | |
46 | return 1 | |
47 | fi | |
48 | opt=$alt | |
49 | fi | |
50 | ||
99267518 RM |
51 | test -z "$CU_DUMP" || { |
52 | echo "##########################################################" | |
53 | echo "#### $cmdb" | |
54 | $cmd $opt | |
55 | } >> "$tofile" 2>&1 | |
56 | ||
e8e9267f RM |
57 | out=$("$cmd" $opt 2>/dev/null) |
58 | err=$("$cmd" $opt 2>&1 >/dev/null) | |
a3c29314 RM |
59 | ret=$? |
60 | ||
61 | # hardcoded ... nologin should always return false | |
62 | if test "$cmdb" = "nologin" && | |
63 | test "$opt" = "--help" -o "$opt" = "--version"; then | |
66b74699 RM |
64 | if test "$ret" -eq 0 -o "$ret" -ge 128; then |
65 | echo "$cmdb, $opt, should return false: $ret" | |
a3c29314 RM |
66 | fi |
67 | ret=0 | |
68 | fi | |
69 | ||
70 | return 0 | |
71 | } | |
72 | ||
73 | ||
74 | function check_help { | |
75 | local cb=$1 | |
76 | local c=$2 | |
77 | ||
78 | if ! exec_option "$cb" "$c" --help; then | |
79 | return 1 | |
80 | fi | |
81 | ||
82 | if test $ret != 0; then | |
83 | echo "$cb: $opt, returns error" | |
84 | else | |
85 | if test -z "$out"; then | |
86 | echo "$cb: $opt, no stdout" | |
87 | fi | |
88 | if test -n "$err"; then | |
89 | echo "$cb: $opt, non-empty stderr" | |
90 | fi | |
91 | fi | |
92 | return 0 | |
93 | } | |
94 | ||
95 | function check_version { | |
96 | local cb=$1 | |
97 | local c=$2 | |
98 | ||
99 | if ! exec_option "$cb" "$c" --version; then | |
100 | return 1 | |
101 | fi | |
102 | ||
103 | if test $ret != 0; then | |
104 | echo "$cb: $opt, returns error" | |
105 | else | |
106 | if test -z "$out"; then | |
107 | echo "$cb: $opt, no stdout" | |
108 | fi | |
109 | if test -n "$err"; then | |
110 | echo "$cb: $opt, non-empty stderr" | |
111 | fi | |
112 | fi | |
113 | } | |
114 | ||
115 | function check_unknownopt { | |
116 | local cb=$1 | |
117 | local c=$2 | |
118 | local nohelp=$3 | |
119 | ||
120 | if ! exec_option "$cb" "$c" --unknownopt; then | |
121 | return 1 | |
122 | fi | |
123 | ||
124 | if test $ret = 0; then | |
125 | echo "$cb: $opt, returns no error" | |
66b74699 RM |
126 | elif test $ret -ge 128; then |
127 | echo "$cb: $opt, abnormal exit: $ret" | |
a3c29314 RM |
128 | fi |
129 | if test -n "$out"; then | |
130 | echo "$cb: $opt, non-empty stdout" | |
131 | fi | |
132 | if test -z "$err"; then | |
133 | echo "$cb: $opt, no stderr" | |
134 | elif test -z "$nohelp" -o "$nohelp" != "yes"; then | |
135 | out_len=$(echo "$out" | wc -l) | |
136 | err_len=$(echo "$err" | wc -l) | |
137 | if test "$err_len" -gt 2; then | |
e8e9267f | 138 | echo "$cb: $opt, stderr too long: $err_len" |
a3c29314 RM |
139 | elif test "$err_len" -lt 2; then |
140 | echo "$cb: $opt, stderr too short: $err_len" | |
141 | fi | |
142 | fi | |
143 | } | |
144 | ||
145 | for cb in $cmds; do | |
146 | c="$builddir/$cb" | |
147 | if ! type "$c" &>/dev/null; then | |
148 | echo "$cb: does not exist" | |
149 | continue | |
150 | fi | |
151 | ||
152 | nohelp="no" | |
153 | if ! check_help "$cb" "$c"; then | |
154 | nohelp="yes" | |
155 | fi | |
156 | check_version "$cb" "$c" | |
157 | check_unknownopt "$cb" "$c" "$nohelp" | |
158 | done | |
159 |