[HAVE_UNISTD_H]: Include unistd.h.
[gnulib.git] / lib / ylwrap
1 #! /bin/sh
2 # ylwrap - wrapper for lex/yacc invocations.
3 # Copyright (C) 1996, 1997 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 2, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 # Usage:
21 #     ylwrap PROGRAM INPUT [OUTPUT DESIRED]... -- [ARGS]...
22 # * PROGRAM is program to run.
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 # Any number of OUTPUT,DESIRED pairs may be used.
28
29 # The program to run.
30 prog="$1"
31 shift
32
33 # The input.
34 input="$1"
35 shift
36 case "$input" in
37  /*)
38     # Absolute path; do nothing.
39     ;;
40  *)
41     # Relative path.  Make it absolute.  Why?  Because otherwise any
42     # debugging info in the generated file will point to the wrong
43     # place.  This is really gross.
44     input="`pwd`/$input"
45     ;;
46 esac
47
48 pairlist=
49 while test "$#" -ne 0; do
50    if test "$1" = "--"; then
51       break
52    fi
53    pairlist="$pairlist $1"
54    shift
55 done
56
57 # FIXME: add hostname here for parallel makes that run commands on
58 # other machines.  But that might take us over the 14-char limit.
59 dirname=ylwrap$$
60 trap "cd `pwd`; rm -rf $dirname > /dev/null 2>&1" 1 2 3 15
61 mkdir $dirname || exit 1
62
63 cd $dirname
64 $prog ${1+"$@"} "$input"
65 status=$?
66
67 if test $status -eq 0; then
68    set X $pairlist
69    shift
70    first=yes
71    while test "$#" -ne 0; do
72       if test -f "$1"; then
73          # If $2 is an absolute path name, then just use that,
74          # otherwise prepend `../'.
75          case "$2" in
76            /*) target="$2";;
77            *) target="../$2";;
78          esac
79          mv "$1" "$target" || status=$?
80       else
81          # A missing file is only an error for the first file.  This
82          # is a blatant hack to let us support using "yacc -d".  If -d
83          # is not specified, we don't want an error when the header
84          # file is "missing".
85          if test $first = yes; then
86             status=1
87          fi
88       fi
89       shift
90       shift
91       first=no
92    done
93 else
94    status=$?
95 fi
96
97 # Remove the directory.
98 cd ..
99 #rm -rf $dirname
100
101 exit $status