diff options
Diffstat (limited to 'modules/ext')
| -rw-r--r-- | modules/ext/critical-ext-bundle.js | 28 | ||||
| -rw-r--r-- | modules/ext/shell.js | 28 |
2 files changed, 44 insertions, 12 deletions
diff --git a/modules/ext/critical-ext-bundle.js b/modules/ext/critical-ext-bundle.js index f892617..d241f33 100644 --- a/modules/ext/critical-ext-bundle.js +++ b/modules/ext/critical-ext-bundle.js @@ -857,7 +857,12 @@ class ShellModule { const dirPath = '/' + rawDir.map(s => String(s).split('/').filter(Boolean).join('/')) .filter(Boolean).join('/'); let entries = []; - try { entries = this.terminal.fs.ls(dirPath) || []; } catch (e) { entries = []; } + try { + const fsv = this.terminal.fs; + // ls() is the canonical lister; readDir() is the fallback so the helper also works + // against a bare fake fs (and against any future module that only implements one). + entries = (fsv && (fsv.ls ? fsv.ls(dirPath) : null)) || (fsv && fsv.readDir ? fsv.readDir(dirPath) : null) || []; + } catch (e) { entries = []; } const re = globToRe(namePat); const hits = entries.filter(e => re.test(e.name)).map(e => (dirPath === '/' ? '' : dirPath) + '/' + e.name); return hits.length ? hits : [a]; @@ -869,7 +874,11 @@ class ShellModule { // and the bracket spelling of the same. Anything else is false, never a crash. _evalTest(expr, env) { const t = this._expandVars(String(expr == null ? '' : expr), env || {}) - .trim().replace(/^\[/, '').replace(/\]$/, '').trim(); + .trim() + .replace(/^\[\s*/, '') // `[ -f x ]` + .replace(/\s*\]$/, '') + .replace(/^test\s+/, '') // `test -f x` — without this every `if test` was false + .trim(); if (!t) return false; const fs = this.terminal.fs; const parts = t.split(/\s+/); @@ -891,10 +900,17 @@ class ShellModule { let ran = 0; for (const node of nodes) { if (node.op === 'for') { - for (const item of node.items) { - env[node.var] = this._expandVars(item, env); - const r = await this._runInstructions(node.body, env, ctx); - ran += r.ran + 1; + for (const rawItem of node.items) { + const expanded = this._expandVars(rawItem, env); + // `for f in /work/*.txt` used to run ONCE with $f set to the literal pattern, + // because only command arguments went through glob expansion. + const cwd = (this.terminal.fs && this.terminal.fs.currentPath) || []; + const items = this._expandGlobs([expanded], cwd); + for (const item of items) { + env[node.var] = item; + const r = await this._runInstructions(node.body, env, ctx); + ran += r.ran + 1; + } } continue; } diff --git a/modules/ext/shell.js b/modules/ext/shell.js index ad66e63..5784ef3 100644 --- a/modules/ext/shell.js +++ b/modules/ext/shell.js @@ -721,7 +721,12 @@ class ShellModule { const dirPath = '/' + rawDir.map(s => String(s).split('/').filter(Boolean).join('/')) .filter(Boolean).join('/'); let entries = []; - try { entries = this.terminal.fs.ls(dirPath) || []; } catch (e) { entries = []; } + try { + const fsv = this.terminal.fs; + // ls() is the canonical lister; readDir() is the fallback so the helper also works + // against a bare fake fs (and against any future module that only implements one). + entries = (fsv && (fsv.ls ? fsv.ls(dirPath) : null)) || (fsv && fsv.readDir ? fsv.readDir(dirPath) : null) || []; + } catch (e) { entries = []; } const re = globToRe(namePat); const hits = entries.filter(e => re.test(e.name)).map(e => (dirPath === '/' ? '' : dirPath) + '/' + e.name); return hits.length ? hits : [a]; @@ -733,7 +738,11 @@ class ShellModule { // and the bracket spelling of the same. Anything else is false, never a crash. _evalTest(expr, env) { const t = this._expandVars(String(expr == null ? '' : expr), env || {}) - .trim().replace(/^\[/, '').replace(/\]$/, '').trim(); + .trim() + .replace(/^\[\s*/, '') // `[ -f x ]` + .replace(/\s*\]$/, '') + .replace(/^test\s+/, '') // `test -f x` — without this every `if test` was false + .trim(); if (!t) return false; const fs = this.terminal.fs; const parts = t.split(/\s+/); @@ -755,10 +764,17 @@ class ShellModule { let ran = 0; for (const node of nodes) { if (node.op === 'for') { - for (const item of node.items) { - env[node.var] = this._expandVars(item, env); - const r = await this._runInstructions(node.body, env, ctx); - ran += r.ran + 1; + for (const rawItem of node.items) { + const expanded = this._expandVars(rawItem, env); + // `for f in /work/*.txt` used to run ONCE with $f set to the literal pattern, + // because only command arguments went through glob expansion. + const cwd = (this.terminal.fs && this.terminal.fs.currentPath) || []; + const items = this._expandGlobs([expanded], cwd); + for (const item of items) { + env[node.var] = item; + const r = await this._runInstructions(node.body, env, ctx); + ran += r.ran + 1; + } } continue; } |