xref: /plan9/sys/src/cmd/ssh2/funclen (revision 63afb9a5d3f910047231762bcce0ee49fed3d07c)
1#!/bin/rc
2# funclen [file]... - print lengths of C functions in file(s)
3#	assumes the one true brace style (V7 kernel style).
4#	added some slight tolerance for bogus styles.
5exec awk '
6/^(#|\/\/)|;[\t ]*$|\\$|"\)|^[\t ]*\*/	{ next }
7/^((static|void|unsigned|int|u?v?long|double|char|struct[\t ]+[a-z_0-9]+)[\t ]*)*(\*[\t ]*)*[a-zA-Z0-9_µμ]+( +__P)? *\(/ {
8	# function name
9	paren = index($0, "(")
10	prelude = substr($0, 1, paren-1)
11	n = split(prelude, fields)
12	funcname = fields[n]
13}
14/^{/ {						# function or struct start
15	if (funcname == "")
16		next
17	if (start != 0)
18		print "unclosed function " funcname " at " FILENAME ":" FNR \
19			>"/fd/2"
20	start = FNR
21	file = FILENAME
22}
23/^}[^();]*($|\/\*|\/\/)/ && $0 !~ "^}[^*/]*[;={]" {
24	# function end, not struct end
25	if (start == 0 || file != FILENAME || funcname == "")
26		print "unopened function or macro end at " \
27			FILENAME ":" FNR >"/fd/2"
28	else
29		print FNR - start "\t" FILENAME ":" start "," FNR "\t" \
30			funcname "()"
31	start = 0				# function has ended
32	funcname = ""
33}
34END {
35	if (start != 0)
36		print "unclosed function " funcname " at " FILENAME ":" FNR \
37			>"/fd/2"
38}
39' $*
40