-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainClass1.java
More file actions
157 lines (144 loc) · 5.9 KB
/
MainClass1.java
File metadata and controls
157 lines (144 loc) · 5.9 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
package terminal;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Date;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.Vector;
import java.util.regex.Pattern;
import static terminal.Terminal.currentFile;
import static terminal.Terminal.defaultDirectory;
public class MainClass1 {
//public static File defaultDirectory , currentFile ;
public static void main(String[] args) throws FileNotFoundException {
currentFile=new File("D:\\");//current dir
defaultDirectory =new File(System.getProperty("user.dir")) ;
File file;
Parser p = new Parser();
while (true){
Scanner in = new Scanner(System.in);
String command = in.nextLine();
String [] words = command.split(Pattern.quote("|"));
Vector<String> parts=new Vector<>();
for(int y=0;y<words.length;y++){
words[y]=words[y].trim();
if(words[y].length() > 1){
parts.add(words[y]);
}
}
/* for (String str:words){
// str=str.trim();
parts.add(str);
}*/
/*for (int o=0;o<parts.size()-1;o++){
System.out.println("---"+parts.get(o));
}*/
for(int i=0;i<parts.size();i++){
String sub_command=parts.get(i);
boolean x;
x = p.parse(sub_command);
if (x == true && p.getArguments().length == 1 && p.getCmd().equals("cd")) {
Terminal t = new Terminal();
String arg = p.getArguments()[0];
if (arg.equals("..")) {
t.cd();
// break;
}
else {
file = new File(arg);
if (file.isDirectory()) {
t.cd(arg);
}
else{
t.cd(arg);
}
}
}else if (x == true && p.getCmd().equals("cp")) {
Terminal t = new Terminal();
t.cp(p.getArguments()[0], p.getArguments()[1]);
} else if (x == true && p.getCmd().equals("mv")) {
Terminal t = new Terminal();
t.mv(p.getArguments()[0], p.getArguments()[1]);
} else if (x == true && p.getCmd().equals("rm")) {
Terminal t = new Terminal();
t.rm(p.getArguments()[0]);
} else if (x == true && p.getCmd().equals("mkdir")) {
Terminal t = new Terminal();
t.mkdir(p.getArguments()[0]);
} else if (x == true && p.getCmd().equals("rmdir")) {
Terminal t = new Terminal();
t.rmdir(p.getArguments()[0]);
} else if (x == true && p.getCmd().equals("pwd")) {
Terminal t = new Terminal();
if (p.getArguments().length == 2) {
try {
boolean flag = false;
if (p.getArguments()[0].equals(">>")) {
flag = true;
}
PrintWriter out = new PrintWriter(new FileOutputStream(new File(p.getArguments()[1]), flag));
out.println(t.pwd());
out.close();
System.out.println("Done");
} catch (Exception e) {
System.out.println(e.getMessage());
}
} else {
System.out.println(t.pwd());
}
} else if (x == true && p.getCmd().equals("clear")) {
Terminal t = new Terminal();
t.clear();
} else if ((x == true && p.getCmd().equals("ls"))) {
Terminal t = new Terminal();
if (p.getArguments().length == 2) {
boolean flag = false;
if (p.getArguments()[0].equals(">>")) {
flag = true;
}else if (p.getArguments()[0].equals(">")){
flag = false;
}
// t.ls(p.getArguments()[1], flag);
t.ls(p.getArguments()[1],flag);
System.out.println("Done");
} else {
t.ls();
}
} else if (x == true && p.getCmd().equals("help")) {
Terminal t = new Terminal();
t.help();
}
else if(x == true && p.getCmd().equals("date")){
Terminal t=new Terminal();
t.date();
}
else if(x==true && p.getCmd().equals("more")){
Terminal t =new Terminal ();
t.more(p.getArguments()[0]);
}
else if(x==true &&p.getCmd().equals("cat")){
Terminal t=new Terminal();
if(p.getArguments().length == 1){
t.cat(p.getArguments()[0]);
}
// boolean flag = false;
// if (p.getArguments()[2].equals(">>")) flag = true;
// t.cat(parts.get(1), parts.get(2), parts.get(4), flag);
//System.out.println("Done");
// }
else{
t.cat(p.getArguments());
}
}else if(x == true && p.getCmd().equals("args")){
Terminal t=new Terminal();
t.args(p.getArguments()[0]);
}
// p.parse(command);
// TODO code application logic here
}
}
}}