maint.mk: avoid spurious failure of _sc_search_regexp-using tests
[gnulib.git] / build-aux / ylwrap
1 #! /bin/sh
2 # ylwrap - wrapper for lex/yacc invocations.
3
4 scriptversion=2011-08-25.18; # UTC
5
6 # Copyright (C) 1996-2012 Free Software Foundation, Inc.
7 #
8 # Written by Tom Tromey <tromey@cygnus.com>.
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2, or (at your option)
13 # any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
23 # As a special exception to the GNU General Public License, if you
24 # distribute this file as part of a program that contains a
25 # configuration script generated by Autoconf, you may include it under
26 # the same distribution terms that you use for the rest of that program.
27
28 # This file is maintained in Automake, please report
29 # bugs to <bug-automake@gnu.org> or send patches to
30 # <automake-patches@gnu.org>.
31
32 case "$1" in
33   '')
34     echo "$0: No files given.  Try '$0 --help' for more information." 1>&2
35     exit 1
36     ;;
37   --basedir)
38     basedir=$2
39     shift 2
40     ;;
41   -h|--h*)
42     cat <<\EOF
43 Usage: ylwrap [--help|--version] INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
44
45 Wrapper for lex/yacc invocations, renaming files as desired.
46
47   INPUT is the input file
48   OUTPUT is one file PROG generates
49   DESIRED is the file we actually want instead of OUTPUT
50   PROGRAM is program to run
51   ARGS are passed to PROG
52
53 Any number of OUTPUT,DESIRED pairs may be used.
54
55 Report bugs to <bug-automake@gnu.org>.
56 EOF
57     exit $?
58     ;;
59   -v|--v*)
60     echo "ylwrap $scriptversion"
61     exit $?
62     ;;
63 esac
64
65
66 # The input.
67 input="$1"
68 shift
69 case "$input" in
70   [\\/]* | ?:[\\/]*)
71     # Absolute path; do nothing.
72     ;;
73   *)
74     # Relative path.  Make it absolute.
75     input="`pwd`/$input"
76     ;;
77 esac
78
79 pairlist=
80 while test "$#" -ne 0; do
81   if test "$1" = "--"; then
82     shift
83     break
84   fi
85   pairlist="$pairlist $1"
86   shift
87 done
88
89 # The program to run.
90 prog="$1"
91 shift
92 # Make any relative path in $prog absolute.
93 case "$prog" in
94   [\\/]* | ?:[\\/]*) ;;
95   *[\\/]*) prog="`pwd`/$prog" ;;
96 esac
97
98 # FIXME: add hostname here for parallel makes that run commands on
99 # other machines.  But that might take us over the 14-char limit.
100 dirname=ylwrap$$
101 do_exit="cd '`pwd`' && rm -rf $dirname > /dev/null 2>&1;"' (exit $ret); exit $ret'
102 trap "ret=129; $do_exit" 1
103 trap "ret=130; $do_exit" 2
104 trap "ret=141; $do_exit" 13
105 trap "ret=143; $do_exit" 15
106 mkdir $dirname || exit 1
107
108 cd $dirname
109
110 case $# in
111   0) "$prog" "$input" ;;
112   *) "$prog" "$@" "$input" ;;
113 esac
114 ret=$?
115
116 if test $ret -eq 0; then
117   set X $pairlist
118   shift
119   first=yes
120   # Since DOS filename conventions don't allow two dots,
121   # the DOS version of Bison writes out y_tab.c instead of y.tab.c
122   # and y_tab.h instead of y.tab.h. Test to see if this is the case.
123   y_tab_nodot="no"
124   if test -f y_tab.c || test -f y_tab.h; then
125     y_tab_nodot="yes"
126   fi
127
128   # The directory holding the input.
129   input_dir=`echo "$input" | sed -e 's,\([\\/]\)[^\\/]*$,\1,'`
130   # Quote $INPUT_DIR so we can use it in a regexp.
131   # FIXME: really we should care about more than '.' and '\'.
132   input_rx=`echo "$input_dir" | sed 's,\\\\,\\\\\\\\,g;s,\\.,\\\\.,g'`
133
134   while test "$#" -ne 0; do
135     from="$1"
136     # Handle y_tab.c and y_tab.h output by DOS
137     if test $y_tab_nodot = "yes"; then
138       if test $from = "y.tab.c"; then
139         from="y_tab.c"
140       else
141         if test $from = "y.tab.h"; then
142           from="y_tab.h"
143         fi
144       fi
145     fi
146     if test -f "$from"; then
147       # If $2 is an absolute path name, then just use that,
148       # otherwise prepend '../'.
149       case "$2" in
150         [\\/]* | ?:[\\/]*) target="$2";;
151         *) target="../$2";;
152       esac
153
154       # We do not want to overwrite a header file if it hasn't
155       # changed.  This avoid useless recompilations.  However the
156       # parser itself (the first file) should always be updated,
157       # because it is the destination of the .y.c rule in the
158       # Makefile.  Divert the output of all other files to a temporary
159       # file so we can compare them to existing versions.
160       if test $first = no; then
161         realtarget="$target"
162         target="tmp-`echo $target | sed s/.*[\\/]//g`"
163       fi
164       # Edit out '#line' or '#' directives.
165       #
166       # We don't want the resulting debug information to point at
167       # an absolute srcdir; it is better for it to just mention the
168       # .y file with no path.
169       #
170       # We want to use the real output file name, not yy.lex.c for
171       # instance.
172       #
173       # We want the include guards to be adjusted too.
174       FROM=`echo "$from" | sed \
175             -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
176             -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`
177       TARGET=`echo "$2" | sed \
178             -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
179             -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`
180
181       sed -e "/^#/!b" -e "s,$input_rx,," -e "s,$from,$2," \
182           -e "s,$FROM,$TARGET," "$from" >"$target" || ret=$?
183
184       # Check whether header files must be updated.
185       if test $first = no; then
186         if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
187           echo "$2" is unchanged
188           rm -f "$target"
189         else
190           echo updating "$2"
191           mv -f "$target" "$realtarget"
192         fi
193       fi
194     else
195       # A missing file is only an error for the first file.  This
196       # is a blatant hack to let us support using "yacc -d".  If -d
197       # is not specified, we don't want an error when the header
198       # file is "missing".
199       if test $first = yes; then
200         ret=1
201       fi
202     fi
203     shift
204     shift
205     first=no
206   done
207 else
208   ret=$?
209 fi
210
211 # Remove the directory.
212 cd ..
213 rm -rf $dirname
214
215 exit $ret
216
217 # Local Variables:
218 # mode: shell-script
219 # sh-indentation: 2
220 # eval: (add-hook 'write-file-hooks 'time-stamp)
221 # time-stamp-start: "scriptversion="
222 # time-stamp-format: "%:y-%02m-%02d.%02H"
223 # time-stamp-time-zone: "UTC"
224 # time-stamp-end: "; # UTC"
225 # End: