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