1e23ec014Smrg#! /bin/sh
205b261ecSmrg# ylwrap - wrapper for lex/yacc invocations.
305b261ecSmrg
454b5899cSmrgscriptversion=2024-06-19.01; # UTC
505b261ecSmrg
654b5899cSmrg# Copyright (C) 1996-2024 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
21e23ec014Smrg# along with this program.  If not, see <https://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  -h|--h*)
7005b261ecSmrg    cat <<\EOF
7105b261ecSmrgUsage: ylwrap [--help|--version] INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
7205b261ecSmrg
7305b261ecSmrgWrapper for lex/yacc invocations, renaming files as desired.
7405b261ecSmrg
7505b261ecSmrg  INPUT is the input file
7605b261ecSmrg  OUTPUT is one file PROG generates
7705b261ecSmrg  DESIRED is the file we actually want instead of OUTPUT
7805b261ecSmrg  PROGRAM is program to run
7905b261ecSmrg  ARGS are passed to PROG
8005b261ecSmrg
8105b261ecSmrgAny number of OUTPUT,DESIRED pairs may be used.
8205b261ecSmrg
8305b261ecSmrgReport bugs to <bug-automake@gnu.org>.
8454b5899cSmrgGNU Automake home page: <https://www.gnu.org/software/automake/>.
8554b5899cSmrgGeneral help using GNU software: <https://www.gnu.org/gethelp/>.
8605b261ecSmrgEOF
8705b261ecSmrg    exit $?
8805b261ecSmrg    ;;
8905b261ecSmrg  -v|--v*)
9054b5899cSmrg    echo "ylwrap (GNU Automake) $scriptversion"
9105b261ecSmrg    exit $?
9205b261ecSmrg    ;;
9305b261ecSmrgesac
9405b261ecSmrg
9505b261ecSmrg
9605b261ecSmrg# The input.
9735c4bbdfSmrginput=$1
9805b261ecSmrgshift
9935c4bbdfSmrg# We'll later need for a correct munging of "#line" directives.
10035c4bbdfSmrginput_sub_rx=`get_dirname "$input" | quote_for_sed`
10135c4bbdfSmrgcase $input in
10205b261ecSmrg  [\\/]* | ?:[\\/]*)
10305b261ecSmrg    # Absolute path; do nothing.
10405b261ecSmrg    ;;
10505b261ecSmrg  *)
10605b261ecSmrg    # Relative path.  Make it absolute.
10735c4bbdfSmrg    input=`pwd`/$input
10805b261ecSmrg    ;;
10905b261ecSmrgesac
11035c4bbdfSmrginput_rx=`get_dirname "$input" | quote_for_sed`
11135c4bbdfSmrg
11235c4bbdfSmrg# Since DOS filename conventions don't allow two dots,
11335c4bbdfSmrg# the DOS version of Bison writes out y_tab.c instead of y.tab.c
11435c4bbdfSmrg# and y_tab.h instead of y.tab.h. Test to see if this is the case.
11535c4bbdfSmrgy_tab_nodot=false
11635c4bbdfSmrgif test -f y_tab.c || test -f y_tab.h; then
11735c4bbdfSmrg  y_tab_nodot=true
11835c4bbdfSmrgfi
11935c4bbdfSmrg
12035c4bbdfSmrg# The parser itself, the first file, is the destination of the .y.c
12135c4bbdfSmrg# rule in the Makefile.
12235c4bbdfSmrgparser=$1
12335c4bbdfSmrg
12435c4bbdfSmrg# A sed program to s/FROM/TO/g for all the FROM/TO so that, for
12535c4bbdfSmrg# instance, we rename #include "y.tab.h" into #include "parse.h"
12635c4bbdfSmrg# during the conversion from y.tab.c to parse.c.
12735c4bbdfSmrgsed_fix_filenames=
12805b261ecSmrg
12935c4bbdfSmrg# Also rename header guards, as Bison 2.7 for instance uses its header
13035c4bbdfSmrg# guard in its implementation file.
13135c4bbdfSmrgsed_fix_header_guards=
13235c4bbdfSmrg
13335c4bbdfSmrgwhile test $# -ne 0; do
13435c4bbdfSmrg  if test x"$1" = x"--"; then
13505b261ecSmrg    shift
13605b261ecSmrg    break
13705b261ecSmrg  fi
13835c4bbdfSmrg  from=$1
13935c4bbdfSmrg  # Handle y_tab.c and y_tab.h output by DOS
14035c4bbdfSmrg  if $y_tab_nodot; then
14135c4bbdfSmrg    case $from in
14235c4bbdfSmrg      "y.tab.c") from=y_tab.c;;
14335c4bbdfSmrg      "y.tab.h") from=y_tab.h;;
14435c4bbdfSmrg    esac
14535c4bbdfSmrg  fi
14605b261ecSmrg  shift
14735c4bbdfSmrg  to=$1
14835c4bbdfSmrg  shift
14935c4bbdfSmrg  sed_fix_filenames="${sed_fix_filenames}s|"`quote_for_sed "$from"`"|$to|g;"
15035c4bbdfSmrg  sed_fix_header_guards="${sed_fix_header_guards}s|"`guard "$from"`"|"`guard "$to"`"|g;"
15105b261ecSmrgdone
15205b261ecSmrg
15305b261ecSmrg# The program to run.
15435c4bbdfSmrgprog=$1
15505b261ecSmrgshift
15605b261ecSmrg# Make any relative path in $prog absolute.
15735c4bbdfSmrgcase $prog in
15805b261ecSmrg  [\\/]* | ?:[\\/]*) ;;
15935c4bbdfSmrg  *[\\/]*) prog=`pwd`/$prog ;;
16005b261ecSmrgesac
16105b261ecSmrg
16205b261ecSmrgdirname=ylwrap$$
16335c4bbdfSmrgdo_exit="cd '`pwd`' && rm -rf $dirname > /dev/null 2>&1;"' (exit $ret); exit $ret'
16435c4bbdfSmrgtrap "ret=129; $do_exit" 1
16535c4bbdfSmrgtrap "ret=130; $do_exit" 2
16635c4bbdfSmrgtrap "ret=141; $do_exit" 13
16735c4bbdfSmrgtrap "ret=143; $do_exit" 15
16805b261ecSmrgmkdir $dirname || exit 1
16905b261ecSmrg
17005b261ecSmrgcd $dirname
17105b261ecSmrg
17205b261ecSmrgcase $# in
17335c4bbdfSmrg  0) "$prog" "$input" ;;
17435c4bbdfSmrg  *) "$prog" "$@" "$input" ;;
17505b261ecSmrgesac
17605b261ecSmrgret=$?
17705b261ecSmrg
17805b261ecSmrgif test $ret -eq 0; then
17935c4bbdfSmrg  for from in *
18035c4bbdfSmrg  do
18135c4bbdfSmrg    to=`printf '%s\n' "$from" | sed "$sed_fix_filenames"`
18205b261ecSmrg    if test -f "$from"; then
18305b261ecSmrg      # If $2 is an absolute path name, then just use that,
18435c4bbdfSmrg      # otherwise prepend '../'.
18535c4bbdfSmrg      case $to in
18635c4bbdfSmrg        [\\/]* | ?:[\\/]*) target=$to;;
18735c4bbdfSmrg        *) target=../$to;;
18805b261ecSmrg      esac
18905b261ecSmrg
19035c4bbdfSmrg      # Do not overwrite unchanged header files to avoid useless
19135c4bbdfSmrg      # recompilations.  Always update the parser itself: it is the
19235c4bbdfSmrg      # destination of the .y.c rule in the Makefile.  Divert the
19335c4bbdfSmrg      # output of all other files to a temporary file so we can
19435c4bbdfSmrg      # compare them to existing versions.
19535c4bbdfSmrg      if test $from != $parser; then
19635c4bbdfSmrg        realtarget=$target
19735c4bbdfSmrg        target=tmp-`printf '%s\n' "$target" | sed 's|.*[\\/]||g'`
19805b261ecSmrg      fi
19935c4bbdfSmrg
20035c4bbdfSmrg      # Munge "#line" or "#" directives.  Don't let the resulting
20135c4bbdfSmrg      # debug information point at an absolute srcdir.  Use the real
20235c4bbdfSmrg      # output file name, not yy.lex.c for instance.  Adjust the
20335c4bbdfSmrg      # include guards too.
20435c4bbdfSmrg      sed -e "/^#/!b"                           \
20535c4bbdfSmrg          -e "s|$input_rx|$input_sub_rx|"       \
20635c4bbdfSmrg          -e "$sed_fix_filenames"               \
20735c4bbdfSmrg          -e "$sed_fix_header_guards"           \
20835c4bbdfSmrg        "$from" >"$target" || ret=$?
20935c4bbdfSmrg
21035c4bbdfSmrg      # Check whether files must be updated.
21135c4bbdfSmrg      if test "$from" != "$parser"; then
21235c4bbdfSmrg        if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
21335c4bbdfSmrg          echo "$to is unchanged"
21435c4bbdfSmrg          rm -f "$target"
21535c4bbdfSmrg        else
21635c4bbdfSmrg          echo "updating $to"
21705b261ecSmrg          mv -f "$target" "$realtarget"
21805b261ecSmrg        fi
21905b261ecSmrg      fi
22005b261ecSmrg    else
22135c4bbdfSmrg      # A missing file is only an error for the parser.  This is a
22235c4bbdfSmrg      # blatant hack to let us support using "yacc -d".  If -d is not
22335c4bbdfSmrg      # specified, don't fail when the header file is "missing".
22435c4bbdfSmrg      if test "$from" = "$parser"; then
22505b261ecSmrg        ret=1
22605b261ecSmrg      fi
22705b261ecSmrg    fi
22805b261ecSmrg  done
22905b261ecSmrgfi
23005b261ecSmrg
23105b261ecSmrg# Remove the directory.
23205b261ecSmrgcd ..
23305b261ecSmrgrm -rf $dirname
23405b261ecSmrg
23505b261ecSmrgexit $ret
23605b261ecSmrg
23705b261ecSmrg# Local Variables:
23805b261ecSmrg# mode: shell-script
23905b261ecSmrg# sh-indentation: 2
240e23ec014Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
24105b261ecSmrg# time-stamp-start: "scriptversion="
24205b261ecSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
2431b5d61b8Smrg# time-stamp-time-zone: "UTC0"
24435c4bbdfSmrg# time-stamp-end: "; # UTC"
24505b261ecSmrg# End:
246