生成cscope.out的bash脚本

生成cscope.out的bash脚本,接收两个参数,第一个是源文件路径,第二个是存放cscope.out文件的目录名。

修改和make_find_arg()里面的FILETYPES和FILENAMES数组,就可以修改和增删cscope需要解析的文件。

#########################################################################
# File Name: makescope.sh
# Author: tintinr
# mail: tintinr@gmail.com
# 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: tintinr@gmail.com
# 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、重复文件名的文件解析会失败。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值