From ce368466b303efeba69304f2b400bca37b18a07e Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Wed, 25 May 2022 23:40:22 +0200 Subject: grepc, grepc.1: -c: Add flag to color uses Signed-off-by: Alejandro Colomar --- bin/grepc | 49 ++++++++++++++++++++++++++++++++++++++----------- share/man/man1/grepc.1 | 7 +++++++ 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/bin/grepc b/bin/grepc index 3b5509e..4d4f220 100755 --- 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 \ diff --git a/share/man/man1/grepc.1 b/share/man/man1/grepc.1 index 8a74fc8..aa69c7b 100644 --- a/share/man/man1/grepc.1 +++ b/share/man/man1/grepc.1 @@ -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 -- cgit v1.2.3