5 pgegrep - A simple grep using PGE for matching
9 B<pgegrep> [I<OPTIONS>] B<PATTERN> [I<FILE...>]
13 pgegrep aims to be a small and easy to use program in replacement of the
14 standard grep utility. Regex support is whatever PGE will allow. It
15 searches through files line by line and tests if the given pattern matches.
25 print lines not matching PATTERN
31 print the version and exit
35 show this help and exit
41 recursively descend into directories
45 =item --files-without-matches
47 print a list of files that do not match PATTERN
51 =item --files-with-matches
53 print a list of files that do match PATTERN
59 treat binary files as text.
61 This uses a basic heuristic to discover if a file is binary or not. Files are
62 read line by line, and it keeps processing "normally" until a control character
63 is found, and then stops and goes onto the next file is that line matches.
69 print the line number for each match
75 print the filename for each match
81 # Readability improved!
82 .include 'hllmacros.pir'
85 .param pmc argv # the script name, then our options.
86 .local string progname
88 load_bytecode 'Getopt/Obj.pbc'
89 load_bytecode 'PGE.pbc'
91 getopts = new 'Getopt::Obj'
92 getopts.'notOptStop'(1)
93 push getopts, 'with-filename|H'
94 push getopts, 'files-with-matches|l'
95 push getopts, 'files-without-matches|L'
96 push getopts, 'line-number|n'
97 push getopts, 'text|a'
98 push getopts, 'recursive|r'
99 push getopts, 'invert-match|v'
100 push getopts, 'version|V'
104 opts = getopts.'get_options'(argv)
105 $I0 = defined opts['help']
109 $I0 = defined opts['version']
116 .Unless(argc>1, { showhelp() }) # need rule and at least one file
119 .local pmc p6rule_compile, matchsub
121 p6rule_compile = compreg 'PGE::Perl6Regex'
122 matchsub = p6rule_compile(rule)
123 .If(null matchsub, { die 'Unable to compile regex' })
125 .local int i, filecount
126 .local string filename
127 .local pmc File, OS, files, handle
128 files = new 'ResizableStringArray'
131 # define with-filename if there's more than one file
132 .If(filecount >= 2, { opts['with-filename'] = 1 })
135 # This must be here, or else it'll get filled with junk data we use stdin...
142 filename = '(standard input)'
145 .For(, i < filecount, inc i, {
147 .If(filename == '-', {
150 $I1 = File.'is_file'(filename)
153 handle = open filename, 'r'
155 # Not a file, hopefully a directory
156 $I1 = File.'is_dir'(filename)
157 $I0 = defined opts['recursive']
160 printerr "pgegrep: '"
162 printerr "': Operation not supported.\n"
165 $P0 = OS.'readdir'(filename)
171 $P1 = new 'ResizableStringArray'
174 splice files, $P1, $I0, 0
181 checkfile(handle, filename, matchsub, opts)
188 .local pmc exception, pmcmsg
189 .local string message
190 .get_results (exception)
191 pmcmsg = getattribute exception, 'message'
194 message = "pgegrep: " . message
200 .param string filename
206 .local int lineno, linelen, matched
208 matched = 0 # Only used for --files-without-matches
209 line = readline handle
210 linelen = length line
212 .local pmc p6rule_compile, cntrlchar
213 $S0 = '<+cntrl-[\t\r\n]>'
214 p6rule_compile = compreg 'PGE::Perl6Regex'
215 cntrlchar = p6rule_compile($S0)
218 line = readline handle
219 linelen = length line
222 match = matchsub(line)
224 match = cntrlchar(line)
227 $I0 = defined opts['files-without-matches']
229 .If($I1, { matched = 1 })
232 $I0 = defined opts['files-with-matches']
239 $I0 = defined opts['invert-match']
243 $I0 = defined opts['text']
251 $I0 = defined opts['with-filename']
252 $I1 = defined opts['recursive']
258 $I0 = defined opts['line-number']
268 $I0 = defined opts['files-without-matches']
269 .If($I0, { say filename })
275 Usage: pgegrep [OPTIONS] PATTERN [FILE...]
276 Search for the Perl 6 Rule PATTERN in each file.
278 -v --invert-match print lines not matching PATTERN
279 -V --version print the version and exit
280 --help show this help and exit
281 -r --recursive recursively descend into directories
282 -L --files-without-matches print a list of files that do not match PATTERN
283 -l --files-with-matches print a list of files that do match PATTERN
284 -a --text treat binary files as text
285 -n --line-number print the line number for each match
286 -H --with-filename print the filename for each match
303 # vim: expandtab shiftwidth=4 ft=pir: