-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBashTerminal.java
More file actions
99 lines (91 loc) · 3.5 KB
/
BashTerminal.java
File metadata and controls
99 lines (91 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* A replication of a terminal with linux commands. The directories and files are stored using a tree.
* This class contains the main method which manages I/O of the bash terminal.
*/
import java.util.Scanner;
public class BashTerminal {
static String errorPrint;
/**
* @param args
* @throws IllegalArgumentException when files and directories contain illegal characters
* @throws FullDirectoryException when all the leaves of the directory are full (three leaves)
* @throws NotADirectoryException thrown when one tries to add leaves to a file
* @throws DirectoryNotFoundException thrown when one tries to search for a directory that does not exist
*/
public static void main(String[] args) throws IllegalArgumentException, FullDirectoryException, NotADirectoryException, DirectoryNotFoundException{
DirectoryNode root = new DirectoryNode("root");
DirectoryTree tree = new DirectoryTree(root);
System.out.println("Starting bash terminal.");
Scanner input = new Scanner(System.in);
boolean running = true;
while (running) {
try {
System.out.print("[ghalollari@host]: $");
String answer = input.nextLine();
String[] answerArr = answer.trim().split(" ");
if (answerArr[0].contains("pwd")) {
System.out.println(tree.presentWorkingDirectory());
}
if (answerArr[0].contains("ls") && answerArr.length == 1) {
System.out.println(tree.listDirectory());
}
if (answerArr[0].contains("ls") && answerArr.length > 1) {
tree.printDirectoryTree();
}
if (answerArr[0].contains("find") && answerArr.length > 1) {
String print = tree.findMe(answerArr[1]);
System.out.println(print);
}
if (answerArr[0].contains("mv") && answerArr.length > 2) {
tree.resetCursor();
String array[] = answerArr[1].trim().split("/");
String array2[] = answerArr[2].trim().split("/");
int index = array.length;
int index2 = array2.length;
//System.out.println(array[index - 1]);
//System.out.println(array2[index2-1]);
tree.moveMe(array[index - 1], array2[index2-1], array, array2);
}
if (answerArr[0].contains("cd") && answerArr[1].contains("/") && !answerArr[1].equals("/")) {
tree.resetCursor();
String array[] = answerArr[1].trim().split("/");
errorPrint = answerArr[1];
int index = array.length;
for (int i = 1; i < index; i++) {
//System.out.println(array[i]);
tree.changeDirectory(array[i]);
}
}
if (answerArr[0].contains("cd") && answerArr[1].equals("..")) {
tree.moveToParent();
}
if (answerArr[0].contains("cd") && !answerArr[1].equals("/") && !answerArr[1].equals("..") && !answerArr[1].contains("/")) {
errorPrint = answerArr[1];
tree.changeDirectory(answerArr[1]);
}
if (answerArr[0].contains("cd") && answerArr[1].equals("/")) {
tree.resetCursor();
}
if (answerArr[0].contains("mkdir")) {
tree.makeDirectory(answerArr[1]);
}
if (answerArr[0].contains("touch")) {
tree.makeFile(answerArr[1]);
}
if (answerArr[0].contains("exit")) {
System.out.println("Program terminating normally");
running = false;
}
} catch (FullDirectoryException e) {
System.out.println("ERROR: Present directory is full.");
} catch (NotADirectoryException e) {
System.out.println("EERROR: Cannot change directory into a file.");
} catch (DirectoryNotFoundException e) {
System.out.println("ERROR: No such directory named '" + errorPrint + "'.");
} catch (Exception e) {
System.out.println("ERROR: Illegal entry, try again.");
}
}
input.close();
}
}