Blame ylwrap

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