summaryrefslogtreecommitdiffstats
path: root/bin/grepc
blob: 592306df13190ff93b77fe7e093beda6181edd81 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/bin/bash


# Defaults:
A='';
B='';
C='';
c='';
h='-H';
i='';
k='no';
l='';
m='';
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_r='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';
t_v='no';
x=''


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


while getopts "A:B:C:chiklm:nrt:x:" 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';		;;&
	m)	m="-m$OPTARG";	;;&
	n)	n='-n';		;;&
	r)	r='yes';	;;&
	t)
		if [ -z "$x" ]; then
			x='c';
		fi;

		case "$x" in
		c)
			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;
			;;

		mk)
			case "$OPTARG" in
			r)		t_r='yes';		;;&
			v)		t_v='yes';		;;&
			[rv])
					t='yes';
					;;
			*)
					grepc_err "-$opt: $OPTARG: Unknown argument.";
					;;
			esac;
			;;
		esac;
		;;
	x)
		if [ "$t" = 'yes' ]; then
			grepc_err "-$opt: This option must come before '-t'.";
		fi;
		if [ -n "$x" ]; then
			grepc_err "-$opt: This option cannot be repeated.";
		fi;

		case "$OPTARG" in
		c)	x='c';		;;
		mk)	x='mk';		;;
		*)
			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 [ -z "$x" ]; then
	x='c';
fi;
if [ "$t" = 'no' ]; then
	if [ "$x" = 'c' ]; 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';
	elif [ "$x" = 'mk' ]; then
		t_v='yes';
	fi;
fi;


grepc_c_e()		{ printf '%s\n' '(?s)^([\w[]+[\w\s]*)?\benum\b[ \t]*([\w \t[\]]|::)*\n*([ \t]*){[^}]*^[ \t]*'"$1"'\b\s*[=,].*?^\3}.*?;'; }
grepc_c_fp()		{ printf '%s\n' '(?s)^[\w[](?:[\w\s\(,\)[\]*]|::)+[\w\s\)*\]]\s+\**\(?'"$1"'\)?\s*(\((?:[\w\s,[\]*]|::|(?1))*(?:\.\.\.)?\))(?:[\w\s\(,\)[\]]|::)*;'; }
grepc_c_fd()		{ printf '%s\n' '(?s)^[\w[](?:[\w\s\(,\)[\]*]|::)*[\w\s\)*\]]\s+\**\(?'"$1"'\)?\s*(\((?:[\w\s,[\]*]|::|(?1))*(?:\.\.\.)?\))[ \t]*\n([ \t]*){.*?^\2}'; }
grepc_c_fgd_libm()	{ grepc_c_fd "M_DECL_FUNC \(__$1\)"; }
grepc_c_fgd_libio()	{ grepc_c_fd "_IO_$1"; }
grepc_c_fgp_libio()	{ grepc_c_fp "_IO_$1"; }
grepc_c_fgp()		{ grepc_c_fgp_libio "$1"; }
grepc_c_flp()		{ printf '%s\n' '(?s)^asmlinkage\s+[\w\s]+\**sys_'"$1"'\s*\(.*?\)'; }
grepc_c_fld()		{ printf '%s\n' '(?s)^(COMPAT_)?SYSCALL_DEFINE.\('"$1"'\b.*?^}'; }
grepc_c_mf()		{ printf '%s\n' '(?s)^[ \t]*#\s*define\s[\s\\]*'"$1"'\(.*?[^\\]$'; }
grepc_c_mo()		{ printf '%s\n' '(?s)^[ \t]*#\s*define\s[\s\\]*'"$1"'\b(?!\().*?(?<!\\)$'; }
grepc_c_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_c_t_td_simple()	{ printf '%s\n' '(?s)^[ \t]*typedef\s+[^{};]+'"$1"';'; }
grepc_c_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_c_t_td_func()	{ printf '%s\n' '(?s)^[ \t]*typedef\s+[^{};]+\(\**'"$1"'\)\s*\([^{};]+;'; }
grepc_c_ue()		{ printf '%s\n' '(?s)^([\w[]+[\w\s]*)?\benum\b([\w \t[\]]|::)*\n*([ \t]*){[^}]*^\s*\w+[\w\s[\]=]*'"$1"'.*?^\3}.*?;'; }
grepc_c_uf_def()	{ printf '%s\n' '(?s)^[\w[]([\w\s\(,\)[\]*]|::)*[\w\s\)*\]]\s+\**\w+\s*\(([\w\s\(,\)[\]*]|::)+?(\.\.\.)?\)[ \t]*\n*([ \t]*){(?:(?!^\4?}).)*'"$1"'.*?^\4}'; }
grepc_c_uf_linux_def()	{ printf '%s\n' '(?s)^(COMPAT_)?SYSCALL_DEFINE.\(\w+\b(?:(?!^}).)*'"$1"'.?^}'; }
grepc_c_um()		{ printf '%s\n' '(?s)^[ \t]*#\s*define\s[\s\\]*\w+\b(\([^\)]*\))?(?:(?![^\\]$).)*'"$1"'.*?[^\\]$'; }
grepc_c_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_c_ut_td_simple()	{ printf '%s\n' '(?s)^[ \t]*typedef\s+[^{};]*'"$1"'[^{};]+;'; }
grepc_c_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+;'; }

grepc_mk_r()		{ printf '%s\n' '(?s)^(\$\()?'"$1"'\)?\s*:[^=].*?(?<!\\)$(?:(?!^[^\t]).)*'; }
grepc_mk_v()		{ printf '%s\n' '(?s)^'"$1"'\s*[:?+]*=.*?(?<!\\)$'; }


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


(
	if [ "$x" = 'c' ]; then
		test $t_e = yes			&& grepc_c_e "$identifier";
		test $t_fp = yes		&& grepc_c_fp "$identifier";
		test $t_fd = yes		&& grepc_c_fd "$identifier";
		test $t_flp = yes		&& grepc_c_flp "$identifier";
		test $t_fld = yes		&& grepc_c_fld "$identifier";
		test $t_fgp = yes		&& grepc_c_fgp "$identifier";
		test $t_fgd_libm = yes		&& grepc_c_fgd_libm "$identifier";
		test $t_fgd_libio = yes		&& grepc_c_fgd_libio "$identifier";
		test $t_mf = yes		&& grepc_c_mf "$identifier";
		test $t_mo = yes		&& grepc_c_mo "$identifier";
		test $t_t_braced = yes		&& grepc_c_t_braced "$identifier";
		test $t_t_td_simple = yes	&& grepc_c_t_td_simple "$identifier";
		test $t_t_td_braced = yes	&& grepc_c_t_td_braced "$identifier";
		test $t_t_td_func = yes		&& grepc_c_t_td_func "$identifier";
		test $t_ue = yes		&& grepc_c_ue "$identifier";
		test $t_uf_def = yes		&& grepc_c_uf_def "$identifier";
		test $t_uf_linux_def = yes	&& grepc_c_uf_linux_def "$identifier";
		test $t_um = yes		&& grepc_c_um "$identifier";
		test $t_ut_su = yes		&& grepc_c_ut_su "$identifier";
		test $t_ut_td_simple = yes	&& grepc_c_ut_td_simple "$identifier";
		test $t_ut_td_su = yes		&& grepc_c_ut_td_su "$identifier";
	elif [ "$x" = 'mk' ]; then
		test $t_r = yes			&& grepc_mk_r "$identifier";
		test $t_v = yes			&& grepc_mk_v "$identifier";
	fi;
) >"$patterns";


# shellcheck disable=SC2206  # We want only non-empty variables in the array.
opts=($A $B $C $c $h $i $l -M $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;