Skip to content

Instantly share code, notes, and snippets.

@richlowe
Last active May 5, 2019 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richlowe/1722376 to your computer and use it in GitHub Desktop.
Save richlowe/1722376 to your computer and use it in GitHub Desktop.
csgrep: Search cscope and provide output similar enough to grep for M-x grep, :grep, etc.
#!/bin/bash
if [[ ! -f cscope.out ]]; then
print -u2 "No cscope.out"
exit 1
fi
# Common to all versions
modes[0]="symbol"; SYMBOL=0
modes[1]="definition of"; DEFINITION=1
modes[2]="functions called by"; CALLED=2
modes[3]="functions calling"; CALLING=3
DBVERSION=$(nawk '{print $2; exit}' cscope.out)
# If the file version is '13' we're looking at a cscope-fast file, if it's greater it's cscope
if (( $DBVERSION < 13 )); then
CSCOPE=/opt/SUNWspro/bin/cscope
modes[7]="file"; FILE=7
modes[8]="files including"; INCLUDING=8
elif (( $DBVERSION == 13 )); then
CSCOPE=/opt/onbld/bin/$(mach)/cscope-fast
CSOPTS="-q"
modes[4]="assignments to"; ASSIGNMENT=4
modes[7]="file"; FILE=7
modes[8]="files including"; INCLUDING=8
else
CSCOPE=/usr/bin/cscope
CSOPTS="-q"
modes[7]="file"; FILE=7
modes[8]="files including"; INCLUDING=8
modes[9]="assignments to"; ASSIGNMENT=9
fi
MODE=$SYMBOL # Symbol search
while getopts "dCcafi" opt; do
case $opt in
d) MODE=$DEFINITION;;
c) MODE=$CALLED;;
C) MODE=$CALLING;;
a) MODE=$ASSIGNMENT;;
f) MODE=$FILE;;
i) MODE=$INCLUDING;;
esac
done
shift $(($OPTIND - 1))
if [[ -z $MODE ]]; then
print -u2 "not supported by this cscope"
exit 1
fi
echo "Searching for ${modes[MODE]} $1" >&2
$CSCOPE $CSOPTS -d -L${MODE} "$1" | nawk '
{ m=$0;
sub("[^ ]+ [^ ]+ [^ ]+ ", "", m);
printf("%s:%d: (%s) %s\n", $1, $3, $2, m);
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment