1706f2543Smrg#! /bin/sh
2706f2543Smrg# ylwrap - wrapper for lex/yacc invocations.
3706f2543Smrg
4706f2543Smrgscriptversion=2005-05-14.22
5706f2543Smrg
6706f2543Smrg# Copyright (C) 1996, 1997, 1998, 1999, 2001, 2002, 2003, 2004, 2005
7706f2543Smrg#   Free Software Foundation, Inc.
8706f2543Smrg#
9706f2543Smrg# Written by Tom Tromey <tromey@cygnus.com>.
10706f2543Smrg#
11706f2543Smrg# This program is free software; you can redistribute it and/or modify
12706f2543Smrg# it under the terms of the GNU General Public License as published by
13706f2543Smrg# the Free Software Foundation; either version 2, or (at your option)
14706f2543Smrg# any later version.
15706f2543Smrg#
16706f2543Smrg# This program is distributed in the hope that it will be useful,
17706f2543Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
18706f2543Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19706f2543Smrg# GNU General Public License for more details.
20706f2543Smrg#
21706f2543Smrg# You should have received a copy of the GNU General Public License
22706f2543Smrg# along with this program; if not, write to the Free Software
23706f2543Smrg# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24706f2543Smrg# 02110-1301, USA.
25706f2543Smrg
26706f2543Smrg# As a special exception to the GNU General Public License, if you
27706f2543Smrg# distribute this file as part of a program that contains a
28706f2543Smrg# configuration script generated by Autoconf, you may include it under
29706f2543Smrg# the same distribution terms that you use for the rest of that program.
30706f2543Smrg
31706f2543Smrg# This file is maintained in Automake, please report
32706f2543Smrg# bugs to <bug-automake@gnu.org> or send patches to
33706f2543Smrg# <automake-patches@gnu.org>.
34706f2543Smrg
35706f2543Smrgcase "$1" in
36706f2543Smrg  '')
37706f2543Smrg    echo "$0: No files given.  Try \`$0 --help' for more information." 1>&2
38706f2543Smrg    exit 1
39706f2543Smrg    ;;
40706f2543Smrg  --basedir)
41706f2543Smrg    basedir=$2
42706f2543Smrg    shift 2
43706f2543Smrg    ;;
44706f2543Smrg  -h|--h*)
45706f2543Smrg    cat <<\EOF
46706f2543SmrgUsage: ylwrap [--help|--version] INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
47706f2543Smrg
48706f2543SmrgWrapper for lex/yacc invocations, renaming files as desired.
49706f2543Smrg
50706f2543Smrg  INPUT is the input file
51706f2543Smrg  OUTPUT is one file PROG generates
52706f2543Smrg  DESIRED is the file we actually want instead of OUTPUT
53706f2543Smrg  PROGRAM is program to run
54706f2543Smrg  ARGS are passed to PROG
55706f2543Smrg
56706f2543SmrgAny number of OUTPUT,DESIRED pairs may be used.
57706f2543Smrg
58706f2543SmrgReport bugs to <bug-automake@gnu.org>.
59706f2543SmrgEOF
60706f2543Smrg    exit $?
61706f2543Smrg    ;;
62706f2543Smrg  -v|--v*)
63706f2543Smrg    echo "ylwrap $scriptversion"
64706f2543Smrg    exit $?
65706f2543Smrg    ;;
66706f2543Smrgesac
67706f2543Smrg
68706f2543Smrg
69706f2543Smrg# The input.
70706f2543Smrginput="$1"
71706f2543Smrgshift
72706f2543Smrgcase "$input" in
73706f2543Smrg  [\\/]* | ?:[\\/]*)
74706f2543Smrg    # Absolute path; do nothing.
75706f2543Smrg    ;;
76706f2543Smrg  *)
77706f2543Smrg    # Relative path.  Make it absolute.
78706f2543Smrg    input="`pwd`/$input"
79706f2543Smrg    ;;
80706f2543Smrgesac
81706f2543Smrg
82706f2543Smrgpairlist=
83706f2543Smrgwhile test "$#" -ne 0; do
84706f2543Smrg  if test "$1" = "--"; then
85706f2543Smrg    shift
86706f2543Smrg    break
87706f2543Smrg  fi
88706f2543Smrg  pairlist="$pairlist $1"
89706f2543Smrg  shift
90706f2543Smrgdone
91706f2543Smrg
92706f2543Smrg# The program to run.
93706f2543Smrgprog="$1"
94706f2543Smrgshift
95706f2543Smrg# Make any relative path in $prog absolute.
96706f2543Smrgcase "$prog" in
97706f2543Smrg  [\\/]* | ?:[\\/]*) ;;
98706f2543Smrg  *[\\/]*) prog="`pwd`/$prog" ;;
99706f2543Smrgesac
100706f2543Smrg
101706f2543Smrg# FIXME: add hostname here for parallel makes that run commands on
102706f2543Smrg# other machines.  But that might take us over the 14-char limit.
103706f2543Smrgdirname=ylwrap$$
104706f2543Smrgtrap "cd `pwd`; rm -rf $dirname > /dev/null 2>&1" 1 2 3 15
105706f2543Smrgmkdir $dirname || exit 1
106706f2543Smrg
107706f2543Smrgcd $dirname
108706f2543Smrg
109706f2543Smrgcase $# in
110706f2543Smrg  0) $prog "$input" ;;
111706f2543Smrg  *) $prog "$@" "$input" ;;
112706f2543Smrgesac
113706f2543Smrgret=$?
114706f2543Smrg
115706f2543Smrgif test $ret -eq 0; then
116706f2543Smrg  set X $pairlist
117706f2543Smrg  shift
118706f2543Smrg  first=yes
119706f2543Smrg  # Since DOS filename conventions don't allow two dots,
120706f2543Smrg  # the DOS version of Bison writes out y_tab.c instead of y.tab.c
121706f2543Smrg  # and y_tab.h instead of y.tab.h. Test to see if this is the case.
122706f2543Smrg  y_tab_nodot="no"
123706f2543Smrg  if test -f y_tab.c || test -f y_tab.h; then
124706f2543Smrg    y_tab_nodot="yes"
125706f2543Smrg  fi
126706f2543Smrg
127706f2543Smrg  # The directory holding the input.
128706f2543Smrg  input_dir=`echo "$input" | sed -e 's,\([\\/]\)[^\\/]*$,\1,'`
129706f2543Smrg  # Quote $INPUT_DIR so we can use it in a regexp.
130706f2543Smrg  # FIXME: really we should care about more than `.' and `\'.
131706f2543Smrg  input_rx=`echo "$input_dir" | sed 's,\\\\,\\\\\\\\,g;s,\\.,\\\\.,g'`
132706f2543Smrg
133706f2543Smrg  while test "$#" -ne 0; do
134706f2543Smrg    from="$1"
135706f2543Smrg    # Handle y_tab.c and y_tab.h output by DOS
136706f2543Smrg    if test $y_tab_nodot = "yes"; then
137706f2543Smrg      if test $from = "y.tab.c"; then
138706f2543Smrg    	from="y_tab.c"
139706f2543Smrg      else
140706f2543Smrg    	if test $from = "y.tab.h"; then
141706f2543Smrg    	  from="y_tab.h"
142706f2543Smrg    	fi
143706f2543Smrg      fi
144706f2543Smrg    fi
145706f2543Smrg    if test -f "$from"; then
146706f2543Smrg      # If $2 is an absolute path name, then just use that,
147706f2543Smrg      # otherwise prepend `../'.
148706f2543Smrg      case "$2" in
149706f2543Smrg    	[\\/]* | ?:[\\/]*) target="$2";;
150706f2543Smrg    	*) target="../$2";;
151706f2543Smrg      esac
152706f2543Smrg
153706f2543Smrg      # We do not want to overwrite a header file if it hasn't
154706f2543Smrg      # changed.  This avoid useless recompilations.  However the
155706f2543Smrg      # parser itself (the first file) should always be updated,
156706f2543Smrg      # because it is the destination of the .y.c rule in the
157706f2543Smrg      # Makefile.  Divert the output of all other files to a temporary
158706f2543Smrg      # file so we can compare them to existing versions.
159706f2543Smrg      if test $first = no; then
160706f2543Smrg	realtarget="$target"
161706f2543Smrg	target="tmp-`echo $target | sed s/.*[\\/]//g`"
162706f2543Smrg      fi
163706f2543Smrg      # Edit out `#line' or `#' directives.
164706f2543Smrg      #
165706f2543Smrg      # We don't want the resulting debug information to point at
166706f2543Smrg      # an absolute srcdir; it is better for it to just mention the
167706f2543Smrg      # .y file with no path.
168706f2543Smrg      #
169706f2543Smrg      # We want to use the real output file name, not yy.lex.c for
170706f2543Smrg      # instance.
171706f2543Smrg      #
172706f2543Smrg      # We want the include guards to be adjusted too.
173706f2543Smrg      FROM=`echo "$from" | sed \
174706f2543Smrg            -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
175706f2543Smrg            -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`
176706f2543Smrg      TARGET=`echo "$2" | sed \
177706f2543Smrg            -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
178706f2543Smrg            -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`
179706f2543Smrg
180706f2543Smrg      sed -e "/^#/!b" -e "s,$input_rx,," -e "s,$from,$2," \
181706f2543Smrg          -e "s,$FROM,$TARGET," "$from" >"$target" || ret=$?
182706f2543Smrg
183706f2543Smrg      # Check whether header files must be updated.
184706f2543Smrg      if test $first = no; then
185706f2543Smrg	if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
186706f2543Smrg	  echo "$2" is unchanged
187706f2543Smrg	  rm -f "$target"
188706f2543Smrg	else
189706f2543Smrg          echo updating "$2"
190706f2543Smrg          mv -f "$target" "$realtarget"
191706f2543Smrg        fi
192706f2543Smrg      fi
193706f2543Smrg    else
194706f2543Smrg      # A missing file is only an error for the first file.  This
195706f2543Smrg      # is a blatant hack to let us support using "yacc -d".  If -d
196706f2543Smrg      # is not specified, we don't want an error when the header
197706f2543Smrg      # file is "missing".
198706f2543Smrg      if test $first = yes; then
199706f2543Smrg        ret=1
200706f2543Smrg      fi
201706f2543Smrg    fi
202706f2543Smrg    shift
203706f2543Smrg    shift
204706f2543Smrg    first=no
205706f2543Smrg  done
206706f2543Smrgelse
207706f2543Smrg  ret=$?
208706f2543Smrgfi
209706f2543Smrg
210706f2543Smrg# Remove the directory.
211706f2543Smrgcd ..
212706f2543Smrgrm -rf $dirname
213706f2543Smrg
214706f2543Smrgexit $ret
215706f2543Smrg
216706f2543Smrg# Local Variables:
217706f2543Smrg# mode: shell-script
218706f2543Smrg# sh-indentation: 2
219706f2543Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
220706f2543Smrg# time-stamp-start: "scriptversion="
221706f2543Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
222706f2543Smrg# time-stamp-end: "$"
223706f2543Smrg# End:
224