11553Srgrimes#! /bin/sh
21553Srgrimes# ylwrap - wrapper for lex/yacc invocations.
31553Srgrimes
41553Srgrimesscriptversion=2009-04-28.21; # UTC
51553Srgrimes
61553Srgrimes# Copyright (C) 1996, 1997, 1998, 1999, 2001, 2002, 2003, 2004, 2005,
71553Srgrimes# 2007, 2009 Free Software Foundation, Inc.
81553Srgrimes#
91553Srgrimes# Written by Tom Tromey <tromey@cygnus.com>.
101553Srgrimes#
111553Srgrimes# This program is free software; you can redistribute it and/or modify
121553Srgrimes# it under the terms of the GNU General Public License as published by
131553Srgrimes# the Free Software Foundation; either version 2, or (at your option)
141553Srgrimes# any later version.
151553Srgrimes#
161553Srgrimes# This program is distributed in the hope that it will be useful,
171553Srgrimes# but WITHOUT ANY WARRANTY; without even the implied warranty of
181553Srgrimes# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
191553Srgrimes# GNU General Public License for more details.
201553Srgrimes#
211553Srgrimes# You should have received a copy of the GNU General Public License
221553Srgrimes# along with this program.  If not, see <http://www.gnu.org/licenses/>.
231553Srgrimes
241553Srgrimes# As a special exception to the GNU General Public License, if you
251553Srgrimes# distribute this file as part of a program that contains a
261553Srgrimes# configuration script generated by Autoconf, you may include it under
271553Srgrimes# the same distribution terms that you use for the rest of that program.
281553Srgrimes
291553Srgrimes# This file is maintained in Automake, please report
301553Srgrimes# bugs to <bug-automake@gnu.org> or send patches to
311553Srgrimes# <automake-patches@gnu.org>.
321553Srgrimes
331553Srgrimescase "$1" in
341553Srgrimes  '')
3530027Scharnier    echo "$0: No files given.  Try \`$0 --help' for more information." 1>&2
361553Srgrimes    exit 1
3730027Scharnier    ;;
3830027Scharnier  --basedir)
3944303Swollman    basedir=$2
401553Srgrimes    shift 2
411553Srgrimes    ;;
421553Srgrimes  -h|--h*)
431553Srgrimes    cat <<\EOF
4430027ScharnierUsage: ylwrap [--help|--version] INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
4530027Scharnier
461553SrgrimesWrapper for lex/yacc invocations, renaming files as desired.
471553Srgrimes
4844303Swollman  INPUT is the input file
4930027Scharnier  OUTPUT is one file PROG generates
5044303Swollman  DESIRED is the file we actually want instead of OUTPUT
5144303Swollman  PROGRAM is program to run
5244303Swollman  ARGS are passed to PROG
5344303Swollman
5444303SwollmanAny number of OUTPUT,DESIRED pairs may be used.
5544303Swollman
5644303SwollmanReport bugs to <bug-automake@gnu.org>.
571553SrgrimesEOF
581553Srgrimes    exit $?
591553Srgrimes    ;;
601553Srgrimes  -v|--v*)
611553Srgrimes    echo "ylwrap $scriptversion"
621553Srgrimes    exit $?
631553Srgrimes    ;;
6430027Scharnieresac
651553Srgrimes
661553Srgrimes
671553Srgrimes# The input.
681553Srgrimesinput="$1"
691553Srgrimesshift
701553Srgrimescase "$input" in
711553Srgrimes  [\\/]* | ?:[\\/]*)
721553Srgrimes    # Absolute path; do nothing.
731553Srgrimes    ;;
741553Srgrimes  *)
751553Srgrimes    # Relative path.  Make it absolute.
761553Srgrimes    input="`pwd`/$input"
772860Srgrimes    ;;
781553Srgrimesesac
791553Srgrimes
801553Srgrimespairlist=
811553Srgrimeswhile test "$#" -ne 0; do
821553Srgrimes  if test "$1" = "--"; then
831553Srgrimes    shift
841553Srgrimes    break
851553Srgrimes  fi
861553Srgrimes  pairlist="$pairlist $1"
871553Srgrimes  shift
881553Srgrimesdone
891553Srgrimes
902860Srgrimes# The program to run.
911553Srgrimesprog="$1"
921553Srgrimesshift
931553Srgrimes# Make any relative path in $prog absolute.
941553Srgrimescase "$prog" in
951553Srgrimes  [\\/]* | ?:[\\/]*) ;;
961553Srgrimes  *[\\/]*) prog="`pwd`/$prog" ;;
971553Srgrimesesac
981553Srgrimes
991553Srgrimes# FIXME: add hostname here for parallel makes that run commands on
1001553Srgrimes# other machines.  But that might take us over the 14-char limit.
1011553Srgrimesdirname=ylwrap$$
1021553Srgrimestrap "cd '`pwd`'; rm -rf $dirname > /dev/null 2>&1" 1 2 3 15
1031553Srgrimesmkdir $dirname || exit 1
1041553Srgrimes
1051553Srgrimescd $dirname
1061553Srgrimes
1071553Srgrimescase $# in
1081553Srgrimes  0) "$prog" "$input" ;;
1091553Srgrimes  *) "$prog" "$@" "$input" ;;
1101553Srgrimesesac
1111553Srgrimesret=$?
1121553Srgrimes
1131553Srgrimesif test $ret -eq 0; then
1141553Srgrimes  set X $pairlist
1151553Srgrimes  shift
1161553Srgrimes  first=yes
1171553Srgrimes  # Since DOS filename conventions don't allow two dots,
1181553Srgrimes  # the DOS version of Bison writes out y_tab.c instead of y.tab.c
1191553Srgrimes  # and y_tab.h instead of y.tab.h. Test to see if this is the case.
1201553Srgrimes  y_tab_nodot="no"
1211553Srgrimes  if test -f y_tab.c || test -f y_tab.h; then
1221553Srgrimes    y_tab_nodot="yes"
1231553Srgrimes  fi
1241553Srgrimes
1251553Srgrimes  # The directory holding the input.
1261553Srgrimes  input_dir=`echo "$input" | sed -e 's,\([\\/]\)[^\\/]*$,\1,'`
1271553Srgrimes  # Quote $INPUT_DIR so we can use it in a regexp.
1281553Srgrimes  # FIXME: really we should care about more than `.' and `\'.
1292860Srgrimes  input_rx=`echo "$input_dir" | sed 's,\\\\,\\\\\\\\,g;s,\\.,\\\\.,g'`
13038020Sbde
1311553Srgrimes  while test "$#" -ne 0; do
1321553Srgrimes    from="$1"
1331553Srgrimes    # Handle y_tab.c and y_tab.h output by DOS
1341553Srgrimes    if test $y_tab_nodot = "yes"; then
1351553Srgrimes      if test $from = "y.tab.c"; then
1361553Srgrimes    	from="y_tab.c"
1371553Srgrimes      else
1381553Srgrimes    	if test $from = "y.tab.h"; then
1391553Srgrimes    	  from="y_tab.h"
1401553Srgrimes    	fi
1411553Srgrimes      fi
1421553Srgrimes    fi
1432860Srgrimes    if test -f "$from"; then
14438020Sbde      # If $2 is an absolute path name, then just use that,
1451553Srgrimes      # otherwise prepend `../'.
1461553Srgrimes      case "$2" in
1471553Srgrimes    	[\\/]* | ?:[\\/]*) target="$2";;
1481553Srgrimes    	*) target="../$2";;
1491553Srgrimes      esac
1501553Srgrimes
1511553Srgrimes      # We do not want to overwrite a header file if it hasn't
1521553Srgrimes      # changed.  This avoid useless recompilations.  However the
1531553Srgrimes      # parser itself (the first file) should always be updated,
1541553Srgrimes      # because it is the destination of the .y.c rule in the
1551553Srgrimes      # Makefile.  Divert the output of all other files to a temporary
1561553Srgrimes      # file so we can compare them to existing versions.
1571553Srgrimes      if test $first = no; then
1581553Srgrimes	realtarget="$target"
1591553Srgrimes	target="tmp-`echo $target | sed s/.*[\\/]//g`"
1601553Srgrimes      fi
1611553Srgrimes      # Edit out `#line' or `#' directives.
1621553Srgrimes      #
1631553Srgrimes      # We don't want the resulting debug information to point at
1641553Srgrimes      # an absolute srcdir; it is better for it to just mention the
1651553Srgrimes      # .y file with no path.
1661553Srgrimes      #
1671553Srgrimes      # We want to use the real output file name, not yy.lex.c for
1681553Srgrimes      # instance.
1691553Srgrimes      #
1701553Srgrimes      # We want the include guards to be adjusted too.
1711553Srgrimes      FROM=`echo "$from" | sed \
1721553Srgrimes            -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
1731553Srgrimes            -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`
1741553Srgrimes      TARGET=`echo "$2" | sed \
1751553Srgrimes            -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
1761553Srgrimes            -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`
1771553Srgrimes
1781553Srgrimes      sed -e "/^#/!b" -e "s,$input_rx,," -e "s,$from,$2," \
1791553Srgrimes          -e "s,$FROM,$TARGET," "$from" >"$target" || ret=$?
1801553Srgrimes
1811553Srgrimes      # Check whether header files must be updated.
1821553Srgrimes      if test $first = no; then
1831553Srgrimes	if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
1841553Srgrimes	  echo "$2" is unchanged
1851553Srgrimes	  rm -f "$target"
1861553Srgrimes	else
1873052Sdg          echo updating "$2"
18818404Snate          mv -f "$target" "$realtarget"
18918404Snate        fi
1901553Srgrimes      fi
1911553Srgrimes    else
19218404Snate      # A missing file is only an error for the first file.  This
1931553Srgrimes      # is a blatant hack to let us support using "yacc -d".  If -d
19418404Snate      # is not specified, we don't want an error when the header
1951553Srgrimes      # file is "missing".
1961553Srgrimes      if test $first = yes; then
1971553Srgrimes        ret=1
1981553Srgrimes      fi
1991553Srgrimes    fi
2001553Srgrimes    shift
2011553Srgrimes    shift
2021553Srgrimes    first=no
2031553Srgrimes  done
2041553Srgrimeselse
2051553Srgrimes  ret=$?
2061553Srgrimesfi
2071553Srgrimes
2081553Srgrimes# Remove the directory.
2091553Srgrimescd ..
2101553Srgrimesrm -rf $dirname
2111553Srgrimes
2121553Srgrimesexit $ret
2138857Srgrimes
2141553Srgrimes# Local Variables:
2151553Srgrimes# mode: shell-script
2161553Srgrimes# sh-indentation: 2
2171553Srgrimes# eval: (add-hook 'write-file-hooks 'time-stamp)
21844303Swollman# time-stamp-start: "scriptversion="
2196286Swollman# time-stamp-format: "%:y-%02m-%02d.%02H"
2209490Sphk# time-stamp-time-zone: "UTC"
2218857Srgrimes# time-stamp-end: "; # UTC"
22244303Swollman# End:
2236286Swollman