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