
#  Copyright (c) 1988-1990 The Regents of the University of California.
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that: (1) source code distributions
#  retain the above copyright notice and this paragraph in its entirety, (2)
#  distributions including binary code include the above copyright notice and
#  this paragraph in its entirety in the documentation or other materials
#  provided with the distribution, and (3) all advertising materials mentioning
#  features or use of this software display the following acknowledgement:
#  ``This product includes software developed by the University of California,
#  Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
#  the University nor the names of its contributors may be used to endorse
#  or promote products derived from this software without specific prior
#  written permission.
#  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# @(#) $Header: /usr/staff/martinh/tcpview/RCS/Makefile,v 1.3 1992/11/17 21:59:47 martinh Exp martinh $ (UW)

# Possible Options:
#  -DCSLIP	include support for kernel slip interface
#  -DPPP	include support for point to point protocol
#  -DIBMRTPC	enable the MERIT additions to the Stanford Enetfilter
#  -DNOVFPRINTF	simulate vfprintf() on systems that don't have it
#
# CSLIP and PPP work only under BPF.					
#
DEFINES = 
CCOPT = -O2
INCLUDES = -I..
LIB =

# These are for the Sun4 version
#CCOPT = -O2 -Bstatic
#INCLUDES = -I.. -I/usr/openwin/include
#LIB =  -lresolv

XLIB= -lXm -lXt -lX11

# The installed binary is owned by this group.
GROUP = staff

# Full pathname of where to install the binaries
BINDEST = /usr/local/bin
TCPSLICE_BINDEST = /usr/local/bin

# Full pathname of where to install tcpview support files
TCPVIEW_DIR = /usr/local/tcpview
FILTER_DIR = /usr/local/tcpview/filters

# Full pathname of where to install the manual entries
TCPDUMP_MANDEST = /usr/man/manl/tcpdump.l
TCPVIEW_MANDEST = /usr/man/manl/tcpview.l
TCPSLICE_MANDEST = /usr/man/manl/tcpslice.l

# Standard CFLAGS
STANDARD_CFLAGS = $(CCOPT) $(DEFINES) $(INCLUDES)

# CFLAGS definitions per machine vs. os
#hp300-bsd#CFLAGS = $(STANDARD_CFLAGS)
#mips-ultrix#CFLAGS = $(STANDARD_CFLAGS)
#rt-bsd#CFLAGS = $(STANDARD_CFLAGS) -U__STDC__
#sun3-sunos3#CFLAGS = $(STANDARD_CFLAGS) 
#sun3-sunos4#CFLAGS = $(STANDARD_CFLAGS)
#sun4-sunos4#CFLAGS = $(STANDARD_CFLAGS)
#sun4-bsd#CFLAGS = $(STANDARD_CFLAGS)
#tahoe-bsd#CFLAGS = $(STANDARD_CFLAGS)
#vax-bsd#CFLAGS = $(STANDARD_CFLAGS)
#vax-ultrix#
CFLAGS = $(STANDARD_CFLAGS)
#mips-irix#CFLAGS = $(STANDARD_CFLAGS) -D_BSD_COMPAT

SUBDIRS = hp300-bsd mips-ultrix sun?-sunos? sun4-bsd tahoe-bsd vax-bsd \
    vax-ultrix rt-bsd mips-irix

# Change these to cc/yacc if you don't have gcc, and/or bison.
# DON'T use flex for lex
CC = cc
LEX = lex
YACC = bison -y
MAKE = make

# Script (or program) that returns the machine and os types,
# or just edit in the name yourself.
MD=`./mdtype`
OS=`./ostype`

# Explicitly define compiliation rule since SunOS 4's make doesn't like gcc.
# Also, gcc does not remove the .o before forking 'as', which can be a
# problem if you don't own the file but can write to the directory.
.c.o:
	rm -f $@; $(CC) $(CFLAGS) -c $*.c

# List all C source files here

TCPDUMP_CSRC =	tcpdump.c addrtoname.c hd.c \
	print-ether.c print-ip.c print-arp.c print-tcp.c print-udp.c \
	print-atalk.c print-domain.c print-tftp.c print-bootp.c print-nfs.c \
	print-icmp.c nametoaddr.c print-sl.c print-ppp.c print-rip.c \
	print-snmp.c print-ntp.c print-null.c print-egp.c print-ospf.c \
	print-fddi.c print-sunrpc.c savefile.c util.c etherent.c inet.c \
	gencode.c optimize.c bpf_dump.c bpf_filter.c bpf_image.c \
	sniffer_file.c

TCPVIEW_CSRC = tcpview.c callbacks.c motifutils.c dialogs.c filter-dialog.c \
        print-ether.c print-arp.c print-ip.c print-tcp.c print-udp.c \
        print-nfs.c print-atalk.c print-tftp.c print-sunrpc.c print-rip.c \
        print-icmp.c print-bootp.c print-snmp.c print-ntp.c print-domain.c \
        print-ospf.c print-egp.c tv_addrtoname.c detail-ether.c detail-arp.c \
        detail-ip.c detail-tcp.c detail-udp.c print-fddi.c print-ppp.c \
        print-sl.c print-null.c gencode.c optimize.c nametoaddr.c  \
        savefile.c tv_etherent.c sniffer_file.c hd.c \
        util.c print.c bpf_filter.c getservent.c filter-stream.c text.c \
        inet.c capture.c detail-rip.c detail-icmp.c detail-domain.c detail-snmp.c

TCPSLICE_CSRC = \
	tcpslice.c gwtm2secs.c search.c

TCPSLICE_LINT_SRC = \
	$(TCPSLICE_CSRC) savefile.c bpf_filter.c md.c util.c

TCPDUMP_SRC =	$(TCPDUMP_CSRC) tcpgram.y tcplex.l \
	$(TCPSLICE_CSRC)

TCPVIEW_SRC =	$(TCPVIEW_CSRC) tcpgram.y tcplex.l

FILTER_SRC = hex.c bgp.c generic.c telnet.c

SRC =	$(TCPVIEW_SRC) $(TCPSLICE_CSRC) tcpdump.c etherent.c addrtoname.c \
	bpf_dump.c bpf_image.c $(FILTER_SRC) 	

MSRC =	md-hp300.c md-mips.c md-sun3.c md-sun4.c md-vax.c md-rt.c

PSRC =	pcap-nit.c pcap-pf.c pcap-snit.c pcap-bpf.c pcap-enet.c

OSRC =	os-sunos3.c os-sunos4.c os-bsd.c os-ultrix.c

TCPDUMP_OBJ =	tcpdump.o tcpgram.o tcplex.o addrtoname.o \
	print-ether.o print-ip.o print-arp.o print-tcp.o print-udp.o \
	print-atalk.o print-domain.o print-tftp.o print-bootp.o print-nfs.o \
	print-icmp.o nametoaddr.o print-sl.o print-ppp.o print-rip.o \
	print-snmp.o print-ntp.o print-null.o print-egp.o print-ospf.o \
	print-fddi.o print-sunrpc.o pcap.o md.o os.o \
	savefile.o util.o etherent.o inet.o gencode.o optimize.o \
	bpf_dump.o bpf_filter.o bpf_image.o version.o sniffer_file.o hd.o

TCPVIEW_OBJ = tcpview.o version.o motifutils.o dialogs.o filter-dialog.o \
        print-ether.o print-arp.o print-ip.o print-tcp.o print-udp.o \
        print-nfs.o print-atalk.o print-tftp.o print-sunrpc.o print-rip.o \
        print-icmp.o print-bootp.o print-snmp.o print-ntp.o print-domain.o \
        print-ospf.o print-egp.o tv_addrtoname.o detail-ether.o detail-arp.o \
        detail-ip.o detail-tcp.o detail-udp.o print-fddi.o print-ppp.o \
        print-sl.o print-null.o gencode.o optimize.o nametoaddr.o tcpgram.o \
        savefile.o md.o os.o tv_etherent.o sniffer_file.o hd.o \
        util.o print.o tcplex.o bpf_filter.o getservent.o filter-stream.o text.o \
        pcap.o inet.o capture.o callbacks.o detail-rip.o detail-icmp.o \
	detail-domain.o detail-snmp.o

TCPSLICE_OBJ = \
	tcpslice.o gwtm2secs.o search.o savefile.o bpf_filter.o md.o util.o

HDR =	interface.h appletalk.h addrtoname.h ip_icmp.h \
	bootp.h savefile.h etherproto.h nametoaddr.h \
	etherent.h ntp.h gencode.h extract.h ospf.h tcpview.h motif.h filter.h snmp.h

MHDR =	md-hp300.h md-sun3.h md-sun4.h md-mips.h md-vax.h md-rt.h

OHDR =	os-bsd.h os-sunos3.h os-sunos4.h os-ultrix.h

TAGHDR=	/usr/include/netinet/in.h \
	/usr/include/netinet/udp.h \
	/usr/include/netinet/tcp.h \
	/usr/include/arpa/tftp.h \
	/usr/include/netinet/if_ether.h

# These files are automatically generated.
#
GEN =	tokdefs.h tcpgram.c tcplex.c version.h version.c

AWKS =	atime.awk packetdat.awk send-ack.awk stime.awk

# "all" must come before tcpdump so we can delete it with sed
all: submake

tcpdump: $(TCPDUMP_OBJ)
	$(CC) $(CFLAGS) -o $@ $(TCPDUMP_OBJ) $(LIB)

tcpview: $(TCPVIEW_OBJ)
	$(CC) $(CFLAGS) -o $@ $(TCPVIEW_OBJ) $(XLIB) $(LIB)

tcpslice: $(TCPSLICE_OBJ)
	$(CC) $(CFLAGS) -o $@ $(TCPSLICE_OBJ)

FILTERS: BGP generic telnet

BGP:	bgp.o hex.o
	$(CC) $(CFLAGS) -o $@ bgp.o hex.o

generic:	generic.o hex.o
	$(CC) $(CFLAGS) -o $@ generic.o hex.o

telnet:	telnet.o hex.o
	$(CC) $(CFLAGS) -o $@ telnet.o hex.o

submake: force
	-@dir=$(MD)-$(OS)-tcpdump; \
	if [ ! -d $$dir ]; then ${MAKE} ${MFLAGS} config1; fi; \
	set -x; \
	cd $$dir; ${MAKE} ${MFLAGS} tcpdump; ${MAKE} ${MFLAGS} tcpslice 
	-@dir=$(MD)-$(OS)-tcpview; \
	if [ ! -d $$dir ]; then ${MAKE} ${MFLAGS} config2; fi; \
	set -x; \
	cd $$dir; ${MAKE} ${MFLAGS} DEFINES="${DEFINES} -DTCPVIEW" tcpview; \
	${MAKE} ${MFLAGS} FILTERS

# N.B.- symbolic links are used in the subdirectory rather than VPATH
# because at least one Sun cc compiler puts the .o in the wrong place
# when using VPATH and it's almost impossible to get "make depend" to
# do the right thing.

config1:
	-@md=$(MD); os=$(OS); dir=$$md-$$os-tcpdump; list=; \
	if [ -f /dev/bpf0 ] ; then pcap="bpf"; \
	elif [ $$os = "sunos3" ] ; then pcap="nit"; \
	elif [ $$os = "sunos4" ] ; then pcap="snit"; \
	elif [ $$os = "ultrix" ] ; then pcap="pf"; \
	elif [ -f /usr/include/net/enet.h ] ; then pcap="enet"; \
	else pcap="bpf"; fi; \
	for i in $(TCPDUMP_SRC) $(HDR); do list="../$$i $$list"; done; \
	set -x; mkdir $$dir; chmod ug+w $$dir; ln -s $$list $$dir; \
	ln -s ../md-$$md.c $$dir/md.c; ln -s ../md-$$md.h $$dir/md.h; \
	ln -s ../os-$$os.c $$dir/os.c; ln -s ../os-$$os.h $$dir/os.h; \
	ln -s ../pcap-$$pcap.c $$dir/pcap.c; \
	ln -s ../VERSION $$dir/VERSION; \
	ln -s ../TCPVIEW_VERSION $$dir/TCPVIEW_VERSION; \
	ln -s ../net $$dir/net; \
	sed -e "/^all:/d" -e "s/^#$$dir#//" Makefile >$$dir/Makefile; \
	chmod ug+w $$dir/Makefile; \
	cd $$dir ; ${MAKE} ${MFLAGS} depend1

config2:
	-@md=$(MD); os=$(OS); dir=$$md-$$os-tcpview; list=; \
	if [ -f /dev/bpf0 ] ; then pcap="bpf"; \
	elif [ $$os = "sunos3" ] ; then pcap="nit"; \
	elif [ $$os = "sunos4" ] ; then pcap="snit"; \
	elif [ $$os = "ultrix" ] ; then pcap="pf"; \
	elif [ -f /usr/include/net/enet.h ] ; then pcap="enet"; \
	else pcap="bpf"; fi; \
	for i in $(TCPVIEW_SRC) $(HDR) $(FILTER_SRC); do list="../$$i $$list"; done; \
	set -x; mkdir $$dir; chmod ug+w $$dir; ln -s $$list $$dir; \
	ln -s ../md-$$md.c $$dir/md.c; ln -s ../md-$$md.h $$dir/md.h; \
	ln -s ../os-$$os.c $$dir/os.c; ln -s ../os-$$os.h $$dir/os.h; \
	ln -s ../pcap-$$pcap.c $$dir/pcap.c; \
	ln -s ../VERSION $$dir/VERSION; \
	ln -s ../TCPVIEW_VERSION $$dir/TCPVIEW_VERSION; \
	sed -e "/^all:/d" -e "s/^#$$dir#//" Makefile >$$dir/Makefile; \
	chmod ug+w $$dir/Makefile; \
	cd $$dir ; ${MAKE} ${MFLAGS} depend2

tcplex.c: tcplex.l
	rm -f $@
	$(LEX) $<
	mv -f lex.yy.c tcplex.c

tokdefs.h: tcpgram.c
tcpgram.c: tcpgram.y
	rm -f tcpgram.c tokdefs.h
	$(YACC) -d $<
	mv y.tab.c tcpgram.c
	mv y.tab.h tokdefs.h

version.o: version.c
version.c version.h: VERSION TCPVIEW_VERSION
	rm -f version.c ; sed 's/.*/char version[] = "&";/' VERSION > version.c
	sed 's/.*/char tcpview_version[] = "&";/' TCPVIEW_VERSION >> version.c
	set `sed 's/\([0-9]*\)\.\([0-9]*\).*/\1 \2/' VERSION` ; \
		{ echo '#define VERSION_MAJOR' $$1 ; \
		  echo '#define VERSION_MINOR' $$2 ; } > version.h

# in case mkdep doesn't work
savefile.o: version.h
tcpslice.o: version.h

install: force
	-mkdir -p $(TCPVIEW_DIR)
	-mkdir -p $(FILTER_DIR) 
	@dir=$(MD)-$(OS)-tcpdump; set -x; \
	cd $$dir ; \
	    install -m 755 -g $(GROUP) tcpdump $(DESTDIR)$(BINDEST); \
	    install -m 755 tcpslice $(DESTDIR)$(TCPSLICE_BINDEST)
	@dir=$(MD)-$(OS)-tcpview; set -x; \
	cd $$dir ; \
	    install -m 755 -g $(GROUP) tcpview $(DESTDIR)$(BINDEST); \
	    install -m 755 -g $(GROUP) BGP $(FILTER_DIR); \
	    install -m 755 -g $(GROUP) telnet $(FILTER_DIR); \
	    install -m 755 -g $(GROUP) generic $(FILTER_DIR); \
	cd ../DOC ; \
	install -m 666 -c hosts $(TCPVIEW_DIR); \
	install -m 666 -c manuf $(TCPVIEW_DIR); \
	install -c tcpdump.1 $(DESTDIR)$(TCPDUMP_MANDEST); \
	install -c tcpview.1 $(DESTDIR)$(TCPVIEW_MANDEST); \
	install -c tcpslice.1 $(DESTDIR)$(TCPSLICE_MANDEST)

lint:	force
	lint -hbxn $(CSRC) | \
	    grep -v 'struct/union .* never defined' | \
	    grep -v 'possible pointer alignment problem'
	lint -hbxn $(TCPSLICE_LINT_SRC) | \
	    grep -v 'struct/union .* never defined' | \
	    grep -v 'possible pointer alignment problem'

# This might be futile
lint.ultrix: force
	lint $(CFLAGS_ULTRIX) $(CSRC) tcplex.c tcpgram.c

clean:
	rm -f *.o tcpdump tcpview tcpslice tcpdump.shar $(GEN) ; \
	rm -rf $(SUBDIRS)

TAGFILES = $(SRC) md.c os.c pcap.c $(HDR) md.h os.h $(TAGHDR) 

tags:	$(TAGFILES)
	ctags -wtd $(TAGFILES)

TARFILES = 	VERSION TCPVIEW_VERSION Makefile mkdep \
		mdtype ostype ostype.awk \
		$(SRC) $(MSRC) $(OSRC) $(PSRC) \
		$(HDR) $(MHDR) $(OHDR) $(FILTER_SRC) \
		SUNOS4 net

tar:	force
	@v=`cat TCPVIEW_VERSION`; dirname=tcpview-$$v; tarname=/tmp/$$dirname.tar; \
	cd .. ; \
	if [ ! -d $$dirname ] ; then \
		ln -s tcpview $$dirname ; \
	fi ; \
	list="" ; \
	for i in $(TARFILES) ; do list="$$list $$dirname/$$i" ; done; \
	set -x; \
	tar cf $$tarname $$list; \
	tar ufhFF $$tarname $$dirname/bpf; \
	tar ufhFF $$tarname $$dirname/DOC; \
	tar ufhFF $$tarname $$dirname/UTIL; \
	rm -f $$tarname.Z; \
	compress $$tarname

force:	/tmp
depend1:	force
	@set -x ; \
	rm -f $(GEN) ; \
	grep '^#include' tcpgram.y > tcpgram.c ; \
	grep '^#include' tcplex.l > tcplex.c ; \
	touch version.h tokdefs.h
	-../mkdep $(DEFINES) $(INCLUDES) $(TCPDUMP_CSRC) $(TCPSLICE_CSRC) \
		tcpgram.c tcplex.c
	rm -f $(GEN)

depend2:	force
	@set -x ; \
	rm -f $(GEN) ; \
	grep '^#include' tcpgram.y > tcpgram.c ; \
	grep '^#include' tcplex.l > tcplex.c ; \
	touch version.h tokdefs.h
	-../mkdep $(DEFINES) $(INCLUDES) $(TCPVIEW_CSRC) \
		tcpgram.c tcplex.c
	rm -f $(GEN)





