brsh 3.10.1 → 3.10.2
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 +46 -13
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +2 -2
- package/dist/shell.min.js.map +1 -1
- package/dist/wbt.es.js +20 -8
- package/dist/wbt.es.js.map +1 -1
- package/dist/wbt.min.js +1 -1
- package/dist/wbt.min.js.map +1 -1
- package/index.js +43 -9
- package/lib/ScriptCommand.js +19 -7
- package/package.json +1 -1
- package/test/scripts.test.js +48 -0
- package/test/shell.test.js +60 -0
package/index.js
CHANGED
|
@@ -1406,15 +1406,49 @@ class Shell extends EventEmitter {
|
|
|
1406
1406
|
|
|
1407
1407
|
async _parseCommandLine(string) {
|
|
1408
1408
|
const parts = Shell._splitCommand(string);
|
|
1409
|
-
const
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
)
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1409
|
+
const firstToken = parts.length > 0 ? parts[0].value : '';
|
|
1410
|
+
const isDeclCmd = firstToken === 'export' || firstToken === 'alias' || firstToken === 'local';
|
|
1411
|
+
|
|
1412
|
+
const expandedParts = [];
|
|
1413
|
+
for (let i = 0; i < parts.length; i++) {
|
|
1414
|
+
const { value: rawValue, hasQuote } = parts[i];
|
|
1415
|
+
|
|
1416
|
+
// POSIX "$@" expansion: expands to "$1" "$2" ... as separate quoted words
|
|
1417
|
+
// (or zero words when $# is 0).
|
|
1418
|
+
if (hasQuote && rawValue === '$@') {
|
|
1419
|
+
const count = parseInt(this.context.getVar('#') || '0', 10) || 0;
|
|
1420
|
+
for (let idx = 1; idx <= count; idx++) {
|
|
1421
|
+
expandedParts.push({
|
|
1422
|
+
value: String(this.context.getVar(String(idx))),
|
|
1423
|
+
hasQuote: true
|
|
1424
|
+
});
|
|
1425
|
+
}
|
|
1426
|
+
continue;
|
|
1427
|
+
}
|
|
1428
|
+
|
|
1429
|
+
const expanded = await this._replaceVariables(rawValue);
|
|
1430
|
+
|
|
1431
|
+
// Variable / alias assignments (VAR=val, export VAR=val, alias foo=bar)
|
|
1432
|
+
// and explicitly quoted tokens do not undergo IFS field splitting.
|
|
1433
|
+
const isBareAssignment = parts.length === 1 && /^[A-Za-z_]\w*=/.test(rawValue);
|
|
1434
|
+
const isDeclAssignment = i > 0 && isDeclCmd && /^[A-Za-z_]\w*=/.test(rawValue);
|
|
1435
|
+
|
|
1436
|
+
if (hasQuote || isBareAssignment || isDeclAssignment || !rawValue.includes('$')) {
|
|
1437
|
+
if (expanded.length > 0 || hasQuote) {
|
|
1438
|
+
expandedParts.push({ value: expanded, hasQuote });
|
|
1439
|
+
}
|
|
1440
|
+
continue;
|
|
1441
|
+
}
|
|
1442
|
+
|
|
1443
|
+
// Unquoted parameter / command / arithmetic expansion: perform POSIX IFS word splitting.
|
|
1444
|
+
// Preserve leading redirect prefix if attached directly to an expansion (e.g. >$OUT).
|
|
1445
|
+
const fields = expanded.trim().split(/\s+/).filter(Boolean);
|
|
1446
|
+
for (const field of fields) {
|
|
1447
|
+
expandedParts.push({ value: field, hasQuote: false });
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1450
|
+
|
|
1451
|
+
return expandedParts;
|
|
1418
1452
|
}
|
|
1419
1453
|
|
|
1420
1454
|
static _sanitiseCommandPart(string) {
|
package/lib/ScriptCommand.js
CHANGED
|
@@ -490,13 +490,25 @@ class ScriptCommand extends LocalCommand {
|
|
|
490
490
|
const FuncCmd = class extends LocalCmdBase {
|
|
491
491
|
static get name() { return funcName; }
|
|
492
492
|
run() {
|
|
493
|
-
// Set positional parameters ($1, $2,
|
|
494
|
-
const
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
self.sandbox.context.
|
|
493
|
+
// Set positional parameters ($1, $2, ..., $#, $@, $*) from call arguments
|
|
494
|
+
const prevCount = parseInt(self.sandbox.context.getVar('#') || '0', 10) || 0;
|
|
495
|
+
const maxCount = Math.max(prevCount, this.arguments.length);
|
|
496
|
+
const saved = [];
|
|
497
|
+
for (let i = 1; i <= maxCount; i++) {
|
|
498
|
+
const key = String(i);
|
|
499
|
+
saved.push({ key, prev: self.sandbox.context.getVar(key) });
|
|
500
|
+
if (i <= this.arguments.length) {
|
|
501
|
+
self.sandbox.context.setVar(key, this.arguments[i - 1]);
|
|
502
|
+
} else {
|
|
503
|
+
self.sandbox.context.unsetVar(key);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
for (const key of ['#', '@', '*']) {
|
|
507
|
+
saved.push({ key, prev: self.sandbox.context.getVar(key) });
|
|
508
|
+
}
|
|
509
|
+
self.sandbox.context.setVar('#', String(this.arguments.length));
|
|
510
|
+
self.sandbox.context.setVar('@', this.arguments.join(' '));
|
|
511
|
+
self.sandbox.context.setVar('*', this.arguments.join(' '));
|
|
500
512
|
const restore = () => saved.forEach(({ key, prev }) =>
|
|
501
513
|
prev ? self.sandbox.context.setVar(key, prev)
|
|
502
514
|
: self.sandbox.context.unsetVar(key));
|
package/package.json
CHANGED
package/test/scripts.test.js
CHANGED
|
@@ -561,4 +561,52 @@ describe('multiline quotes and line continuations in scripts', () => {
|
|
|
561
561
|
expect(res.exitCode).toBe(0);
|
|
562
562
|
expect(res.output).toEqual(['matched-2', 'matched-help', 'other:custom']);
|
|
563
563
|
});
|
|
564
|
+
|
|
565
|
+
it('splits accumulated $EXTRA_ARGS and preserves distinct "$@" arguments in wrapper scripts', async () => {
|
|
566
|
+
const received = [];
|
|
567
|
+
class TargetApp extends Command {
|
|
568
|
+
static get name() { return 'target-app'; }
|
|
569
|
+
run() {
|
|
570
|
+
received.push([...this.arguments]);
|
|
571
|
+
return Promise.resolve();
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
const shell = await createShell({
|
|
576
|
+
filesystem: {
|
|
577
|
+
bin: {
|
|
578
|
+
'launcher.sh': [
|
|
579
|
+
'#!/bin/sh',
|
|
580
|
+
'EXTRA_ARGS=""',
|
|
581
|
+
'for ARG in $@',
|
|
582
|
+
'do',
|
|
583
|
+
' case "$ARG" in',
|
|
584
|
+
' --blocks|--block|-b)',
|
|
585
|
+
' EXTRA_ARGS="$EXTRA_ARGS -blocks"',
|
|
586
|
+
' ;;',
|
|
587
|
+
' *)',
|
|
588
|
+
' EXTRA_ARGS="$EXTRA_ARGS $ARG"',
|
|
589
|
+
' ;;',
|
|
590
|
+
' esac',
|
|
591
|
+
'done',
|
|
592
|
+
'target-app -iwad "/game/freedoom1.wad" -nosound $EXTRA_ARGS'
|
|
593
|
+
].join('\n'),
|
|
594
|
+
'passthrough.sh': '#!/bin/sh\ntarget-app "$@"\n'
|
|
595
|
+
}
|
|
596
|
+
},
|
|
597
|
+
permissions: execPerms('/bin/launcher.sh', '/bin/passthrough.sh')
|
|
598
|
+
});
|
|
599
|
+
shell.context.setCommand(TargetApp);
|
|
600
|
+
|
|
601
|
+
await run(shell, 'launcher.sh --blocks -scaling 2');
|
|
602
|
+
await run(shell, 'passthrough.sh "first with spaces" --blocks');
|
|
603
|
+
await run(shell, 'passthrough.sh');
|
|
604
|
+
|
|
605
|
+
expect(received).toEqual([
|
|
606
|
+
['-iwad', '/game/freedoom1.wad', '-nosound', '-blocks', '-scaling', '2'],
|
|
607
|
+
['first with spaces', '--blocks'],
|
|
608
|
+
[]
|
|
609
|
+
]);
|
|
610
|
+
});
|
|
564
611
|
});
|
|
612
|
+
|
package/test/shell.test.js
CHANGED
|
@@ -719,3 +719,63 @@ describe('stream redirection syntax', () => {
|
|
|
719
719
|
});
|
|
720
720
|
});
|
|
721
721
|
|
|
722
|
+
describe('unquoted expansion word splitting', () => {
|
|
723
|
+
let shell;
|
|
724
|
+
let capturedArgs;
|
|
725
|
+
|
|
726
|
+
beforeEach(async () => {
|
|
727
|
+
capturedArgs = [];
|
|
728
|
+
class ArgCaptureCmd extends Shell.Command {
|
|
729
|
+
static get name() { return 'capture'; }
|
|
730
|
+
run() {
|
|
731
|
+
capturedArgs.push([...this.arguments]);
|
|
732
|
+
return Promise.resolve();
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
shell = await createShell();
|
|
736
|
+
shell.context.setCommand(ArgCaptureCmd);
|
|
737
|
+
});
|
|
738
|
+
|
|
739
|
+
it('splits unquoted variable expansions on whitespace and strips leading/trailing spaces', async () => {
|
|
740
|
+
await run(shell, 'EXTRA_ARGS=""');
|
|
741
|
+
await run(shell, 'EXTRA_ARGS="$EXTRA_ARGS -blocks"');
|
|
742
|
+
await run(shell, 'capture -iwad "/path with spaces/freedoom1.wad" -nosound $EXTRA_ARGS');
|
|
743
|
+
expect(capturedArgs).toEqual([
|
|
744
|
+
['-iwad', '/path with spaces/freedoom1.wad', '-nosound', '-blocks']
|
|
745
|
+
]);
|
|
746
|
+
});
|
|
747
|
+
|
|
748
|
+
it('splits multiple flags accumulated in an unquoted variable into separate argv tokens', async () => {
|
|
749
|
+
await run(shell, 'EXTRA_ARGS=" -blocks -scaling 2 "');
|
|
750
|
+
await run(shell, 'capture $EXTRA_ARGS');
|
|
751
|
+
expect(capturedArgs).toEqual([['-blocks', '-scaling', '2']]);
|
|
752
|
+
});
|
|
753
|
+
|
|
754
|
+
it('omits empty unquoted variable expansions while preserving explicit empty quotes ""', async () => {
|
|
755
|
+
await run(shell, 'EMPTY=""');
|
|
756
|
+
await run(shell, 'capture a $EMPTY "" b');
|
|
757
|
+
expect(capturedArgs).toEqual([['a', '', 'b']]);
|
|
758
|
+
});
|
|
759
|
+
|
|
760
|
+
it('preserves whitespace when variable expansion is double-quoted', async () => {
|
|
761
|
+
await run(shell, 'EXTRA_ARGS=" -blocks -scaling 2"');
|
|
762
|
+
await run(shell, 'capture "$EXTRA_ARGS"');
|
|
763
|
+
expect(capturedArgs).toEqual([[' -blocks -scaling 2']]);
|
|
764
|
+
});
|
|
765
|
+
|
|
766
|
+
it('does not word-split bare variable assignments or export assignments', async () => {
|
|
767
|
+
await run(shell, 'SRC=" hello world "');
|
|
768
|
+
await run(shell, 'DEST=$SRC');
|
|
769
|
+
expect(shell.context.getVar('DEST')).toBe(' hello world ');
|
|
770
|
+
|
|
771
|
+
await run(shell, 'export EXPORTED=$(echo alpha beta)');
|
|
772
|
+
expect(shell.context.getVar('EXPORTED')).toBe('alpha beta');
|
|
773
|
+
});
|
|
774
|
+
|
|
775
|
+
it('preserves backslash-escaped spaces on literal tokens without $ expansion', async () => {
|
|
776
|
+
await run(shell, 'capture hello\\ world');
|
|
777
|
+
expect(capturedArgs).toEqual([['hello world']]);
|
|
778
|
+
});
|
|
779
|
+
});
|
|
780
|
+
|
|
781
|
+
|