Catch retransmission of FINs, so if we're doing "reassemble until end of
[metze/wireshark/wip.git] / tools / runlex.sh
1 #! /bin/sh
2
3 #
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.
9 #
10 # $Id$
11 #
12 # Wireshark - Network traffic analyzer
13 # By Gerald Combs <gerald@wireshark.org>
14 # Copyright 2007 Gerald Combs
15 #
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.
20 #
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.
25 #
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.
29 #
30
31 #
32 # Get the name of the command to run, and then shift to get the arguments.
33 #
34 if [ $# -eq 0 ]
35 then
36         echo "Usage: runlex <lex/flex command to run> [ arguments ]" 1>&2
37         exit 1
38 fi
39 LEX="$1"
40 shift
41
42 #
43 # Check whether we have it.
44 #
45 if [ -z "${LEX}" ]
46 then
47         echo "Neither lex nor flex was found" 1>&2
48         exit 1
49 fi
50
51 #
52 # Process the flags.  We don't use getopt because we don't want to
53 # embed complete knowledge of what options are supported by Lex/Flex.
54 #
55 flags=""
56 outfile=lex.yy.c
57 while [ $# -ne 0 ]
58 do
59         case "$1" in
60
61         -o*)
62                 #
63                 # Set the output file name.
64                 #
65                 outfile=`echo "$1" | sed 's/-o\(.*\)/\1/'`
66                 ;;
67
68         -*)
69                 #
70                 # Add this to the list of flags.
71                 #
72                 flags="$flags $1"
73                 ;;
74
75         --|*)
76                 #
77                 # End of flags.
78                 #
79                 break
80                 ;;
81         esac
82         shift
83 done
84
85 #
86 # OK, run it.
87 #
88 echo "Running ${LEX}"
89 ${LEX} -o"$outfile" $flags "$@"
90
91 #
92 # Did it succeed?
93 #
94 if [ $? -ne 0 ]
95 then
96         #
97         # No.  Exit with the failing exit status.
98         #
99         exitstatus=$?
100         echo "${LEX} failed: exit status $exitstatus"
101         exit $exitstatus
102 fi
103
104 #
105 # Flex has the annoying habit of stripping all but the last component of
106 # the "-o" flag argument and using that as the place to put the output.
107 # This gets in the way of building in a directory different from the
108 # source directory.  Try to work around this.
109 #
110 # Is the outfile where we think it is?
111 #
112 outfile_base=`basename "$outfile"`
113 if [ "$outfile_base" != "$outfile" -a \( ! -r "$outfile" \) -a -r "$outfile_base" ]
114 then
115         #
116         # No, it's not, but it is in the current directory.  Put it
117         # where it's supposed to be.
118         #
119         mv "$outfile_base" "$outfile"
120         if [ $? -ne 0 ]
121         then
122                 echo $?
123         fi
124 fi
125
126 echo "Wrote $outfile"
127
128 #
129 # OK, now let's generate a header file declaring the relevant functions
130 # defined by the .c file; if the .c file is .../foo.c, the header file
131 # will be .../foo_lex.h.
132 #
133 # This works around some other Flex suckage, wherein it doesn't declare
134 # the lex routine before defining it, causing compiler warnings.
135 # XXX - newer versions of Flex support --header-file=, to generate the
136 # appropriate header file.  With those versions, we should use that option.
137 #
138
139 #
140 # Get the name of the prefix; scan the source files for a %option prefix
141 # line.  We use the last one.
142 #
143 echo "Getting prefix"
144 prefix=`sed -n 's/%option[      ][      ]*prefix="\(.*\)".*/\1/p' "$@" | tail -1`
145 if [ ! -z "$prefix" ]
146 then
147         prefixline="#define yylex ${prefix}lex"
148 fi
149
150 #
151 # Construct the name of the header file.
152 #
153 echo "Getting header file name"
154 header_file=`dirname "$outfile"`/`basename "$outfile" .c`_lex.h
155
156 #
157 # Spew out the declaration.
158 #
159 echo "Writing $header_file"
160 cat <<EOF >$header_file
161 /* This is generated by runlex.sh.  Do not edit it. */
162 $prefixline
163 #ifndef YY_DECL
164 #define YY_DECL int yylex(void)
165 #endif
166 YY_DECL;
167 EOF
168
169 echo "Wrote $header_file"