2019-11-07 17:49:56 +08:00
|
|
|
#!/bin/sh
|
|
|
|
|
2021-01-01 16:03:39 +08:00
|
|
|
# Copyright (C) 2019-2021 Free Software Foundation, Inc.
|
2019-11-07 17:49:56 +08:00
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2019-11-26 06:00:03 +08:00
|
|
|
# This script intends to facilitate spell checking of source/doc files.
|
2019-11-07 17:49:56 +08:00
|
|
|
# It:
|
2019-11-26 06:00:03 +08:00
|
|
|
# - transforms the files into a list of lowercase words
|
2019-11-07 17:49:56 +08:00
|
|
|
# - prefixes each word with the frequency
|
|
|
|
# - filters out words within a frequency range
|
|
|
|
# - sorts the words, longest first
|
|
|
|
#
|
2019-11-26 06:00:03 +08:00
|
|
|
# If '-c' is passed as option, it operates on the C comments only, rather than
|
|
|
|
# on the entire file.
|
|
|
|
#
|
2019-11-07 17:49:56 +08:00
|
|
|
# For:
|
|
|
|
# ...
|
2019-11-26 06:00:03 +08:00
|
|
|
# $ files=$(find gdb -type f -name "*.c" -o -name "*.h")
|
|
|
|
# $ ./gdb/contrib/words.sh -c $files
|
2019-11-07 17:49:56 +08:00
|
|
|
# ...
|
|
|
|
# it generates a list of ~15000 words prefixed with frequency.
|
|
|
|
#
|
|
|
|
# This could be used to generate a dictionary that is kept as part of the
|
|
|
|
# sources, against which new code can be checked, generating a warning or
|
|
|
|
# error. The hope is that misspellings would trigger this frequently, and rare
|
|
|
|
# words rarely, otherwise the burden of updating the dictionary would be too
|
|
|
|
# much.
|
|
|
|
#
|
|
|
|
# And for:
|
|
|
|
# ...
|
2019-11-26 06:00:03 +08:00
|
|
|
# $ files=$(find gdb -type f -name "*.c" -o -name "*.h")
|
|
|
|
# $ ./gdb/contrib/words.sh -c -f 1 $files
|
2019-11-07 17:49:56 +08:00
|
|
|
# ...
|
|
|
|
# it generates a list of ~5000 words with frequency 1.
|
|
|
|
#
|
|
|
|
# This can be used to scan for misspellings manually.
|
|
|
|
#
|
|
|
|
|
|
|
|
minfreq=
|
|
|
|
maxfreq=
|
2019-11-26 06:00:03 +08:00
|
|
|
c=false
|
2019-11-07 17:49:56 +08:00
|
|
|
while [ $# -gt 0 ]; do
|
|
|
|
case "$1" in
|
2019-11-26 06:00:03 +08:00
|
|
|
-c)
|
|
|
|
c=true
|
|
|
|
shift
|
|
|
|
;;
|
2019-11-07 17:49:56 +08:00
|
|
|
--freq|-f)
|
|
|
|
minfreq=$2
|
|
|
|
maxfreq=$2
|
|
|
|
shift 2
|
|
|
|
;;
|
|
|
|
--min)
|
|
|
|
minfreq=$2
|
|
|
|
if [ "$maxfreq" = "" ]; then
|
|
|
|
maxfreq=0
|
|
|
|
fi
|
|
|
|
shift 2
|
|
|
|
;;
|
|
|
|
--max)
|
|
|
|
maxfreq=$2
|
|
|
|
if [ "$minfreq" = "" ]; then
|
|
|
|
minfreq=0
|
|
|
|
fi
|
|
|
|
shift 2
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
break;
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ "$minfreq" = "" ] && [ "$maxfreq" = "" ]; then
|
|
|
|
minfreq=0
|
|
|
|
maxfreq=0
|
|
|
|
fi
|
|
|
|
|
|
|
|
awkfile=$(mktemp)
|
|
|
|
trap 'rm -f "$awkfile"' EXIT
|
|
|
|
|
|
|
|
cat > "$awkfile" <<EOF
|
|
|
|
BEGIN {
|
|
|
|
in_comment=0
|
|
|
|
}
|
|
|
|
|
|
|
|
// {
|
|
|
|
line=\$0
|
|
|
|
}
|
|
|
|
|
|
|
|
/\/\*/ {
|
|
|
|
in_comment=1
|
|
|
|
sub(/.*\/\*/, "", line)
|
|
|
|
}
|
|
|
|
|
|
|
|
/\*\// {
|
|
|
|
sub(/\*\/.*/, "", line)
|
|
|
|
in_comment=0
|
|
|
|
print line
|
|
|
|
next
|
|
|
|
}
|
|
|
|
|
|
|
|
// {
|
|
|
|
if (in_comment) {
|
|
|
|
print line
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
|
|
|
|
# Stabilize sort.
|
|
|
|
export LC_ALL=C
|
|
|
|
|
2019-11-26 06:00:03 +08:00
|
|
|
if $c; then
|
|
|
|
awk \
|
|
|
|
-f "$awkfile" \
|
|
|
|
-- "$@"
|
|
|
|
else
|
|
|
|
cat "$@"
|
|
|
|
fi \
|
2019-11-22 23:23:22 +08:00
|
|
|
| sed \
|
2019-11-22 23:23:22 +08:00
|
|
|
-e 's/[!"?;:%^$~#{}`&=@,. \t\/_()|<>\+\*-]/\n/g' \
|
2019-11-22 23:23:22 +08:00
|
|
|
-e 's/\[/\n/g' \
|
|
|
|
-e 's/\]/\n/g' \
|
2019-11-22 23:23:22 +08:00
|
|
|
-e "s/'/\n/g" \
|
2019-11-22 23:23:22 +08:00
|
|
|
-e 's/[0-9][0-9]*/\n/g' \
|
|
|
|
-e 's/[ \t]*//g' \
|
2019-11-07 17:49:56 +08:00
|
|
|
| tr '[:upper:]' '[:lower:]' \
|
|
|
|
| sort \
|
|
|
|
| uniq -c \
|
|
|
|
| awk "{ if (($minfreq == 0 || $minfreq <= \$1) \
|
|
|
|
&& ($maxfreq == 0 || \$1 <= $maxfreq)) { print \$0; } }" \
|
|
|
|
| awk '{ print length($0) " " $0; }' \
|
|
|
|
| sort -n -r \
|
|
|
|
| cut -d ' ' -f 2-
|