Skip to content

Instantly share code, notes, and snippets.

@yesmar
Created October 29, 2020 15:03
Show Gist options
  • Save yesmar/9fc08d9fff9fc6af53ab4efaff3b13b0 to your computer and use it in GitHub Desktop.
Save yesmar/9fc08d9fff9fc6af53ab4efaff3b13b0 to your computer and use it in GitHub Desktop.
Script to inject the license message required for free use of PVS-Studio
#!/bin/bash
# pvs-inject-free.bash 20180403 yesmar@gmail.com
# Use this script to inject the license message required for free use of
# PVS-Studio. https://github.com/viva64/how-to-use-pvs-studio-free is useful
# for adding these comments to your source files, but that program instruments
# static (i.e. pre-existing) source files. It doesn't work so well with
# dynamically generated files.
# The pvs-inject-free.bash script prepends the specified license
# (academic/FOSS/individual) comment to the top of the source file. The script
# can be run as part of the build or within a separate command sequence
# (e.g., to annotate a dynamically generated source file).
# shellcheck disable=SC2034 # for LONG_OPTARG
script=$(basename "$0")
pvs_info='PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com'
no_backup=0
function usage {
printf "Usage: %s [option] [license] <file>\\n" "$script" >&2
printf " -h,--help - display this help text\\n" >&2
printf " -l,--licenses - display list of supported licenses\\n" >&2
printf " -n,--no-backup - do not create backup file\\n" >&2
exit 1
}
function licenses {
printf "%s: available licenses:\\n\\n" "$script" >&2
printf " academic - for students\\n" >&2
printf " foss - for free open source projects\\n" >&2
printf " individual - for individual developers\\n" >&2
printf "\\nSee https://www.viva64.com/en/b/0457/ for details.\\n" >&2
exit 1
}
while getopts hln-: arg; do
case $arg in
h) usage ;;
l) licenses ;;
n) no_backup=1 ;;
-) LONG_OPTARG="${OPTARG#*=}"
case $OPTARG in
help) usage ;;
licenses) licenses ;;
no-backup) no_backup=1 ;;
*) echo "$0: illegal option -- $OPTARG" >&2; exit 2 ;;
esac ;;
\?) exit 2 ;; # getopts already reported the illegal option.
esac
done
shift $((OPTIND-1)) # Remove parsed options and args from $@ list.
case $# in
1) license='foss'
source=$1 ;;
2) license=$1
source=$2 ;;
*) usage ;;
esac
case "$license" in
'academic') license_type='This is a personal academic project.' ;;
'foss') license_type='This is an open source non-commercial project.' ;;
'individual') license_type='This is an independent project of an individual developer.' ;;
*) printf "%s: \"%s\" is not a recognized license\\n" "$script" "$license" >&2
printf "%s: run \"./%s --licenses\" for a list of supported licenses\\n" "$script" "$script" >&2
exit 1
;;
esac
cp "$source" "$source~"
printf "// %s Dear PVS-Studio, please check it.\\n" "$license_type" > "$source"
printf "// %s\\n" "$pvs_info" >> "$source"
cat "$source~" >> "$source"
if ((no_backup == 1)); then
rm "$source~"
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment