blob: 8f072a8e97c00490c74a591edc511af919584adf [file] [log] [blame]
Ben Cheng25b3c042013-11-20 14:45:36 -08001#! /bin/sh
2# ylwrap - wrapper for lex/yacc invocations.
3
Elliott Hughes03333822015-02-18 22:19:45 -08004scriptversion=2013-01-12.17; # UTC
Ben Cheng25b3c042013-11-20 14:45:36 -08005
Elliott Hughes03333822015-02-18 22:19:45 -08006# Copyright (C) 1996-2013 Free Software Foundation, Inc.
Ben Cheng25b3c042013-11-20 14:45:36 -08007#
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
Elliott Hughes03333822015-02-18 22:19:45 -080032get_dirname ()
33{
34 case $1 in
35 */*|*\\*) printf '%s\n' "$1" | sed -e 's|\([\\/]\)[^\\/]*$|\1|';;
36 # Otherwise, we want the empty string (not ".").
37 esac
38}
39
40# guard FILE
41# ----------
42# The CPP macro used to guard inclusion of FILE.
43guard ()
44{
45 printf '%s\n' "$1" \
46 | sed \
47 -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' \
48 -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g' \
49 -e 's/__*/_/g'
50}
51
52# quote_for_sed [STRING]
53# ----------------------
54# Return STRING (or stdin) quoted to be used as a sed pattern.
55quote_for_sed ()
56{
57 case $# in
58 0) cat;;
59 1) printf '%s\n' "$1";;
60 esac \
61 | sed -e 's|[][\\.*]|\\&|g'
62}
63
Ben Cheng25b3c042013-11-20 14:45:36 -080064case "$1" in
65 '')
Elliott Hughes03333822015-02-18 22:19:45 -080066 echo "$0: No files given. Try '$0 --help' for more information." 1>&2
Ben Cheng25b3c042013-11-20 14:45:36 -080067 exit 1
68 ;;
69 --basedir)
70 basedir=$2
71 shift 2
72 ;;
73 -h|--h*)
74 cat <<\EOF
75Usage: ylwrap [--help|--version] INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
76
77Wrapper for lex/yacc invocations, renaming files as desired.
78
79 INPUT is the input file
80 OUTPUT is one file PROG generates
81 DESIRED is the file we actually want instead of OUTPUT
82 PROGRAM is program to run
83 ARGS are passed to PROG
84
85Any number of OUTPUT,DESIRED pairs may be used.
86
87Report bugs to <bug-automake@gnu.org>.
88EOF
89 exit $?
90 ;;
91 -v|--v*)
92 echo "ylwrap $scriptversion"
93 exit $?
94 ;;
95esac
96
97
98# The input.
Elliott Hughes03333822015-02-18 22:19:45 -080099input=$1
Ben Cheng25b3c042013-11-20 14:45:36 -0800100shift
Elliott Hughes03333822015-02-18 22:19:45 -0800101# We'll later need for a correct munging of "#line" directives.
102input_sub_rx=`get_dirname "$input" | quote_for_sed`
103case $input in
Ben Cheng25b3c042013-11-20 14:45:36 -0800104 [\\/]* | ?:[\\/]*)
105 # Absolute path; do nothing.
106 ;;
107 *)
108 # Relative path. Make it absolute.
Elliott Hughes03333822015-02-18 22:19:45 -0800109 input=`pwd`/$input
Ben Cheng25b3c042013-11-20 14:45:36 -0800110 ;;
111esac
Elliott Hughes03333822015-02-18 22:19:45 -0800112input_rx=`get_dirname "$input" | quote_for_sed`
Ben Cheng25b3c042013-11-20 14:45:36 -0800113
Elliott Hughes03333822015-02-18 22:19:45 -0800114# Since DOS filename conventions don't allow two dots,
115# the DOS version of Bison writes out y_tab.c instead of y.tab.c
116# and y_tab.h instead of y.tab.h. Test to see if this is the case.
117y_tab_nodot=false
118if test -f y_tab.c || test -f y_tab.h; then
119 y_tab_nodot=true
120fi
121
122# The parser itself, the first file, is the destination of the .y.c
123# rule in the Makefile.
124parser=$1
125
126# A sed program to s/FROM/TO/g for all the FROM/TO so that, for
127# instance, we rename #include "y.tab.h" into #include "parse.h"
128# during the conversion from y.tab.c to parse.c.
129sed_fix_filenames=
130
131# Also rename header guards, as Bison 2.7 for instance uses its header
132# guard in its implementation file.
133sed_fix_header_guards=
134
135while test $# -ne 0; do
136 if test x"$1" = x"--"; then
Ben Cheng25b3c042013-11-20 14:45:36 -0800137 shift
138 break
139 fi
Elliott Hughes03333822015-02-18 22:19:45 -0800140 from=$1
141 # Handle y_tab.c and y_tab.h output by DOS
142 if $y_tab_nodot; then
143 case $from in
144 "y.tab.c") from=y_tab.c;;
145 "y.tab.h") from=y_tab.h;;
146 esac
147 fi
Ben Cheng25b3c042013-11-20 14:45:36 -0800148 shift
Elliott Hughes03333822015-02-18 22:19:45 -0800149 to=$1
150 shift
151 sed_fix_filenames="${sed_fix_filenames}s|"`quote_for_sed "$from"`"|$to|g;"
152 sed_fix_header_guards="${sed_fix_header_guards}s|"`guard "$from"`"|"`guard "$to"`"|g;"
Ben Cheng25b3c042013-11-20 14:45:36 -0800153done
154
155# The program to run.
Elliott Hughes03333822015-02-18 22:19:45 -0800156prog=$1
Ben Cheng25b3c042013-11-20 14:45:36 -0800157shift
158# Make any relative path in $prog absolute.
Elliott Hughes03333822015-02-18 22:19:45 -0800159case $prog in
Ben Cheng25b3c042013-11-20 14:45:36 -0800160 [\\/]* | ?:[\\/]*) ;;
Elliott Hughes03333822015-02-18 22:19:45 -0800161 *[\\/]*) prog=`pwd`/$prog ;;
Ben Cheng25b3c042013-11-20 14:45:36 -0800162esac
163
Ben Cheng25b3c042013-11-20 14:45:36 -0800164dirname=ylwrap$$
Elliott Hughes03333822015-02-18 22:19:45 -0800165do_exit="cd '`pwd`' && rm -rf $dirname > /dev/null 2>&1;"' (exit $ret); exit $ret'
166trap "ret=129; $do_exit" 1
167trap "ret=130; $do_exit" 2
168trap "ret=141; $do_exit" 13
169trap "ret=143; $do_exit" 15
Ben Cheng25b3c042013-11-20 14:45:36 -0800170mkdir $dirname || exit 1
171
172cd $dirname
173
174case $# in
175 0) "$prog" "$input" ;;
176 *) "$prog" "$@" "$input" ;;
177esac
178ret=$?
179
180if test $ret -eq 0; then
Elliott Hughes03333822015-02-18 22:19:45 -0800181 for from in *
182 do
183 to=`printf '%s\n' "$from" | sed "$sed_fix_filenames"`
Ben Cheng25b3c042013-11-20 14:45:36 -0800184 if test -f "$from"; then
185 # If $2 is an absolute path name, then just use that,
Elliott Hughes03333822015-02-18 22:19:45 -0800186 # otherwise prepend '../'.
187 case $to in
188 [\\/]* | ?:[\\/]*) target=$to;;
189 *) target=../$to;;
Ben Cheng25b3c042013-11-20 14:45:36 -0800190 esac
191
Elliott Hughes03333822015-02-18 22:19:45 -0800192 # Do not overwrite unchanged header files to avoid useless
193 # recompilations. Always update the parser itself: it is the
194 # destination of the .y.c rule in the Makefile. Divert the
195 # output of all other files to a temporary file so we can
196 # compare them to existing versions.
197 if test $from != $parser; then
198 realtarget=$target
199 target=tmp-`printf '%s\n' "$target" | sed 's|.*[\\/]||g'`
Ben Cheng25b3c042013-11-20 14:45:36 -0800200 fi
Ben Cheng25b3c042013-11-20 14:45:36 -0800201
Elliott Hughes03333822015-02-18 22:19:45 -0800202 # Munge "#line" or "#" directives. Don't let the resulting
203 # debug information point at an absolute srcdir. Use the real
204 # output file name, not yy.lex.c for instance. Adjust the
205 # include guards too.
206 sed -e "/^#/!b" \
207 -e "s|$input_rx|$input_sub_rx|" \
208 -e "$sed_fix_filenames" \
209 -e "$sed_fix_header_guards" \
210 "$from" >"$target" || ret=$?
Ben Cheng25b3c042013-11-20 14:45:36 -0800211
Elliott Hughes03333822015-02-18 22:19:45 -0800212 # Check whether files must be updated.
213 if test "$from" != "$parser"; then
214 if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
215 echo "$to is unchanged"
216 rm -f "$target"
217 else
218 echo "updating $to"
Ben Cheng25b3c042013-11-20 14:45:36 -0800219 mv -f "$target" "$realtarget"
220 fi
221 fi
222 else
Elliott Hughes03333822015-02-18 22:19:45 -0800223 # A missing file is only an error for the parser. This is a
224 # blatant hack to let us support using "yacc -d". If -d is not
225 # specified, don't fail when the header file is "missing".
226 if test "$from" = "$parser"; then
Ben Cheng25b3c042013-11-20 14:45:36 -0800227 ret=1
228 fi
229 fi
Ben Cheng25b3c042013-11-20 14:45:36 -0800230 done
Ben Cheng25b3c042013-11-20 14:45:36 -0800231fi
232
233# Remove the directory.
234cd ..
235rm -rf $dirname
236
237exit $ret
238
239# Local Variables:
240# mode: shell-script
241# sh-indentation: 2
242# eval: (add-hook 'write-file-hooks 'time-stamp)
243# time-stamp-start: "scriptversion="
244# time-stamp-format: "%:y-%02m-%02d.%02H"
245# time-stamp-time-zone: "UTC"
246# time-stamp-end: "; # UTC"
247# End: