Imported Upstream version 2.4.2
[platform/upstream/libtool.git] / tests / sh.test
1 #! /bin/sh
2 # sh.test - check for some nonportable or dubious or undesired shell
3 #           constructs in shell scripts.
4 #
5 #   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2010 Free
6 #   Software Foundation, Inc.
7 #   Written by Gary V. Vaughan, 2003
8 #
9 #   This file is part of GNU Libtool.
10 #
11 # GNU Libtool is free software; you can redistribute it and/or
12 # modify it under the terms of the GNU General Public License as
13 # published by the Free Software Foundation; either version 2 of
14 # the License, or (at your option) any later version.
15 #
16 # GNU Libtool is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with GNU Libtool; see the file COPYING.  If not, a copy
23 # can be downloaded from  http://www.gnu.org/licenses/gpl.html,
24 # or obtained by writing to the Free Software Foundation, Inc.,
25 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 ####
27
28 . tests/defs || exit 1
29
30 # Check all the "portable" shell scripts.
31 status=$EXIT_SUCCESS
32
33 # Check for bad binary operators.
34 if $EGREP -n -e 'if[    ]+["'\'']?\$[^  ]+[     ]+(=|-[lg][te]|-eq|-ne)' $scripts; then
35   echo "use \`if test \$something =' instead of \`if \$something ='"
36   status=$EXIT_FAILURE
37 fi
38
39 # Check for bad unary operators.
40 if $EGREP -n -e 'if[    ]+-' $scripts; then
41   echo "use \`if test -X' instead of \`if -X'"
42   status=$EXIT_FAILURE
43 fi
44
45 # Check for using `[' instead of `test'.
46 if $EGREP -n -e 'if[    ]+\[' $scripts; then
47   echo "use \`if test' instead of \`if ['"
48   status=$EXIT_FAILURE
49 fi
50
51 # Check for using test X... instead of test "X...
52 if $EGREP -n -e 'test[  ]+(![   ])?(-.[         ]+)?X' $scripts; then
53   echo "use \`test \"X...\"' instead of \`test X'"
54   status=$EXIT_FAILURE
55 fi
56
57 # Check for using test $... instead of test "$...
58 if $EGREP -n -e 'test[  ]+(![   ])?(-.[         ]+)?X?\$' $scripts; then
59   echo "use \`test \"\$...\"' instead of \`test \$'"
60   status=$EXIT_FAILURE
61 fi
62
63 # Never use test -e.
64 if $EGREP -n -e 'test[  ]+(![   ])?-e' $scripts; then
65   echo "use \`test -f' instead of \`test -e'"
66   status=$EXIT_FAILURE
67 fi
68
69 # Check for uses of Xsed without corresponding echo "X
70 if $EGREP -n -e '\$Xsed' $scripts | $EGREP -v -n -e '\$ECHO \\*"X'; then
71   echo "occurrences of \`\$Xsed\' without \`echo \"X\' on the same line"
72   status=$EXIT_FAILURE
73 fi
74
75 # Check for quotes within backquotes within quotes "`"bar"`"
76 if $EGREP -n -e '"[^`"]*`[^"`]*"[^"`]*".*`[^`"]*"' $scripts | \
77    $EGREP -v "### testsuite: skip nested quoting test$"; then
78   echo "nested quotes are dangerous"
79   status=$EXIT_FAILURE
80 fi
81
82 # Check for using set -- instead of set dummy
83 if $EGREP -n -e 'set[   ]+--[   ]+' $scripts; then
84   echo "use \`set dummy ...' instead of \`set -- ...'"
85   status=$EXIT_FAILURE
86 fi
87
88 # Check for using shift after set dummy (same or following line).
89 for s in $scripts
90 do
91   if $SED -n '
92       /set[     ][      ]*dummy/{
93           /set.*dummy.*;.*shift/d
94           N
95           /\n.*shift/D
96           p
97       }' "$s" | $EGREP .; then
98     echo "use \`shift' after \`set dummy' in $s"
99     status=$EXIT_FAILURE
100   fi
101 done
102
103 # Check for opening brace on next line in shell function definition.
104 # redirect stderr so we also barf when sed issues diagnostics.
105 for s in $scripts
106 do
107   if $SED -n '
108       /^func_.*(/{
109           N
110           /^func_[^     ]* ()\n{$/d
111           p
112       }' "$s" 2>&1 | $EGREP .; then
113     echo "Function definitions should look like this in $s:
114 func_foo ()
115 {
116   # ...
117 }"
118     status=$EXIT_FAILURE
119   fi
120 done
121
122 # Check for correct usage of $cc_basename.
123 # redirect stderr so we also barf when sed issues diagnostics.
124 for s in "$m4dir/libtool.m4"
125 do
126   if $SED -n '/case \$cc_basename in/,/esac/ {
127               /^[       ]*[a-zA-Z][a-zA-Z0-9+]*[^*][    ]*)/p
128               }'  $s 2>&1 | $EGREP .; then
129     echo "\$cc_basename matches should include a trailing \`*' in $s."
130     status=$EXIT_FAILURE
131   fi
132 done
133
134 exit $status