find struct with gtags

#! /bin/bash



## check_cache struct history.
## @output: "line file"
function check_cache ()
{
    if test $# -ne 2; then
    return 1;
    else
    local struct="$1"
    local cache="$2"

    result=$(awk '{if ($1 == "'"${struct}"'") {printf "%s %s",$2,$3; exit 0}}' /
        ${cache} 2>/dev/null)
    fi
}


## print_struct line_number file.
## @input "line file"
function print_result ()
{
    if test $# -ne 1 ; then
    return 1;
    else

    local line_number=$(echo ${1} | cut -f 1 -d " ")
    local file=$(echo ${1} | cut -f 2 -d " ")

    less +${line_number} "${file}"
    fi
}

####################################
## begin here.
####################################
declare struct="$1"
declare work_dir=


case $# in
    0)
    exit
    ;;
    1)
    struct="$1"
    ;;
    2)
    struct="$1"
    working_dir="$2"
    ;;
    *)
    echo "Too much parameter."
    exit 1
    ;;
esac
   
   



declare cache_file=
declare result=
declare number=
declare results=


if test -z "${working_dir}";then
    if test -z "${GTAGSROOT}" ;then
    if test -f GTAGS -a -f GPATH -a -f GSYMS -a -f GRTAGS;then
        export GTAGSROOT="${PWD}"
        cache_file="${PWD}/.cache_file"

        if !test -f "${cache_file}"; then
        touch "${cache_file}" 2>/dev/null 1>&2
        if test $? -eq 0;then
            echo "Cant't make cache file."
            exit 1
        fi
        chmod 777 "${cache_file}"
        fi
    else
        echo "No TAGS database."
        exit 1
    fi
    else
    cache_file="${GTAGSROOT}/.cache_file"
    fi
else
    cd "${working_dir}" 2>/dev/null 1>&2
    if test $? -ne 0;then
    echo "Can't access ${working_dir}"
    exit 1
    else
    if test -f GTAGS -a -f GSYMS -a -f GPATH -a -f GRTAGS;then
        export GTAGSROOT="${PWD}"
        cache_file="${PWD}/.cache_file"

        if ! test -f "${cache_file}";then
        touch "${cache_file}" 2>/dev/null 1>&2
        if test $? -ne 0;then
            echo "Can't make cache file."
            exit 1
        else
            chmod 777 "${cache_file}"
        fi
        fi
    else
        echo "No TAGS database."
        exit 1
    fi
    fi
fi


check_cache "${struct}" "${cache_file}"
if ! test -z "${result}"; then
    print_result "${result}"
    exit 0;
fi



results=$(global -x "${struct}" 2>/dev/null)
if test -z "${results}"; then
    exit 1
fi

records_number=$(echo -e "$results" | wc -l)

if test ${records_number} -le 0; then
    echo "Can't find the definition of struct ${struct}"
    exit 1
else
    echo "${results}" | cat -n
    read -p "<>: " -n 3 number
    if test `expr "${number}" : '[1-9]/{1,3/}'` -ne 1;then
    echo "input error."
    exit 1
   
    else
    if test ${number} -gt ${records_number} -o ${number} -lt 1;then
        echo "input error."
        exit 1
    else
        result=$(echo "${results}" | awk '{if (NR == '"${number}"') printf "%s %s",$2,$3}')
    fi
    fi

fi

line=$(echo "${result}" | cut -f 1 -d " ")
file=$(echo "${result}" | cut -f 2 -d " ")
result_dir=${file%/*}
result_file=${file##*/}
cd ${result_dir}
echo "${struct} ${line} ${PWD}/${result_file}" >> "${cache_file}"
print_result "${line} ${result_file}"

你可能感兴趣的:(find struct with gtags)