Imported Upstream version 1.4.17
[platform/upstream/m4.git] / checks / check-them
1 #!/bin/sh
2 # Check GNU m4 against examples from the manual source.
3 # Copyright (C) 1992, 2006-2013 Free Software Foundation, Inc.
4 #
5 # This file is part of GNU M4.
6 #
7 # GNU M4 is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # GNU M4 is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 # Clean up temp files on exit
21 pwd=`pwd`
22 tmp=m4-tmp.$$
23 trap 'stat=$?; cd "$pwd"; rm -rf $tmp && exit $stat' 0
24 trap '(exit $?); exit $?' 1 2 13 15
25
26 # Create scratch dir
27 framework_failure=0
28 mkdir $tmp || framework_failure=1
29
30 if test $framework_failure = 1; then
31   echo "$0: failure in testing framework" 1>&2
32   (exit 1); exit 1
33 fi
34
35 out=$tmp/m4-out
36 err=$tmp/m4-err
37 xout=$tmp/m4-xout
38 xerr=$tmp/m4-xerr
39 failed=
40 skipped=
41 strip_needed=false
42 diffopts=-c
43
44 # Allow user to select sed
45 : ${SED=sed}
46
47 # Find out where the examples live.
48 examples=.
49 if test "x$1" = x-I ; then
50   examples="$2"
51   shift; shift
52 fi
53
54 # Find out how to run m4.
55 m4=m4
56 if test "x$1" = x-m ; then
57   m4="$2"
58   shift; shift
59 fi
60
61 # Find out how the executable prints argv[0]
62 m4name=`"$m4" --help | ${SED} -e 's/Usage: \(.*\) \[OPTION.*/\1/' \
63   -e 's/\\\\/\\\\\\\\/g' -e 1q`
64
65 # Find out if we should strip \r in the output
66 "$m4" --version | tee $out
67 "$m4" --version | tr -d '\015' > $xout
68 if cmp -s $out $xout; then
69   :
70 else
71   echo "Ignoring carriage returns"
72   strip_needed=:
73 fi
74
75 # Find out if diff supports useful options.
76 if diff -u /dev/null /dev/null 2>/dev/null ; then
77   diffopts="-u"
78 fi
79 if diff -a /dev/null /dev/null 2>/dev/null ; then
80   diffopts="$diffopts -a"
81 fi
82
83 # Run the tests.
84 for file
85 do
86   test -f "$file" || {
87     echo "No such file: $file"
88     continue
89   }
90   echo "Checking $file"
91
92   case $file in
93     *stackovf.test)
94       "$file" "$m4"
95       case $? in
96         77) skipped="$skipped $file";;
97         0) ;;
98         *) failed="$failed $file"
99       esac
100       continue ;;
101   esac
102
103   options=`${SED} -ne '3s/^dnl @ extra options: //p;3q' "$file"`
104   ${SED} -e '/^dnl @/d' -e '/^\^D$/q' "$file" \
105     | LC_MESSAGES=C M4PATH=$examples "$m4" -d $options - >$out 2>$err
106   stat=$?
107
108   xstat=`${SED} -ne '2s/^dnl @ expected status: //p;2q' "$file"`
109   case $stat in
110     77)
111       skipped="$skipped $file"
112       cat $err
113       continue
114       ;;
115     $xstat) ;;
116     *)
117       failed="$failed $file:status"
118       echo `${SED} -e 's/^dnl //' -e 1q $file`
119       echo "$file: status was $stat, expected $xstat"
120       ;;
121   esac
122
123   xoutfile=`${SED} -n 's/^dnl @ expected output: //p' "$file"`
124   if test -z "$xoutfile" ; then
125     ${SED} -e '/^dnl @result{}/!d' -e 's///' -e "s|examples/|$examples/|" \
126       "$file" > $xout
127   else
128     cp "$examples/$xoutfile" $xout
129   fi
130
131   xerrfile=`${SED} -n 's/^dnl @ expected error: //p' "$file"`
132   case $xerrfile in
133     ignore)
134       cp $err $xerr ;;
135     '')
136       ${SED} '/^dnl @error{}/!d
137            s///; '"s|^m4:|$m4name:|; s|examples/|$examples/|" \
138         "$file" > $xerr ;;
139     *)
140       ${SED} "s|^m4:|$m4name:|; s|examples/|$examples/|" \
141         "$examples/$xerrfile" > $xerr ;;
142   esac
143
144   # For the benefit of mingw, normalize \r\n line endings
145   if $strip_needed ; then
146     tr -d '\015' < $out > $out.t
147     mv $out.t $out
148     tr -d '\015' < $xout > $xout.t
149     mv $xout.t $xout
150     tr -d '\015' < $err > $err.t
151     mv $err.t $err
152     tr -d '\015' < $xerr > $xerr.t
153     mv $xerr.t $xerr
154   fi
155
156   if cmp -s $out $xout; then
157     :
158   else
159     failed="$failed $file:out"
160     echo `${SED} -e 's/^dnl //' -e 1q $file`
161     echo "$file: stdout mismatch"
162     diff $diffopts $xout $out
163   fi
164
165   if cmp -s $err $xerr; then
166     :
167   else
168     failed="$failed $file:err"
169     echo `${SED} -e 's/^dnl //' -e 1q $file`
170     echo "$file: stderr mismatch"
171     diff $diffopts $xerr $err
172   fi
173
174 done
175
176 rm -f $out $err $xout $xerr
177
178 echo
179 if test -n "$skipped"; then
180   echo "Skipped checks were:"
181   echo " $skipped"
182 fi
183 if test -z "$failed"; then
184   echo "All checks successful"
185   stat=0
186 else
187   echo "Failed checks were:"
188   echo " $failed"
189   stat=1
190 fi
191 (exit $stat); exit $stat