dect
/
libpcap
Archived
13
0
Fork 0

Add a wrapper around Lex/Flex; that lets us handle Lex's lack of support

for -o and Flex's support for it in a way that lets us more easily fail
if Lex/Flex fails (so that we don't try to compile a bogus scanner.c
that might be generated; that appears to have happened on at least one
occasion, with the resulting scanner.o missing some functions, causing
weird errors in configure scripts for programs using libpcap), and also
prepares us to handle newer versions of Flex where we want Flex to
generate a header file so we don't get "defined but not declared"
warnings.
This commit is contained in:
guy 2007-12-30 00:28:17 +00:00
parent 25ab78f4f7
commit cfada28fd7
5 changed files with 204 additions and 4 deletions

View File

@ -6,6 +6,7 @@ config.h
stamp-h
stamp-h.in
Makefile
scanner.h
scanner.c
grammar.c
tokdefs.h

1
FILES
View File

@ -116,6 +116,7 @@ pcap.3
pcap.c
pcap.h
ppp.h
runlex.sh
savefile.c
scanner.l
sunatmpos.h

View File

@ -1,4 +1,4 @@
@(#) $Header: /tcpdump/master/libpcap/INSTALL.txt,v 1.22 2007-10-20 01:15:14 guy Exp $ (LBL)
@(#) $Header: /tcpdump/master/libpcap/INSTALL.txt,v 1.23 2007-12-30 00:28:17 guy Exp $ (LBL)
To build libpcap, run "./configure" (a shell script). The configure
script will determine your system attributes and generate an
@ -392,6 +392,7 @@ pcap.3 - manual entry
pcap.c - pcap utility routines
pcap.h - header for backwards compatibility
ppp.h - Point to Point Protocol definitions
runlex.sh - wrapper for Lex/Flex
savefile.c - offline support
scanner.l - filter string scanner
sunatmpos.h - definitions for SunATM capturing

View File

@ -17,7 +17,7 @@
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# @(#) $Header: /tcpdump/master/libpcap/Makefile.in,v 1.108 2007-07-24 02:36:34 mcr Exp $ (LBL)
# @(#) $Header: /tcpdump/master/libpcap/Makefile.in,v 1.109 2007-12-30 00:28:17 guy Exp $ (LBL)
#
# Various configurable paths (remember to edit Makefile.in, not Makefile)
@ -90,7 +90,7 @@ OBJ = $(PSRC:.c=.o) $(FSRC:.c=.o) $(CSRC:.c=.o) $(SSRC:.c=.o) $(GENSRC:.c=.o) $(
HDR = pcap.h pcap-int.h pcap-namedb.h pcap-nit.h pcap-pf.h \
ethertype.h gencode.h gnuc.h
GENHDR = \
tokdefs.h version.h
scanner.h tokdefs.h version.h
TAGHDR = \
pcap-bpf.h
@ -126,7 +126,7 @@ libpcap.dylib: $(OBJ)
scanner.c: $(srcdir)/scanner.l
@rm -f $@
$(LEX) -t $< > $$$$.$@; mv $$$$.$@ $@
./runlex.sh "$(LEX)" -o$@ $<
scanner.o: scanner.c tokdefs.h
$(CC) $(CFLAGS) -c scanner.c

197
runlex.sh Executable file
View File

@ -0,0 +1,197 @@
#! /bin/sh
#
# runlex.sh
# Script to run Lex/Flex.
# First argument is the (quoted) name of the command; if it's null, that
# means that neither Flex nor Lex was found, so we report an error and
# quit.
#
# @(#) $Header: /tcpdump/master/libpcap/runlex.sh,v 1.1 2007-12-30 00:28:17 guy Exp $
#
#
# Get the name of the command to run, and then shift to get the arguments.
#
if [ $# -eq 0 ]
then
echo "Usage: runlex <lex/flex command to run> [ arguments ]" 1>&2
exit 1
fi
LEX="$1"
shift
#
# Check whether we have it.
#
if [ -z "${LEX}" ]
then
echo "Neither lex nor flex was 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 Lex/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
#
# OK, run it.
# If it's lex, it doesn't support -o, so we just write to
# lex.yy.c and, if it succeeds, rename it to the right name,
# otherwise we remove lex.yy.c.
# If it's flex, it supports -o, so we use that - flex with -P doesn't
# write to lex.yy.c, it writes to a lex.{prefix from -P}.c.
#
if expr "${LEX}" : flex >/dev/null
then
#
# "flex" appears at the beginning of ${LEX}, so it's flex.
#
${LEX} $flags -o"$outfile" "$@"
#
# Did it succeed?
#
status=$?
if [ $status -ne 0 ]
then
#
# No. Exit with the failing exit status.
#
exit $status
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.
#
# 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.
#
mv "$outfile_base" "$outfile"
#
# Did that succeed?
#
status=$?
if [ $status -ne 0 ]
then
#
# No. Exit with the failing exit status.
#
exit $status
fi
fi
else
${LEX} $flags "$@"
#
# Did it succeed?
#
status=$?
if [ $status -ne 0 ]
then
#
# No. Get rid of any lex.yy.c file we generated, and
# exit with the failing exit status.
#
rm -f lex.yy.c
exit $status
fi
#
# OK, rename lex.yy.c to the right output file.
#
mv lex.yy.c "$outfile"
#
# Did that succeed?
#
status=$?
if [ $status -ne 0 ]
then
#
# No. Get rid of any lex.yy.c file we generated, and
# exit with the failing exit status.
#
rm -f lex.yy.c
exit $status
fi
fi
#
# OK, now let's generate a header file declaring the relevant functions
# defined by the .c file; if the .c file is .../foo.c, the header file
# will be .../foo.h.
#
# This works around some other Flex suckage, wherein it doesn't declare
# the lex routine before defining it, causing compiler warnings.
# XXX - newer versions of Flex support --header-file=, to generate the
# appropriate header file. With those versions, we should use that option.
#
#
# Get the name of the prefix; scan the source files for a %option prefix
# line. We use the last one.
#
prefix=`sed -n 's/%option[ ][ ]*prefix="\(.*\)".*/\1/p' "$@" | tail -1`
if [ ! -z "$prefix" ]
then
prefixline="#define yylex ${prefix}lex"
fi
#
# Construct the name of the header file.
#
header_file=`dirname "$outfile"`/`basename "$outfile" .c`.h
#
# Spew out the declaration.
#
cat <<EOF >$header_file
/* This is generated by runlex.sh. Do not edit it. */
$prefixline
#ifndef YY_DECL
#define YY_DECL int yylex(void)
#endif
YY_DECL;
EOF