-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.java
More file actions
172 lines (164 loc) · 5.95 KB
/
Parser.java
File metadata and controls
172 lines (164 loc) · 5.95 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import java.util.ArrayList;
public class Parser{
public enum Command{
NONE, EXIT, COPY, MOVE, CHANGE_DIR, LIST_DIR, CONCATENATE, MORE, CREATE_DIR, DELETE_DIR, DELETE_FILE, PRINT_ARGS, PRINT_DATE, PRINT_HELP, PRINT_WORKING_DIR, CLEAR_SCREEN,
PIPE, OUTPUT_REDIRECT, OUTPUT_REDIRECT_APPEND, NOT_REGISTERED
};
private Command cmd;
private String[] args;
Parser(){
this.cmd = null;
this.args = null;
}
public boolean parse(String input){
/*
* Precendence: (|) (>,>>) (commands)
*/
// Partition command into words
int pipe = -1, oredirect = Integer.MAX_VALUE, aredirect = Integer.MAX_VALUE;
ArrayList<String> parts = new ArrayList<String>();
String current = "";
Character escape = null;
for(int i = 0; i < input.length(); ++i){
// Handle quoted partitions
if(input.charAt(i) == '\'' || input.charAt(i) == '"'){
if(escape != null && input.charAt(i) == escape){
escape = null;
if(!current.equals(""))
parts.add(current);
current = "";
}else if(escape != null){
current += input.charAt(i);
}else{
escape = input.charAt(i);
}
}else if(escape != null){
current += input.charAt(i);
}else{
// Word boundaries are <Space>, "|", ">", ">>"
if(input.charAt(i) == ' '){
if(!current.equals(""))
parts.add(current);
current = "";
}else if(input.charAt(i) == '|'){
pipe = i;
break;
}else if(input.charAt(i) == '>'){
if(i < input.length() - 1 && input.charAt(i+1) == '>')
aredirect = i;
else
oredirect = i;
}else{
current += input.charAt(i);
}
}
}
if(escape != null)
return false;
if(!current.equals(""))
parts.add(current);
current = "";
if(pipe != -1){
String left = input.substring(0, pipe).trim();
String right = input.substring(pipe+1).trim();
if(left.length() == 0 || right.length() == 0){
return false;
}
this.args = new String[2];
this.args[0] = left;
this.args[1] = right;
this.cmd = Command.PIPE;
}else if(oredirect != Integer.MAX_VALUE || aredirect != Integer.MAX_VALUE){
// Precendence left to right
if(oredirect < aredirect){
String left = input.substring(0, oredirect).trim();
String right = input.substring(oredirect+1).trim();
if(left.length() == 0 || right.length() == 0){
return false;
}
this.args = new String[2];
this.args[0] = left;
this.args[1] = right;
this.cmd = Command.OUTPUT_REDIRECT;
}else if(aredirect < oredirect){
String left = input.substring(0, aredirect).trim();
String right = input.substring(aredirect+2).trim();
if(left.length() == 0 || right.length() == 0){
return false;
}
this.args = new String[2];
this.args[0] = left;
this.args[1] = right;
this.cmd = Command.OUTPUT_REDIRECT_APPEND;
}
}else{
if(parts.isEmpty())
parts.add("__NO__COMMAND__");
this.args = new String[parts.size() - 1];
for(int i = 1; i < parts.size(); ++i)
this.args[i-1] = parts.get(i);
switch(parts.get(0)){
case "cp":
this.cmd = Command.COPY;
break;
case "mv":
this.cmd = Command.MOVE;
break;
case "cd":
this.cmd = Command.CHANGE_DIR;
break;
case "ls":
this.cmd = Command.LIST_DIR;
break;
case "cat":
this.cmd = Command.CONCATENATE;
break;
case "more":
this.cmd = Command.MORE;
break;
case "mkdir":
this.cmd = Command.CREATE_DIR;
break;
case "rmdir":
this.cmd = Command.DELETE_DIR;
break;
case "rm":
this.cmd = Command.DELETE_FILE;
break;
case "args":
this.cmd = Command.PRINT_ARGS;
break;
case "date":
this.cmd = Command.PRINT_DATE;
break;
case "help":
this.cmd = Command.PRINT_HELP;
break;
case "pwd":
this.cmd = Command.PRINT_WORKING_DIR;
break;
case "clear":
this.cmd = Command.CLEAR_SCREEN;
break;
case "__NO__COMMAND__":
this.cmd = Command.NONE;
break;
case "exit":
this.cmd = Command.EXIT;
break;
default:
this.cmd = Command.NOT_REGISTERED;
break;
}
}
return true;
}
public Command getCommand(){
if(this.cmd == null)
return Command.NONE;
return this.cmd;
}
public String[] getArguments(){
return this.args;
}
};