com.darwinsys.regex.JGrep Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of darwinsys-api Show documentation
Show all versions of darwinsys-api Show documentation
Ian Darwin's assorted Java stuff,
assembled as an API.
package com.darwinsys.regex;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import com.darwinsys.lang.GetOpt;
// BEGIN main
/** A command-line grep-like program. Accepts some command-line options,
* and takes a pattern and a list of text files.
* N.B. The current implementation of GetOpt does not allow combining short
* arguments, so put spaces e.g., "JGrep -l -r -i pattern file..." is OK, but
* "JGrep -lri pattern file..." will fail. Getopt will hopefully be fixed soon.
*/
public class JGrep {
private static final String USAGE =
"Usage: JGrep pattern [-chilrsnv][-f pattfile][filename...]";
/** The pattern we're looking for */
protected Pattern pattern;
/** The matcher for this pattern */
protected Matcher matcher;
private boolean debug;
/** Are we to only count lines, instead of printing? */
protected static boolean countOnly = false;
/** Are we to ignore case? */
protected static boolean ignoreCase = false;
/** Are we to suppress printing of filenames? */
protected static boolean dontPrintFileName = false;
/** Are we to only list names of files that match? */
protected static boolean listOnly = false;
/** are we to print line numbers? */
protected static boolean numbered = false;
/** Are we to be silent about errors? */
protected static boolean silent = false;
/** are we to print only lines that DONT match? */
protected static boolean inVert = false;
/** Are we to process arguments recursively if directories? */
protected static boolean recursive = false;
/** Construct a Grep object for the pattern, and run it
* on all input files listed in argv.
* Be aware that a few of the command-line options are not
* acted upon in this version - left as an exercise for the reader!
*/
public static void main(String[] argv) {
if (argv.length < 1) {
System.err.println(USAGE);
System.exit(1);
}
String patt = null;
GetOpt go = new GetOpt("cf:hilnrRsv");
char c;
while ((c = go.getopt(argv)) != 0) {
switch(c) {
case 'c':
countOnly = true;
break;
case 'f': /* External file contains the pattern */
try (BufferedReader b =
new BufferedReader(new FileReader(go.optarg()))) {
patt = b.readLine();
} catch (IOException e) {
System.err.println(
"Can't read pattern file " + go.optarg());
System.exit(1);
}
break;
case 'h':
dontPrintFileName = true;
break;
case 'i':
ignoreCase = true;
break;
case 'l':
listOnly = true;
break;
case 'n':
numbered = true;
break;
case 'r':
case 'R':
recursive = true;
break;
case 's':
silent = true;
break;
case 'v':
inVert = true;
break;
case '?':
System.err.println("Getopts was not happy!");
System.err.println(USAGE);
break;
}
}
int ix = go.getOptInd();
if (patt == null)
patt = argv[ix++];
JGrep prog = null;
try {
prog = new JGrep(patt);
} catch (PatternSyntaxException ex) {
System.err.println("RE Syntax error in " + patt);
return;
}
if (argv.length == ix) {
dontPrintFileName = true; // Don't print filenames if stdin
if (recursive) {
System.err.println("Warning: recursive search of stdin!");
}
prog.process(new InputStreamReader(System.in), null);
} else {
if (!dontPrintFileName)
dontPrintFileName = ix == argv.length - 1; // Nor if only one file.
if (recursive)
dontPrintFileName = false; // unless a directory!
for (int i=ix; i