-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnsh.js
executable file
·184 lines (179 loc) · 5.06 KB
/
nsh.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
#!/usr/bin/env node
var esprima = require('./vendor/esprima');
var spoon = require('spoon');
var escodegen = require('escodegen');
// Error.stackTraceLimit = 100;
function NSHELL() {
// Error.stackTraceLimit = 100;
var waitpid = require('waitpid')
__$callback = function (err, val) {
if (err) throw err;
return val;
}
var Stream = require('stream').Stream;
var BufferedStream = require('buffered').BufferedStream;
JOBS = [];
EXPORTS = {};
function FilteringStream(fn, options) {
Stream.call(this, options);
this.fn = fn;
this.piped = false;
var self = this;
this.on('pipe', function (s) {
s.on('data', function (data) {
self.write(data);
})
s.on('end', function () {
self.end();
})
})
return this;
}
FilteringStream.prototype = new Stream;
FilteringStream.prototype.constructor = FilteringStream;
FilteringStream.prototype.pipe = function (s, options) {
Stream.prototype.pipe.call(this, s, options);
}
FilteringStream.prototype.write = function (d) {
var val = this.fn(d) + '';
this.emit('data', val);
}
FilteringStream.prototype.end = function () {
this.emit('end');
}
var getOriginStream = function (origin) {
if (origin.pipe
|| (origin.pid && origin.stdio)) {
return getStreamable(origin);
}
var buff = new BufferedStream();
buff.write(origin + '');
return buff;
}
var getStreamable = function (origin) {
if (origin.pipe) {
return origin;
}
if (origin.pid && origin.stdio) {
return origin;
}
if (typeof origin === 'function') {
return new FilteringStream(origin);
}
var buff = new FilteringStream(function (d) {
return d | origin;
});
return buff;
}
var getOutputStream = function (origin) {
if (origin.pid) {
return origin.stdio[1];
}
return origin;
}
var getInputStream = function (origin) {
if (origin.pid) {
return origin.stdio[0];
}
return origin;
}
$OR = function (origin, destination, next) {
if (originStream && destinationStream) {
origin.pipe(destination);
next();
}
else {
next( null, origin || destination );
}
}
$ENV = function (k) {
return process.env[k];
}
$PIPE = function () {
var args = [].slice.call(arguments);
var next = args.pop();
// console.error('piping', arguments)
var togo = 0;
var done = false;
function finish(e) {
if (done) return;
if (e) {
done = true;
}
else {
togo--;
if (!togo) done = true;
}
next(e);
}
var arg = args.shift();
var original = getOriginStream(arg);
for (var i = 0; i < args.length;i++) {
args[i] = getStreamable(args[i]);
}
for (var i = args.length - 1; i >= 0; i--) {
args[i] = getInputStream(args[i]);
if (i) getOutputStream(args[i - 1]).pipe(args[i]);
}
original.pipe(args[0]);
return null;
}
$ = function (cmd, next) {
cmd = cmd + '';
var proc = require('child_process').spawn(process.env.SHELL || 'sh', ['-c', cmd]);
var stdio = proc.stdio.map(function () {return ''});
proc.stdio.forEach(function (pipe, i) {
pipe.on('data', function (data) {
stdio[i] += data+''
})
});
JOBS.push(proc);
proc.on('exit', function (code) {
var index = JOBS.indexOf(proc);
if (index !== -1) JOBS.splice(index, 1);
if (code) {
next(new Error('$(' + JSON.stringify(cmd).slice(1, -1) + ') exited with code: ' + code));
}
else {
next(null, stdio[1]);
}
});
}
$CALL = function (cmd, args) {
switch(cmd) {
case "cd":
process.cwd(args.length ? args[0] : process.env.HOME);
break;
default:
var proc = require('child_process').spawn(process.env.SHELL || 'sh', ['-c', [cmd].concat(args || []).join(' ')], {
stdio: [
process.stdin, process.stdout, process.stderr
]
});
waitpid(proc.pid);
}
}
process.on('uncaughtException', function (e) {console.error(e.stack);})
}
var readline = require('readline');
var repl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: true
});
var vm = require('vm');
var ctx = vm.createContext({
process: process,
require: require,
console: console
});
vm.runInContext('(' + NSHELL.toString() + ')();', ctx);
repl.question('~ ', evaluate);
function evaluate(str) {
var program = str; // should dump "0" to console
var parsed = esprima.parse(program);
var toSpoon = escodegen.generate(parsed);
// console.error(toSpoon);
vm.runInContext('(function () { ' + spoon(toSpoon, ['$', '$PIPE']) + '})()', ctx);
repl.question('~ ', evaluate);
}