summaryrefslogtreecommitdiff
path: root/Source/Applications/Shell/Shell.ns.cpp
blob: d34c360beea3109573344850d09db264061b818e (plain) (blame)
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
#include "Shell.ns.h"
#include <Binding/Sys.ns.h>

namespace Shell {

FSNode cwd(0);


u32int run() {
	struct {	//Command list
		String name;
		void (*cmd)(Vector<String>&);
	} commands[] = {
		{"ls",		ls},
		{"cd",		cd},
		{"pwd",		pwd},
		{"rm",		rm},
		{"mkdir",	mkdir},
		{"cat",		cat},
		{"wf",		wf},
		{"run",		run},
		{"",		0}
	};

	cwd = FS::cwdNode();
	while (1) {
		outvt << "{" << cwd.getName() << "}: ";
		String s = invt.readLine();
		if (s.empty()) continue;
		while (s[0] == WChar(" ") or s[0] == WChar("\t")) {
			s = s.substr(1, s.size() - 1);
		}
		if (s.empty()) continue;
		if (s[0] == WChar("#")) continue;

		//Parse string
		Vector<String> cmd;
		cmd.push(String());
		bool inQuote = false;
		for (u32int i = 0; i < s.size(); i++) {
			if (s[i] == WChar("'")) {
				inQuote = !inQuote;
			} else if (s[i] == WChar("\\")) {
				i++;
				cmd.back() += s[i];
			} else if (s[i] == WChar(" ") and !inQuote) {
				cmd.push(String());
			} else {
				cmd.back() += s[i];
			}
		}

		//Run command
		if (cmd[0] == "exit") {
			if (cmd.size() == 1) return 0;
			return cmd[1].toInt();
		} else if (cmd[0] == "halt") {
			Sys::halt();
			outvt << "Something went wrong.\n";
		} else if (cmd[0] == "reboot") {
			Sys::reboot();
			outvt << "Something went wrong.\n";
		} else if (cmd[0] == "uptime") {
			outvt << "Uptime : " << (s64int)Sys::uptime() << "s\n";
		} else if (cmd[0] == "free") {
			outvt << "Free RAM : " << (s64int)Sys::freeRam() << " Kio of " << (s64int)Sys::totalRam() << " Kio\n"; 
		} else if (cmd[0] == "help") {
			while (cmd.size() > 1) cmd.pop();
			cmd.push("/Applications/Shell/Help.txt");
			cat(cmd);
		} else {
			u32int i = 0;
			bool found = false;
			while (!commands[i].name.empty()) {
				if (commands[i].name == cmd[0]) {
					found = true;
					if (commands[i].cmd == 0) {
						outvt << "Not implemented yet.\n";
					} else {
						commands[i].cmd(cmd);
					}
					break;
				}
				i++;
			}
			if (!found) outvt << "Unknown command : " << cmd[0] << "\n";
		}
	}
}

}