138889Sjdp#! /bin/sh
238889Sjdp# ylwrap - wrapper for lex/yacc invocations.
3218822Sdim
4218822Sdimscriptversion=2005-05-14.22
5218822Sdim
6218822Sdim# Copyright (C) 1996, 1997, 1998, 1999, 2001, 2002, 2003, 2004, 2005
7218822Sdim#   Free Software Foundation, Inc.
8218822Sdim#
938889Sjdp# Written by Tom Tromey <tromey@cygnus.com>.
1038889Sjdp#
1138889Sjdp# This program is free software; you can redistribute it and/or modify
1238889Sjdp# it under the terms of the GNU General Public License as published by
1338889Sjdp# the Free Software Foundation; either version 2, or (at your option)
1438889Sjdp# any later version.
1538889Sjdp#
1638889Sjdp# This program is distributed in the hope that it will be useful,
1738889Sjdp# but WITHOUT ANY WARRANTY; without even the implied warranty of
1838889Sjdp# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1938889Sjdp# GNU General Public License for more details.
2038889Sjdp#
2138889Sjdp# You should have received a copy of the GNU General Public License
2238889Sjdp# along with this program; if not, write to the Free Software
23218822Sdim# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24218822Sdim# 02110-1301, USA.
2538889Sjdp
26218822Sdim# As a special exception to the GNU General Public License, if you
27218822Sdim# distribute this file as part of a program that contains a
28218822Sdim# configuration script generated by Autoconf, you may include it under
29218822Sdim# the same distribution terms that you use for the rest of that program.
3038889Sjdp
31218822Sdim# This file is maintained in Automake, please report
32218822Sdim# bugs to <bug-automake@gnu.org> or send patches to
33218822Sdim# <automake-patches@gnu.org>.
34218822Sdim
35218822Sdimcase "$1" in
36218822Sdim  '')
37218822Sdim    echo "$0: No files given.  Try \`$0 --help' for more information." 1>&2
38218822Sdim    exit 1
39218822Sdim    ;;
40218822Sdim  --basedir)
41218822Sdim    basedir=$2
42218822Sdim    shift 2
43218822Sdim    ;;
44218822Sdim  -h|--h*)
45218822Sdim    cat <<\EOF
46218822SdimUsage: ylwrap [--help|--version] INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
47218822Sdim
48218822SdimWrapper for lex/yacc invocations, renaming files as desired.
49218822Sdim
50218822Sdim  INPUT is the input file
51218822Sdim  OUTPUT is one file PROG generates
52218822Sdim  DESIRED is the file we actually want instead of OUTPUT
53218822Sdim  PROGRAM is program to run
54218822Sdim  ARGS are passed to PROG
55218822Sdim
56218822SdimAny number of OUTPUT,DESIRED pairs may be used.
57218822Sdim
58218822SdimReport bugs to <bug-automake@gnu.org>.
59218822SdimEOF
60218822Sdim    exit $?
61218822Sdim    ;;
62218822Sdim  -v|--v*)
63218822Sdim    echo "ylwrap $scriptversion"
64218822Sdim    exit $?
65218822Sdim    ;;
6638889Sjdpesac
6738889Sjdp
68218822Sdim
6938889Sjdp# The input.
7038889Sjdpinput="$1"
7138889Sjdpshift
7238889Sjdpcase "$input" in
73218822Sdim  [\\/]* | ?:[\\/]*)
7438889Sjdp    # Absolute path; do nothing.
7538889Sjdp    ;;
76218822Sdim  *)
77218822Sdim    # Relative path.  Make it absolute.
7838889Sjdp    input="`pwd`/$input"
7938889Sjdp    ;;
8038889Sjdpesac
8138889Sjdp
8238889Sjdppairlist=
8338889Sjdpwhile test "$#" -ne 0; do
84218822Sdim  if test "$1" = "--"; then
85218822Sdim    shift
86218822Sdim    break
87218822Sdim  fi
88218822Sdim  pairlist="$pairlist $1"
89218822Sdim  shift
9038889Sjdpdone
9138889Sjdp
92218822Sdim# The program to run.
93218822Sdimprog="$1"
94218822Sdimshift
95218822Sdim# Make any relative path in $prog absolute.
96218822Sdimcase "$prog" in
97218822Sdim  [\\/]* | ?:[\\/]*) ;;
98218822Sdim  *[\\/]*) prog="`pwd`/$prog" ;;
99218822Sdimesac
100218822Sdim
10138889Sjdp# FIXME: add hostname here for parallel makes that run commands on
10238889Sjdp# other machines.  But that might take us over the 14-char limit.
10338889Sjdpdirname=ylwrap$$
10438889Sjdptrap "cd `pwd`; rm -rf $dirname > /dev/null 2>&1" 1 2 3 15
10538889Sjdpmkdir $dirname || exit 1
10638889Sjdp
10738889Sjdpcd $dirname
108218822Sdim
109218822Sdimcase $# in
110218822Sdim  0) $prog "$input" ;;
111218822Sdim  *) $prog "$@" "$input" ;;
11260484Sobrienesac
113218822Sdimret=$?
11438889Sjdp
115218822Sdimif test $ret -eq 0; then
116218822Sdim  set X $pairlist
117218822Sdim  shift
118218822Sdim  first=yes
119218822Sdim  # Since DOS filename conventions don't allow two dots,
120218822Sdim  # the DOS version of Bison writes out y_tab.c instead of y.tab.c
121218822Sdim  # and y_tab.h instead of y.tab.h. Test to see if this is the case.
122218822Sdim  y_tab_nodot="no"
123218822Sdim  if test -f y_tab.c || test -f y_tab.h; then
124218822Sdim    y_tab_nodot="yes"
125218822Sdim  fi
126218822Sdim
127218822Sdim  # The directory holding the input.
128218822Sdim  input_dir=`echo "$input" | sed -e 's,\([\\/]\)[^\\/]*$,\1,'`
129218822Sdim  # Quote $INPUT_DIR so we can use it in a regexp.
130218822Sdim  # FIXME: really we should care about more than `.' and `\'.
131218822Sdim  input_rx=`echo "$input_dir" | sed 's,\\\\,\\\\\\\\,g;s,\\.,\\\\.,g'`
132218822Sdim
133218822Sdim  while test "$#" -ne 0; do
134218822Sdim    from="$1"
135218822Sdim    # Handle y_tab.c and y_tab.h output by DOS
136218822Sdim    if test $y_tab_nodot = "yes"; then
137218822Sdim      if test $from = "y.tab.c"; then
138218822Sdim    	from="y_tab.c"
13938889Sjdp      else
140218822Sdim    	if test $from = "y.tab.h"; then
141218822Sdim    	  from="y_tab.h"
142218822Sdim    	fi
14338889Sjdp      fi
144218822Sdim    fi
145218822Sdim    if test -f "$from"; then
146218822Sdim      # If $2 is an absolute path name, then just use that,
147218822Sdim      # otherwise prepend `../'.
148218822Sdim      case "$2" in
149218822Sdim    	[\\/]* | ?:[\\/]*) target="$2";;
150218822Sdim    	*) target="../$2";;
151218822Sdim      esac
152218822Sdim
153218822Sdim      # We do not want to overwrite a header file if it hasn't
154218822Sdim      # changed.  This avoid useless recompilations.  However the
155218822Sdim      # parser itself (the first file) should always be updated,
156218822Sdim      # because it is the destination of the .y.c rule in the
157218822Sdim      # Makefile.  Divert the output of all other files to a temporary
158218822Sdim      # file so we can compare them to existing versions.
159218822Sdim      if test $first = no; then
160218822Sdim	realtarget="$target"
161218822Sdim	target="tmp-`echo $target | sed s/.*[\\/]//g`"
162218822Sdim      fi
163218822Sdim      # Edit out `#line' or `#' directives.
164218822Sdim      #
165218822Sdim      # We don't want the resulting debug information to point at
166218822Sdim      # an absolute srcdir; it is better for it to just mention the
167218822Sdim      # .y file with no path.
168218822Sdim      #
169218822Sdim      # We want to use the real output file name, not yy.lex.c for
170218822Sdim      # instance.
171218822Sdim      #
172218822Sdim      # We want the include guards to be adjusted too.
173218822Sdim      FROM=`echo "$from" | sed \
174218822Sdim            -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
175218822Sdim            -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`
176218822Sdim      TARGET=`echo "$2" | sed \
177218822Sdim            -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
178218822Sdim            -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`
179218822Sdim
180218822Sdim      sed -e "/^#/!b" -e "s,$input_rx,," -e "s,$from,$2," \
181218822Sdim          -e "s,$FROM,$TARGET," "$from" >"$target" || ret=$?
182218822Sdim
183218822Sdim      # Check whether header files must be updated.
184218822Sdim      if test $first = no; then
185218822Sdim	if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
186218822Sdim	  echo "$2" is unchanged
187218822Sdim	  rm -f "$target"
188218822Sdim	else
189218822Sdim          echo updating "$2"
190218822Sdim          mv -f "$target" "$realtarget"
191218822Sdim        fi
192218822Sdim      fi
193218822Sdim    else
194218822Sdim      # A missing file is only an error for the first file.  This
195218822Sdim      # is a blatant hack to let us support using "yacc -d".  If -d
196218822Sdim      # is not specified, we don't want an error when the header
197218822Sdim      # file is "missing".
198218822Sdim      if test $first = yes; then
199218822Sdim        ret=1
200218822Sdim      fi
201218822Sdim    fi
202218822Sdim    shift
203218822Sdim    shift
204218822Sdim    first=no
205218822Sdim  done
20638889Sjdpelse
207218822Sdim  ret=$?
20838889Sjdpfi
20938889Sjdp
21038889Sjdp# Remove the directory.
21138889Sjdpcd ..
21238889Sjdprm -rf $dirname
21338889Sjdp
214218822Sdimexit $ret
215218822Sdim
216218822Sdim# Local Variables:
217218822Sdim# mode: shell-script
218218822Sdim# sh-indentation: 2
219218822Sdim# eval: (add-hook 'write-file-hooks 'time-stamp)
220218822Sdim# time-stamp-start: "scriptversion="
221218822Sdim# time-stamp-format: "%:y-%02m-%02d.%02H"
222218822Sdim# time-stamp-end: "$"
223218822Sdim# End:
224