configure: make perl path with whitespace a warning, not error.
[automake.git] / lib / ylwrap
blob9e63acd92a22526ee9de5b16e5ae840501bc59f3
1 #! /bin/sh
2 # ylwrap - wrapper for lex/yacc invocations.
4 scriptversion=2024-06-19.01; # UTC
6 # Copyright (C) 1996-2024 Free Software Foundation, Inc.
8 # Written by Tom Tromey <tromey@cygnus.com>.
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.
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.
20 # You should have received a copy of the GNU General Public License
21 # along with this program. If not, see <https://www.gnu.org/licenses/>.
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.
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>.
32 get_dirname ()
34 case $1 in
35 */*|*\\*) printf '%s\n' "$1" | sed -e 's|\([\\/]\)[^\\/]*$|\1|';;
36 # Otherwise, we want the empty string (not ".").
37 esac
40 # guard FILE
41 # ----------
42 # The CPP macro used to guard inclusion of FILE.
43 guard ()
45 printf '%s\n' "$1" \
46 | sed \
47 -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' \
48 -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g' \
49 -e 's/__*/_/g'
52 # quote_for_sed [STRING]
53 # ----------------------
54 # Return STRING (or stdin) quoted to be used as a sed pattern.
55 quote_for_sed ()
57 case $# in
58 0) cat;;
59 1) printf '%s\n' "$1";;
60 esac \
61 | sed -e 's|[][\\.*]|\\&|g'
64 case "$1" in
65 '')
66 echo "$0: No files given. Try '$0 --help' for more information." 1>&2
67 exit 1
69 -h|--h*)
70 cat <<\EOF
71 Usage: ylwrap [--help|--version] INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
73 Wrapper for lex/yacc invocations, renaming files as desired.
75 INPUT is the input file
76 OUTPUT is one file PROG generates
77 DESIRED is the file we actually want instead of OUTPUT
78 PROGRAM is program to run
79 ARGS are passed to PROG
81 Any number of OUTPUT,DESIRED pairs may be used.
83 Report bugs to <bug-automake@gnu.org>.
84 GNU Automake home page: <https://www.gnu.org/software/automake/>.
85 General help using GNU software: <https://www.gnu.org/gethelp/>.
86 EOF
87 exit $?
89 -v|--v*)
90 echo "ylwrap (GNU Automake) $scriptversion"
91 exit $?
93 esac
96 # The input.
97 input=$1
98 shift
99 # We'll later need for a correct munging of "#line" directives.
100 input_sub_rx=`get_dirname "$input" | quote_for_sed`
101 case $input in
102 [\\/]* | ?:[\\/]*)
103 # Absolute path; do nothing.
106 # Relative path. Make it absolute.
107 input=`pwd`/$input
109 esac
110 input_rx=`get_dirname "$input" | quote_for_sed`
112 # Since DOS filename conventions don't allow two dots,
113 # the DOS version of Bison writes out y_tab.c instead of y.tab.c
114 # and y_tab.h instead of y.tab.h. Test to see if this is the case.
115 y_tab_nodot=false
116 if test -f y_tab.c || test -f y_tab.h; then
117 y_tab_nodot=true
120 # The parser itself, the first file, is the destination of the .y.c
121 # rule in the Makefile.
122 parser=$1
124 # A sed program to s/FROM/TO/g for all the FROM/TO so that, for
125 # instance, we rename #include "y.tab.h" into #include "parse.h"
126 # during the conversion from y.tab.c to parse.c.
127 sed_fix_filenames=
129 # Also rename header guards, as Bison 2.7 for instance uses its header
130 # guard in its implementation file.
131 sed_fix_header_guards=
133 while test $# -ne 0; do
134 if test x"$1" = x"--"; then
135 shift
136 break
138 from=$1
139 # Handle y_tab.c and y_tab.h output by DOS
140 if $y_tab_nodot; then
141 case $from in
142 "y.tab.c") from=y_tab.c;;
143 "y.tab.h") from=y_tab.h;;
144 esac
146 shift
147 to=$1
148 shift
149 sed_fix_filenames="${sed_fix_filenames}s|"`quote_for_sed "$from"`"|$to|g;"
150 sed_fix_header_guards="${sed_fix_header_guards}s|"`guard "$from"`"|"`guard "$to"`"|g;"
151 done
153 # The program to run.
154 prog=$1
155 shift
156 # Make any relative path in $prog absolute.
157 case $prog in
158 [\\/]* | ?:[\\/]*) ;;
159 *[\\/]*) prog=`pwd`/$prog ;;
160 esac
162 dirname=ylwrap$$
163 do_exit="cd '`pwd`' && rm -rf $dirname > /dev/null 2>&1;"' (exit $ret); exit $ret'
164 trap "ret=129; $do_exit" 1
165 trap "ret=130; $do_exit" 2
166 trap "ret=141; $do_exit" 13
167 trap "ret=143; $do_exit" 15
168 mkdir $dirname || exit 1
170 cd $dirname
172 case $# in
173 0) "$prog" "$input" ;;
174 *) "$prog" "$@" "$input" ;;
175 esac
176 ret=$?
178 if test $ret -eq 0; then
179 for from in *
181 to=`printf '%s\n' "$from" | sed "$sed_fix_filenames"`
182 if test -f "$from"; then
183 # If $2 is an absolute path name, then just use that,
184 # otherwise prepend '../'.
185 case $to in
186 [\\/]* | ?:[\\/]*) target=$to;;
187 *) target=../$to;;
188 esac
190 # Do not overwrite unchanged header files to avoid useless
191 # recompilations. Always update the parser itself: it is the
192 # destination of the .y.c rule in the Makefile. Divert the
193 # output of all other files to a temporary file so we can
194 # compare them to existing versions.
195 if test $from != $parser; then
196 realtarget=$target
197 target=tmp-`printf '%s\n' "$target" | sed 's|.*[\\/]||g'`
200 # Munge "#line" or "#" directives. Don't let the resulting
201 # debug information point at an absolute srcdir. Use the real
202 # output file name, not yy.lex.c for instance. Adjust the
203 # include guards too.
204 sed -e "/^#/!b" \
205 -e "s|$input_rx|$input_sub_rx|" \
206 -e "$sed_fix_filenames" \
207 -e "$sed_fix_header_guards" \
208 "$from" >"$target" || ret=$?
210 # Check whether files must be updated.
211 if test "$from" != "$parser"; then
212 if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
213 echo "$to is unchanged"
214 rm -f "$target"
215 else
216 echo "updating $to"
217 mv -f "$target" "$realtarget"
220 else
221 # A missing file is only an error for the parser. This is a
222 # blatant hack to let us support using "yacc -d". If -d is not
223 # specified, don't fail when the header file is "missing".
224 if test "$from" = "$parser"; then
225 ret=1
228 done
231 # Remove the directory.
232 cd ..
233 rm -rf $dirname
235 exit $ret
237 # Local Variables:
238 # mode: shell-script
239 # sh-indentation: 2
240 # eval: (add-hook 'before-save-hook 'time-stamp)
241 # time-stamp-start: "scriptversion="
242 # time-stamp-format: "%:y-%02m-%02d.%02H"
243 # time-stamp-time-zone: "UTC0"
244 # time-stamp-end: "; # UTC"
245 # End: