[svn:parrot] r48698 - in trunk: . examples/tools t/examples t/tools tools/util
jkeenan at svn.parrot.org
jkeenan at svn.parrot.org
Sat Aug 28 00:16:15 UTC 2010
Author: jkeenan
Date: Sat Aug 28 00:16:15 2010
New Revision: 48698
URL: https://trac.parrot.org/parrot/changeset/48698
Log:
Per discussion on list (also see TT #677), move pgegrep to examples/tools/ and move related test to t/examples/tools/.
Added:
trunk/examples/tools/pgegrep (props changed)
- copied unchanged from r48697, trunk/tools/util/pgegrep
trunk/t/examples/pgegrep.t (contents, props changed)
- copied, changed from r48697, trunk/t/tools/pgegrep.t
Deleted:
trunk/t/tools/pgegrep.t
trunk/tools/util/pgegrep
Modified:
trunk/MANIFEST
Modified: trunk/MANIFEST
==============================================================================
--- trunk/MANIFEST Fri Aug 27 17:31:30 2010 (r48697)
+++ trunk/MANIFEST Sat Aug 28 00:16:15 2010 (r48698)
@@ -1,7 +1,7 @@
# ex: set ro:
# $Id$
#
-# generated by tools/dev/mk_manifest_and_skip.pl Wed Aug 25 22:58:44 2010 UT
+# generated by tools/dev/mk_manifest_and_skip.pl Sat Aug 28 00:12:06 2010 UT
#
# See below for documentation on the format of this file.
#
@@ -824,6 +824,7 @@
examples/tge/branch/transform.pir [examples]
examples/tools/Makefile [examples]
examples/tools/pbc_checker.cpp [examples]
+examples/tools/pgegrep [examples]
examples/tutorial/00_README.pod [examples]
examples/tutorial/01_temp_var.pir [examples]
examples/tutorial/02_local_var.pir [examples]
@@ -1688,6 +1689,7 @@
t/examples/namespace.t [test]
t/examples/pasm.t [test]
t/examples/past.t [test]
+t/examples/pgegrep.t [test]
t/examples/pir.t [test]
t/examples/pod.t [test]
t/examples/shootout.t [test]
@@ -2081,7 +2083,6 @@
t/tools/pbc_disassemble.t [test]
t/tools/pbc_dump.t [test]
t/tools/pbc_merge.t [test]
-t/tools/pgegrep.t [test]
t/tools/pmc2cutils/01-pmc2cutils.t [test]
t/tools/pmc2cutils/02-find_file.t [test]
t/tools/pmc2cutils/03-dump_vtable.t [test]
@@ -2168,7 +2169,6 @@
tools/util/perlcritic-cage.conf []
tools/util/perlcritic.conf []
tools/util/perltidy.conf []
-tools/util/pgegrep []
tools/util/release.json []
tools/util/templates.json []
tools/util/update_copyright.pl []
Copied: trunk/examples/tools/pgegrep (from r48697, trunk/tools/util/pgegrep)
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ trunk/examples/tools/pgegrep Sat Aug 28 00:16:15 2010 (r48698, copy of r48697, trunk/tools/util/pgegrep)
@@ -0,0 +1,308 @@
+#! parrot
+
+=head1 NAME
+
+pgegrep - A simple grep using PGE for matching
+
+=head1 SYNOPSIS
+
+B<pgegrep> [I<OPTIONS>] B<PATTERN> [I<FILE...>]
+
+=head1 DESCRIPTION
+
+pgegrep aims to be a small and easy to use program in replacement of the
+standard grep utility. Regex support is whatever PGE will allow. It
+searches through files line by line and tests if the given pattern matches.
+
+=head1 OPTIONS
+
+=over 4
+
+=item -v
+
+=item --invert-match
+
+print lines not matching PATTERN
+
+=item -V
+
+=item --version
+
+print the version and exit
+
+=item --help
+
+show this help and exit
+
+=item -r
+
+=item --recursive
+
+recursively descend into directories
+
+=item -L
+
+=item --files-without-matches
+
+print a list of files that do not match PATTERN
+
+=item -l
+
+=item --files-with-matches
+
+print a list of files that do match PATTERN
+
+=item -a
+
+=item --text
+
+treat binary files as text.
+
+This uses a basic heuristic to discover if a file is binary or not. Files are
+read line by line, and it keeps processing "normally" until a control character
+is found, and then stops and goes onto the next file is that line matches.
+
+=item -n
+
+=item --line-number
+
+print the line number for each match
+
+=item -H
+
+=item --with-filename
+
+print the filename for each match
+
+=back
+
+=cut
+
+# Readability improved!
+.include 'hllmacros.pir'
+
+# for getstdin and friends
+.loadlib 'io_ops'
+
+.sub main :main
+ .param pmc argv # the script name, then our options.
+ .local string progname
+ progname = shift argv
+ load_bytecode 'Getopt/Obj.pbc'
+ load_bytecode 'PGE.pbc'
+ .local pmc getopts
+ getopts = new [ 'Getopt';'Obj' ]
+ getopts.'notOptStop'(1)
+ push getopts, 'with-filename|H'
+ push getopts, 'files-with-matches|l'
+ push getopts, 'files-without-matches|L'
+ push getopts, 'line-number|n'
+ push getopts, 'text|a'
+ push getopts, 'recursive|r'
+ push getopts, 'invert-match|v'
+ push getopts, 'version|V'
+ push getopts, 'help'
+ push_eh handler
+ .local pmc opts
+ opts = getopts.'get_options'(argv)
+ $I0 = defined opts['help']
+ .If($I0, {
+ showhelp()
+ })
+ $I0 = defined opts['version']
+ .If($I0, {
+ showversion()
+ })
+
+ .local int argc
+ argc = elements argv
+ .Unless(argc>1, { showhelp() }) # need rule and at least one file
+
+ .local string rule
+ .local pmc p6rule_compile, matchsub
+ rule = shift argv
+ p6rule_compile = compreg 'PGE::Perl6Regex'
+ matchsub = p6rule_compile(rule)
+ .If(null matchsub, { die 'Unable to compile regex' })
+
+ .local int i, filecount
+ .local string filename
+ .local pmc File, OS, files, handle
+ files = new 'ResizableStringArray'
+ files = argv
+ filecount = files
+ # define with-filename if there's more than one file
+ .If(filecount >= 2, { opts['with-filename'] = 1 })
+ $P0 = loadlib 'file'
+ File = new 'File'
+ $P0 = loadlib 'os'
+ OS = new 'OS'
+ # This must be here, or else it'll get filled with junk data we use stdin...
+ i = 0
+
+ .Unless(filecount, {
+ # no args, use stdin
+ stdindashhack:
+ handle = getstdin
+ filename = '(standard input)'
+ goto stdinhack
+ })
+ .For(, i < filecount, inc i, {
+ filename = files[i]
+ .If(filename == '-', {
+ goto stdindashhack
+ })
+ $I1 = File.'is_file'(filename)
+ .IfElse($I1, {
+ # Is a file
+ handle = open filename, 'r'
+ },{
+ # Not a file, hopefully a directory
+ $I1 = File.'is_dir'(filename)
+ $I0 = defined opts['recursive']
+ $I1 &= $I0
+ .Unless($I1, {
+ printerr "pgegrep: '"
+ printerr filename
+ printerr "': Operation not supported.\n"
+ goto nextfor_0
+ })
+ $P0 = OS.'readdir'(filename)
+ .Foreach($S0, $P0, {
+ .If($S0 != '.', {
+ .If($S0 != '..', {
+ $S1 = filename . '/'
+ $S0 = $S1 . $S0
+ $P1 = new 'ResizableStringArray'
+ $P1[0] = $S0
+ $I0 = i + 1
+ splice files, $P1, $I0, 0
+ }) })
+ })
+ filecount = files
+ goto nextfor_0
+ })
+ stdinhack:
+ checkfile(handle, filename, matchsub, opts)
+ close handle
+ nextfor_0:
+ })
+
+ end
+handler:
+ .local pmc exception, pmcmsg
+ .local string message
+ .get_results (exception)
+ pmcmsg = getattribute exception, 'message'
+ pop_eh
+ message = pmcmsg
+ message = "pgegrep: " . message
+ die message
+.end
+
+.sub checkfile
+ .param pmc handle
+ .param string filename
+ .param pmc matchsub
+ .param pmc opts
+
+ .local pmc match
+ .local string line
+ .local int lineno, linelen, matched
+ lineno = 1
+ matched = 0 # Only used for --files-without-matches
+ line = readline handle
+ linelen = length line
+
+ .local pmc p6rule_compile, cntrlchar
+ $S0 = '<+cntrl-[\t\r\n]>'
+ p6rule_compile = compreg 'PGE::Perl6Regex'
+ cntrlchar = p6rule_compile($S0)
+
+ .For(, linelen, {
+ line = readline handle
+ linelen = length line
+ inc lineno
+ }, {
+ match = matchsub(line)
+ $I1 = istrue match
+ match = cntrlchar(line)
+
+ $I2 = istrue match
+ $I0 = defined opts['files-without-matches']
+ .If($I0, {
+ .If($I1, { matched = 1 })
+ goto next
+ })
+ $I0 = defined opts['files-with-matches']
+ $I0 = $I0 && $I1
+ .If($I0, {
+ say filename
+ .return()
+ })
+
+ $I0 = defined opts['invert-match']
+ not $I0
+ $I1 = xor $I1, $I0
+ .Unless($I1, {
+ $I0 = defined opts['text']
+ $I0 = xor $I0, $I2
+ .If($I0, {
+ print 'Binary file '
+ print filename
+ say ' matches'
+ .return()
+ })
+ $I0 = defined opts['with-filename']
+ $I1 = defined opts['recursive']
+ $I0 = $I0 || $I1
+ .If($I0, {
+ print filename
+ print ':'
+ })
+ $I0 = defined opts['line-number']
+ .If($I0, {
+ print lineno
+ print ':'
+ })
+ print line
+ })
+ #---------
+ next:
+ })
+ $I0 = defined opts['files-without-matches']
+ .If($I0, { say filename })
+ .return()
+.end
+
+.sub showhelp
+ print <<'HELP'
+Usage: pgegrep [OPTIONS] PATTERN [FILE...]
+Search for the Perl 6 Rule PATTERN in each file.
+
+ -v --invert-match print lines not matching PATTERN
+ -V --version print the version and exit
+ --help show this help and exit
+ -r --recursive recursively descend into directories
+ -L --files-without-matches print a list of files that do not match PATTERN
+ -l --files-with-matches print a list of files that do match PATTERN
+ -a --text treat binary files as text
+ -n --line-number print the line number for each match
+ -H --with-filename print the filename for each match
+
+HELP
+ end
+.end
+
+.sub showversion
+ print <<'VERSION'
+pgegrep v0.0.1
+VERSION
+ end
+.end
+
+# Local Variables:
+# mode: pir
+# fill-column: 100
+# End:
+# vim: expandtab shiftwidth=4 ft=pir:
Copied and modified: trunk/t/examples/pgegrep.t (from r48697, trunk/t/tools/pgegrep.t)
==============================================================================
--- trunk/t/tools/pgegrep.t Fri Aug 27 17:31:30 2010 (r48697, copy source)
+++ trunk/t/examples/pgegrep.t Sat Aug 28 00:16:15 2010 (r48698)
@@ -8,11 +8,11 @@
=head1 SYNOPSIS
- % prove t/tools/pgegrep.t
+ % prove t/examples/tools/pgegrep.t
=head1 DESCRIPTION
-Tests the features of of the C<pgegrep> utility.
+Tests the features of of the C<pgegrep> utility (see F<examples/pgegrep>).
=cut
@@ -34,7 +34,7 @@
my ($options, $snippet, $desc) = @_;
my $PARROT = ".$PConfig{slash}$PConfig{test_prog}";
- my $pgegrep = File::Spec->catfile( qw{. tools util pgegrep} );
+ my $pgegrep = File::Spec->catfile( qw{. examples tools pgegrep} );
my $out = `$PARROT $pgegrep $options`;
like( $out, $snippet, $desc );
Deleted: trunk/t/tools/pgegrep.t
==============================================================================
--- trunk/t/tools/pgegrep.t Sat Aug 28 00:16:15 2010 (r48697)
+++ /dev/null 00:00:00 1970 (deleted)
@@ -1,111 +0,0 @@
-#! perl
-# Copyright (C) 2009, Parrot Foundation.
-# $Id$
-
-=head1 NAME
-
-t/tools/pgegrep.t - test the script tools/utils/pgegrep
-
-=head1 SYNOPSIS
-
- % prove t/tools/pgegrep.t
-
-=head1 DESCRIPTION
-
-Tests the features of of the C<pgegrep> utility.
-
-=cut
-
-use strict;
-use warnings;
-use lib qw( . lib ../lib ../../lib );
-
-use Fatal qw{open close};
-use Test::More;
-use Parrot::Test tests => 10;
-use Parrot::Config;
-use File::Spec ();
-
-my $testdata = File::Spec->catfile(qw{. t tools testdata });
-my $testdata_escaped = $testdata;
-$testdata_escaped =~ s!\\!\\\\!g;
-
-sub pgegrep_output_like {
- my ($options, $snippet, $desc) = @_;
-
- my $PARROT = ".$PConfig{slash}$PConfig{test_prog}";
- my $pgegrep = File::Spec->catfile( qw{. tools util pgegrep} );
- my $out = `$PARROT $pgegrep $options`;
-
- like( $out, $snippet, $desc );
-
- return;
-}
-
-pgegrep_output_like(
- '-V',
- qr!\Qpgegrep v0.0.1\E!,
- 'pge reports correct version'
-);
-
-pgegrep_output_like(
- "cat $testdata",
- qr!keyboardcat!,
- 'basic sanity of matching a literal'
-);
-
-pgegrep_output_like(
- "-n cat $testdata",
- qr!1:keyboardcat!,
- 'matching a literal with line number'
-);
-
-pgegrep_output_like(
- "--line-number cat $testdata",
- qr!1:keyboardcat!,
- 'matching a literal with line number with long option'
-);
-
-pgegrep_output_like(
- "-H cat $testdata",
- qr!$testdata_escaped:keyboardcat!,
- 'matching a literal with file name'
-);
-
-pgegrep_output_like(
- "--with-filename cat $testdata",
- qr!$testdata_escaped:keyboardcat!,
- 'matching a literal with file name with long option'
-);
-
-
-pgegrep_output_like(
- "-v cat $testdata",
- qr!saxophonegiraffe!,
- 'test inversion of match'
-);
-
-pgegrep_output_like(
- "--invert-match cat $testdata",
- qr!saxophonegiraffe!,
- 'test inversion of match with long option'
-);
-
-pgegrep_output_like(
- "-l cat $testdata",
- qr!$testdata_escaped!,
- 'find files that match'
-);
-
-pgegrep_output_like(
- "--files-with-matches cat $testdata",
- qr!$testdata_escaped!,
- 'find files that match with long option'
-);
-
-# Local Variables:
-# mode: cperl
-# cperl-indent-level: 4
-# fill-column: 100
-# End:
-# vim: expandtab shiftwidth=4:
Deleted: trunk/tools/util/pgegrep
==============================================================================
--- trunk/tools/util/pgegrep Sat Aug 28 00:16:15 2010 (r48697)
+++ /dev/null 00:00:00 1970 (deleted)
@@ -1,308 +0,0 @@
-#! parrot
-
-=head1 NAME
-
-pgegrep - A simple grep using PGE for matching
-
-=head1 SYNOPSIS
-
-B<pgegrep> [I<OPTIONS>] B<PATTERN> [I<FILE...>]
-
-=head1 DESCRIPTION
-
-pgegrep aims to be a small and easy to use program in replacement of the
-standard grep utility. Regex support is whatever PGE will allow. It
-searches through files line by line and tests if the given pattern matches.
-
-=head1 OPTIONS
-
-=over 4
-
-=item -v
-
-=item --invert-match
-
-print lines not matching PATTERN
-
-=item -V
-
-=item --version
-
-print the version and exit
-
-=item --help
-
-show this help and exit
-
-=item -r
-
-=item --recursive
-
-recursively descend into directories
-
-=item -L
-
-=item --files-without-matches
-
-print a list of files that do not match PATTERN
-
-=item -l
-
-=item --files-with-matches
-
-print a list of files that do match PATTERN
-
-=item -a
-
-=item --text
-
-treat binary files as text.
-
-This uses a basic heuristic to discover if a file is binary or not. Files are
-read line by line, and it keeps processing "normally" until a control character
-is found, and then stops and goes onto the next file is that line matches.
-
-=item -n
-
-=item --line-number
-
-print the line number for each match
-
-=item -H
-
-=item --with-filename
-
-print the filename for each match
-
-=back
-
-=cut
-
-# Readability improved!
-.include 'hllmacros.pir'
-
-# for getstdin and friends
-.loadlib 'io_ops'
-
-.sub main :main
- .param pmc argv # the script name, then our options.
- .local string progname
- progname = shift argv
- load_bytecode 'Getopt/Obj.pbc'
- load_bytecode 'PGE.pbc'
- .local pmc getopts
- getopts = new [ 'Getopt';'Obj' ]
- getopts.'notOptStop'(1)
- push getopts, 'with-filename|H'
- push getopts, 'files-with-matches|l'
- push getopts, 'files-without-matches|L'
- push getopts, 'line-number|n'
- push getopts, 'text|a'
- push getopts, 'recursive|r'
- push getopts, 'invert-match|v'
- push getopts, 'version|V'
- push getopts, 'help'
- push_eh handler
- .local pmc opts
- opts = getopts.'get_options'(argv)
- $I0 = defined opts['help']
- .If($I0, {
- showhelp()
- })
- $I0 = defined opts['version']
- .If($I0, {
- showversion()
- })
-
- .local int argc
- argc = elements argv
- .Unless(argc>1, { showhelp() }) # need rule and at least one file
-
- .local string rule
- .local pmc p6rule_compile, matchsub
- rule = shift argv
- p6rule_compile = compreg 'PGE::Perl6Regex'
- matchsub = p6rule_compile(rule)
- .If(null matchsub, { die 'Unable to compile regex' })
-
- .local int i, filecount
- .local string filename
- .local pmc File, OS, files, handle
- files = new 'ResizableStringArray'
- files = argv
- filecount = files
- # define with-filename if there's more than one file
- .If(filecount >= 2, { opts['with-filename'] = 1 })
- $P0 = loadlib 'file'
- File = new 'File'
- $P0 = loadlib 'os'
- OS = new 'OS'
- # This must be here, or else it'll get filled with junk data we use stdin...
- i = 0
-
- .Unless(filecount, {
- # no args, use stdin
- stdindashhack:
- handle = getstdin
- filename = '(standard input)'
- goto stdinhack
- })
- .For(, i < filecount, inc i, {
- filename = files[i]
- .If(filename == '-', {
- goto stdindashhack
- })
- $I1 = File.'is_file'(filename)
- .IfElse($I1, {
- # Is a file
- handle = open filename, 'r'
- },{
- # Not a file, hopefully a directory
- $I1 = File.'is_dir'(filename)
- $I0 = defined opts['recursive']
- $I1 &= $I0
- .Unless($I1, {
- printerr "pgegrep: '"
- printerr filename
- printerr "': Operation not supported.\n"
- goto nextfor_0
- })
- $P0 = OS.'readdir'(filename)
- .Foreach($S0, $P0, {
- .If($S0 != '.', {
- .If($S0 != '..', {
- $S1 = filename . '/'
- $S0 = $S1 . $S0
- $P1 = new 'ResizableStringArray'
- $P1[0] = $S0
- $I0 = i + 1
- splice files, $P1, $I0, 0
- }) })
- })
- filecount = files
- goto nextfor_0
- })
- stdinhack:
- checkfile(handle, filename, matchsub, opts)
- close handle
- nextfor_0:
- })
-
- end
-handler:
- .local pmc exception, pmcmsg
- .local string message
- .get_results (exception)
- pmcmsg = getattribute exception, 'message'
- pop_eh
- message = pmcmsg
- message = "pgegrep: " . message
- die message
-.end
-
-.sub checkfile
- .param pmc handle
- .param string filename
- .param pmc matchsub
- .param pmc opts
-
- .local pmc match
- .local string line
- .local int lineno, linelen, matched
- lineno = 1
- matched = 0 # Only used for --files-without-matches
- line = readline handle
- linelen = length line
-
- .local pmc p6rule_compile, cntrlchar
- $S0 = '<+cntrl-[\t\r\n]>'
- p6rule_compile = compreg 'PGE::Perl6Regex'
- cntrlchar = p6rule_compile($S0)
-
- .For(, linelen, {
- line = readline handle
- linelen = length line
- inc lineno
- }, {
- match = matchsub(line)
- $I1 = istrue match
- match = cntrlchar(line)
-
- $I2 = istrue match
- $I0 = defined opts['files-without-matches']
- .If($I0, {
- .If($I1, { matched = 1 })
- goto next
- })
- $I0 = defined opts['files-with-matches']
- $I0 = $I0 && $I1
- .If($I0, {
- say filename
- .return()
- })
-
- $I0 = defined opts['invert-match']
- not $I0
- $I1 = xor $I1, $I0
- .Unless($I1, {
- $I0 = defined opts['text']
- $I0 = xor $I0, $I2
- .If($I0, {
- print 'Binary file '
- print filename
- say ' matches'
- .return()
- })
- $I0 = defined opts['with-filename']
- $I1 = defined opts['recursive']
- $I0 = $I0 || $I1
- .If($I0, {
- print filename
- print ':'
- })
- $I0 = defined opts['line-number']
- .If($I0, {
- print lineno
- print ':'
- })
- print line
- })
- #---------
- next:
- })
- $I0 = defined opts['files-without-matches']
- .If($I0, { say filename })
- .return()
-.end
-
-.sub showhelp
- print <<'HELP'
-Usage: pgegrep [OPTIONS] PATTERN [FILE...]
-Search for the Perl 6 Rule PATTERN in each file.
-
- -v --invert-match print lines not matching PATTERN
- -V --version print the version and exit
- --help show this help and exit
- -r --recursive recursively descend into directories
- -L --files-without-matches print a list of files that do not match PATTERN
- -l --files-with-matches print a list of files that do match PATTERN
- -a --text treat binary files as text
- -n --line-number print the line number for each match
- -H --with-filename print the filename for each match
-
-HELP
- end
-.end
-
-.sub showversion
- print <<'VERSION'
-pgegrep v0.0.1
-VERSION
- end
-.end
-
-# Local Variables:
-# mode: pir
-# fill-column: 100
-# End:
-# vim: expandtab shiftwidth=4 ft=pir:
More information about the parrot-commits
mailing list