You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jd...@apache.org on 2008/10/04 11:11:33 UTC
svn commit: r701619 - in
/geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main:
java/org/apache/geronimo/gshell/commands/vfs/GrepAction.java
resources/org/apache/geronimo/gshell/commands/vfs/GrepAction.properties
Author: jdillon
Date: Sat Oct 4 02:11:32 2008
New Revision: 701619
URL: http://svn.apache.org/viewvc?rev=701619&view=rev
Log:
Added -c, -i, -n and -v options to the grep command. Return 0 for match, 1 for no match
Modified:
geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/GrepAction.java
geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/GrepAction.properties
Modified: geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/GrepAction.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/GrepAction.java?rev=701619&r1=701618&r2=701619&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/GrepAction.java (original)
+++ geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/java/org/apache/geronimo/gshell/commands/vfs/GrepAction.java Sat Oct 4 02:11:32 2008
@@ -19,18 +19,24 @@
package org.apache.geronimo.gshell.commands.vfs;
+import org.apache.commons.vfs.FileObject;
+import org.apache.geronimo.gshell.clp.Argument;
+import org.apache.geronimo.gshell.clp.Option;
import org.apache.geronimo.gshell.command.CommandContext;
import org.apache.geronimo.gshell.io.IO;
-import org.apache.geronimo.gshell.clp.Argument;
import org.apache.oro.text.MatchAction;
-import org.apache.oro.text.MatchActionProcessor;
import org.apache.oro.text.MatchActionInfo;
+import org.apache.oro.text.MatchActionProcessor;
import org.apache.oro.text.regex.MalformedPatternException;
-import org.apache.commons.vfs.FileObject;
+import org.apache.oro.text.regex.Perl5Compiler;
+import org.apache.oro.text.regex.Perl5Matcher;
+import org.apache.oro.text.regex.PatternMatcher;
+import org.apache.oro.text.regex.Pattern;
+import org.apache.oro.text.regex.PatternMatcherInput;
+import org.apache.oro.text.regex.MatchResult;
+import org.apache.oro.text.regex.PatternCompiler;
import org.codehaus.plexus.util.IOUtil;
-import java.io.IOException;
-import java.io.FileInputStream;
import java.io.BufferedInputStream;
/**
@@ -41,22 +47,69 @@
public class GrepAction
extends VfsActionSupport
{
+ /** Return value when matches are found. */
+ public static final int FOUND = 0;
+
+ /** Return value when no matches are found. */
+ public static final int NOT_FOUND = 1;
+
+ //
+ // TODO: Add --pattern option (in addition to this argument) to allow patterns to start with "-"
+ //
+
@Argument(index=0, required=true)
private String pattern;
@Argument(index=1, required=true)
private String path;
+ @Option(name="-c", aliases={"--count"})
+ private boolean count;
+
+ @Option(name="-i", aliases={"--ignore-case"})
+ private boolean ignoreCase;
+
+ @Option(name="-n", aliases={"--line-number"})
+ private boolean lineNumbers;
+
+ @Option(name="-v", aliases={"--invert-match"})
+ private boolean invertMatch;
+
+ /** Tracks the number of matches. */
+ private int matches = 0;
+
public Object execute(final CommandContext context) throws Exception {
assert context != null;
final IO io = context.getIo();
- MatchActionProcessor processor = new MatchActionProcessor();
+ PatternCompiler compiler = new Perl5Compiler();
+ InvertableMatcher matcher = new InvertableMatcher(new Perl5Matcher());
+ MatchActionProcessor processor = new MatchActionProcessor(compiler, matcher);
try {
- processor.addAction(pattern, new MatchAction() {
+ int options = Perl5Compiler.DEFAULT_MASK;
+
+ if (ignoreCase) {
+ options = Perl5Compiler.CASE_INSENSITIVE_MASK;
+ }
+
+ processor.addAction(pattern, options, new MatchAction() {
public void processMatch(final MatchActionInfo info) {
- io.info("{}", info.line);
+ matches++;
+
+ // Render output unless --count was configured
+ if (!count) {
+ StringBuilder buff = new StringBuilder();
+
+ if (lineNumbers) {
+ buff.append(info.lineNumber);
+ buff.append(":");
+ }
+
+ buff.append(info.line);
+
+ io.info(buff.toString());
+ }
}
});
}
@@ -75,6 +128,78 @@
closeFile(file);
}
- return Result.SUCCESS;
+ if (count) {
+ io.info("{}", matches);
+ }
+
+ return matches != 0 ? FOUND: NOT_FOUND;
+ }
+
+ /**
+ * Delegating {@link PatternMatcher} which allows the match/contains results to be
+ * inverted based on the {@link GrepAction#invertMatch} field for --invert-match support.
+ */
+ private final class InvertableMatcher
+ implements PatternMatcher
+ {
+ private final PatternMatcher delegate;
+
+ public InvertableMatcher(final PatternMatcher delegate) {
+ this.delegate = delegate;
+ }
+
+ public boolean matchesPrefix(char[] input, Pattern pattern, int offset) {
+ boolean result = delegate.matchesPrefix(input, pattern, offset);
+ return invertMatch ? !result : result;
+ }
+
+ public boolean matchesPrefix(String input, Pattern pattern) {
+ boolean result = delegate.matchesPrefix(input, pattern);
+ return invertMatch ? !result : result;
+ }
+
+ public boolean matchesPrefix(char[] input, Pattern pattern) {
+ boolean result = delegate.matchesPrefix(input, pattern);
+ return invertMatch ? !result : result;
+ }
+
+ public boolean matchesPrefix(PatternMatcherInput input, Pattern pattern) {
+ boolean result = delegate.matchesPrefix(input, pattern);
+ return invertMatch ? !result : result;
+ }
+
+ public boolean matches(String input, Pattern pattern) {
+ boolean result = delegate.matches(input, pattern);
+ return invertMatch ? !result : result;
+ }
+
+ public boolean matches(char[] input, Pattern pattern) {
+ boolean result = delegate.matches(input, pattern);
+ return invertMatch ? !result : result;
+ }
+
+ public boolean matches(PatternMatcherInput input, Pattern pattern) {
+ boolean result = delegate.matches(input, pattern);
+ return invertMatch ? !result : result;
+ }
+
+ public boolean contains(String input, Pattern pattern) {
+ boolean result = delegate.contains(input, pattern);
+ return invertMatch ? !result : result;
+ }
+
+ public boolean contains(char[] input, Pattern pattern) {
+ boolean result = delegate.contains(input, pattern);
+ return invertMatch ? !result : result;
+ }
+
+ public boolean contains(PatternMatcherInput input, Pattern pattern) {
+ boolean result = delegate.contains(input, pattern);
+ return invertMatch ? !result : result;
+ }
+
+ public MatchResult getMatch() {
+ return delegate.getMatch();
+ }
}
}
Modified: geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/GrepAction.properties
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/GrepAction.properties?rev=701619&r1=701618&r2=701619&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/GrepAction.properties (original)
+++ geronimo/gshell/trunk/gshell-commands/gshell-vfs/src/main/resources/org/apache/geronimo/gshell/commands/vfs/GrepAction.properties Sat Oct 4 02:11:32 2008
@@ -31,5 +31,13 @@
command.argument.path=Path of file to search
command.argument.path.token=PATH
+command.option.count=Suppress normal output; instead print a count of matching lines for each input file
+
+command.option.ignoreCase=Ignore case distinctions in both the PATTERN and the input files
+
+command.option.lineNumbers=Prefix each line of output with the line number within its input file
+
+command.option.invertMatch=Invert the sense of matching, to select non-matching lines
+
command.manual=\
TODO: grep manual
\ No newline at end of file