]> git.ipfire.org Git - thirdparty/man-pages.git/commitdiff
grepc: grepc_helper: Add helper function to remove repeated code
authorAlejandro Colomar <alx.manpages@gmail.com>
Sun, 8 May 2022 22:59:27 +0000 (00:59 +0200)
committerAlejandro Colomar <alx@kernel.org>
Wed, 29 Oct 2025 20:28:49 +0000 (21:28 +0100)
Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
bin/grepc

index 50b94dd66abce5518601d3d7b86b0f7e207054b8..f8c933d65132124818e28426baf6e873f34e6490 100755 (executable)
--- a/bin/grepc
+++ b/bin/grepc
@@ -7,25 +7,30 @@ if (($# != 1)); then
 fi;
 
 
-function grepc_macro_simple()
+function grepc_helper()
 {
        find . -type f \
-       | grep '\.[ch]$' \
-       | xargs grep -lP "define\s+$1\b[^(]" \
+       | grep "$1" \
+       | xargs grep -lP "$2" \
        | sort \
-       | xargs pcregrep -Mn "(?s)define\s+$1\b[^(].*?[^\\\\]$" /dev/null \
+       | xargs pcregrep -Mn "$3" /dev/null \
        | sed -E 's/^[^: ]+:[0-9]+:/\n\n&\n\n/';
 }
 
 
+function grepc_macro_simple()
+{
+       grepc_helper '\.[ch]$' \
+         "define\s+$1\b[^(]" \
+         "(?s)define\s+$1\b[^(].*?[^\\\\]$";
+}
+
+
 function grepc_macro_func()
 {
-       find . -type f \
-       | grep '\.[ch]$' \
-       | xargs grep -lP "define\s+$1\(" \
-       | sort \
-       | xargs pcregrep -Mn "(?s)define\s+$1\(.*?[^\\\\]$" /dev/null \
-       | sed -E 's/^[^: ]+:[0-9]+:/\n\n&\n\n/';
+       grepc_helper '\.[ch]$' \
+         "define\s+$1\(" \
+         "(?s)define\s+$1\(.*?[^\\\\]$";
 }
 
 
@@ -38,37 +43,25 @@ function grepc_macro()
 
 function grepc_enum_constant()
 {
-       find . -type f \
-       | grep '\.[ch]$' \
-       | xargs grep -lP "\b$1\s*[,=]" \
-       | sort \
-       | xargs pcregrep -Mn \
-         "(?s)\benum\b\s*[\w\s[\]]*{[^}]*\b$1\s*[=,].*?^}.*?;" /dev/null \
-       | sed -E 's/^[^: ]+:[0-9]+:/\n\n&\n\n/';
+       grepc_helper '\.[ch]$' \
+         "\b$1\s*[,=]" \
+         "(?s)\benum\b\s*[\w\s[\]]*{[^}]*\b$1\s*[=,].*?^}.*?;";
 }
 
 
 function grepc_func_decl()
 {
-       find . -type f \
-       | grep '\.[ch]$' \
-       | xargs grep -lP "\b$1\s*\(" \
-       | sort \
-       | xargs pcregrep -Mn \
-         "(?s)^[\w[][\w\s(,)[:\]*]+\s+\**$1\s*\([\w\s(,)[\]*]+?(...)?\)[\w\s(,)[:\]]*;" /dev/null \
-       | sed -E 's/^[^: ]+:[0-9]+:/\n\n&\n\n/';
+       grepc_helper '\.[ch]$' \
+         "\b$1\s*\(" \
+         "(?s)^[\w[][\w\s(,)[:\]*]+\s+\**$1\s*\([\w\s(,)[\]*]+?(...)?\)[\w\s(,)[:\]]*;";
 }
 
 
 function grepc_func_def()
 {
-       find . -type f \
-       | grep '\.[ch]$' \
-       | xargs grep -lP "\b$1\s*\(" \
-       | sort \
-       | xargs pcregrep -Mn \
-         "(?s)^[\w[][\w\s(,)[:\]*]+\s+\**$1\s*\([\w\s(,)[\]*]+?(...)?\)\s*{.*?^}" /dev/null \
-       | sed -E 's/^[^: ]+:[0-9]+:/\n\n&\n\n/';
+       grepc_helper '\.[ch]$' \
+         "\b$1\s*\(" \
+         "(?s)^[\w[][\w\s(,)[:\]*]+\s+\**$1\s*\([\w\s(,)[\]*]+?(...)?\)\s*{.*?^}";
 }
 
 
@@ -81,24 +74,17 @@ function grepc_func()
 
 function grepc_syscall_decl()
 {
-       find . -type f \
-       | grep '\.[ch]$' \
-       | xargs grep -lP "\bsys_$1\s*\(" \
-       | sort \
-       | xargs pcregrep -Mn \
-         "(?s)^asmlinkage\s+[\w\s]+\**sys_$1\s*\(.*?\)" /dev/null \
-       | sed -E 's/^[^: ]+:[0-9]+:/\n\n&\n\n/';
+       grepc_helper '\.[ch]$' \
+         "\bsys_$1\s*\(" \
+         "(?s)^asmlinkage\s+[\w\s]+\**sys_$1\s*\(.*?\)";
 }
 
 
 function grepc_syscall_def()
 {
-       find . -type f \
-       | grep '\.c$' \
-       | xargs grep -lP "SYSCALL_DEFINE.\($1\b" \
-       | sort \
-       | xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\($1\b.*?^}" /dev/null \
-       | sed -E 's/^[^: ]+:[0-9]+:/\n\n&\n\n/';
+       grepc_helper '\.c$' \
+         "SYSCALL_DEFINE.\($1\b" \
+         "(?s)^\w*SYSCALL_DEFINE.\($1\b.*?^}";
 }
 
 
@@ -111,35 +97,25 @@ function grepc_syscall()
 
 function grepc_type_struct_union_enum()
 {
-       find . -type f \
-       | grep '\.[ch]$' \
-       | xargs grep -lP "\b(struct|union|enum)\s+$1\b" \
-       | sort \
-       | xargs pcregrep -Mn \
-         "(?s)^([\w[][\w\s(,)[:\]*]+\s+)?\b(struct|union|enum)\s+$1\b\s*[\w\s[\]]*{.*?^}.*?;" /dev/null \
-       | sed -E 's/^[^: ]+:[0-9]+:/\n\n&\n\n/';
+       grepc_helper '\.[ch]$' \
+         "\b(struct|union|enum)\s+$1\b" \
+         "(?s)^([\w[][\w\s(,)[:\]*]+\s+)?\b(struct|union|enum)\s+$1\b\s*[\w\s[\]]*{.*?^}.*?;";
 }
 
 
 function grepc_type_typedef_simple()
 {
-       find . -type f \
-       | grep '\.[ch]$' \
-       | xargs grep -lP "\b$1;" \
-       | sort \
-       | xargs pcregrep -Mn "(?s)^typedef\s+[^;]+$1;" /dev/null \
-       | sed -E 's/^[^: ]+:[0-9]+:/\n\n&\n\n/';
+       grepc_helper '\.[ch]$' \
+         "\b$1;" \
+         "(?s)^typedef\s+[^;]+$1;";
 }
 
 
 function grepc_type_typedef_struct_union_enum()
 {
-       find . -type f \
-       | grep '\.[ch]$' \
-       | xargs grep -lP "^}\s*$1;" \
-       | sort \
-       | xargs pcregrep -Mn "(?s)^typedef\s(?:(?!^}).)*^}\s*$1;" /dev/null \
-       | sed -E 's/^[^: ]+:[0-9]+:/\n\n&\n\n/';
+       grepc_helper '\.[ch]$' \
+         "^}\s*$1;" \
+         "(?s)^typedef\s(?:(?!^}).)*^}\s*$1;";
 }