Blame config/ylwrap

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