summaryrefslogtreecommitdiffstats
path: root/quilt/grep.in
blob: 8b8261dc751308b7278aa9322d32ca2aef8fe549 (plain) (blame)
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
127
128
129
130
131
#! @BASH@

#  This script is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 2 as
#  published by the Free Software Foundation.
#
#  See the COPYING and AUTHORS files for more details.

# Read in library functions
if [ "$(type -t patch_file_name)" != function ]
then
	if ! [ -r @SCRIPTS@/patchfns ]
	then
		echo "Cannot read library @SCRIPTS@/patchfns" >&2
		exit 1
	fi
	. @SCRIPTS@/patchfns
fi

usage()
{
	printf $"Usage: quilt grep [-h|options] {pattern}\n"
	if [ x$1 = x-h ]
	then
		printf $"
Grep through the source files, recursively, skipping patches and quilt
meta-information. If no filename argument is given, the whole source
tree is searched. Please see the grep(1) manual page for options.

-h	Print this help. The grep -h option can be passed after a
	double-dash (--). Search expressions that start with a dash
	can be passed after a second double-dash (-- --).
"

		exit 0
	else
		exit 1
	fi
}

get_options() {
	getopt -o EFGPe:f:iwxzsvVm:bHnhqoaId:D:RrlLcB:ZA:C:Uu		      \
		--long extended-regexp,fixed-strings,basic-regexp,perl-regexp \
		--long regexp:,file:,ignore-case,word-regexp	      \
		--long line-regexp,null-data,no-messages,invert-match,version \
		--long help,mmap,max-count:,byte-offset,line-number	      \
		--long line-buffered,with-filename,no-filename,label:    \
		--long only-matching,quiet,silent,binary-files:,text,	      \
		--long directories:,devices:,recursive,include:,exclude:      \
		--long exclude-from:,files-without-match,files-with-matches   \
		--long count,null,before-context:,after-context:,context:     \
		--long color::,colour::,binary,unix-byte-offsets	      \
		-- "$@"
}

shift_myargs() {
	set -- "${myargs[@]}"
	shift
	myargs=( "$@" )
}

shift_args() {
	while true
	do
		case "${myargs[0]}" in
		--)
			shift_myargs
			return ;;
		-h)
			opt_h=1 ;;
		-e|-f|--regexp|--file)
			has_pattern=1
			args=( "${args[@]}" "${myargs[0]}" )
			shift_myargs ;;
		-m|-d|-D|-B|-A|-C|\
		--max-count|--label|--binary-files|\
		--directories|--devices|--include|--exclude|--exclude-from|\
		--before-context|--after-context|--context|--color|--colour)
			args=( "${args[@]}" "${myargs[0]}" )
			shift_myargs ;;
		esac
		args=( "${args[@]}" "${myargs[0]}" )
		shift_myargs
	done
}

options=$(get_options "$@")
[ $? -ne 0 ] && usage
eval set -- "$options"
myargs=( "$@" )

args=()
opt_h=
has_pattern=
shift_args
[ -n "$opt_h" ] && usage -h
case "${myargs[0]}" in
	-*)
	options=$(get_options "${myargs[@]}")
	[ $? -ne 0 ] && usage
	eval set -- "$options"
	myargs=( "$@" )
	shift_args ;;
esac

if [ -z "$has_pattern" ]
then
	[ ${#myargs[@]} -eq 0 ] && usage
	args=( "${args[@]}" -- "${myargs[0]}" )
	shift_myargs
fi

# Print the filename for each match, unless -h is given. Otherwise, xargs
# may pass a single filename to grep and cause it to omit the file name.
[ -z "$opt_h" ] && opt_H=-H

find "${myargs[@]}" \( \
	-path "./$QUILT_PATCHES/*" -o \
	-path "./$QUILT_PC/*" \) -prune -o \
	-type f -print \
| xargs grep $opt_H "${args[@]}" \
| if [ ${#myargs[@]} -eq 0 ]; then
	@SED@ -e 's,^./,,'
else
	cat
fi

### Local Variables:
### mode: shell-script
### End:
# vim:filetype=sh