]> git.ipfire.org Git - thirdparty/man-pages.git/commitdiff
grepc, grepc.1: -c: Add flag to color uses
authorAlejandro Colomar <alx.manpages@gmail.com>
Wed, 25 May 2022 21:40:22 +0000 (23:40 +0200)
committerAlejandro Colomar <alx@kernel.org>
Wed, 29 Oct 2025 20:29:08 +0000 (21:29 +0100)
Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
bin/grepc
share/man/man1/grepc.1

index 3b5509e6012c040a77006ed4c34d20f012f0c6da..4d4f2207e9833aa4328483e1eb97081a83c7f61f 100755 (executable)
--- a/bin/grepc
+++ b/bin/grepc
@@ -33,8 +33,11 @@ grepc_usage()
 
 grepc_parse_cmd()
 {
-       while getopts "ghiklt:x:" opt; do
+       while getopts "cghiklt:x:" opt; do
                case "$opt" in
+               c)
+                       cflag='yes';
+                       ;;
                g)
                        git='yes';
                        ;;
@@ -135,14 +138,17 @@ grepc_find_files()
                find $FILES -type f;
        fi \
        | grep -P -- "$ext" \
-       | xargs grep -${iflag}lPI -- "$1";
+       | xargs grep -${iflag}lPI -- "$1" \
+       | tee "$files_use" \
+       | xargs grep -${iflag}lP -- "$1\b" \
+       > "$files";
 }
 
 
 grepc_helper()
 {
        <"$files" \
-       xargs grep -${iflag}lPI -- "$1" \
+       xargs grep -${iflag}lP -- "$1" \
        | xargs grep -${iflag}lP -- "$2" \
        | sort \
        | xargs pcregrep -${iflag}${lflag}Mn -- "$3" /dev/null \
@@ -154,6 +160,26 @@ grepc_helper()
 }
 
 
+grepc_helper_use()
+{
+       <"$files_use" \
+       xargs grep -${iflag}lP -- "$1" \
+       | xargs grep -${iflag}lP -- "$2" \
+       | sort \
+       | xargs pcregrep -${iflag}${lflag}Mn -- "$3" /dev/null \
+       | if [ "$kflag" = 'no' ]; then
+               sed -E 's/^[^: ]+:[0-9]+:/\n\n&\n/';
+       else
+               cat;
+       fi \
+       | if [ "$cflag" = 'yes' ]; then
+               perl -pe 's/('"$identifier"')/\033[32m\1\033[0m/';
+       else
+               cat;
+       fi;
+}
+
+
 grepc_enum_constant()
 {
        grepc_helper \
@@ -305,7 +331,7 @@ grepc_type()
 
 grepc_use_enum()
 {
-       grepc_helper \
+       grepc_helper_use \
          '\benum\b' \
          "\b$1\b" \
          '(?s)^([\w[]+[\w\s]*)?\benum\b([\w \t[\]]|::)*\n*([ \t]*){[^}]*^\s*\w+[\w\s[\]=]*'"$1"'.*?^\3}.*?;';
@@ -314,7 +340,7 @@ grepc_use_enum()
 
 grepc_use_func_def()
 {
-       grepc_helper \
+       grepc_helper_use \
          "\b$1\b" \
          '.' \
          '(?s)^[\w[]([\w\s\(,\)[\]*]|::)*[\w\s\)*\]]\s+\**\w+\s*\(([\w\s\(,\)[\]*]|::)+?(\.\.\.)?\)[ \t]*\n*([ \t]*){(?:(?!^\4?}).)*'"$1"'.*?^\4}';
@@ -323,7 +349,7 @@ grepc_use_func_def()
 
 grepc_linux_use_func_syscall_def()
 {
-       grepc_helper \
+       grepc_helper_use \
          "SYSCALL_DEFINE.\(" \
          "\b$1\b" \
          '(?s)^(COMPAT_)?SYSCALL_DEFINE.\(\w+\b(?:(?!^}).)*'"$1"'.?^}';
@@ -345,7 +371,7 @@ grepc_use_func()
 
 grepc_use_macro()
 {
-       grepc_helper \
+       grepc_helper_use \
          "\b$1\b" \
          'define' \
          '(?s)^[ \t]*#\s*define\s[\s\\]*\w+\b(\([^\)]*\))?(?:(?![^\\]$).)*'"$1"'.*?[^\\]$';
@@ -354,7 +380,7 @@ grepc_use_macro()
 
 grepc_use_type_struct_union()
 {
-       grepc_helper \
+       grepc_helper_use \
          "\b(struct|union)\b" \
          "\b$1\b" \
          '(?s)^(?!^[ \t]*typedef\b)([\w[]([\w\s\(,\)[\]*]|::)*[\w\s\)*\]]\s+)?\b(struct|union)\b([\w \t[\]]|::)*\w+[ \t]*\n*([ \t]*){(?:(?!^\5?}).)*?'"$1"'.*?^\5}.*?;';
@@ -363,7 +389,7 @@ grepc_use_type_struct_union()
 
 grepc_use_type_typedef_simple()
 {
-       grepc_helper \
+       grepc_helper_use \
          '^[ \t]*typedef\s' \
          "\b$1\b" \
          '(?s)^[ \t]*typedef\s+[^{};]*'"$1"'[^{};]+;';
@@ -372,7 +398,7 @@ grepc_use_type_typedef_simple()
 
 grepc_use_type_typedef_struct_union()
 {
-       grepc_helper \
+       grepc_helper_use \
          '^[ \t]*typedef\s+(struct|union)\b[^;]*$' \
          "\b$1\b" \
          '(?s)^[ \t]*typedef\s+(struct|union)\b([\w \t[\]]|::)*\n*([ \t]*){(?:(?!^\3?}|^\s*typedef).)*'"$1"'(?:(?!^\3?}|^\s*typedef).)*^\3}\s*\w+;';
@@ -461,10 +487,11 @@ grepc_search()
 
 main()
 {
+       files_use="$(mktemp -t 'grepc.XXXXXX')";
        files="$(mktemp -t 'grepc.XXXXXX')";
 
        grepc_parse_cmd "$@";
-       grepc_find_files "$identifier" >"$files";
+       grepc_find_files "$identifier";
        grepc_search "$identifier" \
        | if [ -n "$lflag" ]; then
                sort \
index 8a74fc8f7173fb93fdf5e799e9e09c1e5e5d5a5e..aa69c7bd2a82696248b0a909c26b5866558d41e0 100644 (file)
@@ -108,6 +108,13 @@ Uses within type definitions
 .RE
 .SH OPTIONS
 .TP
+.B \-c
+Surround the matched strings
+with escape sequences to display them in color on the terminal.
+Only uses
+.RB ( u\fI*\fP )
+are highlighted.
+.TP
 .B \-g
 Restrict the search to files tracked by git.
 .TP