Re: [RFC PATCH] tools: add perltidy to the syntax checker/fixer

2017-05-30 Thread Paul Moore
On Tue, May 30, 2017 at 9:52 AM, Stephen Smalley  wrote:
> On Fri, 2017-05-26 at 11:58 -0400, Paul Moore wrote:
>> From: Paul Moore 
>>
>> Signed-off-by: Paul Moore 
>> ---
>>  tools/check-syntax |   86 
>> 
>>  1 file changed, 66 insertions(+), 20 deletions(-)

...

>>  #
>> -# Fix the formatting on a C source/header file
>> +# Fix the formatting on a file
>>  #
>>  # Arguments:
>> -# 1File to fix
>> +# 1Language
>> +# 2File to check
>>  #
>> -function tool_c_style_fix() {
>> - [[ -z "$1" || ! -r "$1" ]] && return
>> +function style_fix() {
>> + [[ -z "$1" ]] && return
>> + [[ -z "$2" || ! -w "$2" ]] && return
>>
>> - tmp="$(mktemp --tmpdir=$(dirname "$1"))"
>> - tool_c_style "$1" > "$tmp"
>> - mv "$tmp" "$1"
>> + tmp="$(mktemp --tmpdir=$(dirname "$2"))"
>> + case "$1" in
>> + c|C)
>> + tool_c_style "$2" > "$tmp"
>> + ;;
>> + perl|Perl)
>> + tool_perl_style "$2" > "$tmp"
>> + ;;
>> + esac
>> + mv "$tmp" "$2"
>
> This approach doesn't preserve mode or other attributes on the file,
> and therefore leaves the perl scripts non-executable after running
> ./tools/check_syntax -f.

Yes, good point.  I'll replace that final mv command with the following:

  cat "$tmp" > "$2"
  rm "$tmp"

-- 
paul moore
www.paul-moore.com


Re: [RFC PATCH] tools: add perltidy to the syntax checker/fixer

2017-05-30 Thread Stephen Smalley
On Fri, 2017-05-26 at 11:58 -0400, Paul Moore wrote:
> From: Paul Moore 
> 
> Signed-off-by: Paul Moore 
> ---
>  tools/check-syntax |   86 
> 
>  1 file changed, 66 insertions(+), 20 deletions(-)
> 
> diff --git a/tools/check-syntax b/tools/check-syntax
> index 72cb06b..ee83c03 100755
> --- a/tools/check-syntax
> +++ b/tools/check-syntax
> @@ -13,6 +13,9 @@
>  CHK_C_LIST="$(find tests/ -name "*.c") $(find tests/ -name "*.h")"
>  CHK_C_EXCLUDE=""
>  
> +CHK_PERL_LIST="$(find tests/ -name "*.pl") $(find tests/ -name
> "test")"
> +CHK_PERL_EXCLUDE=""
> +
>  
>  # functions
>  
> @@ -66,50 +69,92 @@ function tool_c_style() {
>  }
>  
>  #
> -# Check the formatting on a C source/header file
> +# Generate a properly formatted Perl source file
>  #
>  # Arguments:
> -# 1File to check
> +# 1Source file
>  #
> -function tool_c_style_check() {
> - [[ -z "$1" || ! -r "$1" ]] && return
> +function tool_perl_style() {
> + perltidy < "$1"
> +}
>  
> - tool_c_style "$1" | diff -pu --label="$1.orig" "$1" --
> label="$1" -
> +#
> +# Check the formatting on a file
> +#
> +# Arguments:
> +# 1Language
> +# 2File to check
> +#
> +function style_check() {
> + [[ -z "$1" ]] && return
> + [[ -z "$2" || ! -r "$2" ]] && return
> +
> + case "$1" in
> + c|C)
> + tool_c_style "$2" | \
> + diff -pu --label="$2.orig" "$2" --label="$2" 
> -
> + ;;
> + perl|Perl)
> + tool_perl_style "$2" | \
> + diff -pu --label="$2.orig" "$2" --label="$2" 
> -
> + ;;
> + esac
>  }
>  
>  #
> -# Fix the formatting on a C source/header file
> +# Fix the formatting on a file
>  #
>  # Arguments:
> -# 1File to fix
> +# 1Language
> +# 2File to check
>  #
> -function tool_c_style_fix() {
> - [[ -z "$1" || ! -r "$1" ]] && return
> +function style_fix() {
> + [[ -z "$1" ]] && return
> + [[ -z "$2" || ! -w "$2" ]] && return
>  
> - tmp="$(mktemp --tmpdir=$(dirname "$1"))"
> - tool_c_style "$1" > "$tmp"
> - mv "$tmp" "$1"
> + tmp="$(mktemp --tmpdir=$(dirname "$2"))"
> + case "$1" in
> + c|C)
> + tool_c_style "$2" > "$tmp"
> + ;;
> + perl|Perl)
> + tool_perl_style "$2" > "$tmp"
> + ;;
> + esac
> + mv "$tmp" "$2"

This approach doesn't preserve mode or other attributes on the file,
and therefore leaves the perl scripts non-executable after running
./tools/check_syntax -f.

>  }
>  
>  #
> -# Perform all known syntax checks for the configured C
> sources/headers
> +# Perform all known syntax checks for the configured files
>  #
> -function check_c() {
> +function check() {
>   for i in $CHK_C_LIST; do
>   echo "$CHK_C_EXCLUDE" | grep -q "$i" && continue
>   echo "Differences for $i"
> - tool_c_style_check "$i"
> + style_check c "$i"
> + done
> +
> + for i in $CHK_PERL_LIST; do
> + echo "$CHK_PERL_EXCLUDE" | grep -q "$i" && continue
> + echo "Differences for $i"
> + style_check perl "$i"
>   done
>  }
>  
>  #
> -# Perform all known syntax fixes for the configured C
> sources/headers
> +# Perform all known syntax fixes for the configured files
>  #
> -function fix_c() {
> +function fix() {
>   for i in $CHK_C_LIST; do
>   echo "$CHK_C_EXCLUDE" | grep -q "$i" && continue
>   echo "Fixing $i"
> - tool_c_style_fix "$i"
> + style_fix c "$i"
> + done
> +
> + for i in $CHK_PERL_LIST; do
> + echo "$CHK_PERL_EXCLUDE" | grep -q "$i" && continue
> + echo "Fixing $i"
> + style_fix perl "$i"
>   done
>  }
>  
> @@ -117,6 +162,7 @@ function fix_c() {
>  # main
>  
>  verify_deps astyle
> +verify_deps perltidy
>  
>  opt_fix=0
>  
> @@ -136,9 +182,9 @@ done
>  echo "=== $(date) ==="
>  echo "Code Syntax Check Results (\"check-syntax $*\")"
>  if [[ $opt_fix -eq 1 ]]; then
> - fix_c
> + fix
>  else
> - check_c
> + check
>  fi
>  echo ""
>  


Re: [RFC PATCH] tools: add perltidy to the syntax checker/fixer

2017-05-26 Thread Paul Moore
On Fri, May 26, 2017 at 11:58 AM, Paul Moore  wrote:
> From: Paul Moore 
>
> Signed-off-by: Paul Moore 
> ---
>  tools/check-syntax |   86 
> 
>  1 file changed, 66 insertions(+), 20 deletions(-)

As previously mentioned this patch is an RFC because I think we should
have some discussion about what Perl style we want to adopt for the
tests.  I'm not much of a Perl coder so I'm happy to defer to others
who may have a strong opinion.

At present the script runs perltidy without any options, according to
the manpage this should format the Perl code according to the
perlstyle(1) manpage; if no one has a strong opinion, this may be the
"right" style to use.

-- 
paul moore
www.paul-moore.com


[RFC PATCH] tools: add perltidy to the syntax checker/fixer

2017-05-26 Thread Paul Moore
From: Paul Moore 

Signed-off-by: Paul Moore 
---
 tools/check-syntax |   86 
 1 file changed, 66 insertions(+), 20 deletions(-)

diff --git a/tools/check-syntax b/tools/check-syntax
index 72cb06b..ee83c03 100755
--- a/tools/check-syntax
+++ b/tools/check-syntax
@@ -13,6 +13,9 @@
 CHK_C_LIST="$(find tests/ -name "*.c") $(find tests/ -name "*.h")"
 CHK_C_EXCLUDE=""
 
+CHK_PERL_LIST="$(find tests/ -name "*.pl") $(find tests/ -name "test")"
+CHK_PERL_EXCLUDE=""
+
 
 # functions
 
@@ -66,50 +69,92 @@ function tool_c_style() {
 }
 
 #
-# Check the formatting on a C source/header file
+# Generate a properly formatted Perl source file
 #
 # Arguments:
-# 1File to check
+# 1Source file
 #
-function tool_c_style_check() {
-   [[ -z "$1" || ! -r "$1" ]] && return
+function tool_perl_style() {
+   perltidy < "$1"
+}
 
-   tool_c_style "$1" | diff -pu --label="$1.orig" "$1" --label="$1" -
+#
+# Check the formatting on a file
+#
+# Arguments:
+# 1Language
+# 2File to check
+#
+function style_check() {
+   [[ -z "$1" ]] && return
+   [[ -z "$2" || ! -r "$2" ]] && return
+
+   case "$1" in
+   c|C)
+   tool_c_style "$2" | \
+   diff -pu --label="$2.orig" "$2" --label="$2" -
+   ;;
+   perl|Perl)
+   tool_perl_style "$2" | \
+   diff -pu --label="$2.orig" "$2" --label="$2" -
+   ;;
+   esac
 }
 
 #
-# Fix the formatting on a C source/header file
+# Fix the formatting on a file
 #
 # Arguments:
-# 1File to fix
+# 1Language
+# 2File to check
 #
-function tool_c_style_fix() {
-   [[ -z "$1" || ! -r "$1" ]] && return
+function style_fix() {
+   [[ -z "$1" ]] && return
+   [[ -z "$2" || ! -w "$2" ]] && return
 
-   tmp="$(mktemp --tmpdir=$(dirname "$1"))"
-   tool_c_style "$1" > "$tmp"
-   mv "$tmp" "$1"
+   tmp="$(mktemp --tmpdir=$(dirname "$2"))"
+   case "$1" in
+   c|C)
+   tool_c_style "$2" > "$tmp"
+   ;;
+   perl|Perl)
+   tool_perl_style "$2" > "$tmp"
+   ;;
+   esac
+   mv "$tmp" "$2"
 }
 
 #
-# Perform all known syntax checks for the configured C sources/headers
+# Perform all known syntax checks for the configured files
 #
-function check_c() {
+function check() {
for i in $CHK_C_LIST; do
echo "$CHK_C_EXCLUDE" | grep -q "$i" && continue
echo "Differences for $i"
-   tool_c_style_check "$i"
+   style_check c "$i"
+   done
+
+   for i in $CHK_PERL_LIST; do
+   echo "$CHK_PERL_EXCLUDE" | grep -q "$i" && continue
+   echo "Differences for $i"
+   style_check perl "$i"
done
 }
 
 #
-# Perform all known syntax fixes for the configured C sources/headers
+# Perform all known syntax fixes for the configured files
 #
-function fix_c() {
+function fix() {
for i in $CHK_C_LIST; do
echo "$CHK_C_EXCLUDE" | grep -q "$i" && continue
echo "Fixing $i"
-   tool_c_style_fix "$i"
+   style_fix c "$i"
+   done
+
+   for i in $CHK_PERL_LIST; do
+   echo "$CHK_PERL_EXCLUDE" | grep -q "$i" && continue
+   echo "Fixing $i"
+   style_fix perl "$i"
done
 }
 
@@ -117,6 +162,7 @@ function fix_c() {
 # main
 
 verify_deps astyle
+verify_deps perltidy
 
 opt_fix=0
 
@@ -136,9 +182,9 @@ done
 echo "=== $(date) ==="
 echo "Code Syntax Check Results (\"check-syntax $*\")"
 if [[ $opt_fix -eq 1 ]]; then
-   fix_c
+   fix
 else
-   check_c
+   check
 fi
 echo ""