in gshell-commands/gshell-text/src/main/java/org/apache/geronimo/gshell/commands/text/GrepAction.java [84:143]
public Object execute(final CommandContext context) throws Exception {
assert context != null;
final IO io = context.getIo();
PatternCompiler compiler = new Perl5Compiler();
InvertableMatcher matcher = new InvertableMatcher(new Perl5Matcher());
MatchActionProcessor processor = new MatchActionProcessor(compiler, matcher);
try {
int options = Perl5Compiler.DEFAULT_MASK;
if (ignoreCase) {
options = Perl5Compiler.CASE_INSENSITIVE_MASK;
}
processor.addAction(pattern, options, new MatchAction() {
public void processMatch(final MatchActionInfo info) {
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());
}
}
});
}
catch (MalformedPatternException e) {
io.error("Invalid pattern: " + e, e);
return CommandAction.Result.FAILURE;
}
if (path != null) {
FileObject file = resolveFile(context, path);
try {
grep(context, processor, file);
}
finally {
FileObjects.close(file);
}
}
else {
processor.processMatches(context.getIo().inputStream, context.getIo().outputStream);
}
if (count) {
io.info("{}", matches);
}
return matches != 0 ? FOUND: NOT_FOUND;
}