brsh 2.2.1 → 2.4.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 CHANGED
@@ -1,3 +1,17 @@
1
+ # [2.4.0](https://github.com/eliotstocker/Brsh-JS/compare/v2.3.0...v2.4.0) (2026-05-23)
2
+
3
+
4
+ ### Features
5
+
6
+ * quick pipes imp ([b783c3d](https://github.com/eliotstocker/Brsh-JS/commit/b783c3d781ff41302c3d073c0b9b51f3b321d2a0))
7
+
8
+ # [2.3.0](https://github.com/eliotstocker/Brsh-JS/compare/v2.2.1...v2.3.0) (2026-05-23)
9
+
10
+
11
+ ### Features
12
+
13
+ * add set cmd and allow -c -e etc to brsh-shell ([02be2a6](https://github.com/eliotstocker/Brsh-JS/commit/02be2a68594526d8c6685092afcf4b115b0c033c))
14
+
1
15
  ## [2.2.1](https://github.com/eliotstocker/Brsh-JS/compare/v2.2.0...v2.2.1) (2026-05-23)
2
16
 
3
17
 
@@ -0,0 +1,158 @@
1
+ # POSIX Shell Compatibility Checklist
2
+
3
+ Track progress toward a POSIX-compliant `sh` implementation in Brsh-JS.
4
+
5
+ ---
6
+
7
+ ## Shell Syntax & Built-in Behaviour
8
+
9
+ ### Operators / Redirection
10
+ - [x] Pipes (`|`) — `cmd1 | cmd2`
11
+ - [x] Logical AND (`&&`)
12
+ - [x] Logical OR (`||`)
13
+ - [x] Sequential (`;`)
14
+ - [x] Output redirect (`>`, `>>`)
15
+ - [ ] Input redirect (`<`)
16
+ - [ ] Here-doc (`<<EOF … EOF`)
17
+ - [ ] Here-string (`<<<`)
18
+ - [ ] Process substitution (`<(cmd)`, `>(cmd)`) *(non-POSIX but common)*
19
+
20
+ ### Control Flow
21
+ - [x] `if` / `elif` / `else` / `fi`
22
+ - [x] `while` / `do` / `done`
23
+ - [x] `for var in list; do … done`
24
+ - [x] `case … in … esac`
25
+ - [ ] `break [n]` — exit loop
26
+ - [ ] `continue [n]` — next iteration
27
+ - [ ] Subshell `( cmds )`
28
+
29
+ ### Functions
30
+ - [x] `name() { … }` definition
31
+ - [x] `function name { … }` definition
32
+ - [x] `return [n]`
33
+ - [x] Positional parameters (`$1`, `$2`, … inside function)
34
+ - [ ] `local var` — function-scoped variables
35
+
36
+ ### Variable Expansion
37
+ - [x] `$VAR` / `${VAR}`
38
+ - [x] `$?` — last exit code
39
+ - [x] `$#` — positional parameter count
40
+ - [x] `$1` … `$n` — positional parameters
41
+ - [ ] `$@` — all positional params as separate words
42
+ - [ ] `$*` — all positional params as single word
43
+ - [ ] `$0` — script / shell name
44
+ - [ ] `$$` — current PID *(can be faked)*
45
+ - [ ] `${var:-default}` — use default if unset/empty
46
+ - [ ] `${var:=default}` — assign default if unset/empty
47
+ - [ ] `${var:?error}` — error if unset/empty
48
+ - [ ] `${var:+alt}` — use alt if set
49
+ - [ ] `${#var}` — string length
50
+ - [ ] `${var#pattern}` — strip shortest prefix
51
+ - [ ] `${var##pattern}` — strip longest prefix
52
+ - [ ] `${var%pattern}` — strip shortest suffix
53
+ - [ ] `${var%%pattern}` — strip longest suffix
54
+ - [ ] `${var/from/to}` — replace first *(non-POSIX but common)*
55
+ - [ ] `${var//from/to}` — replace all *(non-POSIX but common)*
56
+
57
+ ### Expansion
58
+ - [x] Command substitution `$(cmd)`
59
+ - [x] Arithmetic expansion `$((expr))`
60
+ - [ ] Tilde expansion (`~`, `~user`)
61
+ - [ ] Filename glob expansion (`*`, `?`, `[a-z]`)
62
+ - [ ] Brace expansion `{a,b,c}` *(non-POSIX but common)*
63
+
64
+ ### Special Built-ins (POSIX required)
65
+ - [x] `exit [n]`
66
+ - [x] `export VAR=val`
67
+ - [x] `return [n]`
68
+ - [x] `set` (flags + positional params)
69
+ - [x] `source` / `. script` *(`.` alias missing — see below)*
70
+ - [x] `.` — dot command (alias for `source`)
71
+ - [ ] `:` — no-op, always exits 0
72
+ - [ ] `break [n]`
73
+ - [ ] `continue [n]`
74
+ - [ ] `shift [n]`
75
+ - [ ] `unset var`
76
+ - [ ] `readonly var`
77
+ - [ ] `eval string`
78
+ - [ ] `exec [cmd]`
79
+ - [ ] `trap cmd sig`
80
+ - [ ] `times`
81
+ - [ ] `wait [pid]`
82
+ - [ ] `getopts optstring var`
83
+
84
+ ### Other Shell Features
85
+ - [x] `set -e` (errexit)
86
+ - [x] `set -x` (xtrace)
87
+ - [ ] `set -u` (nounset — flag stored but not enforced)
88
+ - [ ] `set -n` (noexec — flag stored but not enforced)
89
+ - [ ] `set -o pipefail` *(non-POSIX but common)*
90
+
91
+ ---
92
+
93
+ ## Commands / Utilities
94
+
95
+ ### Text Processing
96
+ - [x] `echo`
97
+ - [x] `printf`
98
+ - [x] `cat`
99
+ - [x] `grep`
100
+ - [ ] `wc`
101
+ - [ ] `head`
102
+ - [ ] `tail`
103
+ - [ ] `sort`
104
+ - [ ] `uniq`
105
+ - [ ] `tr`
106
+ - [ ] `cut`
107
+ - [ ] `tee` *(requires pipe support)*
108
+ - [ ] `sed`
109
+ - [ ] `awk`
110
+
111
+ ### Filesystem
112
+ - [x] `ls`
113
+ - [x] `cp`
114
+ - [x] `mv`
115
+ - [x] `rm`
116
+ - [x] `mkdir`
117
+ - [x] `chmod`
118
+ - [x] `cd` / `pwd`
119
+ - [ ] `touch`
120
+ - [ ] `find`
121
+ - [ ] `ln` (symlinks)
122
+ - [ ] `stat`
123
+
124
+ ### Path Utilities
125
+ - [ ] `basename`
126
+ - [ ] `dirname`
127
+ - [ ] `realpath` *(non-POSIX but common)*
128
+
129
+ ### System / Misc
130
+ - [ ] `date`
131
+ - [ ] `sleep`
132
+ - [ ] `env` / `printenv`
133
+ - [ ] `kill`
134
+ - [ ] `xargs`
135
+ - [ ] `test` (as standalone command) — `[` and `[[` covered by `test` builtin
136
+ - [x] `which`
137
+ - [x] `true`
138
+ - [x] `false`
139
+ - [x] `read`
140
+ - [x] `alias`
141
+ - [x] `exit`
142
+
143
+ ---
144
+
145
+ ## Priority Order
146
+
147
+ 1. **Pipes (`|`)** — unlocks most shell idioms; highest value
148
+ 2. **`break` / `continue`** — small change, very common in loops
149
+ 3. **`shift` / `unset` / `.` / `:`** — small special builtins, high frequency in scripts
150
+ 4. **`$@` / `$*` / `$0`** — needed for argument-forwarding patterns
151
+ 5. **`${var:-default}` parameter expansion forms** — very common in real scripts
152
+ 6. **`wc`, `head`, `tail`, `sort`, `uniq`, `tr`, `cut`** — most-used text utilities
153
+ 7. **`basename` / `dirname`** — common path helpers
154
+ 8. **`local`** — function-scoped variables
155
+ 9. **`date`, `sleep`, `touch`** — misc utilities
156
+ 10. **`set -u` enforcement** — nounset mode
157
+ 11. **Glob expansion** — needed for `ls *.txt` style usage
158
+ 12. **`sed` / `awk`** — complex; consider wasm or partial implementation
package/bin/brsh-shell.js CHANGED
@@ -1,11 +1,56 @@
1
1
  #!/usr/bin/env node
2
2
  'use strict';
3
3
 
4
- // Interactive brsh shell against the real filesystem.
5
- // Run via: npm run shell
4
+ // brsh shell — interactive REPL or non-interactive command runner.
5
+ //
6
+ // Usage:
7
+ // brsh-shell interactive REPL
8
+ // brsh-shell -c "cmd" run a single command and exit
9
+ // brsh-shell script.sh [arg1 ...] run a script file and exit
10
+ // brsh-shell -e -x script.sh run with shell flags active from the start
11
+ // brsh-shell -i -c "cmd" force interactive after running -c
6
12
 
7
13
  const Shell = require('../index.js');
8
14
  const readline = require('readline');
15
+ const nodePath = require('path');
16
+
17
+ // ── CLI argument parsing ──────────────────────────────────────────────────────
18
+
19
+ const argv = process.argv.slice(2);
20
+ let commandString = null; // -c "string"
21
+ let scriptFile = null; // positional script path
22
+ let scriptArgs = []; // args after the script filename
23
+ const shellFlags = []; // single-char flags to activate via `set`
24
+ let forceInteractive = false;
25
+
26
+ for (let i = 0; i < argv.length; i++) {
27
+ const arg = argv[i];
28
+ if (arg === '--') {
29
+ if (scriptFile) scriptArgs = argv.slice(i + 1);
30
+ break;
31
+ }
32
+ if (arg === '-c') {
33
+ commandString = argv[++i] !== undefined ? argv[i] : '';
34
+ continue;
35
+ }
36
+ if (arg === '-i') {
37
+ forceInteractive = true;
38
+ continue;
39
+ }
40
+ if (arg.startsWith('-') && arg.length > 1) {
41
+ // Flags like -e, -x, -u, -n, or combined -ex
42
+ shellFlags.push(...arg.slice(1));
43
+ continue;
44
+ }
45
+ // First non-flag argument is the script file; the rest are its positional args
46
+ scriptFile = arg;
47
+ scriptArgs = argv.slice(i + 1);
48
+ break;
49
+ }
50
+
51
+ const isInteractive = forceInteractive || (commandString === null && scriptFile === null);
52
+
53
+ // ── Shell setup ───────────────────────────────────────────────────────────────
9
54
 
10
55
  const shell = new Shell({
11
56
  useRealFilesystem: true,
@@ -15,16 +60,57 @@ const shell = new Shell({
15
60
 
16
61
  shell.on('stdOut', line => process.stdout.write(line + '\n'));
17
62
  shell.on('stdErr', line => process.stderr.write(line + '\n'));
63
+
18
64
  let destroyed = false;
65
+ let rl;
66
+
19
67
  shell.on('exit', code => {
20
68
  destroyed = true;
21
- rl.close();
69
+ if (rl) rl.close();
22
70
  process.exit(code);
23
71
  });
24
- shell.on('status', status => {
25
- if (status === Shell.STATUS_READY) {
26
- ask();
72
+
73
+ // ── Non-interactive execution ─────────────────────────────────────────────────
74
+
75
+ async function runNonInteractive() {
76
+ // Apply any shell flags (-e, -x, etc.) first
77
+ if (shellFlags.length > 0) {
78
+ await shell.onCommand(`set -${shellFlags.join('')}`, false);
79
+ }
80
+
81
+ if (commandString !== null) {
82
+ // -c mode: run the command string directly
83
+ await shell.onCommand(commandString, false);
84
+ } else {
85
+ // Script file mode: set positional params then run the script
86
+ const absScript = nodePath.resolve(process.cwd(), scriptFile);
87
+ if (scriptArgs.length > 0) {
88
+ const quoted = scriptArgs.map(a => `"${a.replace(/"/g, '\\"')}"`).join(' ');
89
+ await shell.onCommand(`set -- ${quoted}`, false);
90
+ }
91
+ // Use the absolute path; _parseCommand will find it on the real filesystem.
92
+ // If the file lacks execute permission, fall back to source.
93
+ const runCmd = `"${absScript.replace(/"/g, '\\"')}"`;
94
+ await shell.onCommand(runCmd, false).catch(async () => {
95
+ await shell.onCommand(`source "${absScript.replace(/"/g, '\\"')}"`, false);
96
+ });
27
97
  }
98
+
99
+ const exitCode = parseInt(shell.context.getVar('?') || '0', 10);
100
+ process.exit(exitCode);
101
+ }
102
+
103
+ if (!isInteractive) {
104
+ shell.once('status', status => {
105
+ if (status === Shell.STATUS_READY) runNonInteractive();
106
+ });
107
+ return;
108
+ }
109
+
110
+ // ── Interactive mode ──────────────────────────────────────────────────────────
111
+
112
+ shell.on('status', status => {
113
+ if (status === Shell.STATUS_READY) ask();
28
114
  });
29
115
 
30
116
  function prompt() {
@@ -43,7 +129,7 @@ function completer(line) {
43
129
  return [completions, partial];
44
130
  }
45
131
 
46
- const rl = readline.createInterface({
132
+ rl = readline.createInterface({
47
133
  input: process.stdin,
48
134
  output: process.stdout,
49
135
  terminal: true,
@@ -71,3 +157,4 @@ rl.on('line', function(line) {
71
157
  shell.onInput('Enter');
72
158
  }
73
159
  });
160
+