-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathultimate_shell.js
More file actions
83 lines (75 loc) · 2.18 KB
/
ultimate_shell.js
File metadata and controls
83 lines (75 loc) · 2.18 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
import * as std from "std";
import * as os from "os";
import { mount } from "sys_ops";
function printHelp() {
std.printf("Commands: ls, cd, cat, mkdir, mount, exit\n");
}
// Native Cat: Works without /bin/cat
function cat(path) {
const f = std.open(path, "r");
if (!f) {
std.printf("cat: %s: No such file\n", path);
return;
}
let line;
while ((line = f.getline()) !== null) {
std.printf("%s\n", line);
}
f.close();
}
std.printf("--- ULTIMATE LINUX SHELL ---\n");
printHelp();
while (true) {
const cwd = os.getcwd()[0] || "/";
std.printf("[%s] # ", cwd);
std.out.flush();
const line = std.in.getline();
if (line === null) break;
const input = line.trim();
if (!input) continue;
const args = input.split(/\s+/);
const cmd = args[0];
try {
if (cmd === "ls") {
const [files, err] = os.readdir(args[1] || ".");
if (err !== 0) std.printf("ls error: %d\n", err);
else {
files.forEach(f => std.printf("%s ", f));
std.printf("\n");
}
}
else if (cmd === "cd") {
os.chdir(args[1] || "/");
}
else if (cmd === "cat") {
cat(args[1]);
}
else if (cmd === "mount") {
const res = mount(args[1], args[2], args[3] || "ext4");
std.printf("Mount %s -> %s: %s\n", args[1], args[2], res === 0 ? "Success" : "Error " + res);
}
else if (cmd === "mkdir") {
const path = args[1];
if (!path) {
std.printf("usage: mkdir <path>\n");
} else {
// 0o755 is the standard octal for rwxr-xr-x
const res = os.mkdir(path, 0o755);
if (res !== 0) {
std.printf("mkdir: cannot create '%s' (error %d)\n", path, res);
}
}
}
else if (cmd === "help") {
printHelp();
}
else if (cmd === "exit") {
break;
}
else {
std.printf("No idea what to do lol\n");
}
} catch (e) {
std.printf("Shell Error: %s\n", e.toString());
}
}