#! /bin/sh
#	Copyright (c) 1984 AT&T
#	  All Rights Reserved

#	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
#	The copyright notice above does not evidence any
#	actual or intended publication of such source code.

#ident	"$Header: lint.sh 1.11 89/12/18 $@(#)lint:lint.sh	1.13"
#
#
# New lint shell script.  Changed to make lint(1) act as much as is possible
# like a different version of the cc(1) command.  This includes the notion of
# a ``lint .o'' (.ln) and incremental linting.  Thu Jan 27 10:07:15 EST 1983
#
: ${PARALLEL=1}
N=00000				# the current number being linted.
TMPDIR=/usr/tmp/lint.$$		# temp dir were linting is done
TOUT=$TMPDIR/tlint		# combined input for second pass
HOUT=$TMPDIR/hlint		# header messages file
MOUT=$TMPDIR/mlint		# msgs from pass1
LDIR=/usr/lib			# where first & second pass are
LLDIR=/usr/lib			# where lint libraries are found
PATH=/bin:/usr/bin
CPP=/lib			# where the preprocessor for cc is
CC=cc
CCF="-E -C -Dlint"		# options for the cc command
ISSEQ=				# flag to enable Sequent key words
LINTF=				# options for the lint passes
HLINTF=				# list of header files for pass 2
FILES=				# the *.c and *.ln files in order
DEFL=$LLDIR/llib-lc.ln		# the default library to use
LLIB=				# lint library file to create
CONLY=				# set for ``compile only''
pre=				# these three variables used for
post=				# handling options with arguments
optarg=				# list variable to add argument to
#
DIRLST=				# ordered list of args to -L option
LIBLST=				# lint libs to look for in DIRLST
#
trap "rm -rf $TMPDIR; exit 2" 1 2 3 15
mkdir $TMPDIR
#
# First, run through all of the arguments, building lists
#
#	lint's options are "Labchl:no:puvx"
#	cc/cpp options are "I:D:U:gO"
#
for OPT in "$@"
do
	if [ "$optarg" ]
	then
		if [ "$optarg" = "LLIB" ]	# special case...
		then
			OPT=`basename $OPT`
			eval "$optarg=\"\$$optarg \$pre\$OPT\$post\""
		elif [ "$optarg" = "LLDIR" ]	# special case...
		then
			DIRLST="$DIRLST $OPT"
		else
		    eval "$optarg=\"\$$optarg \$pre\$OPT\$post\""
		fi
		pre=
		post=
		optarg=
		continue
	fi
	case "$OPT" in
	CPP=*)	CPP="`echo $OPT | sed -e s/CPP=//`"
		CCF="$CCF -Yp,$CPP" ;;
	-Wp,*)	CCF="$CCF $OPT" ;;
	LL=*)	LDIR="`echo $OPT | sed -e s/LL=//`" ;;
	LLDIR=*) LLDIR="`echo $OPT | sed -e s/LLDIR=//`" 
		DEFL=$LLDIR/llib-lc.ln;;
	CC=*)	CC="`echo $OPT | sed -e s/CC=//`" ;;
	-Y*)	CC="$CC `echo $OPT`" ;;
	-Wc,-seq)	ISSEQ="-s" ;;
	-Wc,-fpa)	CCF="$CCF -Ui387 -Dw1167" ;;
	-Wc*)	;;
	*.c)	FILES="$FILES $OPT" ;;
	*.ln)	FILES="$FILES $OPT";;
	-*)	OPT=`echo $OPT | sed s/-//p`
		while [ "$OPT" ]
		do
			O=`echo $OPT | sed "s/\(.\).*/\1/p"`
			OPT=`echo $OPT | sed s/.//p`
			case $O in
			p)	LINTF="$LINTF -p"
				CCF="$CCF -Wp,-T"
				DEFL=$LLDIR/llib-port.ln;;
			n)	LINTF="$LINTF -n"
				DEFL=;;
			c)	CONLY=1;;
			[abhuvx]) LINTF="$LINTF -$O";;
			[gO])	CCF="$CCF -$O";;
			[IDU])	if [ "$OPT" ]
				then
					CCF="$CCF -$O$OPT"
				else
					optarg=CCF
					pre=-$O
				fi
				break;;
			l)	if [ "$OPT" ]	
				then
					LIBLST="$LIBLST llib-l$OPT.ln"
				else
					optarg=LIBLST
					pre=llib-l
					post=.ln
				fi
				break;;
			o)	if [ "$OPT" ]
				then
					OPT=`basename $OPT`
					LLIB="llib-l$OPT.ln"
				else
					LLIB=
					optarg=LLIB
					pre=llib-l
					post=.ln
				fi
				break;;
			L)	if [ "$OPT" ]
				then
					DIRLST="$DIRLST $OPT"
				else
					optarg="LLDIR"
				fi
				break;;
			*)	echo "lint: bad option ignored: $O";;
			esac
		done;;
	*)	echo "lint: file with unknown suffix ignored: $OPT";;
	esac
done

LLDIR="$DIRLST $LLDIR"

for LIB in $LIBLST
do
    for DIR in $LLDIR
    do
	if [ -r "$DIR"/"$LIB" ]
	then
	    FILES="$FILES  $DIR/$LIB"
	    break
	fi
    done
    if [ ! -r "$DIR"/"$LIB" ]
    then
	echo "$0:  $LIB not found"
    fi
done

#
# Second, walk through the FILES list, running all .c's through
# lint's first pass, and just adding all .ln's to the running result
#
if [ "$CONLY" ]		# run lint1 on *.c's only producing *.ln's
then
	for i in $FILES
	do
		case $i in
		*.c)	T=`basename $i .c`.ln
			(echo "$i:"; $CC $CCF $i | $LDIR/lint1 $ISSEQ $LINTF -H$HOUT.$N $i >$T) >> $MOUT.$N 2>&1 &
			HLINTF="$HLINTF -H$HOUT.$N"
			N=`awk "END { printf \"%05d\", $N + 1 }" < /dev/null` ;;
		esac
	done
	wait
	cat $TMPDIR/mlint.*
	$LDIR/lint2 $HLINTF
	rm -rf $TMPDIR
else			# send all *.c's through lint1 run all through lint2
	rm -f $TOUT $HOUT
	for i in $FILES
	do
		case $i in
		*.ln)	cat <$i >>$TOUT;;
		*.c)
			(echo "$i:"; $CC $CCF $i|$LDIR/lint1 $ISSEQ $LINTF -H$HOUT.$N $i >>$TOUT.$N) >> $MOUT.$N 2>&1 &
			HLINTF="$HLINTF -H$HOUT.$N"
			N=`awk "END { printf \"%05d\", $N + 1 }" < /dev/null` ;;
		esac
	done
	wait
	cat $TMPDIR/mlint.*
	cat $TMPDIR/tlint.* >> $TOUT
	if [ "$LLIB" ]
	then
		cp $TOUT $LLIB
	fi
	if [ "$DEFL" ]
	then
		cat <$DEFL >>$TOUT
	fi
	$LDIR/lint2 -T$TOUT $LINTF $HLINTF
fi
rm -rf $TMPDIR
