生成cscope.out的bash腳本

生成cscope.out的bash腳本,接收兩個參數,第一個是源文件路徑,第二個是存放cscope.out文件的目錄名。

修改和make_find_arg()裏面的FILETYPES和FILENAMES數組,就可以修改和增刪cscope需要解析的文件。

#########################################################################
# File Name: makescope.sh
# Author: tintinr
# mail: [email protected]
# Created Time: 日  8/ 4 10:51:38 2013
#########################################################################
#!/bin/bash

make_find_arg()
{
	# 文件類型,如*.c
	FILETYPES=("c" "cc" "cpp" "h" 
			   "mk" 'sh' 
			   "java" 
			   "S")
	# 文件名,如Makefile
	FILENAMES=("Makefile" "makefile")
	for each_file_type in ${FILETYPES[@]}
	do
		if [ -n "$find_arg" ]
		then
			single_find_arg=" -o "
		else
			single_find_arg=""
		fi
		single_find_arg+="-name \"*." 
		single_find_arg+=$each_file_type
		single_find_arg+="\" "
		find_arg+=$single_find_arg
	done

	for each_file in ${FILENAMES[@]}
	do
		single_find_arg=" -o -name \""
		single_find_arg+=$each_file
		single_find_arg+="\" "
		find_arg+=$single_find_arg
	done
}

usage()
{
	echo "Usage: makescope src_path project_name"
	echo "==src_path: source root path"
	echo "==project_name: cscope db will be stored in ~/cscope/project_name/ dir"
}

if [ $# -ne 2 ]
then
	usage
	return -1
fi

src_path=$1
project_path=~/cscope/$2

current_dir=$(PWD)
mkdir -p $project_path
cd $project_path

find_arg=""
make_find_arg
echo "find" $src_path " " $find_arg
find_str="find "
find_str+=$src_path
find_str+=" "
find_str+=$find_arg
eval $find_str > cscope.files
cscope -bkq -i cscope.files


cd $current_dir

功能雖然實現了,但是看上去有些醜,重構之:

#########################################################################
# File Name: makescope.sh
# Author: tintinr
# mail: [email protected]
# Created Time: 日  8/ 4 10:51:38 2013
#########################################################################
#!/bin/bash

make_find_name_arg()
{
	file_prefix=$1
	declare -a file_types=("${!2}")

	for each_file in ${file_types[@]}
	do
		if [ -n "$find_arg" ]
		then
			single_find_arg="-o "
		else
			single_find_arg=""
		fi
		single_find_arg+="-name \""
		single_find_arg+=$file_prefix
		single_find_arg+=$each_file
		single_find_arg+="\" "
		find_arg+=$single_find_arg
	done
}

make_find_arg()
{
	# 文件類型,如*.c
	local FILETYPES=("c" "cc" "cpp" "h" 
			   "mk" 'sh' 
			   "java" 
			   "S")
	# 文件名,如Makefile

	local FILENAMES=("Makefile" "makefile")
	make_find_name_arg "*." FILETYPES[@]
	make_find_name_arg "" FILENAMES[@]
}

build_ecsope_file()
{
	find_arg=""
	make_find_arg
	#echo "find" $src_path " " $find_arg
	find_str="find "
	find_str+=$src_path
	find_str+=" "
	find_str+=$find_arg
	eval $find_str > cscope.files
}

usage()
{
	echo "Usage: makescope src_path project_name"
	echo "==src_path: source root path"
	echo "==project_name: cscope db will be stored in ~/cscope/project_name/ dir"
}


main()
{
	current_dir=$(PWD)
	mkdir -p $project_path
	cd $project_path

	build_ecsope_file
	cscope -bkq -i cscope.files

	cd $current_dir
}

if [ $# -ne 2 ]
then
	usage
	return -1
fi

src_path=$1
project_path=~/cscope/$2
main

在腳本驗證過程中,貌似發現cscope兩個限制:

1、文件名長度限制:最長只支持250個字符。

2、重複文件名的文件解析會失敗。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章