summaryrefslogtreecommitdiff
path: root/Source/Applications/Shell/Shell-fs.class.cpp
blob: 30faaf766c0989b0b3e5ea45c619367ae7629239 (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
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
#include "Shell.class.h"
#include <TextFile.class.h>
#include <Binding/Process.class.h>

void Shell::ls(Vector<String>& args) {
	FSNode d = cwd;
	if (args.size() == 2) {
		FSNode n = FS::find(args[1], cwd);
		d = FSNode(0);
		if (!n.valid())
			outvt << "No such directory : " << args[1] << "\n";
		else if (n.type() != NT_DIRECTORY)
			outvt << "Not a directory : " << args[1] << "\n";
		else
			d = n;
	}	
	if (d.valid()) outvt << "Contents of directory " << d.path() << " :\n";
	if (!d.valid()) return;
	for (u32int i = 0; i < d.getLength(); i++) {
		FSNode n = d.getChild(i);
		if (!n.valid()) {
			outvt << " [inacessible file]\n";	//This is a file we are not supposed to be able to read
			continue;
		}
		String perm = "rwxrwxrwx";
		u32int p = n.getPerm();
		for (u32int i = 0; i < 9; i++) {
			if (((p >> i) & 1) == 0) perm[8 - i] = "-";
		}
		if (n.type() == NT_FILE) {
			outvt << " FILE " << perm << " " << n.getName() << FLUSH;
			outvt.setCsrCol(30);
			outvt << (s32int)n.getLength() << " bytes.\n";
		} else if (n.type() == NT_DIRECTORY) {
			outvt << " DIR  " << perm << " " << n.getName() << "/" << FLUSH;
			outvt.setCsrCol(30);
			outvt << (s32int)n.getLength() << " items.\n";
		}
	}
}

void Shell::cd(Vector<String>& args) {
	if (args.size() != 2) {
		outvt << "Invalid argument count.\n";
	} else {
		FSNode ncwd = FS::find(args[1], cwd);
		if (!ncwd.valid()) {
			outvt << "No such directory : " << args[1] << "\n";
		} else if (ncwd.type() == NT_DIRECTORY) {
			ncwd.setCwd();
			cwd = ncwd;
		} else {
			outvt << "Not a directory.\n";
		}
	}
}

void Shell::pwd(Vector<String>& args) {
	outvt << "Current directory : " << cwd.path() << "\n";
}

void Shell::rm(Vector<String>& args) {
	if (args.size() == 1) outvt << "No file to remove.\n";
	for (u32int i = 1; i < args.size(); i++) {
		if (!FS::find(args[i], cwd).remove()) {
			outvt << "Error while removing file " << args[i] << "\n";
		}
	}
}

void Shell::mkdir(Vector<String>& args) {
	if (args.size() == 1) outvt << "No directory to create.\n";
	for (u32int i = 1; i < args.size(); i++) {
		if (!FS::mkdir(args[i], cwd).valid()) {
			outvt << "Error while creating directory " << args[i] << "\n";
		}
	}
}

void Shell::cat(Vector<String>& args) {
	if (args.size() == 1) outvt << "Meow.\n";
	for (u32int i = 1; i < args.size(); i++) {
		TextFile f(args[i], FM_READ, cwd);
		if (f.valid() && f.validOpened()) {
			while (!f.eof()) {
				outvt << f.readLine() << "\n";
			}
			f.close();
		} else {
			outvt << "Error while reading from file " << args[i] << "\n";
		}
	}
}

void Shell::wf(Vector<String>& args) {
	if (args.size() == 1) {
		outvt << "No file to write !\n";
	} else {
		TextFile f(args[1], FM_TRUNCATE, cwd);
		if (f.valid() && f.validOpened()) {
			outvt << "Enter contents for file " << args[1] << " and end your entry with <CR>.<CR>\n";
			String t = invt.readLine();
			while (t != ".") {
				f.write(t, true);
				t = invt.readLine();
			}
			f.close();
		} else {
			outvt << "Error opening file.\n";
		}
	}
}

void Shell::run(Vector<String>& args) {
	if (args.size() == 1) {
		outvt << "Nothing to run...\n";
	} else {
		Process p = Process::run(args[1]);
		if (p.valid()) {
			p.setInVT(invt);
			p.setOutVT(outvt);
			for (u32int i = 2; i < args.size(); i++) {
				p.pushArg(args[i]);
			}
			p.start();
			s32int v = p.wait();
			outvt << "Return value : " << (s64int)v << "\n";
		} else {
			outvt << "Error while launching process.\n";
		}
	}
}