您的位置:首页 > 运维架构 > Shell

生成cscope.out的bash脚本

2013-08-05 22:05 162 查看
生成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、重复文件名的文件解析会失败。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  vim cscope