Portable wrapper for lex/yacc invocations, by Tom Tromey
<tromey@cygnus.com>, modified by Sergey Poznyakoff <gray@farlep.net> to replace yy prefixes, so multiple grammers can co-exist.
Showing
1 changed file
with
170 additions
and
0 deletions
sieve/ylwrap
0 → 100755
1 | #! /bin/sh | ||
2 | # ylwrap - wrapper for lex/yacc invocations. | ||
3 | # Copyright 1996, 1997, 1998, 1999 Free Software Foundation, Inc. | ||
4 | # Written by Tom Tromey <tromey@cygnus.com>. | ||
5 | # -yy modification by Sergey Poznyakoff <gray@farlep.net> | ||
6 | # | ||
7 | # This program is free software; you can redistribute it and/or modify | ||
8 | # it under the terms of the GNU General Public License as published by | ||
9 | # the Free Software Foundation; either version 2, or (at your option) | ||
10 | # any later version. | ||
11 | # | ||
12 | # This program is distributed in the hope that it will be useful, | ||
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | # GNU General Public License for more details. | ||
16 | # | ||
17 | # You should have received a copy of the GNU General Public License | ||
18 | # along with this program; if not, write to the Free Software | ||
19 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
20 | |||
21 | # Usage: | ||
22 | # ylwrap PROGRAM [ARGS] INPUT [OUTPUT DESIRED]... -- [-yy repl] [ARGS]... | ||
23 | # * PROGRAM is program to run; options can follow but must start with `-'. | ||
24 | # * INPUT is the input file | ||
25 | # * OUTPUT is file PROG generates | ||
26 | # * DESIRED is file we actually want | ||
27 | # * ARGS are passed to PROG | ||
28 | # * Optional -yy introduces the sequence to replace yy prefixes with. | ||
29 | # Any number of OUTPUT,DESIRED pairs may be used. | ||
30 | |||
31 | # The program to run. | ||
32 | prog="$1" | ||
33 | shift | ||
34 | # Make any relative path in $prog absolute. | ||
35 | case "$prog" in | ||
36 | /* | [A-Za-z]:*) ;; | ||
37 | */*) prog="`pwd`/$prog" ;; | ||
38 | esac | ||
39 | |||
40 | # We also have to accept options here and append them to the program. | ||
41 | # Why? Suppose YACC is set to `bison -y'. Clearly nobody uses | ||
42 | # ylwrap, or this would have been discovered earlier! | ||
43 | while :; do | ||
44 | case "$1" in | ||
45 | -*) | ||
46 | prog="$prog $1" | ||
47 | shift | ||
48 | ;; | ||
49 | *) | ||
50 | break | ||
51 | ;; | ||
52 | esac | ||
53 | done | ||
54 | |||
55 | # The input. | ||
56 | input="$1" | ||
57 | shift | ||
58 | case "$input" in | ||
59 | /* | [A-Za-z]:*) | ||
60 | # Absolute path; do nothing. | ||
61 | ;; | ||
62 | *) | ||
63 | # Relative path. Make it absolute. | ||
64 | input="`pwd`/$input" | ||
65 | ;; | ||
66 | esac | ||
67 | |||
68 | # The directory holding the input. | ||
69 | input_dir="`echo $input | sed -e 's,/[^/]*$,,'`" | ||
70 | # Quote $INPUT_DIR so we can use it in a regexp. | ||
71 | # FIXME: really we should care about more than `.'. | ||
72 | input_rx="`echo $input_dir | sed -e 's,\.,\\\.,g'`" | ||
73 | |||
74 | pairlist= | ||
75 | while test "$#" -ne 0; do | ||
76 | if test "$1" = "--"; then | ||
77 | shift | ||
78 | break | ||
79 | fi | ||
80 | pairlist="$pairlist $1" | ||
81 | shift | ||
82 | done | ||
83 | |||
84 | if [ $# -ne 0 ]; then | ||
85 | if [ "x$1" = "x-yy" ]; then | ||
86 | shift | ||
87 | if [ $# -eq 0 ]; then | ||
88 | echo "ylwrap: -yy requires an argument" | ||
89 | exit 1 | ||
90 | fi | ||
91 | YYREPL=$1 | ||
92 | shift | ||
93 | fi | ||
94 | fi | ||
95 | |||
96 | # FIXME: add hostname here for parallel makes that run commands on | ||
97 | # other machines. But that might take us over the 14-char limit. | ||
98 | dirname=ylwrap$$ | ||
99 | trap "cd `pwd`; rm -rf $dirname > /dev/null 2>&1" 1 2 3 15 | ||
100 | mkdir $dirname || exit 1 | ||
101 | |||
102 | cd $dirname | ||
103 | |||
104 | $prog ${1+"$@"} "$input" | ||
105 | status=$? | ||
106 | |||
107 | if test $status -eq 0; then | ||
108 | set X $pairlist | ||
109 | shift | ||
110 | first=yes | ||
111 | # Since DOS filename conventions don't allow two dots, | ||
112 | # the DOS version of Bison writes out y_tab.c instead of y.tab.c | ||
113 | # and y_tab.h instead of y.tab.h. Test to see if this is the case. | ||
114 | y_tab_nodot="no" | ||
115 | if test -f y_tab.c || test -f y_tab.h; then | ||
116 | y_tab_nodot="yes" | ||
117 | fi | ||
118 | |||
119 | while test "$#" -ne 0; do | ||
120 | from="$1" | ||
121 | # Handle y_tab.c and y_tab.h output by DOS | ||
122 | if test $y_tab_nodot = "yes"; then | ||
123 | if test $from = "y.tab.c"; then | ||
124 | from="y_tab.c" | ||
125 | else | ||
126 | if test $from = "y.tab.h"; then | ||
127 | from="y_tab.h" | ||
128 | fi | ||
129 | fi | ||
130 | fi | ||
131 | if test -f "$from"; then | ||
132 | # If $2 is an absolute path name, then just use that, | ||
133 | # otherwise prepend `../'. | ||
134 | case "$2" in | ||
135 | /* | [A-Za-z]:*) target="$2";; | ||
136 | *) target="../$2";; | ||
137 | esac | ||
138 | |||
139 | # Edit out `#line' or `#' directives. We don't want the | ||
140 | # resulting debug information to point at an absolute srcdir; | ||
141 | # it is better for it to just mention the .y file with no | ||
142 | # path. | ||
143 | EXPR="/^#/ s,$input_rx/,," | ||
144 | if [ ! -z "$YYREPL" ]; then | ||
145 | EXPR="$EXPR;s/yy/$YYREPL/g" | ||
146 | fi | ||
147 | sed -e "$EXPR" "$from" > "$target" || status=$? | ||
148 | else | ||
149 | # A missing file is only an error for the first file. This | ||
150 | # is a blatant hack to let us support using "yacc -d". If -d | ||
151 | # is not specified, we don't want an error when the header | ||
152 | # file is "missing". | ||
153 | if test $first = yes; then | ||
154 | status=1 | ||
155 | fi | ||
156 | fi | ||
157 | shift | ||
158 | shift | ||
159 | first=no | ||
160 | done | ||
161 | else | ||
162 | status=$? | ||
163 | fi | ||
164 | |||
165 | # Remove the directory. | ||
166 | cd .. | ||
167 | rm -rf $dirname | ||
168 | |||
169 | exit $status | ||
170 |
-
Please register or sign in to post a comment