/* BARE_OS_BIN_API 1.0.0 — bump when staged /bin script semantics change (see developer guide). */ /** Shared helpers for drive-resident /bin scripts (prepended before each command). */ function bareStdin(ctx) { return typeof ctx.shellStdin === 'string' ? ctx.shellStdin : '' } /** @param {number} mode @param {'file' | 'directory' | 'symlink'} type */ function bareFormatModeString(mode, type) { const typeChar = type === 'directory' ? 'd' : type === 'symlink' ? 'l' : '-' const perm = mode & 0o777 const r = (bit) => (perm & bit ? 'r' : '-') const w = (bit) => (perm & bit ? 'w' : '-') const x = (bit) => (perm & bit ? 'x' : '-') return ( typeChar + r(0o400) + w(0o200) + x(0o100) + r(0o040) + w(0o020) + x(0o010) + r(0o004) + w(0o002) + x(0o001) ) } /** @param {number} mtimeMs @param {number} [nowMs] */ function bareFormatLsMtime(mtimeMs, nowMs) { const now = nowMs != null ? nowMs : Date.now() const d = new Date(mtimeMs) const months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ] const mon = months[d.getMonth()] const day = String(d.getDate()).padStart(2, ' ') const sixMo = 180 * 24 * 3600 * 1000 if (Math.abs(now - mtimeMs) > sixMo) { const yr = String(d.getFullYear()).padStart(4, ' ') return mon + ' ' + day + ' ' + yr } const hh = String(d.getHours()).padStart(2, '0') const mm = String(d.getMinutes()).padStart(2, '0') return mon + ' ' + day + ' ' + hh + ':' + mm } /** @param {number} size */ function barePosixBlocks(size) { return Math.ceil(Number(size) / 512) || 0 } /** * Raw stdout for NUL/binary when **`process.stdout.write`** is missing. * If **`ctx.bareOsBinWrite(Uint8Array|string)`** is set (tests / host), use it. * @param {Record} ctx * @param {string | Uint8Array} chunk * @returns {boolean} */ function bareOsEmitRaw(ctx, chunk) { if (typeof ctx.bareOsBinWrite === 'function') { const b4 = ctx.b4a const u8 = typeof chunk === 'string' ? b4 && typeof b4.from === 'function' ? b4.from(chunk) : new TextEncoder().encode(chunk) : chunk ctx.bareOsBinWrite(u8 instanceof Uint8Array ? u8 : new Uint8Array(u8)) return true } const w = globalThis.process?.stdout?.write if (typeof w === 'function') { w.call(globalThis.process.stdout, chunk) return true } return false } /** * Subset of POSIX/GNU grep behavior using JavaScript RegExp / string search. * Not bit-identical to GNU grep (no PCRE, different escaping, UTF-16 strings). */ async function run(ctx, argv) { const vfs = ctx.vfs const patterns = [] const patternFiles = [] let fixed = false let icase = false let invert = false let numbers = false let countOnly = false let listFiles = false let quiet = false let suppressErrors = false let forceFilename = false let noFilename = false let word = false let fullLine = false let onlyMatching = false /** @type {number} */ let maxMatchLines = Number.POSITIVE_INFINITY let afterCtx = 0 let beforeCtx = 0 /** @type {'never' | 'always' | 'auto'} */ let colorMode = 'never' let recursive = false /** @type {string[]} */ const includeGlobs = [] /** @type {string[]} */ const excludeGlobs = [] /** @type {string[]} */ const excludeDirGlobs = [] const args = argv.slice(1) let i = 0 function usage() { ctx.console.error( 'usage: grep [-E|-F] [-r] [-i] [-v] [-w] [-x] [-n] [-c] [-l] [-o] [-m NUM] [-A|-B|-C N] [--color=never|always|auto] [-q] [-s] [-H|-h] [-e pat] ... [-f file] ... [pattern] [file...]' ) ctx.exitCode = 2 } while (i < args.length) { const a = args[i] if (a === '--') { i++ break } if (a === '-' || !a.startsWith('-')) break if (a.startsWith('--')) { if (a === '--color' || a.startsWith('--color=')) { const v = a === '--color' ? args[++i] || 'auto' : a.slice('--color='.length) || 'auto' if (v === 'never' || v === 'always' || v === 'auto') colorMode = v else { ctx.console.error('grep: invalid --color value') ctx.exitCode = 2 return } i++ continue } if (a === '--after-context' || a.startsWith('--after-context=')) { const raw = a === '--after-context' ? args[++i] : a.slice('--after-context='.length) const n = Number.parseInt(raw, 10) if (!Number.isFinite(n) || n < 0) { usage() return } afterCtx = n i++ continue } if (a === '--before-context' || a.startsWith('--before-context=')) { const raw = a === '--before-context' ? args[++i] : a.slice('--before-context='.length) const n = Number.parseInt(raw, 10) if (!Number.isFinite(n) || n < 0) { usage() return } beforeCtx = n i++ continue } if (a === '--recursive') { recursive = true i++ continue } if (a === '--include' && args[i + 1]) { includeGlobs.push(args[++i]) i++ continue } if (a.startsWith('--include=')) { includeGlobs.push(a.slice('--include='.length)) i++ continue } if (a === '--exclude' && args[i + 1]) { excludeGlobs.push(args[++i]) i++ continue } if (a.startsWith('--exclude=')) { excludeGlobs.push(a.slice('--exclude='.length)) i++ continue } if (a === '--exclude-dir' && args[i + 1]) { excludeDirGlobs.push(args[++i]) i++ continue } if (a.startsWith('--exclude-dir=')) { excludeDirGlobs.push(a.slice('--exclude-dir='.length)) i++ continue } ctx.console.error('grep: unknown option ' + a) ctx.exitCode = 2 return } if (a === '-A' || /^-A\d+$/.test(a)) { const n = a === '-A' ? Number.parseInt(args[++i], 10) : Number.parseInt(a.slice(2), 10) if (!Number.isFinite(n) || n < 0) { usage() return } afterCtx = n i++ continue } if (a === '-B' || /^-B\d+$/.test(a)) { const n = a === '-B' ? Number.parseInt(args[++i], 10) : Number.parseInt(a.slice(2), 10) if (!Number.isFinite(n) || n < 0) { usage() return } beforeCtx = n i++ continue } if (a === '-C' || /^-C\d+$/.test(a)) { const n = a === '-C' ? Number.parseInt(args[++i], 10) : Number.parseInt(a.slice(2), 10) if (!Number.isFinite(n) || n < 0) { usage() return } beforeCtx = afterCtx = n i++ continue } if (a === '-m') { if (i + 1 >= args.length) { usage() return } const n = Number.parseInt(args[++i], 10) if (!Number.isFinite(n) || n < 0) { ctx.console.error('grep: invalid -m value') ctx.exitCode = 2 return } maxMatchLines = n === 0 ? Number.POSITIVE_INFINITY : n i++ continue } if (/^-m\d+$/.test(a)) { const n = Number.parseInt(a.slice(2), 10) if (!Number.isFinite(n) || n < 0) { ctx.console.error('grep: invalid -m value') ctx.exitCode = 2 return } maxMatchLines = n === 0 ? Number.POSITIVE_INFINITY : n i++ continue } if (a === '-e') { if (i + 1 >= args.length) { usage() return } patterns.push(args[++i]) i++ continue } if (a.startsWith('-e') && a.length > 2) { patterns.push(a.slice(2)) i++ continue } if (a === '-f') { if (i + 1 >= args.length) { usage() return } patternFiles.push(args[++i]) i++ continue } if (a.startsWith('-f') && a.length > 2) { patternFiles.push(a.slice(2)) i++ continue } const rest = a.slice(1) for (let j = 0; j < rest.length; j++) { const c = rest[j] switch (c) { case 'E': /* Accepted for GNU compatibility; patterns use JS RegExp (ERE-like). */ break case 'F': fixed = true break case 'i': icase = true break case 'v': invert = true break case 'n': numbers = true break case 'c': countOnly = true break case 'l': listFiles = true break case 'q': quiet = true break case 's': suppressErrors = true break case 'H': forceFilename = true break case 'h': noFilename = true break case 'w': word = true break case 'x': fullLine = true break case 'o': onlyMatching = true break case 'r': case 'R': recursive = true break case 'm': { let num = '' let jj = j + 1 while (jj < rest.length && /[0-9]/.test(rest[jj])) num += rest[jj++] if (!num) { ctx.console.error('grep: option requires an argument -- m') ctx.exitCode = 2 return } const n = Number.parseInt(num, 10) maxMatchLines = n === 0 ? Number.POSITIVE_INFINITY : n j = jj - 1 break } default: ctx.console.error('grep: invalid option -- ' + c) ctx.exitCode = 2 return } } i++ } for (const pf of patternFiles) { try { const buf = await vfs.readFile(pf) if (!buf) { if (!suppressErrors) ctx.console.error('grep: ' + pf + ': No such file') ctx.exitCode = 2 return } const t = ctx.b4a.toString(buf) for (const line of t.split(/\r?\n/)) { const s = line.trimEnd() if (s === '' || s.startsWith('#')) continue patterns.push(s) } } catch (e) { if (!suppressErrors) ctx.console.error('grep: ' + pf + ': ' + (e.message || e)) ctx.exitCode = 2 return } } const rest = args.slice(i) if (patterns.length === 0) { if (rest.length === 0) { usage() return } patterns.push(rest.shift()) } let fatal = false /** @type {string[]} */ let fileArgs = rest if (recursive) { if (fileArgs.length === 0) fileArgs = ['.'] const acc = [] for (const p of fileArgs) { try { const st = await vfs.lstat(p) if (!st) { if (!suppressErrors) ctx.console.error('grep: ' + p + ': No such file') fatal = true continue } if (st.type === 'directory') { await grepWalkFiles(ctx, p, acc, suppressErrors, { includeGlobs, excludeGlobs, excludeDirGlobs, filterCap: Number.parseInt( String(ctx.vfs?.env?.BARE_OS_GREP_FILTER_MAX || '32'), 10 ) || 32 }) } else { acc.push(p) } } catch (e) { if (!suppressErrors) ctx.console.error('grep: ' + p + ': ' + (e.message || e)) fatal = true } } fileArgs = acc } const useStdin = fileArgs.length === 0 && !recursive const inputs = useStdin ? [{ label: null, path: null }] : fileArgs.map((p) => ({ label: p, path: p })) let matchers try { matchers = buildMatchers(patterns, { fixed, icase, word, fullLine }) } catch (e) { ctx.console.error('grep: ' + (e.message || e)) ctx.exitCode = 2 return } const multiFile = inputs.length > 1 const showName = !noFilename && (multiFile || forceFilename || (useStdin && forceFilename)) const shellEnv = (ctx.vfs && ctx.vfs.env) || ctx.env || {} const useColor = colorMode === 'always' || (colorMode === 'auto' && globalThis.process?.stdout?.isTTY === true && shellEnv.NO_COLOR !== '1' && shellEnv.NO_COLOR !== 'true') const C_RED = '\x1b[01;31m' const C_RST = '\x1b[0m' const useContext = (afterCtx > 0 || beforeCtx > 0) && !countOnly && !listFiles && !quiet && !onlyMatching let anyMatch = false let matchingLinesTotal = 0 for (const { label, path } of inputs) { let text if (path == null) { text = bareStdin(ctx) } else { try { const buf = await vfs.readFile(path) if (!buf) { if (!suppressErrors) ctx.console.error('grep: ' + path + ': No such file') fatal = true continue } text = ctx.b4a.toString(buf) } catch (e) { if (!suppressErrors) ctx.console.error('grep: ' + path + ': ' + (e.message || e)) fatal = true continue } } const lines = splitLines(text) let count = 0 let fileMatched = false const out = [] if (useContext) { const n = lines.length /** @type {boolean[]} */ const isMatch = new Array(n).fill(false) for (let li = 0; li < n; li++) { if (matchingLinesTotal >= maxMatchLines) break const line = stripCr(lines[li]) const matched = matchers.some((fn) => fn(line)) const hit = invert ? !matched : matched isMatch[li] = hit if (!hit) continue matchingLinesTotal++ fileMatched = true anyMatch = true count++ } if (quiet) continue /** @type {number[][]} */ const iv = [] for (let li = 0; li < n; li++) { if (!isMatch[li]) continue iv.push([ Math.max(0, li - beforeCtx), Math.min(n - 1, li + afterCtx) ]) } const merged = grepMergeIntervals(iv) for (let mi = 0; mi < merged.length; mi++) { if (mi > 0) ctx.console.log('--') for (let li = merged[mi][0]; li <= merged[mi][1]; li++) { const line = stripCr(lines[li]) const lineNum = li + 1 let body = line if (useColor && isMatch[li] && !invert) body = C_RED + line + C_RST let chunk = numbers ? lineNum + ':' + body : body if (showName && label != null) chunk = label + ':' + chunk else if (showName && label == null && useStdin) chunk = '(standard input):' + chunk ctx.console.log(chunk) } } continue } for (let li = 0; li < lines.length; li++) { if (matchingLinesTotal >= maxMatchLines) break const line = stripCr(lines[li]) const lineNum = li + 1 const matched = matchers.some((fn) => fn(line)) const hit = invert ? !matched : matched if (!hit) continue matchingLinesTotal++ fileMatched = true anyMatch = true count++ if (quiet) continue if (!countOnly && !listFiles) { if (onlyMatching && !invert) { const parts = extractOnlyMatching(line, patterns, { fixed, icase, word }) for (const part of parts) { let chunk = part if (useColor) chunk = C_RED + chunk + C_RST if (numbers) chunk = lineNum + ':' + chunk if (showName && label != null) chunk = label + ':' + chunk else if (showName && label == null && useStdin) chunk = '(standard input):' + chunk out.push(chunk) } } else { let body = line if (useColor && !invert) body = C_RED + line + C_RST let chunk = numbers ? lineNum + ':' + body : body if (showName && label != null) chunk = label + ':' + chunk else if (showName && label == null && useStdin) chunk = '(standard input):' + chunk out.push(chunk) } } } if (quiet) continue if (listFiles && countOnly) { if (fileMatched) { const prefix = namePrefixForCount(showName, label, useStdin, multiFile) ctx.console.log(prefix + count) } } else if (listFiles) { if (fileMatched) { if (label != null) ctx.console.log(label) else if (useStdin) ctx.console.log('(standard input)') } } else if (countOnly) { const prefix = namePrefixForCount(showName, label, useStdin, multiFile) ctx.console.log(prefix + count) } else { for (const o of out) ctx.console.log(o) } } if (fatal) ctx.exitCode = 2 else ctx.exitCode = anyMatch ? 0 : 1 } const GREP_RECURSE_MAX_DEPTH = 64 /** * @param {Record} ctx * @param {string} dir * @param {string[]} acc * @param {boolean} suppressErrors */ /** * @param {string} name * @param {string} pat */ function grepSimpleGlobMatch(name, pat) { if (!pat || pat === '*') return true if (pat.includes('/')) return name === pat if (pat.startsWith('*') && pat.length > 1 && pat.endsWith('*')) { const mid = pat.slice(1, -1) return mid !== '' && name.includes(mid) } if (pat.startsWith('*')) return name.endsWith(pat.slice(1)) if (pat.endsWith('*')) return name.startsWith(pat.slice(0, -1)) return name === pat } /** * @param {{ includeGlobs?: string[], excludeGlobs?: string[], excludeDirGlobs?: string[], filterCap?: number }} [opts] */ async function grepWalkFiles(ctx, dir, acc, suppressErrors, opts = {}) { const vfs = ctx.vfs const cap = opts.filterCap && opts.filterCap > 0 ? opts.filterCap : 32 const inc = (opts.includeGlobs || []).slice(0, cap) const exc = (opts.excludeGlobs || []).slice(0, cap) const excd = (opts.excludeDirGlobs || []).slice(0, cap) /** @param {string} d @param {number} depth */ const walk = async (d, depth) => { if (depth > GREP_RECURSE_MAX_DEPTH) return let names = [] try { names = await vfs.readdir(d) } catch (e) { if (!suppressErrors) ctx.console.error('grep: ' + d + ': ' + (e.message || e)) return } for (const n of names) { if (n === '.' || n === '..' || n === '.git') continue const sub = d.endsWith('/') ? d + n : d + '/' + n let st try { st = await vfs.lstat(sub) } catch { continue } if (!st) continue if (st.type === 'directory') { if (excd.some((p) => grepSimpleGlobMatch(n, p))) continue await walk(sub, depth + 1) } else if (st.type === 'file') { if (inc.length && !inc.some((p) => grepSimpleGlobMatch(n, p))) continue if (exc.some((p) => grepSimpleGlobMatch(n, p))) continue acc.push(sub) } else if (st.type === 'symlink') { try { const ft = await vfs.stat(sub) if (ft && ft.type === 'file') { if (inc.length && !inc.some((p) => grepSimpleGlobMatch(n, p))) continue if (exc.some((p) => grepSimpleGlobMatch(n, p))) continue acc.push(sub) } } catch { /* skip */ } } } } await walk(dir, 0) } /** @param {number[][]} iv */ function grepMergeIntervals(iv) { if (!iv.length) return [] const copy = iv.map((x) => [x[0], x[1]]) copy.sort((a, b) => a[0] - b[0]) /** @type {number[][]} */ const out = [[copy[0][0], copy[0][1]]] for (let k = 1; k < copy.length; k++) { const cur = copy[k] const last = out[out.length - 1] if (cur[0] <= last[1] + 1) last[1] = Math.max(last[1], cur[1]) else out.push([cur[0], cur[1]]) } return out } /** * @param {string[]} patterns * @param {{ fixed: boolean, icase: boolean, word?: boolean, fullLine?: boolean }} o */ function buildMatchers(patterns, o) { if (patterns.length === 0) throw new Error('no pattern') if (o.fixed) { const pats = o.icase ? patterns.map((p) => p.toLowerCase()) : patterns.slice() return pats.map((p, idx) => { const orig = patterns[idx] if (o.fullLine) { return (line) => { const cmpL = o.icase ? line.toLowerCase() : line const cmpP = o.icase ? p : orig return cmpL === cmpP } } if (o.word) { const esc = p.replace(/[\\^$*+?.()|[\]{}]/g, '\\$&') const flags = o.icase ? 'i' : '' const re = new RegExp( '(?:^|[^0-9A-Za-z_])' + esc + '(?:$|[^0-9A-Za-z_])', flags ) return (line) => re.test(line) } return (line) => o.icase ? line.toLowerCase().includes(p) : line.includes(orig) }) } const flags = o.icase ? 'i' : '' return patterns.map((p) => { try { const body = o.word ? '\\b(?:' + p + ')\\b' : p const wrapped = o.fullLine ? '^(?:' + body + ')$' : body const re = new RegExp(wrapped, flags) return (line) => re.test(line) } catch (e) { throw new Error('invalid regex: ' + (e.message || e)) } }) } /** * @param {string} line * @param {string[]} patterns * @param {{ fixed: boolean, icase: boolean, word: boolean }} o */ function extractOnlyMatching(line, patterns, o) { /** @type {{ start: number, end: number, text: string }[]} */ const raw = [] if (o.fixed) { for (const pat of patterns) { const needle = o.icase ? pat.toLowerCase() : pat const hay = o.icase ? line.toLowerCase() : line let pos = 0 while (pos <= hay.length) { const idx = hay.indexOf(needle, pos) if (idx === -1) break const end = idx + needle.length if (o.word) { const before = idx > 0 ? hay[idx - 1] : ' ' const after = end < hay.length ? hay[end] : ' ' if (/[0-9A-Za-z_]/.test(before) || /[0-9A-Za-z_]/.test(after)) { pos = idx + 1 continue } } raw.push({ start: idx, end, text: line.slice(idx, end) }) pos = idx + 1 } } } else { const flags = o.icase ? 'i' : '' for (const p of patterns) { const body = o.word ? '\\b(?:' + p + ')\\b' : p let re try { re = new RegExp(body, flags + 'g') } catch (e) { throw new Error('invalid regex: ' + (e.message || e)) } let m while ((m = re.exec(line)) !== null) { raw.push({ start: m.index, end: m.index + m[0].length, text: m[0] }) if (m[0] === '') { re.lastIndex++ if (re.lastIndex > line.length) break } } } } raw.sort((a, b) => a.start - b.start || b.end - a.end) /** @type {string[]} */ const out = [] let lastEnd = -1 for (const c of raw) { if (c.start >= lastEnd) { out.push(c.text) lastEnd = c.end } } return out } /** @param {string} text */ function splitLines(text) { if (text === '') return [''] return text.split(/\n/) } /** @param {string} line */ function stripCr(line) { return line.endsWith('\r') ? line.slice(0, -1) : line } /** * @param {boolean} showName * @param {string | null} label * @param {boolean} useStdin * @param {boolean} multiFile */ function namePrefixForCount(showName, label, useStdin, multiFile) { const need = multiFile || (showName && (label != null || useStdin)) if (!need) return '' if (label != null) return label + ':' return '(standard input):' }