ylwrap revision f46a6179
1f46a6179Smrg#! /bin/sh
2f46a6179Smrg# ylwrap - wrapper for lex/yacc invocations.
3f46a6179Smrg
4f46a6179Smrgscriptversion=2007-11-22.22
5f46a6179Smrg
6f46a6179Smrg# Copyright (C) 1996, 1997, 1998, 1999, 2001, 2002, 2003, 2004, 2005,
7f46a6179Smrg# 2007  Free Software Foundation, Inc.
8f46a6179Smrg#
9f46a6179Smrg# Written by Tom Tromey <tromey@cygnus.com>.
10f46a6179Smrg#
11f46a6179Smrg# This program is free software; you can redistribute it and/or modify
12f46a6179Smrg# it under the terms of the GNU General Public License as published by
13f46a6179Smrg# the Free Software Foundation; either version 2, or (at your option)
14f46a6179Smrg# any later version.
15f46a6179Smrg#
16f46a6179Smrg# This program is distributed in the hope that it will be useful,
17f46a6179Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
18f46a6179Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19f46a6179Smrg# GNU General Public License for more details.
20f46a6179Smrg#
21f46a6179Smrg# You should have received a copy of the GNU General Public License
22f46a6179Smrg# along with this program; if not, write to the Free Software
23f46a6179Smrg# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24f46a6179Smrg# 02110-1301, USA.
25f46a6179Smrg
26f46a6179Smrg# As a special exception to the GNU General Public License, if you
27f46a6179Smrg# distribute this file as part of a program that contains a
28f46a6179Smrg# configuration script generated by Autoconf, you may include it under
29f46a6179Smrg# the same distribution terms that you use for the rest of that program.
30f46a6179Smrg
31f46a6179Smrg# This file is maintained in Automake, please report
32f46a6179Smrg# bugs to <bug-automake@gnu.org> or send patches to
33f46a6179Smrg# <automake-patches@gnu.org>.
34f46a6179Smrg
35f46a6179Smrgcase "$1" in
36f46a6179Smrg  '')
37f46a6179Smrg    echo "$0: No files given.  Try \`$0 --help' for more information." 1>&2
38f46a6179Smrg    exit 1
39f46a6179Smrg    ;;
40f46a6179Smrg  --basedir)
41f46a6179Smrg    basedir=$2
42f46a6179Smrg    shift 2
43f46a6179Smrg    ;;
44f46a6179Smrg  -h|--h*)
45f46a6179Smrg    cat <<\EOF
46f46a6179SmrgUsage: ylwrap [--help|--version] INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
47f46a6179Smrg
48f46a6179SmrgWrapper for lex/yacc invocations, renaming files as desired.
49f46a6179Smrg
50f46a6179Smrg  INPUT is the input file
51f46a6179Smrg  OUTPUT is one file PROG generates
52f46a6179Smrg  DESIRED is the file we actually want instead of OUTPUT
53f46a6179Smrg  PROGRAM is program to run
54f46a6179Smrg  ARGS are passed to PROG
55f46a6179Smrg
56f46a6179SmrgAny number of OUTPUT,DESIRED pairs may be used.
57f46a6179Smrg
58f46a6179SmrgReport bugs to <bug-automake@gnu.org>.
59f46a6179SmrgEOF
60f46a6179Smrg    exit $?
61f46a6179Smrg    ;;
62f46a6179Smrg  -v|--v*)
63f46a6179Smrg    echo "ylwrap $scriptversion"
64f46a6179Smrg    exit $?
65f46a6179Smrg    ;;
66f46a6179Smrgesac
67f46a6179Smrg
68f46a6179Smrg
69f46a6179Smrg# The input.
70f46a6179Smrginput="$1"
71f46a6179Smrgshift
72f46a6179Smrgcase "$input" in
73f46a6179Smrg  [\\/]* | ?:[\\/]*)
74f46a6179Smrg    # Absolute path; do nothing.
75f46a6179Smrg    ;;
76f46a6179Smrg  *)
77f46a6179Smrg    # Relative path.  Make it absolute.
78f46a6179Smrg    input="`pwd`/$input"
79f46a6179Smrg    ;;
80f46a6179Smrgesac
81f46a6179Smrg
82f46a6179Smrgpairlist=
83f46a6179Smrgwhile test "$#" -ne 0; do
84f46a6179Smrg  if test "$1" = "--"; then
85f46a6179Smrg    shift
86f46a6179Smrg    break
87f46a6179Smrg  fi
88f46a6179Smrg  pairlist="$pairlist $1"
89f46a6179Smrg  shift
90f46a6179Smrgdone
91f46a6179Smrg
92f46a6179Smrg# The program to run.
93f46a6179Smrgprog="$1"
94f46a6179Smrgshift
95f46a6179Smrg# Make any relative path in $prog absolute.
96f46a6179Smrgcase "$prog" in
97f46a6179Smrg  [\\/]* | ?:[\\/]*) ;;
98f46a6179Smrg  *[\\/]*) prog="`pwd`/$prog" ;;
99f46a6179Smrgesac
100f46a6179Smrg
101f46a6179Smrg# FIXME: add hostname here for parallel makes that run commands on
102f46a6179Smrg# other machines.  But that might take us over the 14-char limit.
103f46a6179Smrgdirname=ylwrap$$
104f46a6179Smrgtrap "cd '`pwd`'; rm -rf $dirname > /dev/null 2>&1" 1 2 3 15
105f46a6179Smrgmkdir $dirname || exit 1
106f46a6179Smrg
107f46a6179Smrgcd $dirname
108f46a6179Smrg
109f46a6179Smrgcase $# in
110f46a6179Smrg  0) "$prog" "$input" ;;
111f46a6179Smrg  *) "$prog" "$@" "$input" ;;
112f46a6179Smrgesac
113f46a6179Smrgret=$?
114f46a6179Smrg
115f46a6179Smrgif test $ret -eq 0; then
116f46a6179Smrg  set X $pairlist
117f46a6179Smrg  shift
118f46a6179Smrg  first=yes
119f46a6179Smrg  # Since DOS filename conventions don't allow two dots,
120f46a6179Smrg  # the DOS version of Bison writes out y_tab.c instead of y.tab.c
121f46a6179Smrg  # and y_tab.h instead of y.tab.h. Test to see if this is the case.
122f46a6179Smrg  y_tab_nodot="no"
123f46a6179Smrg  if test -f y_tab.c || test -f y_tab.h; then
124f46a6179Smrg    y_tab_nodot="yes"
125f46a6179Smrg  fi
126f46a6179Smrg
127f46a6179Smrg  # The directory holding the input.
128f46a6179Smrg  input_dir=`echo "$input" | sed -e 's,\([\\/]\)[^\\/]*$,\1,'`
129f46a6179Smrg  # Quote $INPUT_DIR so we can use it in a regexp.
130f46a6179Smrg  # FIXME: really we should care about more than `.' and `\'.
131f46a6179Smrg  input_rx=`echo "$input_dir" | sed 's,\\\\,\\\\\\\\,g;s,\\.,\\\\.,g'`
132f46a6179Smrg
133f46a6179Smrg  while test "$#" -ne 0; do
134f46a6179Smrg    from="$1"
135f46a6179Smrg    # Handle y_tab.c and y_tab.h output by DOS
136f46a6179Smrg    if test $y_tab_nodot = "yes"; then
137f46a6179Smrg      if test $from = "y.tab.c"; then
138f46a6179Smrg    	from="y_tab.c"
139f46a6179Smrg      else
140f46a6179Smrg    	if test $from = "y.tab.h"; then
141f46a6179Smrg    	  from="y_tab.h"
142f46a6179Smrg    	fi
143f46a6179Smrg      fi
144f46a6179Smrg    fi
145f46a6179Smrg    if test -f "$from"; then
146f46a6179Smrg      # If $2 is an absolute path name, then just use that,
147f46a6179Smrg      # otherwise prepend `../'.
148f46a6179Smrg      case "$2" in
149f46a6179Smrg    	[\\/]* | ?:[\\/]*) target="$2";;
150f46a6179Smrg    	*) target="../$2";;
151f46a6179Smrg      esac
152f46a6179Smrg
153f46a6179Smrg      # We do not want to overwrite a header file if it hasn't
154f46a6179Smrg      # changed.  This avoid useless recompilations.  However the
155f46a6179Smrg      # parser itself (the first file) should always be updated,
156f46a6179Smrg      # because it is the destination of the .y.c rule in the
157f46a6179Smrg      # Makefile.  Divert the output of all other files to a temporary
158f46a6179Smrg      # file so we can compare them to existing versions.
159f46a6179Smrg      if test $first = no; then
160f46a6179Smrg	realtarget="$target"
161f46a6179Smrg	target="tmp-`echo $target | sed s/.*[\\/]//g`"
162f46a6179Smrg      fi
163f46a6179Smrg      # Edit out `#line' or `#' directives.
164f46a6179Smrg      #
165f46a6179Smrg      # We don't want the resulting debug information to point at
166f46a6179Smrg      # an absolute srcdir; it is better for it to just mention the
167f46a6179Smrg      # .y file with no path.
168f46a6179Smrg      #
169f46a6179Smrg      # We want to use the real output file name, not yy.lex.c for
170f46a6179Smrg      # instance.
171f46a6179Smrg      #
172f46a6179Smrg      # We want the include guards to be adjusted too.
173f46a6179Smrg      FROM=`echo "$from" | sed \
174f46a6179Smrg            -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
175f46a6179Smrg            -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`
176f46a6179Smrg      TARGET=`echo "$2" | sed \
177f46a6179Smrg            -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
178f46a6179Smrg            -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`
179f46a6179Smrg
180f46a6179Smrg      sed -e "/^#/!b" -e "s,$input_rx,," -e "s,$from,$2," \
181f46a6179Smrg          -e "s,$FROM,$TARGET," "$from" >"$target" || ret=$?
182f46a6179Smrg
183f46a6179Smrg      # Check whether header files must be updated.
184f46a6179Smrg      if test $first = no; then
185f46a6179Smrg	if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
186f46a6179Smrg	  echo "$2" is unchanged
187f46a6179Smrg	  rm -f "$target"
188f46a6179Smrg	else
189f46a6179Smrg          echo updating "$2"
190f46a6179Smrg          mv -f "$target" "$realtarget"
191f46a6179Smrg        fi
192f46a6179Smrg      fi
193f46a6179Smrg    else
194f46a6179Smrg      # A missing file is only an error for the first file.  This
195f46a6179Smrg      # is a blatant hack to let us support using "yacc -d".  If -d
196f46a6179Smrg      # is not specified, we don't want an error when the header
197f46a6179Smrg      # file is "missing".
198f46a6179Smrg      if test $first = yes; then
199f46a6179Smrg        ret=1
200f46a6179Smrg      fi
201f46a6179Smrg    fi
202f46a6179Smrg    shift
203f46a6179Smrg    shift
204f46a6179Smrg    first=no
205f46a6179Smrg  done
206f46a6179Smrgelse
207f46a6179Smrg  ret=$?
208f46a6179Smrgfi
209f46a6179Smrg
210f46a6179Smrg# Remove the directory.
211f46a6179Smrgcd ..
212f46a6179Smrgrm -rf $dirname
213f46a6179Smrg
214f46a6179Smrgexit $ret
215f46a6179Smrg
216f46a6179Smrg# Local Variables:
217f46a6179Smrg# mode: shell-script
218f46a6179Smrg# sh-indentation: 2
219f46a6179Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
220f46a6179Smrg# time-stamp-start: "scriptversion="
221f46a6179Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
222f46a6179Smrg# time-stamp-end: "$"
223f46a6179Smrg# End:
224