MSWSP: fix scope of VT_TYPE
[wireshark-wip.git] / tools / runlex.sh
blob1e58c50f8bc1f569e2f9c742e028beedd69d3f2d
1 #! /bin/sh
4 # runlex.sh
5 # Script to run Lex/Flex.
6 # First argument is the (quoted) name of the command; if it's null, that
7 # means that neither Flex nor Lex was found, so we report an error and
8 # quit.
10 # $Id$
12 # Wireshark - Network traffic analyzer
13 # By Gerald Combs <gerald@wireshark.org>
14 # Copyright 2007 Gerald Combs
16 # This program is free software; you can redistribute it and/or
17 # modify it under the terms of the GNU General Public License
18 # as published by the Free Software Foundation; either version 2
19 # of the License, or (at your option) any later version.
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
32 # Get the name of the command to run, and then shift to get the arguments.
34 if [ $# -eq 0 ]
35 then
36 echo "Usage: runlex <lex/flex command to run> [ arguments ]" 1>&2
37 exit 1
39 LEX="$1"
40 shift
42 # Check whether we have it.
44 if [ -z "${LEX}" ]
45 then
46 echo "Neither lex nor flex was found" 1>&2
47 exit 1
50 SED="$1"
51 shift
53 # Check whether we have it.
55 if [ -z "${SED}" ]
56 then
57 echo "Sed was not found" 1>&2
58 exit 1
62 # Process the flags. We don't use getopt because we don't want to
63 # embed complete knowledge of what options are supported by Lex/Flex.
65 flags=""
66 outfile=lex.yy.c
67 while [ $# -ne 0 ]
69 case "$1" in
71 -o*)
73 # Set the output file name.
75 outfile=`echo "$1" | ${SED} 's/-o\(.*\)/\1/'`
78 -*)
80 # Add this to the list of flags.
82 flags="$flags $1"
85 --|*)
87 # End of flags.
89 break
91 esac
92 shift
93 done
96 # OK, run it.
98 echo "Running ${LEX}"
99 ${LEX} -o"$outfile" $flags "$@"
102 # Did it succeed?
104 if [ $? -ne 0 ]
105 then
107 # No. Exit with the failing exit status.
109 exitstatus=$?
110 echo "${LEX} failed: exit status $exitstatus"
111 exit $exitstatus
115 # Flex has the annoying habit of stripping all but the last component of
116 # the "-o" flag argument and using that as the place to put the output.
117 # This gets in the way of building in a directory different from the
118 # source directory. Try to work around this.
120 # Is the outfile where we think it is?
122 outfile_base=`basename "$outfile"`
123 if [ "$outfile_base" != "$outfile" -a \( ! -r "$outfile" \) -a -r "$outfile_base" ]
124 then
126 # No, it's not, but it is in the current directory. Put it
127 # where it's supposed to be.
129 mv "$outfile_base" "$outfile"
130 if [ $? -ne 0 ]
131 then
132 echo $?
136 echo "Wrote $outfile"
139 # OK, now let's generate a header file declaring the relevant functions
140 # defined by the .c file; if the .c file is .../foo.c, the header file
141 # will be .../foo_lex.h.
143 # This works around some other Flex suckage, wherein it doesn't declare
144 # the lex routine before defining it, causing compiler warnings.
145 # XXX - newer versions of Flex support --header-file=, to generate the
146 # appropriate header file. With those versions, we should use that option.
150 # Get the name of the prefix; scan the source files for a %option prefix
151 # line. We use the last one.
153 echo "Getting prefix"
154 prefix=`${SED} -n 's/%option[ ][ ]*prefix="\(.*\)".*/\1/p' "$@" | tail -1`
155 if [ ! -z "$prefix" ]
156 then
157 prefixline="#define yylex ${prefix}lex"
161 # Construct the name of the header file.
163 echo "Getting header file name"
164 header_file=`dirname "$outfile"`/`basename "$outfile" .c`_lex.h
167 # Spew out the declaration.
169 echo "Writing $header_file"
170 cat <<EOF >$header_file
171 /* This is generated by runlex.sh. Do not edit it. */
172 $prefixline
173 #ifndef YY_DECL
174 #define YY_DECL int yylex(void)
175 #endif
176 YY_DECL;
179 echo "Wrote $header_file"