brsh 2.1.5 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/dist/shell.es.js +461 -287
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +5 -4
- package/dist/shell.min.js.map +1 -1
- package/dist/terminal.es.js +420 -246
- package/dist/terminal.es.js.map +1 -1
- package/dist/terminal.min.js +8 -7
- package/dist/terminal.min.js.map +1 -1
- package/index.js +9 -0
- package/lib/Command.js +1 -2
- package/lib/ScriptCommand.js +220 -8
- package/lib/builtins/index.js +1 -0
- package/lib/builtins/printf.js +48 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -156,6 +156,15 @@ class Shell extends EventEmitter {
|
|
|
156
156
|
if (!args.length) return;
|
|
157
157
|
let bin = args.shift().value;
|
|
158
158
|
|
|
159
|
+
// Bare variable assignment: VAR=value (only when no additional args)
|
|
160
|
+
if (args.length === 0 && /^[A-Za-z_]\w*=/.test(bin)) {
|
|
161
|
+
const eqIdx = bin.indexOf('=');
|
|
162
|
+
this.context.setVar(bin.slice(0, eqIdx), bin.slice(eqIdx + 1));
|
|
163
|
+
this.lastCode = 0;
|
|
164
|
+
this.emit('exitCode', 0);
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
|
|
159
168
|
// Parse output redirection (> and >>)
|
|
160
169
|
// Quoted tokens (hasQuote: true) are never treated as redirects.
|
|
161
170
|
let redirectPath = null;
|
package/lib/Command.js
CHANGED
|
@@ -38,7 +38,7 @@ class Command extends EventEmitter {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
onInput(char) {
|
|
41
|
-
//
|
|
41
|
+
//ignored by default, can be overridden in a command if needed to get input
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
set exitCode(code) {
|
|
@@ -54,7 +54,6 @@ class Command extends EventEmitter {
|
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
flush() {
|
|
57
|
-
console.log("flush now");
|
|
58
57
|
this.emit('flush', this.result.flush());
|
|
59
58
|
}
|
|
60
59
|
}
|
package/lib/ScriptCommand.js
CHANGED
|
@@ -2,6 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
const LocalCommand = require('./local/LocalCommand');
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Returns true if the first line of a file is a shebang for a known/supported
|
|
7
|
+
* interpreter: #!/sh.js (legacy), #!/bin/sh, or #!/bin/brsh.
|
|
8
|
+
*/
|
|
9
|
+
function isKnownInterpreter(firstLine) {
|
|
10
|
+
return /^#!\/(sh\.js|bin\/(sh|brsh))/.test(firstLine);
|
|
11
|
+
}
|
|
12
|
+
|
|
5
13
|
class ScriptCommand extends LocalCommand {
|
|
6
14
|
constructor(script, args, context, source = false) {
|
|
7
15
|
super(args, context);
|
|
@@ -9,6 +17,14 @@ class ScriptCommand extends LocalCommand {
|
|
|
9
17
|
this.source = source;
|
|
10
18
|
}
|
|
11
19
|
|
|
20
|
+
get captureInput() { return true; }
|
|
21
|
+
|
|
22
|
+
onInput(char) {
|
|
23
|
+
if (this.sandbox) {
|
|
24
|
+
this.sandbox.onInput(char);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
12
28
|
run() {
|
|
13
29
|
const fsOpts = this.context.fs.isRealFilesystem
|
|
14
30
|
? { useRealFilesystem: true }
|
|
@@ -22,19 +38,32 @@ class ScriptCommand extends LocalCommand {
|
|
|
22
38
|
this.sandbox.on('stdOut', data => this.stdOut = data);
|
|
23
39
|
this.sandbox.on('stdErr', data => this.stdErr = data);
|
|
24
40
|
|
|
41
|
+
this._scriptFunctions = {};
|
|
42
|
+
|
|
25
43
|
const lines = this.script.split(/\r?\n/);
|
|
26
44
|
|
|
27
|
-
if(!lines[0]
|
|
28
|
-
|
|
45
|
+
if (!isKnownInterpreter(lines[0])) {
|
|
46
|
+
const interpreter = lines[0].startsWith('#!')
|
|
47
|
+
? lines[0].slice(2).split(/\s/)[0]
|
|
48
|
+
: lines[0];
|
|
49
|
+
return Promise.reject(`${interpreter}: bad interpreter: No such file or directory`);
|
|
29
50
|
}
|
|
30
51
|
|
|
31
52
|
const nodes = this._parseLines(lines.slice(1));
|
|
32
|
-
return this._executeNodes(nodes)
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
53
|
+
return this._executeNodes(nodes)
|
|
54
|
+
.catch(e => {
|
|
55
|
+
if (e && e.__type === 'return') {
|
|
56
|
+
this.sandbox.lastCode = e.code;
|
|
57
|
+
} else {
|
|
58
|
+
return Promise.reject(e);
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
.then(() => {
|
|
62
|
+
this.exitCode = this.sandbox.lastCode;
|
|
63
|
+
if(this.source) {
|
|
64
|
+
this.context.source(this.sandbox.context);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
38
67
|
}
|
|
39
68
|
|
|
40
69
|
_parseLines(lines) {
|
|
@@ -62,6 +91,18 @@ class ScriptCommand extends LocalCommand {
|
|
|
62
91
|
i += result.consumed;
|
|
63
92
|
continue;
|
|
64
93
|
}
|
|
94
|
+
if (line.startsWith('case ') || line === 'case') {
|
|
95
|
+
const result = this._parseCase(lines, i);
|
|
96
|
+
nodes.push(result.node);
|
|
97
|
+
i += result.consumed;
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
if (/^(?:function\s+[\w\d_]+|[\w\d_]+\s*\(\s*\))\s*(?:\{.*)?$/.test(line)) {
|
|
101
|
+
const result = this._parseFunction(lines, i);
|
|
102
|
+
nodes.push(result.node);
|
|
103
|
+
i += result.consumed;
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
65
106
|
nodes.push({ type: 'command', line });
|
|
66
107
|
i++;
|
|
67
108
|
}
|
|
@@ -177,12 +218,39 @@ class ScriptCommand extends LocalCommand {
|
|
|
177
218
|
}
|
|
178
219
|
|
|
179
220
|
_executeNode(node) {
|
|
221
|
+
if (node.type === 'function') {
|
|
222
|
+
const funcName = node.name;
|
|
223
|
+
const funcBody = node.bodyLines;
|
|
224
|
+
const self = this;
|
|
225
|
+
const LocalCmdBase = require('./local/LocalCommand');
|
|
226
|
+
const FuncCmd = class extends LocalCmdBase {
|
|
227
|
+
static get name() { return funcName; }
|
|
228
|
+
run() {
|
|
229
|
+
return self._executeFunctionBody(funcBody).catch(e => {
|
|
230
|
+
if (e && e.__type === 'return') {
|
|
231
|
+
this.exitCode = e.code;
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
return Promise.reject(e);
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
this.sandbox.context.setCommand(FuncCmd);
|
|
239
|
+
return Promise.resolve();
|
|
240
|
+
}
|
|
180
241
|
if (node.type === 'command') {
|
|
242
|
+
// Intercept 'return [n]' before it reaches the sandbox
|
|
243
|
+
if (/^return(\s+\d+)?\s*$/.test(node.line.trim())) {
|
|
244
|
+
const match = node.line.trim().match(/^return\s+(\d+)/);
|
|
245
|
+
const code = match ? parseInt(match[1], 10) : 0;
|
|
246
|
+
return Promise.reject({ __type: 'return', code });
|
|
247
|
+
}
|
|
181
248
|
return this.sandbox.onCommand(node.line, false);
|
|
182
249
|
}
|
|
183
250
|
if (node.type === 'if') return this._executeIf(node);
|
|
184
251
|
if (node.type === 'while') return this._executeWhile(node);
|
|
185
252
|
if (node.type === 'for') return this._executeFor(node);
|
|
253
|
+
if (node.type === 'case') return this._executeCase(node);
|
|
186
254
|
return Promise.resolve();
|
|
187
255
|
}
|
|
188
256
|
|
|
@@ -216,6 +284,150 @@ class ScriptCommand extends LocalCommand {
|
|
|
216
284
|
});
|
|
217
285
|
}, Promise.resolve());
|
|
218
286
|
}
|
|
287
|
+
|
|
288
|
+
_parseFunction(lines, start) {
|
|
289
|
+
const line = lines[start].trim();
|
|
290
|
+
const nameMatch = line.match(/^function\s+([\w\d_]+)/)
|
|
291
|
+
|| line.match(/^([\w\d_]+)\s*\(/);
|
|
292
|
+
const name = nameMatch ? nameMatch[1] : '';
|
|
293
|
+
|
|
294
|
+
let i = start + 1;
|
|
295
|
+
// If opening brace not on the definition line, find it
|
|
296
|
+
if (!line.includes('{')) {
|
|
297
|
+
while (i < lines.length && !lines[i].includes('{')) i++;
|
|
298
|
+
i++; // skip the { line
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const bodyLines = [];
|
|
302
|
+
let depth = 1;
|
|
303
|
+
while (i < lines.length) {
|
|
304
|
+
const trimmed = lines[i].trim();
|
|
305
|
+
if (trimmed === '}') { i++; break; }
|
|
306
|
+
// Track nested brace depth
|
|
307
|
+
const opens = (lines[i].match(/\{/g) || []).length;
|
|
308
|
+
const closes = (lines[i].match(/\}/g) || []).length;
|
|
309
|
+
depth += opens - closes;
|
|
310
|
+
if (depth <= 0) { i++; break; }
|
|
311
|
+
bodyLines.push(lines[i]);
|
|
312
|
+
i++;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
return { node: { type: 'function', name, bodyLines }, consumed: i - start };
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
_parseCase(lines, start) {
|
|
319
|
+
const firstLine = lines[start].trim();
|
|
320
|
+
const match = firstLine.match(/^case\s+(.+?)\s+in\s*$/);
|
|
321
|
+
const word = match ? match[1] : '';
|
|
322
|
+
|
|
323
|
+
let i = start + 1;
|
|
324
|
+
const clauses = [];
|
|
325
|
+
let currentPattern = null;
|
|
326
|
+
let currentBody = [];
|
|
327
|
+
|
|
328
|
+
while (i < lines.length) {
|
|
329
|
+
const raw = lines[i];
|
|
330
|
+
const line = raw.trim();
|
|
331
|
+
i++;
|
|
332
|
+
|
|
333
|
+
if (!line || line.startsWith('#')) continue;
|
|
334
|
+
if (line === 'esac') break;
|
|
335
|
+
|
|
336
|
+
if (currentPattern === null) {
|
|
337
|
+
// Pattern line: "pattern) rest" or "pattern)"
|
|
338
|
+
const patMatch = line.match(/^(.+?)\)\s*(.*)$/);
|
|
339
|
+
if (patMatch) {
|
|
340
|
+
currentPattern = patMatch[1].trim();
|
|
341
|
+
const rest = patMatch[2].trim();
|
|
342
|
+
if (rest === ';;') {
|
|
343
|
+
clauses.push({ pattern: currentPattern, bodyLines: [] });
|
|
344
|
+
currentPattern = null;
|
|
345
|
+
} else if (rest.endsWith(';;')) {
|
|
346
|
+
const body = rest.slice(0, -2).trim();
|
|
347
|
+
clauses.push({ pattern: currentPattern, bodyLines: body ? [body] : [] });
|
|
348
|
+
currentPattern = null;
|
|
349
|
+
} else if (rest) {
|
|
350
|
+
currentBody = [rest];
|
|
351
|
+
} else {
|
|
352
|
+
currentBody = [];
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
} else if (line === ';;') {
|
|
356
|
+
clauses.push({ pattern: currentPattern, bodyLines: currentBody });
|
|
357
|
+
currentPattern = null;
|
|
358
|
+
currentBody = [];
|
|
359
|
+
} else if (line.endsWith(';;')) {
|
|
360
|
+
currentBody.push(line.slice(0, -2).trim());
|
|
361
|
+
clauses.push({ pattern: currentPattern, bodyLines: currentBody });
|
|
362
|
+
currentPattern = null;
|
|
363
|
+
currentBody = [];
|
|
364
|
+
} else {
|
|
365
|
+
currentBody.push(raw);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// Handle an unclosed final clause
|
|
370
|
+
if (currentPattern !== null) {
|
|
371
|
+
clauses.push({ pattern: currentPattern, bodyLines: currentBody });
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
return { node: { type: 'case', word, clauses }, consumed: i - start };
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
_executeFunctionBody(bodyLines) {
|
|
378
|
+
return this._executeNodes(this._parseLines(bodyLines));
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
_executeCase(node) {
|
|
382
|
+
return this.sandbox._replaceVariables(node.word).then(expanded => {
|
|
383
|
+
// Strip outer quotes produced by variable expansion
|
|
384
|
+
const value = ((expanded.startsWith('"') && expanded.endsWith('"'))
|
|
385
|
+
|| (expanded.startsWith("'") && expanded.endsWith("'")))
|
|
386
|
+
? expanded.slice(1, -1) : expanded;
|
|
387
|
+
|
|
388
|
+
for (const clause of node.clauses) {
|
|
389
|
+
if (this._matchPattern(clause.pattern, value)) {
|
|
390
|
+
return this._executeNodes(this._parseLines(clause.bodyLines));
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
_matchPattern(pattern, value) {
|
|
397
|
+
let regexStr = '';
|
|
398
|
+
let i = 0;
|
|
399
|
+
while (i < pattern.length) {
|
|
400
|
+
const ch = pattern[i];
|
|
401
|
+
if (ch === '[') {
|
|
402
|
+
const end = pattern.indexOf(']', i + 1);
|
|
403
|
+
if (end !== -1) {
|
|
404
|
+
regexStr += pattern.slice(i, end + 1);
|
|
405
|
+
i = end + 1;
|
|
406
|
+
} else {
|
|
407
|
+
regexStr += '\\[';
|
|
408
|
+
i++;
|
|
409
|
+
}
|
|
410
|
+
} else if (ch === '*') {
|
|
411
|
+
regexStr += '.*';
|
|
412
|
+
i++;
|
|
413
|
+
} else if (ch === '?') {
|
|
414
|
+
regexStr += '.';
|
|
415
|
+
i++;
|
|
416
|
+
} else if (/[.+^${}()|\\]/.test(ch)) {
|
|
417
|
+
regexStr += '\\' + ch;
|
|
418
|
+
i++;
|
|
419
|
+
} else {
|
|
420
|
+
regexStr += ch;
|
|
421
|
+
i++;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
try {
|
|
425
|
+
return new RegExp('^' + regexStr + '$').test(value);
|
|
426
|
+
} catch (_) {
|
|
427
|
+
return false;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
219
430
|
}
|
|
220
431
|
|
|
221
432
|
module.exports = ScriptCommand;
|
|
433
|
+
module.exports.isKnownInterpreter = isKnownInterpreter;
|
package/lib/builtins/index.js
CHANGED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Command = require('../Command');
|
|
4
|
+
|
|
5
|
+
class Printf extends Command {
|
|
6
|
+
static get name() { return 'printf'; }
|
|
7
|
+
|
|
8
|
+
run() {
|
|
9
|
+
const args = [...this.arguments];
|
|
10
|
+
if (!args.length) return Promise.resolve();
|
|
11
|
+
|
|
12
|
+
let fmt = args.shift();
|
|
13
|
+
|
|
14
|
+
// Process escape sequences in the format string
|
|
15
|
+
fmt = fmt
|
|
16
|
+
.replace(/\\n/g, '\n')
|
|
17
|
+
.replace(/\\t/g, '\t')
|
|
18
|
+
.replace(/\\r/g, '\r')
|
|
19
|
+
.replace(/\\a/g, '\x07')
|
|
20
|
+
.replace(/\\b/g, '\x08')
|
|
21
|
+
.replace(/\\e/g, '\x1b')
|
|
22
|
+
.replace(/\\f/g, '\x0c')
|
|
23
|
+
.replace(/\\v/g, '\x0b')
|
|
24
|
+
.replace(/\\\\/g, '\\');
|
|
25
|
+
|
|
26
|
+
// Process format specifiers
|
|
27
|
+
let result = '';
|
|
28
|
+
let argIdx = 0;
|
|
29
|
+
for (let i = 0; i < fmt.length; i++) {
|
|
30
|
+
if (fmt[i] === '%' && i + 1 < fmt.length) {
|
|
31
|
+
const spec = fmt[i + 1];
|
|
32
|
+
if (spec === 's') { result += String(args[argIdx++] ?? ''); i++; }
|
|
33
|
+
else if (spec === 'd' || spec === 'i') { result += String(parseInt(args[argIdx++]) || 0); i++; }
|
|
34
|
+
else if (spec === 'f') { result += String(parseFloat(args[argIdx++]) || 0); i++; }
|
|
35
|
+
else if (spec === '%') { result += '%'; i++; }
|
|
36
|
+
else { result += fmt[i]; }
|
|
37
|
+
} else {
|
|
38
|
+
result += fmt[i];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Emit each line directly — no automatic trailing newline (unlike echo)
|
|
43
|
+
result.split(/\r?\n/).forEach(line => this.stdOut = line);
|
|
44
|
+
return Promise.resolve();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
module.exports = Printf;
|