Blame build-aux/ylwrap

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