1#! /bin/sh
2# ylwrap - wrapper for lex/yacc invocations.
3#
4# Copyright (C) 1996, 1997, 1998, 1999, 2001, 2002 Free Software
5# Foundation, Inc.
6#
7# Written by Tom Tromey <tromey@cygnus.com>.
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2, or (at your option)
12# any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
23# As a special exception to the GNU General Public License, if you
24# distribute this file as part of a program that contains a
25# configuration script generated by Autoconf, you may include it under
26# the same distribution terms that you use for the rest of that program.
27
28# Usage:
29#     ylwrap INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
30# * INPUT is the input file
31# * OUTPUT is file PROG generates
32# * DESIRED is file we actually want
33# * PROGRAM is program to run
34# * ARGS are passed to PROG
35# Any number of OUTPUT,DESIRED pairs may be used.
36
37# The input.
38input="$1"
39shift
40case "$input" in
41 [\\/]* | ?:[\\/]*)
42    # Absolute path; do nothing.
43    ;;
44 *)
45    # Relative path.  Make it absolute.
46    input="`pwd`/$input"
47    ;;
48esac
49
50pairlist=
51while test "$#" -ne 0; do
52   if test "$1" = "--"; then
53      shift
54      break
55   fi
56   pairlist="$pairlist $1"
57   shift
58done
59
60# The program to run.
61prog="$1"
62shift
63# Make any relative path in $prog absolute.
64case "$prog" in
65 [\\/]* | ?:[\\/]*) ;;
66 *[\\/]*) prog="`pwd`/$prog" ;;
67esac
68
69# FIXME: add hostname here for parallel makes that run commands on
70# other machines.  But that might take us over the 14-char limit.
71dirname=ylwrap$$
72trap "cd `pwd`; rm -rf $dirname > /dev/null 2>&1" 1 2 3 15
73mkdir $dirname || exit 1
74
75cd $dirname
76
77case $# in
78 0) $prog "$input" ;;
79 *) $prog "$@" "$input" ;;
80esac
81status=$?
82
83if test $status -eq 0; then
84   set X $pairlist
85   shift
86   first=yes
87   # Since DOS filename conventions don't allow two dots,
88   # the DOS version of Bison writes out y_tab.c instead of y.tab.c
89   # and y_tab.h instead of y.tab.h. Test to see if this is the case.
90   y_tab_nodot="no"
91   if test -f y_tab.c || test -f y_tab.h; then
92      y_tab_nodot="yes"
93   fi
94
95   # The directory holding the input.
96   input_dir=`echo "$input" | sed -e 's,\([\\/]\)[^\\/]*$,\1,'`
97   # Quote $INPUT_DIR so we can use it in a regexp.
98   # FIXME: really we should care about more than `.' and `\'.
99   input_rx=`echo "$input_dir" | sed 's,\\\\,\\\\\\\\,g;s,\\.,\\\\.,g'`
100
101   while test "$#" -ne 0; do
102      from="$1"
103      # Handle y_tab.c and y_tab.h output by DOS
104      if test $y_tab_nodot = "yes"; then
105	 if test $from = "y.tab.c"; then
106	    from="y_tab.c"
107	 else
108	    if test $from = "y.tab.h"; then
109	       from="y_tab.h"
110	    fi
111	 fi
112      fi
113      if test -f "$from"; then
114         # If $2 is an absolute path name, then just use that,
115         # otherwise prepend `../'.
116         case "$2" in
117	   [\\/]* | ?:[\\/]*) target="$2";;
118	   *) target="../$2";;
119	 esac
120
121	 # Edit out `#line' or `#' directives.
122	 #
123	 # We don't want the resulting debug information to point at
124	 # an absolute srcdir; it is better for it to just mention the
125	 # .y file with no path.
126	 #
127	 # We want to use the real output file name, not yy.lex.c for
128	 # instance.
129	 #
130	 # We want the include guards to be adjusted too.
131	 FROM=`echo "$from" | sed \
132                 -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
133                 -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`
134	 TARGET=`echo "$2" | sed \
135                 -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
136                 -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`
137         sed "/^#/{s,$input_rx,,;s,$from,$2,;s,$FORM,$TO,;}" "$from" >"$target" ||
138            status=$?
139      else
140	 # A missing file is only an error for the first file.  This
141	 # is a blatant hack to let us support using "yacc -d".  If -d
142	 # is not specified, we don't want an error when the header
143	 # file is "missing".
144	 if test $first = yes; then
145	    status=1
146	 fi
147      fi
148      shift
149      shift
150      first=no
151   done
152else
153   status=$?
154fi
155
156# Remove the directory.
157cd ..
158rm -rf $dirname
159
160exit $status
161