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