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