Here's a rule I'm adding to coreutils' cfg.mk. However, I would like to use it several other projects, so rather than duplicating it in each, I'm thinking of putting it in gnulib's maint.mk.
Opinions? I know that at least Bruno likes to retain trailing empty lines in modules/ files. But for a class of files like that, it's easy to exempt "^modules/" or to omit the rule altogether. Does anyone object to my adding this syntax check to maint.mk? Aside from that, can anyone improve on the code? The multi-line Perl script is the only way I could get the efficiency I wanted, but there may well be another more concise way. >From bc6da1230dfc7c998deefab9cb1890907b6da595 Mon Sep 17 00:00:00 2001 From: Jim Meyering <[email protected]> Date: Sat, 10 Apr 2010 14:19:11 +0200 Subject: [PATCH] maint: new syntax-check rule: prohibit empty lines at EOF * cfg.mk (detect_empty_lines_at_EOF_): Define. (sc_prohibit_empty_lines_at_EOF): New rule. * .x-sc_prohibit_empty_lines_at_EOF: New file. Exempt pr test inputs. * Makefile.am (syntax_check_exceptions): Add it. --- .x-sc_prohibit_empty_lines_at_EOF | 1 + Makefile.am | 1 + cfg.mk | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 0 deletions(-) create mode 100644 .x-sc_prohibit_empty_lines_at_EOF diff --git a/.x-sc_prohibit_empty_lines_at_EOF b/.x-sc_prohibit_empty_lines_at_EOF new file mode 100644 index 0000000..92571d6 --- /dev/null +++ b/.x-sc_prohibit_empty_lines_at_EOF @@ -0,0 +1 @@ +^tests/pr/ diff --git a/Makefile.am b/Makefile.am index 1eb9c12..9a7f45c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -47,6 +47,7 @@ syntax_check_exceptions = \ .x-sc_po_check \ .x-sc_program_name \ .x-sc_prohibit_atoi_atof \ + .x-sc_prohibit_empty_lines_at_EOF \ .x-sc_prohibit_fail_0 \ .x-sc_prohibit_magic_number_exit \ .x-sc_prohibit_stat_st_blocks \ diff --git a/cfg.mk b/cfg.mk index 75ce77a..75f4be0 100644 --- a/cfg.mk +++ b/cfg.mk @@ -315,6 +315,40 @@ sc_space_before_open_paren: else :; \ fi +# This Perl code is slightly obfuscated. Not only is each "$" doubled +# because it's in a Makefile, but the $$c's are comments; we cannot +# use "#" due to the way the script ends up concatentated onto one line. +# It would be much more concise, and with better output (including counts) as +# perl -ln -0777 -e '/\n(\n+)$/ and print "$ARGV: ".length $1' ... +# but that would be far less efficient, reading the entire contents +# of each file, rather than just the last two bytes of each. +# +# This is a perl script that is expected to be the single-quoted argument +# to a command-line "-le". The remaining arguments are file names. +# Print the name of each file that ends in two or more newline bytes. +# Exit nonzero if at least one such file is found, otherwise, exit 0. +# Warn about, but otherwise ignore open failure. Ignore seek/read failure. +# +# Use this if you want to remove trailing empty lines from selected files: +# perl -pi -0777 -e 's/\n\n+$/\n/' ... +# +detect_empty_lines_at_EOF_ = \ + foreach my $$f (@ARGV) { \ + open F, "<", $$f or (warn "failed to open $$f: $$!\n"), next; \ + my $$p = sysseek (F, -2, 2); \ + my $$c = "seek failure probably means file has < 2 bytes; ignore"; \ + my $$two; \ + defined $$p and $$p = sysread F, $$two, 2; \ + close F; \ + $$c = "ignore read failure"; \ + $$p && $$two eq "\n\n" and (print $$f), $$fail=1; \ + } END { exit defined $$fail } + +sc_prohibit_empty_lines_at_EOF: + @perl -le '$(detect_empty_lines_at_EOF_)' $$($(VC_LIST_EXCEPT)) \ + || { echo '$(ME): the above files have empty lines at EOF' \ + 1>&2; exit 1; } || :; \ + include $(srcdir)/dist-check.mk update-copyright-env = \ -- 1.7.1.rc0.264.g94f6e
