/* 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 } /** * 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 const args = argv.slice(1) let i = 0 function usage() { ctx.console.error( 'usage: grep [-E|-F] [-i] [-v] [-w] [-x] [-n] [-c] [-l] [-o] [-m NUM] [-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('--')) { ctx.console.error('grep: unknown option ' + a) ctx.exitCode = 2 return } 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 '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()) } const fileArgs = rest const useStdin = fileArgs.length === 0 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)) let anyMatch = false let fatal = 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 = [] 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 (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 chunk = line 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) } } } 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 } /** * @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):' }