12b15cb3dSCy Schubert#! /bin/sh
22b15cb3dSCy Schubert# ylwrap - wrapper for lex/yacc invocations.
32b15cb3dSCy Schubert
4*2d4e511cSCy Schubertscriptversion=2013-01-12.17; # UTC
52b15cb3dSCy Schubert
6*2d4e511cSCy Schubert# Copyright (C) 1996-2014 Free Software Foundation, Inc.
72b15cb3dSCy Schubert#
82b15cb3dSCy Schubert# Written by Tom Tromey <tromey@cygnus.com>.
92b15cb3dSCy Schubert#
102b15cb3dSCy Schubert# This program is free software; you can redistribute it and/or modify
112b15cb3dSCy Schubert# it under the terms of the GNU General Public License as published by
122b15cb3dSCy Schubert# the Free Software Foundation; either version 2, or (at your option)
132b15cb3dSCy Schubert# any later version.
142b15cb3dSCy Schubert#
152b15cb3dSCy Schubert# This program is distributed in the hope that it will be useful,
162b15cb3dSCy Schubert# but WITHOUT ANY WARRANTY; without even the implied warranty of
172b15cb3dSCy Schubert# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
182b15cb3dSCy Schubert# GNU General Public License for more details.
192b15cb3dSCy Schubert#
202b15cb3dSCy Schubert# You should have received a copy of the GNU General Public License
212b15cb3dSCy Schubert# along with this program.  If not, see <http://www.gnu.org/licenses/>.
222b15cb3dSCy Schubert
232b15cb3dSCy Schubert# As a special exception to the GNU General Public License, if you
242b15cb3dSCy Schubert# distribute this file as part of a program that contains a
252b15cb3dSCy Schubert# configuration script generated by Autoconf, you may include it under
262b15cb3dSCy Schubert# the same distribution terms that you use for the rest of that program.
272b15cb3dSCy Schubert
282b15cb3dSCy Schubert# This file is maintained in Automake, please report
292b15cb3dSCy Schubert# bugs to <bug-automake@gnu.org> or send patches to
302b15cb3dSCy Schubert# <automake-patches@gnu.org>.
312b15cb3dSCy Schubert
32f0574f5cSXin LIget_dirname ()
33f0574f5cSXin LI{
34f0574f5cSXin LI  case $1 in
35f0574f5cSXin LI    */*|*\\*) printf '%s\n' "$1" | sed -e 's|\([\\/]\)[^\\/]*$|\1|';;
36f0574f5cSXin LI    # Otherwise,  we want the empty string (not ".").
37f0574f5cSXin LI  esac
38f0574f5cSXin LI}
39f0574f5cSXin LI
40f0574f5cSXin LI# guard FILE
41f0574f5cSXin LI# ----------
42f0574f5cSXin LI# The CPP macro used to guard inclusion of FILE.
43f0574f5cSXin LIguard ()
44f0574f5cSXin LI{
45f0574f5cSXin LI  printf '%s\n' "$1"                                                    \
46f0574f5cSXin LI    | sed                                                               \
47f0574f5cSXin LI        -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'   \
48f0574f5cSXin LI        -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'                        \
49f0574f5cSXin LI        -e 's/__*/_/g'
50f0574f5cSXin LI}
51f0574f5cSXin LI
52f0574f5cSXin LI# quote_for_sed [STRING]
53f0574f5cSXin LI# ----------------------
54f0574f5cSXin LI# Return STRING (or stdin) quoted to be used as a sed pattern.
55f0574f5cSXin LIquote_for_sed ()
56f0574f5cSXin LI{
57f0574f5cSXin LI  case $# in
58f0574f5cSXin LI    0) cat;;
59f0574f5cSXin LI    1) printf '%s\n' "$1";;
60f0574f5cSXin LI  esac \
61f0574f5cSXin LI    | sed -e 's|[][\\.*]|\\&|g'
62f0574f5cSXin LI}
63f0574f5cSXin LI
642b15cb3dSCy Schubertcase "$1" in
652b15cb3dSCy Schubert  '')
66f0574f5cSXin LI    echo "$0: No files given.  Try '$0 --help' for more information." 1>&2
672b15cb3dSCy Schubert    exit 1
682b15cb3dSCy Schubert    ;;
692b15cb3dSCy Schubert  --basedir)
702b15cb3dSCy Schubert    basedir=$2
712b15cb3dSCy Schubert    shift 2
722b15cb3dSCy Schubert    ;;
732b15cb3dSCy Schubert  -h|--h*)
742b15cb3dSCy Schubert    cat <<\EOF
752b15cb3dSCy SchubertUsage: ylwrap [--help|--version] INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
762b15cb3dSCy Schubert
772b15cb3dSCy SchubertWrapper for lex/yacc invocations, renaming files as desired.
782b15cb3dSCy Schubert
792b15cb3dSCy Schubert  INPUT is the input file
802b15cb3dSCy Schubert  OUTPUT is one file PROG generates
812b15cb3dSCy Schubert  DESIRED is the file we actually want instead of OUTPUT
822b15cb3dSCy Schubert  PROGRAM is program to run
832b15cb3dSCy Schubert  ARGS are passed to PROG
842b15cb3dSCy Schubert
852b15cb3dSCy SchubertAny number of OUTPUT,DESIRED pairs may be used.
862b15cb3dSCy Schubert
872b15cb3dSCy SchubertReport bugs to <bug-automake@gnu.org>.
882b15cb3dSCy SchubertEOF
892b15cb3dSCy Schubert    exit $?
902b15cb3dSCy Schubert    ;;
912b15cb3dSCy Schubert  -v|--v*)
922b15cb3dSCy Schubert    echo "ylwrap $scriptversion"
932b15cb3dSCy Schubert    exit $?
942b15cb3dSCy Schubert    ;;
952b15cb3dSCy Schubertesac
962b15cb3dSCy Schubert
972b15cb3dSCy Schubert
982b15cb3dSCy Schubert# The input.
99f0574f5cSXin LIinput=$1
1002b15cb3dSCy Schubertshift
101f0574f5cSXin LI# We'll later need for a correct munging of "#line" directives.
102f0574f5cSXin LIinput_sub_rx=`get_dirname "$input" | quote_for_sed`
103f0574f5cSXin LIcase $input in
1042b15cb3dSCy Schubert  [\\/]* | ?:[\\/]*)
1052b15cb3dSCy Schubert    # Absolute path; do nothing.
1062b15cb3dSCy Schubert    ;;
1072b15cb3dSCy Schubert  *)
1082b15cb3dSCy Schubert    # Relative path.  Make it absolute.
109f0574f5cSXin LI    input=`pwd`/$input
1102b15cb3dSCy Schubert    ;;
1112b15cb3dSCy Schubertesac
112f0574f5cSXin LIinput_rx=`get_dirname "$input" | quote_for_sed`
1132b15cb3dSCy Schubert
114f0574f5cSXin LI# Since DOS filename conventions don't allow two dots,
115f0574f5cSXin LI# the DOS version of Bison writes out y_tab.c instead of y.tab.c
116f0574f5cSXin LI# and y_tab.h instead of y.tab.h. Test to see if this is the case.
117f0574f5cSXin LIy_tab_nodot=false
118f0574f5cSXin LIif test -f y_tab.c || test -f y_tab.h; then
119f0574f5cSXin LI  y_tab_nodot=true
120f0574f5cSXin LIfi
121f0574f5cSXin LI
122f0574f5cSXin LI# The parser itself, the first file, is the destination of the .y.c
123f0574f5cSXin LI# rule in the Makefile.
124f0574f5cSXin LIparser=$1
125f0574f5cSXin LI
126f0574f5cSXin LI# A sed program to s/FROM/TO/g for all the FROM/TO so that, for
127f0574f5cSXin LI# instance, we rename #include "y.tab.h" into #include "parse.h"
128f0574f5cSXin LI# during the conversion from y.tab.c to parse.c.
129f0574f5cSXin LIsed_fix_filenames=
130f0574f5cSXin LI
131f0574f5cSXin LI# Also rename header guards, as Bison 2.7 for instance uses its header
132f0574f5cSXin LI# guard in its implementation file.
133f0574f5cSXin LIsed_fix_header_guards=
134f0574f5cSXin LI
135f0574f5cSXin LIwhile test $# -ne 0; do
136f0574f5cSXin LI  if test x"$1" = x"--"; then
1372b15cb3dSCy Schubert    shift
1382b15cb3dSCy Schubert    break
1392b15cb3dSCy Schubert  fi
140f0574f5cSXin LI  from=$1
141f0574f5cSXin LI  # Handle y_tab.c and y_tab.h output by DOS
142f0574f5cSXin LI  if $y_tab_nodot; then
143f0574f5cSXin LI    case $from in
144f0574f5cSXin LI      "y.tab.c") from=y_tab.c;;
145f0574f5cSXin LI      "y.tab.h") from=y_tab.h;;
146f0574f5cSXin LI    esac
147f0574f5cSXin LI  fi
1482b15cb3dSCy Schubert  shift
149f0574f5cSXin LI  to=$1
150f0574f5cSXin LI  shift
151f0574f5cSXin LI  sed_fix_filenames="${sed_fix_filenames}s|"`quote_for_sed "$from"`"|$to|g;"
152f0574f5cSXin LI  sed_fix_header_guards="${sed_fix_header_guards}s|"`guard "$from"`"|"`guard "$to"`"|g;"
1532b15cb3dSCy Schubertdone
1542b15cb3dSCy Schubert
1552b15cb3dSCy Schubert# The program to run.
156f0574f5cSXin LIprog=$1
1572b15cb3dSCy Schubertshift
1582b15cb3dSCy Schubert# Make any relative path in $prog absolute.
159f0574f5cSXin LIcase $prog in
1602b15cb3dSCy Schubert  [\\/]* | ?:[\\/]*) ;;
161f0574f5cSXin LI  *[\\/]*) prog=`pwd`/$prog ;;
1622b15cb3dSCy Schubertesac
1632b15cb3dSCy Schubert
1642b15cb3dSCy Schubertdirname=ylwrap$$
165f0574f5cSXin LIdo_exit="cd '`pwd`' && rm -rf $dirname > /dev/null 2>&1;"' (exit $ret); exit $ret'
166f0574f5cSXin LItrap "ret=129; $do_exit" 1
167f0574f5cSXin LItrap "ret=130; $do_exit" 2
168f0574f5cSXin LItrap "ret=141; $do_exit" 13
169f0574f5cSXin LItrap "ret=143; $do_exit" 15
1702b15cb3dSCy Schubertmkdir $dirname || exit 1
1712b15cb3dSCy Schubert
1722b15cb3dSCy Schubertcd $dirname
1732b15cb3dSCy Schubert
1742b15cb3dSCy Schubertcase $# in
1752b15cb3dSCy Schubert  0) "$prog" "$input" ;;
1762b15cb3dSCy Schubert  *) "$prog" "$@" "$input" ;;
1772b15cb3dSCy Schubertesac
1782b15cb3dSCy Schubertret=$?
1792b15cb3dSCy Schubert
1802b15cb3dSCy Schubertif test $ret -eq 0; then
181f0574f5cSXin LI  for from in *
182f0574f5cSXin LI  do
183f0574f5cSXin LI    to=`printf '%s\n' "$from" | sed "$sed_fix_filenames"`
1842b15cb3dSCy Schubert    if test -f "$from"; then
1852b15cb3dSCy Schubert      # If $2 is an absolute path name, then just use that,
186f0574f5cSXin LI      # otherwise prepend '../'.
187f0574f5cSXin LI      case $to in
188f0574f5cSXin LI        [\\/]* | ?:[\\/]*) target=$to;;
189f0574f5cSXin LI        *) target=../$to;;
1902b15cb3dSCy Schubert      esac
1912b15cb3dSCy Schubert
192f0574f5cSXin LI      # Do not overwrite unchanged header files to avoid useless
193f0574f5cSXin LI      # recompilations.  Always update the parser itself: it is the
194f0574f5cSXin LI      # destination of the .y.c rule in the Makefile.  Divert the
195f0574f5cSXin LI      # output of all other files to a temporary file so we can
196f0574f5cSXin LI      # compare them to existing versions.
197f0574f5cSXin LI      if test $from != $parser; then
198f0574f5cSXin LI        realtarget=$target
199f0574f5cSXin LI        target=tmp-`printf '%s\n' "$target" | sed 's|.*[\\/]||g'`
2002b15cb3dSCy Schubert      fi
2012b15cb3dSCy Schubert
202f0574f5cSXin LI      # Munge "#line" or "#" directives.  Don't let the resulting
203f0574f5cSXin LI      # debug information point at an absolute srcdir.  Use the real
204f0574f5cSXin LI      # output file name, not yy.lex.c for instance.  Adjust the
205f0574f5cSXin LI      # include guards too.
206f0574f5cSXin LI      sed -e "/^#/!b"                           \
207f0574f5cSXin LI          -e "s|$input_rx|$input_sub_rx|"       \
208f0574f5cSXin LI          -e "$sed_fix_filenames"               \
209f0574f5cSXin LI          -e "$sed_fix_header_guards"           \
210f0574f5cSXin LI        "$from" >"$target" || ret=$?
2112b15cb3dSCy Schubert
212f0574f5cSXin LI      # Check whether files must be updated.
213f0574f5cSXin LI      if test "$from" != "$parser"; then
2142b15cb3dSCy Schubert        if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
215f0574f5cSXin LI          echo "$to is unchanged"
2162b15cb3dSCy Schubert          rm -f "$target"
2172b15cb3dSCy Schubert        else
218f0574f5cSXin LI          echo "updating $to"
2192b15cb3dSCy Schubert          mv -f "$target" "$realtarget"
2202b15cb3dSCy Schubert        fi
2212b15cb3dSCy Schubert      fi
2222b15cb3dSCy Schubert    else
223f0574f5cSXin LI      # A missing file is only an error for the parser.  This is a
224f0574f5cSXin LI      # blatant hack to let us support using "yacc -d".  If -d is not
225f0574f5cSXin LI      # specified, don't fail when the header file is "missing".
226f0574f5cSXin LI      if test "$from" = "$parser"; then
2272b15cb3dSCy Schubert        ret=1
2282b15cb3dSCy Schubert      fi
2292b15cb3dSCy Schubert    fi
2302b15cb3dSCy Schubert  done
2312b15cb3dSCy Schubertfi
2322b15cb3dSCy Schubert
2332b15cb3dSCy Schubert# Remove the directory.
2342b15cb3dSCy Schubertcd ..
2352b15cb3dSCy Schubertrm -rf $dirname
2362b15cb3dSCy Schubert
2372b15cb3dSCy Schubertexit $ret
2382b15cb3dSCy Schubert
2392b15cb3dSCy Schubert# Local Variables:
2402b15cb3dSCy Schubert# mode: shell-script
2412b15cb3dSCy Schubert# sh-indentation: 2
2422b15cb3dSCy Schubert# eval: (add-hook 'write-file-hooks 'time-stamp)
2432b15cb3dSCy Schubert# time-stamp-start: "scriptversion="
2442b15cb3dSCy Schubert# time-stamp-format: "%:y-%02m-%02d.%02H"
245*2d4e511cSCy Schubert# time-stamp-time-zone: "UTC"
2462b15cb3dSCy Schubert# time-stamp-end: "; # UTC"
2472b15cb3dSCy Schubert# End:
248