1130803Smarcel#! /bin/sh
2130803Smarcel# ylwrap - wrapper for lex/yacc invocations.
3130803Smarcel# Copyright (C) 1996, 1997 Free Software Foundation, Inc.
4130803Smarcel# Written by Tom Tromey <tromey@cygnus.com>.
5130803Smarcel#
6130803Smarcel# This program is free software; you can redistribute it and/or modify
7130803Smarcel# it under the terms of the GNU General Public License as published by
8130803Smarcel# the Free Software Foundation; either version 2, or (at your option)
9130803Smarcel# any later version.
10130803Smarcel#
11130803Smarcel# This program is distributed in the hope that it will be useful,
12130803Smarcel# but WITHOUT ANY WARRANTY; without even the implied warranty of
13130803Smarcel# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14130803Smarcel# GNU General Public License for more details.
15130803Smarcel#
16130803Smarcel# You should have received a copy of the GNU General Public License
17130803Smarcel# along with this program; if not, write to the Free Software
18130803Smarcel# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19130803Smarcel
20130803Smarcel# Usage:
21130803Smarcel#     ylwrap PROGRAM INPUT [OUTPUT DESIRED]... -- [ARGS]...
22130803Smarcel# * PROGRAM is program to run.
23130803Smarcel# * INPUT is the input file
24130803Smarcel# * OUTPUT is file PROG generates
25130803Smarcel# * DESIRED is file we actually want
26130803Smarcel# * ARGS are passed to PROG
27130803Smarcel# Any number of OUTPUT,DESIRED pairs may be used.
28130803Smarcel
29130803Smarcel# The program to run.
30130803Smarcelprog="$1"
31130803Smarcelshift
32130803Smarcel# Make any relative path in $prog absolute.
33130803Smarcelcase "$prog" in
34130803Smarcel /* | [A-Za-z]:\\*) ;;
35130803Smarcel */*) prog="`pwd`/$prog" ;;
36130803Smarcelesac
37130803Smarcel
38130803Smarcel# The input.
39130803Smarcelinput="$1"
40130803Smarcelshift
41130803Smarcelcase "$input" in
42130803Smarcel /* | [A-Za-z]:\\*)
43130803Smarcel    # Absolute path; do nothing.
44130803Smarcel    ;;
45130803Smarcel *)
46130803Smarcel    # Relative path.  Make it absolute.  Why?  Because otherwise any
47130803Smarcel    # debugging info in the generated file will point to the wrong
48130803Smarcel    # place.  This is really gross.
49130803Smarcel    input="`pwd`/$input"
50130803Smarcel    ;;
51130803Smarcelesac
52130803Smarcel
53130803Smarcel# We don't want to use the absolute path if the input in the current
54130803Smarcel# directory like when making a tar ball.
55130803Smarcelinput_base=`echo $input | sed -e 's|.*/||'`
56130803Smarcelif test -f $input_base && cmp $input_base $input >/dev/null 2>&1; then
57130803Smarcel  input=$input_base
58130803Smarcelfi
59130803Smarcel
60130803Smarcelpairlist=
61130803Smarcelwhile test "$#" -ne 0; do
62130803Smarcel   if test "$1" = "--"; then
63130803Smarcel      shift
64130803Smarcel      break
65130803Smarcel   fi
66130803Smarcel   pairlist="$pairlist $1"
67130803Smarcel   shift
68130803Smarceldone
69130803Smarcel
70130803Smarcel# FIXME: add hostname here for parallel makes that run commands on
71130803Smarcel# other machines.  But that might take us over the 14-char limit.
72130803Smarceldirname=ylwrap$$
73130803Smarceltrap "cd `pwd`; rm -rf $dirname > /dev/null 2>&1" 1 2 3 15
74130803Smarcelmkdir $dirname || exit 1
75130803Smarcel
76130803Smarcelcd $dirname
77130803Smarcelcase "$input" in
78130803Smarcel /* | [A-Za-z]:\\*)
79130803Smarcel    # Absolute path; do nothing.
80130803Smarcel    ;;
81130803Smarcel *)
82130803Smarcel    # Make a symbolic link, hard link or hardcopy.
83130803Smarcel    ln -s ../"$input" . > /dev/null 2>&1 || ln ../"$input" . > /dev/null 2>&1 || cp ../"$input" .
84130803Smarcel    ;;
85130803Smarcelesac
86130803Smarcel$prog ${1+"$@"} "$input"
87130803Smarcelstatus=$?
88130803Smarcel
89130803Smarcelif test $status -eq 0; then
90130803Smarcel   set X $pairlist
91130803Smarcel   shift
92130803Smarcel   first=yes
93130803Smarcel   while test "$#" -ne 0; do
94130803Smarcel      if test -f "$1"; then
95130803Smarcel         # If $2 is an absolute path name, then just use that,
96130803Smarcel         # otherwise prepend `../'.
97130803Smarcel         case "$2" in
98130803Smarcel	   /* | [A-Za-z]:\\*) target="$2";;
99130803Smarcel	   *) target="../$2";;
100130803Smarcel	 esac
101130803Smarcel	 mv "$1" "$target" || status=$?
102130803Smarcel      else
103130803Smarcel	 # A missing file is only an error for the first file.  This
104130803Smarcel	 # is a blatant hack to let us support using "yacc -d".  If -d
105130803Smarcel	 # is not specified, we don't want an error when the header
106130803Smarcel	 # file is "missing".
107130803Smarcel	 if test $first = yes; then
108130803Smarcel	    status=1
109130803Smarcel	 fi
110130803Smarcel      fi
111130803Smarcel      shift
112130803Smarcel      shift
113130803Smarcel      first=no
114130803Smarcel   done
115130803Smarcelelse
116130803Smarcel   status=$?
117130803Smarcelfi
118130803Smarcel
119130803Smarcel# Remove the directory.
120130803Smarcelcd ..
121130803Smarcelrm -rf $dirname
122130803Smarcel
123130803Smarcelexit $status
124