Blame ylwrap

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