-
Notifications
You must be signed in to change notification settings - Fork 0
/
cppfind
executable file
·126 lines (116 loc) · 4.24 KB
/
cppfind
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/sh
#extended recursive search for documents - c++ files (cpp,h)
#web: http://safrm.net/projects/efind
#author: Miroslav Safr <miroslav.safr@gmail.com>
VERSION=NA
VERSION_DATE=NA
#support color escape characters on different terminals
alias echoc="$(which echo) -e"
BASENAME=`basename $0`
usage() {
echo "$BASENAME $VERSION - extended recursive search for c++ files (cpp,h)"
echo " http://safrm.net/projects/efind/"
echo "Usage:$BASENAME [OPTIONS]... PATTERN [SEARCHING_DIR]"
echo " PATTERN ............................ string (requires \"\" if contains spaces) "
echo " options: "
echo " -h --help .......................... shows command line help"
echo " -V, --version ..................... prints version of scripts"
echo " -cs --case-sensitive ............... case sensitive content search (def: insensitive)"
echo " -nf --no-filenames ................. disable searching in filenames (def: enabled)"
echo " -nc --no-content ................... disable searching in file content (def: enabled)"
echo " -no --name-only .................... print only filenames (def: disabled)"
echo " -ext --extension <ext> ........... choose extension (def: h,hpp,cpp,cxx,c,cc)"
echo " "
}
START_TIME=`date +'%s'`
CASE_SENS=0
TOTAL_COUNT=0
FOUND_COUNT=0
FOUND_COUNT_FN=0
SEARCH_FILENAMES=1
SEARCH_CONTENT=1
FILENAME_ONLY=0
EXT="h\|hpp\|cpp\|cxx\|cc|c"
while [ $# -gt 0 ]; do
case "$1" in
-h|--help) usage ; exit 0 ;;
-V|--version) echo "`basename $0` $VERSION $VERSION_DATE" ; exit 0 ;;
-cs|--case-sensitive) CASE_SENS=1 ;;
-nf|--no-filenames) SEARCH_FILENAMES=0 ;;
-nc|--no-content) SEARCH_CONTENT=0 ;;
-no|--name-only) FILENAME_ONLY=1 ;;
-ext|--extension) shift ; EXT="$1" ;;
*) if [ -z "$WHAT" ]; then
WHAT="$1"
elif [ -z "$SEARCH_DIR" ]; then
SEARCH_DIR="$1"
else
echo "There can be only 2 arguments without <WHAT> <WHERE>, exiting.." ; exit 1
fi
;;
esac
shift
done
EXT_RE=$(printf '\.\(%s\)$' $EXT)
#echo "$EXT_RE"
if [ -z "$SEARCH_DIR" ] || [ ! -e "$SEARCH_DIR" ]; then
SEARCH_DIR="./"
fi
#check if terminal supports colors
COLORS=
tput setaf 1>/dev/null 2>&1 && COLORS=" --color=always "
#case sensitive flag - by default ignore cases
CSF="-i "
if [ $CASE_SENS -eq 1 ]; then
CSF=""
fi
FIND_WHOLENAME_FLAG="-wholename"
find -wholename something --version > /dev/null 2>&1
[ $? -ne 0 ] && FIND_WHOLENAME_FLAG="-path"
FILES=$(find $SEARCH_DIR -type f -not $FIND_WHOLENAME_FLAG "*\/.git*" | grep -i "$EXT_RE")
FILES_COUNT=$(echo "$FILES" | grep -v '^$' | wc -l)
echoc "searching '\033[32m$WHAT\033[0m' in \033[01;34m$SEARCH_DIR\033[0m \033[46m$FILES_COUNT $EXT\033[0m files CS:$CASE_SENS FS:$SEARCH_FILENAMES"
#filenames search
FIFO=${TMPDIR:-/tmp}/FOUND_COUNT_FN.$$
trap "rm -f $FIFO" 0
mkfifo $FIFO 2> /dev/null
if [ $SEARCH_FILENAMES -eq 1 ]; then
echo "$FILES" | \
{ while IFS='\n' read -r FILE ; do
# only file basename
RESULT=$(echo "$FILE" | sed 's|.*/||g' | grep $COLORS $CSF -E "$WHAT")
if [ -n "$RESULT" ]; then
if [ $FILENAME_ONLY -eq 1 ]; then
echoc "\033[01;34m$FILE\033[0m"
else
echoc "\033[01;34m$FILE\033[0m: $RESULT"
fi
FOUND_COUNT_FN=$(($FOUND_COUNT_FN + 1))
fi
done
echo "$FOUND_COUNT_FN" > $FIFO &
}
#pass the value back to script
if [ -e $FIFO ]; then
read FOUND_COUNT_FN < $FIFO
wait $!
fi
fi
#content search
if [ $SEARCH_CONTENT -eq 1 ]; then
echo "$FILES" | \
{ while IFS='\n' read -r FILE ; do
RESULT=$(cat "$FILE" 2> /dev/null | grep $COLORS $CSF -E "$WHAT")
if [ -n "$RESULT" ]; then
if [ $FILENAME_ONLY -eq 1 ]; then
echoc "\033[01;34m$FILE\033[0m"
else
echoc "\033[01;34m$FILE\033[0m: $RESULT"
fi
FOUND_COUNT=$(($FOUND_COUNT + 1))
fi
done
ELAPSED_TIME=$((`date +'%s'` - $START_TIME))
echoc "$BASENAME found \033[42m$FOUND_COUNT_FN/$FOUND_COUNT from $FILES_COUNT \033[0m files, took:$(($ELAPSED_TIME/60)) min $(($ELAPSED_TIME%60)) s"
}
fi