/* * grep.java * * Implementation of the GREP command. */ package commands; import java.io.*; import kernel.*; /** * Searches the files given by the input arguments for a specific pattern. If * there are no files requested or the argument "-" is used, the command finds * the pattern in the standard input. * * @author Jan Horky */ public class grep extends AbstractProcess { /** * Realizes the GREP command. * * @throws AppException * Exception caused by an error in the virtual machines manager. */ public void realization() throws AppException { BufferedReader in = (this.getIn() != null) ? new BufferedReader( new InputStreamReader(this.getIn())) : null; BufferedWriter out = new BufferedWriter(new OutputStreamWriter(this .getOut())); try { if (getArgs().size() == 0) { this.getErrOut().write(("grep: Too few arguments\n").getBytes()); this.getErrOut().flush(); } else { String pattern = getArgs().get(0); if (getArgs().size() == 1) processInput(in, out, pattern, true); else { for (int i = 1; i < getArgs().size(); i++) if (getArgs().get(i).equals("-")) { in = new BufferedReader(new InputStreamReader(((shell) this .getParent()).getStdInPipe().getPipeIn())); processInput(in, out, pattern, true); } else { in = new BufferedReader(openFile(getArgs().get(i))); processInput(in, out, pattern, false); in.close(); } } } } catch (FileNotFoundException fnfe) { try { this.getErrOut().write((fnfe.getMessage() + "\n").getBytes()); this.getErrOut().flush(); } catch (IOException e) { e.printStackTrace(); } fnfe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); throw new AppException("I/O operation error\n" + ioe.getMessage(), ioe); } catch (NullPointerException npe) { npe.printStackTrace(); throw new AppException("Input or output wasn't defined\n" + npe.getMessage(), npe); } catch (Exception e) { e.printStackTrace(); throw new AppException("Unexpected error\n" + e.getMessage(), e); } } private FileReader openFile(String fileName) throws FileNotFoundException { File file = new File(fileName); if (!file.isAbsolute()) fileName = this.getCurrentPath() + System.getProperty("file.separator") + fileName; return new FileReader(fileName); } private void processInput(BufferedReader in, BufferedWriter out, String pattern, boolean stIn) throws IOException { String data; // If we are expecting the data from the standard input ... if (getIn() == null) { enableStdIn(); in = new BufferedReader(new InputStreamReader(((shell) this.getParent()) .getStdInPipe().getPipeIn())); // Read and process the input data until the CTRL+D while ((data = in.readLine()) != null) { if (data.indexOf(pattern) != -1) { out.write(data); out.newLine(); out.flush(); } } } // Otherwise we are reading from a file or pipe ... else while ((data = in.readLine()) != null) if (data.indexOf(pattern) != -1) { out.write(data); out.newLine(); out.flush(); } } /** * Provides full description of the command to be used in the MAN command. * * @return Full description of the command including the syntax. */ public String getHelp() { return "\n\nDescription:\nSearches the input streams for a specified substring and returns the corresponding lines.\n\nUsage:\n" + "grep [input1 [input2 [...]]]\n\n" + "input1, input2, ...\tFiles to be searched or '-' representing the standard input"; } }