summaryrefslogtreecommitdiffstats
path: root/bin/grepc
diff options
context:
space:
mode:
authorAlejandro Colomar <alx.manpages@gmail.com>2022-05-05 17:44:03 +0200
committerAlejandro Colomar <alx.manpages@gmail.com>2022-05-06 00:52:25 +0200
commit1d6fe852a3b891709a3fcd272aae3eb87b6fbc44 (patch)
treeb19dd4dd00ce582d079257fcf692503d462b2910 /bin/grepc
parent46df1bd7210723a35b956f8352f8fccc6b829eea (diff)
bin/grepc: Add program
Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
Diffstat (limited to 'bin/grepc')
-rwxr-xr-xbin/grepc141
1 files changed, 141 insertions, 0 deletions
diff --git a/bin/grepc b/bin/grepc
new file mode 100755
index 0000000..a9038c1
--- /dev/null
+++ b/bin/grepc
@@ -0,0 +1,141 @@
+#!/bin/bash
+
+
+function grepc_macro_simple()
+{
+ find . -type f \
+ | grep '\.[ch]$' \
+ | xargs grep -l "define $1\b[^(]" \
+ | sort \
+ | xargs pcregrep -Mn "(?s)define $1\b[^(].*?[^\\\\]$" /dev/null \
+ | sed -E 's/^[^: ]+:[0-9]+:/\n\n&\n\n/';
+}
+
+
+function grepc_macro_func()
+{
+ find . -type f \
+ | grep '\.[ch]$' \
+ | xargs grep -l "define $1(" \
+ | sort \
+ | xargs pcregrep -Mn "(?s)define $1\(.*?[^\\\\]$" /dev/null \
+ | sed -E 's/^[^: ]+:[0-9]+:/\n\n&\n\n/';
+}
+
+
+function grepc_macro()
+{
+ grepc_macro_simple $1;
+ grepc_macro_func $1;
+}
+
+
+function grepc_func_decl()
+{
+ find . -type f \
+ | grep '\.[ch]$' \
+ | xargs grep -l "\b$1\b" \
+ | sort \
+ | xargs pcregrep -Mn "(?s)^\w[\w\s*]+\s\**$1\([^;{]*?;" /dev/null \
+ | sed -E 's/^[^: ]+:[0-9]+:/\n\n&\n\n/';
+}
+
+
+function grepc_func_def()
+{
+ find . -type f \
+ | grep '\.[ch]$' \
+ | xargs grep -l "^$1\b" \
+ | sort \
+ | xargs pcregrep -Mn "(?s)^\$[\w\s*]+?^$1\(.*?^}" /dev/null \
+ | sed -E 's/^[^: ]+:[0-9]+:/\n\n&/';
+}
+
+
+function grepc_func()
+{
+ grepc_func_decl $@;
+ grepc_func_def $@;
+}
+
+
+function grepc_type_struct_union_enum()
+{
+ find . -type f \
+ | grep '\.[ch]$' \
+ | xargs grep -l "$1 *{" \
+ | sort \
+ | xargs pcregrep -Mn "(?s)^\$[\w\s]+?$1 {.*?^}.*?;" /dev/null \
+ | sed -E 's/^[^: ]+:[0-9]+:/\n\n&/';
+}
+
+
+function grepc_type_typedef_oneline()
+{
+ find . -type f \
+ | grep '\.[ch]$' \
+ | xargs grep -l "^typedef .* $1;" \
+ | sort \
+ | xargs grep -n "^typedef .* $1;" /dev/null \
+ | sed -E 's/^[^: ]+:[0-9]+:/\n\n&\n\n/';
+}
+
+
+function grepc_type_typedef_struct_union_enum()
+{
+ find . -type f \
+ | grep '\.[ch]$' \
+ | xargs grep -l "^} $1;" \
+ | sort \
+ | while read f; do
+ grep -n "^} $1;" $f /dev/null \
+ | sed -E 's/^([^: ]+:[0-9]+:).*/\n\n\1\n/';
+
+ tac <$f \
+ | sed -n "/^} $1;/,/^typedef/p" \
+ | tac;
+ done;
+}
+
+
+function grepc_type_typedef_underlying_struct_union_enum()
+{
+ find . -type f \
+ | grep '\.[ch]$' \
+ | xargs grep -h "^typedef .* *$1;" \
+ | sed -e 's/^typedef //' -e "s/ *$1;.*//" \
+ | while read t; do
+ grepc_type_struct_union_enum "$t";
+ done;
+}
+
+
+function grepc_type_typedef()
+{
+ grepc_type_typedef_oneline $1;
+ grepc_type_typedef_struct_union_enum $1;
+ grepc_type_typedef_underlying_struct_union_enum $1;
+}
+
+
+function grepc_type()
+{
+ grepc_type_struct_union_enum $1;
+ grepc_type_typedef $1;
+}
+
+
+function main()
+{
+ if (($# != 1)); then
+ >&2 echo "Usage: $0 <identifier>";
+ return 1;
+ fi;
+
+ grepc_macro $1;
+ grepc_func $1;
+ grepc_type $1;
+}
+
+
+main $@