#
# Copyright (c) 2008 Bruce Blinn
#
# To use this file, add the following lines to your .bashrc file.  Change 
# the file path as appropriate for the location where this file is stored
# in your environment.
#
#	#
#	# Initialize the cscope commands.
#	#
#	if [ -f $HOME/bin/cscope.sh ]; then
#		. $HOME/bin/cscope.sh
#	fi
#
#
# cscope commands
#
#	csbuild		create the cscope database (cscope.out)
#	cs		run cscope interactively
#	whorefs		find all references to a symbol
#	whodefs		find the definition of a symbol
#	callswho	find the functions called by a function
#	whocalls	find the functions that call a function
#	findstring	search for a string
#	findpattern	search files for a pattern
#	findfile	find a file
#	whoincludes	find the files that include a header file
#
# The cscope database (cscope.out) needs to be in the current directory before
# using these functions.  If it does not exist, use csbuild to build the
# database.
#
cs()		{ cscope -d; }
whorefs()	{ cscope -d -f $LINUX/cscope.out -L -0 "$@" | sort | uniq; }
whodefs()	{ cscope -d -f $LINUX/cscope.out -L -1 "$@" | sort | uniq; }
callswho()	{ cscope -d -f $LINUX/cscope.out -L -2 "$@" | awk '{print $2}' | sort | uniq; }
whocalls()	{ cscope -d -f $LINUX/cscope.out -L -3 "$@" | sort | uniq; }
findstring()	{ cscope -d -f $LINUX/cscope.out -L -4 "$@" | sort | uniq; }
unused_1()	{ cscope -d -f $LINUX/cscope.out -L -5 "$@" | sort | uniq; }
findpattern()	{ cscope -d -f $LINUX/cscope.out -L -6 "$@" | sort | uniq; }
findfile()	{ cscope -d -f $LINUX/cscope.out -L -7 "$@" | sort | uniq; }
whoincludes()	{ cscope -d -f $LINUX/cscope.out -L -8 "$@" | awk '{print $1}' | sort | uniq; }
unused_2()	{ cscope -d -f $LINUX/cscope.out -L -9 "$@" | sort | uniq; }

csbuild() {
	#
	# NAME
	#	csbuild - create the cscope database (cscope.out)
	#
	# SYNOPSIS
	#	csbuild
	#
	# DESCRIPTION
	#	This command will create the cscope database (cscope.out) for
	#	the current directory and its subdirectories.
	#
	########################################################################

	#
	# If cscope database exists, remove it.
	#
	rm -f cscope.files
	rm -f cscope.out

	#
	# Recursively search the current directory for files that can
	# be used by cscope.
	#
	find . -follow -name "*.[chSly]" -print >cscope.files
	if [ ! -s cscope.files ]; then
		rm -f cscope.files
		echo "csbuild: No source files were found." 1>&2
		return 1
	fi

	#
	# Build/update the database.
	#
	cscope -b 2>&1 | grep -v "cscope: building symbol database"
	return 0		# Override exit status from grep
}
