Created
March 2, 2012 11:39
-
-
Save aprell/1957931 to your computer and use it in GitHub Desktop.
Looks for a given function in a number of files. Glorified wrapper for ctags.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
print_usage() | |
{ | |
echo "Usage: findfn [OPTION]... [FUNCTION] [FILE]..." | |
echo "Example 1: findfn foo main.c" | |
echo "Example 2: findfn foo main.c foo.c" | |
} | |
check_usage() | |
{ | |
while getopts h option; do | |
case $option in | |
h) print_usage | |
exit 0 | |
;; | |
?) print_usage | |
exit 2 | |
;; | |
esac | |
done | |
} | |
# Search for definition of $1 in list of files $2 | |
find_fn_def_with_ctags() | |
{ | |
fn=$1 | |
files=$2 | |
fn_def= | |
for file in $files; do | |
if [ -e $file ]; then | |
# Exuberant ctags only? | |
line=$(ctags -x --c-kinds=f $file | grep [\ ]$fn[\ \(]) | |
if [ -z "$line" ]; then | |
continue | |
fi | |
lineno=$(echo $line | cut -d" " -f3) | |
fn_def=$(sed -n "${lineno}p" $file) | |
prevlineno=$((lineno - 1)) | |
if [[ "$fn_def" =~ ^"$fn" ]]; then | |
fn_def_ret=$(sed -n "${prevlineno}p" $file) | |
fn_def="$fn_def_ret $fn_def" | |
fi | |
nextlineno=$((lineno + 1)) | |
while [[ ! "$fn_def" =~ "{" ]]; do | |
fn_def+=$(sed -n "${nextlineno}p" $file) | |
nextlineno=$((nextlineno + 1)) | |
done | |
fn_def=$(echo $fn_def | cut -d"{" -f1) | |
# Strip unneeded spaces from function definition | |
fn_def=$(echo $fn_def | sed "s/^ //; s/ $//" | \ | |
sed "s/$fn (/$fn(/; s/$fn( /$fn(/; s/ )$/)/" | \ | |
sed "s/ ,/,/g") | |
echo File $file, line $lineno: $fn_def | |
fi | |
done | |
} | |
check_usage $@ | |
fn=$1 | |
shift | |
files=$@ | |
find_fn_def_with_ctags $fn "$files" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment