xref: /netbsd-src/external/gpl3/binutils/dist/ylwrap (revision 9573673d78c64ea1eac42d7f2e9521be89932ae5)
12a6b7db3Sskrll#! /bin/sh
22a6b7db3Sskrll# ylwrap - wrapper for lex/yacc invocations.
32a6b7db3Sskrll
4*9573673dSchristosscriptversion=2013-01-12.17; # UTC
52a6b7db3Sskrll
6*9573673dSchristos# Copyright (C) 1996-2014 Free Software Foundation, Inc.
72a6b7db3Sskrll#
82a6b7db3Sskrll# Written by Tom Tromey <tromey@cygnus.com>.
92a6b7db3Sskrll#
102a6b7db3Sskrll# This program is free software; you can redistribute it and/or modify
112a6b7db3Sskrll# it under the terms of the GNU General Public License as published by
122a6b7db3Sskrll# the Free Software Foundation; either version 2, or (at your option)
132a6b7db3Sskrll# any later version.
142a6b7db3Sskrll#
152a6b7db3Sskrll# This program is distributed in the hope that it will be useful,
162a6b7db3Sskrll# but WITHOUT ANY WARRANTY; without even the implied warranty of
172a6b7db3Sskrll# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
182a6b7db3Sskrll# GNU General Public License for more details.
192a6b7db3Sskrll#
202a6b7db3Sskrll# You should have received a copy of the GNU General Public License
21be9ac0eaSchristos# along with this program.  If not, see <http://www.gnu.org/licenses/>.
222a6b7db3Sskrll
232a6b7db3Sskrll# As a special exception to the GNU General Public License, if you
242a6b7db3Sskrll# distribute this file as part of a program that contains a
252a6b7db3Sskrll# configuration script generated by Autoconf, you may include it under
262a6b7db3Sskrll# the same distribution terms that you use for the rest of that program.
272a6b7db3Sskrll
282a6b7db3Sskrll# This file is maintained in Automake, please report
292a6b7db3Sskrll# bugs to <bug-automake@gnu.org> or send patches to
302a6b7db3Sskrll# <automake-patches@gnu.org>.
312a6b7db3Sskrll
32*9573673dSchristosget_dirname ()
33*9573673dSchristos{
34*9573673dSchristos  case $1 in
35*9573673dSchristos    */*|*\\*) printf '%s\n' "$1" | sed -e 's|\([\\/]\)[^\\/]*$|\1|';;
36*9573673dSchristos    # Otherwise,  we want the empty string (not ".").
37*9573673dSchristos  esac
38*9573673dSchristos}
39*9573673dSchristos
40*9573673dSchristos# guard FILE
41*9573673dSchristos# ----------
42*9573673dSchristos# The CPP macro used to guard inclusion of FILE.
43*9573673dSchristosguard ()
44*9573673dSchristos{
45*9573673dSchristos  printf '%s\n' "$1"                                                    \
46*9573673dSchristos    | sed                                                               \
47*9573673dSchristos        -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'   \
48*9573673dSchristos        -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'                        \
49*9573673dSchristos        -e 's/__*/_/g'
50*9573673dSchristos}
51*9573673dSchristos
52*9573673dSchristos# quote_for_sed [STRING]
53*9573673dSchristos# ----------------------
54*9573673dSchristos# Return STRING (or stdin) quoted to be used as a sed pattern.
55*9573673dSchristosquote_for_sed ()
56*9573673dSchristos{
57*9573673dSchristos  case $# in
58*9573673dSchristos    0) cat;;
59*9573673dSchristos    1) printf '%s\n' "$1";;
60*9573673dSchristos  esac \
61*9573673dSchristos    | sed -e 's|[][\\.*]|\\&|g'
62*9573673dSchristos}
63*9573673dSchristos
642a6b7db3Sskrllcase "$1" in
652a6b7db3Sskrll  '')
66*9573673dSchristos    echo "$0: No files given.  Try '$0 --help' for more information." 1>&2
672a6b7db3Sskrll    exit 1
682a6b7db3Sskrll    ;;
692a6b7db3Sskrll  --basedir)
702a6b7db3Sskrll    basedir=$2
712a6b7db3Sskrll    shift 2
722a6b7db3Sskrll    ;;
732a6b7db3Sskrll  -h|--h*)
742a6b7db3Sskrll    cat <<\EOF
752a6b7db3SskrllUsage: ylwrap [--help|--version] INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
762a6b7db3Sskrll
772a6b7db3SskrllWrapper for lex/yacc invocations, renaming files as desired.
782a6b7db3Sskrll
792a6b7db3Sskrll  INPUT is the input file
802a6b7db3Sskrll  OUTPUT is one file PROG generates
812a6b7db3Sskrll  DESIRED is the file we actually want instead of OUTPUT
822a6b7db3Sskrll  PROGRAM is program to run
832a6b7db3Sskrll  ARGS are passed to PROG
842a6b7db3Sskrll
852a6b7db3SskrllAny number of OUTPUT,DESIRED pairs may be used.
862a6b7db3Sskrll
872a6b7db3SskrllReport bugs to <bug-automake@gnu.org>.
882a6b7db3SskrllEOF
892a6b7db3Sskrll    exit $?
902a6b7db3Sskrll    ;;
912a6b7db3Sskrll  -v|--v*)
922a6b7db3Sskrll    echo "ylwrap $scriptversion"
932a6b7db3Sskrll    exit $?
942a6b7db3Sskrll    ;;
952a6b7db3Sskrllesac
962a6b7db3Sskrll
972a6b7db3Sskrll
982a6b7db3Sskrll# The input.
99*9573673dSchristosinput=$1
1002a6b7db3Sskrllshift
101*9573673dSchristos# We'll later need for a correct munging of "#line" directives.
102*9573673dSchristosinput_sub_rx=`get_dirname "$input" | quote_for_sed`
103*9573673dSchristoscase $input in
1042a6b7db3Sskrll  [\\/]* | ?:[\\/]*)
1052a6b7db3Sskrll    # Absolute path; do nothing.
1062a6b7db3Sskrll    ;;
1072a6b7db3Sskrll  *)
1082a6b7db3Sskrll    # Relative path.  Make it absolute.
109*9573673dSchristos    input=`pwd`/$input
1102a6b7db3Sskrll    ;;
1112a6b7db3Sskrllesac
112*9573673dSchristosinput_rx=`get_dirname "$input" | quote_for_sed`
1132a6b7db3Sskrll
114*9573673dSchristos# The parser itself, the first file, is the destination of the .y.c
115*9573673dSchristos# rule in the Makefile.
116*9573673dSchristosparser=$1
117*9573673dSchristos
118*9573673dSchristos# A sed program to s/FROM/TO/g for all the FROM/TO so that, for
119*9573673dSchristos# instance, we rename #include "y.tab.h" into #include "parse.h"
120*9573673dSchristos# during the conversion from y.tab.c to parse.c.
121*9573673dSchristossed_fix_filenames=
122*9573673dSchristos
123*9573673dSchristos# Also rename header guards, as Bison 2.7 for instance uses its header
124*9573673dSchristos# guard in its implementation file.
125*9573673dSchristossed_fix_header_guards=
126*9573673dSchristos
127*9573673dSchristoswhile test $# -ne 0; do
128*9573673dSchristos  if test x"$1" = x"--"; then
1292a6b7db3Sskrll    shift
1302a6b7db3Sskrll    break
1312a6b7db3Sskrll  fi
132*9573673dSchristos  from=$1
1332a6b7db3Sskrll  shift
134*9573673dSchristos  to=$1
135*9573673dSchristos  shift
136*9573673dSchristos  sed_fix_filenames="${sed_fix_filenames}s|"`quote_for_sed "$from"`"|$to|g;"
137*9573673dSchristos  sed_fix_header_guards="${sed_fix_header_guards}s|"`guard "$from"`"|"`guard "$to"`"|g;"
1382a6b7db3Sskrlldone
1392a6b7db3Sskrll
1402a6b7db3Sskrll# The program to run.
141*9573673dSchristosprog=$1
1422a6b7db3Sskrllshift
1432a6b7db3Sskrll# Make any relative path in $prog absolute.
144*9573673dSchristoscase $prog in
1452a6b7db3Sskrll  [\\/]* | ?:[\\/]*) ;;
146*9573673dSchristos  *[\\/]*) prog=`pwd`/$prog ;;
1472a6b7db3Sskrllesac
1482a6b7db3Sskrll
1492a6b7db3Sskrlldirname=ylwrap$$
150*9573673dSchristosdo_exit="cd '`pwd`' && rm -rf $dirname > /dev/null 2>&1;"' (exit $ret); exit $ret'
151*9573673dSchristostrap "ret=129; $do_exit" 1
152*9573673dSchristostrap "ret=130; $do_exit" 2
153*9573673dSchristostrap "ret=141; $do_exit" 13
154*9573673dSchristostrap "ret=143; $do_exit" 15
1552a6b7db3Sskrllmkdir $dirname || exit 1
1562a6b7db3Sskrll
1572a6b7db3Sskrllcd $dirname
1582a6b7db3Sskrll
1592a6b7db3Sskrllcase $# in
160be9ac0eaSchristos  0) "$prog" "$input" ;;
161be9ac0eaSchristos  *) "$prog" "$@" "$input" ;;
1622a6b7db3Sskrllesac
1632a6b7db3Sskrllret=$?
1642a6b7db3Sskrll
1652a6b7db3Sskrllif test $ret -eq 0; then
166*9573673dSchristos  for from in *
167*9573673dSchristos  do
168*9573673dSchristos    to=`printf '%s\n' "$from" | sed "$sed_fix_filenames"`
1692a6b7db3Sskrll    if test -f "$from"; then
1702a6b7db3Sskrll      # If $2 is an absolute path name, then just use that,
171*9573673dSchristos      # otherwise prepend '../'.
172*9573673dSchristos      case $to in
173*9573673dSchristos        [\\/]* | ?:[\\/]*) target=$to;;
174*9573673dSchristos        *) target=../$to;;
1752a6b7db3Sskrll      esac
1762a6b7db3Sskrll
177*9573673dSchristos      # Do not overwrite unchanged header files to avoid useless
178*9573673dSchristos      # recompilations.  Always update the parser itself: it is the
179*9573673dSchristos      # destination of the .y.c rule in the Makefile.  Divert the
180*9573673dSchristos      # output of all other files to a temporary file so we can
181*9573673dSchristos      # compare them to existing versions.
182*9573673dSchristos      if test $from != $parser; then
183*9573673dSchristos        realtarget=$target
184*9573673dSchristos        target=tmp-`printf '%s\n' "$target" | sed 's|.*[\\/]||g'`
1852a6b7db3Sskrll      fi
1862a6b7db3Sskrll
187*9573673dSchristos      # Munge "#line" or "#" directives.  Don't let the resulting
188*9573673dSchristos      # debug information point at an absolute srcdir.  Use the real
189*9573673dSchristos      # output file name, not yy.lex.c for instance.  Adjust the
190*9573673dSchristos      # include guards too.
191*9573673dSchristos      sed -e "/^#/!b"                           \
192*9573673dSchristos          -e "s|$input_rx|$input_sub_rx|"       \
193*9573673dSchristos          -e "$sed_fix_filenames"               \
194*9573673dSchristos          -e "$sed_fix_header_guards"           \
195*9573673dSchristos        "$from" >"$target" || ret=$?
1962a6b7db3Sskrll
197*9573673dSchristos      # Check whether files must be updated.
198*9573673dSchristos      if test "$from" != "$parser"; then
1992a6b7db3Sskrll        if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
200*9573673dSchristos          echo "$to is unchanged"
2012a6b7db3Sskrll          rm -f "$target"
2022a6b7db3Sskrll        else
203*9573673dSchristos          echo "updating $to"
2042a6b7db3Sskrll          mv -f "$target" "$realtarget"
2052a6b7db3Sskrll        fi
2062a6b7db3Sskrll      fi
2072a6b7db3Sskrll    else
208*9573673dSchristos      # A missing file is only an error for the parser.  This is a
209*9573673dSchristos      # blatant hack to let us support using "yacc -d".  If -d is not
210*9573673dSchristos      # specified, don't fail when the header file is "missing".
211*9573673dSchristos      if test "$from" = "$parser"; then
2122a6b7db3Sskrll        ret=1
2132a6b7db3Sskrll      fi
2142a6b7db3Sskrll    fi
2152a6b7db3Sskrll  done
2162a6b7db3Sskrllfi
2172a6b7db3Sskrll
2182a6b7db3Sskrll# Remove the directory.
2192a6b7db3Sskrllcd ..
2202a6b7db3Sskrllrm -rf $dirname
2212a6b7db3Sskrll
2222a6b7db3Sskrllexit $ret
2232a6b7db3Sskrll
2242a6b7db3Sskrll# Local Variables:
2252a6b7db3Sskrll# mode: shell-script
2262a6b7db3Sskrll# sh-indentation: 2
2272a6b7db3Sskrll# eval: (add-hook 'write-file-hooks 'time-stamp)
2282a6b7db3Sskrll# time-stamp-start: "scriptversion="
2292a6b7db3Sskrll# time-stamp-format: "%:y-%02m-%02d.%02H"
230be9ac0eaSchristos# time-stamp-time-zone: "UTC"
231be9ac0eaSchristos# time-stamp-end: "; # UTC"
2322a6b7db3Sskrll# End:
233