-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.java
117 lines (107 loc) · 3.46 KB
/
Parser.java
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.company;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
// Interface for parser
public class Parser {
ArrayList<ArrayList<String>> args;
ArrayList<String> cmd;
Terminal terminal = null;
public Parser() throws IOException {
this.args = new ArrayList<>();
terminal = new Terminal();
}
public boolean parse(ArrayList<String> input) throws IOException {
if(input.contains(">") || input.contains(">>") || input.contains("cat")){
if(input.get(0).equals("cat")){
if (checkArgSize(2,input)){
terminal.cat(input);
}
}
else if(input.get(1).equals(">")){
terminal.outputOperator(true, input.get(0), null, input.get(2));
}
else if(input.get(1).equals(">>")){
terminal.outputOperator(false, input.get(0), null, input.get(2));
}
}
else if(input.get(0).equals("clear")){
terminal.clear();
}
else if(input.get(0).equals("cd")){
if(checkArgSize(2, input)){
terminal.cd(input.get(1));
}
}
else if(input.get(0).equals("ls")){
terminal.ls();
}
else if(input.get(0).equals("cp")){
if(checkArgSize(3, input)){
terminal.cp(input.get(1), input.get(2));
}
}
else if(input.get(0).equals("mv")){
if(checkArgSize(3, input)) {
terminal.mv(input.get(1), input.get(2));
}
}
else if(input.get(0).equals("rm")){
if(checkArgSize(2,input)){
terminal.rm(input.get(1));
}
}
else if(input.get(0).equals("mkdir")){
if(checkArgSize(2,input)) {
terminal.mkdir(input.get(1));
}
}
else if(input.get(0).equals("rmdir")){
if(checkArgSize(2,input)) {
String filename = input.get(1);
filename = terminal.removeQuotes(filename);
filename = terminal.getPath(filename);
File file = new File(filename);
terminal.rmdir(file);
}
}
else if(input.get(0).equals("more")){
if (checkArgSize(2,input)) {
terminal.more(input.get(1));
}
}
else if(input.get(0).equals("pwd")){
terminal.pwd();
}
else if(input.get(0).equals("date")){
terminal.date();
}
else if(input.get(0).equals("help")){
terminal.help();
}
else if(input.get(0).equals("args")){
if (checkArgSize(2,input)) {
terminal.getArgs(input.get(1));
}
}
else if(input.get(0).equals("exit")){
System.exit(0);
}
else {
System.out.println("Command doesn't exist");
}
return true;
}
public boolean checkArgSize(int size, ArrayList<String> command){
return command.size() >= size;
}
public ArrayList<ArrayList<String>> getArgs() {
return args;
}
public ArrayList<String> getCmd() {
return cmd;
}
public String currentWorkingDirectory(){
return terminal.getCurrentWorkingDirectory();
}
}