#!/bin/sh
# (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
# See the copyright notice in the ACK home directory, in the file "Copyright".
#
# $Header$

#	L I N T   D R I V E R

PATH=/bin:/usr/bin

EMDIR=/usr/em
EMLINT=$EMDIR/lib.bin/lint
INCLUDES=-I$EMDIR/include/_tail_cc

#LDEFMACH=-Dmc68000

PARAMS1=$LDEFMACH
LPASS1=${LPASS1-"$EMLINT/lpass1"}		# pass 1 program
LPASS2=${LPASS2-"$EMLINT/lpass2"}		# pass 2 program
LLIB=${LLIB-"$EMLINT/llib"}			# lint libraries directory

LINTLIB=${LINTLIB-$LLIB}
CLIB=c.llb

TMP=/usr/tmp/lint1.$$
NEW=/usr/tmp/lint2.$$

trap "rm -f $TMP $NEW; exit 1" 1 2 15
trap "rm -f $TMP $NEW; exit 0" 0

set dummy $LINTFLAGS $@			# dummy as a shield for $LINTFLAGS
shift					# remove dummy

LIBRARY=

# get the non-library options
while	test -n "$1"
do
	case "$1" in
	-ansi)	LPASS1=${LPASS1}.ansi
		INCLUDES=-I$EMDIR/include/tail_ac
		CLIB=ansi_c.llb
		shift
		;;
	-l*)	# library parameter; start pass 1
		break
		;;
	-KR)	# strictly Kernighan & Ritchie, pass 1
		PARAMS1="$PARAMS1 -R"
		shift
		;;
	-[DUI]*)# Define, Undef and Include; for pass 1 only
		PARAMS1="$PARAMS1 $1"
		shift
		;;
	-L*)	# make a lint library
		LIBRARY=`expr "$1" : '-L\(.*\)'`
		shift
		;;
	-*)	# for pass 1 or pass 2
		PARAMS1="$PARAMS1 $1"
		PARAMS2="$PARAMS2 $1"
		shift
		;;
	*)	# input file; start pass 1
		break
		;;
	esac
done

case "$LIBRARY" in
'')	# normal lint; we want its messages on stdout; this takes some doing
	(	# intermediate file has to go to stdout for pipe connection
		(	# pass 1: messages to stderr
			LIBC=true	# true if $CLIB to be included
			STATNR=0	# static scope number

			for F in $*
			do
				case $F in
				-l)	# do NOT include $CLIB
					LIBC=false
					;;
				-lc)	# do include $CLIB
					LIBC=true
					;;
				-l*)	# include special lint library
					cat $LINTLIB/`expr $F : '-l\(.*\)'`.llb
					;;
				*.c)	# a real C-file
					STATNR=` expr $STATNR + 1 `
					$LPASS1 -S$STATNR -Dlint $PARAMS1 $INCLUDES $F
					;;
				*)	# a lint library?
					case `basename $F` in
					*.llb)	# yes, it is
						cat $F
						;;
					*)
						echo $0: unknown input $F >&2
						;;
					esac
					;;
				esac
			done

			case "$LIBC" in
			true)	# append $CLIB
				cat $LINTLIB/$CLIB
				;;
			esac
		) |
		sort -u |
		(	# pass 2: divert messages to avoid interleaving
			$LPASS2 $PARAMS2 2>$TMP
		)
	) 2>&1				# messages pass 1 to stdout

	# append messages pass 2
	cat $TMP
	;;

?*)	# making a lint library
	set -e				# stop at first sign of trouble

	case `basename $LIBRARY` in
	*.llb)	# OK
		;;
	*)	# no suffix .llb
		LIBRARY=$LIBRARY.llb
		;;
	esac

	if	/bin/test ! -r $LIBRARY
	then	cp /dev/null $LIBRARY
	fi

	# collect pass 1 intermediate output for all input files
	for F in $@
	do
		case $F in
		*.c)	# a C file
			$LPASS1 $PARAMS1 $INCLUDES -Dlint -L $F
			;;
		*)	# a library?
			case `basename $F` in
			*.llb)	# yes, it is
				cat $F
				;;
			*)
				echo $0: unknown input $F >&2
				;;
			esac
			;;
		esac
	done >$NEW

	# get the last line for each name and sort them
	cat $LIBRARY $NEW |
	awk -F: '
		{
		entry[$1] = $0;
	}
	END	{
		for (e in entry) {print entry[e];}
	}
	' |
	sort |
	grep -v '^main:' >$TMP

	cp $TMP $LIBRARY

esac

rm -f $TMP $NEW

