package org.jboss.fresh.shell.commands;
import org.jboss.fresh.io.AutoConvertInputStream;
import org.jboss.fresh.io.BufferObjectReader;
import org.jboss.fresh.io.BufferWriter;
import org.jboss.fresh.io.PrintWriter2;
import org.jboss.fresh.shell.AbstractExecutable;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class GrepExe extends AbstractExecutable {
private static final String VERSION = "$Revision: 1860 $";
boolean useRegExp = false;
boolean ignoreCase = false;
boolean onlyWords = false;
boolean onlyLines = false;
boolean supressMessages = false;
boolean invertMatch = false;
boolean printVersion = false;
boolean onlyCount = false;
protected void printHelp(PrintWriter out) {
out.println("Usage: GrepExe [--help] [-i] [-e] [pattern]");
out.println("Grep reads data from his standard input Buffer.");
out.println("Regexp selection and interpretation:");
out.println(" -E, -J, -P, --regexp The given patern is a Java regular expression");
out.println(" -i, --ignore-case ignore case distinctions");
out.println("Miscellaneous:");
out.println(" -w, --words force PATTERN to match only whole words");
out.println(" -x, --lines force PATTERN to match only whole lines");
out.println(" -s, --no-messages suppress error messages");
out.println(" -v, --invert-match select non-matching lines");
out.println(" -V, --version print version information and exit");
out.println(" -h, --help display this help and exit");
out.println("Output control:");
out.println(" -c, --count only print a count of matching lines");
out.close();
return;
}
protected boolean matches(String line, String word) {
if (useRegExp) {
if (onlyLines) {
if (!word.startsWith("^")) {
word = "^" + word;
}
if (!word.endsWith("$")) {
word = word + "$";
}
}
return (line.matches(word));
} else {
if (ignoreCase) {
line = line.toUpperCase();
}
if (onlyLines) {
return (line.equals(word));
} else if (onlyWords) {
line = " " + line.replaceAll("[\\p{L}\\d]+", " ") + " ";
return (line.indexOf(" " + word + " ") != -1);
} else {
return (line.indexOf(word) != -1);
}
}
}
public void process(String exename, String[] params) throws Exception {
PrintWriter2 out = new PrintWriter2(new BufferWriter(getStdOut()));
String word = new String();
String[] invalids = getInvalidSwitches(args, "cEJPisvVhwx", new String[]{"count", "regexp", "ignore-case", "no-messages", "invert-match", "version", "help", "words", "lines"});
useRegExp = isSwitchActive(args, new String[]{"E", "J", "P", "regexp"});
ignoreCase = isSwitchActive(args, "i", "ignore-case");
onlyWords = isSwitchActive(args, "w", "words");
onlyLines = isSwitchActive(args, "x", "lines");
supressMessages = isSwitchActive(args, "s", "no-messages");
invertMatch = isSwitchActive(args, "v", "invert-match");
printVersion = isSwitchActive(args, "V", "version");
onlyCount = isSwitchActive(args, "c", "count");
if (isSwitchActive(args, "V", "version")) {
out.println("grep (CP2 grep), version: " + VERSION);
out.println("");
return;
}
if ((invalids.length > 0) || helpRequested() || (params.length == 0)) {
if (canThrowEx()) {
if (invalids.length > 0) {
StringBuffer sb = new StringBuffer("Unknown or invalid switch(es):");
for (int i = 0; i < invalids.length; i++) {
sb.append(" " + invalids[i]);
}
throw new RuntimeException(sb.toString());
}
} else {
if (invalids.length > 0) {
out.println("Unknown or invalid switch:");
for (int i = 0; i < invalids.length; i++) {
out.println("\t" + invalids[i]);
}
out.println();
}
printHelp(out);
}
return;
}
int i = 0;
while (i < params.length) {
if (!params[i].startsWith("-")) {
break;
}
i++;
}
word = params[i];
if (!useRegExp && ignoreCase) {
word = word.toUpperCase();
}
BufferObjectReader oin = new BufferObjectReader(getStdIn());
AutoConvertInputStream acis = new AutoConvertInputStream(oin);
InputStreamReader isr = new InputStreamReader(acis);
java.io.BufferedReader br = new java.io.BufferedReader(isr);
String line = br.readLine();
long count = 0;
while (line != null) {
if (matches(line, word) && !invertMatch) {
if (!onlyCount) {
out.println(line);
} else {
count++;
}
} else if (invertMatch) {
if (!onlyCount) {
out.println(line);
} else {
count++;
}
}
line = br.readLine();
}
if (onlyCount) {
out.println(count);
}
}
}