-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsh.js
executable file
·266 lines (245 loc) · 8.32 KB
/
jsh.js
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
(function() {
const
FILE_T = 100,
DIR_T = 101,
LINK_T = 102,
READ_P = 4,
WRITE_P = 2,
EXEC_P = 1,
NIL_P = 0;
if (!('isSpace' in String.prototype)) {
String.prototype.isSpace = function() {
return ($.trim(this).length == 0);
};
}
Term = function(fs, env, output, prompt, input) {
this.fs = fs;
if (!('HOME' in env && 'PATH' in env && 'USER' in env &&
'HOSTNAME' in env)) {
// oops
return;
}
this.env = env;
if (!('PWD' in this.env)) {
this.env['PWD'] = this.env['HOME'];
}
this.elts = {
output: output,
prompt: prompt,
input: input
};
var parentize = function(dir) {
for (var subdir in dir.contents) {
if (dir.contents && dir.contents[subdir].type == DIR_T) {
dir.contents[subdir].parent = dir;
parentize(dir.contents[subdir]);
}
}
};
parentize(this.fs);
this.fs.parent = this.fs;
this.elts.prompt.append(document.createTextNode(this.getPrompt()));
this.readline = new Readline(this.elts.input, this, this.doline);
};
Term.prototype.types = Term.types = {
file: FILE_T,
link: LINK_T,
dir: DIR_T
};
Term.prototype.perms = Term.perms = {
read: READ_P,
write: WRITE_P,
exec: EXEC_P,
nil: NIL_P
};
/**
* Gets what would be the current prompt
*/
Term.prototype.getPrompt = function() {
return this.env['USER'] + '@' + this.env['HOSTNAME'] + ' ' +
this.canonpath(this.env['PWD']) + ' $ ';
};
/**
* Useful both for determining if a retval is an error, and for
* something like perror. Maybe I should write perror....
*/
Term.prototype.errnos = {
EACCES: 'Permission denied',
EISDIR: 'Is a directory',
EINVAL: 'Invalid argument',
ENOENT: 'No such file or directory',
ENOTDIR: 'not a directory'
};
Term.prototype.doline = function(text, exec) {
// Print the last prompt and command
this.print(this.getPrompt() + text + '\n');
var retval = false;
// Run
if (exec) {
retval = this.exec.apply(this, text.split(' '));
}
// Print a new prompt
this.elts.prompt.text(this.getPrompt());
return retval;
};
/**
* Actually more like system(3) in its semantics, and more like
* execlp(3) in its invokation.
*/
Term.prototype.exec = function() {
var argv = $.grep($.makeArray(arguments), function(s) {
return s.isSpace();
}, true);
if (argv.length < 1) {
return false;
}
var bin = this.pathtonodeexec(argv[0]);
var retval = true;
if (bin in this.errnos) {
this.print(argv[0] + ': ' + this.errnos[bin] + '\n');
retval = false;
} else if (bin.type != FILE_T || !(bin.perm & EXEC_P)) {
this.print(argvtokens[0] + ': ' + this.errnos['EACCES'] + '\n');
retval = false;
} else {
retval = bin.exec.apply(this, argv.slice(1, argv.length));
}
return retval;
};
/**
* Helper function for path resolution. Use pathtonode* instead of
* using directly.
*/
Term.prototype._find = function(root, pathbits) {
if (pathbits.length == 0) {
return root;
} else if (pathbits[0] == '.') {
return this._find(root, pathbits.slice(1, pathbits.length));
} else if (pathbits[0] == '..') {
return this._find(root.parent, pathbits.slice(1, pathbits.length));
}
if (!('contents' in root && pathbits[0] in root.contents)) {
return 'ENOENT';
}
var nextnode = root.contents[pathbits[0]];
if (nextnode.perm & READ_P) {
return this._find(nextnode, pathbits.slice(1, pathbits.length));
} else {
return 'EACCES';
}
};
/**
* Resolves a path, relative or absolute.
*/
Term.prototype.pathtonode = function(path) {
return this._find(this.fs,
$.grep(this.abspath(path).split('/'), function(s) {
return s.isSpace();
}, true));
};
/**
* Resolves a path, relative or absolute, but with shell semantics:
* - absolute paths are resolved normally
* - relative paths containing a '/' character are resolved normally
* - single filenames are searched for in $PATH. Currently, $PATH
* only contains one directory.
*/
Term.prototype.pathtonodeexec = function(path) {
var pathbits = path.split('/');
if (path[0] != '/' && pathbits.length == 1) {
var splitenvpath = this.env['PATH'].split(':');
var found = false;
var term = this;
$.each(splitenvpath, function() {
found = term._find(term.pathtonode(this), pathbits);
if (found && !(found in term.errnos)) {
return false; // break
}
return true; // continue
});
if (found == false) {
return 'ENOENT';
} else {
return found;
}
} else {
return this.pathtonode(path);
}
};
/**
* Converts a relative or absolute path to an absolute one. Also
* performs tilde expansion, but without a username.
*/
Term.prototype.abspath = function(path) {
var pathfromroot = '';
if (path[0] == '/') {
pathfromroot = path;
} else if (path.length == 1 && path[0] == '~') {
return this.env['HOME'];
} else if (path.substring(0,2) == '~/') {
pathfromroot = this.env['HOME'] + '/' +
path.substring(2, path.length);
} else {
pathfromroot = this.env['PWD'] + '/' + path;
}
var splitpath = $.grep(pathfromroot.split('/'), function(s) {
return s.isSpace();
}, true);
var abspathbits = new Array();
$.each(splitpath, function() {
if (this == '..') {
if (abspathbits.length > 0) {
abspathbits.pop();
}
} else if (this != '.') {
abspathbits.push(this);
}
});
if (abspathbits.length == 0) {
return '/';
} else {
return '/' + abspathbits.join('/');
}
};
/**
* Converts a path to an absolute path, replacing $HOME with the tilde.
*/
Term.prototype.canonpath = function(path) {
var abspath = this.abspath(path);
return abspath.replace(new RegExp('^' + this.env['HOME']), '~');
};
/**
* Wraps stuff in <span> or <a> tags (if something is a link), then
* outputs it.
*/
Term.prototype.show = function(name, node) {
var elt;
if (node.type == FILE_T) {
if (node.perm & EXEC_P) {
elt = $('<span class="file exec" />');
} else {
elt = $('<span class="file" />');
}
} else if (node.type == LINK_T) {
elt = $('<a href="' + node.contents[0] + '" class="link" target="_blank" />');
} else if (node.type == DIR_T) {
elt = $('<span class="dir" />');
}
elt.text(name);
return elt;
};
/**
* Prints something to the "terminal". This can be either a string or a DOM
* node.
*/
Term.prototype.print = function(o) {
if (o) {
this.elts.output.append(o);
}
$.scrollTo('max');
};
Term.prototype.println = function(o) {
this.print(o);
this.print('\n');
};
})();