ylwrap revision 05b261ec
105b261ecSmrg#! /bin/sh
205b261ecSmrg# ylwrap - wrapper for lex/yacc invocations.
305b261ecSmrg
405b261ecSmrgscriptversion=2007-11-22.22
505b261ecSmrg
605b261ecSmrg# Copyright (C) 1996, 1997, 1998, 1999, 2001, 2002, 2003, 2004, 2005,
705b261ecSmrg# 2007  Free Software Foundation, Inc.
805b261ecSmrg#
905b261ecSmrg# Written by Tom Tromey <tromey@cygnus.com>.
1005b261ecSmrg#
1105b261ecSmrg# This program is free software; you can redistribute it and/or modify
1205b261ecSmrg# it under the terms of the GNU General Public License as published by
1305b261ecSmrg# the Free Software Foundation; either version 2, or (at your option)
1405b261ecSmrg# any later version.
1505b261ecSmrg#
1605b261ecSmrg# This program is distributed in the hope that it will be useful,
1705b261ecSmrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
1805b261ecSmrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1905b261ecSmrg# GNU General Public License for more details.
2005b261ecSmrg#
2105b261ecSmrg# You should have received a copy of the GNU General Public License
2205b261ecSmrg# along with this program; if not, write to the Free Software
2305b261ecSmrg# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
2405b261ecSmrg# 02110-1301, USA.
2505b261ecSmrg
2605b261ecSmrg# As a special exception to the GNU General Public License, if you
2705b261ecSmrg# distribute this file as part of a program that contains a
2805b261ecSmrg# configuration script generated by Autoconf, you may include it under
2905b261ecSmrg# the same distribution terms that you use for the rest of that program.
3005b261ecSmrg
3105b261ecSmrg# This file is maintained in Automake, please report
3205b261ecSmrg# bugs to <bug-automake@gnu.org> or send patches to
3305b261ecSmrg# <automake-patches@gnu.org>.
3405b261ecSmrg
3505b261ecSmrgcase "$1" in
3605b261ecSmrg  '')
3705b261ecSmrg    echo "$0: No files given.  Try \`$0 --help' for more information." 1>&2
3805b261ecSmrg    exit 1
3905b261ecSmrg    ;;
4005b261ecSmrg  --basedir)
4105b261ecSmrg    basedir=$2
4205b261ecSmrg    shift 2
4305b261ecSmrg    ;;
4405b261ecSmrg  -h|--h*)
4505b261ecSmrg    cat <<\EOF
4605b261ecSmrgUsage: ylwrap [--help|--version] INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
4705b261ecSmrg
4805b261ecSmrgWrapper for lex/yacc invocations, renaming files as desired.
4905b261ecSmrg
5005b261ecSmrg  INPUT is the input file
5105b261ecSmrg  OUTPUT is one file PROG generates
5205b261ecSmrg  DESIRED is the file we actually want instead of OUTPUT
5305b261ecSmrg  PROGRAM is program to run
5405b261ecSmrg  ARGS are passed to PROG
5505b261ecSmrg
5605b261ecSmrgAny number of OUTPUT,DESIRED pairs may be used.
5705b261ecSmrg
5805b261ecSmrgReport bugs to <bug-automake@gnu.org>.
5905b261ecSmrgEOF
6005b261ecSmrg    exit $?
6105b261ecSmrg    ;;
6205b261ecSmrg  -v|--v*)
6305b261ecSmrg    echo "ylwrap $scriptversion"
6405b261ecSmrg    exit $?
6505b261ecSmrg    ;;
6605b261ecSmrgesac
6705b261ecSmrg
6805b261ecSmrg
6905b261ecSmrg# The input.
7005b261ecSmrginput="$1"
7105b261ecSmrgshift
7205b261ecSmrgcase "$input" in
7305b261ecSmrg  [\\/]* | ?:[\\/]*)
7405b261ecSmrg    # Absolute path; do nothing.
7505b261ecSmrg    ;;
7605b261ecSmrg  *)
7705b261ecSmrg    # Relative path.  Make it absolute.
7805b261ecSmrg    input="`pwd`/$input"
7905b261ecSmrg    ;;
8005b261ecSmrgesac
8105b261ecSmrg
8205b261ecSmrgpairlist=
8305b261ecSmrgwhile test "$#" -ne 0; do
8405b261ecSmrg  if test "$1" = "--"; then
8505b261ecSmrg    shift
8605b261ecSmrg    break
8705b261ecSmrg  fi
8805b261ecSmrg  pairlist="$pairlist $1"
8905b261ecSmrg  shift
9005b261ecSmrgdone
9105b261ecSmrg
9205b261ecSmrg# The program to run.
9305b261ecSmrgprog="$1"
9405b261ecSmrgshift
9505b261ecSmrg# Make any relative path in $prog absolute.
9605b261ecSmrgcase "$prog" in
9705b261ecSmrg  [\\/]* | ?:[\\/]*) ;;
9805b261ecSmrg  *[\\/]*) prog="`pwd`/$prog" ;;
9905b261ecSmrgesac
10005b261ecSmrg
10105b261ecSmrg# FIXME: add hostname here for parallel makes that run commands on
10205b261ecSmrg# other machines.  But that might take us over the 14-char limit.
10305b261ecSmrgdirname=ylwrap$$
10405b261ecSmrgtrap "cd '`pwd`'; rm -rf $dirname > /dev/null 2>&1" 1 2 3 15
10505b261ecSmrgmkdir $dirname || exit 1
10605b261ecSmrg
10705b261ecSmrgcd $dirname
10805b261ecSmrg
10905b261ecSmrgcase $# in
11005b261ecSmrg  0) "$prog" "$input" ;;
11105b261ecSmrg  *) "$prog" "$@" "$input" ;;
11205b261ecSmrgesac
11305b261ecSmrgret=$?
11405b261ecSmrg
11505b261ecSmrgif test $ret -eq 0; then
11605b261ecSmrg  set X $pairlist
11705b261ecSmrg  shift
11805b261ecSmrg  first=yes
11905b261ecSmrg  # Since DOS filename conventions don't allow two dots,
12005b261ecSmrg  # the DOS version of Bison writes out y_tab.c instead of y.tab.c
12105b261ecSmrg  # and y_tab.h instead of y.tab.h. Test to see if this is the case.
12205b261ecSmrg  y_tab_nodot="no"
12305b261ecSmrg  if test -f y_tab.c || test -f y_tab.h; then
12405b261ecSmrg    y_tab_nodot="yes"
12505b261ecSmrg  fi
12605b261ecSmrg
12705b261ecSmrg  # The directory holding the input.
12805b261ecSmrg  input_dir=`echo "$input" | sed -e 's,\([\\/]\)[^\\/]*$,\1,'`
12905b261ecSmrg  # Quote $INPUT_DIR so we can use it in a regexp.
13005b261ecSmrg  # FIXME: really we should care about more than `.' and `\'.
13105b261ecSmrg  input_rx=`echo "$input_dir" | sed 's,\\\\,\\\\\\\\,g;s,\\.,\\\\.,g'`
13205b261ecSmrg
13305b261ecSmrg  while test "$#" -ne 0; do
13405b261ecSmrg    from="$1"
13505b261ecSmrg    # Handle y_tab.c and y_tab.h output by DOS
13605b261ecSmrg    if test $y_tab_nodot = "yes"; then
13705b261ecSmrg      if test $from = "y.tab.c"; then
13805b261ecSmrg    	from="y_tab.c"
13905b261ecSmrg      else
14005b261ecSmrg    	if test $from = "y.tab.h"; then
14105b261ecSmrg    	  from="y_tab.h"
14205b261ecSmrg    	fi
14305b261ecSmrg      fi
14405b261ecSmrg    fi
14505b261ecSmrg    if test -f "$from"; then
14605b261ecSmrg      # If $2 is an absolute path name, then just use that,
14705b261ecSmrg      # otherwise prepend `../'.
14805b261ecSmrg      case "$2" in
14905b261ecSmrg    	[\\/]* | ?:[\\/]*) target="$2";;
15005b261ecSmrg    	*) target="../$2";;
15105b261ecSmrg      esac
15205b261ecSmrg
15305b261ecSmrg      # We do not want to overwrite a header file if it hasn't
15405b261ecSmrg      # changed.  This avoid useless recompilations.  However the
15505b261ecSmrg      # parser itself (the first file) should always be updated,
15605b261ecSmrg      # because it is the destination of the .y.c rule in the
15705b261ecSmrg      # Makefile.  Divert the output of all other files to a temporary
15805b261ecSmrg      # file so we can compare them to existing versions.
15905b261ecSmrg      if test $first = no; then
16005b261ecSmrg	realtarget="$target"
16105b261ecSmrg	target="tmp-`echo $target | sed s/.*[\\/]//g`"
16205b261ecSmrg      fi
16305b261ecSmrg      # Edit out `#line' or `#' directives.
16405b261ecSmrg      #
16505b261ecSmrg      # We don't want the resulting debug information to point at
16605b261ecSmrg      # an absolute srcdir; it is better for it to just mention the
16705b261ecSmrg      # .y file with no path.
16805b261ecSmrg      #
16905b261ecSmrg      # We want to use the real output file name, not yy.lex.c for
17005b261ecSmrg      # instance.
17105b261ecSmrg      #
17205b261ecSmrg      # We want the include guards to be adjusted too.
17305b261ecSmrg      FROM=`echo "$from" | sed \
17405b261ecSmrg            -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
17505b261ecSmrg            -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`
17605b261ecSmrg      TARGET=`echo "$2" | sed \
17705b261ecSmrg            -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
17805b261ecSmrg            -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`
17905b261ecSmrg
18005b261ecSmrg      sed -e "/^#/!b" -e "s,$input_rx,," -e "s,$from,$2," \
18105b261ecSmrg          -e "s,$FROM,$TARGET," "$from" >"$target" || ret=$?
18205b261ecSmrg
18305b261ecSmrg      # Check whether header files must be updated.
18405b261ecSmrg      if test $first = no; then
18505b261ecSmrg	if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
18605b261ecSmrg	  echo "$2" is unchanged
18705b261ecSmrg	  rm -f "$target"
18805b261ecSmrg	else
18905b261ecSmrg          echo updating "$2"
19005b261ecSmrg          mv -f "$target" "$realtarget"
19105b261ecSmrg        fi
19205b261ecSmrg      fi
19305b261ecSmrg    else
19405b261ecSmrg      # A missing file is only an error for the first file.  This
19505b261ecSmrg      # is a blatant hack to let us support using "yacc -d".  If -d
19605b261ecSmrg      # is not specified, we don't want an error when the header
19705b261ecSmrg      # file is "missing".
19805b261ecSmrg      if test $first = yes; then
19905b261ecSmrg        ret=1
20005b261ecSmrg      fi
20105b261ecSmrg    fi
20205b261ecSmrg    shift
20305b261ecSmrg    shift
20405b261ecSmrg    first=no
20505b261ecSmrg  done
20605b261ecSmrgelse
20705b261ecSmrg  ret=$?
20805b261ecSmrgfi
20905b261ecSmrg
21005b261ecSmrg# Remove the directory.
21105b261ecSmrgcd ..
21205b261ecSmrgrm -rf $dirname
21305b261ecSmrg
21405b261ecSmrgexit $ret
21505b261ecSmrg
21605b261ecSmrg# Local Variables:
21705b261ecSmrg# mode: shell-script
21805b261ecSmrg# sh-indentation: 2
21905b261ecSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
22005b261ecSmrg# time-stamp-start: "scriptversion="
22105b261ecSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
22205b261ecSmrg# time-stamp-end: "$"
22305b261ecSmrg# End:
224