xref: /dflybsd-src/contrib/gmp/ylwrap (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino#! /bin/sh
286d7f5d3SJohn Marino# ylwrap - wrapper for lex/yacc invocations.
386d7f5d3SJohn Marino
486d7f5d3SJohn Marinoscriptversion=2009-04-28.21; # UTC
586d7f5d3SJohn Marino
686d7f5d3SJohn Marino# Copyright (C) 1996, 1997, 1998, 1999, 2001, 2002, 2003, 2004, 2005,
786d7f5d3SJohn Marino# 2007, 2009 Free Software Foundation, Inc.
886d7f5d3SJohn Marino#
986d7f5d3SJohn Marino# Written by Tom Tromey <tromey@cygnus.com>.
1086d7f5d3SJohn Marino#
1186d7f5d3SJohn Marino# This program is free software; you can redistribute it and/or modify
1286d7f5d3SJohn Marino# it under the terms of the GNU General Public License as published by
1386d7f5d3SJohn Marino# the Free Software Foundation; either version 2, or (at your option)
1486d7f5d3SJohn Marino# any later version.
1586d7f5d3SJohn Marino#
1686d7f5d3SJohn Marino# This program is distributed in the hope that it will be useful,
1786d7f5d3SJohn Marino# but WITHOUT ANY WARRANTY; without even the implied warranty of
1886d7f5d3SJohn Marino# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1986d7f5d3SJohn Marino# GNU General Public License for more details.
2086d7f5d3SJohn Marino#
2186d7f5d3SJohn Marino# You should have received a copy of the GNU General Public License
2286d7f5d3SJohn Marino# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2386d7f5d3SJohn Marino
2486d7f5d3SJohn Marino# As a special exception to the GNU General Public License, if you
2586d7f5d3SJohn Marino# distribute this file as part of a program that contains a
2686d7f5d3SJohn Marino# configuration script generated by Autoconf, you may include it under
2786d7f5d3SJohn Marino# the same distribution terms that you use for the rest of that program.
2886d7f5d3SJohn Marino
2986d7f5d3SJohn Marino# This file is maintained in Automake, please report
3086d7f5d3SJohn Marino# bugs to <bug-automake@gnu.org> or send patches to
3186d7f5d3SJohn Marino# <automake-patches@gnu.org>.
3286d7f5d3SJohn Marino
3386d7f5d3SJohn Marinocase "$1" in
3486d7f5d3SJohn Marino  '')
3586d7f5d3SJohn Marino    echo "$0: No files given.  Try \`$0 --help' for more information." 1>&2
3686d7f5d3SJohn Marino    exit 1
3786d7f5d3SJohn Marino    ;;
3886d7f5d3SJohn Marino  --basedir)
3986d7f5d3SJohn Marino    basedir=$2
4086d7f5d3SJohn Marino    shift 2
4186d7f5d3SJohn Marino    ;;
4286d7f5d3SJohn Marino  -h|--h*)
4386d7f5d3SJohn Marino    cat <<\EOF
4486d7f5d3SJohn MarinoUsage: ylwrap [--help|--version] INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
4586d7f5d3SJohn Marino
4686d7f5d3SJohn MarinoWrapper for lex/yacc invocations, renaming files as desired.
4786d7f5d3SJohn Marino
4886d7f5d3SJohn Marino  INPUT is the input file
4986d7f5d3SJohn Marino  OUTPUT is one file PROG generates
5086d7f5d3SJohn Marino  DESIRED is the file we actually want instead of OUTPUT
5186d7f5d3SJohn Marino  PROGRAM is program to run
5286d7f5d3SJohn Marino  ARGS are passed to PROG
5386d7f5d3SJohn Marino
5486d7f5d3SJohn MarinoAny number of OUTPUT,DESIRED pairs may be used.
5586d7f5d3SJohn Marino
5686d7f5d3SJohn MarinoReport bugs to <bug-automake@gnu.org>.
5786d7f5d3SJohn MarinoEOF
5886d7f5d3SJohn Marino    exit $?
5986d7f5d3SJohn Marino    ;;
6086d7f5d3SJohn Marino  -v|--v*)
6186d7f5d3SJohn Marino    echo "ylwrap $scriptversion"
6286d7f5d3SJohn Marino    exit $?
6386d7f5d3SJohn Marino    ;;
6486d7f5d3SJohn Marinoesac
6586d7f5d3SJohn Marino
6686d7f5d3SJohn Marino
6786d7f5d3SJohn Marino# The input.
6886d7f5d3SJohn Marinoinput="$1"
6986d7f5d3SJohn Marinoshift
7086d7f5d3SJohn Marinocase "$input" in
7186d7f5d3SJohn Marino  [\\/]* | ?:[\\/]*)
7286d7f5d3SJohn Marino    # Absolute path; do nothing.
7386d7f5d3SJohn Marino    ;;
7486d7f5d3SJohn Marino  *)
7586d7f5d3SJohn Marino    # Relative path.  Make it absolute.
7686d7f5d3SJohn Marino    input="`pwd`/$input"
7786d7f5d3SJohn Marino    ;;
7886d7f5d3SJohn Marinoesac
7986d7f5d3SJohn Marino
8086d7f5d3SJohn Marinopairlist=
8186d7f5d3SJohn Marinowhile test "$#" -ne 0; do
8286d7f5d3SJohn Marino  if test "$1" = "--"; then
8386d7f5d3SJohn Marino    shift
8486d7f5d3SJohn Marino    break
8586d7f5d3SJohn Marino  fi
8686d7f5d3SJohn Marino  pairlist="$pairlist $1"
8786d7f5d3SJohn Marino  shift
8886d7f5d3SJohn Marinodone
8986d7f5d3SJohn Marino
9086d7f5d3SJohn Marino# The program to run.
9186d7f5d3SJohn Marinoprog="$1"
9286d7f5d3SJohn Marinoshift
9386d7f5d3SJohn Marino# Make any relative path in $prog absolute.
9486d7f5d3SJohn Marinocase "$prog" in
9586d7f5d3SJohn Marino  [\\/]* | ?:[\\/]*) ;;
9686d7f5d3SJohn Marino  *[\\/]*) prog="`pwd`/$prog" ;;
9786d7f5d3SJohn Marinoesac
9886d7f5d3SJohn Marino
9986d7f5d3SJohn Marino# FIXME: add hostname here for parallel makes that run commands on
10086d7f5d3SJohn Marino# other machines.  But that might take us over the 14-char limit.
10186d7f5d3SJohn Marinodirname=ylwrap$$
10286d7f5d3SJohn Marinotrap "cd '`pwd`'; rm -rf $dirname > /dev/null 2>&1" 1 2 3 15
10386d7f5d3SJohn Marinomkdir $dirname || exit 1
10486d7f5d3SJohn Marino
10586d7f5d3SJohn Marinocd $dirname
10686d7f5d3SJohn Marino
10786d7f5d3SJohn Marinocase $# in
10886d7f5d3SJohn Marino  0) "$prog" "$input" ;;
10986d7f5d3SJohn Marino  *) "$prog" "$@" "$input" ;;
11086d7f5d3SJohn Marinoesac
11186d7f5d3SJohn Marinoret=$?
11286d7f5d3SJohn Marino
11386d7f5d3SJohn Marinoif test $ret -eq 0; then
11486d7f5d3SJohn Marino  set X $pairlist
11586d7f5d3SJohn Marino  shift
11686d7f5d3SJohn Marino  first=yes
11786d7f5d3SJohn Marino  # Since DOS filename conventions don't allow two dots,
11886d7f5d3SJohn Marino  # the DOS version of Bison writes out y_tab.c instead of y.tab.c
11986d7f5d3SJohn Marino  # and y_tab.h instead of y.tab.h. Test to see if this is the case.
12086d7f5d3SJohn Marino  y_tab_nodot="no"
12186d7f5d3SJohn Marino  if test -f y_tab.c || test -f y_tab.h; then
12286d7f5d3SJohn Marino    y_tab_nodot="yes"
12386d7f5d3SJohn Marino  fi
12486d7f5d3SJohn Marino
12586d7f5d3SJohn Marino  # The directory holding the input.
12686d7f5d3SJohn Marino  input_dir=`echo "$input" | sed -e 's,\([\\/]\)[^\\/]*$,\1,'`
12786d7f5d3SJohn Marino  # Quote $INPUT_DIR so we can use it in a regexp.
12886d7f5d3SJohn Marino  # FIXME: really we should care about more than `.' and `\'.
12986d7f5d3SJohn Marino  input_rx=`echo "$input_dir" | sed 's,\\\\,\\\\\\\\,g;s,\\.,\\\\.,g'`
13086d7f5d3SJohn Marino
13186d7f5d3SJohn Marino  while test "$#" -ne 0; do
13286d7f5d3SJohn Marino    from="$1"
13386d7f5d3SJohn Marino    # Handle y_tab.c and y_tab.h output by DOS
13486d7f5d3SJohn Marino    if test $y_tab_nodot = "yes"; then
13586d7f5d3SJohn Marino      if test $from = "y.tab.c"; then
13686d7f5d3SJohn Marino    	from="y_tab.c"
13786d7f5d3SJohn Marino      else
13886d7f5d3SJohn Marino    	if test $from = "y.tab.h"; then
13986d7f5d3SJohn Marino    	  from="y_tab.h"
14086d7f5d3SJohn Marino    	fi
14186d7f5d3SJohn Marino      fi
14286d7f5d3SJohn Marino    fi
14386d7f5d3SJohn Marino    if test -f "$from"; then
14486d7f5d3SJohn Marino      # If $2 is an absolute path name, then just use that,
14586d7f5d3SJohn Marino      # otherwise prepend `../'.
14686d7f5d3SJohn Marino      case "$2" in
14786d7f5d3SJohn Marino    	[\\/]* | ?:[\\/]*) target="$2";;
14886d7f5d3SJohn Marino    	*) target="../$2";;
14986d7f5d3SJohn Marino      esac
15086d7f5d3SJohn Marino
15186d7f5d3SJohn Marino      # We do not want to overwrite a header file if it hasn't
15286d7f5d3SJohn Marino      # changed.  This avoid useless recompilations.  However the
15386d7f5d3SJohn Marino      # parser itself (the first file) should always be updated,
15486d7f5d3SJohn Marino      # because it is the destination of the .y.c rule in the
15586d7f5d3SJohn Marino      # Makefile.  Divert the output of all other files to a temporary
15686d7f5d3SJohn Marino      # file so we can compare them to existing versions.
15786d7f5d3SJohn Marino      if test $first = no; then
15886d7f5d3SJohn Marino	realtarget="$target"
15986d7f5d3SJohn Marino	target="tmp-`echo $target | sed s/.*[\\/]//g`"
16086d7f5d3SJohn Marino      fi
16186d7f5d3SJohn Marino      # Edit out `#line' or `#' directives.
16286d7f5d3SJohn Marino      #
16386d7f5d3SJohn Marino      # We don't want the resulting debug information to point at
16486d7f5d3SJohn Marino      # an absolute srcdir; it is better for it to just mention the
16586d7f5d3SJohn Marino      # .y file with no path.
16686d7f5d3SJohn Marino      #
16786d7f5d3SJohn Marino      # We want to use the real output file name, not yy.lex.c for
16886d7f5d3SJohn Marino      # instance.
16986d7f5d3SJohn Marino      #
17086d7f5d3SJohn Marino      # We want the include guards to be adjusted too.
17186d7f5d3SJohn Marino      FROM=`echo "$from" | sed \
17286d7f5d3SJohn Marino            -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
17386d7f5d3SJohn Marino            -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`
17486d7f5d3SJohn Marino      TARGET=`echo "$2" | sed \
17586d7f5d3SJohn Marino            -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
17686d7f5d3SJohn Marino            -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`
17786d7f5d3SJohn Marino
17886d7f5d3SJohn Marino      sed -e "/^#/!b" -e "s,$input_rx,," -e "s,$from,$2," \
17986d7f5d3SJohn Marino          -e "s,$FROM,$TARGET," "$from" >"$target" || ret=$?
18086d7f5d3SJohn Marino
18186d7f5d3SJohn Marino      # Check whether header files must be updated.
18286d7f5d3SJohn Marino      if test $first = no; then
18386d7f5d3SJohn Marino	if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
18486d7f5d3SJohn Marino	  echo "$2" is unchanged
18586d7f5d3SJohn Marino	  rm -f "$target"
18686d7f5d3SJohn Marino	else
18786d7f5d3SJohn Marino          echo updating "$2"
18886d7f5d3SJohn Marino          mv -f "$target" "$realtarget"
18986d7f5d3SJohn Marino        fi
19086d7f5d3SJohn Marino      fi
19186d7f5d3SJohn Marino    else
19286d7f5d3SJohn Marino      # A missing file is only an error for the first file.  This
19386d7f5d3SJohn Marino      # is a blatant hack to let us support using "yacc -d".  If -d
19486d7f5d3SJohn Marino      # is not specified, we don't want an error when the header
19586d7f5d3SJohn Marino      # file is "missing".
19686d7f5d3SJohn Marino      if test $first = yes; then
19786d7f5d3SJohn Marino        ret=1
19886d7f5d3SJohn Marino      fi
19986d7f5d3SJohn Marino    fi
20086d7f5d3SJohn Marino    shift
20186d7f5d3SJohn Marino    shift
20286d7f5d3SJohn Marino    first=no
20386d7f5d3SJohn Marino  done
20486d7f5d3SJohn Marinoelse
20586d7f5d3SJohn Marino  ret=$?
20686d7f5d3SJohn Marinofi
20786d7f5d3SJohn Marino
20886d7f5d3SJohn Marino# Remove the directory.
20986d7f5d3SJohn Marinocd ..
21086d7f5d3SJohn Marinorm -rf $dirname
21186d7f5d3SJohn Marino
21286d7f5d3SJohn Marinoexit $ret
21386d7f5d3SJohn Marino
21486d7f5d3SJohn Marino# Local Variables:
21586d7f5d3SJohn Marino# mode: shell-script
21686d7f5d3SJohn Marino# sh-indentation: 2
21786d7f5d3SJohn Marino# eval: (add-hook 'write-file-hooks 'time-stamp)
21886d7f5d3SJohn Marino# time-stamp-start: "scriptversion="
21986d7f5d3SJohn Marino# time-stamp-format: "%:y-%02m-%02d.%02H"
22086d7f5d3SJohn Marino# time-stamp-time-zone: "UTC"
22186d7f5d3SJohn Marino# time-stamp-end: "; # UTC"
22286d7f5d3SJohn Marino# End:
223