-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShell.ts
338 lines (312 loc) · 9.12 KB
/
Shell.ts
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import { ProcessMain, Process } from './Process';
import { IDisposable } from 'xterm';
import { Pipe, getLogPipe } from './Pipe';
import { tcsetattr, isatty, tcgetattr } from './Tty';
import { TERMIOS_RAW, TERMIOS_COOKED, IFlags, OFlags, LFlags } from './Termios';
import { IPipe } from './Types';
import STTY from './executables/stty';
/**
* Simple line based shell REPL.
*/
export const FakeShell: ProcessMain = (argv, process) => {
interface ICmd {
cmd: string;
argv: string[];
error: string;
}
let currentReadHandler: IDisposable;
let lastExitCode = 0; // TODO: provide $? var
let shellTermios = tcgetattr(process.stdin);
function showPrompt(): void {
process.stdout.write('FakeShell> ');
}
function parseCommand(data: string): ICmd[] {
const result: ICmd[] = [{cmd: '', argv: [], error: ''}];
let cmdPos = 0;
data = data.trim();
if (!data) return result;
if (!~data.indexOf(' ')) {
result[cmdPos].cmd = data;
return result;
}
result[cmdPos].cmd = data.slice(0, data.indexOf(' '));
data = data.slice(data.indexOf(' ') + 1).trim();
if (!~data.indexOf('"')) {
if (!~data.indexOf('|')) {
result[cmdPos].argv = data.split(/\s+/);
return result;
}
}
const parts = data.split('"');
if (parts.length % 2 === 0) {
result[cmdPos].error = 'unterminated argument';
return result;
}
let isCmd = false;
for (let i = 0; i < parts.length; ++i) {
let part = parts[i].trim();
if (!part) continue;
if (i % 2) {
result[cmdPos].argv.push(part);
} else {
part = part.replace(/[|]/g, ' | ');
const sub = part.split(/\s+/);
for (let i = 0; i < sub.length; ++i) {
if (sub[i] === '|') {
result.push({cmd: '', argv: [], error: ''});
cmdPos++;
isCmd = true;
continue;
}
if (isCmd) {
result[cmdPos].cmd = sub[i];
isCmd = false;
} else {
if (sub[i]) {
result[cmdPos].argv.push(sub[i]);
}
}
}
}
}
return result;
}
function evalCommand(cmd: ICmd[]): boolean {
// basic error checks
if (cmd[0].error) {
process.stdout.write(`[Error]: ${cmd[0].error}\n`);
return false;
}
if (!cmd[0].cmd) {
return false;
}
// check if programs are available
const progs = cmd.map(el => el.cmd);
for (const prog of progs) {
if (!(prog in KNOWN_COMMANDS)) {
process.stdout.write(`unknown command: "${prog}"\nType \`commands\` to see a list of supported commands.\n`);
return false;
}
}
return true;
}
function runCommand(cmd: ICmd[]): void {
// create pipes
const ttyIn = (process.stdin as any)._tty;
const ttyOut = (process.stdout as any)._tty;
const pipes: IPipe[] = [];
for (let i = 0; i < cmd.length - 1; ++i) {
pipes.push(new Pipe());
}
// create processes
const processes: Process[] = [];
for (let i = 0; i < cmd.length; ++i) {
if (i === 0) {
processes.push(new Process(KNOWN_COMMANDS[cmd[0].cmd], ttyIn, pipes[0] || ttyOut, pipes[0] || ttyOut));
} else if (i === cmd.length - 1) {
processes.push(new Process(KNOWN_COMMANDS[cmd[cmd.length - 1].cmd], pipes[pipes.length - 1] || ttyIn, ttyOut, ttyOut));
} else {
processes.push(new Process(KNOWN_COMMANDS[cmd[i].cmd], pipes[i - 1], pipes[i], pipes[i]));
}
}
// detach shell from tty
currentReadHandler?.dispose();
// reattach after last process is gone
processes[processes.length - 1].afterExit(() => {
// dont restore old termios settings by default to allow stty changes take effect
// NOTE: to still recover from raw settings: LF reset LF (LF is Ctrl-J)
// tcsetattr(process.stdin, shellTermios);
currentReadHandler = process.stdin.onData(readHandler);
showPrompt();
});
// grab exit code from last process
processes[processes.length - 1].onExit(statusCode => { lastExitCode = statusCode; });
// run processes
for (let i = 0; i < processes.length; ++i) {
// handle data === null in fg; TODO: better fg distinction
processes[i].stdin.onData(data => { if (data === null) processes[i].exit(); });
processes[i].run(cmd[i].argv, {...process.env, SHELL: 'FakeShell'});
}
}
// primitive REPL
let cmdstring = '';
const readHandler = (data: string) => {
if (data === null) {
// shell exit
process.exit();
return;
}
// FIXME: write handling of messed up termios settings (entry for `reset`)
cmdstring += data;
// parse, eval and run
if (cmdstring.endsWith('\n')) {
const cmd = parseCommand(cmdstring);
cmdstring = '';
if (evalCommand(cmd)) runCommand(cmd);
else showPrompt();
}
}
process.onExit(() => process.stdout.write('\n[Exiting FakeShell]\n'));
// startup
currentReadHandler = process.stdin.onData(readHandler);
process.stdout.write('\x1b[2J\x1b[H');
showPrompt();
}
/**
* Example program.
*/
const FUN: ProcessMain = (argv, process) => {
process.stdout.write('This actually does nothing more than printing this...\n');
process.exit();
}
/**
* Simple echo program.
*/
const ECHO: ProcessMain = (argv, process) => {
process.stdout.write(
argv.join(' ')
.replace(/\\n/g, '\n')
.replace(/\\r/g, '\r')
.replace(/\\t/g, '\t')
.replace(/\\x([0-9A-Fa-f][0-9A-Fa-f])/g, (m, p1) => String.fromCharCode(parseInt(p1, 16)))
+ '\n'
);
process.exit();
}
/**
* Simple wc implementation.
*/
const WC: ProcessMain = (argv, process) => {
let gChars = 0;
let gLines = 0;
let gWords = 0;
let lastChunk = '';
process.onExit(() => process.stdout.write(`\t${gLines}\t${gWords}\t${gChars}\n`));
process.stdin.onData(data => {
// last chunk is null in interactive mode
if (data === null) {
return;
}
gChars += data.length;
data += lastChunk;
lastChunk = '';
const lines = data.split('\n');
gLines += lines.length;
for (let i = 0; i < lines.length; ++i) {
const words = lines[i].split(/\s+/);
gWords += words.filter(Boolean).length;
if (i === lines.length - 1) {
// fix split last word
if (words[words.length - 1]) {
gWords--;
lastChunk = words[words.length - 1];
} else if (lines[i] === '') {
gLines--;
lastChunk = '\n';
}
}
}
});
}
/**
* Testing raw mode...
*/
const RAW: ProcessMain = (argv, process) => {
const oldTermios = tcgetattr(process.stdin);
process.stdout.write('Exit with Ctrl-D.\r\n');
tcsetattr(process.stdin, TERMIOS_RAW);
process.stdin.onData(data => {
if (data === '\x04') {
tcsetattr(process.stdin, oldTermios);
process.exit();
return;
}
// escape any control codes
let escaped = '';
for (let i = 0; i < data.length; ++i) {
if (data.charCodeAt(i) < 0x20) {
escaped += '^' + String.fromCharCode(data.charCodeAt(i) + 0x40);
} else {
escaped += data[i];
}
}
process.stdout.write(`byte read: "${escaped}"\r\n`);
});
}
const RESET: ProcessMain = (argv, process) => {
if (isatty(process.stdout)) {
tcsetattr(process.stdout, TERMIOS_COOKED);
process.stdout.write('\x1bc');
}
setTimeout(() => process.exit(), 300);
}
const CAT: ProcessMain = (argv, process) => {
process.stdin.onData(data => {
if (data !== null) {
process.stdout.write(data);
}
});
}
const SLEEP: ProcessMain = (argv, process) => {
let seconds;
if (argv.length !== 1 || isNaN(seconds = parseFloat(argv[0]))) {
process.stderr.write('sleep: illegal operant\n');
process.exit(1);
}
setTimeout(() => process.exit(), seconds * 1000);
}
const EXPORT: ProcessMain = (argv, process) => {
if (argv.length) {
} else {
for (const key of Object.keys(process.env).sort()) {
process.stdout.write(`${key}=${process.env[key]}\n`);
}
}
process.exit();
}
const LONGRUN: ProcessMain = (argv, process) => {
let running = true;
process.onExit(() => { running = false; });
async function main(): Promise<void> {
let counter = 0;
while(running) {
process.stdout.write(`${counter++}\n`);
await new Promise(resolve => setTimeout(resolve, 100));
}
}
main();
};
// tee to console.log
const LOG: ProcessMain = (argv, process) => {
const logWriter = getLogPipe().getWriter();
process.stdin.onData(data => {
if (data !== null) {
process.stdout.write(data);
logWriter.write(data);
}
});
}
const COMMANDS: ProcessMain = (argv, process) => {
const commands = Object.keys(KNOWN_COMMANDS);
commands.sort((a, b) => a.localeCompare(b));
for (let i = 0; i < commands.length; ++i) {
process.stdout.write(`${commands[i]}\n`);
}
process.exit();
}
const KNOWN_COMMANDS: {[key: string]: ProcessMain} = {
'fun': FUN,
'echo': ECHO,
'stty': STTY,
'raw': RAW,
'reset': RESET,
'commands': COMMANDS,
'wc': WC,
'cat': CAT,
'sleep': SLEEP,
'export': EXPORT,
'longrun': LONGRUN,
'log': LOG
};
// missing shell operators: &&, ||, ;, redirects
// missing process primitives: cterm, fg, bg