#! /bin/sh # # Copyright by The HDF Group. # All rights reserved. # # This file is part of h5check. The full h5check copyright notice, # including terms governing use, modification, and redistribution, is # contained in the file COPYING, which can be found at the root of the # source code distribution tree. If you do not have access to this file, # you may request a copy from help@hdfgroup.org. # Check Copyright notice. # Check that all the files have the proper copyright notice. # It goes down directories recursively. # # Programmer: Albert Cheng # Created Data: 2011/08/16 (Adapted from HDF5 version) # Modification: # # Setup # PROGNAME=$0 DIFF="diff" INITFILE=.chkright.ini EXCEPTIONS=/tmp/chkright.except.$$ tmpfile=/tmp/chkright_tmp.$$ EXCEPTIONDIRS="-name CVS -o -name .svn" # at least skip CVS directories. EXTRACTEDFILE=/tmp/chkright.extracted.$$ VERBOSE= # default no FIXIT= # default no DIRS=. # default current directory NFAILEDFILES=0 # Number of failed files found. NPASSEDFILES=0 # Number of passed files found. NFIXEDFILES=0 # Number of files fixed. NFIXFAILEDFILES=0 # Number of files fix failed. NUMBEGINLINES=60 # Copyright notice should be located within the # this number of lines at the beginning of the file. UICOPYRIGHTSTR="Copyright by the Board of Trustees of the University of Illinois" THGCOPYRIGHTSTR="Copyright by The HDF Group." PASSEDLOG=/tmp/chkright_passed.$$ SKIPPEDLOG=/tmp/chkright_skipped.$$ FAILEDLOG=/tmp/chkright_failed.$$ FIXEDLOG=/tmp/chkright_fixed.$$ FIXFAILEDLOG=/tmp/chkright_fixfailed.$$ C_COPYRIGHT=/tmp/chkright_C.$$ # C style copyright FTN_COPYRIGHT=/tmp/chkright_FTN.$$ # Fortran style copyright HTM_COPYRIGHT=/tmp/chkright_HTM.$$ # HTML style copyright SH_COPYRIGHT=/tmp/chkright_SH.$$ # SHELL style copyright SH_COPYRIGHT2=/tmp/chkright_SH2.$$ # SHELL style copyright, 2nd style. WINBAT_COPYRIGHT=/tmp/chkright_WINBAT.$$ # Windows Batch file Copyright notice VMSCMD_COPYRIGHT=/tmp/chkright_VMSCMD.$$ # VMS command file Copyright notice CONFIGURE_IN_COPYRIGHT=/tmp/chkright_CONFIGURE_IN.$$ # configure.in file Copyright notice tmpfixfile=/tmp/chkright_fix.$$ # Temporary fixed copy of file # Caution message of the fix feature. FIXCAUTIONMSG() { cat < ${C_COPYRIGHT} << \EOF * Copyright by The HDF Group. * * All rights reserved. * * * * This file is part of h5check. The full h5check copyright notice, * * including terms governing use, modification, and redistribution, is * * contained in the file COPYING, which can be found at the root of the * * source code distribution tree. If you do not have access to this file, * * you may request a copy from help@hdfgroup.org. * EOF # Fortran9X source Copyright notice cat > ${FTN_COPYRIGHT} << \EOF ! Copyright by The HDF Group. * ! All rights reserved. * ! * ! This file is part of h5check. The full h5check copyright notice, * ! including terms governing use, modification, and redistribution, is * ! contained in the file COPYING, which can be found at the root of the * ! source code distribution tree. If you do not have access to this file, * ! you may request a copy from help@hdfgroup.org. * EOF # HTML file Copyright notice cat > ${HTM_COPYRIGHT} << \EOF * Copyright by The HDF Group. * * All rights reserved. * * * * This file is part of h5check. The full h5check copyright notice, * * including terms governing use, modification, and redistribution, is * * contained in the file COPYING, which can be found at the root of the * * source code distribution tree. If you do not have access to this file, * * you may request a copy from help@hdfgroup.org. * EOF # Shell style Copyright notice cat > ${SH_COPYRIGHT} << \EOF # Copyright by The HDF Group. # All rights reserved. # # This file is part of h5check. The full h5check copyright notice, # including terms governing use, modification, and redistribution, is # contained in the file COPYING, which can be found at the root of the # source code distribution tree. If you do not have access to this file, # you may request a copy from help@hdfgroup.org. EOF # Shell style Copyright notice (2nd type) # Lines with ## are used in Makefile.am so that they are not # copied to Makefile.in by automake. cat > ${SH_COPYRIGHT2} << \EOF ## Copyright by The HDF Group. ## All rights reserved. ## ## This file is part of h5check. The full h5check copyright notice, ## including terms governing use, modification, and redistribution, is ## contained in the file COPYING, which can be found at the root of the ## source code distribution tree. If you do not have access to this file, ## you may request a copy from help@hdfgroup.org. EOF # Windows Batch file Copyright notice cat > ${WINBAT_COPYRIGHT} << \EOF @REM Copyright by The HDF Group. @REM All rights reserved. @REM @REM This file is part of h5check. The full h5check copyright notice, @REM including terms governing use, modification, and redistribution, is @REM contained in the file COPYING, which can be found at the root of the @REM source code distribution tree. If you do not have access to this file, @REM you may request a copy from help@hdfgroup.org. EOF # configure.in file Copyright notice cat > ${CONFIGURE_IN_COPYRIGHT} << \EOF dnl Copyright by The HDF Group. dnl All rights reserved. dnl dnl This file is part of h5check. The full h5check copyright notice, dnl including terms governing use, modification, and redistribution, is dnl contained in the file COPYING, which can be found at the root of the dnl source code distribution tree. If you do not have access to this file, dnl you may request a copy from help@hdfgroup.org. EOF # VMS command file Copyright notice cat > ${VMSCMD_COPYRIGHT} << \EOF $!# Copyright by The HDF Group. $!# All rights reserved. $!# $!# This file is part of h5check. The full h5check copyright notice, $!# including terms governing use, modification, and redistribution, is $!# contained in the file COPYING, which can be found at the root of the $!# source code distribution tree. If you do not have access to this file, $!# you may request a copy from help@hdfgroup.org. EOF } # Initialization # # Initialize file format: # Each line is a keyword for action and the rest are values. # Keywords: # '#' Comments # skip Files to be skipped # prune Directories to be skipped. Notice this prunes all directories # with the same name. E.g., # "prune test" skips test, fortran/test, c++/test, ... # (See -name option in the find command.) # prunepath Directory or file to be skipped. Notice this is different from # prunes since it matches the exact pathname. E.g., # "prunepath ./tools/testfiles" skips the directory/file matching # exactly that path but NOT tools/h5dump/testfiles nor # tools/h5dump/testfiles. # (See -path option in the find command.) # INITIALIZATION() { # clean up log files rm -f $PASSEDLOG $SKIPPEDLOG $FAILEDLOG $FIXEDLOG $FIXFAILEDLOG # Generate various styles of copyright notice. BUILDCOPYRIGHT echo Initialization... # setup exceptions. cp /dev/null $EXCEPTIONS # Process Initial setting file if exists if [ -r $INITFILE ]; then while read key value; do case "$key" in \#* | '') # Comment or blank line, skip it continue ;; skip) echo $key $value echo $value >> $EXCEPTIONS ;; prune) echo $key $value EXCEPTIONDIRS="$EXCEPTIONDIRS -o -name $value" ;; prunepath) echo $key $value EXCEPTIONDIRS="$EXCEPTIONDIRS -o -path $value" ;; *) echo unknown setting input in file $INITFILE echo $key $value ;; esac done < $INITFILE fi # Change EXCEPTIONDIRS to be compatible with find command. EXCEPTIONDIRS="( $EXCEPTIONDIRS ) -prune -o" echo Initialization done } # Parse Options # PARSE_OPTION() { while test $# -gt 0 ; do case "$1" in -h | -help ) USAGE exit 0 ;; -fname ) shift FNAME="$1" ;; -fix ) FIXIT=yes ;; -v* ) VERBOSE=yes if test X$1 = X-v9; then set -x fi ;; -* ) echo "***Unknown option ($1)" USAGE exit 1 ;; * ) DIRS=$* break ;; esac shift done } # Rinse the file by, # removing all \r which is often present in Windows files; # replace tabs with equivalent spaces; # removing all trailing spaces. # $1 is the file to be rinsed. RINSE() { rf=$1 cp $rf $tmpfile dos2unix < $tmpfile | expand | sed -e 's/ *$//' > $rf } # Locate a line in the file and print the line number. # Print 0 if not found; -1 if error. # $1 The line. # $2 The file. # FindLineInFile() { if [ $# -ne 2 ]; then # expect two arguments echo -1 return fi xline=$1 xf=$2 xpos=`grep -n "${xline}" $xf` if [ "$?" -ne 0 ] ; then # Not found, return 0 xpos=0 else xpos=`echo $xpos | cut -f1 -d:` fi echo $xpos } # Match Copyright notice. # $1 file which contains the expected copyright notice. # $2 file in which to look for the copyright notice. # Copyright notice must be found within the beginning $NUMBEGINLINES of lines. # Hunt for the particular string $UICOPYRIGHTSTR which signifies the beginning # of the copyright notice. # MATCH_COPYRIGHT() { if [ $# -ne 2 ]; then # expect two arguments echo FAILED return fi COPYRIGHTFILE=$1 f=$2 nlines=`wc -l ${COPYRIGHTFILE} | cut -f1 -d' '` # Find a line that contains the copyright string and its line number in # the file. begin=`FindLineInFile "${THGCOPYRIGHTSTR}" $f` if [ "$begin" -le 0 ] ; then # Not found, generate an empty dummy file cp /dev/null ${EXTRACTEDFILE} false else end=`expr $begin + $nlines - 1` sed -n -e "${begin},${end}p" < $f > ${EXTRACTEDFILE} RINSE ${EXTRACTEDFILE} $DIFF ${EXTRACTEDFILE} ${COPYRIGHTFILE} >/dev/null 2>&1 fi if test $? -eq 0; then echo PASSED else echo FAILED fi } # Fix Copyright notice. # $1 file which contains the expected copyright notice. # $2 file in which to look for the copyright notice. # Copyright notice must be found within the beginning $NUMBEGINLINES of lines. # Hunt for the particular string $UICOPYRIGHTSTR which signifies the beginning # of the copyright notice. # FIX_COPYRIGHT() { if [ $# -ne 2 ]; then # expect two arguments echo FAILED return fi COPYRIGHTFILE=$1 f=$2 nlines=`wc -l ${COPYRIGHTFILE} | cut -f1 -d' '` # If the file has UICOPYRIGHTSTR but not THGCOPYRIGHTSTR, then replace the lines # starting at UICOPYRIGHTSTR and down. # If the file has THGCOPYRIGHTSTR, then replace the lines starting at the # THGCOPYRIGHTSTR and down. # If neither found, abort it. # Find a line that contains the THG copyright string and its line number in # the file. insertbegin=`FindLineInFile "${THGCOPYRIGHTSTR}" $f` if [ $insertbegin -gt 0 ]; then insertend=`expr $insertbegin + $nlines` # no need to -1. See below. else insertbegin=`FindLineInFile "${UICOPYRIGHTSTR}" $f` if [ $insertbegin -gt 0 ]; then insertend=`expr $insertbegin + $nlines - 1` # no need to -2. See below. else FIXFAILED return fi fi # Copy line 1 up to insertbegin from original file xbegin=`expr $insertbegin - 1` if [ $xbegin -gt 0 ]; then sed -n -e "1,${xbegin}p" $f > $tmpfixfile else cp /dev/null $tmpfixfile # make it empty. fi # now the correct copyright file cat $COPYRIGHTFILE >> $tmpfixfile # the rest of the original file sed -n -e "${insertend},"'$p' $f >> $tmpfixfile # copy them all back to the original file cp $tmpfixfile $f FIXED rm -f $tmpfixfile } # Check C and C++ source files # C_SOURCE() { f=$1 case `MATCH_COPYRIGHT $C_COPYRIGHT $f` in PASSED) PASSED $f return ;; FAILED) # show the difference FAILED $f $DIFF ${EXTRACTEDFILE} ${C_COPYRIGHT} if [ -n "$FIXIT" ]; then FIX_COPYRIGHT $C_COPYRIGHT $f fi ;; esac } # Check Fortran90 source files # FORTRAN_SOURCE() { f=$1 case `MATCH_COPYRIGHT $FTN_COPYRIGHT $f` in PASSED) PASSED $f return ;; FAILED) # show the difference FAILED $f $DIFF ${EXTRACTEDFILE} ${FTN_COPYRIGHT} if [ -n "$FIXIT" ]; then FIX_COPYRIGHT $FTN_COPYRIGHT $f fi ;; esac } # Check HTML Files # HTML_FILE() { f=$1 case `MATCH_COPYRIGHT $HTM_COPYRIGHT $f` in PASSED) PASSED $f return ;; FAILED) # show the difference FAILED $f $DIFF ${EXTRACTEDFILE} ${HTM_COPYRIGHT} if [ -n "$FIXIT" ]; then FIX_COPYRIGHT $HTM_COPYRIGHT $f fi ;; esac } # Check Shell script files which use the style of copyright notice of # leading #'s. First try single #, then double ##. # SHELL_FILE() { f=$1 case `MATCH_COPYRIGHT $SH_COPYRIGHT $f` in PASSED) PASSED $f return ;; FAILED) case `MATCH_COPYRIGHT $SH_COPYRIGHT2 $f` in PASSED) PASSED $f return ;; FAILED) # let it fall through to print FAILED result. ;; esac # show the difference FAILED $f $DIFF ${EXTRACTEDFILE} ${SH_COPYRIGHT} if [ -n "$FIXIT" ]; then FIX_COPYRIGHT $SH_COPYRIGHT $f fi ;; esac } # Check files that use #'s as comments such as Makefile. # The Copyright body of text happen to be the same as used by Shell script # files. Makefile.am may use ## for comments. # MAKE_FILE() { f=$1 case `MATCH_COPYRIGHT $SH_COPYRIGHT $f` in PASSED) PASSED $f return ;; FAILED) case `MATCH_COPYRIGHT $SH_COPYRIGHT2 $f` in PASSED) PASSED $f return ;; FAILED) # let it fall through to print FAILED result. ;; esac # show the difference FAILED $f $DIFF ${EXTRACTEDFILE} ${SH_COPYRIGHT} if [ -n "$FIXIT" ]; then FIX_COPYRIGHT $SH_COPYRIGHT $f fi ;; esac } # Check Windows Batch files # BATCH_FILE() { f=$1 case `MATCH_COPYRIGHT $WINBAT_COPYRIGHT $f` in PASSED) PASSED $f return ;; FAILED) # show the difference FAILED $f $DIFF ${EXTRACTEDFILE} ${WINBAT_COPYRIGHT} if [ -n "$FIXIT" ]; then FIX_COPYRIGHT $WINBAT_COPYRIGHT $f fi ;; esac } # Check Windows Batch files # VMSCMD_FILE() { f=$1 case `MATCH_COPYRIGHT $VMSCMD_COPYRIGHT $f` in PASSED) PASSED $f return ;; FAILED) # show the difference FAILED $f $DIFF ${EXTRACTEDFILE} ${VMSCMD_COPYRIGHT} if [ -n "$FIXIT" ]; then FIX_COPYRIGHT $VMSCMD_COPYRIGHT $f fi ;; esac } # Check Configure.in type files # CONFIGURE_IN_FILE() { f=$1 case `MATCH_COPYRIGHT $CONFIGURE_IN_COPYRIGHT $f` in PASSED) PASSED $f return ;; FAILED) # show the difference FAILED $f $DIFF ${EXTRACTEDFILE} ${CONFIGURE_IN_COPYRIGHT} if [ -n "$FIXIT" ]; then FIX_COPYRIGHT $CONFIGURE_IN_COPYRIGHT $f fi ;; esac } # Guess the type of file. # Inspect the first 5 lines to guess what type of file it is. # GUESS_File_Type() { if [ $# -ne 1 ]; then echo "wrong number of arguments($#)" return fi f=$1 # Now guess the file type. head -5 < $f > $tmpfile if head -1 < $tmpfile | grep '^#!' > /dev/null; then # First line is "#!". It is likely a shell script or similar type. echo SHELL_FILE elif grep '\/\*' < $tmpfile > /dev/null; then # Found some lines containing '/*'. It may be a C/C++ style file. echo C_SOURCE elif grep '^!' < $tmpfile > /dev/null; then # Some lines start with a "!". It may be a Fortran 9X style file. echo FORTRAN_SOURCE elif grep '^#' < $tmpfile > /dev/null; then # Some lines start with a "#". It may be a shell like type. # Put this after C_SOURCE which may have #define and such lines. echo SHELL_FILE elif grep '^dnl' < $tmpfile > /dev/null; then # Some lines start with a "dnl". It may be a configure.in type file. echo CONFIGURE_IN_FILE elif grep -i '^' < $tmpfile > /dev/null || \ grep '^