home *** CD-ROM | disk | FTP | other *** search
/ IRIS Development Option 6.2 / IRIS_Development_Option_6.2_814-0478-001.iso / dist / c_dev.idb / usr / bin / lint.z / lint
Text File  |  1996-03-14  |  9KB  |  349 lines

  1. #!/usr/bin/sh
  2. #Tag 1568
  3. #ident    "@(#)alint:common/lint    1.23"
  4.  
  5. PATH=/bin:/usr/bin
  6. if [ "$TMPDIR" = "" ]
  7. then
  8.     unixver=`uname -r`
  9.     case $unixver in
  10.     4*) TMPDIR=/var/tmp ;;
  11.     3*) TMPDIR=/usr/tmp ;;
  12.     *)  TMPDIR=/usr/tmp ;;    # uts, etc.
  13.     esac
  14. fi
  15. #LPASS=${CCSLIB:-/usr/ccs/lib}    # where pass 1 and pass2 are found
  16. #LLDIR=${CCSLIB:-/usr/ccs/lib}    # where lint libraries are found
  17. LPASS=${LPASSDIR:-$TOOLROOT/usr/lib/lint} # where pass 1 and pass2 are found
  18. LLDIR=${LIBDIR:-$ROOT/usr/lib/lint}       # where lint libraries are found
  19. TOUT=$TMPDIR/tlint.$$        # combined input for second pass
  20. T1=$TMPDIR/tlint1$$        # definitions from C source
  21. T2=$TMPDIR/tlint2$$        # tentative definitions from C source
  22. T3=$TMPDIR/tlint3$$        # function calls, misc
  23. T4=$TMPDIR/tlint4$$        # strings for printf/scanf checking
  24. HOUT=$TMPDIR/hlint$$        # header messages file
  25. NOUT=$TMPDIR/nlint$$        # holds names of included files
  26. LINT1=$LPASS/lint1        # pass 1 of lint
  27. LINT2=$LPASS/lint2        # pass 2 of lint
  28.  
  29. CPPF="-C -Alint(on)"        # options for cpp part of lint1
  30. LINTF=                # options for the lint passes
  31. FILES=                # the *.c and *.ln files in order
  32. NDOTC=0                # how many *.c were there
  33. DEFL=$LLDIR/llib-lc.ln        # the default lint library to use
  34. LLIB=                # lint library file to create
  35. LNOUT=                # for cflow
  36. CXOUT=                # for cxref
  37. CONLY=                # set for compile only (no second pass)
  38. PREONLY=            # run cpp only on file
  39. FULLPATH=            # set if -F option
  40. DIRLST=                # ordered list of args to -L option
  41. LIBLST=                # lint libs to look for in DIRLST
  42. RETVAL=
  43. EXIT=0                # Normal exit code
  44. VERSION=
  45. VERBOSE=            # Echo lint1 and lint2 commands
  46. ERRORS=                # list of files with errors in them
  47. AMODE="a"            # ANSI modes: -Xt, -Xa, -Xc
  48. TMPFILES="$TOUT $HOUT $NOUT $T1 $T2 $T3 $T4"
  49.  
  50. trap "rm -f $TMPFILES; exit 2" 1 2 3 15
  51.  
  52. USAGE="Usage: lint [-abcdhkmnpsuvxyFV] [-lx] [-o lib] [-L libdir] file ..."
  53. BADUSE="lint: file with unknown suffix ignored:"
  54. # OPTARG=abcghkl:mno:spuvxyD:FVI:L:U:O1:2:X:PW:R:Y:E
  55. OPTARG=abcdghkl:mno:spuvxyD:FVI:L:U:O1:2:X:PY:E
  56.  
  57. # Remove cc options that may be misinterpreted as lint options,
  58. # and set OPTARG.
  59. #
  60. # set -- `getopt $OPTARG "$@"`
  61. #
  62. ARGS=`echo "$@" | sed -e 's/-nostdinc//g'`
  63. set -- `getopt $OPTARG $ARGS`
  64.  
  65. if [ $? -ne 0 ]
  66. then
  67.     echo $USAGE >&2
  68.     exit 2
  69. fi
  70. while [ $# -gt 0 ]
  71. do
  72.     case $1 in
  73.     -p)    LINTF="$LINTF $1"; shift;;
  74.     -n)    shift; DEFL=;;
  75.     -c)    shift; CONLY=1;;
  76. #
  77. # W option for cfloW only - makes output go to specified file
  78. # R option for cxRef only - makes identifier output go to specified file
  79. #    --- We support neither of these options! (rdahl)
  80. #
  81. #    -W)    LINTF="$LINTF $1"; LNOUT="$2"; shift 2;;
  82. #    -R)    LINTF="$LINTF $1$2"; CXOUT="$2"; shift 2;;
  83.     -[abhkmsuvxy]) LINTF="$LINTF $1"; shift;;
  84.     -d)     VERBOSE=1; shift;;
  85.     -V)    LINTF="$LINTF $1"; VERSION=1; shift;;
  86.     -F)    LINTF="$LINTF $1"; FULLPATH=1; shift;;
  87.     -[IDUY]) CPPF="$CPPF $1$2"; shift 2;;
  88.     -[PE])    CPPF="$CPPF $1"; shift; PREONLY=1;;
  89.     -L)    DIRLST="$DIRLST $2"; shift 2;;
  90.     -l)    LIBLST="$LIBLST llib-l$2.ln"; shift 2;;
  91.     -o)    OPT=`basename $2`; LLIB="llib-l$2.ln"; shift 2;;
  92.     -[12])    LINTF="$LINTF $1$2"; shift 2;;
  93.     -X)    LINTF="$LINTF $1$2"; AMODE=$2; shift 2;;
  94. #
  95. # some cc options (mostly) to be ignored.  (Should have separate processing
  96. # to handle -woff).
  97. #
  98.     -[Og])    shift;;
  99. #    -w)    shift 3;;
  100. #    -cckr)    LINTF="$LINTF "-Xt"; AMODE="t"; shift;;
  101. #    -xansi)    LINTF="$LINTF "-Xa"; AMODE="a"; shift;;
  102. #    -ansi)    LINTF="$LINTF "-Xc"; AMODE="c"; shift;;
  103.     --)    shift;
  104.         while [ $# -gt 0 ]
  105.         do
  106.             case $1 in    
  107.             -*)    set -- `getopt $OPTARG "$@"`;
  108.                 if [ $? -ne 0 ]
  109.                 then
  110.                     echo $USAGE >&2
  111.                     exit 2
  112.                 fi
  113.                 break;;
  114.             *.[ci])    FILES="$FILES $1"
  115.                 NDOTC=`expr $NDOTC + 1`
  116.                 shift;;
  117.             *.ln)    FILES="$FILES $1"; shift;;
  118. #
  119. # If making a lint library, then allow any suffix.  This is because
  120. # lint libraries typically look like: llib-lX where X stands for the
  121. # library (i.e. llib-lc, llib-lm, llib-lmalloc, etc ...)
  122.             *)    if [ ! "$LLIB" ]
  123.                 then
  124.                     echo $BADUSE $1 >&2
  125.                 else
  126.                     FILES="$FILES $1"
  127.                     NDOTC=`expr $NDOTC + 1`
  128.                 fi
  129.                 shift;;
  130.             esac
  131.         done
  132.         ;;
  133.     -*)    echo "lint: bad option ignored: $1" >&2;;
  134.     esac
  135. done
  136.  
  137. # Set default include search path (after processing the -I options above).
  138. # (rdahl)
  139. CPPF="$CPPF -I""$ROOT/usr/include"
  140.  
  141. # Define the same symbols the driver defines for cfe, except __LONGLONG. 
  142. # (rdahl)
  143. if [ "$AMODE" = "c" ]
  144. then
  145.     CPPF="$CPPF -D_MIPS_FPSET=16 -D_MIPS_ISA=_MIPS_ISA_MIPS1 -D_MIPS_SIM=_MIPS_SIM_ABI32 -D_MIPS_SZINT=32 -D_MIPS_SZLONG=32 -D_MIPS_SZPTR=32 -D_LANGUAGE_C -D__sgi -D__unix -D__host_mips -D_MODERN_C -D__DSO__ -D_SYSTYPE_SVR4 -D__mips=1 -D_MIPSEB -D_CFE -D__ANSI_CPP__"
  146. fi
  147. if [ "$AMODE" = "a" ]
  148. then
  149.     CPPF="$CPPF -D_MIPS_FPSET=16 -D_MIPS_ISA=_MIPS_ISA_MIPS1 -D_MIPS_SIM=_MIPS_SIM_ABI32 -D_MIPS_SZINT=32 -D_MIPS_SZLONG=32 -D_MIPS_SZPTR=32 -D__EXTENSIONS__ -DLANGUAGE_C -D_LANGUAGE_C -Dsgi -D__sgi -Dunix -Dmips -Dhost_mips -D__unix -D__host_mips -D_SVR4_SOURCE -D_MODERN_C -D_SGI_SOURCE -D__DSO__ -DSYSTYPE_SVR4 -D_SYSTYPE_SVR4 -D__mips=1 -D_MIPSEB -DMIPSEB -D_CFE -D__ANSI_CPP__"
  150. fi
  151. if [ "$AMODE" = "t" ]
  152. then
  153.     CPPF="$CPPF -D_MIPS_FPSET=16 -D_MIPS_ISA=_MIPS_ISA_MIPS1 -D_MIPS_SIM=_MIPS_SIM_ABI32 -D_MIPS_SZINT=32 -D_MIPS_SZLONG=32 -D_MIPS_SZPTR=32 -D__EXTENSIONS__ -DLANGUAGE_C -D_LANGUAGE_C -Dsgi -D__sgi -Dunix -Dmips -Dhost_mips -D__unix -D__host_mips -D_SVR4_SOURCE -D_MODERN_C -D_SGI_SOURCE -D__DSO__ -DSYSTYPE_SVR4 -D_SYSTYPE_SVR4 -D__mips=1 -D_MIPSEB -DMIPSEB -D_CFE -U__STDC__"
  154. fi
  155.  
  156. #
  157. # Only use -Dlint if this isn't running as cxref (CXOUT) or
  158. # as cflow (LNOUT) or mode is not -Xc
  159. #
  160. if [ "$LNOUT" = "" -a "$CXOUT" = "" -a "$AMODE" != "c" ]
  161. then
  162.     CPPF="$CPPF -Dlint"
  163. fi
  164.     
  165.  
  166. # Give something to lint to do!
  167. if [ "$FILES" = "" ]
  168. then
  169.     if [ "$VERSION" = "" ]
  170.     then
  171.         echo "lint: no file arguments" >&2
  172.         exit 1
  173.     else
  174.         echo "$LINT1 -V"
  175.         $LINT1 -V
  176.         exit 0
  177.     fi
  178. fi
  179.  
  180. #
  181. # Check to see if all the lint libraries specified on the command line
  182. # can be found either in the initial lint directory (normally /usr/ccs/lib),
  183. # /usr/lib, or in one of the user specified directories (via -L <dir>).
  184. #
  185. #LLDIR="$DIRLST $LLDIR /usr/lib"
  186. LLDIR="$DIRLST $LLDIR/lint $LLDIR /usr/lib/lint /usr/lib"
  187. for LIB in $LIBLST
  188. do
  189.     for DIR in $LLDIR
  190.     do
  191.         if [ -r "$DIR"/"$LIB" ]
  192.         then
  193.             FILES="$FILES $DIR/$LIB"
  194.             break
  195.         fi
  196.     done
  197.     if [ ! -r "$DIR"/"$LIB" ]
  198.     then
  199.         echo "lint: $LIB not found" >&2
  200.     fi
  201. done
  202.  
  203. #
  204. # Run pass1 only, creating .ln files.
  205. # Any .ln files on the command line will be ignored.
  206. #
  207. if [ "$CONLY" -o "$PREONLY" ]
  208. then
  209.     for i in $FILES
  210.     do
  211.     case $i in
  212.     *.ln)    ;;
  213.     *)    T=`basename $i .c`.ln
  214.         if [ "$FULLPATH" ]
  215.         then
  216.             FNAME=$i
  217.         else
  218.             FNAME=`basename $i`
  219.         fi
  220.  
  221.         if [ $NDOTC -gt 1 ]
  222.         then
  223.             echo $FNAME:
  224.         fi
  225.         if [ "$VERBOSE" ]
  226.         then
  227.            echo "$LINT1 $CPPF $LINTF -T $HOUT,$NOUT,$T1,$T2,$T3,$T4 $i"
  228.                 fi
  229.         $LINT1 $CPPF $LINTF -T $HOUT,$NOUT,$T1,$T2,$T3,$T4 $i
  230. #
  231. # If return value is 0, save the output to the .ln file, and print
  232. # header file warnings.
  233.         RETVAL=$?
  234.         if [ $RETVAL = 0 ]
  235.         then
  236.             if [ ! "$PREONLY" ]
  237.             then
  238.             cat $T1 $T2 $T3 $T4 > $T
  239.             cat -s $HOUT
  240.             fi
  241.         else
  242.             cat -s $HOUT
  243.             echo "lint: errors in $FNAME; no output created" >&2
  244.             EXIT=$RETVAL
  245.         fi
  246.         rm -f $HOUT $NOUT;;
  247.     esac
  248.     done
  249. else
  250.     for i in $FILES
  251.     do
  252.     case $i in
  253.     *.ln)    cat <$i >> $TOUT;;
  254.     *)    if [ "$FULLPATH" ]
  255.         then
  256.             FNAME=$i
  257.         else
  258.             FNAME=`basename $i`
  259.         fi
  260.  
  261.         if [ $NDOTC -gt 1 ]
  262.         then
  263.             echo $FNAME:
  264.         fi
  265.         if [ "$VERBOSE" ]
  266.         then
  267.            echo "$LINT1 $CPPF $LINTF -T $HOUT,$NOUT,$T1,$T2,$T3,$T4 $i"
  268.                 fi
  269.         $LINT1 $CPPF $LINTF -T $HOUT,$NOUT,$T1,$T2,$T3,$T4 $i
  270.         RETVAL=$?
  271. #
  272. # If return value ok, save into temp file for later use by lint2.
  273. # Otherwise, cat the header file messages, and indicate there was
  274. # a problem.
  275. #
  276.  
  277.         if [ $RETVAL = 0 ]
  278.         then
  279.             cat $T1 $T2 $T3 $T4 >> $TOUT
  280.         else
  281.         #rm -f $HOUT $NOUT
  282.             echo "lint: errors in $FNAME; no output created" >&2
  283.             if [ "$ERRORS" = "" ]
  284.             then
  285.             ERRORS=$FNAME
  286.             else
  287.             ERRORS="$ERRORS,$FNAME"
  288.             fi
  289.             EXIT=$RETVAL
  290.         fi
  291.         ;;
  292.     esac
  293.     done
  294.  
  295.     if [ $EXIT != 0 ]
  296.     then
  297.     cat -s $HOUT
  298.     rm -f $TMPFILES
  299.     echo "lint: pass2 not run - errors in $ERRORS" >&2
  300.     exit $EXIT
  301.     fi
  302.  
  303. # A lint library is to be created.
  304.     if [ "$LLIB" ]
  305.     then
  306.     cp $TOUT $LLIB
  307.     fi
  308.  
  309.     if [ "$LNOUT" ]
  310.     then
  311.     cp $TOUT $LNOUT
  312.     rm -f $TMPFILES
  313.     exit 0
  314.     fi
  315.  
  316.     if [ "$CXOUT" ]
  317.     then
  318.     rm -f $TMPFILES
  319.     exit 0
  320.     fi
  321.  
  322. # Add in the default lint library (if -n was not used)
  323.     if [ "$DEFL" ]
  324.     then
  325.     cat <$DEFL >> $TOUT
  326.     fi
  327.  
  328. # Print the errors/warnings from the header file
  329.     cat -s $HOUT
  330.  
  331.     if [ "$VERBOSE" ]
  332.     then
  333.        echo "$LINT2 $LINTF $TOUT"
  334.     fi
  335.     $LINT2 $LINTF $TOUT
  336.     RETVAL=$?
  337.     if [ $RETVAL != 0 ]
  338.     then
  339.     rm -f $TMPFILES
  340.     exit $RETVAL
  341.     fi
  342. fi
  343.  
  344. rm -f $TMPFILES
  345. exit $EXIT
  346.