aboutsummaryrefslogtreecommitdiffstats
path: root/tools/runlex.sh
blob: 48a27b61427571e0a387dc450f9c15ee79ea2abf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/bin/sh

#
# runlex.sh
# Script to run Flex.
# First argument is the (quoted) name of the command; if it's null, that
# means that Flex wasn't found, so we report an error and quit.
# Second arg is the sed executable
#
# Wireshark - Network traffic analyzer
# By Gerald Combs <gerald@wireshark.org>
# Copyright 2007 Gerald Combs
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#

#
# Get the name of the command to run, and then shift to get the arguments.
#
if [ $# -lt 2 ]
then
	echo "Usage: runlex <Flex command to run> <path to sed> [ arguments ]" 1>&2
	exit 1
fi

case "$OS" in

Windows*)
	PATH=$PATH:/bin
	LEX=`cygpath --unix $1`
	echo "$1 -> $LEX"
	;;

*)
	LEX="$1"
	;;
esac

shift
#
# Check whether we have Flex.
#
if [ -z "${LEX}" ]
then
	echo "Flex was not found" 1>&2
	exit 1
fi

SED="$1"
shift
#
# Check whether we have sed.
#
if [ -z "${SED}" ]
then
	echo "Sed was not found" 1>&2
	exit 1
fi

#
# Process the flags.  We don't use getopt because we don't want to
# embed complete knowledge of what options are supported by Flex.
#
flags=""
outfile=lex.yy.c
while [ $# -ne 0 ]
do
	case "$1" in

	-o*)
		#
		# Set the output file name.
		#
		outfile=`echo "$1" | ${SED} 's/-o\(.*\)/\1/'`
		;;

	-*)
		#
		# Add this to the list of flags.
		#
		flags="$flags $1"
		;;

	--|*)
		#
		# End of flags.
		#
		break
		;;
	esac
	shift
done

#
# We make Flex generate a header file declaring the relevant functions
# defined by the .c file, using the --header-file= flag; if the .c file
# is .../foo.c, the header file will be .../foo_lex.h.
#
#echo "Getting header file name"
header_file=`dirname "$outfile"`/`basename "$outfile" .c`_lex.h

#
# OK, run Flex.
#
#echo "Running ${LEX} -o\"$outfile\" --header-file=\"$header_file\" $flags \"$@\""
${LEX} -o"$outfile" --header-file="$header_file" $flags "$@"

#
# Did it succeed?
#
exitstatus=$?
if [ $exitstatus -ne 0 ]
then
	#
	# No.  Exit with the failing exit status.
	#
	echo "${LEX} failed: exit status $exitstatus"
	exit $exitstatus
fi

#
# Flex has the annoying habit of stripping all but the last component of
# the "-o" flag argument and using that as the place to put the output.
# This gets in the way of building in a directory different from the
# source directory.  Try to work around this.
#
# XXX - where is this an issue?
#
#
# Is the outfile where we think it is?
#
outfile_base=`basename "$outfile"`
if [ "$outfile_base" != "$outfile" -a \( ! -r "$outfile" \) -a -r "$outfile_base" ]
then
	#
	# No, it's not, but it is in the current directory.  Put it
	# where it's supposed to be.
	#
echo "Moving $outfile_base to $outfile"
	mv "$outfile_base" "$outfile"
	if [ $? -ne 0 ]
	then
		echo $?
	fi
fi

#
# Is the header file where we think it is?
#
header_file_base=`basename "$header_file"`
if [ "$header_file_base" != "$header_file" -a \( ! -r "$header_file" \) -a -r "$header_file_base" ]
then
	#
	# No, it's not, but it is in the current directory.  Put it
	# where it's supposed to be.
	#
echo "Moving $header_file_base to $header_file"
	mv "$header_file_base" "$header_file"
	if [ $? -ne 0 ]
	then
		echo $?
	fi
fi

echo "Wrote $outfile and $header_file"