1104 lines
29 KiB
Plaintext
1104 lines
29 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
|
|
}
|
|
|
|
/** Session env map (`vfs.env`, then `ctx.env`). Never throws. */
|
|
function bareOsEnv(ctx) {
|
|
const v = ctx && ctx.vfs && ctx.vfs.env
|
|
if (v && typeof v === 'object') return v
|
|
const e = ctx && ctx.env
|
|
if (e && typeof e === 'object') return e
|
|
return {}
|
|
}
|
|
|
|
/**
|
|
* Strict POSIX-ish decimal integer (no octal, no exponent, no empty).
|
|
* @param {unknown} s
|
|
* @returns {number}
|
|
*/
|
|
function bareOsParseDecInt(s) {
|
|
const t = String(s == null ? '' : s).trim()
|
|
if (!/^[+-]?(?:0|[1-9][0-9]*)$/.test(t)) return NaN
|
|
const n = Number.parseInt(t, 10)
|
|
return Number.isSafeInteger(n) ? n : NaN
|
|
}
|
|
|
|
/** @param {unknown} s */
|
|
function bareOsParseNonNegInt(s) {
|
|
const n = bareOsParseDecInt(s)
|
|
return n >= 0 ? n : NaN
|
|
}
|
|
|
|
/**
|
|
* @param {Record<string, unknown>} ctx
|
|
* @param {string} name
|
|
* @param {number} fallback
|
|
* @param {number} [min]
|
|
* @param {number} [max]
|
|
*/
|
|
function bareOsEnvInt(ctx, name, fallback, min, max) {
|
|
const raw = bareOsEnv(ctx)[name]
|
|
if (raw == null || raw === '') return fallback
|
|
const n = Number.parseInt(String(raw), 10)
|
|
if (!Number.isFinite(n)) return fallback
|
|
let v = n
|
|
if (min != null && v < min) v = min
|
|
if (max != null && v > max) v = max
|
|
return v
|
|
}
|
|
|
|
/**
|
|
* @param {Record<string, unknown>} ctx
|
|
* @param {string} msg
|
|
* @param {number} [code]
|
|
*/
|
|
function bareOsFail(ctx, msg, code) {
|
|
if (msg) ctx.console.error(msg)
|
|
ctx.exitCode = code == null ? 1 : code
|
|
}
|
|
|
|
/** @param {unknown} e */
|
|
function bareOsIsNotFoundErr(e) {
|
|
const code = e && typeof e === 'object' ? e.code : ''
|
|
if (code === 'ENOENT') return true
|
|
const msg = String((e && e.message) || e || '')
|
|
return /ENOENT|No such file|not found/i.test(msg)
|
|
}
|
|
|
|
/**
|
|
* @param {Record<string, unknown>} ctx
|
|
* @param {unknown} buf
|
|
* @returns {Uint8Array}
|
|
*/
|
|
function bareOsToU8(ctx, buf) {
|
|
if (!buf) return new Uint8Array(0)
|
|
if (buf instanceof Uint8Array) return buf
|
|
if (ctx && ctx.b4a && typeof ctx.b4a.from === 'function') return ctx.b4a.from(buf)
|
|
return new Uint8Array(buf)
|
|
}
|
|
|
|
/** @param {string} dir @param {string} name */
|
|
function bareOsJoinPath(dir, name) {
|
|
const d = String(dir || '').replace(/\/+$/, '')
|
|
const n = String(name || '').replace(/^\/+/, '')
|
|
if (!d || d === '/') return '/' + n
|
|
return d + '/' + n
|
|
}
|
|
|
|
/** @param {string} p */
|
|
function bareOsBaseName(p) {
|
|
const t = String(p || '').replace(/\/+$/, '')
|
|
if (!t || t === '/') return t === '/' ? '/' : ''
|
|
const i = t.lastIndexOf('/')
|
|
return i < 0 ? t : t.slice(i + 1) || t
|
|
}
|
|
|
|
/** @param {string} p */
|
|
function bareOsParentDir(p) {
|
|
const t = String(p || '').replace(/\/+$/, '') || '/'
|
|
if (t === '/') return '/'
|
|
const i = t.lastIndexOf('/')
|
|
return i <= 0 ? '/' : t.slice(0, i) || '/'
|
|
}
|
|
|
|
/** @param {string} p */
|
|
function bareOsNormPath(p) {
|
|
return String(p || '').replace(/\/+$/, '') || '/'
|
|
}
|
|
|
|
/**
|
|
* @param {Record<string, unknown>} ctx
|
|
* @param {string} p
|
|
*/
|
|
function bareOsResolvePath(ctx, p) {
|
|
if (ctx && ctx.vfs && typeof ctx.vfs.resolveLogical === 'function') {
|
|
try {
|
|
return String(ctx.vfs.resolveLogical(p) || p)
|
|
} catch {
|
|
/* fall through */
|
|
}
|
|
}
|
|
return String(p || '')
|
|
}
|
|
|
|
/**
|
|
* True when dest is src or lives under src (self-copy / self-move).
|
|
* @param {Record<string, unknown>} ctx
|
|
* @param {string} src
|
|
* @param {string} dest
|
|
*/
|
|
function bareOsDestInsideSrc(ctx, src, dest) {
|
|
const s = bareOsNormPath(bareOsResolvePath(ctx, src))
|
|
const d = bareOsNormPath(bareOsResolvePath(ctx, dest))
|
|
if (s === d) return true
|
|
if (s === '/') return d !== '/'
|
|
return d === s || d.startsWith(s + '/')
|
|
}
|
|
|
|
const BARE_OS_B64_ALPH =
|
|
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
|
|
|
|
/** @param {Uint8Array} u8 */
|
|
function bareOsB64Encode(u8) {
|
|
let out = ''
|
|
let i = 0
|
|
for (; i + 2 < u8.length; i += 3) {
|
|
const n = (u8[i] << 16) | (u8[i + 1] << 8) | u8[i + 2]
|
|
out +=
|
|
BARE_OS_B64_ALPH[(n >> 18) & 63] +
|
|
BARE_OS_B64_ALPH[(n >> 12) & 63] +
|
|
BARE_OS_B64_ALPH[(n >> 6) & 63] +
|
|
BARE_OS_B64_ALPH[n & 63]
|
|
}
|
|
const rest = u8.length - i
|
|
if (rest === 1) {
|
|
const n = u8[i] << 16
|
|
out += BARE_OS_B64_ALPH[(n >> 18) & 63] + BARE_OS_B64_ALPH[(n >> 12) & 63] + '=='
|
|
} else if (rest === 2) {
|
|
const n = (u8[i] << 16) | (u8[i + 1] << 8)
|
|
out +=
|
|
BARE_OS_B64_ALPH[(n >> 18) & 63] +
|
|
BARE_OS_B64_ALPH[(n >> 12) & 63] +
|
|
BARE_OS_B64_ALPH[(n >> 6) & 63] +
|
|
'='
|
|
}
|
|
return out
|
|
}
|
|
|
|
/**
|
|
* RFC 4648 Base64 decode (also accepts URL-safe alphabet). Rejects junk.
|
|
* @param {string} s
|
|
* @returns {Uint8Array}
|
|
*/
|
|
function bareOsB64Decode(s) {
|
|
const t = String(s).replace(/\s+/g, '')
|
|
if (!t) return new Uint8Array(0)
|
|
if (t.length % 4 === 1) throw new Error('invalid base64 length')
|
|
let pad = 0
|
|
if (t.endsWith('==')) pad = 2
|
|
else if (t.endsWith('=')) pad = 1
|
|
const body = pad ? t.slice(0, t.length - pad) : t
|
|
const bytes = []
|
|
let buf = 0
|
|
let bits = 0
|
|
for (let i = 0; i < body.length; i++) {
|
|
const c = body[i]
|
|
let v = BARE_OS_B64_ALPH.indexOf(c)
|
|
if (v < 0) {
|
|
if (c === '-') v = 62
|
|
else if (c === '_') v = 63
|
|
else throw new Error('invalid base64 character')
|
|
}
|
|
buf = (buf << 6) | v
|
|
bits += 6
|
|
if (bits >= 8) {
|
|
bits -= 8
|
|
bytes.push((buf >> bits) & 255)
|
|
}
|
|
}
|
|
if (pad) {
|
|
const want = Math.floor((body.length * 6) / 8)
|
|
if (bytes.length > want) bytes.length = want
|
|
}
|
|
return new Uint8Array(bytes)
|
|
}
|
|
|
|
/**
|
|
* @param {string} s
|
|
* @returns {Uint8Array}
|
|
*/
|
|
function bareOsHexDecode(s) {
|
|
const t = String(s).replace(/\s+/g, '')
|
|
if (t.length % 2 !== 0) throw new Error('odd hex length')
|
|
const out = new Uint8Array(t.length / 2)
|
|
for (let i = 0; i < out.length; i++) {
|
|
const pair = t.slice(i * 2, i * 2 + 2)
|
|
if (!/^[0-9a-fA-F]{2}$/.test(pair)) throw new Error('invalid hex')
|
|
out[i] = Number.parseInt(pair, 16)
|
|
}
|
|
return out
|
|
}
|
|
|
|
/** @param {Uint8Array} u8 */
|
|
function bareOsHexEncode(u8) {
|
|
let s = ''
|
|
for (let i = 0; i < u8.length; i++) s += u8[i].toString(16).padStart(2, '0')
|
|
return s
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
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
|
|
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
|
|
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') {
|
|
const walkErr = await grepWalkFiles(ctx, p, acc, suppressErrors, {
|
|
includeGlobs,
|
|
excludeGlobs,
|
|
excludeDirGlobs,
|
|
filterCap: Number.parseInt(
|
|
String(ctx.vfs?.env?.BARE_OS_GREP_FILTER_MAX || '32'),
|
|
10
|
|
) || 32
|
|
})
|
|
if (walkErr) fatal = true
|
|
} 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 */
|
|
let hadError = false
|
|
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))
|
|
hadError = true
|
|
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 (e) {
|
|
if (!suppressErrors)
|
|
ctx.console.error('grep: ' + sub + ': ' + (e.message || e))
|
|
hadError = true
|
|
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 {
|
|
hadError = true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
await walk(dir, 0)
|
|
return hadError
|
|
}
|
|
|
|
/** @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 ['']
|
|
const lines = text.split(/\n/)
|
|
if (text.endsWith('\n') && lines.length && lines[lines.length - 1] === '')
|
|
lines.pop()
|
|
return lines.length ? lines : ['']
|
|
}
|
|
|
|
/** @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):'
|
|
}
|