Blame build-aux/ylwrap

Packit 8f70b4
#! /bin/sh
Packit 8f70b4
# ylwrap - wrapper for lex/yacc invocations.
Packit 8f70b4
Packit 8f70b4
scriptversion=2013-01-12.17; # UTC
Packit 8f70b4
Packit 8f70b4
# Copyright (C) 1996-2014 Free Software Foundation, Inc.
Packit 8f70b4
#
Packit 8f70b4
# Written by Tom Tromey <tromey@cygnus.com>.
Packit 8f70b4
#
Packit 8f70b4
# This program is free software; you can redistribute it and/or modify
Packit 8f70b4
# it under the terms of the GNU General Public License as published by
Packit 8f70b4
# the Free Software Foundation; either version 2, or (at your option)
Packit 8f70b4
# any later version.
Packit 8f70b4
#
Packit 8f70b4
# This program is distributed in the hope that it will be useful,
Packit 8f70b4
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8f70b4
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 8f70b4
# GNU General Public License for more details.
Packit 8f70b4
#
Packit 8f70b4
# You should have received a copy of the GNU General Public License
Packit 8f70b4
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 8f70b4
Packit 8f70b4
# As a special exception to the GNU General Public License, if you
Packit 8f70b4
# distribute this file as part of a program that contains a
Packit 8f70b4
# configuration script generated by Autoconf, you may include it under
Packit 8f70b4
# the same distribution terms that you use for the rest of that program.
Packit 8f70b4
Packit 8f70b4
# This file is maintained in Automake, please report
Packit 8f70b4
# bugs to <bug-automake@gnu.org> or send patches to
Packit 8f70b4
# <automake-patches@gnu.org>.
Packit 8f70b4
Packit 8f70b4
get_dirname ()
Packit 8f70b4
{
Packit 8f70b4
  case $1 in
Packit 8f70b4
    */*|*\\*) printf '%s\n' "$1" | sed -e 's|\([\\/]\)[^\\/]*$|\1|';;
Packit 8f70b4
    # Otherwise,  we want the empty string (not ".").
Packit 8f70b4
  esac
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
# guard FILE
Packit 8f70b4
# ----------
Packit 8f70b4
# The CPP macro used to guard inclusion of FILE.
Packit 8f70b4
guard ()
Packit 8f70b4
{
Packit 8f70b4
  printf '%s\n' "$1"                                                    \
Packit 8f70b4
    | sed                                                               \
Packit 8f70b4
        -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'   \
Packit 8f70b4
        -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'                        \
Packit 8f70b4
        -e 's/__*/_/g'
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
# quote_for_sed [STRING]
Packit 8f70b4
# ----------------------
Packit 8f70b4
# Return STRING (or stdin) quoted to be used as a sed pattern.
Packit 8f70b4
quote_for_sed ()
Packit 8f70b4
{
Packit 8f70b4
  case $# in
Packit 8f70b4
    0) cat;;
Packit 8f70b4
    1) printf '%s\n' "$1";;
Packit 8f70b4
  esac \
Packit 8f70b4
    | sed -e 's|[][\\.*]|\\&|g'
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
case "$1" in
Packit 8f70b4
  '')
Packit 8f70b4
    echo "$0: No files given.  Try '$0 --help' for more information." 1>&2
Packit 8f70b4
    exit 1
Packit 8f70b4
    ;;
Packit 8f70b4
  --basedir)
Packit 8f70b4
    basedir=$2
Packit 8f70b4
    shift 2
Packit 8f70b4
    ;;
Packit 8f70b4
  -h|--h*)
Packit 8f70b4
    cat <<\EOF
Packit 8f70b4
Usage: ylwrap [--help|--version] INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
Packit 8f70b4
Packit 8f70b4
Wrapper for lex/yacc invocations, renaming files as desired.
Packit 8f70b4
Packit 8f70b4
  INPUT is the input file
Packit 8f70b4
  OUTPUT is one file PROG generates
Packit 8f70b4
  DESIRED is the file we actually want instead of OUTPUT
Packit 8f70b4
  PROGRAM is program to run
Packit 8f70b4
  ARGS are passed to PROG
Packit 8f70b4
Packit 8f70b4
Any number of OUTPUT,DESIRED pairs may be used.
Packit 8f70b4
Packit 8f70b4
Report bugs to <bug-automake@gnu.org>.
Packit 8f70b4
EOF
Packit 8f70b4
    exit $?
Packit 8f70b4
    ;;
Packit 8f70b4
  -v|--v*)
Packit 8f70b4
    echo "ylwrap $scriptversion"
Packit 8f70b4
    exit $?
Packit 8f70b4
    ;;
Packit 8f70b4
esac
Packit 8f70b4
Packit 8f70b4
Packit 8f70b4
# The input.
Packit 8f70b4
input=$1
Packit 8f70b4
shift
Packit 8f70b4
# We'll later need for a correct munging of "#line" directives.
Packit 8f70b4
input_sub_rx=`get_dirname "$input" | quote_for_sed`
Packit 8f70b4
case $input in
Packit 8f70b4
  [\\/]* | ?:[\\/]*)
Packit 8f70b4
    # Absolute path; do nothing.
Packit 8f70b4
    ;;
Packit 8f70b4
  *)
Packit 8f70b4
    # Relative path.  Make it absolute.
Packit 8f70b4
    input=`pwd`/$input
Packit 8f70b4
    ;;
Packit 8f70b4
esac
Packit 8f70b4
input_rx=`get_dirname "$input" | quote_for_sed`
Packit 8f70b4
Packit 8f70b4
# Since DOS filename conventions don't allow two dots,
Packit 8f70b4
# the DOS version of Bison writes out y_tab.c instead of y.tab.c
Packit 8f70b4
# and y_tab.h instead of y.tab.h. Test to see if this is the case.
Packit 8f70b4
y_tab_nodot=false
Packit 8f70b4
if test -f y_tab.c || test -f y_tab.h; then
Packit 8f70b4
  y_tab_nodot=true
Packit 8f70b4
fi
Packit 8f70b4
Packit 8f70b4
# The parser itself, the first file, is the destination of the .y.c
Packit 8f70b4
# rule in the Makefile.
Packit 8f70b4
parser=$1
Packit 8f70b4
Packit 8f70b4
# A sed program to s/FROM/TO/g for all the FROM/TO so that, for
Packit 8f70b4
# instance, we rename #include "y.tab.h" into #include "parse.h"
Packit 8f70b4
# during the conversion from y.tab.c to parse.c.
Packit 8f70b4
sed_fix_filenames=
Packit 8f70b4
Packit 8f70b4
# Also rename header guards, as Bison 2.7 for instance uses its header
Packit 8f70b4
# guard in its implementation file.
Packit 8f70b4
sed_fix_header_guards=
Packit 8f70b4
Packit 8f70b4
while test $# -ne 0; do
Packit 8f70b4
  if test x"$1" = x"--"; then
Packit 8f70b4
    shift
Packit 8f70b4
    break
Packit 8f70b4
  fi
Packit 8f70b4
  from=$1
Packit 8f70b4
  # Handle y_tab.c and y_tab.h output by DOS
Packit 8f70b4
  if $y_tab_nodot; then
Packit 8f70b4
    case $from in
Packit 8f70b4
      "y.tab.c") from=y_tab.c;;
Packit 8f70b4
      "y.tab.h") from=y_tab.h;;
Packit 8f70b4
    esac
Packit 8f70b4
  fi
Packit 8f70b4
  shift
Packit 8f70b4
  to=$1
Packit 8f70b4
  shift
Packit 8f70b4
  sed_fix_filenames="${sed_fix_filenames}s|"`quote_for_sed "$from"`"|$to|g;"
Packit 8f70b4
  sed_fix_header_guards="${sed_fix_header_guards}s|"`guard "$from"`"|"`guard "$to"`"|g;"
Packit 8f70b4
done
Packit 8f70b4
Packit 8f70b4
# The program to run.
Packit 8f70b4
prog=$1
Packit 8f70b4
shift
Packit 8f70b4
# Make any relative path in $prog absolute.
Packit 8f70b4
case $prog in
Packit 8f70b4
  [\\/]* | ?:[\\/]*) ;;
Packit 8f70b4
  *[\\/]*) prog=`pwd`/$prog ;;
Packit 8f70b4
esac
Packit 8f70b4
Packit 8f70b4
dirname=ylwrap$$
Packit 8f70b4
do_exit="cd '`pwd`' && rm -rf $dirname > /dev/null 2>&1;"' (exit $ret); exit $ret'
Packit 8f70b4
trap "ret=129; $do_exit" 1
Packit 8f70b4
trap "ret=130; $do_exit" 2
Packit 8f70b4
trap "ret=141; $do_exit" 13
Packit 8f70b4
trap "ret=143; $do_exit" 15
Packit 8f70b4
mkdir $dirname || exit 1
Packit 8f70b4
Packit 8f70b4
cd $dirname
Packit 8f70b4
Packit 8f70b4
case $# in
Packit 8f70b4
  0) "$prog" "$input" ;;
Packit 8f70b4
  *) "$prog" "$@" "$input" ;;
Packit 8f70b4
esac
Packit 8f70b4
ret=$?
Packit 8f70b4
Packit 8f70b4
if test $ret -eq 0; then
Packit 8f70b4
  for from in *
Packit 8f70b4
  do
Packit 8f70b4
    to=`printf '%s\n' "$from" | sed "$sed_fix_filenames"`
Packit 8f70b4
    if test -f "$from"; then
Packit 8f70b4
      # If $2 is an absolute path name, then just use that,
Packit 8f70b4
      # otherwise prepend '../'.
Packit 8f70b4
      case $to in
Packit 8f70b4
        [\\/]* | ?:[\\/]*) target=$to;;
Packit 8f70b4
        *) target=../$to;;
Packit 8f70b4
      esac
Packit 8f70b4
Packit 8f70b4
      # Do not overwrite unchanged header files to avoid useless
Packit 8f70b4
      # recompilations.  Always update the parser itself: it is the
Packit 8f70b4
      # destination of the .y.c rule in the Makefile.  Divert the
Packit 8f70b4
      # output of all other files to a temporary file so we can
Packit 8f70b4
      # compare them to existing versions.
Packit 8f70b4
      if test $from != $parser; then
Packit 8f70b4
        realtarget=$target
Packit 8f70b4
        target=tmp-`printf '%s\n' "$target" | sed 's|.*[\\/]||g'`
Packit 8f70b4
      fi
Packit 8f70b4
Packit 8f70b4
      # Munge "#line" or "#" directives.  Don't let the resulting
Packit 8f70b4
      # debug information point at an absolute srcdir.  Use the real
Packit 8f70b4
      # output file name, not yy.lex.c for instance.  Adjust the
Packit 8f70b4
      # include guards too.
Packit 8f70b4
      sed -e "/^#/!b"                           \
Packit 8f70b4
          -e "s|$input_rx|$input_sub_rx|"       \
Packit 8f70b4
          -e "$sed_fix_filenames"               \
Packit 8f70b4
          -e "$sed_fix_header_guards"           \
Packit 8f70b4
        "$from" >"$target" || ret=$?
Packit 8f70b4
Packit 8f70b4
      # Check whether files must be updated.
Packit 8f70b4
      if test "$from" != "$parser"; then
Packit 8f70b4
        if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
Packit 8f70b4
          echo "$to is unchanged"
Packit 8f70b4
          rm -f "$target"
Packit 8f70b4
        else
Packit 8f70b4
          echo "updating $to"
Packit 8f70b4
          mv -f "$target" "$realtarget"
Packit 8f70b4
        fi
Packit 8f70b4
      fi
Packit 8f70b4
    else
Packit 8f70b4
      # A missing file is only an error for the parser.  This is a
Packit 8f70b4
      # blatant hack to let us support using "yacc -d".  If -d is not
Packit 8f70b4
      # specified, don't fail when the header file is "missing".
Packit 8f70b4
      if test "$from" = "$parser"; then
Packit 8f70b4
        ret=1
Packit 8f70b4
      fi
Packit 8f70b4
    fi
Packit 8f70b4
  done
Packit 8f70b4
fi
Packit 8f70b4
Packit 8f70b4
# Remove the directory.
Packit 8f70b4
cd ..
Packit 8f70b4
rm -rf $dirname
Packit 8f70b4
Packit 8f70b4
exit $ret
Packit 8f70b4
Packit 8f70b4
# Local Variables:
Packit 8f70b4
# mode: shell-script
Packit 8f70b4
# sh-indentation: 2
Packit 8f70b4
# eval: (add-hook 'write-file-hooks 'time-stamp)
Packit 8f70b4
# time-stamp-start: "scriptversion="
Packit 8f70b4
# time-stamp-format: "%:y-%02m-%02d.%02H"
Packit 8f70b4
# time-stamp-time-zone: "UTC"
Packit 8f70b4
# time-stamp-end: "; # UTC"
Packit 8f70b4
# End: