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