725b388383814348c2424e81c244a9d43ff19fdb
[platform/upstream/automake.git] / lib / ylwrap
1 #! /bin/sh
2 # ylwrap - wrapper for lex/yacc invocations.
3
4 scriptversion=2012-07-13.14; # UTC
5
6 # Copyright (C) 1996-2012 Free Software Foundation, Inc.
7 #
8 # Written by Tom Tromey <tromey@cygnus.com>.
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2, or (at your option)
13 # any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
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 # This file is maintained in Automake, please report
29 # bugs to <bug-automake@gnu.org> or send patches to
30 # <automake-patches@gnu.org>.
31
32 get_dirname ()
33 {
34   case $1 in
35     */*|*\\*) printf '%s\n' "$1" | sed -e 's|\([\\/]\)[^\\/]*$|\1|';;
36     # Otherwise,  we want the empty string (not ".").
37   esac
38 }
39
40 # guard FILE
41 # ----------
42 # The CPP macro used to guard inclusion of FILE.
43 guard()
44 {
45   printf '%s\n' "$from" \
46     | sed \
47         -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
48         -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'
49 }
50
51 # quote_for_sed [STRING]
52 # ----------------------
53 # Return STRING (or stdin) quoted to be used as a sed pattern.
54 quote_for_sed ()
55 {
56   case $# in
57     0) cat;;
58     1) printf '%s\n' "$1";;
59   esac \
60     | sed -e 's|[][\\.*]|\\&|g'
61 }
62
63 case "$1" in
64   '')
65     echo "$0: No files given.  Try '$0 --help' for more information." 1>&2
66     exit 1
67     ;;
68   --basedir)
69     basedir=$2
70     shift 2
71     ;;
72   -h|--h*)
73     cat <<\EOF
74 Usage: ylwrap [--help|--version] INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
75
76 Wrapper for lex/yacc invocations, renaming files as desired.
77
78   INPUT is the input file
79   OUTPUT is one file PROG generates
80   DESIRED is the file we actually want instead of OUTPUT
81   PROGRAM is program to run
82   ARGS are passed to PROG
83
84 Any number of OUTPUT,DESIRED pairs may be used.
85
86 Report bugs to <bug-automake@gnu.org>.
87 EOF
88     exit $?
89     ;;
90   -v|--v*)
91     echo "ylwrap $scriptversion"
92     exit $?
93     ;;
94 esac
95
96
97 # The input.
98 input="$1"
99 shift
100 # We'll later need for a correct munging of "#line" directives.
101 input_sub_rx=`get_dirname "$input" | quote_for_sed`
102 case "$input" in
103   [\\/]* | ?:[\\/]*)
104     # Absolute path; do nothing.
105     ;;
106   *)
107     # Relative path.  Make it absolute.
108     input="`pwd`/$input"
109     ;;
110 esac
111
112 # Since DOS filename conventions don't allow two dots,
113 # the DOS version of Bison writes out y_tab.c instead of y.tab.c
114 # and y_tab.h instead of y.tab.h. Test to see if this is the case.
115 y_tab_nodot=false
116 if test -f y_tab.c || test -f y_tab.h; then
117   y_tab_nodot=true
118 fi
119
120 # The list of file to rename: FROM TO...
121 pairlist=
122 # A sed program to s/FROM/TO/g for all the FROM/TO so that, for
123 # instance, we rename #include "y.tab.h" into #include "parse.h"
124 # during the conversion from y.tab.c to parse.c.
125 rename_sed=
126 while test "$#" -ne 0; do
127   if test "$1" = "--"; then
128     shift
129     break
130   fi
131   from=$1
132   # Handle y_tab.c and y_tab.h output by DOS
133   if $y_tab_nodot; then
134     case $from in
135       "y.tab.c") from=y_tab.c;;
136       "y.tab.h") from=y_tab.h;;
137     esac
138   fi
139   shift
140   to=$1
141   shift
142   pairlist="$pairlist $from $to"
143   rename_sed="${rename_sed}s|"`quote_for_sed "$from"`"|$to|g;"
144 done
145
146 # The program to run.
147 prog="$1"
148 shift
149 # Make any relative path in $prog absolute.
150 case "$prog" in
151   [\\/]* | ?:[\\/]*) ;;
152   *[\\/]*) prog="`pwd`/$prog" ;;
153 esac
154
155 # FIXME: add hostname here for parallel makes that run commands on
156 # other machines.  But that might take us over the 14-char limit.
157 dirname=ylwrap$$
158 do_exit="cd '`pwd`' && rm -rf $dirname > /dev/null 2>&1;"' (exit $ret); exit $ret'
159 trap "ret=129; $do_exit" 1
160 trap "ret=130; $do_exit" 2
161 trap "ret=141; $do_exit" 13
162 trap "ret=143; $do_exit" 15
163 mkdir $dirname || exit 1
164
165 cd $dirname
166
167 case $# in
168   0) "$prog" "$input" ;;
169   *) "$prog" "$@" "$input" ;;
170 esac
171 ret=$?
172
173 if test $ret -eq 0; then
174   set X $pairlist
175   shift
176   first=yes
177
178   input_rx=`get_dirname "$input" | quote_for_sed`
179
180   while test "$#" -ne 0; do
181     from=$1
182     to=$2
183     if test -f "$from"; then
184       # If $2 is an absolute path name, then just use that,
185       # otherwise prepend '../'.
186       case $to in
187         [\\/]* | ?:[\\/]*) target=$to;;
188         *) target="../$to";;
189       esac
190
191       # Do not overwrite unchanged header files to avoid useless
192       # recompilations.  Always update the parser itself (the first
193       # file): it is the destination of the .y.c rule in the Makefile.
194       # Divert the output of all other files to a temporary file so we
195       # can compare them to existing versions.
196       if test $first = no; then
197         realtarget="$target"
198         target=tmp-`printf '%s\n' "$target" | sed s/.*[\\/]//g`
199       fi
200
201       # Munge "#line" or "#" directives.  Don't let the resulting
202       # debug information point at an absolute srcdir.  Use the real
203       # output file name, not yy.lex.c for instance.  Adjust the
204       # include guards too.
205       FROM=`guard "$from"`
206       TARGET=`guard "$to"`
207       sed -e "/^#/!b" -e "s|$input_rx|$input_sub_rx|" -e "$rename_sed" \
208           -e "s|$FROM|$TARGET|" "$from" >"$target" || ret=$?
209
210       # Check whether header files must be updated.
211       if test $first = no; then
212         if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
213           echo "$to is unchanged"
214           rm -f "$target"
215         else
216           echo "updating $to"
217           mv -f "$target" "$realtarget"
218         fi
219       fi
220     else
221       # A missing file is only an error for the first file.  This
222       # is a blatant hack to let us support using "yacc -d".  If -d
223       # is not specified, we don't want an error when the header
224       # file is "missing".
225       if test $first = yes; then
226         ret=1
227       fi
228     fi
229     shift
230     shift
231     first=no
232   done
233 else
234   ret=$?
235 fi
236
237 # Remove the directory.
238 cd ..
239 rm -rf $dirname
240
241 exit $ret
242
243 # Local Variables:
244 # mode: shell-script
245 # sh-indentation: 2
246 # eval: (add-hook 'write-file-hooks 'time-stamp)
247 # time-stamp-start: "scriptversion="
248 # time-stamp-format: "%:y-%02m-%02d.%02H"
249 # time-stamp-time-zone: "UTC"
250 # time-stamp-end: "; # UTC"
251 # End: