ylwrap revision a85aecdf
1350952b9Smrg#! /bin/sh
2350952b9Smrg# ylwrap - wrapper for lex/yacc invocations.
3350952b9Smrg
4a85aecdfSmrgscriptversion=2011-08-25.18; # UTC
5350952b9Smrg
6350952b9Smrg# Copyright (C) 1996, 1997, 1998, 1999, 2001, 2002, 2003, 2004, 2005,
7a85aecdfSmrg# 2007, 2009, 2010, 2011 Free Software Foundation, Inc.
8350952b9Smrg#
9350952b9Smrg# Written by Tom Tromey <tromey@cygnus.com>.
10350952b9Smrg#
11350952b9Smrg# This program is free software; you can redistribute it and/or modify
12350952b9Smrg# it under the terms of the GNU General Public License as published by
13350952b9Smrg# the Free Software Foundation; either version 2, or (at your option)
14350952b9Smrg# any later version.
15350952b9Smrg#
16350952b9Smrg# This program is distributed in the hope that it will be useful,
17350952b9Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
18350952b9Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19350952b9Smrg# GNU General Public License for more details.
20350952b9Smrg#
21350952b9Smrg# You should have received a copy of the GNU General Public License
22350952b9Smrg# along with this program.  If not, see <http://www.gnu.org/licenses/>.
23350952b9Smrg
24350952b9Smrg# As a special exception to the GNU General Public License, if you
25350952b9Smrg# distribute this file as part of a program that contains a
26350952b9Smrg# configuration script generated by Autoconf, you may include it under
27350952b9Smrg# the same distribution terms that you use for the rest of that program.
28350952b9Smrg
29350952b9Smrg# This file is maintained in Automake, please report
30350952b9Smrg# bugs to <bug-automake@gnu.org> or send patches to
31350952b9Smrg# <automake-patches@gnu.org>.
32350952b9Smrg
33350952b9Smrgcase "$1" in
34350952b9Smrg  '')
35350952b9Smrg    echo "$0: No files given.  Try \`$0 --help' for more information." 1>&2
36350952b9Smrg    exit 1
37350952b9Smrg    ;;
38350952b9Smrg  --basedir)
39350952b9Smrg    basedir=$2
40350952b9Smrg    shift 2
41350952b9Smrg    ;;
42350952b9Smrg  -h|--h*)
43350952b9Smrg    cat <<\EOF
44350952b9SmrgUsage: ylwrap [--help|--version] INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
45350952b9Smrg
46350952b9SmrgWrapper for lex/yacc invocations, renaming files as desired.
47350952b9Smrg
48350952b9Smrg  INPUT is the input file
49350952b9Smrg  OUTPUT is one file PROG generates
50350952b9Smrg  DESIRED is the file we actually want instead of OUTPUT
51350952b9Smrg  PROGRAM is program to run
52350952b9Smrg  ARGS are passed to PROG
53350952b9Smrg
54350952b9SmrgAny number of OUTPUT,DESIRED pairs may be used.
55350952b9Smrg
56350952b9SmrgReport bugs to <bug-automake@gnu.org>.
57350952b9SmrgEOF
58350952b9Smrg    exit $?
59350952b9Smrg    ;;
60350952b9Smrg  -v|--v*)
61350952b9Smrg    echo "ylwrap $scriptversion"
62350952b9Smrg    exit $?
63350952b9Smrg    ;;
64350952b9Smrgesac
65350952b9Smrg
66350952b9Smrg
67350952b9Smrg# The input.
68350952b9Smrginput="$1"
69350952b9Smrgshift
70350952b9Smrgcase "$input" in
71350952b9Smrg  [\\/]* | ?:[\\/]*)
72350952b9Smrg    # Absolute path; do nothing.
73350952b9Smrg    ;;
74350952b9Smrg  *)
75350952b9Smrg    # Relative path.  Make it absolute.
76350952b9Smrg    input="`pwd`/$input"
77350952b9Smrg    ;;
78350952b9Smrgesac
79350952b9Smrg
80350952b9Smrgpairlist=
81350952b9Smrgwhile test "$#" -ne 0; do
82350952b9Smrg  if test "$1" = "--"; then
83350952b9Smrg    shift
84350952b9Smrg    break
85350952b9Smrg  fi
86350952b9Smrg  pairlist="$pairlist $1"
87350952b9Smrg  shift
88350952b9Smrgdone
89350952b9Smrg
90350952b9Smrg# The program to run.
91350952b9Smrgprog="$1"
92350952b9Smrgshift
93350952b9Smrg# Make any relative path in $prog absolute.
94350952b9Smrgcase "$prog" in
95350952b9Smrg  [\\/]* | ?:[\\/]*) ;;
96350952b9Smrg  *[\\/]*) prog="`pwd`/$prog" ;;
97350952b9Smrgesac
98350952b9Smrg
99350952b9Smrg# FIXME: add hostname here for parallel makes that run commands on
100350952b9Smrg# other machines.  But that might take us over the 14-char limit.
101350952b9Smrgdirname=ylwrap$$
102a85aecdfSmrgdo_exit="cd '`pwd`' && rm -rf $dirname > /dev/null 2>&1;"' (exit $ret); exit $ret'
103a85aecdfSmrgtrap "ret=129; $do_exit" 1
104a85aecdfSmrgtrap "ret=130; $do_exit" 2
105a85aecdfSmrgtrap "ret=141; $do_exit" 13
106a85aecdfSmrgtrap "ret=143; $do_exit" 15
107350952b9Smrgmkdir $dirname || exit 1
108350952b9Smrg
109350952b9Smrgcd $dirname
110350952b9Smrg
111350952b9Smrgcase $# in
112350952b9Smrg  0) "$prog" "$input" ;;
113350952b9Smrg  *) "$prog" "$@" "$input" ;;
114350952b9Smrgesac
115350952b9Smrgret=$?
116350952b9Smrg
117350952b9Smrgif test $ret -eq 0; then
118350952b9Smrg  set X $pairlist
119350952b9Smrg  shift
120350952b9Smrg  first=yes
121350952b9Smrg  # Since DOS filename conventions don't allow two dots,
122350952b9Smrg  # the DOS version of Bison writes out y_tab.c instead of y.tab.c
123350952b9Smrg  # and y_tab.h instead of y.tab.h. Test to see if this is the case.
124350952b9Smrg  y_tab_nodot="no"
125350952b9Smrg  if test -f y_tab.c || test -f y_tab.h; then
126350952b9Smrg    y_tab_nodot="yes"
127350952b9Smrg  fi
128350952b9Smrg
129350952b9Smrg  # The directory holding the input.
130350952b9Smrg  input_dir=`echo "$input" | sed -e 's,\([\\/]\)[^\\/]*$,\1,'`
131350952b9Smrg  # Quote $INPUT_DIR so we can use it in a regexp.
132350952b9Smrg  # FIXME: really we should care about more than `.' and `\'.
133350952b9Smrg  input_rx=`echo "$input_dir" | sed 's,\\\\,\\\\\\\\,g;s,\\.,\\\\.,g'`
134350952b9Smrg
135350952b9Smrg  while test "$#" -ne 0; do
136350952b9Smrg    from="$1"
137350952b9Smrg    # Handle y_tab.c and y_tab.h output by DOS
138350952b9Smrg    if test $y_tab_nodot = "yes"; then
139350952b9Smrg      if test $from = "y.tab.c"; then
140a85aecdfSmrg        from="y_tab.c"
141350952b9Smrg      else
142a85aecdfSmrg        if test $from = "y.tab.h"; then
143a85aecdfSmrg          from="y_tab.h"
144a85aecdfSmrg        fi
145350952b9Smrg      fi
146350952b9Smrg    fi
147350952b9Smrg    if test -f "$from"; then
148350952b9Smrg      # If $2 is an absolute path name, then just use that,
149350952b9Smrg      # otherwise prepend `../'.
150350952b9Smrg      case "$2" in
151a85aecdfSmrg        [\\/]* | ?:[\\/]*) target="$2";;
152a85aecdfSmrg        *) target="../$2";;
153350952b9Smrg      esac
154350952b9Smrg
155350952b9Smrg      # We do not want to overwrite a header file if it hasn't
156350952b9Smrg      # changed.  This avoid useless recompilations.  However the
157350952b9Smrg      # parser itself (the first file) should always be updated,
158350952b9Smrg      # because it is the destination of the .y.c rule in the
159350952b9Smrg      # Makefile.  Divert the output of all other files to a temporary
160350952b9Smrg      # file so we can compare them to existing versions.
161350952b9Smrg      if test $first = no; then
162a85aecdfSmrg        realtarget="$target"
163a85aecdfSmrg        target="tmp-`echo $target | sed s/.*[\\/]//g`"
164350952b9Smrg      fi
165350952b9Smrg      # Edit out `#line' or `#' directives.
166350952b9Smrg      #
167350952b9Smrg      # We don't want the resulting debug information to point at
168350952b9Smrg      # an absolute srcdir; it is better for it to just mention the
169350952b9Smrg      # .y file with no path.
170350952b9Smrg      #
171350952b9Smrg      # We want to use the real output file name, not yy.lex.c for
172350952b9Smrg      # instance.
173350952b9Smrg      #
174350952b9Smrg      # We want the include guards to be adjusted too.
175350952b9Smrg      FROM=`echo "$from" | sed \
176350952b9Smrg            -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
177350952b9Smrg            -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`
178350952b9Smrg      TARGET=`echo "$2" | sed \
179350952b9Smrg            -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
180350952b9Smrg            -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`
181350952b9Smrg
182350952b9Smrg      sed -e "/^#/!b" -e "s,$input_rx,," -e "s,$from,$2," \
183350952b9Smrg          -e "s,$FROM,$TARGET," "$from" >"$target" || ret=$?
184350952b9Smrg
185350952b9Smrg      # Check whether header files must be updated.
186350952b9Smrg      if test $first = no; then
187a85aecdfSmrg        if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
188a85aecdfSmrg          echo "$2" is unchanged
189a85aecdfSmrg          rm -f "$target"
190a85aecdfSmrg        else
191350952b9Smrg          echo updating "$2"
192350952b9Smrg          mv -f "$target" "$realtarget"
193350952b9Smrg        fi
194350952b9Smrg      fi
195350952b9Smrg    else
196350952b9Smrg      # A missing file is only an error for the first file.  This
197350952b9Smrg      # is a blatant hack to let us support using "yacc -d".  If -d
198350952b9Smrg      # is not specified, we don't want an error when the header
199350952b9Smrg      # file is "missing".
200350952b9Smrg      if test $first = yes; then
201350952b9Smrg        ret=1
202350952b9Smrg      fi
203350952b9Smrg    fi
204350952b9Smrg    shift
205350952b9Smrg    shift
206350952b9Smrg    first=no
207350952b9Smrg  done
208350952b9Smrgelse
209350952b9Smrg  ret=$?
210350952b9Smrgfi
211350952b9Smrg
212350952b9Smrg# Remove the directory.
213350952b9Smrgcd ..
214350952b9Smrgrm -rf $dirname
215350952b9Smrg
216350952b9Smrgexit $ret
217350952b9Smrg
218350952b9Smrg# Local Variables:
219350952b9Smrg# mode: shell-script
220350952b9Smrg# sh-indentation: 2
221350952b9Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
222350952b9Smrg# time-stamp-start: "scriptversion="
223350952b9Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
224350952b9Smrg# time-stamp-time-zone: "UTC"
225350952b9Smrg# time-stamp-end: "; # UTC"
226350952b9Smrg# End:
227