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