ylwrap revision 1.1 1 1.1 christos #! /bin/sh
2 1.1 christos # ylwrap - wrapper for lex/yacc invocations.
3 1.1 christos
4 1.1 christos scriptversion=2005-02-02.22
5 1.1 christos
6 1.1 christos # Copyright (C) 1996, 1997, 1998, 1999, 2001, 2002, 2003, 2004, 2005
7 1.1 christos # Free Software Foundation, Inc.
8 1.1 christos #
9 1.1 christos # Written by Tom Tromey <tromey (at] cygnus.com>.
10 1.1 christos #
11 1.1 christos # This program is free software; you can redistribute it and/or modify
12 1.1 christos # it under the terms of the GNU General Public License as published by
13 1.1 christos # the Free Software Foundation; either version 2, or (at your option)
14 1.1 christos # any later version.
15 1.1 christos #
16 1.1 christos # This program is distributed in the hope that it will be useful,
17 1.1 christos # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 1.1 christos # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 1.1 christos # GNU General Public License for more details.
20 1.1 christos #
21 1.1 christos # You should have received a copy of the GNU General Public License
22 1.1 christos # along with this program; if not, write to the Free Software
23 1.1 christos # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 1.1 christos
25 1.1 christos # As a special exception to the GNU General Public License, if you
26 1.1 christos # distribute this file as part of a program that contains a
27 1.1 christos # configuration script generated by Autoconf, you may include it under
28 1.1 christos # the same distribution terms that you use for the rest of that program.
29 1.1 christos
30 1.1 christos # This file is maintained in Automake, please report
31 1.1 christos # bugs to <bug-automake (at] gnu.org> or send patches to
32 1.1 christos # <automake-patches (at] gnu.org>.
33 1.1 christos
34 1.1 christos case "$1" in
35 1.1 christos '')
36 1.1 christos echo "$0: No files given. Try \`$0 --help' for more information." 1>&2
37 1.1 christos exit 1
38 1.1 christos ;;
39 1.1 christos --basedir)
40 1.1 christos basedir=$2
41 1.1 christos shift 2
42 1.1 christos ;;
43 1.1 christos -h|--h*)
44 1.1 christos cat <<\EOF
45 1.1 christos Usage: ylwrap [--help|--version] INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
46 1.1 christos
47 1.1 christos Wrapper for lex/yacc invocations, renaming files as desired.
48 1.1 christos
49 1.1 christos INPUT is the input file
50 1.1 christos OUTPUT is one file PROG generates
51 1.1 christos DESIRED is the file we actually want instead of OUTPUT
52 1.1 christos PROGRAM is program to run
53 1.1 christos ARGS are passed to PROG
54 1.1 christos
55 1.1 christos Any number of OUTPUT,DESIRED pairs may be used.
56 1.1 christos
57 1.1 christos This wrapper will prefix each conflicting yacc/lex symbol with a unique name
58 1.1 christos produced from INPUT. For example, if INPUT is foo_parse.y, then yyparse()
59 1.1 christos becomes foo_parse(). This requires that both your .l and corresponding .y
60 1.1 christos files have the same prefix (e.g., foo_parse.y and foo_tok.l).
61 1.1 christos
62 1.1 christos Report bugs to <bug-automake (at] gnu.org>.
63 1.1 christos EOF
64 1.1 christos exit $?
65 1.1 christos ;;
66 1.1 christos -v|--v*)
67 1.1 christos echo "ylwrap $scriptversion"
68 1.1 christos exit $?
69 1.1 christos ;;
70 1.1 christos esac
71 1.1 christos
72 1.1 christos
73 1.1 christos # The input.
74 1.1 christos input="$1"
75 1.1 christos shift
76 1.1 christos case "$input" in
77 1.1 christos [\\/]* | ?:[\\/]*)
78 1.1 christos # Absolute path; do nothing.
79 1.1 christos ;;
80 1.1 christos *)
81 1.1 christos # Relative path. Make it absolute.
82 1.1 christos input="`pwd`/$input"
83 1.1 christos ;;
84 1.1 christos esac
85 1.1 christos
86 1.1 christos pairlist=
87 1.1 christos while test "$#" -ne 0; do
88 1.1 christos if test "$1" = "--"; then
89 1.1 christos shift
90 1.1 christos break
91 1.1 christos fi
92 1.1 christos pairlist="$pairlist $1"
93 1.1 christos shift
94 1.1 christos done
95 1.1 christos
96 1.1 christos # The program to run.
97 1.1 christos prog="$1"
98 1.1 christos shift
99 1.1 christos # Make any relative path in $prog absolute.
100 1.1 christos case "$prog" in
101 1.1 christos [\\/]* | ?:[\\/]*) ;;
102 1.1 christos *[\\/]*) prog="`pwd`/$prog" ;;
103 1.1 christos esac
104 1.1 christos
105 1.1 christos # FIXME: add hostname here for parallel makes that run commands on
106 1.1 christos # other machines. But that might take us over the 14-char limit.
107 1.1 christos dirname=ylwrap$$
108 1.1 christos trap "cd `pwd`; rm -rf $dirname > /dev/null 2>&1" 1 2 3 15
109 1.1 christos mkdir $dirname || exit 1
110 1.1 christos
111 1.1 christos cd $dirname
112 1.1 christos
113 1.1 christos case $# in
114 1.1 christos 0) $prog "$input" ;;
115 1.1 christos *) $prog "$@" "$input" ;;
116 1.1 christos esac
117 1.1 christos ret=$?
118 1.1 christos
119 1.1 christos if test $ret -eq 0; then
120 1.1 christos set X $pairlist
121 1.1 christos shift
122 1.1 christos first=yes
123 1.1 christos # Since DOS filename conventions don't allow two dots,
124 1.1 christos # the DOS version of Bison writes out y_tab.c instead of y.tab.c
125 1.1 christos # and y_tab.h instead of y.tab.h. Test to see if this is the case.
126 1.1 christos y_tab_nodot="no"
127 1.1 christos if test -f y_tab.c || test -f y_tab.h; then
128 1.1 christos y_tab_nodot="yes"
129 1.1 christos fi
130 1.1 christos
131 1.1 christos # The directory holding the input.
132 1.1 christos input_dir=`echo "$input" | sed -e 's,\([\\/]\)[^\\/]*$,\1,'`
133 1.1 christos # Quote $INPUT_DIR so we can use it in a regexp.
134 1.1 christos # FIXME: really we should care about more than `.' and `\'.
135 1.1 christos input_rx=`echo "$input_dir" | sed 's,\\\\,\\\\\\\\,g;s,\\.,\\\\.,g'`
136 1.1 christos
137 1.1 christos while test "$#" -ne 0; do
138 1.1 christos from="$1"
139 1.1 christos # Handle y_tab.c and y_tab.h output by DOS
140 1.1 christos if test $y_tab_nodot = "yes"; then
141 1.1 christos if test $from = "y.tab.c"; then
142 1.1 christos from="y_tab.c"
143 1.1 christos else
144 1.1 christos if test $from = "y.tab.h"; then
145 1.1 christos from="y_tab.h"
146 1.1 christos fi
147 1.1 christos fi
148 1.1 christos fi
149 1.1 christos if test -f "$from"; then
150 1.1 christos # If $2 is an absolute path name, then just use that,
151 1.1 christos # otherwise prepend `../'.
152 1.1 christos case "$2" in
153 1.1 christos [\\/]* | ?:[\\/]*) target="$2";;
154 1.1 christos *) target="../$2";;
155 1.1 christos esac
156 1.1 christos
157 1.1 christos # We do not want to overwrite a header file if it hasn't
158 1.1 christos # changed. This avoid useless recompilations. However the
159 1.1 christos # parser itself (the first file) should always be updated,
160 1.1 christos # because it is the destination of the .y.c rule in the
161 1.1 christos # Makefile. Divert the output of all other files to a temporary
162 1.1 christos # file so we can compare them to existing versions.
163 1.1 christos if test $first = no; then
164 1.1 christos realtarget="$target"
165 1.1 christos target="tmp-`echo $target | sed s/.*[\\/]//g`"
166 1.1 christos fi
167 1.1 christos # Edit out `#line' or `#' directives.
168 1.1 christos #
169 1.1 christos # We don't want the resulting debug information to point at
170 1.1 christos # an absolute srcdir; it is better for it to just mention the
171 1.1 christos # .y file with no path.
172 1.1 christos #
173 1.1 christos # We want to use the real output file name, not yy.lex.c for
174 1.1 christos # instance.
175 1.1 christos #
176 1.1 christos # We want the include guards to be adjusted too.
177 1.1 christos FROM=`echo "$from" | sed \
178 1.1 christos -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
179 1.1 christos -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`
180 1.1 christos TARGET=`echo "$2" | sed \
181 1.1 christos -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
182 1.1 christos -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`
183 1.1 christos prefix=`echo $input | sed \
184 1.1 christos -e 's,^.*/,,g' \
185 1.1 christos -e 's/_parse.[yl]$/_/g' \
186 1.1 christos -e 's/_tok.[yl]$/_/g'`
187 1.1 christos
188 1.1 christos case $prefix in
189 1.1 christos *.y)
190 1.1 christos code_prefix="$(basename $prefix _gram.y)_yy";;
191 1.1 christos *.l)
192 1.1 christos code_prefix="$(basename $prefix _lex.l)_yy";;
193 1.1 christos *)
194 1.1 christos code_prefix="$prefix";;
195 1.1 christos esac
196 1.1 christos sed -e "/^#/!b" -e "s,$input_rx,," -e "s,$from,$2," \
197 1.1 christos -e "s,$FROM,$TARGET," "$from" | sed "s/yy/$code_prefix/g" > "$target" || ret=$?
198 1.1 christos
199 1.1 christos # Check whether header files must be updated.
200 1.1 christos if test $first = no; then
201 1.1 christos if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
202 1.1 christos echo "$2" is unchanged
203 1.1 christos rm -f "$target"
204 1.1 christos else
205 1.1 christos echo updating "$2"
206 1.1 christos mv -f "$target" "$realtarget"
207 1.1 christos fi
208 1.1 christos fi
209 1.1 christos else
210 1.1 christos # A missing file is only an error for the first file. This
211 1.1 christos # is a blatant hack to let us support using "yacc -d". If -d
212 1.1 christos # is not specified, we don't want an error when the header
213 1.1 christos # file is "missing".
214 1.1 christos if test $first = yes; then
215 1.1 christos ret=1
216 1.1 christos fi
217 1.1 christos fi
218 1.1 christos shift
219 1.1 christos shift
220 1.1 christos first=no
221 1.1 christos done
222 1.1 christos else
223 1.1 christos ret=$?
224 1.1 christos fi
225 1.1 christos
226 1.1 christos # Remove the directory.
227 1.1 christos cd ..
228 1.1 christos rm -rf $dirname
229 1.1 christos
230 1.1 christos exit $ret
231 1.1 christos
232 1.1 christos # Local Variables:
233 1.1 christos # mode: shell-script
234 1.1 christos # sh-indentation: 2
235 1.1 christos # eval: (add-hook 'write-file-hooks 'time-stamp)
236 1.1 christos # time-stamp-start: "scriptversion="
237 1.1 christos # time-stamp-format: "%:y-%02m-%02d.%02H"
238 1.1 christos # time-stamp-end: "$"
239 1.1 christos # End:
240