/** * Prompt to confirm that the user wants to delete the JShellItem from the file system. If so, * remove it. If -f is supplied, do not prompt and confirm. If the root directory is specified, * its contents but not itself will be subject to deletion. * * @param paths the JShellItem to be removed. */ public void rm(List<String> paths) throws Exception { // Loop through the user inputs. for (String path : paths) { // Initialize the current JShellItem. JShellItem item = getItemAtPath(path, 0); if (item == null) { System.out.printf("%s: does not exist.\n", path); } else { // Keep track of the size of that JShellItem. int size = item.getSize(); if (size > 0) { List<String> recursiveList = recurseOnPath(path, true); rm(recursiveList.subList(0, size + 1)); if (item.getSize() == 0) { rm(recursiveList.subList(size + 1, size + 2)); } } else { if (item.getPath().equals("/")) { return; } // Check the -f option. if (!currentOptions_.equals("f")) { System.out.printf( "Really remove %s from %s? (y/n) ", item.getName(), item.getParentDirectory().getPath()); // Read the user input. ArrayList<String> in = readInput(); // If 'y' if (in.toString().equals("[y]")) { item.getParentDirectory().removeItem(item); // if not 'n', ask again. } else if (!in.toString().equals("[n]")) { List<String> tempStore = new ArrayList<String>(); tempStore.add(path); rm(tempStore); } // If -f is specified. } else { item.getParentDirectory().removeItem(item); } } } } }
/** * Prints the path of the files specified by the paths argument that contain a string that matches * regex, followed by the particular line. * * @param regex is a String regular expression. * @param paths is a List of paths. */ public String grep(String regex, List<String> paths) throws Exception { if (regex.startsWith("\"")) { int i = 0; for (i = 0; i < paths.size() && !regex.endsWith("\""); i++) regex += " " + paths.get(i); regex = regex.substring(1, regex.length() - 1); paths = paths.subList(i, paths.size()); } if (paths.isEmpty()) throw new Exception("Usage: grep [OPTION]... PATTERN [FILE]..."); String results = ""; for (String path : paths) { JShellItem item = getItemAtPath(path, 0); if (item.getSize() > 0 && currentOptions_.equals("R")) { results += grep(regex, recurseOnPath(path, true).subList(0, item.getSize())); } else if (item instanceof File) { String content = ((File) item).getContent(); if (content.contains(regex)) { if (!results.isEmpty()) results += "\n"; results += item.getPath() + ":\n"; for (String line : content.split("\\n")) { if (line.contains(regex)) { results += line + "\n"; } } results = results.substring(0, results.length() - 1); } } else if (item instanceof Directory && !currentOptions_.equals("R")) { results += "Cannot call grep on a directory without -R."; } else { results += ""; } } return results; }