test(protocol): add deterministic MBR failover-key coverage docs(protocol): align package-bare-os-protocol version to 0.9.1 test(booter): add MBR corruption and wrong-topic smoke fixtures test(peer-seed): add strict pre-MBR bare_os.capabilities negotiation check feat(seeder): validate BARE_OS_SEED_REQUIRE_MBR_LABELS feat(seeder): validate BARE_OS_SEED_CAPABILITY_ATTESTATION_JSON schema docs(boot-policy): add requireProtocolPackageMin 0.9.1 example test(kernel): cover boot.policy denySeedRpcMethods behavior test(protocol): add app/cap/chat/meshdrop channel compatibility fixture test(swarm-disk): cover duplicate Protomux channel null-return path test(protocol): add 11-word kernelCapabilityWords round-trip fixture docs(schema): add mbr-layout schema and validate seeder examples test(protocol): add topicKey() golden hash fixture docs(trust): document block-0 trust assumptions in boot docs feat(seeder): add discovery.flushed readiness logging feat(booter): record peer discovery timings in boot-perf.json feat(integration): add local testnet mode to integration lab smoke test(booter): add Hyperswarm connection-budget env regression coverage test(booter): add swarm plus Corestore suspend/resume integration coverage feat(booter): mirror swarm ban events into host audit logs feat(booter): add direct-peer boot via BARE_OS_BOOT_JOIN_PEER_HEX feat(seeder): pass BARE_OS_SEED_MAX_PEERS to Hyperswarm feat(seeder): log drive.version and discoveryKey at startup test(booter): add Hyperdrive.checkout read-only boot probe coverage feat(booter): prefetch /boot/init.js before kernel handoff feat(booter): add optional /bin warm replication via downloadDiff feat(seeder): add manifestPaths SHA-256 generation in stage-kernel-tree test(peer-seed): cover helper-served block-0 after seeder exit feat(protocol): add Protomux cork batching for initial channel sends test(boot-graph): compare kernel/init labels with booter graph proc docs(boot-policy): add v9-v11 schema examples feat(release): add requireInitJsSha256 fixture generation step test(vfs): add BARE_OS_VFS_SYSTEM_RO_ALIAS coverage test(vfs): strengthen system-drive write-deny path coverage feat(identity): add personal-drive namespace export/import docs and tests test(booter): add guest-to-login warm cache invalidation regression test(vfs): add guest deny coverage for /.bare sensitive paths test(coreutils): add cross-drive mv failure injection coverage test(vfs): add .bareos_empty round-trip coverage across mkdir/rmdir/cp/git-fs test(vfs): add /dev/shm quota enforcement coverage test(proc): add /proc/bare_os/index.json sortedness and schema checks test(vfs): add warm read cache invalidation on replication growth docs(ctx): document bareOsInvalidateWarmReadCaches(reason) test(kernel): add BARE_OS_BOOT_DRY_RUN behavior coverage docs(posix): add dashboard rows for all COREUTILS_COMMANDS feat(curl): expand -w variables beyond http_code/url_effective/size_download feat(wget): mark -N timestamping as explicit unsupported error feat(curl): plumb mutual TLS cert/key intent to ctx.httpFetch metadata feat(shuf): add deterministic seed mode via BARE_OS_SHUF_SEED docs(sort): document -M month-sort as unsupported feat(grep): add explicit -E and -G mode handling test(sed): add Open Group Issue 7 golden fixtures test(awk): add getline VFS regressions for missing/repeat/boundary cases test(shell): add non-interactive here-doc coverage test(shell): add trap delivery coverage for synthetic PIDs/job IDs test(shell): add set -e compound-body behavior coverage docs(shell): strengthen read builtin opt-in guidance test(env): add Bare-runtime coverage for -S and --env-file docs(man): add examples for pathcap-verify pkg-swarm-index corestorectl test(identity): add account/vault backup-restore smoke coverage feat(audit): add tamper detection verification for audit chain rows test(peer-admission): cover strict empty allowlist deny behavior test(peer-admission): add denylist precedence over allowlist coverage test(peer-admission): add BARE_OS_PEER_REQUIRE_CAPS_JSON metadata checks docs(identity): add trusted-key rotation example for path capabilities feat(schema): tighten extensionSignerPinsV2-V4 hash validation test(delegate): add allowlist negative cases for curl/wget/git/hrpc/systemctl test(proc): extend /proc/self/environ redaction key coverage docs(security): add peer-assisted block-0 mirroring threat-model notes feat(bench): add boot budget trend output from real booter phases test(baretop): align fixture coverage with /proc snapshot key set test(metrics): validate /proc/bare_os/metrics.prom OpenMetrics shape docs(ops): add structured seeder NDJSON examples test(replication): add live stall-hint coverage for no_peers/length_unavailable/ok docs(release): add corestore-snapshot workflow to checklist docs(ops): add mirror-drive experiment utility to maintainer workflow test(booter): add monitor progress coverage for replication live sketch feat(seeder): validate DHT bootstrap address class JSON inputs docs(network): add HYPERSWARM_BOOTSTRAP testnet operator guidance chore(root): add deterministic test:integration script docs(ci): add local CI runbook for no-.github environments docs(release): add npm run test:bare after npm test feat(verify): add protocol docs/package version parity checker feat(verify): enforce feature-roadmap canonical path consistency feat(lockfile-drift): add tier-1 strict fail option for mismatches docs(lockfile-drift): add udx-native and blind-peering upgrade workflow notes docs(cli-parity): add bare-fetch upstream issue tracking row feat(bundle-health): generate per-tier bundle size regression thresholds feat(doc-contracts): verify handbook references to current proc schema versions feat(pretest): add validate-mermaid-syntax gate feat(probe): add bare-runtime top-25 critical command lane docs(protocol): update capability-word prose from bits..bits5 to current words docs(two-drive): document /tmp /var/log and account-prefix routing docs(security): add concise boot trust model page and links docs(dev-guide): add P2P lab cookbook section docs(dev-guide): add how-to for adding seed RPCs docs(dev-guide): add how-to for adding /proc/bare_os nodes docs(dev-guide): add /bin utility checklist for man/posix/build/parity/tests docs(user-manual): add short What BareOS is not section
870 lines
23 KiB
Plaintext
870 lines
23 KiB
Plaintext
/* 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<string, unknown>} 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
|
|
/** @type {'basic' | 'extended'} */
|
|
let regexMode = 'basic'
|
|
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|-G|-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':
|
|
regexMode = 'extended'
|
|
fixed = false
|
|
break
|
|
case 'G':
|
|
regexMode = 'basic'
|
|
fixed = false
|
|
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,
|
|
regexMode
|
|
})
|
|
} 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<string, unknown>} 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)
|
|
})
|
|
}
|
|
// Both -E and -G map to JS RegExp semantics in this implementation.
|
|
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):'
|
|
}
|