All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.darwinsys.regex.JGrep Maven / Gradle / Ivy

There is a newer version: 1.8.0
Show newest version
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;

// tag::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 args.
	 * Be aware that a few of the command-line options are not
	 * acted upon in this version - left as an exercise for the reader!
	 * @param args args
	 */
	public static void main(String[] args) {

		if (args.length < 1) {
		    System.err.println(USAGE);
		    System.exit(1);
		}
		String patt = null;

		GetOpt go = new GetOpt("cf:hilnrRsv");

		char c;
		while ((c = go.getopt(args)) != 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 = args[ix++];

		JGrep prog = null;
		try {
			prog = new JGrep(patt);
		} catch (PatternSyntaxException ex) {
			System.err.println("RE Syntax error in " + patt);
			return;
		}

		if (args.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 == args.length - 1; // Nor if only one file.
			if (recursive)
				dontPrintFileName = false;				// unless a directory!

			for (int i=ix; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy