1#! /bin/sh
2# ylwrap - wrapper for lex/yacc invocations.
3# Copyright 1996, 1997, 1998, 1999, 2007 Free Software Foundation, Inc.
4# Written by Tom Tromey <tromey@cygnus.com>.
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 3, or (at your option)
9# any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20# Usage:
21#     ylwrap PROGRAM [ARGS] INPUT [OUTPUT DESIRED]... -- [-yy repl] [ARGS]...
22# * PROGRAM is program to run; options can follow but must start with `-'.
23# * INPUT is the input file
24# * OUTPUT is file PROG generates
25# * DESIRED is file we actually want
26# * ARGS are passed to PROG
27# * Optional -yy introduces the sequence to replace yy prefixes with.
28# Any number of OUTPUT,DESIRED pairs may be used.
29
30# The program to run.
31prog="$1"
32shift
33# Make any relative path in $prog absolute.
34case "$prog" in
35 /* | [A-Za-z]:*) ;;
36 */*) prog="`pwd`/$prog" ;;
37esac
38
39# We also have to accept options here and append them to the program.
40# Why?  Suppose YACC is set to `bison -y'.  Clearly nobody uses
41# ylwrap, or this would have been discovered earlier!
42while :; do
43   case "$1" in
44    -*)
45       prog="$prog $1"
46       shift
47       ;;
48    *)
49       break
50       ;;
51   esac
52done
53
54# The input.
55input="$1"
56shift
57case "$input" in
58 /* | [A-Za-z]:*)
59    # Absolute path; do nothing.
60    ;;
61 *)
62    # Relative path.  Make it absolute.
63    input="`pwd`/$input"
64    ;;
65esac
66
67# The directory holding the input.
68input_dir="`echo $input | sed -e 's,/[^/]*$,,'`"
69# Quote $INPUT_DIR so we can use it in a regexp.
70# FIXME: really we should care about more than `.'.
71input_rx="`echo $input_dir | sed -e 's,\.,\\\.,g'`"
72
73pairlist=
74defout=$1
75while test "$#" -ne 0; do
76   if test "$1" = "--"; then
77      shift
78      break
79   fi
80   pairlist="$pairlist $1"
81   shift
82done
83
84STDOUT=""
85if [ $# -ne 0 ]; then
86    while test "$#" -ne 0; do
87	case "x$1" in
88	x-yy)
89	    shift
90	    if [ $# -eq 0 ]; then
91		echo "ylwrap: -yy requires an argument"
92		exit 1
93	    fi
94	    YYREPL=$1
95	    shift;;
96	x-stdout)
97	    shift
98	    STDOUT=$defout
99	    ;;
100	*)
101	    break;;
102	esac
103    done
104fi
105
106# FIXME: add hostname here for parallel makes that run commands on
107# other machines.  But that might take us over the 14-char limit.
108dirname=ylwrap$$
109trap "cd `pwd`; rm -rf $dirname > /dev/null 2>&1" 1 2 3 15
110mkdir $dirname || exit 1
111
112cd $dirname
113
114if [ -n "$STDOUT" ]; then
115    $prog ${1+"$@"} "$input" > $STDOUT
116else
117    $prog ${1+"$@"} "$input"
118fi
119status=$?
120
121if test $status -eq 0; then
122   set X $pairlist
123   shift
124   first=yes
125   # Since DOS filename conventions don't allow two dots,
126   # the DOS version of Bison writes out y_tab.c instead of y.tab.c
127   # and y_tab.h instead of y.tab.h. Test to see if this is the case.
128   y_tab_nodot="no"
129   if test -f y_tab.c || test -f y_tab.h; then
130      y_tab_nodot="yes"
131   fi
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	   /* | [A-Za-z]:*) target="$2";;
150	   *) target="../$2";;
151	 esac
152
153	 # Edit out `#line' or `#' directives.  We don't want the
154	 # resulting debug information to point at an absolute srcdir;
155	 # it is better for it to just mention the .y file with no
156	 # path.
157	 T=`basename $target`
158	 EXPR="/^#/ s,$input_rx/,,;s,\"$from\",\"$T\","
159	 if [ ! -z "$YYREPL" ]; then
160	    EXPR="$EXPR;s/yy/$YYREPL/g"
161	 fi
162	 sed -e "$EXPR" "$from" > "$target" || status=$?
163      else
164	 # A missing file is only an error for the first file.  This
165	 # is a blatant hack to let us support using "yacc -d".  If -d
166	 # is not specified, we don't want an error when the header
167	 # file is "missing".
168	 if test $first = yes; then
169	    status=1
170	 fi
171      fi
172      shift
173      shift
174      first=no
175   done
176else
177   status=$?
178fi
179
180# Remove the directory.
181cd ..
182rm -rf $dirname
183
184exit $status
185