Hello, I noticed that there is a lot of files in the tree
with trailing whitespaces at the end of lines, mostly .c,
.h, man pages, documentation and regular text files.

I included a script below to deal with them, but some
examples first:

Amount of lines with trailing whitespaces in some dirs and files:
- src/Makefile.cross: 1
- src/Makefile: 1
- src/bin: 86
- src/distrib: 19
- src/etc: 64
- src/games: 375
- src/include: 99
- src/lib: 1867
- src/libexec: 35
- src/share: 203
- src/usr.bin: 2112

And the most notable:
- src/sys/dev/microcode/cyclades/cyzfirm.h: 13435

Here's a simple script to find/remove trailing whitespaces
at the end of lines. It can be used selectively to dirs and
files. It doesn't touch anything else.

$ script-name dir/file

  prints file names and corresponding lines

$ script-name -s dir/file

  strips files by removing all trailing whitespaces

############################################################

#!/bin/sh

set -e

progname=${0##*/}
strip=0

usage()
{
    printf "usage: %s [-s] file|directory\n" "$progname" 1>&2
    exit 1
}

report()
{
    rc=0
    files=$(grep -IHrn '[[:space:]]\{1,\}$' "$target") || rc=$?
    [ "$rc" -le 1 ] || exit "$rc"
    [ -n "$files" ] || return 0

    while IFS=: read -r file line text
    do
        printf "%s:%s: %s\n" "$file" "$line" "$text"
    done <<__LINES__
$files
__LINES__
}

strip_files()
{
    rc=0
    files=$(grep -Irl '[[:space:]]\{1,\}$' "$target") || rc=$?
    [ "$rc" -le 1 ] || exit "$rc"
    [ -n "$files" ] || return 0

    while IFS= read -r file
    do
        printf "%s\n" "$file" 1>&2
        ed -s "$file" <<'ED_EOF'
g/[[:space:]]\{1,\}$/s/[[:space:]]\{1,\}$//
w
q
ED_EOF
    done <<__FILES__
$files
__FILES__
}

while getopts :s opt
do
    case $opt in
        s) strip=1 ;;
        *) usage ;;
    esac
done

shift $((OPTIND - 1))
[ "$#" -eq 1 ] || usage
target=$1

if [ ! -e "$target" ]
then
    printf "%s: no such file or directory: %s\n" "$progname" "$target" 1>&2
    exit 1
fi

if [ "$strip" -eq 0 ]
then
    report
else
    strip_files
fi

############################################################

Some other small notes (outside of src/gnu and src/regress):

Files with no line terminator at EOF:
- src/sys/dev/pci/drm/amd/amdgpu/amdgpu_ras_eeprom.c
- src/sys/dev/pci/drm/amd/amdgpu/umc_v6_1.c
- src/sys/dev/pci/drm/amd/display/dc/clk_mgr/dcn201/dcn201_clk_mgr.h
- src/sys/dev/pci/drm/amd/display/dc/inc/hw/vpg.h
- src/sys/dev/pci/drm/amd/include/asic_reg/clk/clk_11_0_1_offset.h
- src/sys/dev/pci/drm/amd/include/asic_reg/clk/clk_11_0_1_sh_mask.h
- src/sys/dev/pci/drm/amd/include/asic_reg/dcn/dcn_3_5_0_sh_mask.h
- src/sys/dev/pci/drm/amd/pm/swsmu/smu14/smu_v14_0_0_ppt.h

Files with CRLF instead of LF:
- src/share/snmp/UUID-TC-MIB.txt
- sys/dev/microcode/bnx/bnxfw.h
- usr.bin/vi/docs/internals/cscope.NOTES

Maybe you will find it all useful to keep things clean and
consistent.

Regards,
Egor

Reply via email to