-
Notifications
You must be signed in to change notification settings - Fork 1
/
Terminal.java
241 lines (221 loc) · 8.74 KB
/
Terminal.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package com.company;
import javafx.util.Pair;
import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import java.util.Scanner;
/** todo help function for every command - take care of commands with no class
* todo operator | < >
* todo args , rm ,more
*
*/
/// todo : Pipe operator, > , >> , args, paths
public class Terminal {
private String CurrentPath = null;
private String HomePath = "/home/tw3" ; /// todo current home on my machine , if you gonna try it with another machine you have to change it
MyParser terminalParser ;
String[]ListOfCommands = {"cd"};
Terminal() throws IOException {
terminalParser = new MyParser();
CurrentPath = HomePath;
while (true)
PromptForCommands();
}
Terminal(String path) throws IOException {
CurrentPath = path ;
while(true)
PromptForCommands();
}
Terminal(MyParser p) throws IOException {
CurrentPath = HomePath ;
terminalParser = p ;
while(true)
PromptForCommands();
}
private void PromptForCommands() throws IOException {
if(CurrentPath == null || CurrentPath.compareTo(HomePath) ==0)
System.out.print("Lamya'sPC:~$ ");
else
System.out.print("Lamya'sPC:~"+CurrentPath + " $ ");
// runCommand(); /// todo remove
Scanner s = new Scanner(System.in);
String cmd = s.nextLine();
terminalParser.parse(cmd);
while(terminalParser.QueueNotEmpty())
runCommand(terminalParser.currentcmd() , terminalParser.currentArgs() );
}
protected boolean DirectoryExist(String path){ /// todo handle the short path
File tmpFile = new File(path);
return tmpFile.exists() && tmpFile.isDirectory();
}
protected boolean DirectoryExist(Path _path){ ///
return _path.toFile().exists()&& _path.toFile().isDirectory();
}
protected boolean FileExist(String path){
File tmpFile = new File(path);
return tmpFile.exists() && tmpFile.isFile();
}
String toAbsolutePath(String _path){
Path path = Paths.get(_path);
if(!path.toFile().isAbsolute()) {
path = Paths.get(CurrentPath + File.separatorChar + _path);
}
return path.normalize().toAbsolutePath().toString();
}
Pair<Boolean, Object> DirectCommmand(String operator,String[] directionFile ) throws IOException {
if(directionFile.length > 1){
System.out.println("too much argument");
return new Pair<Boolean , Object>(false , null);
}
directionFile[0] = toAbsolutePath(directionFile[0]);
new File(directionFile[0]).createNewFile();
if(FileExist(directionFile[0])) {
PrintStream tp = null;
InputStream ip = null;
if(operator.compareTo(">>") == 0)
tp = new PrintStream(new FileOutputStream(directionFile[0] , true));
else if(operator.compareTo(">") == 0 )
tp = new PrintStream(new FileOutputStream(directionFile[0]));
else {
ip = new FileInputStream(directionFile[0]);
return new Pair <Boolean, Object>(true , ip);
}
// p = tp;
return new Pair<Boolean , Object>(true , tp);
}
else{
System.out.println("Not a file");
return new Pair<Boolean , Object>(false , null);
}
}
public void runCommand(String cmd , String args[]) throws IOException { /// we have < operator but we can't take input because we already parsed the command and it's arguments
// System.out.println(cmd + " " + args[0]);
PrintStream consoleout = System.out;
InputStream consolein = System.in;
InputStream in = null ;
PrintStream p = null ;
if(terminalParser.DirectCommand()) {
String operator = terminalParser.currentcmd();/// redundant to move the queue
String[] directionFile = terminalParser.currentArgs();
Pair <Boolean , Object> pair = DirectCommmand(operator , directionFile);
if(!pair.getKey())
return ;
else if(operator.compareTo("<") == 0){
in = (InputStream)pair.getValue();
}
else
p = (PrintStream) pair.getValue();
}
if(p != null){
System.setOut(p);
}
if( cmd.compareTo("cd") == 0){
cdCommand c = new cdCommand(args, CurrentPath);
CurrentPath = c.changeDirectory();
}
else if( cmd.compareTo("pwd") == 0)
pwd(args);
else if(cmd.compareTo("ls") == 0){
lsCommand l = new lsCommand(args , CurrentPath);
l.run(CurrentPath);
}
else if(cmd.compareTo("cat") == 0){
CatCommand c = new CatCommand(args , CurrentPath);
c.run(CurrentPath);
}
else if(cmd.compareTo("cp") == 0){
cpCommand c =new cpCommand(args , CurrentPath);
c.run(CurrentPath);
}
else if(cmd.compareTo("date") == 0){
Date d = new Date();
System.out.println (d);
}
else if(cmd.compareTo("mkdir") ==0 ){
mkdirCommand c = new mkdirCommand(Paths.get(CurrentPath) ,args );
c.run(CurrentPath);
}
else if(cmd.compareTo("clear") == 0){
clear(args);
}
else if(cmd.compareTo("help") == 0){
help(args);
}
else if(cmd.compareTo("mv") == 0){
mvCommand c = new mvCommand(args, CurrentPath);
c.run(CurrentPath);
}
else if(cmd.compareTo("rm") == 0) {
rmCommand c = new rmCommand(args, CurrentPath);
c.run(CurrentPath);
}
else if(cmd.compareTo("rmdir") == 0) {
rmdirCommand c = new rmdirCommand(args, CurrentPath);
c.run(CurrentPath);
}
else if(cmd.compareTo("more") == 0){
moreCommand c = new moreCommand(args, CurrentPath);
c.run(CurrentPath);
}
else if(cmd.compareTo("exit") == 0){
System.exit(1);
}
else if(cmd.compareTo("args")==0){
if(args.length > 1)
System.out.println("too much arguments");
else if(args.length == 0 )
System.out.println("need arguments");
else {
/// todo
}
}
else {
System.out.println(cmd +" command not found ");
}
System.setOut(consoleout);
System.setIn(consolein);
}
void pwd(String args[]){ /// todo check the parameters size
if(args.length >0){
System.out.println("Too much arguments" );
}
else
System.out.println(CurrentPath);
}
void help(String args[]){
if(args.length >0){
System.out.println("Too much arguments" );
}
else {
System.out.println("\t--cd: \tchange current directory");
System.out.println("\t--ls: \tlist files and directories names");
System.out.println("\t--cp: \tcopy files and directories");
System.out.println("\t--args: \tlist all command arguments");
System.out.println("\t--mv: \tmoves files or directories from one place to another");
System.out.println("\t--rm: \tremove objects such as files and empty directories");
System.out.println("\t--mkdir: \tcreate new directory");
System.out.println("\t--rmdir: \tremove empty directories");
System.out.println("\t--cat: \tcreate file and view its content");
System.out.println("\t--more: \tview output in scrollable manner");
System.out.println("\t--pwd: \tget full system path");
System.out.println("\t--date: \tcurrent system date and time");
System.out.println("\t--help: \tlist all possible commands");
System.out.println("\t-->: \tredirect output to be overwritten in a file");
System.out.println("\t-->>: \tredirect output to be appended to a file");
System.out.println("\t--|: \tredirect output of previous command as an input to another command");
System.out.println("\t--clear: \tclear terminal screen");
System.out.println("\t--exit: \texit (close) terminal");
}
}
void clear(String args []){
if(args.length >0){
System.out.println("Too much arguments" );
}
else
{
for(int i=0; i<20; i++)
System.out.println("\n");
}
}
}