Blame ylwrap

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