#!/usr/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/ccs/bin/lint/lint.sh 1.22.2.14 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1985,1989 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)12        1.22.2.14  src/bos/usr/ccs/bin/lint/lint.sh, cmdprog, bos720 3/22/07 09:31:18
# COMPONENT_NAME: (CMDPROG) Programming Utilities
#
# FUNCTIONS: 
#
# ORIGINS: 3 10 27 32
#
# (C) COPYRIGHT International Business Machines Corp. 1985, 1999
# All Rights Reserved
# Licensed Materials - Property of IBM
#
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
#
# Lint shell script.  Interacts with options from cc(1).
#
LLDIR=/usr/lib                  # location of lint executables
LCPP=/lib/cpp
LINT1=$LLDIR/lint1
LINT2=$LLDIR/lint2
TMP=/usr/tmp/tlint.$$           # preprocessor scratch file
TOUT=/usr/tmp/olint.$$          # combined input for second pass
LIBA=$LLDIR/llib-lansi.ln       # standard ANSI library
LIBE=$LLDIR/llib-lc.ln          # default EXTD library
ERRFLAG=0
DASHFLAG=0

# options for the preprocessor
POPT="-E -C -U__STR__ -U__MATH__ -Dlint -DLINT -D_AIX -D_IBMR2 -D_POWER"

# C++ support
CPPCOMP=/usr/bin/xlC                            # C++ compiler (pass1)
CPPPOPT="-U__STR__ -U__MATH__ -Dlint -DLINT"    # options for the C++ compiler
CPPLLDIR=/usr/lpp/xlC/lib                       # location of lint libraries
CPPLIB=$CPPLLDIR/llib-lC.ln                     # standard C++ library
CPPLIBA="$CPPLIB $CPPLLDIR/llib-lansi.ln"       # standard C++ ANSI C library
CPPLIBE="$CPPLIB $CPPLLDIR/llib-lc.ln"          # default C++ C library

L1OPT=                          # options for the lint passes
L2OPT=                          # options for lint pass2 only
CFILES=                         # the *.c files in order
CPPFILES=                       # the *.C files in order
LFILES=                         # the *.ln files in order
CPPLFILES=                      # the *.ln files in order (for C++)
LIBFILES=                       # the library files in order
CPPLIBFILES=                    # the library files in order (for C++)
LLIB=                           # lint library file to create
WARNS="-wA"                     # lint warning level
CONLY=                          # set for ``compile only''
MANSI=                          # set if ANSI mode
LOOK=                           # set for echo commands only
RC=0                            # Value of 2 to signal failure of lint1
CF=0                            # C files specified on input line
CPPLIBS=0                       # Use C++ lint libraries
ISWARN=0

RM=/usr/bin/rm
DSPMSG=/usr/bin/dspmsg
BASENAME=/usr/bin/basename

trap "$RM -f $TMP $TOUT; exit 2" 1 2 3 15

#
# Process each of the arguments, building lists of options.
#

while [ "" != "$*" ] && [ $DASHFLAG -ne 1 ]

do
        OPT=$1
        case "$OPT" in
        *.c)    CFILES="$CFILES $OPT";;
        *.C)    CPPFILES="$CPPFILES $OPT";;
        *.ln)   LFILES="$LFILES $OPT"
                CPPLFILES="$CPPLFILES $OPT";;
        --)     DASHFLAG=1;;
        -*)     OPT=`echo X$OPT | /usr/bin/sed s/X-//`
                while [ "" != "$OPT" ]
                do
                        O=`echo $OPT | /usr/bin/sed 's/\\(.\\).*/\\1/'`  # option letter
                        OPT=`echo $OPT | /usr/bin/sed s/.//`     # option argument
                        case $O in
                        \#)     LOOK=1;;                # echo commands only
                        c)      CONLY=1;;       # lint1 only, make .ln files
                        p)      L2OPT="$L2OPT -$O";
                                # extreme portability
                                LIBE="$LLDIR/llib-port.ln";
                                CPPLIBE="$CPPLIB $CPPLLDIR/llib-port.ln";;
                        n)      LIBA=                   # no libraries
                                LIBE=
                                CPPLIBA=
                                CPPLIBE=;;
                        v)      L1OPT="$L1OPT -v"       # parameter usage check
                                ;;
                        a)      WARNS="${WARNS}l"     # warning message options
                                ;;
                        w)      ISWARN=`echo $OPT | /usr/bin/grep -e "[^acdhklnoprsuACDOPR]"`
                                if [ "$OPT" = "" ] || [ "$ISWARN" != "" ]
                                then
                                        $DSPMSG lint.cat 110 "Incorrect usage of option: %s\n" $O >&2
                                        exit 1
                                fi 
                                WARNS="$WARNS$OPT"    # warning message options
                                break;;
                        b)      WARNS="${WARNS}R"    # warning message options
                                ;;
                        [hu])   WARNS="$WARNS$O"    # warning message options
                                ;;
                        x)      WARNS="${WARNS}D"    # warning message options
                                ;;
                        [N])   L1OPT="$L1OPT -$O$OPT"  # valid cc(1) options
                                break;;
                        C)      CPPLIBS=1               # use C++ libraries
                                break;;
                        M)      if [ "$OPT" != "A" ]
                                then
                                        $DSPMSG lint.cat 107 "lint: %s option is not recognized\n" "$O$OPT" >&2
                                        exit 1
                                fi
                                L1OPT="$L1OPT -$O$OPT"  # valid cc(1) options
                                POPT="$POPT -qlanglvl=ansi -D_ANSI_C_SOURCE"  #invoke cpp in ansi mode
                                MANSI=1
                                break;;
                        l)      if [ "" != "$OPT" ]     # include a lint library
                                then
                                    LIBFILES="$LLDIR/llib-l$OPT.ln $LIBFILES"
                                    CPPLIBFILES="$CPPLLDIR/llib-l$OPT.ln $CPPLIBFILES"
                                else
                                    $DSPMSG lint.cat 110 "Incorrect usage of option: %s\n" $O >&2
                                    exit 1
                                fi
                                break;;
                        o)      if [ "" != "$OPT" ]             # make a lint library
                                then
                                        OPT=`$BASENAME $OPT`
                                        LLIB="llib-l$OPT.ln"
                                else if [ "" != "$2" ] # pick up next arg as lib
                                     then
                                        OPT=`$BASENAME $2`
                                        LLIB="llib-l$OPT.ln"
                                        shift
                                     else
                                        # situation of -o with no more args
                                        $DSPMSG lint.cat 110 "Incorrect usage of option: %s\n" $O >&2
                                        exit 1
                                     fi
                                fi
                                break;;
                        g)
                                break;;
                        t)
                                L1OPT="$L1OPT -wacdhklnoprsuCDOPR -t"
                                break;;

                        O)      
                                break;;
                        [IDUq]) if [ "" != "$OPT" ]             # preprocessor options
                                then
                                        POPT="$POPT -$O$OPT"
                                        CPPPOPT="$CPPPOPT -$O$OPT"
                                else if [ "" != "$2" ]
                                     then
                                        POPT="$POPT -$O$2"
                                        CPPPOPT="$CPPPOPT -$O$2"
                                        shift
                                     else
                                        $DSPMSG lint.cat 110 "Incorrect usage of option: %s\n" $O >&2
                                        exit 1
                                     fi
                                fi
                                break;;
                        C)      CPPLIBS=1;;
                        *)      $DSPMSG lint.cat 107 "lint: %s option is not recognized\n" $O >&2
                                exit 1;;
                        esac
                done;;
        *)      $DSPMSG lint.cat 106 "lint: File %s must have a .c, .C or .ln extension. It is ignored.\n" $OPT >&2
                ERRFLAG=1;;
        esac
        shift
done

if [ $DASHFLAG -eq 1 ]
then
        while [ "" != "$*" ]
        do
                OPT=$1
                case "$OPT" in
                *.c)    CFILES="$CFILES $OPT";;
                *.C)    CPPFILES="$CPPFILES $OPT";;
                *.ln)   LFILES="$LFILES $OPT"
                        CPPLFILES="$CPPLFILES $OPT";;
                *)      $DSPMSG lint.cat 106 "lint: File %s must have a .c, .C or .ln extension. It is ignored.\n" $OPT >&2
                        ERRFLAG=1;;
                esac
                shift
        done
fi
        
if [ "" = "$CFILES$CPPFILES$LFILES" ]
then
        $DSPMSG lint.cat 108 "lint: there are no files to process.\n" >&2
        exit 1
fi

#
# Check for full ANSI library.
#
if [ "" != "$MANSI" ]
then
        LIBFILES="$LIBA $LIBFILES"          # standard ANSI library
        CPPLIBFILES="$CPPLIBA $CPPLIBFILES"
else
        LIBFILES="$LIBFILES $LIBE"          # standard EXTD library
        CPPLIBFILES="$CPPLIBFILES $CPPLIBE"
	ver=`/usr/bin/lslpp -L xlC.cpp | /usr/bin/head -3 | /usr/bin/tail -1 | /usr/bin/awk '{print $2}' | \
								/usr/bin/awk 'BEGIN { FS = "." } {print $1}'`
	if [ $ver -lt 6 ]
	then
		POPT="$POPT -D_LONG_LONG"
	fi
fi

#
# Use C++ libraries?
#
if [ $CPPLIBS != 0 ]
then
        LFILES="$CPPLFILES"
        LIBFILES="$CPPLIBFILES"
fi


#
# Is there any need for a C++ compiler, but no compiler?
#
if [ $CPPLIBS != 0 -a ! -d $CPPLLDIR ]
then
        $DSPMSG lint.cat 104 "-C specified, but no C++ lint libraries present\n" >&2
        exit 1
fi

if [ "$CPPFILES" != "" -a ! -x $CPPCOMP ]
then
        $DSPMSG lint.cat 105 ".C files specified, but xlC C++ compiler not present\n" >&2
        exit 1
fi

#
# Run the file through lint1 (lint2).
#
if [ "" != "$CONLY" ]           # run lint1 on *.[cC]'s only producing *.ln's
then
        for i in $CFILES
        do
                FILENAME=`$BASENAME $i`
                T=`$BASENAME $i .c`.ln
                $RM -f $TMP $T
                if [ "" != "$LOOK" ]
                then    
                        echo "( $LCPP $POPT -- $i 2>&1 1>$TMP"
                        echo "$LINT1 $WARNS -f$FILENAME $L1OPT $TMP -L$T )"
                else
                        if [ ! -r $i ]
                        then
                                $DSPMSG lint.cat 109 "Cannot open file %s.\n" $i >&2
                                ERRFLAG=1
                                continue
                        fi
                        ( $LCPP $POPT -- $i 2>&1 1>$TMP
                        $LINT1 $WARNS -f$FILENAME $L1OPT $TMP -L$T )
                        RC=$?
                        if [ $RC -eq 2 ]
                        then
                                exit 2
                        fi
                fi
        done
        for i in $CPPFILES
        do
                T=`$BASENAME $i .C`.ln
                $RM -f $TMP $T
                if [ "" != "$LOOK" ]
                then    
                        echo "$CPPCOMP -c -qlint:$WARNS:$T $CPPPOPT -- $i"
                else
                        if [ ! -r $i ]
                        then
                                $DSPMSG lint.cat 109 "Cannot open file %s.\n" $i >&2
                                ERRFLAG=1
                                continue
                        fi
                        $CPPCOMP -c -qlint:$WARNS:$T $CPPPOPT -- $i
                        RC=$?
                        if [ $RC -gt 1 ]
                        then
                                exit 2
                        fi
                fi
        done
else                    # send all *.[cC]'s through lint1 run all through lint2
        $RM -f $TOUT; /usr/bin/touch $TOUT
        for i in $CFILES
        do      
                FILENAME=`$BASENAME $i`
                $RM -f $TMP
                if [ "" != "$LOOK" ]
                then    
                        echo "( $LCPP $POPT -- $i 2>&1 1>$TMP"
                        echo "$LINT1 $WARNS -f$FILENAME $L1OPT $TMP -L$TOUT )"
                else
                        if [ ! -r $i ]
                        then
                                $DSPMSG lint.cat 109 "Cannot open file %s.\n" $i >&2
                                ERRFLAG=1
                                continue
                        fi
                        ( $LCPP $POPT -- $i 2>&1 1>$TMP
                        $LINT1 $WARNS -f$FILENAME $L1OPT $TMP -L$TOUT )
                        RC=$?
                        if [ $RC -eq 2 ]
                        then
                                $RM -f $TMP $TOUT
                                exit $RC;
                        fi
                fi
        done
        for i in $CPPFILES
        do      
                $RM -f $TMP
                if [ "" != "$LOOK" ]
                then    
                        echo "$CPPCOMP -c -qlint:$WARNS:$TOUT $CPPPOPT -- $i"
                else
                        if [ ! -r $i ]
                        then
                                $DSPMSG lint.cat 109 "Cannot open file %s.\n" $i >&2
                                ERRFLAG=1
                                continue
                        fi
                        $CPPCOMP -c -qlint:$WARNS:$TOUT $CPPPOPT -- $i
                        RC=$?
                        if [ $RC -gt 1 ]
                        then
                                $RM -f $TMP $TOUT
                                exit 2
                        fi
                fi
        done

        if [ "" != "$LOOK" ]
        then    
                echo "$LINT2 $WARNS $L1OPT $L2OPT -- $LFILES $TOUT $LIBFILES"
        else
                if [ ! -s $TOUT ]
                then
                        $RM -f $TOUT
                        TOUT=""
                fi
                if [ "" != "$TOUT" ] || [ "" != "$LFILES" ]
                then
                        $LINT2 $WARNS $L1OPT $L2OPT -- $LFILES $TOUT $LIBFILES
                        if [ $? -eq 2 ]
                        then
                                $RM -f $TMP $TOUT
                                exit 2
                        fi
                        
                        if [ "" != "$LLIB" ]    # make a library of lint1 results
                        then
                                if [ "" != "$TOUT" ]
                                then
                                        /usr/bin/mv $TOUT $LLIB
                                fi
                        fi
                fi
        fi
fi
$RM -f $TMP $TOUT
exit $ERRFLAG