#!/bin/bash VERSIONINFO="codestat version 1.4, by scott arrington" # created 13 Nov 98 by scott arrington. # # 19 Nov 98 # the test syntax varies between the cygwin32 environment and # my linux system (both with bash as /bin/sh). i think the # difference is which test is being used (internal vs external). # also, under the cygwin stuff, really large greps cause sh to # have a "GPF", i suppose because the pipe fills up. i don't # know if that's fixable in here. # something that might be nice would be to make subroutines out of # the different little functional blocks here. # added some command line args and a little progress display. # checks to see if there are any files before trying grep, so that # grep won't freeze waiting for stdin. # # 31 Dec 98 # changed a tab to a space in the output, so that the columns # line up properly. # # 16 Feb 99 # forgot to bump the version number on 1.2... changed this to 1.3. # converted some parts to functions, making this a bash script. # we can now process more than one directory on the command line, # and can also suppress the default recursive file-searching. # the "working" dots also disappear if your echo supports the -e # option. # # 17 Feb 99 # minor bugfixes like variable initialization. # added a long report format (-l on the command line). # # 12 Oct 99 # changed highly inefficient code to use loops and such, so now # it's quite easy to add filename extensions. shorter and # cleaner code, as well. might this benefit from a perl rewrite? # bumped version number to 1.4 # # this is empty by default DEPTHSTR= # chooses the report style CODESTAT=codestat # calculate all of the code stats for the given directory. # the first arg should be the desired directory. # function codestat { DIR=$1 echo "--------------------------------------------------------" echo " directory root: $DIR" ncodelines=0 nlines=0 nfilest=0 for i in "*.c" "*.cc" "*.cpp" "*.h"; do thesefiles=$(find $DIR -name "$i" -print $DEPTHSTR) if ! [ -z "$thesefiles" ]; then echo -e "\tnumber of $i files:\t\t$(echo $thesefiles | wc -w)" let nfiles=$nfiles+$(echo $thesefiles | wc -w) let ncodelines=$ncodelines+$(echo $thesefiles | xargs grep ";" | wc -l); let nlines=$nlines+$(cat $thesefiles | wc -l); fi done echo -e "\ttotal number of files:\t\t$nfiles" echo -e "\t total number of lines:\t$nlines" echo -e "\t estimated lines of code:\t$ncodelines" echo "--------------------------------------------------------" } function codestat_4 { # initialize the local variables NFILES=0 NLINES=0 NCODELINES=0 DIR=$1; echo "-----------------------------------------------------------------------------" echo " directory root: $DIR" echo "-----------------------------------------------------------------------------" echo " type # files # lines est # statements" echo "-----------------------------------------------------------------------------" for EXT in "*.c" "*.cc" "*.cpp" "*.h"; do FILES=`find $DIR -name "$EXT" -print $DEPTHSTR` if ! [ -z "$FILES" -a -z "$CSRCS" -a -z "$HEADERS" ]; then # count the files COUNT=`echo $FILES | wc -w | awk '{print $1}'` let NFILES=$NFILES+$COUNT # compute the line stats if [ $COUNT != 0 ]; then let CODELINECOUNT=`cat $FILES | grep ";" | wc -l | awk '{print $1}'` let LINECOUNT=`cat $FILES | wc -l | awk '{print $1}'` fi echo " $EXT $COUNT $LINECOUNT $CODELINECOUNT" let NLINES=$NLINES+$LINECOUNT let NCODELINES=$NCODELINES+$CODELINECOUNT fi done echo " totals $NFILES $NLINES $NCODELINES" echo "-----------------------------------------------------------------------------" } # # display a helpful usage message for those desiring guidance. # function usage { cat <