ylwrap revision 1b5d61b8
11b5d61b8Smrg#!/bin/sh
205b261ecSmrg# ylwrap - wrapper for lex/yacc invocations.
305b261ecSmrg
41b5d61b8Smrgscriptversion=2016-01-11.22; # UTC
505b261ecSmrg
61b5d61b8Smrg# Copyright (C) 1996-2017 Free Software Foundation, Inc.
705b261ecSmrg#
805b261ecSmrg# Written by Tom Tromey <tromey@cygnus.com>.
905b261ecSmrg#
1005b261ecSmrg# This program is free software; you can redistribute it and/or modify
1105b261ecSmrg# it under the terms of the GNU General Public License as published by
1205b261ecSmrg# the Free Software Foundation; either version 2, or (at your option)
1305b261ecSmrg# any later version.
1405b261ecSmrg#
1505b261ecSmrg# This program is distributed in the hope that it will be useful,
1605b261ecSmrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
1705b261ecSmrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1805b261ecSmrg# GNU General Public License for more details.
1905b261ecSmrg#
2005b261ecSmrg# You should have received a copy of the GNU General Public License
2135c4bbdfSmrg# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2205b261ecSmrg
2305b261ecSmrg# As a special exception to the GNU General Public License, if you
2405b261ecSmrg# distribute this file as part of a program that contains a
2505b261ecSmrg# configuration script generated by Autoconf, you may include it under
2605b261ecSmrg# the same distribution terms that you use for the rest of that program.
2705b261ecSmrg
2805b261ecSmrg# This file is maintained in Automake, please report
2905b261ecSmrg# bugs to <bug-automake@gnu.org> or send patches to
3005b261ecSmrg# <automake-patches@gnu.org>.
3105b261ecSmrg
3235c4bbdfSmrgget_dirname ()
3335c4bbdfSmrg{
3435c4bbdfSmrg  case $1 in
3535c4bbdfSmrg    */*|*\\*) printf '%s\n' "$1" | sed -e 's|\([\\/]\)[^\\/]*$|\1|';;
3635c4bbdfSmrg    # Otherwise,  we want the empty string (not ".").
3735c4bbdfSmrg  esac
3835c4bbdfSmrg}
3935c4bbdfSmrg
4035c4bbdfSmrg# guard FILE
4135c4bbdfSmrg# ----------
4235c4bbdfSmrg# The CPP macro used to guard inclusion of FILE.
4335c4bbdfSmrgguard ()
4435c4bbdfSmrg{
4535c4bbdfSmrg  printf '%s\n' "$1"                                                    \
4635c4bbdfSmrg    | sed                                                               \
4735c4bbdfSmrg        -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'   \
4835c4bbdfSmrg        -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'                        \
4935c4bbdfSmrg        -e 's/__*/_/g'
5035c4bbdfSmrg}
5135c4bbdfSmrg
5235c4bbdfSmrg# quote_for_sed [STRING]
5335c4bbdfSmrg# ----------------------
5435c4bbdfSmrg# Return STRING (or stdin) quoted to be used as a sed pattern.
5535c4bbdfSmrgquote_for_sed ()
5635c4bbdfSmrg{
5735c4bbdfSmrg  case $# in
5835c4bbdfSmrg    0) cat;;
5935c4bbdfSmrg    1) printf '%s\n' "$1";;
6035c4bbdfSmrg  esac \
6135c4bbdfSmrg    | sed -e 's|[][\\.*]|\\&|g'
6235c4bbdfSmrg}
6335c4bbdfSmrg
6405b261ecSmrgcase "$1" in
6505b261ecSmrg  '')
6635c4bbdfSmrg    echo "$0: No files given.  Try '$0 --help' for more information." 1>&2
6705b261ecSmrg    exit 1
6805b261ecSmrg    ;;
6905b261ecSmrg  --basedir)
7005b261ecSmrg    basedir=$2
7105b261ecSmrg    shift 2
7205b261ecSmrg    ;;
7305b261ecSmrg  -h|--h*)
7405b261ecSmrg    cat <<\EOF
7505b261ecSmrgUsage: ylwrap [--help|--version] INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
7605b261ecSmrg
7705b261ecSmrgWrapper for lex/yacc invocations, renaming files as desired.
7805b261ecSmrg
7905b261ecSmrg  INPUT is the input file
8005b261ecSmrg  OUTPUT is one file PROG generates
8105b261ecSmrg  DESIRED is the file we actually want instead of OUTPUT
8205b261ecSmrg  PROGRAM is program to run
8305b261ecSmrg  ARGS are passed to PROG
8405b261ecSmrg
8505b261ecSmrgAny number of OUTPUT,DESIRED pairs may be used.
8605b261ecSmrg
8705b261ecSmrgReport bugs to <bug-automake@gnu.org>.
8805b261ecSmrgEOF
8905b261ecSmrg    exit $?
9005b261ecSmrg    ;;
9105b261ecSmrg  -v|--v*)
9205b261ecSmrg    echo "ylwrap $scriptversion"
9305b261ecSmrg    exit $?
9405b261ecSmrg    ;;
9505b261ecSmrgesac
9605b261ecSmrg
9705b261ecSmrg
9805b261ecSmrg# The input.
9935c4bbdfSmrginput=$1
10005b261ecSmrgshift
10135c4bbdfSmrg# We'll later need for a correct munging of "#line" directives.
10235c4bbdfSmrginput_sub_rx=`get_dirname "$input" | quote_for_sed`
10335c4bbdfSmrgcase $input in
10405b261ecSmrg  [\\/]* | ?:[\\/]*)
10505b261ecSmrg    # Absolute path; do nothing.
10605b261ecSmrg    ;;
10705b261ecSmrg  *)
10805b261ecSmrg    # Relative path.  Make it absolute.
10935c4bbdfSmrg    input=`pwd`/$input
11005b261ecSmrg    ;;
11105b261ecSmrgesac
11235c4bbdfSmrginput_rx=`get_dirname "$input" | quote_for_sed`
11335c4bbdfSmrg
11435c4bbdfSmrg# Since DOS filename conventions don't allow two dots,
11535c4bbdfSmrg# the DOS version of Bison writes out y_tab.c instead of y.tab.c
11635c4bbdfSmrg# and y_tab.h instead of y.tab.h. Test to see if this is the case.
11735c4bbdfSmrgy_tab_nodot=false
11835c4bbdfSmrgif test -f y_tab.c || test -f y_tab.h; then
11935c4bbdfSmrg  y_tab_nodot=true
12035c4bbdfSmrgfi
12135c4bbdfSmrg
12235c4bbdfSmrg# The parser itself, the first file, is the destination of the .y.c
12335c4bbdfSmrg# rule in the Makefile.
12435c4bbdfSmrgparser=$1
12535c4bbdfSmrg
12635c4bbdfSmrg# A sed program to s/FROM/TO/g for all the FROM/TO so that, for
12735c4bbdfSmrg# instance, we rename #include "y.tab.h" into #include "parse.h"
12835c4bbdfSmrg# during the conversion from y.tab.c to parse.c.
12935c4bbdfSmrgsed_fix_filenames=
13005b261ecSmrg
13135c4bbdfSmrg# Also rename header guards, as Bison 2.7 for instance uses its header
13235c4bbdfSmrg# guard in its implementation file.
13335c4bbdfSmrgsed_fix_header_guards=
13435c4bbdfSmrg
13535c4bbdfSmrgwhile test $# -ne 0; do
13635c4bbdfSmrg  if test x"$1" = x"--"; then
13705b261ecSmrg    shift
13805b261ecSmrg    break
13905b261ecSmrg  fi
14035c4bbdfSmrg  from=$1
14135c4bbdfSmrg  # Handle y_tab.c and y_tab.h output by DOS
14235c4bbdfSmrg  if $y_tab_nodot; then
14335c4bbdfSmrg    case $from in
14435c4bbdfSmrg      "y.tab.c") from=y_tab.c;;
14535c4bbdfSmrg      "y.tab.h") from=y_tab.h;;
14635c4bbdfSmrg    esac
14735c4bbdfSmrg  fi
14805b261ecSmrg  shift
14935c4bbdfSmrg  to=$1
15035c4bbdfSmrg  shift
15135c4bbdfSmrg  sed_fix_filenames="${sed_fix_filenames}s|"`quote_for_sed "$from"`"|$to|g;"
15235c4bbdfSmrg  sed_fix_header_guards="${sed_fix_header_guards}s|"`guard "$from"`"|"`guard "$to"`"|g;"
15305b261ecSmrgdone
15405b261ecSmrg
15505b261ecSmrg# The program to run.
15635c4bbdfSmrgprog=$1
15705b261ecSmrgshift
15805b261ecSmrg# Make any relative path in $prog absolute.
15935c4bbdfSmrgcase $prog in
16005b261ecSmrg  [\\/]* | ?:[\\/]*) ;;
16135c4bbdfSmrg  *[\\/]*) prog=`pwd`/$prog ;;
16205b261ecSmrgesac
16305b261ecSmrg
16405b261ecSmrgdirname=ylwrap$$
16535c4bbdfSmrgdo_exit="cd '`pwd`' && rm -rf $dirname > /dev/null 2>&1;"' (exit $ret); exit $ret'
16635c4bbdfSmrgtrap "ret=129; $do_exit" 1
16735c4bbdfSmrgtrap "ret=130; $do_exit" 2
16835c4bbdfSmrgtrap "ret=141; $do_exit" 13
16935c4bbdfSmrgtrap "ret=143; $do_exit" 15
17005b261ecSmrgmkdir $dirname || exit 1
17105b261ecSmrg
17205b261ecSmrgcd $dirname
17305b261ecSmrg
17405b261ecSmrgcase $# in
17535c4bbdfSmrg  0) "$prog" "$input" ;;
17635c4bbdfSmrg  *) "$prog" "$@" "$input" ;;
17705b261ecSmrgesac
17805b261ecSmrgret=$?
17905b261ecSmrg
18005b261ecSmrgif test $ret -eq 0; then
18135c4bbdfSmrg  for from in *
18235c4bbdfSmrg  do
18335c4bbdfSmrg    to=`printf '%s\n' "$from" | sed "$sed_fix_filenames"`
18405b261ecSmrg    if test -f "$from"; then
18505b261ecSmrg      # If $2 is an absolute path name, then just use that,
18635c4bbdfSmrg      # otherwise prepend '../'.
18735c4bbdfSmrg      case $to in
18835c4bbdfSmrg        [\\/]* | ?:[\\/]*) target=$to;;
18935c4bbdfSmrg        *) target=../$to;;
19005b261ecSmrg      esac
19105b261ecSmrg
19235c4bbdfSmrg      # Do not overwrite unchanged header files to avoid useless
19335c4bbdfSmrg      # recompilations.  Always update the parser itself: it is the
19435c4bbdfSmrg      # destination of the .y.c rule in the Makefile.  Divert the
19535c4bbdfSmrg      # output of all other files to a temporary file so we can
19635c4bbdfSmrg      # compare them to existing versions.
19735c4bbdfSmrg      if test $from != $parser; then
19835c4bbdfSmrg        realtarget=$target
19935c4bbdfSmrg        target=tmp-`printf '%s\n' "$target" | sed 's|.*[\\/]||g'`
20005b261ecSmrg      fi
20135c4bbdfSmrg
20235c4bbdfSmrg      # Munge "#line" or "#" directives.  Don't let the resulting
20335c4bbdfSmrg      # debug information point at an absolute srcdir.  Use the real
20435c4bbdfSmrg      # output file name, not yy.lex.c for instance.  Adjust the
20535c4bbdfSmrg      # include guards too.
20635c4bbdfSmrg      sed -e "/^#/!b"                           \
20735c4bbdfSmrg          -e "s|$input_rx|$input_sub_rx|"       \
20835c4bbdfSmrg          -e "$sed_fix_filenames"               \
20935c4bbdfSmrg          -e "$sed_fix_header_guards"           \
21035c4bbdfSmrg        "$from" >"$target" || ret=$?
21135c4bbdfSmrg
21235c4bbdfSmrg      # Check whether files must be updated.
21335c4bbdfSmrg      if test "$from" != "$parser"; then
21435c4bbdfSmrg        if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
21535c4bbdfSmrg          echo "$to is unchanged"
21635c4bbdfSmrg          rm -f "$target"
21735c4bbdfSmrg        else
21835c4bbdfSmrg          echo "updating $to"
21905b261ecSmrg          mv -f "$target" "$realtarget"
22005b261ecSmrg        fi
22105b261ecSmrg      fi
22205b261ecSmrg    else
22335c4bbdfSmrg      # A missing file is only an error for the parser.  This is a
22435c4bbdfSmrg      # blatant hack to let us support using "yacc -d".  If -d is not
22535c4bbdfSmrg      # specified, don't fail when the header file is "missing".
22635c4bbdfSmrg      if test "$from" = "$parser"; then
22705b261ecSmrg        ret=1
22805b261ecSmrg      fi
22905b261ecSmrg    fi
23005b261ecSmrg  done
23105b261ecSmrgfi
23205b261ecSmrg
23305b261ecSmrg# Remove the directory.
23405b261ecSmrgcd ..
23505b261ecSmrgrm -rf $dirname
23605b261ecSmrg
23705b261ecSmrgexit $ret
23805b261ecSmrg
23905b261ecSmrg# Local Variables:
24005b261ecSmrg# mode: shell-script
24105b261ecSmrg# sh-indentation: 2
24205b261ecSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
24305b261ecSmrg# time-stamp-start: "scriptversion="
24405b261ecSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
2451b5d61b8Smrg# time-stamp-time-zone: "UTC0"
24635c4bbdfSmrg# time-stamp-end: "; # UTC"
24705b261ecSmrg# End:
248