From: dave@aspect.UUCP (Dave Corcoran)
Newsgroups: comp.lang.c
Subject: Re: Listing names of functions in a file xxx.c
Summary: unix (SunOS) bourne scripts to do it
Date: 10 Apr 92 18:46:56 GMT
Organization: Aspect Telecommunications, San Jose, Ca

In article <2304@keele.keele.ac.uk>, csd09@seq1.keele.ac.uk (A. Vasilakis) writes:
> I am interested in a utility that given a file that contains C source,
> will produce a list of all the functions that this file contains.
> This is usefull if I have a collection of .c files that make a
> program together and want to find out how many functions each file has.
> 

Tried email but it bounced...



These two scripts were intended for personal use so they are  not well
commented and may impose too many constrains for a coding style dissimilar from
mine. They run under SunOS and use ctags; you will have to modify them
to be used under SYSV, specifically the first line:

	#! /bin/sh

should be changed to:

	:

This extracts the function prototype (K&R), the argument list up to
but not including the opening brace and the first line after the brace
if the line contains "rtn"; the variable I use for a return value.
Use this if you want to extract a synopsis of the functions in a file.

--------------------8<--------------------
#! /bin/sh
# extracts function headers from K&R c source
# bugs  -
# 	#define()'s screw it up
#	opening brace must be in leftmost column
#	all function return types must be on one line:
#
#	void func(a,b,c)
#	int a;
#	int b;
#	{
# 
# NOT
#
#	void
#	func(a,b)
#	int a;
#	int b;
#	{
#
tmpdir=/usr/tmp

for i in $*
do
	sed '/^#[ 	]*define/d' $i > $tmpdir/.$$. 
	sedscript=`
		ctags -x $tmpdir/.$$. | 
		awk '{print $2}' | sort -n |
		sed 's@\(.*\)@\1s/^/%/;\1,/^{/{p;/^{/N;/rtn/p;}@'
		`
	sed -n "$sedscript" $tmpdir/.$$. |
		sed '/^%/i\

	s/^%//
	/^{/d'
	rm -f $tmpdir/.$$.
done
--------------------8<--------------------

============================================================
============================================================

This routine extracts the functions in a source file, the output of which
is suitable to be include as a header proto-type. 

Source file constraints:
	do not put parens in comment on same line with the function
		e.g. fnc(a,b) /* ) closing paren in *COMMENT* screws it up */
	the full arg list must be on same line as function name
		e.g. fnc(int i, int k, int l, SOMETYPE *junk) /* ANSI */
			fnc(i,k,l,junk)	/* K&R */
	there may be others I have not encountered

--------------------8<--------------------
#! /bin/sh
# extracts functions declarations from any *.c file

_xfncs()
{
	while read i
	do
		case $i in
			s.*)
				;;
			*)	ctags -x $i
				;;
		esac
	done  |  awk '
		$4 !~ /^#define/ {for (i=4;i<=NF;i++) printf "%s ",$i; printf "\n";}'|
		sed 's/([^)]*)/();/' # use this script if K&R prototype is desired 
		# sed 's/)/);/' # use this script if ANSI prototype is desired 
}	

usage()
{
	echo 'usage
	xfncs [-lg] file1 fileN...

	Writes function prototypes to stdout

	-l	extract only local (static) functions 
	-g	extract only global functions; global functions are identified
		by a pseudo tag: GLOBAL or PUBLIC
		to create a pseudo put 
		
			#define GLOBAL

		in your source file
		then use it thus

			GLOBAL fnc(x)
			int x;
			{
	'
	exit 1
}

## begin

set -- `getopt lg $*`
if [ $? != 0 ]; then usage; fi

re=.

while true
do	#
	case $1 in
		-g ) shift
			# add your favorite pseudo tag here
			# something must be here for the -g flag to work
			re='(GLOBAL|PUBLIC)'
			;;
		-l ) shift
			re='(LOCAL|PRIVATE|static)'
			;;
		-- ) shift
			break
			;;
	esac
done

if [ $# = 0 ]
then
	cat | _xfncs
else 
	echo $* | _xfncs 
fi | egrep $re
--------------------8<--------------------
-- 
David Corcoran		      -@@
uunet!aspect!dave	        ~
Bow een nit Dad!
					--  Jennifer Corcoran (age 2)