-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnsh.js
executable file
·190 lines (156 loc) · 4.14 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
185
186
187
188
189
190
#!/usr/bin/env node
var fs = require('fs');
var glob = require('glob');
var path = require('path');
var rl = require('readline');
var cp = require('child_process');
var parse = require('lib-cmdparse');
var ps = require('lib-pathsearch');
var pc = require('lib-pathcomplete');
var state = {
"?" : null
}
var execinfo_path = process.env.PATH.split(':');
// auto-complete handler
function completer (line, callback) {
var split = line.split(/\s+/);
var item = split.pop();
var outs = [];
// avoid crazy auto-completions when the line is empty
if (!line) return callback(1);
// user is attempting to type a relative directory
var is_rel = item[0] === '.';
// user is typing first command
var is_first = split.length === 0;
// if this is the first token on the line
// autocomplete it against commands in the search path
if (!is_rel && is_first) ps(item, execinfo_path, function (err, execs) {
// if there is only one executable, append a space after it
if (execs.length === 1) execs[0] = execs[0] + ' ';
callback(err, [execs, item]);
});
// if this is
else pc(item, function (err, arr, info) {
// if there is only one completion, and it's a directory
// automatically append a '/' to the completion
if (arr.length === 1) {
if (fs.statSync(info.dir + arr[0]).isDirectory())
callback(err, [[arr[0] + '/'], info.file]);
else callback(err, [arr, info.file]);
}
else callback(err, [arr, info.file]);
});
}
var iface = rl.createInterface({
input : process.stdin,
output : process.stdout,
completer : completer
});
// visually indicate closed pipe with ^D
// otherwise exiting nested shells is confusing
iface.on('close', function () {
process.stdout.write('^D\n');
});
// handle ^C like bash
iface.on('SIGINT', function () {
process.stdout.write('^C');
iface.clearLine();
prompt();
});
process.on('SIGINT', function () {
// ignore
});
function readline(line){
line = interpolate(line.trim(), process.env);
line = interpolate(line, state);
if (line && line.length > 0) {
if (line.substring(0,2) === 'cd'){
// cd is a native command
var dir = line.substring(2).trim();
if (dir.length === 0) dir=process.env.HOME;
// should not crash process with a bad 'cd'
try {
process.chdir(dir);
} catch (e) {
console.log(e);
}
setImmediate(prompt);
}else{
// other commands
run(line);
}
} else {
setImmediate(prompt);
}
}
process.on('close',function(){
process.exit(0);
});
// replace $VARs with environment variables
function interpolate(string, replace){
return string.replace(/\$[^\s]+/g, function (key){
var name = key.substring(1);
var out;
if(replace[name] || replace[name] === 0){
out = replace[name];
} else {
out = key;
}
return out;
});
}
function run(line){
// allow for setting environment variables
// on the command line
var stanza = parse(line);
// fallback to current environment
stanza.envs.__proto__ = process.env;
// We must stop reading STDIN because we will soon
// be the background process group, which will raise
// errors when attempting to read/write to the TTY driver
process.stdin.setRawMode(false)
process.stdin.pause();
// Sub-Process
var args = stanza.args;
var exec = stanza.exec;
var proc = cp.spawn(exec,args,{
cwd: process.cwd(),
env: stanza.envs,
// Inerit the terminal
stdio: 'inherit'
});
// Have this shell resume control after the sub-process exists
function res(){
process.stdin.setRawMode(true)
process.stdin.resume();
prompt();
}
// catch exit code
function end(code, signal){
// the $? variable should contain the exit code
state["?"] = code;
state["??"] = signal;
res();
}
// catch errors
function err(err){
res();
}
proc.on('error', err);
proc.on('exit', end);
}
function prompt(){
var prefix;
try {
prefix = process.cwd();
} catch (e) {
prefix = "(none)";
}
iface.question(prefix + " # ", function (line) {
readline(line);
});
}
// Main method
if(!module.parent){
prompt();
}