-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourceCode-CLI.java
More file actions
363 lines (328 loc) · 12 KB
/
SourceCode-CLI.java
File metadata and controls
363 lines (328 loc) · 12 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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import java.io.*;
import java.nio.file.*;
import java.util.*;
import static java.nio.file.Files.lines;
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
class Parser {
public List<String> hist = new ArrayList<>();
String commandName = ""; // Initialize with a default value
String[] args;
// Method to parse the input command
public boolean parse(String input) {
String[] arrOfStr = input.split(" ");
// Check if there are more than one element in the input
if (arrOfStr.length > 1) {
// Check if the command is "ls -r" or "cp -r"
if ((arrOfStr[0].equals("ls") && arrOfStr[1].equals("-r")) || (arrOfStr[0].equals("cp") && arrOfStr[1].equals("-r"))) {
// Set the commandName to the command name and arguments combined
commandName = arrOfStr[0] + " " + arrOfStr[1];
} else {
// Set the commandName to the first element
commandName = arrOfStr[0];
}
// Initialize the args array with a size equal to the length of arrOfStr minus one
args = new String[arrOfStr.length - 1];
// Copy the arguments from arrOfStr into args
System.arraycopy(arrOfStr, 1, args, 0, args.length);
} else {
// If there's only one element, set it as the command name
commandName = arrOfStr[0];
// Initialize the args array with zero elements
args = new String[0]; // No arguments
}
return true; // Return true to indicate successful parsing
}
// Method to get the parsed command name
public String getCommandName() {
return commandName;
}
// Method to get the parsed arguments
public String[] getArgs() {
return args;
}
}
class Terminal {
Parser parser;
String CurrentDirectory;
public Terminal() {
parser = new Parser();
CurrentDirectory = System.getProperty("user.dir");
}
// Implement each command in a method, for example:
public String pwd() {
// Get the current working directory and return it as a string
return CurrentDirectory;
}
public void echo() {
String OutPut = String.join(" ", parser.getArgs());
System.out.println(OutPut);
}
public void cd(String[] aftercd) {
if (aftercd.length == 0) {
String home = System.getProperty("user.home");
CurrentDirectory = home;
System.setProperty("user.dir", CurrentDirectory);
} else if (aftercd[0].equals("..")) {
File current = new File(CurrentDirectory);
File parentDirectory = current.getParentFile();
if (parentDirectory != null) {
CurrentDirectory = parentDirectory.getAbsolutePath();
System.setProperty("user.dir", CurrentDirectory);
}
} else {
File absolute = new File(aftercd[0]);
if (!absolute.isAbsolute()) {
absolute = new File(CurrentDirectory, aftercd[0]);
}
if (absolute.exists() && absolute.isDirectory()) {
CurrentDirectory = absolute.getAbsolutePath();
System.setProperty("user.dir", CurrentDirectory);
}
}
}
public void ls(String[] args) {
String[] files;
File folder = new File(CurrentDirectory);
files = folder.list();
if (files != null) {
if (args.length > 0 && args[0].equals("-r")) {
Arrays.sort(files, Collections.reverseOrder());
for (String file : files) {
System.out.println(file);
}
} else if (args.length == 0){
Arrays.sort(files);
for (String file : files) {
System.out.println(file);
}
}
else {
System.out.println("Wrong command");
}
}
}
public void touch(String[] args) {
if (args.length != 1) {
System.out.println("error. This function only accepts one argument");
return;
}
Path filePath = Paths.get(CurrentDirectory, args[0]);
try {
Files.createFile(filePath);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void mkdir(String[] args) {
for (String arg : args) {
File dir = new File(arg);
if (!dir.isAbsolute()) {
dir = new File(CurrentDirectory, arg);
}
if (dir.exists()) {
System.out.println("Directory " + arg + " already exists");
} else {
boolean done = dir.mkdir();
if (done) {
System.out.println("Directory " + arg + " successfully created");
} else {
System.out.println("Failed to create " + arg + " directory");
}
}
}
}
public void history() {
int i=0;
for (String s : parser.hist) {
System.out.println((i + 1) + " " + s);
i++;
}
}
public void rmdir(String[] arg) {
if (arg.length != 1) {
System.out.println("error. This function only accepts one argument");
return;
}
if (arg[0].equals("*")) {
File dir = new File(CurrentDirectory);
if (dir.listFiles().length == 0) {
System.out.println("There are no directories to delete");
return;
}
for (File temp : dir.listFiles()) {
if (temp.isDirectory()) {
temp.delete();
}
}
System.out.println("The directories were deleted successfully");
} else {
File dir = new File(arg[0]);
if (!dir.isAbsolute()) {
dir = new File(CurrentDirectory, arg[0]);
}
if (dir.exists() && dir.isDirectory() && dir.delete()) {
System.out.println("The directory was deleted successfully");
} else {
System.out.println("Error. Failed to delete the directory");
}
}
}
public void rm(String[] filename) {
if (filename.length != 1) {
System.out.println("error. This function only accepts one argument");
return;
}
Path filePath = Paths.get(CurrentDirectory, filename[0]);
if (Files.exists(filePath)) {
try {
Files.delete(filePath);
System.out.println("File deleted successfully.");
} catch (IOException e) {
System.out.println("Error deleting the file: " + e.getMessage());
}
} else {
System.out.println("File isn't available in the current directory");
}
}
public void cat(String... filenames) {
for (String filename : filenames) {
Path filePath = Paths.get(CurrentDirectory, filename);
try {
lines(filePath).forEach(System.out::println);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
// This command "cp" is supposed to copy the content of the first file into the second file
public void cp(String[] args) {
// Condition to ensure 2 arguments only
if (args.length != 2) {
System.out.println("Command Format: cp <file1.txt> <file2.txt>");
return;
}
// Reserves places for the 2 files in the args array
String file1 = args[0];
String file2 = args[1];
// Using Path to provide the full path of the current directory by creating an object
Path file1path = Paths.get(CurrentDirectory, file1);
Path file2path = Paths.get(CurrentDirectory, file2);
// A condition to ensure displaying an error if the file doesn't exist
if (!Files.exists(file1path)) {
System.out.println("The file to be copied does not exist!");
return;
}
if (!Files.exists(file2path)) {
System.out.println("The file to be copied to (destination) does not exist");
return;
}
// We will copy the content of the files through the paths with a try and catch to handle any errors
try {
Files.copy(file1path, file2path, StandardCopyOption.REPLACE_EXISTING);
System.out.println("File is copied successfully!");
} catch (IOException e) {
System.err.println("Error while the file was being copied: " + e.getMessage());
}
}
// This method is used to count words, lines, and characters in a file input
public void wc(String[] args) {
// A condition to check there is only one input
if (args.length != 1) {
System.out.println("Command format: wc <file.txt>");
return;
}
String file1 = args[0];
Path file1Path = Paths.get(CurrentDirectory, file1);
// To check if the file does not exist
if (!Files.exists(file1Path)) {
System.out.println("This file does not exist!");
return;
}
int wordCount = 0;
int lineCount = 0;
int charCount = 0;
// Using BufferedReader to read the file line by line
try (BufferedReader reader = Files.newBufferedReader(file1Path)) {
String line;
while ((line = reader.readLine()) != null) {
lineCount++;
charCount += line.length();
String[] words = line.split("\\s+");
wordCount += words.length;
}
System.out.printf("%6d %6d %6d %s%n", lineCount, wordCount, charCount, file1);
} catch (IOException e) {
System.err.println("An error occurred while reading the file: " + e.getMessage());
}
}
// This method will choose the suitable command method to be called
public void chooseCommandAction() {
parser.hist.add(parser.getCommandName());
String[] commandParts = parser.getCommandName().split(" ");
String actualCommand = commandParts[0];
switch (parser.getCommandName()) {
case "pwd":
System.out.println(pwd());
break;
case "cd":
cd(parser.getArgs());
break;
case "ls":
ls(parser.getArgs());
break;
case "ls -r":
ls(parser.getArgs());
break;
case "mkdir":
mkdir(parser.getArgs());
break;
case "rmdir":
rmdir(parser.getArgs());
break;
case "echo":
echo();
break;
case "touch":
touch(parser.getArgs());
break;
case "rm":
rm(parser.getArgs());
break;
case "cat":
cat(parser.getArgs());
break;
case "cp":
cp(parser.getArgs());
break;
case "wc":
wc(parser.getArgs());
break;
case "history":
history();
break;
default:
System.out.println("Enter a valid command.");
break;
}
}
public void exit() {
System.exit(0);
}
public static void main(String[] args) {
Terminal terminal = new Terminal();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Enter a command: ");
String userInput = scanner.nextLine();
if (userInput.equals("exit")) {
break;
}
// Process the user input
terminal.parser.parse(userInput);
terminal.chooseCommandAction();
}
}
}