summaryrefslogtreecommitdiffstats
path: root/bin/grepc
blob: b5a21f05ad79ac40a62ec8f1e015e946a39a8beb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/bin/bash


# Defaults:
A='';
B='';
C='';
c='';
h='-H';
i='';
k='no';
l='';
n='';
r='no';
t='no';
t_e='no';
t_fp='no';
t_fd='no';
t_flp='no';
t_fld='no';
t_fgp='no';
t_fgd_libm='no';
t_fgd_libio='no';
t_mf='no';
t_mo='no';
t_t_braced='no';
t_t_td_simple='no';
t_t_td_braced='no';
t_t_td_func='no';
t_ue='no';
t_uf_def='no';
t_uf_linux_def='no';
t_um='no';
t_ut_su='no';
t_ut_td_simple='no';
t_ut_td_su='no';


grepc_err()
{
	>&2 printf '%s\n' "$(basename "$0"): error: $*";
	exit 1;
}


while getopts "A:B:C:chiklnrt:" opt; do
	case "$opt" in
	A)	A="-A$OPTARG";	;;&
	B)	B="-B$OPTARG";	;;&
	C)	C="-C$OPTARG";	;;&
	c)	c='-c';		;;&
	h)	h='-h';		;;&
	i)	i='-i';		;;&
	k | l)	k='yes';	;;&
	l)	l='-l';		;;&
	n)	n='-n';		;;&
	r)	r='yes';	;;&
	t)
		case "$OPTARG" in
		e)		t_e='yes';		;;&
		f | fp)		t_fp='yes';		;;&
		f | fd)		t_fd='yes';		;;&
		f | fl | flp)	t_flp='yes';		;;&
		f | fl | fld)	t_fld='yes';		;;&
		f | fg | fgp)	t_fgp='yes';		;;&
		f | fg | fgd)	t_fgd_libm='yes';
				t_fgd_libio='yes';	;;&
		m | mf)		t_mf='yes';		;;&
		m | mo)		t_mo='yes';		;;&
		t)		t_t_braced='yes';
				t_t_td_simple='yes';
				t_t_td_braced='yes';
				t_t_td_func='yes';	;;&
		u | ue)		t_ue='yes';		;;&
		u | uf)		t_uf_def='yes';
				t_uf_linux_def='yes';	;;&
		u | um)		t_um='yes';		;;&
		u | ut)		t_ut_su='yes';
				t_ut_td_simple='yes';
				t_ut_td_su='yes';	;;&
		[efmtu] | f[pdlg] | fl[pd] | fg[pd] | m[fo] | u[efmt])
				t='yes';
				;;
		*)
				grepc_err "-$opt: $OPTARG: Unknown argument.";
				;;
		esac;
		;;
	\?)
		exit 1;
		;;
	esac;
done;
shift $((OPTIND-1));

test $# -lt 1 && grepc_err "Missing identifier.";
identifier=$1;
shift;

if [ "$t" = 'no' ]; then
	t_e='yes';
	t_fp='yes';
	t_fd='yes';
	t_flp='yes';
	t_fld='yes';
	t_fgp='yes';
	t_fgd_libm='yes';
	t_fgd_libio='yes';
	t_mf='yes';
	t_mo='yes';
	t_t_braced='yes';
	t_t_td_simple='yes';
	t_t_td_braced='yes';
	t_t_td_func='yes';
fi;


grepc_e()		{ printf '%s\n' '(?s)^([\w[]+[\w\s]*)?\benum\b[ \t]*([\w \t[\]]|::)*\n*([ \t]*){[^}]*^[ \t]*'"$1"'\b\s*[=,].*?^\3}.*?;'; }
grepc_fp()		{ printf '%s\n' '(?s)^[\w[](?:[\w\s\(,\)[\]*]|::)+[\w\s\)*\]]\s+\**\(?'"$1"'\)?\s*(\((?:[\w\s,[\]*]|::|(?1))*(?:\.\.\.)?\))(?:[\w\s\(,\)[\]]|::)*;'; }
grepc_fd()		{ printf '%s\n' '(?s)^[\w[](?:[\w\s\(,\)[\]*]|::)*[\w\s\)*\]]\s+\**\(?'"$1"'\)?\s*(\((?:[\w\s,[\]*]|::|(?1))*(?:\.\.\.)?\))[ \t]*\n([ \t]*){.*?^\2}'; }
grepc_fgd_libm()	{ grepc_fd "M_DECL_FUNC \(__$1\)"; }
grepc_fgd_libio()	{ grepc_fd "_IO_$1"; }
grepc_fgp_libio()	{ grepc_fp "_IO_$1"; }
grepc_fgp()		{ grepc_fgp_libio "$1"; }
grepc_flp()		{ printf '%s\n' '(?s)^asmlinkage\s+[\w\s]+\**sys_'"$1"'\s*\(.*?\)'; }
grepc_fld()		{ printf '%s\n' '(?s)^(COMPAT_)?SYSCALL_DEFINE.\('"$1"'\b.*?^}'; }
grepc_mf()		{ printf '%s\n' '(?s)^[ \t]*#\s*define\s[\s\\]*'"$1"'\(.*?[^\\]$'; }
grepc_mo()		{ printf '%s\n' '(?s)^[ \t]*#\s*define\s[\s\\]*'"$1"'\b(?!\().*?(?<!\\)$'; }
grepc_t_braced()	{ printf '%s\n' '(?s)^([\w[]([\w\s\(,\)[\]*]|::)*[\w\s\)*\]]\s+)?\b(struct|union|enum)\b([\w \t[\]]|::)+\b'"$1"'\b[ \t]*\n*([ \t]*){.*?^\5}.*?;'; }
grepc_t_td_simple()	{ printf '%s\n' '(?s)^[ \t]*typedef\s+[^{};]+'"$1"';'; }
grepc_t_td_braced()	{ printf '%s\n' '(?s)^[ \t]*typedef\s+(struct|union|enum)\b(?:(?!\W'"$1"'\W)([\w \t[\]]|::))*\n*([ \t]*){(?:(?!^\3?}).)*?^\3}\s*'"$1"'(\[[\w\(,\)]\])*;'; }
grepc_t_td_func()	{ printf '%s\n' '(?s)^[ \t]*typedef\s+[^{};]+\(\**'"$1"'\)\s*\([^{};]+;'; }
grepc_ue()		{ printf '%s\n' '(?s)^([\w[]+[\w\s]*)?\benum\b([\w \t[\]]|::)*\n*([ \t]*){[^}]*^\s*\w+[\w\s[\]=]*'"$1"'.*?^\3}.*?;'; }
grepc_uf_def()		{ printf '%s\n' '(?s)^[\w[]([\w\s\(,\)[\]*]|::)*[\w\s\)*\]]\s+\**\w+\s*\(([\w\s\(,\)[\]*]|::)+?(\.\.\.)?\)[ \t]*\n*([ \t]*){(?:(?!^\4?}).)*'"$1"'.*?^\4}'; }
grepc_uf_linux_def()	{ printf '%s\n' '(?s)^(COMPAT_)?SYSCALL_DEFINE.\(\w+\b(?:(?!^}).)*'"$1"'.?^}'; }
grepc_um()		{ printf '%s\n' '(?s)^[ \t]*#\s*define\s[\s\\]*\w+\b(\([^\)]*\))?(?:(?![^\\]$).)*'"$1"'.*?[^\\]$'; }
grepc_ut_su()		{ printf '%s\n' '(?s)^(?!^[ \t]*typedef\b)([\w[]([\w\s\(,\)[\]*]|::)*[\w\s\)*\]]\s+)?\b(struct|union)\b([\w \t[\]]|::)*\w+[ \t]*\n*([ \t]*){(?:(?!^\5?}).)*?'"$1"'.*?^\5}.*?;'; }
grepc_ut_td_simple()	{ printf '%s\n' '(?s)^[ \t]*typedef\s+[^{};]*'"$1"'[^{};]+;'; }
grepc_ut_td_su()	{ printf '%s\n' '(?s)^[ \t]*typedef\s+(struct|union)\b([\w \t[\]]|::)*\n*([ \t]*){(?:(?!^\3?}|^\s*typedef).)*'"$1"'(?:(?!^\3?}|^\s*typedef).)*^\3}\s*\w+;'; }


patterns="$(mktemp -t grepc.patterns.XXXXXX)";


(
	test $t_e = yes			&& grepc_e "$identifier";
	test $t_fp = yes		&& grepc_fp "$identifier";
	test $t_fd = yes		&& grepc_fd "$identifier";
	test $t_flp = yes		&& grepc_flp "$identifier";
	test $t_fld = yes		&& grepc_fld "$identifier";
	test $t_fgp = yes		&& grepc_fgp "$identifier";
	test $t_fgd_libm = yes		&& grepc_fgd_libm "$identifier";
	test $t_fgd_libio = yes		&& grepc_fgd_libio "$identifier";
	test $t_mf = yes		&& grepc_mf "$identifier";
	test $t_mo = yes		&& grepc_mo "$identifier";
	test $t_t_braced = yes		&& grepc_t_braced "$identifier";
	test $t_t_td_simple = yes	&& grepc_t_td_simple "$identifier";
	test $t_t_td_braced = yes	&& grepc_t_td_braced "$identifier";
	test $t_t_td_func = yes		&& grepc_t_td_func "$identifier";
	test $t_ue = yes		&& grepc_ue "$identifier";
	test $t_uf_def = yes		&& grepc_uf_def "$identifier";
	test $t_uf_linux_def = yes	&& grepc_uf_linux_def "$identifier";
	test $t_um = yes		&& grepc_um "$identifier";
	test $t_ut_su = yes		&& grepc_ut_su "$identifier";
	test $t_ut_td_simple = yes	&& grepc_ut_td_simple "$identifier";
	test $t_ut_td_su = yes		&& grepc_ut_td_su "$identifier";
) >"$patterns";


# shellcheck disable=SC2206  # We want only non-empty variables in the array.
opts=($A $B $C $c $h $i $l -M $n);


if test -z "$*"; then
	pcre2grep "${opts[@]}" -f "$patterns";
else
	find "$@" -type f \
	| if test -z "$c"; then
		xargs grep -${i}lPI -- "$identifier";
	else
		cat;
	fi \
	| xargs pcre2grep "${opts[@]}" -f "$patterns";
fi \
| sed -E -f <(test "$k" = 'no' && printf '%s\n' 's/^[^: ]+:[0-9]+:/\n\n&\n/') \
| perl -pe "$(test "$r" = 'yes' && printf '%s\n' 's/('"$identifier"')/\033[32m\1\033[0m/' || printf '%s\n' 's///')" \
| if [ -n "$l" ]; then
	sort \
	| uniq;
else
	cat;
fi;